0

I have loop which I want to parallelize with OpenMP. when I compile with gcc -o prog prog.c -lm -fopenmp I get no errors. But when I execute it, I get segmentation fault(core dumped). The problem surely comes from the OpenMP commands because the program works when I delete the #pragma...

Here is the parallel loop:

ix = (i-1)%ILIGNE+1;
iy = (i-1)/ILIGNE+1;
k = 1;
# pragma omp parallel for private(j,jx,jy,r,R,voisin) shared(NTOT,k,i,ix,iy) num_threads(2) schedule(auto)
for(j = 1;j <= NTOT;j++){
  if(j != i){
    jx = (j-1)%ILIGNE+1;
    jy = (j-1)/ICOLONE+1;
    r[k][0] = (jx-ix)*a;
    r[k][1] = (jy-iy)*a;
    R[k] = sqrt(pow(r[k][0],2.0)+pow(r[k][1],2.0));
    voisin[k] = j;
    k++;
  }
}

I tried to change the stack size to unlimited but it doesn't fix the problem. Please tell me if it is about a memory leak or a race condition or something else? and thank you for your help

0

2 Answers 2

1

As a side note, be careful when you make an array private.

If you allocated it as a static array e.g. int R[5] or something similar then that's fine, each thread gets its own personal copy :).

If you malloc these however e.g.:

int R = malloc(5*sizeof(int)); then it will act as a shared array regardless of whether you define it as private (which could potentially lead to undefined behaviour, segfaults, jibberish in the array etc).

0

I'm not sure what your code does, but I'm pretty sure the OpenMP version is wrong. Indeed, you parallelised over the j loop, but the heart of your algorithm revolves around the k, which is loosely derived from j and i (which is not presented here BTW).

So when you distribute your j indexes across your OpenMP threads, they all start from a different value of j, but all from the same value of k which is shared. From that, k is incremented quite randomly and the accesses to the various arrays using k are very very likely to generate segmentation faults.

Moreover, arrays r, R and voisin shouldn't be declared private if one want the parallelisation to have any effect.

Finally, C loops like this for(j = 1;j <= NTOT;j++) look utterly suspicious to me for off-by-one accesses... Shouldn't that be rather for(j = 0;j < NTOT;j++)? (just mentioning this since the initial value of k is 1 as well...)

Bottom line is that you'd probably better define k from j's value with k = j<i ? j : j-1 instead of trying to increment it inside the code. Assuming all the rest is correct, this might be a valid version:

ix = (i-1)%ILIGNE+1;
iy = (i-1)/ILIGNE+1;
# pragma omp parallel for private(j,jx,jy,k) num_threads(2) schedule(auto)
for(j = 1;j <= NTOT;j++){
  if(j != i){
    k = j<i ? j : j-1;
    jx = (j-1)%ILIGNE+1;
    jy = (j-1)/ICOLONE+1;
    r[k][0] = (jx-ix)*a;
    r[k][1] = (jy-iy)*a;
    R[k] = sqrt(pow(r[k][0],2.0)+pow(r[k][1],2.0));
    voisin[k] = j;
  }
}

Still, be careful with the C indexing from 0 to size-1, not from 1 to size...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.