OpenMP不等负载,无for循环 [英] OpenMP unequal load without for loop

查看:96
本文介绍了OpenMP不等负载,无for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的OpenMP代码

while(counter < MAX)  {
  #pragma omp parallel reduction(+:counter) 
  {
     // do monte carlo stuff
     // if a certain condition is met, counter is incremented

  }
}

因此,想法是只要计数器低于某个值,并行部分就会由可用线程执行.根据场景的不同(我在这里做MC的事情,所以是随机的),计算可能要比其他时间花费更多,因此这里的工作人员之间存在不平衡,这是由于并行结束时隐含的障碍而变得明显部分.

似乎#pragma omp parallel for可能有一些方法可以绕开它(即nowait指令/动态调度),但是我不能使用它,因为我不知道for循环的上层迭代编号. /p>

任何想法/设计模式如何应对这种情况?

最诚挚的问候!

解决方案

在单个并行部分中运行所有内容,并自动访问counter.

int counter = 0;
#pragma omp parallel
while(1) {
     int local_counter;
     #pragma omp atomic read
     local_counter = counter;
     if (local_counter >= MAX) {
          break;
     }
     // do monte carlo stuff
     // if a certain condition is met, counter is incremented
     if (certain_condition) {
         #pragma omp atomic update
         counter++;
     }
}

由于原子访问,您不能直接在while条件中进行检查. 请注意,此代码将过冲,即在并行部分之后可能会出现counter > MAX.请记住,counter被许多线程共享和读取/更新.

I have an OpenMP code that looks like the following

while(counter < MAX)  {
  #pragma omp parallel reduction(+:counter) 
  {
     // do monte carlo stuff
     // if a certain condition is met, counter is incremented

  }
}

Hence, the idea is that the parallel section gets executed by the available threads as long as the counter is below a certain value. Depending on the scenario (I am doing MC stuff here, so it is random), the computations might take long than others, so that there is an imbalance between the workers here which becomes apparent because of the implicit barrier at the end of the parallel section.

It seems like #pragma omp parallel for might have ways to circumvent this (i.e. nowait directive/dynamic scheduling), but I can't use this, as I don't know an upper iteration number for the for loop.

Any ideas/design patterns how to deal with such a situation?

Best regards!

解决方案

Run everything in a single parallel section and access the counter atomically.

int counter = 0;
#pragma omp parallel
while(1) {
     int local_counter;
     #pragma omp atomic read
     local_counter = counter;
     if (local_counter >= MAX) {
          break;
     }
     // do monte carlo stuff
     // if a certain condition is met, counter is incremented
     if (certain_condition) {
         #pragma omp atomic update
         counter++;
     }
}

You can't check directly in the while condition, because of the atomic access. Note that this code will overshoot, i.e. counter > MAX is possible after the parallel section. Keep in mind that counter is shared and read/updated by many threads.

这篇关于OpenMP不等负载,无for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆