Pragma omp for/parallel not working

copenmpparallel-processing

I have this code

#define N 2048
int main(void)
{
    FILE *fp1;
    fp1=fopen("myfile.txt", "a");
    for(int i=0;i<N;i++)
    {
        #pragma omp parallel
        {
            int *x=malloc(sizeof(int)*N);
            int *xtemp=malloc(sizeof(int)*N);
            int (*adj)[N];
            adj=malloc(sizeof *adj *N);
            ...//other declarations
            #pragma omp for
            for(int k=0;k<100;k++)
            {
                /* do things involving x,xtemp, adj...*/
            }
            fprintf(fp1,"things \n");
            ... //free vectors part...
        }
    }
    fclose(fp1);
    return 0;
}

The code seems to run fine, but when I check with htop the usage of my cpu (i3 dual core with hyper-threading), I see that only one thread is working at 100%.

So I put some printf in my code to see how many times I would get the same writing on my terminal, like, after :

#pragma omp parallel
 {
   printf("Hey, I'm inside the par zone!\n"); 
    .... 
 }

but it seems that I get only one printf per time, so I think that only one core is working. I tried the same thing after the pragma omp for part but still get the same issue.

Why it seems that pragma omp parallel is not parallelizing anything? I tried a simpler program (a simple parallelized version of hello world!) and it works, I get as many stamps as my core numbers.

I tried to put all together in a #pragma omp parallel for loop but I get a segmentation fault – core dumped error…

………………………

Here it is my MCVE code, it gives the same problem. (Sorry for the indents, but the file uploader screwed up things).
The functions before main just make some calculations, using rand(), nothing special.

my compilation line is:gcc -w -std=c99 MC.c -o try -lm -fopenmp -lquadmath -O3

Best Answer

Your code has two issues which I can see:

  • Use of a global random number generator in parallel. That is a whole world of issues in itself, but a quick fix could be to either use a per-thread RND like rand_r() instead of the global rand(), or to protect the calls to rand() with a #pragma omp critical RNG (which is likely to be a performance killer). In any case, rand() is not suited for serious work.
  • The attempt to close your output file in a parallel region.

Now, the reason why the parallelism doesn't work is that you wrote #pragma omp shared(mfield) parallel instead of #pragma omp parallel shared(mfield). Once this fixed, the code spawns threads as expected.

Related Topic