OpenMP 并行部分中的指针是私有的吗?
我已将 OpenMP 添加到现有代码库中,以便并行化 for 循环。在parallel for 区域的范围内创建了几个变量,包括一个指针:
#pragma omp parallel for
for (int i = 0; i < n; i++){
[....]
Model *lm;
lm->myfunc();
lm->anotherfunc();
[....]
}
在生成的输出文件中,我注意到不一致,可能是由竞争条件引起的。我最终使用 omp critical
解决了竞争条件。但我的问题仍然存在:lm
是每个线程私有的,还是共享的?
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?
是的,OpenMP 区域内声明的所有变量都是私有的。这包括指针。
每个线程都有自己的指针副本。
它可以让你做这样的事情:
Yes, all variables declared inside the OpenMP region are private. This includes pointers.
Each thread will have its own copy of the pointer.
It lets you do stuff like this: