openMP嵌套并行for循环与内部并行for [英] openMP nested parallel for loops vs inner parallel for

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

问题描述

如果我使用嵌套并行for循环,像这样:

  #pragma omp parallel for schedule(dynamic,1)
for(int x = 0; x #pragma omp parallel for schedule(dynamic,1)
for(int y = 0; y< y_max; ++ y){
//并行化此代码
}
//重要信息:此处没有代码
}

这相当于:

  for 0; x  #pragma omp parallel for schedule(dynamic,1)
for(int y = 0; y //并行化此代码
}
//重要信息:此处没有代码
}

$ b

解决方案

如果你创建一个新的任务,编译器支持OpenMP 3.0,可以使用 collapse 子句:

  pragma omp parallel for schedule(dynamic,1)collapse(2)
for(int x = 0; x < x_max; ++ x){
for(int y = 0; y //并行化此代码
}
//重要:代码在这里
}



如果没有(例如只支持OpenMP 2.5) ,有一个简单的解决方法:

  #pragma omp parallel for schedule(dynamic,1)
for = 0; xy int x = xy / y_max;
int y = xy%y_max;
//并行化此代码
}

omp_set_nested(1); 并且您的嵌套 omp parallel for 代码将起作用,但这可能不是最好的想法。 / p>

顺便说一下,为什么要动态调度?每个循环迭代是否在非恒定时间内进行计算?


If I use nested parallel for loops like this:

#pragma omp parallel for schedule(dynamic,1)
for (int x = 0; x < x_max; ++x) {
    #pragma omp parallel for schedule(dynamic,1)
    for (int y = 0; y < y_max; ++y) { 
    //parallelize this code here
   }
//IMPORTANT: no code in here
}

is this equivalent to:

for (int x = 0; x < x_max; ++x) {
    #pragma omp parallel for schedule(dynamic,1)
    for (int y = 0; y < y_max; ++y) { 
    //parallelize this code here
   }
//IMPORTANT: no code in here
}

Is the outer parallel for doing anything other than creating a new task?

解决方案

If your compiler supports OpenMP 3.0, you can use the collapse clause:

#pragma omp parallel for schedule(dynamic,1) collapse(2)
for (int x = 0; x < x_max; ++x) {
    for (int y = 0; y < y_max; ++y) { 
    //parallelize this code here
    }
//IMPORTANT: no code in here
}

If it doesn't (e.g. only OpenMP 2.5 is supported), there is a simple workaround:

#pragma omp parallel for schedule(dynamic,1)
for (int xy = 0; xy < x_max*y_max; ++xy) {
    int x = xy / y_max;
    int y = xy % y_max;
    //parallelize this code here
}

You can enable nested parallelism with omp_set_nested(1); and your nested omp parallel for code will work but that might not be the best idea.

By the way, why the dynamic scheduling? Is every loop iteration evaluated in non-constant time?

这篇关于openMP嵌套并行for循环与内部并行for的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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