I am a bit confused if there is a data race for variable k. To my understanding, only one thread will execute the single construct but since no wait is asserted, threads will start executing the for construct immediately. Is atomic here enough to prevent any potential data race?
#include <stdio.h>
#include <omp.h>
#define Nthreads 8
void main()
{
int n =9, l,k =n,i,j;
k += n+1;
omp_set_num_threads(Nthreads);
#pragma omp parallel default(none) shared(n, k) private(j)
{
#pragma omp single nowait
{
k = k+5;
}
#pragma omp for nowait
for( i =0; i< n; i++)
{
#pragma omp atomic
k +=n+i+1;
}
}
}
atomicin both worksharing constructs should suffice. You may also explicitly useatomic updateto make your intent clearer to the reader. - Hristo Ilievatomicinsidesingleconstruct as well? - FATzzatomicin both places (singleandfor) as well as explicitly specifying theupdateatomic clause which is now implicitly used at the atomic synchronization construct. Being explicit is a matter of preference though, it will not change the output of the code. - pattakosn