首页 > 解决方案 > 为什么 Visual Studio 2019 在 Openmp 的 for-reduction 中不支持关键字“max”?

问题描述

当我像这样使用openmp时:

#pragma omp parallel for reduction(max: dumax)

IDE 将在 Openmp “reduction” 中引发错误 “max” is invalid

#pragma omp parallel for reduction(max: dumax)
for (int i = 1; i < n + 1; i++)
{
    for (int j = 1; j < n + 1; j++)
    {
    u[i][j] = 0.25 * u[i - 1][j] + 0.25 * u[i][j - 1] + 0.25 * u[i + 1][j] + 0.25 * u[i][j + 1] + h * h * f[i][j];
    dumax = max(dumax, abs(u[i][j] - uold[i][j]));
    }
}

标签: c++openmpvisual-studio-2019

解决方案


MSVC 编译器卡在 OpenMP 2.0 版中,不幸的是,reduction(max:)它仅在 OpenMP C/C++ 标准的 3.1 版中引入(即 2011 年 9 月)

因此,您可以更改编译器,也可以使用一些私有变量以旧方式进行归约操作,并通过critical累积进行最终归约


推荐阅读