我正在编写一个具有数组依赖性的 OpenACC 代码。内循环的每次迭代都可以更新数组的相同位置。这是一些代码:
long unsigned int digits[d + 11];
for (long unsigned int digit = 0; digit < d + 11; ++digit)
digits[digit] = 0;
for (long unsigned int i = 1; i <= n; ++i) {
long unsigned int remainder = 1;
for (long unsigned int digit = 0; digit < d + 11 && remainder; ++digit) {
long unsigned int div = remainder / i;
long unsigned int mod = remainder % i;
digits[digit] += div; // here
remainder = mod * 10;
}
}
OpenMP 版本的写法如下:
#pragma omp parallel private(i)
{
long unsigned int digit_local[d+11];
for(i=0;i<d+11;i++)
digit_local[i] = 0;
#pragma omp for
for (i = 1; i <= n; ++i) {
long unsigned int remainder = 1;
for (long unsigned int digit = 0; digit < d + 11 && remainder; ++digit) {
long unsigned int div = remainder / i;
long unsigned int mod = remainder % i;
digit_local[digit] += div;
remainder = mod * 10;
}
}
#pragma omp critical
for(long unsigned int digit = 0; digit < d+11; ++digit)
digits[digit] += digit_local[digit];
}
在 OpenACC 中,关键字 private 与数组一起使用,但我不知道如何将私有数组与全局数组连接起来。
谢谢。