Is there an implicit omp barrier after omp critical section
For example, Can I modify this following code version-1 into version-2.
VERSION-1
int min = 100;
#pragma omp parallel
{
int localmin = min;
#pragma omp for schedule(static)
for(int i = 0; i < 1000; i++)
localmin = std::min(localmin, arr[i]);
#pragma omp critical
{
min = std::min(localmin, min)
}
}
VERSION-2
int min = 100;
#pragma omp parallel
{
int localmin = min;
#pragma omp for schedule(static) nowait
for(int i = 0; i < 1000; i++)
localmin = std::min(localmin, arr[i]);
#pragma omp critical
{
min = std::min(localmin, min)
}
} // will I get the right "min" after this (because I have included nowait)
Will I get the same result for both version-1 and version-2?
Is there an implicit barrier after omp critical region?
EDIT: Sorry if the example is very poor.. Also, I would like to know whether there would be any performance difference between version-1 and version-2
Critical sections don't have barriers, neither at their beginnings nor at their ends. A critical section is a synchornisation construct on its own that prevents multiple threads from accessing the same data concurrently. You need an additional barrier after the critical section if you'd like to have the correct global minimum before you exit the parallel region. As was already said the parallel region has an implicit barrier at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With