fortran & openmp: put multiple do-s and section-s in the same parallel enviroment

不问归期 提交于 2020-01-07 09:49:19

问题


I have some serial codes like this:

do i=1,N  
    ...  
end do

do j=1,M  
    ...  
end do

...(1)

...(2)

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?

Thanks!


回答1:


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

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

!omp parallel

and not destructed until

!omp end parallel

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.



来源:https://stackoverflow.com/questions/25576916/fortran-openmp-put-multiple-do-s-and-section-s-in-the-same-parallel-enviromen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!