The snippet of my serial code is shown below.
Program main
use omp_lib
Implicit None
Integer :: i, my_id
Real(8) :: t0, t1, t2, t3, a = 0.0d0
!$ t0 = omp_get_wtime()
Call CPU_time(t2)
! ------------------------------------------ !
Do i = 1, 100000000
a = a + Real(i)
End Do
! ------------------------------------------ !
Call CPU_time(t3)
!$ t1 = omp_get_wtime()
! ------------------------------------------ !
Write (*,*) "a = ", a
Write (*,*) "The wall time is ", t1-t0, "s"
Write (*,*) "The CPU time is ", t3-t2, "s"
End Program main
By using omp directives do
and atomic
, I convert serial code into parallel code. However, the parallel program is slower than the serial program. I don't understand why this happened. The next is my parallel code snippet:
Program main
use omp_lib
Implicit None
Integer, Parameter :: n_threads = 8
Integer :: i, my_id
Real(8) :: t0, t1, t2, t3, a = 0.0d0
!$ t0 = omp_get_wtime()
Call CPU_time(t2)
! ------------------------------------------ !
!$OMP Parallel Num_threads(n_threads) shared(a)
!$OMP Do
Do i = 1, 100000000
!$OMP Atomic
a = a + Real(i)
End Do
!$OMP End Do
!$OMP End Parallel
! ------------------------------------------ !
Call CPU_time(t3)
!$ t1 = omp_get_wtime()
! ------------------------------------------ !
Write (*,*) "a = ", a
Write (*,*) "The wall time is ", t1-t0, "s"
Write (*,*) "The CPU time is ", t3-t2, "s"
End Program main
So my question is Why my parallel code using openMP atomic takes a longer time than serial code?