M1 Mac compile Mex file with OpenMP

55 次查看(过去 30 天)
I tried to compile mex function in M1 mac with matlab 2022a and matlab 2022a beta, it works well without openmp. But When I tried to include omp.h, error occurs.
Step1, I installed openmp with xcode,
curl -O https://mac.r-project.org/openmp/openmp-13.0.0-darwin21-Release.tar.gz
sudo tar fvxz openmp-13.0.0-darwin21-Release.tar.gz -C /
test1.cpp
#include <omp.h>
#include <iostream>
int main() {
#pragma omp parallel
std::cout<<"Hello from thread: "<< omp_get_thread_num()<< "nthreads: " << omp_get_num_threads() << "\n";
}
I can compile test1.cpp with (-Xpreprocessor also works):
clang++ -Xclang -fopenmp -lomp test1.cpp
The results looks good:
Hello from thread 0, nthreads 8
Hello from thread 2, nthreads 8
Hello from thread 4, nthreads 8
Hello from thread 3, nthreads 8
Hello from thread 5, nthreads 8
Hello from thread 7, nthreads 8
Hello from thread 1, nthreads 8
Hello from thread 6, nthreads 8
Step2, I tried to build a test function with matlab
test2.cpp
If I use #include "omp.h", I will get: error: "omp.h" file not found, therefore I used absolute path
#include "mex.h"
#include "/usr/local/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 I used
matlab 2022a beta
mex -v CXXFLAGS='$CXXFLAGS -Xclang -fopenmp -arch arm64' LDFLAGS='$LDFLAGS -I/usr/local/include -L/usr/local/lib -lomp -mmacosx-version-min=12.4' -I'usr/local/include' -L'usr/local/lib' test2.cpp
matlab 2022a
mex -v CXXFLAGS='$CXXFLAGS -Xclang -fopenmp -arch x86_64' LDFLAGS='$LDFLAGS -I/usr/local/include -L/usr/local/lib -lomp -mmacosx-version-min=12.4' -I'usr/local/include' -L'usr/local/lib' test.cpp
MEX completed successfully, but if I run the test2.mexmaci64 or test2.mexmaca64, maltab stopped immediately.
If I commented this line,
#pragma omp parallel
The results became:
Hello from thread 0, nthreads 1
hello world!
Supported compiler on mac is Xcode 13.x and my clang --version is:
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: arm64-apple-darwin21.5.0
  1 个评论
shuang zhou
shuang zhou 2022-7-18
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.

请先登录,再进行评论。

采纳的回答

shuang zhou
shuang zhou 2022-7-19
编辑:shuang zhou 2022-7-19
The problem is caused by print out functions,
mexPrintf function, stream out function(https://www.mathworks.com/help/matlab/matlab_external/displaying-output-in-matlab-command-window.html) or even other output functions cannot be in parallel structure!!!
mexPrintf and all other memory allocate function are thread unsafe. https://www.mathworks.com/matlabcentral/answers/418782-thread-safety-of-mx-mex-functions
Wrong usage (this will crash):
#include "mex.h"
#include "omp.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
#pragma omp parallel for
for(int p = 0; p<16; ++p){
mexPrintf("p = %d\n",p);// output function should not in parallel structure!!
}
}
Correct usage test1.cpp:
#include "mex.h"
#include "omp.h"
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
double sum{};
#pragma omp parallel for reduction(+: sum)
for(int p = 0; p<100000000; ++p){
sum += (double)(p)*(double)(p);
}
mexPrintf("sum = %f\n",sum);
}
Compile:
mex CXX_FLAGS="-Xclang -fopenmp" LDFLAGS="$LDFLAGS -lomp" CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp" -I/usr/local/include test1.cpp
benchmark:
tic;test1();toc;
// with openmp 0.02s @M1 pro 8 core
// without openmp 0.1s @M1 pro 8 core
In summary:
step1 install openmp, run this in terminal (https://mac.r-project.org/openmp/):
curl -O https://mac.r-project.org/openmp/openmp-13.0.0-darwin21-Release.tar.gz
sudo tar fvxz openmp-13.0.0-darwin21-Release.tar.gz -C /
step2 compile with -Xclang -fopenmp and -lomp
mex CXX_FLAGS="-Xclang -fopenmp" LDFLAGS="$LDFLAGS -lomp" CXXOPTIMFLAGS="$CXXOPTIMFLAGS -Xclang -fopenmp" -I/usr/local/include test1.cpp
  2 个评论
shuang zhou
shuang zhou 2022-7-19
Matlab R2022a could be not stable, sometimes openmp works, something it will crash(after installation of new toolbox). Matalab R2022a beta is stable.
Calling a magic function could stablize Matlab R2022a.
How to get a magic function:
test.m
function test(n)
parfor i = 1:n
fprintf('i = %d \n',i);
end
end
Convert to mex function with matlab coder (input should be int32 1x1 scalar), run the mex function
test_mex(int32(n));
shuang zhou
shuang zhou 2022-8-13
the magic function can be found in my repo :https://github.com/civerjia/Proton-Bragg-Peak-Fit
go to the utils folder and run ifMacCrashed()

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 C Shared Library Integration 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by