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