假设我们想在OpenMP循环中计数某些内容。请比较一下归约。
使用原子增量
因此提供的功能较少。
为什么要使用减少而不是原子访问计数器?
int counter = 0;
#pragma omp for reduction( + : counter )
for (...) {
...
counter++;
}
使用原子增量
int counter = 0;
#pragma omp for
for (...) {
...
#pragma omp atomic
counter++
}
原子访问能够立即提供结果,而归约只有在循环结束时才能确定其正确值。例如,归约不允许出现以下情况:
int t = counter;
if (t % 1000 == 0) {
printf ("%dk iterations\n", t/1000);
}
因此提供的功能较少。
为什么要使用减少而不是原子访问计数器?