c – 在两个连续的pragma omp的情况下隐式屏障vs nowait

查看文档 here,以下结构已明确定义:
#pragma omp parallel          //Line 1
{
#pragma omp for Nowait        //Line 3
  for (i=0; i<N; i++)
    a[i] = // some expression
#pragma omp for               //Line 6
  for (i=0; i<N; i++)
    b[i] = ...... a[i] ......
}

以来

Here the Nowait clause implies that threads can start on the second loop while other threads are still working on the first. Since the two loops use the same schedule here,an iteration that uses a[i] can indeed rely on it that that value has been computed.

我很难理解为什么会这样.假设第3行是:

#pragma omp for

然后,由于在第6行之前存在隐式屏障,因此下一个for循环将在完全计算的所有索引处具有值.但是,在第3行没有等待的情况下,它会如何运作?

假设,第1行触发4个线程,t1,t2,t3和t4.假设N为8,则第一个for循环中的索引分区为:

t1: 0,4
t2: 1,5
t3: 2,6
t4: 3,7

假设t1首先完成索引0和4并在第6行登陆现在究竟发生了什么?如何保证它现在可以在相同的索引0和4上运行,在上一次迭代中a值是否正确计算?如果第二个for循环访问[i 1]怎么办?

解决方法

你引用的材料是错误的.如果向两个循环添加调度(静态),它就变得正确了 – 这保证了连续循环的线程之间的索引分布相同.认计划是实现定义的,您不能认为它是静态的.引用标准:

Different loop regions with the same schedule and iteration count,
even if they occur in the same parallel region,can distribute
iterations among threads differently. The only exception is for the
static schedule as specified in Table 2.5. Programs that depend on
which thread executes a particular iteration under any other
circumstances are non-conforming.

如果第二个for循环访问[i 1],你必须绝对离开障碍.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...