如何告诉OpenMP的不同步的数组(How to tell openmp not to synchr

2019-09-17 06:30发布

我有一个具有以下结构的代码。

#pragma omp parallel for
for( i = 0; i < N; i++ )
{
    .....
    index = get_index(...);
    array[index] = ...;
    .....
}

现在的价值index是为每个线程唯一的(它永远不会重叠的不同的线程),但当然OpenMP的不能让这种猜测,我想用同步对象访问array

我怎么能要求的OpenMP不使用同步对象的array ,并依靠我, index值是不同的线程是唯一的。 我试图把array私人名单,但有段错误了点。

Answer 1:

OpenMP的并在障碍或隐含的障碍同步。 例如,有在一个端部的隐式屏障for除非指定了nowait子句是构建体。

但是OpenMP的不通过你同步访问内存。 相反,它提供了为每个线程共享和私有内存块的机制。

在你的情况下, index需要是私有的。 否则,每个线程写入相同的内存位置,你将有一个竞争条件,当你访问array[index]

为了演示,我设置的行为明确,虽然i是默认和私人array通过默认共享。

#pragma omp parallel for private(i, index) shared(array)
for( i = 0; i < N; i++ )
{
    .....
    index = get_index(...);
    array[index] = ...;
    .....
}


文章来源: How to tell openmp not to synchronize an array