openMP nested parallel for loops vs inner parallel for(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:openMP 嵌套并行 for 循环与内部并行 for
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
