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

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

问题描述

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

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
}

这相当于:

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?

推荐答案

如果您的编译器支持 OpenMP 3.0,您可以使用 collapse 子句:

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
}

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

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
}

您可以使用 omp_set_nested(1); 启用嵌套并行性,并且您的嵌套 omp parallel for 代码将起作用,但这可能不是最好的主意.

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天全站免登陆