OpenMP SIMD reduction in array: "error: reduction variable must be shared on entry to this OpenMP pragma"

OpenMP SIMD reduction in array: "error: reduction variable must be shared on entry to this OpenMP pragma"

我正在尝试计算矩阵中相邻元素的平均值,但无法让 OpenMP 的矢量化发挥作用。据我了解,第二个嵌套的 for 循环,reduction 子句应确保在写入 next 的元素时不会出现竞争条件。但是,在编译代码时(我尝试使用 GCC GCC 7.3.0 和 ICC 以及 OpenMP > 4.5 进行自动矢量化)我收到报告:“错误:在进入此 OpenMP pragma 时必须共享缩减变量“next””。为什么在默认情况下共享变量时会出现这种情况?我该如何解决这个问题,因为添加 shared(next) 似乎没有帮助?

// CODE ABOVE (...)
size_t const width = 100;
size_t const height = 100;
float * restrict next = malloc(sizeof(float)*width*height);

// INITIALIZATION OF 'next' (this works fine)
#pragma omp for simd collapse(2) 
  for(size_t j = 1; j < height-1; j++)
    for(size_t i = 1; i < width-1; i++)
      next[j*width+i] = 0.0f; 

// COMPUTE AVERAGE FOR INNER ELEMENTS
#pragma omp for simd collapse(4) reduction(+:next[0:width*height]) 
for(size_t j = 1; j < height-1; ++j){
  for(size_t i = 1; i < width-1; ++i){
    // compute average over adjacent elements
    for(size_t _j = 0; _j < 3; _j++) {
      for(size_t _i = 0; _i < 3; _i++) {
          next[j*width + i] += (1.0 / 9.0) * next[(j-1 +_j)*width + (i-1 + _i)]; 
      }
    }
  }
}

问题是GCC 7.3.0不支持

#pragma omp for simd collapse(4) reduction(+:next[0:width*height]) 

在此上下文中使用 reduction 数组部分。

GCC 9转发支持该功能:

Since GCC 9, there is initial OpenMP 5 support (essentially C/C++, only). GCC 10 added some more features, mainly for C/C++ but also for Fortran.