views:

101

answers:

2

i have got the following fraction of code that getting me the stack overflow error


#pragma omp parallel shared(Mo1, Mo2, sum_normalized_p_gn, Data, Mean_Out,Covar_Out,Prior_Out, det) private(i)  num_threads( number_threads )  
{  

        //every thread has a new copy  
        double* normalized_p_gn = (double*)malloc(NMIX*sizeof(double));   
        #pragma omp critical  
        {
            int id =  omp_get_thread_num();
            int threads = omp_get_num_threads();
            mexEvalString("drawnow");  
        }  

        #pragma omp for
        //some parallel process.....

}  

the variables declared in the shared are created by malloc. and they consumes with large amount of memory

there are 2 questions regarding to the above code. 1) why this would generate the stack overflow error( i.e. segmentation fault) before it goes into the parallel for loop? it works fine when it runs in the sequential mode.... 2) am i right to dynamic allocate memory for each thread like "normalized_p_gn" above?

Regards Edwin

+1  A: 

One possibility which we can't exclude, your code snippet doesn't reveal any numbers, is that you are simply trying to allocate too much memory when you run this in parallel. If you can confirm that this is not your problem, comment or edit your question and I'll take another look.

High Performance Mark
+1  A: 

In place of malloc, use mxMalloc in mex files (see here). Don't forget to mxFree when you're finished with the memory.

Ramashalanka
it runs well when it is in the sequential...
Edwin
@Edwin: Yes, I saw that from your OP. `malloc` often works OK in mex files, but it is unreliable: I've found I run the same code and it works sometimes and not others. I'd change to `mxMalloc` and test it. For other problems it'd be good to see a larger code segment, including the free.
Ramashalanka