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


基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01