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
-fopenmp
to use the OpenMP pragmas. Likewise, did you set theOMP_NUM_THREADS
environment variable to any value prior to running your code? – Gilles