我对reduction
子句的行为感到困惑,因为
我编译
int main() {
//default(none) shared(suma)
int suma = 0;
#pragma parallel omp default(shared) num_threads(2)
{
#pragma omp for reduction(+:suma)
for (int n = 0; n<20; n++ ) {
for ( int j = 0; j < 30; j++ ) {
suma += 1;
}
}
}
printf("suma = %d\n", suma);
assert( suma == 20*30);
return 0;
}
编译器说我必须声明suma
为共享,而我明确指定要共享。
reduction.cpp:28:33: error: reduction variable must be shared
#pragma omp for reduction(+:suma)
^
让我发疯的是,当我写错了(过度并行化)的代码时,它可以工作
int main() {
//default(none) shared(suma)
int suma = 0;
#pragma parallel omp default(shared) num_threads(2)
{
#pragma parallel omp for reduction(+:suma)
for (int n = 0; n<20; n++ ) {
for ( int j = 0; j < 30; j++ ) {
suma += 1;
}
}
}
printf("suma = %d\n", suma);
assert( suma == 20*30);
return 0;
}
在这一点上,我不知道我是否缺少某些内容或编译器中是否存在错误。无论如何,我正在使用clang++ -fopenmp reduction.cpp
clang version 7.0.0 (https://git.llvm.org/git/clang.git/ bb7269ae797f282e27e47eb4ebedfa6abe826e9e) (https://git.llvm.org/git/llvm.git/ 37d8f03a3676034f21f0a652359ec4ace8d0521f)
Target: x86_64-unknown-linux-gnu
Thread model: posix