Reputation: 734
I am trying to understand how to use OpenMP sections. The program listed below is extracted from one of the llnl tutorials, the explanation states: 'Simple program demonstrating that different blocks of work will be done by different threads'.
!!compile with: gfortran -fopenmp -o omp_worksections omp_worksections.f90 !! also need: export OMP_NUM_THREADS=2 (or 3 or 4)
PROGRAM WORKSECTIONS
INTEGER N, I, NTHREADS, TID, OMP_GET_NUM_THREADS,OMP_GET_THREAD_NUM
PARAMETER (N=1000)
REAL A(N), B(N), C(N), D(N)
! Some initializations
DO I = 1, N
A(I) = I * 1.5
B(I) = I + 22.35
C(N) = 0.0
D(N) = 0.0
ENDDO
!$OMP PARALLEL SHARED(A,B,C,D,NTHREADS), PRIVATE(I,TID)
TID = OMP_GET_THREAD_NUM()
IF (TID .EQ. 0) THEN
NTHREADS = OMP_GET_NUM_THREADS()
PRINT *, 'Number of threads =', NTHREADS
END IF
PRINT *, 'Thread',TID,' starting...'
!$OMP SECTIONS
!$OMP SECTION
PRINT *, 'Thread',TID,' doing section 1'
DO I = 1, N
C(I) = A(I) + B(I)
if (i.lt.10) then
WRITE(*,100) TID,I,C(I)
end if
100 FORMAT(' Thread',I2,': C(',I2,')=',F8.2)
ENDDO
!$OMP SECTION
PRINT *, 'Thread',TID,' doing section 2'
DO I = 1, N
if (i.lt.10) then
D(I) = A(I) * B(I)
WRITE(*,200) TID,I,D(I)
200 FORMAT(' Thread',I2,': D(',I2,')=',F8.2)
endif
ENDDO
!$OMP END SECTIONS NOWAIT
PRINT *, 'Thread',TID,' done.'
!$OMP END PARALLEL
END PROGRAM WORKSECTIONS
When I compile and run, the result is:
Number of threads = 2
Thread 0 starting...
Thread 0 doing section 1
Thread 0: C( 1)= 24.85
Thread 0: C( 2)= 27.35
Thread 0: C( 3)= 29.85
Thread 0: C( 4)= 32.35
Thread 0: C( 5)= 34.85
Thread 0: C( 6)= 37.35
Thread 0: C( 7)= 39.85
Thread 0: C( 8)= 42.35
Thread 0: C( 9)= 44.85
Thread 1 starting...
Thread 0 doing section 2
Thread 0: D( 1)= 35.03
Thread 0: D( 2)= 73.05
Thread 0: D( 3)= 114.08
Thread 0: D( 4)= 158.10
Thread 0: D( 5)= 205.12
Thread 0: D( 6)= 255.15
Thread 0: D( 7)= 308.18
Thread 0: D( 8)= 364.20
Thread 0: D( 9)= 423.23
Thread 0 done.
Thread 1 done.
It seems thread 0 does both sections 1 and 2? I was expecting the prints from either section to be interleaved with one thread doing section 1 and the other section 2.
I have tried removing the NOWAIT clause in the END SECTIONS DIRECTIVE, and removing C,D from the shared clause in the PARALLEL directive, to no avail.
I am obviously missing some central piece of the puzzle?
Upvotes: 1
Views: 659
Reputation: 59998
It appears that when the OpenMP runtime library is looking for a free thread for the second section, it finds thread 0 free again, because there is too little work to do in the first section. So it assigns the work to thread 0 again.
Try larger n
, like 100000:
Number of threads = 2
Thread 0 starting...
Thread 0 doing section 1
Thread 0: C( 1)= 24.85
Thread 0: C( 2)= 27.35
Thread 0: C( 3)= 29.85
Thread 0: C( 4)= 32.35
Thread 0: C( 5)= 34.85
Thread 1 starting...
Thread 1 doing section 2
Thread 1: D( 1)= 35.03
Thread 1: D( 2)= 73.05
Thread 1: D( 3)= 114.08
Thread 1: D( 4)= 158.10
Thread 1: D( 5)= 205.12
Thread 1: D( 6)= 255.15
Thread 1: D( 7)= 308.18
Thread 1: D( 8)= 364.20
Thread 1: D( 9)= 423.23
Thread 0: C( 6)= 37.35
Thread 0: C( 7)= 39.85
Thread 0: C( 8)= 42.35
Thread 0: C( 9)= 44.85
Thread 1 done.
Thread 0 done.
Upvotes: 1