12

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

veda
  • 6,416
  • 15
  • 58
  • 78
  • That's probably not the best example since there will be a barrier when you leave the parallel region. But good question nonetheless. +1 – Mysticial May 04 '12 at 06:12
  • There is no performance benefit in v2 over v1 in the contrived example you posted, but in the real world, there could be. – Mahmoud Al-Qudsi May 04 '12 at 08:00

2 Answers2

15

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.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
0

You will get a performance benefit by using nowait if there are a large number of iterations.

haxor
  • 211
  • 1
  • 2
  • 11