fortran& openmp:将多个do-s和section-s放在同一并行环境中 [英] fortran & openmp: put multiple do-s and section-s in the same parallel enviroment

查看:326
本文介绍了fortran& openmp:将多个do-s和section-s放在同一并行环境中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像这样的序列号:

I have some serial codes like this:

do i=1,N  
    ...  
end do

do j=1,M  
    ...  
end do

...(1)

...(2)

上面显示了三个带有两个dos和两个独立块的串行代码块.我想将其改编成并行代码.我知道这样做的一种方式是:

Above showed three blocks of serial codes with two do-s and two independent blocks. and I want to adapt it into parallel codes. One way I know of doing is:

!$omp parallel do   
do i ...  
!$omp end parallel  

!$omp parallel do  
do j ...  
!$omp end parallel  

!$omp parallel  
!$omp section ...(1)  
!$omp section ...(2)
!$omp end parallel

请注意,通过这种方式,我正在线程执行四次.作为非专家,我不确定这是否会导致额外的管理时间.是否有可能将所有内容都放在一个并行环境中,并会改善间接费用时间?

Notice that in doing this way, I am threading four times. As a non-expert, I am not sure if this will cause extra overhead time. Would it be possible to put everything in one parallel environment and would it improve the overhead time?

谢谢!

推荐答案

您可以并且可能应该通过放置所有并行构造来分摊创建和销毁OpenMP并行区域的运行时开销-在这种情况下,两个do循环和一对部分-在一个平行区域内.以下代码按预期编译并执行.

You can and probably should amortize the runtime overhead of creating and destroying an OpenMP parallel region by putting all of your parallel constructions - in this case, two do loops and a pair of sections - within a single parallel region. The following code compiles and executes as expected.

program main
implicit none
integer i
!$omp parallel
!$omp do   
do i = 1, 1000
    print*,'loop 1 ',i
enddo
!$omp do   
do i = 1, 1000
    print*,'loop 2 ',i
enddo
!$omp sections
!$omp section
print*,'section 1'
!$omp section
print*,'section 2'
!$omp end sections
!$omp end parallel
return
end program main

请注意,在此程序中,OpenMP线程池是在该行中构造的

Note that in this program, the OpenMP thread pool is constructed in the line

!omp parallel

并且直到被破坏

!omp end parallel

您可以添加"nowait"来减少与OpenMP并行构造结束时隐式屏障相关的运行时开销,但前提是要保留代码的正确性.

You can add "nowait" to reduce the runtime overhead associated with implicit barriers at the end of OpenMP parallel constructs, but only if it preserves the correctness of the code.

这篇关于fortran& openmp:将多个do-s和section-s放在同一并行环境中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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