I execute the following code using two cases:
"$omp sections" and "!$omp sections private(thread_num)"
Is it done each section by a different thread in both cases?
program main
use omp_lib
implicit none
integer, parameter:: ma=100, n=10000, mb= 100
real, dimension (ma,n) :: a
real, dimension (n,mb) :: b
real, dimension (ma,mb) :: c = 0.
integer:: i,j,k, threads=2, ppt, thread_num
integer:: toc, tic, rate
real:: time_parallel, time
call random_number (a)
call random_number (b)
!/////////////////////// PARALLEL PRIVATE ///////////////////////
c=0
CALL system_clock(count_rate=rate)
call system_clock(tic)
ppt = ma/threads
!$ call omp_set_num_threads(threads)
!$omp parallel
!$omp sections private(thread_num) !(HERE IS THE QUESTION TOPIC)
! EXAMPLE PROCESS 1 (it is only an example to test 'omp sections')
!$omp section
!$ thread_num = omp_get_thread_num()
!$ print*, "Section 1 started by thread number:", thread_num
do i= 1,50
do j= 1,mb
do k= 1,n
c(i,j) = c(i,j) + a(i,k)*b(k,j)
end do
end do
end do
!$ print*, "Section 1 finished by thread number:", thread_num
! EXAMPLE PROCESS 2
!$omp section
!$ thread_num = omp_get_thread_num()
!$ print*, "Section 2 started by thread number:", thread_num
do i= 51,100
do j= 1,mb
do k= 1,n
c(i,j) = c(i,j) + a(i,k)*b(k,j)
end do
end do
end do
!$ print*, "Section 2 finished by thread number:", thread_num
!$omp end sections
!$omp end parallel
print*, '//////////////////////////////////////////////////////////////'
print*, 'Result in Parallel'
!$ print*, c(85:90,40)
call system_clock(toc)
time_parallel = real(toc-tic)/real(rate)
!/////////////////////// normal execution ///////////////////////
c = 0
CALL system_clock(count_rate=rate)
call system_clock(tic)
call system_clock(tic)
do i= 1,ma
do j= 1,mb
do k= 1,n
c(i,j) = c(i,j) + a(i,k)*b(k,j)
end do
end do
end do
call system_clock(toc)
time = real(toc-tic)/real(rate)
print*, 'Result in serial mode'
print*, c(85:90,40)
print*, '------------------------------------------------'
print*, 'Threads: ', threads, '| Time Parallel ', time_parallel, 's '
print*, ' Time Normal ', time, 's'
!----------------------------------------------------------------
end program main
That is the result for "!$omp sections" and "!$omp sections private(thread_num)", respectively :
Section 1 started by thread number: 1
Section 2 started by thread number: 1
Section 1 finished by thread number: 1
Section 2 finished by thread number: 1
//////////////////////////////////////////////////////////////
Result in Parallel
2507.23853 2494.16162 2496.83960 2503.58960 2509.34448
2518.64160
Result in serial mode
2507.23853 2494.16162 2496.83960 2503.58960 2509.34448
2518.64160
Threads: 2 | Time Parallel 0.428116574
Time Normal 0.605000019 s
Section 1 started by thread number: 0
Section 2 started by thread number: 1
Section 1 finished by thread number: 0
Section 2 finished by thread number: 1
//////////////////////////////////////////////////////////////
Result in Parallel
2523.38281 2501.28369 2517.81860 2502.66235 2503.13940
2532.35791
Result in serial mode
2523.38281 2501.28369 2517.81860 2502.66235 2503.13940
2532.35791
Threads: 2 | Time Parallel 0.432999998
Time Normal 0.610204018 s
It was compiled using:
gfortran -Wall -fopenmp -O2 -Wall -o prog.exe prueba.f90 ./prog.exe
CPU model of my laptop:
AMD A6-6310 (4 cores and a thread per core)
P.S: the main goal is to test parallel clauses not to speedup matrix calculations
thread_num
should definitely be a private variable. Otherwise both threads use the same variable and hence you got the value 1
from both threads. It is a race condition to write from two threads to the same variable.
You can make it private for the whole parallel region and only call omp_get_thread_num()
once at the start of the region.
!$omp parallel private(thread_num)
!$ thread_num = omp_get_thread_num()
!$omp sections
!$omp section
!$ print*, "Section 1 started by thread number:", thread_num
...