【发布时间】:2014-10-13 05:58:05
【问题描述】:
我在 openmp 中使用了两个不同版本的缩减,我得到了完全不同的结果。以下哪一项是错误的?
omp_set_num_threads(t);
long long unsigned int d = 0;
#pragma omp parallel for default(none) shared(some_stuff) reduction(+:d)
for (int i=start; i< n; i++)
{
d += calc(i,some_stuff);
}
cout << d << endl;
第二个版本是这样的:
omp_set_num_threads(t);
//reduction array
long long unsigned int* d = new long long unsigned int[t];
for(int i = 0; i < t; i++)
d[i] = 0;
#pragma omp parallel for default(none) shared(somestuff, d)
for (int i=start; i< n; i++)
{
long long unsigned dd = calc(i, somestuff);
d[omp_get_thread_num()] += dd;
}
long long unsigned int res = 0;
for(int i = 0; i < omp_get_num_threads(); i++){
res += d[i];
}
delete[] d;
cout << res << endl;
【问题讨论】: