openmp条件并行循环 [英] openmp conditional parallel loop

查看:195
本文介绍了openmp条件并行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个条件成立,我正在尝试使用openmp进行循环.如果条件成立,我可以简单地使用一个if else语句来使用并行的for循环,但是for循环中的代码有点长,如果我仅使用if else语句,它将使代码的长度加倍.因此,基本上,我想要一种更好的方法:

I am trying to use an openmp for loop if a certain condition holds. I could simply use an if else statement to use the parallel for loop if a condition holds, but the code in the for loop is a bit long and it would double the length of the code if I just use the if else statement. So basically, I want a better way to do this:

if(condition_holds){
   // use parallel for loop
   #pragma omp parallel for
   for(...){
     // Long piece of code
   }
}else{
  // Don't use parallel for loop
  for(...){
    // Long piece of code
  }
}

所以我不必在for循环中编写两次代码.

so I won't have to write the code inside the for loop twice.

推荐答案

使用OpenMP的if子句有条件地启用并行性:

Use OpenMP's if clause to conditionally enable parallelism:

#pragma omp parallel for if(condition_holds)
for(...) {

}

您可能会获得一个额外的函数调用开销,因为循环体被OpenMP实现分离为一个函数.

You will probably get an overhead of one additional function call because the loop body is separated into a function by the OpenMP implementation.

这篇关于openmp条件并行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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