加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:41:46 所属栏目:百科 来源:网络整理
导读:查看文档 here,以下结构已明确定义: #pragma omp parallel //Line 1{#pragma omp for nowait //Line 3 for (i=0; iN; i++) a[i] = // some expression#pragma omp for //Line 6 for (i=0; iN; i++) b[i] = ...... a[i] ......} 以来 Here the nowait clause
查看文档 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],你必须绝对离开障碍.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读