2
votes

I am trying to parallelize a code to run some simulations on spiking neuron network. This involves one double loop, where I put a statement '#pragma omp parallel for' outside the main loop. Here's the code:

int main(void){

    int i,j,count[200];

    #pragma omp parallel for
    for(i=0;i<200;i++){
        count[i] = 0;
        for (j=0;j<200;j++){
            if (j!=i){
                count[i]++;
                printf("i: %d j: %d count[i]:%d, count[i]-j:%d\n",i,j,count[i], count[i]-j);
            }
        }

    }

    return 0;
}

Looking at the results, some of the values of count[i] exceed 200, even though the loop only goes from 1 to 200. count[i]-j can either be 0,1 or -1, but the values differ widely, even thought each thread would work on one value of i, and the count array depends only on the current value of i. How do I rewrite the code so that I can safely increment count?

1
What are the final values? Can you add a single loop after this to print all the values?Ajay Brahmakshatriya
Put the #pragma omp parallel before the second loop.Mathieu
@purplepsycho that's just wrong, would create a race condition on count[i].Zulan

1 Answers

1
votes

You must declare j as private. You can do so explicitly via:

#pragma omp parallel for private(j)

i is implicitly private being the loop variable of the worksharing loop. count is implicitly shared because it it defined outside of the loop. Both of which are desirable here.

However, I strongly recommend to always declare variables al locally as possible, especially when using OpenMP. This way the implicit private/shared is almost always right, and it avoids lots of subtle undefined value reads. It is generally good practice

int count[200];

#pragma omp parallel for
for(int i=0;i<200;i++){
    count[i] = 0;
    for (int j=0;j<200;j++){

BTW: Your printout of count[i]-j can show completely arbitrary values. It accesses data that it potentially concurrently written by other threads.