extern int x[10]; extern int f(int); int temp[10], i; for(i = 0; i < 10; i++) { #pragma omp atomic read temp[i] = x[f(i)]; #pragma omp atomic write x[i] = temp[i]*2; #pragma omp atomic update x[i] *= 2; } ...
{ int nThreads = 8; omp_set_num_threads( nThreads ); int sum = 0; int sum1 = 0; std::cout << std::endl; #pragma omp parallel { #pragma omp for for ( int i = 0; i < 100000; ++ i ) { #pragma omp atomic sum ++; sum1 ++; } } std::cout << "sum= " << sum <...
@krzakov,#pragma omp reduction(+:sum) 是不正确的。编译器很可能会忽略它,并且您会遇到一个由数值不精确性隐藏的危险数据竞争。 reduction是一个子句,它必须应用于另一个OpenMP指令 - 在您的情况下是parallel for指令。 - Hristo Iliev 1 替换: #pragma omp atomic 使用#pragma omp reduction(+:sum)或...
*/ #pragma omp atomic p[i] -= 1.0f; Example 2: Atomic read, write, and update extern int x[10]; extern int f(int); int temp[10], i; for(i = 0; i < 10; i++) { #pragma omp atomic read temp[i] = x[f(i)]; #pragma omp atomic write x[i] = temp[i]*2; #pragma...