|
Dear list,
I am trying to parallellize some code C mex files using
openmp. I am having trouble with allocating memory for the
threads. The code I am using roughly looks like this:
double *T, *A;
int i, Bm=100;
A = mxCalloc(Bm*Bm, sizeof(double));
#pragma omp parallel shared(Bm,A) private(i,j,T)
{
T = mxCalloc(Bm, sizeof(double));
/*T = calloc(Bm, sizeof(double));*/
#pragma omp for schedule(dynamic,1) nowait
for (i=0; i<Bm; i++)
{
for (j=0; j<i; j++)
{
/* do some math and fill T */
T[j] = i;
}
/* Write cache to matrix A */
for (j=0; j<i; j++)
A[i*Bm + j] += T[j];
/* Clear T */
bzero(T, sizeof(double)*(i+1));
}
mxFree(T);
/*free(T); */
}
This should reserve memory for each tread, and the code
works fine outside Matlab.
It also seems to work fine inside Matlab, when using
mxCalloc and mxFree in R2007b.
In R2008a Matlab occasionally crashes with:
Severe:
memmgr/memcache.cpp:742: Assert : (hdr->in_use != 0),
"Attempt to free previously freed memory"
The program '[17833] : Native' has exited with code 2 (0x2).
However, when I use calloc/free instead of mxCalloc and
mxFree in Matlab 2008a, it works fine.
So, is there a bug in my OMP loop and it is just luck that
it does not crash, or is this a bug in Matlab?
The code is compiled with Intel C compiler 10. I am now
trying to build gcc-4.3.0 to test with gcc.
Thanks in advance,
Sebastiaan
|