Mex-files + Visual studio 2013 + openMP cause Matlab crashes

3 views (last 30 days)
Dear all,
I'm trying to use openMP inside a mexFunction. After a lot of tests I noticed that the simple "#pragma omp parallel" instruction causes a crash of matlab.
Here is my code :
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
#pragma omp parallel
{
}
plhs[0] = mxCreateDoubleScalar(0.0f);
}
It works perfectly in debug mode. But when I run it in release mode matlab crashes brutally.
But if I add a "PAUSE" instruction, everything work fine :
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
#pragma omp parallel
{
}
mexCallMATLAB(0, NULL, 0, NULL, "pause");//pause;
plhs[0] = mxCreateDoubleScalar(0.0f);
}
According to this post : https://fr.mathworks.com/matlabcentral/answers/237411-can-i-make-use-of-openmp-in-my-matlab-mex-files it may have a conflict between 2 versions of openMP and I should use the intel implementation. But I don't want to buy the intel C++ compiler.
if you have any ideas or solutions about this problem, I would be very grateful
Regards, Pierre.G

Answers (1)

sjhstone
sjhstone on 23 Jan 2019
Under Windows, it is not that difficult to use Intel's OpenMP library with Microsoft Visual C++ Compiler.
Try use the following line of command and see what will happen.
mex -v c_with_openmp.c COMPFLAGS="$COMPFLAGS /openmp" LINKFLAGS="$LINKFLAGS /nodefaultlib:vcomp libiomp5md.lib"
There are several functions that may cause crash if they are called within #pragma omp parallel block, like mexPrintf.
  1 Comment
Walter Roberson
Walter Roberson on 23 Jan 2019
Note though that the old SDK 7.1 and one of the old VS Express compilers did not support OpenMP. Sufficiently old VS predates OpenMP as well. So you have to be a bit careful about which VC++ you are using. My memory is a bit fuzzy as to whether the VS2013 Express had OpenMP support; I believe all of the VS2015 and later versions do have OpenMP support.

Sign in to comment.

Categories

Find more on Execution Speed in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!