[petsc-users] Calling MATLAB code within a parallel C/C++ code

Barry Smith bsmith at petsc.dev
Thu Jun 11 20:21:51 CDT 2020



> On Jun 11, 2020, at 2:47 PM, Amneet Bhalla <mail2amneet at gmail.com> wrote:
> 
> Ok, I'm able to call and use MATLAB functionality from PETSc. If I'm understanding correctly, when PetscMatlabEngineCreate() is called, a MATLAB workspace is created, that persists until PetscMatlabEngineDestroy() is called. PETSc can access/put/manipulate variables in this workspace, and can also call several MATLAB scripts and MATLAB functions, and subsequently access/manipulate new data created in the existing workspace. Is there something else that I should keep in mind to take advantage of Engine functionality or to avoid pitfalls. Also are there any limitations on MATLAB toolboxes that cannot be used via the Engine framework? I don't anticipate using any graphical interface functionality via MatlabEngine.  

  Yes this is all correct. It seems the Matlab engine is Matlab so it provides all the functionality of standalone Matlab. 

   You can use Matlab graphics as usual if you run your program with the additional option "-matlab_engine_graphics   We just had it off by default because sometimes Matlab Engine may have trouble access the users graphics/window system but hopefully it won't be a problem

    Barry


>   
>  
> 
> On Tue, Jun 9, 2020 at 4:26 PM Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>> wrote:
> 
>   Great, yes you should not need the DYLIB paths
> 
>   The branch is off master but what you can do is copy the config/BuildSystem/config/MatlabEngine.py to your PETSc 3.12.2 version and the configure 
> should work (it won't automatically run the test case but you can test it manually).
> 
>    Barry
> 
> 
>> On Jun 9, 2020, at 5:48 PM, Amneet Bhalla <mail2amneet at gmail.com <mailto:mail2amneet at gmail.com>> wrote:
>> 
>> That worked!
>> 
>> amneetb at Amneets-MBP:~/Softwares/PETSc-Gitlab/petsc-matlab$ make PETSC_DIR=/Users/amneetb/Softwares/PETSc-Gitlab/petsc-matlab PETSC_ARCH=darwin-dbg check
>> Running check examples to verify correct installation
>> Using PETSC_DIR=/Users/amneetb/Softwares/PETSc-Gitlab/petsc-matlab and PETSC_ARCH=darwin-dbg
>> C/C++ example src/snes/tutorials/ex19 run successfully with 1 MPI process
>> C/C++ example src/snes/tutorials/ex19 run successfully with 2 MPI processes
>> C/C++ example src/snes/tutorials/ex19 run successfully with hypre
>> Fortran example src/snes/tutorials/ex5f run successfully with 1 MPI process
>> C/C++ example src/vec/vec/tutorials/ex31 run successfully with Matlab engine
>> Completed test examples
>> 
>> Should I remove DYLIB paths from .profile since these are now baked in the linker?
>> 
>> Also, Is this branch based-off of latest master? I'm currently using PETSc v3.12.2 for IBAMR. Anything close to this version should work for me.
>> 
>> 
>> On Tue, Jun 9, 2020 at 3:17 PM Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>> wrote:
>> 
>>    I tried including the directories into my DYLD environmental variables and it didn't work; I am not sure why. This might be why https://stackoverflow.com/questions/35568122/why-isnt-dyld-library-path-being-propagated-here <https://stackoverflow.com/questions/35568122/why-isnt-dyld-library-path-being-propagated-here>
>> 
>>    Anyways try my branch and that should work.
>> 
>>    Barry
>> 
>> 
>> 
>>> On Jun 9, 2020, at 5:09 PM, Amneet Bhalla <mail2amneet at gmail.com <mailto:mail2amneet at gmail.com>> wrote:
>>> 
>>> 
>>> That's where I also found it. Thanks, Sanjay!
>>> 
>>> (This path is included in my .profile file
>>> 
>>> export DYLD_FALLBACK_LIBRARY_PATH=/Applications/MATLAB_R2019b.app/bin/maci64:/Applications/MATLAB_R2019b.app/sys/os/maci64:$DYLD_FALLBACK_LIBRARY_PATH
>>> 
>>> export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2019b.app/bin/maci64:/Applications/MATLAB_R2019b.app/sys/os/maci64:$DYLD_LIBRARY_PATH
>>> 
>>> 
>>> 
>>> )
>>> 
>>> Do I need to tell PETSc about this path as well? Something like:
>>> 
>>> --with-matlab-engine-lib=/Applications/MATLAB_R2019b.app/bin/maci64/libeng.dylib
>>> 
>>> 
>>> On Tue, Jun 9, 2020 at 2:56 PM Sanjay Govindjee <s_g at berkeley.edu <mailto:s_g at berkeley.edu>> wrote:
>>> Try looking in:
>>> 
>>> $(MLROOT)/bin/maci64
>>> 
>>> where MLROOT = /Applications/MATLAB_R2019a.app or the like
>>> 
>>> 
>>> On 6/9/20 2:51 PM, Junchao Zhang wrote:
>>>> Find recursively where is libeng.dylib under /Applications/MATLAB_R2019b.app/extern/engines/
>>>> 
>>>> --Junchao Zhang
>>>> 
>>>> 
>>>> On Tue, Jun 9, 2020 at 4:27 PM Amneet Bhalla <mail2amneet at gmail.com <mailto:mail2amneet at gmail.com>> wrote:
>>>> Forgot to mention that configure found the following about MATLAB
>>>> 
>>>> Matlab:
>>>>   Includes: -I/Applications/MATLAB_R2019b.app/extern/include
>>>>   /Applications/MATLAB_R2019b.app
>>>> MatlabEngine:
>>>>   Library:  -L/Applications/MATLAB_R2019b.app/bin/maci64 -L/Applications/MATLAB_R2019b.app/extern/lib/maci64 -leng -lmex -lmx -lmat -lut -licudata -licui18n -licuuc
>>>> 
>>>> On Tue, Jun 9, 2020 at 2:20 PM Amneet Bhalla <mail2amneet at gmail.com <mailto:mail2amneet at gmail.com>> wrote:
>>>> Do these additional configure flags for MATLAB look OK?
>>>> 
>>>> --with-matlab=1 --with-matlab-dir=/Applications/MATLAB_R2019b.app --with-matlab-engine=1  --with-matlab-engine-dir=/Applications/MATLAB_R2019b.app/extern/engines/
>>>> 
>>>> 
>>>> 
>>>> With this configuration make compiled files like:
>>>> 
>>>>           CC darwin-dbg/obj/sys/classes/viewer/impls/matlab/vmatlab.o
>>>>           CC darwin-dbg/obj/sys/classes/viewer/impls/matlab/ftn-custom/zvmatlabf.o
>>>>           CC darwin-dbg/obj/sys/classes/matlabengine/matlab.o
>>>>           CC darwin-dbg/obj/sys/classes/matlabengine/ftn-auto/matlabf.o
>>>>           CC darwin-dbg/obj/sys/classes/matlabengine/ftn-custom/zmatlabf.o
>>>>           CC darwin-dbg/obj/vec/pf/impls/matlab/cmatlab.o
>>>>           CC darwin-dbg/obj/vec/vec/utils/matlab/gcreatev.o
>>>> 
>>>> make[2]: Leaving directory `/Users/amneetb/Softwares/PETSc-Gitlab/PETSc'
>>>> BEGINNING TO COMPILE MATLAB INTERFACE
>>>> Building with 'Xcode with Clang'.
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> MEX completed successfully.
>>>> Building with 'Xcode with Clang'.
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> MEX completed successfully.
>>>> Building with 'Xcode with Clang'.
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> MEX completed successfully.
>>>> Building with 'Xcode with Clang'.
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> ld: warning: cannot export hidden symbol _mexfilerequiredapiversion from c_mexapi_version.o
>>>> 
>>>> MEX completed successfully.
>>>> 
>>>> 
>>>> However, make check gives error
>>>> 
>>>> amneetb at Amneets-MBP:~/Softwares/PETSc-Gitlab/PETSc$ make PETSC_DIR=/Users/amneetb/Softwares/PETSc-Gitlab/PETSc PETSC_ARCH=darwin-dbg check
>>>> Running test examples to verify correct installation
>>>> Using PETSC_DIR=/Users/amneetb/Softwares/PETSc-Gitlab/PETSc and PETSC_ARCH=darwin-dbg
>>>> Possible error running C/C++ src/snes/examples/tutorials/ex19 with 1 MPI process
>>>> See http://www.mcs.anl.gov/petsc/documentation/faq.html <http://www.mcs.anl.gov/petsc/documentation/faq.html>
>>>> dyld: Library not loaded: @rpath/libeng.dylib
>>>>   Referenced from: /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials/./ex19
>>>>   Reason: image not found
>>>> 
>>>> ===================================================================================
>>>> =   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
>>>> =   PID 14057 RUNNING AT Amneets-MBP
>>>> =   EXIT CODE: 6
>>>> =   CLEANING UP REMAINING PROCESSES
>>>> =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
>>>> ===================================================================================
>>>> YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Abort trap: 6 (signal 6)
>>>> This typically refers to a problem with your application.
>>>> Please see the FAQ page for debugging suggestions
>>>> Possible error running C/C++ src/snes/examples/tutorials/ex19 with 2 MPI processes
>>>> See http://www.mcs.anl.gov/petsc/documentation/faq.html <http://www.mcs.anl.gov/petsc/documentation/faq.html>
>>>> dyld: Library not loaded: @rpath/libeng.dylib
>>>>   Referenced from: /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials/./ex19
>>>>   Reason: image not found
>>>> dyld: Library not loaded: @rpath/libeng.dylib
>>>>   Referenced from: /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials/./ex19
>>>>   Reason: image not found
>>>> 
>>>> ===================================================================================
>>>> =   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
>>>> =   PID 14062 RUNNING AT Amneets-MBP
>>>> =   EXIT CODE: 6
>>>> =   CLEANING UP REMAINING PROCESSES
>>>> =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
>>>> ===================================================================================
>>>> YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Abort trap: 6 (signal 6)
>>>> This typically refers to a problem with your application.
>>>> Please see the FAQ page for debugging suggestions
>>>> 1,5c1,17
>>>> < lid velocity = 0.0016, prandtl # = 1., grashof # = 1.
>>>> <   0 SNES Function norm 0.0406612 
>>>> <   1 SNES Function norm 4.12227e-06 
>>>> <   2 SNES Function norm 6.098e-11 
>>>> < Number of SNES iterations = 2
>>>> ---
>>>> > dyld: Library not loaded: @rpath/libeng.dylib
>>>> >   Referenced from: /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials/./ex19
>>>> >   Reason: image not found
>>>> > dyld: Library not loaded: @rpath/libeng.dylib
>>>> >   Referenced from: /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials/./ex19
>>>> >   Reason: image not found
>>>> > 
>>>> > ===================================================================================
>>>> > =   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
>>>> > =   PID 14072 RUNNING AT Amneets-MBP
>>>> > =   EXIT CODE: 6
>>>> > =   CLEANING UP REMAINING PROCESSES
>>>> > =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
>>>> > ===================================================================================
>>>> > YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Abort trap: 6 (signal 6)
>>>> > This typically refers to a problem with your application.
>>>> > Please see the FAQ page for debugging suggestions
>>>> /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials
>>>> Possible problem with ex19 running with hypre, diffs above
>>>> =========================================
>>>> Possible error running Fortran example src/snes/examples/tutorials/ex5f with 1 MPI process
>>>> See http://www.mcs.anl.gov/petsc/documentation/faq.html <http://www.mcs.anl.gov/petsc/documentation/faq.html>
>>>> dyld: Library not loaded: @rpath/libeng.dylib
>>>>   Referenced from: /Users/amneetb/Softwares/PETSc-Gitlab/PETSc/src/snes/examples/tutorials/./ex5f
>>>>   Reason: image not found
>>>> 
>>>> ===================================================================================
>>>> =   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
>>>> =   PID 14211 RUNNING AT Amneets-MBP
>>>> =   EXIT CODE: 6
>>>> =   CLEANING UP REMAINING PROCESSES
>>>> =   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
>>>> ===================================================================================
>>>> YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Abort trap: 6 (signal 6)
>>>> This typically refers to a problem with your application.
>>>> Please see the FAQ page for debugging suggestions
>>>> Completed test examples
>>>> 
>>>> 
>>>> I have added the following paths in my .profile and sourced it before make all and make check 
>>>> 
>>>> export DYLD_FALLBACK_LIBRARY_PATH=/Applications/MATLAB_R2019b.app/bin/maci64:/Applications/MATLAB_R2019b.app/sys/os/maci64:$DYLD_FALLBACK_LIBRARY_PATH
>>>> 
>>>> export DYLD_LIBRARY_PATH=/Applications/MATLAB_R2019b.app/bin/maci64:/Applications/MATLAB_R2019b.app/sys/os/maci64:$DYLD_LIBRARY_PATH
>>>> 
>>>> 
>>>> 
>>>> On Mon, Jun 8, 2020 at 6:39 PM Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>> wrote:
>>>> 
>>>>   Looks like the tool to list the examples in the manual pages is broken since it didn't find these other examples, I'll take a look at it.
>>>> 
>>>>   There is also a discussion in the users manual
>>>> 
>>>> $ git grep MatlabEngine | grep ex
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEngineCreate(MPI_Comm comm,char *machine,PetscMatlabEngine *e);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEnginePut(PetscMatlabEngine e,PetscObject obj);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEngineGet(PetscMatlabEngine e,PetscObject obj);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEnginePutArray(PetscMatlabEngine e,int m,int n,PetscScalar *array,char *name);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEngineGetArray(PetscMatlabEngine e,int m,int n,PetscScalar *array,char *name);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEngineEvaluate(PetscMatlabEngine,"format",...);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEngineEvaluate(PetscMatlabEngine,"x = \%g *y + z;",avalue);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEngineGetOutput(PetscMatlabEngine,char **);
>>>> src/docs/tex/manual/part2.tex:PetscMatlabEnginedPrintOutput(PetscMatlabEngine,FILE*).
>>>> src/snes/tutorials/ex5.c:  ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localX);CHKERRQ(ierr);
>>>> src/snes/tutorials/ex5.c:  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(comm),"localF=ex5m(localX,%18.16e,%18.16e,%18.16e)",hx,hy,lambda);CHKERRQ(ierr);
>>>> src/snes/tutorials/ex5.c:  ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_(comm),(PetscObject)localF);CHKERRQ(ierr);
>>>> src/sys/tests/ex16.c:static char help[] = "Demonstrates PetscMatlabEngineXXX()\n";
>>>> src/sys/tests/ex16.c:  ierr = PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),&output);CHKERRQ(ierr);
>>>> src/sys/tests/ex16.c:  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),"MPI_Comm_rank");CHKERRQ(ierr);
>>>> src/sys/tests/ex16.c:    ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_(PETSC_COMM_WORLD),user);CHKERRQ(ierr);
>>>> src/vec/vec/tests/ex31.c:static const char help[] = "Demonstrates PetscMatlabEngineXXX()\n";
>>>> src/vec/vec/tests/ex31.c:  ierr = PetscMatlabEngineGetOutput(PETSC_MATLAB_ENGINE_WORLD,&output);
>>>> src/vec/vec/tests/ex31.c:  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD,"MPI_Comm_rank");
>>>> src/vec/vec/tests/ex31.c:  ierr = PetscMatlabEnginePut(PETSC_MATLAB_ENGINE_WORLD,(PetscObject)x);CHKERRQ(ierr);
>>>> src/vec/vec/tests/ex31.c:  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD,"x = x + MPI_Comm_rank;\n");
>>>> src/vec/vec/tests/ex31.c:  ierr = PetscMatlabEngineGet(PETSC_MATLAB_ENGINE_WORLD,(PetscObject)x);CHKERRQ(ierr);
>>>> src/vec/vec/tests/ex31.c:  ierr = PetscMatlabEngineEvaluate(PETSC_MATLAB_ENGINE_WORLD,"whos\n");CHKERRQ(ierr);
>>>> 
>>>>> On Jun 8, 2020, at 8:26 PM, Amneet Bhalla <mail2amneet at gmail.com <mailto:mail2amneet at gmail.com>> wrote:
>>>>> 
>>>>> Great!
>>>>> 
>>>>> Clicking on some of the functions pointed me to this example
>>>>> https://www.mcs.anl.gov/petsc/petsc-3.13/src/snes/tutorials/ex5.c.html <https://www.mcs.anl.gov/petsc/petsc-3.13/src/snes/tutorials/ex5.c.html>
>>>>> 
>>>>> Are there more examples, which are perhaps simpler than the above, which I can refer to? 
>>>>> 
>>>>> On Mon, Jun 8, 2020 at 5:51 PM Barry Smith <bsmith at petsc.dev <mailto:bsmith at petsc.dev>> wrote:
>>>>> 
>>>>>   You should use the PetscMatlabEngine https://www.mcs.anl.gov/petsc/petsc-3.13/docs/manualpages/Sys/PetscMatlabEngine.html <https://www.mcs.anl.gov/petsc/petsc-3.13/docs/manualpages/Sys/PetscMatlabEngine.html>
>>>>> 
>>>>>   Barry
>>>>> 
>>>>> 
>>>>>> On Jun 8, 2020, at 7:09 PM, Amneet Bhalla <mail2amneet at gmail.com <mailto:mail2amneet at gmail.com>> wrote:
>>>>>> 
>>>>>> Hi Folks,
>>>>>> 
>>>>>> My parallel application written in C++ (which also uses PETSc library for linear solvers) requires calling control and optimization toolbox of MATLAB at every time step to get a small number of double values. Likewise the MATLAB function will need to know some values from the parallel C++ application to return those doubles. I'm wondering if there is a recommended way of calling MATLAB from parallel C/C++/Fortran codes, and if PETSc has some interface that could be leveraged for this exchange. 
>>>>>> 
>>>>>> Thanks,
>>>>>> -- 
>>>>>> --Amneet 
>>>>>> 
>>>>>> 
>>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> -- 
>>>>> --Amneet 
>>>>> 
>>>>> 
>>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> --Amneet 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> --Amneet 
>>>> 
>>>> 
>>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> --Amneet 
>>> 
>>> 
>>> 
>> 
>> 
>> 
>> -- 
>> --Amneet 
>> 
>> 
>> 
> 
> 
> 
> -- 
> --Amneet 
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20200611/6adc28ee/attachment-0001.html>


More information about the petsc-users mailing list