如果我像这样使用嵌套的并行循环:
#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
}
除了创建新任务之外,外部并行是否可以做任何事情?
最佳答案
如果您的编译器支持 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 < y_max; ++y) {
//parallelize this code here
}
//IMPORTANT: no code in here
}
如果不支持(例如仅支持 OpenMP 2.5),有一个简单的解决方法:
#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
代码将起作用,但这可能不是最好的主意。
顺便问一下,为什么是动态调度?是否每次循环迭代都在非常量时间内进行评估?
https://stackoverflow.com/questions/10540760/