Matlab crashes in simple MEX C "#pragma omp parallel for" test example

The program below crashes randomly (It appears to be random, it sometimes works). It feels like it might be me misunderstanding some basic thing. If so, I apologize in advance. Please let me know if I can provide more info. I compile using:
>>mex parallel.c CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp"
I am running this on Max OS 10.8.2 and Matlab 7.14.0.739 (R2012a) 64bit. My GCC version is "i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)".
PROGRAM BELOW:
#include "mex.h"
#include <omp.h>
/* Parallel part of program */
double inner_loop(){
double sum;
int p, n;
sum = 0.0;
n = 100;
#pragma omp parallel for shared(n) private(p) reduction(+: sum)
for(p = 0; p<n; p++){
sum += 1.0;
}
return sum;
}
/* Main function */
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
/* Initialize variables */
int i, m;
double sum;
/* Set values */
m =100;
sum = 0.0;
/* Outer loop */
for(i=0; i<m; i++){
sum += inner_loop(); // Call parallel inner loop
}
/* Output data */
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
double* y = mxGetPr(plhs[0]);
y[0]=sum;
return;
}

답변 (2개)

James Tursa
James Tursa 2012년 10월 2일
편집: James Tursa 2012년 10월 2일
There might be some incompatibility between the OpenMP implementation of your compiler and MATLAB. Maybe try something even simpler first and then build from there. E.g.,
#include "mex.h"
#include <omp.h>
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
int p, n;
double sum;
sum = 0.0;
n = 100;
#pragma omp parallel for shared(n) private(p) reduction(+: sum)
for(p = 0; p<n; p++){
sum += 1.0;
}
mexPrintf("sum = %f\n",sum);
}

댓글 수: 1

Hi. Thanks for your reply.
I tried your example, compiling and running it from the command line works. I have a hunch that the problem is placing a loop outside the parallel loop (as was done in my original example). So I tried running the following m-file,
% Test code in m-file:
for i=1:1000
test(); // the script you suggested
end
which works fine. Printing the results as expected.
My explanation would be that there is a conflict between successive iterations of the outer loop if they are run in quick sequence. That they somehow collide. So I tried removing "mexPrintf("sum = %f\n",sum);" which is sort of a pause condition (is it?).
Running the test code m-file with this alteration invariably crashes my Matlab. This narrows down the problem a bit I think. Any ideas or further suggestions? (I also added my gcc version to the original question. (4.2.1))
Edit: Please also see my response below with further information.

댓글을 달려면 로그인하십시오.

Patrik
Patrik 2012년 10월 3일
편집: Patrik 2012년 10월 3일
I have installed the newest version of Matlab (2012b) and I have not been able to reproduce the exact errors discussed above.
The example below however randomly crashes on Matlab 2012b. It works sometimes.
PROGRAM BELOW:
#include "mex.h"
#include <omp.h>
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
int p, n, i, m;
double sum;
sum = 0.0;
n = 100;
m = 100;
for(i = 0; i<m; i++){
#pragma omp parallel for shared(n) private(p) reduction(+: sum)
for(p = 0; p<n; p++){
sum += 1.0;
}
}
mexPrintf("%f",sum);
}

댓글 수: 1

Hi Patrik,
At the omp directive line, try including the reduction variable "sum", in the list of shared variables or replacing "shared(n)" with "default(shared)"
Hopefully, that should alleviate the crashing.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

제품

질문:

2012년 10월 2일

댓글:

2014년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by