Avoid racing in OpenMP (in parallel for loop)

I am writing an OpenMP program in C. I have this shared array of "data" that is updated by all threads. I want to make sure that each thread has finished reading and stored the value in temp before executing the following statement data [j] = temp.

I tried putting a #pragma omp barrier between two statements, but the compiler throws an error. Please help.

#pragma omp parallel for shared(data)

for (j = 0; j < numints; j++){

     if (j >= max_j)

     {

              temp = data[j] + data[j - max_j];
             data[j] = temp; 
     }

}
+3
source share
2 answers

As you saw, barrierit will not work; criticalheavy enough for this particular operation. Atomic weight is less than critical; you can always do

if (j >= max_j)
{
    #pragma omp atomic
    data[j] += data[j-max_j]; 
}

​​ (, ) - ​​ , parallelism ( , , ).

, , () [maxints-1] , [maxints-1-max_j], [maxints-1-2 * max_j].. , OpenMPs. ( , , ).

maxints < 2*max_j, ;

#pragma omp parallel for shared(data)
for (j = max_j; j < numints; j++){
    data[j] += data[j-max_j];
}

, [j], . (), , () ...

+2

Source: https://habr.com/ru/post/1769942/


All Articles