本文介绍了omp关键部分后是否存在隐式屏障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在omp关键部分之后是否存在隐式的omp障碍

Is there an implicit omp barrier after omp critical section

例如,可以将下面的代码从版本1修改为版本2.

For example, Can I modify this following code version-1 into version-2.

版本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)
   }
}

版本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)

版本1和版本2会得到相同的结果吗?

Will I get the same result for both version-1 and version-2?

在omp关键区域之后是否存在隐式障碍?

Is there an implicit barrier after omp critical region?

编辑:很抱歉,如果示例很差..另外,我想知道版本1和版本2之间是否会有性能差异

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.

这篇关于omp关键部分后是否存在隐式屏障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:20