[英]Are pointers private in OpenMP parallel sections?
我已將OpenMP添加到現有代碼庫中,以並行化for循環。 在parallel for
區域的parallel for
范圍內創建了幾個變量,包括一個指針:
#pragma omp parallel for
for (int i = 0; i < n; i++){
[....]
Model *lm;
lm->myfunc();
lm->anotherfunc();
[....]
}
在生成的輸出文件中,我注意到了不一致,這大概是由於競爭條件造成的。 我最終通過使用omp critical
解決了比賽條件。 但是,我的問題仍然存在: lm
是每個線程專用的還是共享的?
是的,在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".
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.