Update:
test.cpp (absolute path of "omp.h" is not required)
#include "mex.h"
#include "omp.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
#pragma omp parallel
mexPrintf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
mexPrintf("hello world!\n");
}
mex command:
mex -v CXX_FLAGS="-Xclang -fopenmp" LDFLAGS="$LDFLAGS -lomp" -I/usr/local/include test.cpp
This command works with 1 thread, this mex command will generate 3 commands for clang++:
/usr/bin/xcrun -sdk macosx12.3 clang++ -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -DMATLAB_MEX_FILE -I"/usr/local/include" -I"/Applications/MATLAB_R2022a.app/extern/include" -I"/Applications/MATLAB_R2022a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -O2 -fwrapv -DNDEBUG "my_path/test.cpp" -o /var/folders/36/h6gn6pqj20g79jrplzyd5y800000gn/T/mex_35678585508292_17546/test.o
/usr/bin/xcrun -sdk macosx12.3 clang++ -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -DMATLAB_MEX_FILE -I"/usr/local/include" -I"/Applications/MATLAB_R2022a.app/extern/include" -I"/Applications/MATLAB_R2022a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -O2 -fwrapv -DNDEBUG "/Applications/MATLAB_R2022a.app/extern/version/cpp_mexapi_version.cpp" -o /var/folders/36/h6gn6pqj20g79jrplzyd5y800000gn/T/mex_35678585508292_17546/cpp_mexapi_version.o
/usr/bin/xcrun -sdk macosx12.3 clang++ -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -framework Cocoa -bundle -stdlib=libc++ -lomp -O -Wl,-exported_symbols_list,"/Applications/MATLAB_R2022a.app/extern/lib/maci64/mexFunction.map" -Wl,-exported_symbols_list,"/Applications/MATLAB_R2022a.app/extern/lib/maci64/c_exportsmexfileversion.map" -Wl,-U,_mexCreateMexFunction -Wl,-U,_mexDestroyMexFunction -Wl,-U,_mexFunctionAdapter -Wl,-exported_symbols_list,"/Applications/MATLAB_R2022a.app/extern/lib/maci64/cppMexFunction.map" /var/folders/36/h6gn6pqj20g79jrplzyd5y800000gn/T/mex_35678585508292_17546/test.o /var/folders/36/h6gn6pqj20g79jrplzyd5y800000gn/T/mex_35678585508292_17546/cpp_mexapi_version.o -L"/Applications/MATLAB_R2022a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2022a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -o test.mexmaci64
-Xclang -fopenmp did not present in these cmds, therefore the test.mexmaci64 can only use 1 thread.
If I add -fopenmp:
mex -v CXX_FLAGS="-Xclang -fopenmp" LDFLAGS="$LDFLAGS -lomp" CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp" -I/usr/local/include test.cpp
Matlab will crash when I run test().
I modified the mex derived 3 commands and find openmp in the first one will crash matlab:
/usr/bin/xcrun -sdk macosx12.3 clang++ -c -DMATLAB_DEFAULT_RELEASE=R2017b -DUSE_MEX_CMD -DMATLAB_MEX_FILE -I"/usr/local/include" -I"/Applications/MATLAB_R2022a.app/extern/include" -I"/Applications/MATLAB_R2022a.app/simulink/include" -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -O2 -fwrapv -DNDEBUG -Xclang -fopenmp -lomp "my_path/test.cpp" -o /var/folders/36/h6gn6pqj20g79jrplzyd5y800000gn/T/mex_35678585508292_17546/test.o
It looks like matlab do not support openmp well, but I know matlab do support openmp with mex.
I tried a simple function test3.m
function test3(n)
parfor i = 1:n
fprintf('i = %d \n',i);
end
end
Then use matlab coder to generate mex function, I found the cmds used in matlab coder
[1/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" /Applications/MATLAB_R2022a.app/extern/version/cpp_mexapi_version.cpp -o build/maci64/cpp_mexapi_version.o
[2/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" test3_data.cpp -o build/maci64/test3_data.o
[3/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" interface/_coder_test3_info.cpp -o build/maci64/_coder_test3_info.o
[4/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" rt_nonfinite.cpp -o build/maci64/rt_nonfinite.o
[5/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" test3_initialize.cpp -o build/maci64/test3_initialize.o
[6/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" eml_int_forloop_overflow_check.cpp -o build/maci64/eml_int_forloop_overflow_check.o
[7/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" interface/_coder_test3_mex.cpp -o build/maci64/_coder_test3_mex.o
[8/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" test3_terminate.cpp -o build/maci64/test3_terminate.o
[9/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" interface/_coder_test3_api.cpp -o build/maci64/_coder_test3_api.o
[10/11] /usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/mypath" -I "./interface" -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test3_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" test3.cpp -o build/maci64/test3.o
[11/11] /usr/bin/xcrun -sdk macosx12.3 clang++ build/maci64/test3_data.o build/maci64/rt_nonfinite.o build/maci64/test3_initialize.o build/maci64/test3_terminate.o build/maci64/test3.o build/maci64/_coder_test3_api.o build/maci64/_coder_test3_mex.o build/maci64/eml_int_forloop_overflow_check.o build/maci64/_coder_test3_info.o build/maci64/cpp_mexapi_version.o -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -framework Cocoa -bundle -stdlib=libc++ -L"/Applications/MATLAB_R2022a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2022a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -Wl,-rpath,@loader_path -o test3_mex.mexmaci64 -lemlrt -lcovrt -lut -lmwmathutil -fPIC -L"/Applications/MATLAB_R2022a.app/sys/os/maci64" -liomp5
run test3_mex(int32(16)), I got this result:
i = 15
i = 16
i = 11
i = 12
i = 13
i = 14
i = 9
i = 10
i = 1
i = 2
i = 7
i = 8
i = 5
i = 6
i = 3
i = 4
Then, I changed the path and filenames in those cmds:
system('/usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" /Applications/MATLAB_R2022a.app/extern/version/cpp_mexapi_version.cpp -o cpp_mexapi_version.o');
system('/usr/bin/xcrun -sdk macosx12.3 clang++ -fno-common -arch x86_64 -mmacosx-version-min=10.15 -fexceptions -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -fobjc-arc -std=c++11 -stdlib=libc++ -DMATLAB_MEX_FILE -O2 -fwrapv -DNDEBUG -fvisibility=hidden -std=c++11 -I "." -I "/Applications/MATLAB_R2022a.app/extern/include" -I "." -DMODEL=test_mex -DMW_NEEDS_VERSION_H -c -fPIC -Xpreprocessor -fopenmp -I "/Applications/MATLAB_R2022a.app/toolbox/eml/externalDependency/omp/maci64/include" -DOpenMP_omp_LIBRARY="/Applications/MATLAB_R2022a.app/sys/os/maci64/libiomp5.dylib" test.cpp -o test.o');
system('/usr/bin/xcrun -sdk macosx12.3 clang++ test.o cpp_mexapi_version.o -Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=10.15 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -framework Cocoa -bundle -stdlib=libc++ -L"/Applications/MATLAB_R2022a.app/bin/maci64" -lmx -lmex -lmat -L"/Applications/MATLAB_R2022a.app/extern/bin/maci64" -lMatlabDataArray -lMatlabEngine -Wl,-rpath,@loader_path -o test_mex.mexmaci64 -lemlrt -lcovrt -lut -lmwmathutil -fPIC -L"/Applications/MATLAB_R2022a.app/sys/os/maci64" -liomp5');
And crashed again.