Is there an implicit Barrier after omp critical section

瘦欲@ 提交于 2019-12-10 12:45:54

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10443528/is-there-an-implicit-barrier-after-omp-critical-section

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!