是否有可能“交叉倒塌”?并行循环? [英] Is it possible to "cross collapse" parallel loops?

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

问题描述

遵循这个答案,我实际上有三个循环的更复杂的代码:

 !$ omp parallel 
!$ omp do
do i = 1,4!可以并行执行
...
do k = 1,1000!将按顺序执行$ ​​b $ b ...
do j = 1,4!可以并行
call job(i,j)

i = 4的。所以我想在最内层的循环中启动线程,但是在每个 i -iteration内依次保留 k -loop。事实上, k 在随机数发生器的变化状态上循环,所以这不能被并行化。



如何只折叠 i j 循环?我怀疑命令子句在这里很有用,但我担心它会再次影响内部循环,但我仍然不确定语法。

解决方案

这种情况下的轻量级和统一方法是使用OpenMP任务。

您可以将它们用于两个并行循环或只为内在的一个。在第二种情况下,我们将用于任务结构的组合。该解决方案利用嵌套并行性,但避免了嵌套并行区域的影响。 taskloop 构造是一种等效且更自动的方法。

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)

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.

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.

解决方案

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天全站免登陆