gpt4 book ai didi

c++ - 在 GCC 中不能同时支持 OpenMP v4 和 v5

转载 作者:行者123 更新时间:2023-12-04 09:25:47 25 4
gpt4 key购买 nike

在尝试使我的 C/C++ 项目同时支持 gcc-8(使用 OpenMP 4.5)和 gcc-9(使用 OpenMP 5.0)时,我遇到了一个不寻常的问题。它是由定义为 const 的变量引起的,这些变量将在线程之间共享。

例如,这里有一些代码与 OpenMP 5 兼容,但在 OpenMP 4.5 上失败(错误 'x' is predefined 'shared' for 'shared'):

const x = 10;
int i;

# pragma omp parallel \
default (none) \
shared (x) \
private (i)
{
# pragma omp for schedule (static)
for (i=0; i<x; i++)
// etc
}

上述结果也与 clang-10 兼容,但不是 clang-3.7

这是与 OpenMP 4.5 兼容的相同代码(只是从 shared 中排除了 x),但在 OpenMP 5 中失败(错误 error: 'numTasks' not在封闭的“并行”中指定):

const x = 10;
int i;

# pragma omp parallel \
default (none) \
private (i)
{
# pragma omp for schedule (static)
for (i=0; i<x; i++)
// etc
}

OpenMP 5 似乎不再智能地假定 const 变量是共享的。唯一可以在两者上编译的代码是不必要地将 x 放在 firstprivate 中的代码:

const x = 10;
int i;

# pragma omp parallel \
default (none) \
firstprivate (x) \
private (i)
{
# pragma omp for schedule (static)
for (i=0; i<x; i++)
// etc
}

这似乎是一个糟糕的解决方案,因为现在我要花钱将 x 复制到每个线程中,而它永远不会被修改!另请注意,我不能使 x 成为非 const,因为它实际上是调用函数的 const 参数。

这是怎么回事,制作不可知代码的正确/有效方法是什么?

最佳答案

移植到 gcc-9 的地址是 here .为了保持与两个版本的 OpenMP 的兼容性,我必须:

  • 删除默认(无)
  • 删除所有const

关于c++ - 在 GCC 中不能同时支持 OpenMP v4 和 v5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63016641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com