gpt4 book ai didi

parallel-processing - OpenMP 任务循环 : synchronization between two consecutive taskloop constructs

转载 作者:行者123 更新时间:2023-12-04 08:01:10 25 4
gpt4 key购买 nike

两个如何同步 taskloop构造完成了吗?具体来说,在下面的伪代码中,如果可用线程数多于第一个循环的任务数,我相信这些空闲线程会在单个构造末尾的隐式障碍处旋转。现在是否允许这些空闲线程同时开始执行第二个循环,从而使以这种方式并行化事情变得不安全(由于对数组 A 的相互依赖性)?

!$omp parallel
!$omp single

!$omp taskloop num_tasks(10)
DO i=1, 10
A(i) = foo()
END DO
!$omp end taskloop

!do other stuff

!$omp taskloop
DO j=1, 10
B(j) = A(j)
END DO
!$omp end taskloop

!$omp end single
!$omp end parallel
我一直无法从 API 规范中找到明确的答案: https://www.openmp.org/spec-html/5.0/openmpsu47.html#x71-2080002.10.2

最佳答案

taskloop构造默认有一个隐式 taskgroup周围。考虑到这一点,您的代码会发生什么single构造从并行团队的可用线程中挑选任何一个线程(我将其称为生产者线程)。然后将 n-1 个其他线程直接发送到 single 的屏障。构造和生产等待工作到达(任务)。
现在与 taskgroup发生的事情是生产者线程开始创建循环任务,然后在 taskloop 的末尾等待。构造所有创建的任务以完成:

!$omp parallel
!$omp single

!$omp taskloop num_tasks(10)
DO i=1, 10
A(i) = foo()
END DO
!$omp end taskloop ! producer waits here for all loop tasks to finish

!do other stuff

!$omp taskloop
DO j=1, 10
B(j) = A(j)
END DO
!$omp end taskloop ! producer waits here for all loop tasks to finish

!$omp end single
!$omp end parallel
因此,如果您的并行度(= 第一个 taskloop 创建的任务数)少于屏障中的 n-1 个工作线程,那么其中一些线程将空闲。
如果您想要更多重叠并且“其他东西”独立于第一个 taskloop ,那么你可以这样做:
!$omp parallel
!$omp single


!$omp taskgroup
!$omp taskloop num_tasks(10) nogroup
DO i=1, 10
A(i) = foo()
END DO
!$omp end taskloop ! producer will not wait for the loop tasks to complete

!do other stuff

!$omp end taskgroup ! wait for the loop tasks (and their descendant tasks)

!$omp taskloop
DO j=1, 10
B(j) = A(j)
END DO
!$omp end taskloop

!$omp end single
!$omp end parallel
唉,从 5.1 版开始,OpenMP API 不支持 taskloop 构造的任务依赖性,因此您无法轻松描述第一个 taskloop 的循环迭代之间的依赖性。第二个 taskloop . OpenMP 语言委员会目前正在为此开展工作,但我认为 OpenMP API 5.2 版没有实现,而是 6.0 版实现了这一点。
PS(编辑):对于第二个 taskloop因为它就在 single 结束之前构造,因此就在屏障之前,您可以轻松添加 nogroup也可以避免等待生产者线程的额外时间。

关于parallel-processing - OpenMP 任务循环 : synchronization between two consecutive taskloop constructs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66455187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com