在OpenMP并行段中指针是否是私有的? [英] Are pointers private in OpenMP parallel sections?

查看:1519
本文介绍了在OpenMP并行段中指针是否是私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将OpenMP添加到现有的代码库中,以并行化for循环。在 parallel 区域范围内创建了几个变量,包括一个指针:

  #pragma omp parallel for 
for(int i = 0; i [....]
Model * lm;
lm-> myfunc();
lm-> anotherfunc();
[...]
}

注意到不一致,可能是由于竞争条件。我最终通过使用 omp critical 解决了竞争条件。我的问题仍然是,

解决方案 lm / div>

是的,在OpenMP区域内声明的所有变量都是私有的。这包括指针。



每个线程都有自己的指针副本。



编辑:



所以它让你做这样的事情:

  int threads = 
int size_per_thread = 10000000;

int * ptr = new int [size_per_thread * threads];

#pragma omp parallel num_threads(threads)
{
int id = omp_get_thread_num();
int * my_ptr = ptr + size_per_thread * id;

//在my_ptr上工作。
}


I've added OpenMP to an existing code base in order to parallelize a for loop. Several variables are created inside the scope of the parallel for region, including a pointer:

#pragma omp parallel for
for (int i = 0; i < n; i++){
    [....]
    Model *lm;
    lm->myfunc();
    lm->anotherfunc();
    [....]
}

In the resulting output files I noticed inconsistencies, presumably caused by a race condition. I ultimately resolved the race condition by using an omp critical. My question remains, though: is lm private to each thread, or is it shared?

解决方案

Yes, all variables declared inside the OpenMP region are private. This includes pointers.

Each thread will have it's own copy of the pointer.

EDIT:

So it lets you do stuff like this:

int threads = 8;
int size_per_thread = 10000000;

int *ptr = new int[size_per_thread * threads];

#pragma omp parallel num_threads(threads)
    {
        int id = omp_get_thread_num();
        int *my_ptr = ptr + size_per_thread * id;

        //  Do work on "my_ptr".
    }

这篇关于在OpenMP并行段中指针是否是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆