I have OS X El Capitan and Matlab R2016a and I would like to use OpenMP, which has previously worked. I have managed to install gcc-5 via homebrew and have openmp working there. I can see from this thread GCC C/C++ MEX Matlab R2015 Mac OS X (with OpenMP) doesn't work that at least in R2014a, it was possible to insert mexopts.sh manually and edit it. However, I do not have such a file to use in order to redirect the compiler flag (CC) to point at the gcc-5 compiler that works with the -fopenmp flag.
Any suggestions? Am I going entirely the wrong way?
Update for macOS 10.12, MATLAB 2016b: The general principle of the following still holds true. Scroll down for changes I had to make.
clang
has been updated to include OpenMP support, however, the version installed with Xcode 7.3.1 does not support OpenMP yet. Instead, it can be installed withhomebrew
. The previous solution usingclang-omp
does not work anymore, becauseclang-omp
has been removed frombrew
.The following process has been tested with Mac OS X 10.11.6, clang 3.8.1, MATLAB 2016a.
Install
llvm
Install the current version of
llvm
frombrew
(fromTerminal
):Modify MATLAB build configuration
Then, copy a default
mex
compiler configuration file to your MATLAB configuration directory. To find the default configuration, entermex -setup C++
on your MATLAB prompt. One option should read something like(Here and later we will assume MATLAB 2016a is the version used). Copy
/Applications/Matlab/MATLAB_R2016a/MATLAB_R2016a.app/bin/maci64/mexopts/clang++_maci64.xml
to~/.matlab/R2016a
, and name itclang++_openmp_maci64.xml
to be able to tell it apart. Openclang++_openmp_maci64.xml
in a text editor and modify it as follows: At the beginning, enter different values for name etc, again to be able to tell it apart:Change the definition of
CXX
to point to the location ofclang
as installed withbrew
. At the default location, this should beAdd
-fopenmp -Wall -I/usr/local/opt/llvm/include
toCXXFLAGS
, so it will readAdd
-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib -fopenmp
toLDFLAGS
so it is similar toTo activate this build configuration, enter at the MATLAB prompt:
Modify MATLAB runtime variables
Now, MATLAB has to know about the locations of the libraries at runtime. Check if you have a file
~/.matlab7rc.sh
. If not, copy the template frommatlabroot/bin
into your home directory. Open this file in a text editor. Scroll down to themac|maci|maci64)
section (line 188 ff). Add/usr/local/opt/llvm/lib
toLDPATH_PREFIX
, so it might readRestart MATLAB.
Test configuration
Create a file
mex_par_test.cpp
containingThis should compile at the MATLAB prompt:
Calling the compiled function with
mex_par_test
should yield something likeUpdate for macOS 10.12, MATLAB 2016b
/Users/username/Library/Application Support/MathWorks/MATLAB/R2016b
rather than in.matlab/R*/
in the user's home folder (checkprefdir
).LDPATH_PREFIX
line in~/.matlab7rc.sh
. In fact,LDPATH_PREFIX
should not be needed for this combination of OS,clang
, and MATLABllvm
:brew reinstall llvm
MATLAB comes with its own version of
libiomp5
, and this should be linked against. Change your build configuration XML (clang++_openmp_maci64.xml
) so that every incidence of-fopenmp
is replaced by-fopenmp=libiomp5
. Add-L$MATLABROOT/sys/os/maci64
before each occurrence of-L/usr/local/opt/llvm/lib
. Finally, linker options should be-Wl,-rpath,$MATLABROOT/sys/os/maci64:/usr/local/opt/llvm/lib
instead of-Wl,-rpath,/usr/local/opt/llvm/lib
. So myCXXFLAGS
andLDFLAGS
now readThis should make the example above compile again and also not crash MATLAB.