arg*_*eus 9 c++ parallel-processing multithreading openmp
我已经将OpenMP添加到现有代码库中,以便并行化for循环.在parallel for区域范围内创建了几个变量,包括一个指针:
#pragma omp parallel for
for (int i = 0; i < n; i++){
[....]
Model *lm;
lm->myfunc();
lm->anotherfunc();
[....]
}
Run Code Online (Sandbox Code Playgroud)
在结果输出文件中,我注意到不一致,可能是由竞争条件引起的.我最终通过使用一个解决了竞争条件omp critical.我的问题仍然存在:lm每个线程都是私有的,还是共享的?
Mys*_*ial 11
是的,在OpenMP区域内声明的所有变量都是私有的.这包括指针.
每个线程都有自己的指针副本.
它可以让你做这样的事情:
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".
}
Run Code Online (Sandbox Code Playgroud)