如何告诉openmp不要同步一个数组 [英] How to tell openmp not to synchronize an array
问题描述
#pragma omp parallel for
for(i = 0; i {
.....
index = get_index(...);
array [index] = ...;
.....
}
现在, 如何让openmp不要为 index
对于每个线程来说都是唯一的(它不会为不同的线程重叠),但是当然OpenMP不能对此进行猜测,我想使用同步对象来访问
array $ c $使用同步对象c>并依靠我,
index
值对于不同的线程是唯一的。我尝试在私人列表中放入 array
,但是出现了分段错误。
结尾有一个隐含的障碍。
但是openmp不会通过访问为你同步内存。相反,它为每个线程提供共享和私有内存块的机制。
在你的情况下, index
需要是私人的。否则,当你访问 array [index]
时,每个线程写入相同的内存位置,并且你将遇到竞争状态。
为了演示,我明确设置了行为,虽然默认情况下 i
是私有的,
#pragma omp parallel for private(i,index)shared(array)
for(i = 0; i < N; i ++)
{
.....
index = get_index(...);
array [index] = ...;
.....
}
I have a code that has the following structure.
#pragma omp parallel for
for( i = 0; i < N; i++ )
{
.....
index = get_index(...);
array[index] = ...;
.....
}
Now the value of index
is unique for each thread (it never gets overlapped for different threads), but of course OpenMP can't make a guess for this and I suppose is using synchronization objects to access array
.
How can I ask openmp not to use synchronization objects for array
and rely on me that index
value is unique for different threads. I try to put array
in private list, but got segmentation fault for that.
Openmp does synchronize at barriers or implicit barriers. For example, there is an implicit barrier at the end of a for
construct unless a nowait clause is specified.
But openmp does not synchronize memory by access for you. Instead, it provides mechanisms for shared and private memory blocks for each thread.
In your case, index
needs to be private. Otherwise each thread writes to the same memory location and you will have a race condition, when you access array[index]
.
For demonstration, I set the behavior explicitly, although i
is private by default and array
shared by default.
#pragma omp parallel for private(i, index) shared(array)
for( i = 0; i < N; i++ )
{
.....
index = get_index(...);
array[index] = ...;
.....
}
这篇关于如何告诉openmp不要同步一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!