openmp条件并行循环 [英] openmp conditional parallel loop
问题描述
如果某个条件成立,我正在尝试使用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屋!