3
votes

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

2
Did you compile with an OpenMP-enabling compiler switch? For example, GCC requires -fopenmp to use the OpenMP pragmas. Likewise, did you set the OMP_NUM_THREADS environment variable to any value prior to running your code?Gilles
Yes I compiled with -fopenmp and I didn't set OMP_NUM_THREADSFrancesco Di Lauro
Could you please give a MCVE exemplifying your issue, along with your compilation command line and execution command?Gilles
Sure, I need some time though... Just a question, do I have to edit my post or post an answer?Francesco Di Lauro
Edit the post. Answers are for answers ;)Gilles

2 Answers

2
votes

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.

0
votes

It seems to be a problem related to the fprintf part of the code. If I parallelize only the k-loop things work just fine. OpenMP and File I/O apparently says the same.