Dri*_*zel 2 c parallel-processing multithreading simd openmp
我正在尝试计算矩阵中相邻元素的平均值,但无法让 OpenMP 的矢量化正常工作。据我了解第二个嵌套 for 循环,该reduction子句应确保在写入 的元素时不会发生竞争条件next。但是,在编译代码时(我尝试使用 GCC GCC 7.3.0 和 ICC 以及 OpenMP > 4.5 进行自动矢量化),我收到报告:“错误:在进入此 OpenMP 编译指示时必须共享缩减变量“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)];
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是GCC 7.3.0不支持
#pragma omp for simd collapse(4) reduction(+:next[0:width*height])
Run Code Online (Sandbox Code Playgroud)
在这种情况下使用reduction数组部分。
GCC 9转发支持此功能:
自 GCC 9 起,开始提供 OpenMP 5 支持(本质上仅限于 C/C++)。GCC 10 添加了更多功能,主要针对 C/C++,但也针对 Fortran。