M1 Mac compile Mex file with OpenMP
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!