c - nested loops with pragma omp parallel for, jumbling up -


i trying parallelize code run simulations on spiking neuron network. involves 1 double loop, put statement '#pragma omp parallel for' outside main loop. here's code:

int main(void){      int i,j,count[200];      #pragma omp parallel     for(i=0;i<200;i++){         count[i] = 0;         (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 @ results, of values of count[i] exceed 200, though loop goes 1 200. count[i]-j can either 0,1 or -1, values differ widely, thought each thread work on 1 value of i, , count array depends on current value of i. how rewrite code can safely increment count?

you must declare j private. can explicitly via:

#pragma omp parallel private(j) 

i implicitly private being loop variable of worksharing loop. count implicitly shared because it defined outside of loop. both of desirable here.

however, recommend always declare variables al locally possible, when using openmp. way implicit private/shared right, , avoids lots of subtle undefined value reads. practice

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

btw: printout of count[i]-j can show arbitrary values. accesses data potentially concurrently written other threads.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -