是否可以“交叉折叠"?并行循环? [英] Is it possible to "cross collapse" parallel loops?

查看:214
本文介绍了是否可以“交叉折叠"?并行循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案之后,我实际上有三个循环的更复杂的代码:

Following this answer, I actually have more complicated code with three loops:

!$omp parallel 
!$omp do
do i=1,4 ! can be parallelized
  ...
  do k=1,1000 !to be executed sequentially
    ...
    do j=1,4  ! can be parallelized
    call job(i,j)

除了i=4,外循环很快完成.因此,我想在最内层的循环上启动线程,但在每个i迭代内依次离开k循环.实际上,k循环遍历随机数生成器的变化状态,因此无法并行化.

The outer loops finish quickly except for i=4. So I want to start threads on the innermost loop, but leaving the k-loop sequentially within each i-iteration. In fact, k loops over the changing states of a random number generator, so this cannot be parallelized.

如何仅折叠ij循环?我怀疑ordered子句在这里很有用,但是我担心它会再次影响内部循环,但我仍然不确定语法.

How can I collapse only the i and j loops? I suspect the ordered clause to be useful here, but I'm afraid that it would affect the inner loop again and still I'm unsure of the syntax.

推荐答案

在这种情况下,一种轻量且统一的方法是使用OpenMP任务.
您可以将它们用于两个并行循环,也可以仅用于内部循环.在第二种情况下,我们将具有for task构造的组合.该解决方案利用嵌套并行性,但避免了嵌套并行区域的影响. taskloop构造是等效的且更加自动化的方法.

A lightweight and uniform approach for this case is to use OpenMP tasks.
You can use them for both parallel loops or just for the inner one. In the second case, we will have a combination of the for and task constructs. This solution exploits nested parallelism but avoids the implications of nested parallel regions. The taskloop construct is an equivalent and more automated approach.

这篇关于是否可以“交叉折叠"?并行循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆