0
votes

I am working in Fortran. My purpose is to parallelize with OPENMP a program of this kind:

do t=1, t_fin  (sequential)
   a sequence of do loops (not sequential)
end do

where the cycle is sequential (temporal). I have tried to create threads at the beginning of each sequential cycle iteration, but this makes the code slower than it could be. Therefore, my question is if it possible to create the threads only once before starting the cycle. Practically:

!$OMP PARALLEL

!$OMP SECTIONS
!$OMP CRITICAL
do t=1, t_fin
    sequence of OMP do
end do
!$OMP END CRITICAL
!$OMP END PARALLEL

I have tried in this way, but it works as if it were only one thread. I suppose this depends on the fact that there is an external critical section that includes the omp do. However, I would like to execute the internal omp do with more than one thread. Is there a way to obtain this?

1
I'm not sure what your question actually is. Why don't you parallelize your loop using a single omp parallel do directive? Why do you use sections? Why do you you critical sections? You are aware of the fact that only a single thread is allowed in the critical section at a time, right? Your code doesn't make sense... - Alexander Vogt

1 Answers

2
votes

If I understand your question correctly, you want to avoid creating threads in each iteration of the outer loop. This can be achieved by taking the OMP PARALLEL directive outside the loop, and leave the other statements inside the loop. Then I see two parallelization schemes.

You could either parallelize the inner loops:

!$OMP PARALLEL
do t=1, t_fin  (sequential)
   !$OMP DO
   first loop
   !$OMP END DO

   !$OMP DO
   second loop
   !$OMP END DO

   !...
end do
!$OMP END PARALLEL

or, use sections to run the loops in parallel (if they are independent of each other):

!$OMP PARALLEL
do t=1, t_fin  (sequential)
   !$OMP SECTIONS

   !$OMP SECTION 
   first loop
   !$OMP END SECTION

   !$OMP SECTION
   second loop
   !$OMP END SECTION

   !...
   !$OMP END SECTIONS
end do
!$OMP END PARALLEL

In both versions, the threads are created outside the loop.