Reputation: 5534
I've read that variables in OpenMP are shared by default. Does this mean that in the following code, which is supposed to sum all the numbers from 0 to N-1, there is a race condition?
int sum = 0,i;
#pragma omp parallel for
for (i = 0; i < N; i++)
sum+=i;
Upvotes: 1
Views: 1062
Reputation: 59998
Yes, there is a race condition. You should add reduction(+:sum)
to the pragma. This will effectively make invisible private copies in each thread and then sum them after the loop.
Upvotes: 3