I am trying to understand why the barrier is required to remove the race condtion?
#include<omp.h>
#include<stdio.h>
int main()
{
int sum = 0;
#pragma omp parallel num_threads(4)
for(int i = 0;i < 10;i )
{
#pragma omp parallel for num_threads(4)
for(int j = 0;j < 10;j )
{
#pragma omp critical
sum = 1;
}
// Uncommenting this barrier removes the race condition. Right now it is non-deterministic.
// #pragma omp barrier
#pragma omp single
sum = 1;
}
printf("%d", sum);
}
CodePudding user response:
Without the "pragma omp barrier", another thread might concurrently be accessing the same sum variable inside the "pragma omp critical" section. This would lead to undefined results.
The barrier forces all threads to finish the inner for loop, and then a single thread can proceed to do the last section without risk of any race condition.