这是一个很简单的OpenMP for并行的代码段
void parallel_test(){
#pragma omp parallel for default(none)
for(int i = 0; i < 8; ++i){
printf("now in parallel thread %d\n",i);
}
}
正常情况下,由于线程执行顺序的不确定性,这里输出的线程id应该是乱序的。
但我们多次运行得到的结果却是顺序的,如下:
now in parallel thread 0
now in parallel thread 1
now in parallel thread 2
now in parallel thread 3
now in parallel thread 4
now in parallel thread 5
now in parallel thread 6
now in parallel thread 7
显然这一输出函数并没有以并行的形式运行,反复检查代码,得到结论
?这一函数嵌套在另一个并行函数里。即
#include "omp.h"
void parallel_test(){
#pragma omp parallel for default(none)
for(int i = 0; i < 8; ++i){
printf("now in parallel thread %d\n",i);
}
}
int main(){
#pragma omp parallel for default(none)
for(int i = 0; i < 2; ++i){
if(i == 0){
parallel_test()
}else{
//do something else
}
}
?而OpenMP嵌套并行是需要通过如下代码启用的,如果没有下面这行代码,则只有最外层的代码会并行运行。
omp_set_nested(1);
所以我们修改后的代码为:
?
#include "omp.h"
void parallel_test(){
#pragma omp parallel for default(none)
for(int i = 0; i < 8; ++i){
printf("now in parallel thread %d\n",i);
}
}
int main(){
omp_set_nested(1); // 设置允许嵌套并行
#pragma omp parallel for default(none)
for(int i = 0; i < 2; ++i){
if(i == 0){
parallel_test()
}else{
//do something else
}
}
?运行结果为
now in parallel thread 1
now in parallel thread 4
now in parallel thread 3
now in parallel thread 5
now in parallel thread 6
now in parallel thread 2
now in parallel thread 0
now in parallel thread 7
综上,如果你发现你的代码没有按预期的并行执行,请检查是否有并行嵌套存在,如果有,记得omp_set_nested(1);
|