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


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.

5

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