Fortran OpenMP program shows no speedup of CPU_TIME()

cpu_time() takes the time on the CPU, not the walltime. In parallel applications these are not the same. See here for details.

Using system_clock() solves this problem:

    PROGRAM MAIN
    use omp_lib
    implicit none
    REAL*8 Times1,Times2
    INTEGER I,J, iTimes1,iTimes2, rate
    real, allocatable, dimension(:) :: a
    allocate(a(1000))

    CALL system_clock(count_rate=rate)
    DO J = 1, 1000
    a(j)=j  
    ENDDO
!    ***************NO PARALLEL CODE ************************************
    call CPU_TIME(Times1)
    call SYSTEM_CLOCK(iTimes1)
    write(*,*) 'CPU NO PARALLEL STARTED:',Times1
    DO I = 1, 1000
    DO J = 1, 500000
    a(I)=a(I)+0.0001
    end do 
    a(I)=a(I)+a(I)+a(I)
    ENDDO
    call CPU_TIME(Times2)
    call SYSTEM_CLOCK(iTimes2)
    write(*,*) 'CPU CPU NO PARALLEL finished:',Times2
    write(*,*) 'NO PARALLEL TIMES:',Times2-Times1, real(iTimes2-iTimes1)/real(rate)
    write(*,*) '---------------------------------------------------'
!    ***************PARALLEL CODE ************************************
    call CPU_TIME(Times1)
    call SYSTEM_CLOCK(iTimes1)
    write(*,*) 'CPU PARALLEL STARTED:',Times1
!$OMP PARALLEL DEFAULT(shared), private(I,J)
!$OMP DO
    DO I = 1, 1000
    DO J = 1, 500000
    a(I)=a(I)+0.0001
    end do 
    a(I)=a(I)+a(I)+a(I)
    ENDDO
!$OMP END DO
!$OMP END PARALLEL
    call CPU_TIME(Times2)
    call SYSTEM_CLOCK(iTimes2)

    write(*,*) 'CPU PARALLEL finished:',Times2
    write(*,*) 'PARALLEL TIMES:',Times2-Times1, real(iTimes2-iTimes1)/real(rate)
    deallocate(a)
    STOP
    END

Then, you can see that the parallel program is indeed faster.

 CPU NO PARALLEL STARTED:   4.0000000000000001E-003
 CPU CPU NO PARALLEL finished:   1.4600000000000000     
 NO PARALLEL TIMES:   1.4560000000000000        1.45400000    
 ---------------------------------------------------
 CPU PARALLEL STARTED:   1.4600000000000000     
 CPU PARALLEL finished:   5.1040000000000001     
 PARALLEL TIMES:   3.6440000000000001       0.920000017  

Leave a Comment