c++ - OpenMP,调用全局变量,通过函数

标签 c++ visual-c++ openmp

执行以下代码时:

#pragma omp parallel for default(shared) private(rx,ry,a, Xmax,Ymax)
for (a=0;a<30000;a++)
{int mn,mnn;
mn=part.i;
mnn=part.j;
setup(mn,mnn,a);
}

函数设置使用全局变量 rx、ry、Xmax 和 Ymax。 Open MP 是否注意到这些被声明为私有(private)的?或者如果在循环中存在的函数中调用全局变量会发生什么情况?

最佳答案

函数中使用的全局变量将被共享,除非您使用threadprivate。因此,如果您在函数中调用它们,rx,ry, Xmax,Ymax 将不会是私有(private)的,除非您使用 threadprivate

在使用和不使用 threadprivate 的情况下尝试以下代码并查看结果。

#include <stdio.h>
int x;
//#pragma omp threadprivate(x)

void foo() {
    printf("%p\n", &x);
}

int main() {
    //#pragma omp parallel
    #pragma omp parallel private(x)
    {
        printf("%p\n", &x);
        foo();
    }
}

参见 OpenMP specificiation在“a中引用的变量的数据共享属性规则”下 区域但不在构造中”

File-scope or namespace-scope variables referenced in called routines in the region are shared unless they appear in a threadprivate directive.

关于c++ - OpenMP,调用全局变量,通过函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24948395/

相关文章:

mysql - 使用 windows api 函数 CreateProcessA 初始化 mysql

c++ - 使用特定于 2012 年 11 月 CTP 的 C++11 功能时,有没有办法抑制 Intellisense 错误?

c - 为什么在这种情况下会发生段错误? Openmp问题

c++ - 通用运算符 >> 用于 std::vector

c++ - 数组下标和其他类型无效 `int[int]'

c++ - 处理标志的首选方法是什么?

c - 解决在R中使用OpenMP时的封闭并行错误

C 与 OpenMP : Matrix times vector

c++ - Node.js 插件的 node-gyp 重建的 g++ 构建错误

c++ - 在 tbb 中正确使用访问器