微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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],你必须绝对离开障碍.

原文地址:https://www.jb51.cc/c/110602.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐