openmp collapse with inner loop reduction

openmp collapse with inner loop reduction

我正在尝试使用#pragma omp simd 对嵌套循环进行矢量化处理,但内部循环有所减少。有什么办法吗?

  #pragma omp simd collapse(2) reduction(+:total[i])
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){

      float distance = sqrt(
      (x[i][0]-x[j][0]) * (x[i][0]-x[j][0]) +
      (x[i][1]-x[j][1]) * (x[i][1]-x[j][1]) +
      (x[i][2]-x[j][2]) * (x[i][2]-x[j][2])
      );

      total[i] += distance; 
    }
  }

目前只是编译的时候报错:

error: identifier "i" is undefined
#pragma omp simd collapse(2) reduction(+:total[i])
                                               ^

您不能将变量 'i' 传递给 +:total[i],要么指定要减少的数组子部分,要么只执行 +:total。但是请记住,您需要一个支持 OpenMP 4.5 数组缩减功能的编译器。

  #pragma omp simd collapse(2) reduction(+:total)
  for (int i=0; i<N; i++){
    for (int j=0; j<N; j++){

      float distance = sqrt(
      (x[i][0]-x[j][0]) * (x[i][0]-x[j][0]) +
      (x[i][1]-x[j][1]) * (x[i][1]-x[j][1]) +
      (x[i][2]-x[j][2]) * (x[i][2]-x[j][2])
      );

      total[i] += distance; 
    }
  }

当编译器不支持 OpenMP 4.5 数组缩减功能时,可以选择显式实现缩减()。