Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an implicit Barrier after omp critical section

Tags:

c++

openmp

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

like image 222
veda Avatar asked May 04 '12 06:05

veda


1 Answers

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.

like image 139
Hristo Iliev Avatar answered Nov 06 '22 19:11

Hristo Iliev