diff --git a/README.md b/README.md index 2546d47..c16180e 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,9 @@ type(timer) :: t call t%timer_start() ! Your code or section to be timed -call t%timer_stop() +call t%timer_stop(nloops, message, print) ! nloops, message and print are optional + +call t%timer_write(file_name) ! Optionally, write the result to a file ``` ### Measuring CPU time @@ -43,7 +45,9 @@ type(timer) :: t call t%ctimer_start() ! Your code or section to be timed -call t%ctimer_stop() +call t%ctimer_stop(nloops, message, print) ! nloops, message and print are optional + +call t%ctimer_write(file_name) ! Optionally, write the result to a file ``` ### Measuring OpenMP (OMP) time @@ -56,7 +60,9 @@ type(timer) :: t call t%otimer_start() ! Your code or section to be timed -call t%otimer_stop() +call t%otimer_stop(nloops, message, print) ! nloops, message and print are optional + +call t%otimer_write(file_name) ! Optionally, write the result to a file ``` **Note:** Ensure you compile with the `-DUSE_OMP` option when using the OpenMP timer. @@ -71,7 +77,9 @@ type(timer) :: t call t%mtimer_start() ! Your code or section to be timed -call t%mtimer_stop() +call t%mtimer_stop(nloops, message, print) ! nloops, message and print are optional + +call t%mtimer_write(file_name) ! Optionally, write the result to a file ``` **Note:** Don't forget to compile with the `-DUSE_MPI` option when using the MPI timer. diff --git a/example/example2.f90 b/example/example2.f90 index 35d0561..92522dc 100644 --- a/example/example2.f90 +++ b/example/example2.f90 @@ -11,7 +11,7 @@ program example2 do nl = 1, nloops call sleep(1) ! Perform operations ntimes end do - call t%timer_stop(nloops = nloops, message = 'Elapsed time:') ! nloops and message are optional. + call t%timer_stop(nloops = nloops, message = 'Elapsed time:', print = .true.) ! nloops, message and print are optional. call t%timer_write('example/example2_etimes') ! Optionally, write the elapsed time to a file end program example2 diff --git a/example/example4.f90 b/example/example4.f90 index f6fb4cc..fd415b7 100644 --- a/example/example4.f90 +++ b/example/example4.f90 @@ -11,7 +11,7 @@ program example4 do nl = 1, nloops call sleep(1) ! Perform operations ntimes end do - call t%ctimer_stop(nloops = nloops, message = 'CPU time:') ! nloops and message are optional + call t%ctimer_stop(nloops = nloops, message = 'CPU time:', print = .true.) ! nloops, message and print are optional call t%ctimer_write('example/example4_ctimes') ! Optionally, write the elapsed time to a file end program example4 diff --git a/fpm.toml b/fpm.toml index 736de05..6f6805e 100644 --- a/fpm.toml +++ b/fpm.toml @@ -87,6 +87,66 @@ name = "test12" source-dir = "test" main = "test12.f90" +[[test]] +name = "test13" +source-dir = "test" +main = "test13.f90" + +[[test]] +name = "test14" +source-dir = "test" +main = "test14.f90" + +[[test]] +name = "test15" +source-dir = "test" +main = "test15.f90" + +[[test]] +name = "test16" +source-dir = "test" +main = "test16.f90" + +[[test]] +name = "test17" +source-dir = "test" +main = "test17.f90" + +[[test]] +name = "test18" +source-dir = "test" +main = "test18.f90" + +[[test]] +name = "test19" +source-dir = "test" +main = "test19.f90" + +[[test]] +name = "test20" +source-dir = "test" +main = "test20.f90" + +[[test]] +name = "test21" +source-dir = "test" +main = "test21.f90" + +[[test]] +name = "test22" +source-dir = "test" +main = "test22.f90" + +[[test]] +name = "test23" +source-dir = "test" +main = "test23.f90" + +[[test]] +name = "test24" +source-dir = "test" +main = "test24.f90" + [[example]] name = "example1" source-dir = "example" diff --git a/src/fortime.f90 b/src/fortime.f90 index 88d8638..78221e0 100644 --- a/src/fortime.f90 +++ b/src/fortime.f90 @@ -83,11 +83,12 @@ end subroutine timer_start !> author: Seyed Ali Ghasemi !> Stops the timer and calculates the elapsed time. !> Optionally, it can print a message along with the elapsed time. - impure subroutine timer_stop(this, nloops, message) + impure subroutine timer_stop(this, nloops, message, print) class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg + logical, intent(in), optional :: print ! Stop the timer call system_clock(count=this%clock_end) @@ -107,7 +108,12 @@ impure subroutine timer_stop(this, nloops, message) else msg = message end if - print '(A, F7.3, " [s]")', trim(msg), this%elapsed_time + + if (present(print)) then + if (print) call print_time(this%elapsed_time, msg) + else + call print_time(this%elapsed_time, msg) + end if ! Deallocate the message if (allocated(msg)) deallocate(msg) @@ -163,11 +169,12 @@ end subroutine ctimer_start !> author: Seyed Ali Ghasemi !> Stops the timer and calculates the CPU time. !> Optionally, it can print a message along with the CPU time. - impure subroutine ctimer_stop(this, nloops, message) + impure subroutine ctimer_stop(this, nloops, message, print) class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg + logical, intent(in), optional :: print ! Stop the timer call cpu_time(this%cpu_end) @@ -185,7 +192,12 @@ impure subroutine ctimer_stop(this, nloops, message) else msg = message end if - print '(A, F16.9, " [s]")', trim(msg), this%cpu_time + + if (present(print)) then + if (print) call print_time(this%cpu_time, msg) + else + call print_time(this%cpu_time, msg) + end if ! Deallocate the message if (allocated(msg)) deallocate(msg) @@ -245,12 +257,13 @@ end subroutine otimer_start !> author: Seyed Ali Ghasemi !> Stops the timer and calculates the OMP time. !> Optionally, it can print a message along with the OMP time. - impure subroutine otimer_stop(this, nloops, message) + impure subroutine otimer_stop(this, nloops, message, print) use omp_lib class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg + logical, intent(in), optional :: print ! Stop the timer this%omp_end = omp_get_wtime() @@ -268,7 +281,12 @@ impure subroutine otimer_stop(this, nloops, message) else msg = message end if - print '(A, F16.9, " [s]")', trim(msg), this%omp_time + + if (present(print)) then + if (print) call print_time(this%omp_time, msg) + else + call print_time(this%omp_time, msg) + end if ! Deallocate the message if (allocated(msg)) deallocate(msg) @@ -339,12 +357,13 @@ end subroutine mtimer_start !> author: Seyed Ali Ghasemi !> Stops the timer and calculates the MPI time. !> Optionally, it can print a message along with the MPI time. - impure subroutine mtimer_stop(this, nloops, message) + impure subroutine mtimer_stop(this, nloops, message, print) ! include 'mpif.h' class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg + logical, intent(in), optional :: print interface function mpi_wtime() @@ -369,7 +388,12 @@ end function mpi_wtime else msg = message end if - print '(A, F16.9, " [s]")', trim(msg), this%mpi_time + + if (present(print)) then + if (print) call print_time(this%mpi_time, msg) + else + call print_time(this%mpi_time, msg) + end if ! Deallocate the message if (allocated(msg)) deallocate(msg) @@ -409,4 +433,15 @@ end subroutine mtimer_write !=============================================================================== #endif + + !=============================================================================== + !> author: Seyed Ali Ghasemi + impure subroutine print_time(time, message) + real(rk), intent(in) :: time + character(*), intent(in) :: message + + print '(A, F16.9, " [s]")', trim(message), time + end subroutine print_time + !=============================================================================== + end module fortime diff --git a/test/test13.f90 b/test/test13.f90 new file mode 100644 index 0000000..e427c6a --- /dev/null +++ b/test/test13.f90 @@ -0,0 +1,21 @@ +program test13 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + + + ! Elapsed time + call t%timer_start() + call sleep(1) ! Perform operations here + call t%timer_stop(print=.false.) + + call ut%check(res=t%elapsed_time, expected=1.0_rk, tol=1.0e-3_rk, msg='test13') + +end program test13 + diff --git a/test/test14.f90 b/test/test14.f90 new file mode 100644 index 0000000..b3c0965 --- /dev/null +++ b/test/test14.f90 @@ -0,0 +1,25 @@ +program test14 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: nl, nloops=3 + + + ! Elapsed time with nloops + call t%timer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%timer_stop(nloops = nloops, message = 'Elapsed time:', print=.false.) + call t%timer_write('test/test14_etimes') ! Optionally, write the elapsed time to a file + + call ut%check(res=t%elapsed_time, expected=1.0_rk, tol=1.0e-3_rk, msg='test14') + +end program test14 + diff --git a/test/test14_etimes b/test/test14_etimes new file mode 100644 index 0000000..4f3e823 --- /dev/null +++ b/test/test14_etimes @@ -0,0 +1 @@ +1.000266666666667 diff --git a/test/test15.f90 b/test/test15.f90 new file mode 100644 index 0000000..9923fd5 --- /dev/null +++ b/test/test15.f90 @@ -0,0 +1,25 @@ +program test15 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: nl, nloops=3 + + + ! Elapsed time with nloops + call t%timer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%timer_stop(message = 'Elapsed time:', print=.false.) + call t%timer_write('test/test15_etimes') ! Optionally, write the elapsed time to a file + + call ut%check(res=t%elapsed_time, expected=real(nloops,rk)*1.0_rk, tol=1.0e-3_rk, msg='test15') + +end program test15 + diff --git a/test/test15_etimes b/test/test15_etimes new file mode 100644 index 0000000..13eb934 --- /dev/null +++ b/test/test15_etimes @@ -0,0 +1 @@ +3.000400000000000 diff --git a/test/test16.f90 b/test/test16.f90 new file mode 100644 index 0000000..c3bc575 --- /dev/null +++ b/test/test16.f90 @@ -0,0 +1,17 @@ +program test16 + + use kinds + use fortime + + implicit none + + type(timer) :: t + + + ! CPU time + call t%ctimer_start() + call sleep(1) ! Perform operations here + call t%ctimer_stop(print=.false.) + +end program test16 + diff --git a/test/test17.f90 b/test/test17.f90 new file mode 100644 index 0000000..3f77fff --- /dev/null +++ b/test/test17.f90 @@ -0,0 +1,21 @@ +program test17 + + use kinds + use fortime + + implicit none + + type(timer) :: t + integer :: nl, nloops=3 + + + ! CPU time with nloops + call t%ctimer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%ctimer_stop(nloops = nloops, message = 'CPU time:', print=.false.) + call t%ctimer_write('test/test17_ctimes') ! Optionally, write the elapsed time to a file + +end program test17 + diff --git a/test/test17_ctimes b/test/test17_ctimes new file mode 100644 index 0000000..248cd03 --- /dev/null +++ b/test/test17_ctimes @@ -0,0 +1 @@ +.2999999999999993E-04 diff --git a/test/test18.f90 b/test/test18.f90 new file mode 100644 index 0000000..364a441 --- /dev/null +++ b/test/test18.f90 @@ -0,0 +1,21 @@ +program test18 + + use kinds + use fortime + + implicit none + + type(timer) :: t + integer :: nl, nloops=3 + + + ! CPU time with nloops + call t%ctimer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%ctimer_stop(message = 'CPU time:', print=.false.) + call t%ctimer_write('test/test18_ctimes') ! Optionally, write the elapsed time to a file + +end program test18 + diff --git a/test/test18_ctimes b/test/test18_ctimes new file mode 100644 index 0000000..a2d8104 --- /dev/null +++ b/test/test18_ctimes @@ -0,0 +1 @@ +.7799999999999994E-04 diff --git a/test/test19.f90 b/test/test19.f90 new file mode 100644 index 0000000..d265383 --- /dev/null +++ b/test/test19.f90 @@ -0,0 +1,23 @@ +program test19 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + + +#if defined(USE_OMP) + ! OMP time + call t%otimer_start() + call sleep(1) ! Perform operations here + call t%otimer_stop(print=.false.) + + call ut%check(res=t%omp_time, expected=1.0_rk, tol=1.0e-3_rk, msg='test19') +#endif + +end program test19 + diff --git a/test/test20.f90 b/test/test20.f90 new file mode 100644 index 0000000..54a39e2 --- /dev/null +++ b/test/test20.f90 @@ -0,0 +1,28 @@ +program test20 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: nl, nloops=3 + + +#if defined(USE_OMP) + ! OMP time with nloops + call t%otimer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%otimer_stop(nloops = nloops, message = 'OMP time:', print=.false.) + call t%otimer_write('test/test20_otimes') ! Optionally, write the elapsed time to a file + + call ut%check(res=t%omp_time, expected=1.0_rk, tol=1.0e-3_rk, msg='test20') + +#endif + +end program test20 + diff --git a/test/test20_otimes b/test/test20_otimes new file mode 100644 index 0000000..a1780a6 --- /dev/null +++ b/test/test20_otimes @@ -0,0 +1 @@ +1.000096003214518 diff --git a/test/test21.f90 b/test/test21.f90 new file mode 100644 index 0000000..2e19888 --- /dev/null +++ b/test/test21.f90 @@ -0,0 +1,28 @@ +program test21 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: nl, nloops=3 + + +#if defined(USE_OMP) + ! OMP time with nloops + call t%otimer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%otimer_stop(message = 'OMP time:', print=.false.) + call t%otimer_write('test/test21_otimes') ! Optionally, write the elapsed time to a file + + call ut%check(res=t%omp_time, expected=real(nloops,rk)*1.0_rk, tol=1.0e-3_rk, msg='test21') + +#endif + +end program test21 + diff --git a/test/test21_otimes b/test/test21_otimes new file mode 100644 index 0000000..84ad368 --- /dev/null +++ b/test/test21_otimes @@ -0,0 +1 @@ +3.000643968582153 diff --git a/test/test22.f90 b/test/test22.f90 new file mode 100644 index 0000000..fb596cb --- /dev/null +++ b/test/test22.f90 @@ -0,0 +1,27 @@ +program test22 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: ierr + + +#if defined(USE_MPI) + ! MPI time + call mpi_init(ierr) + call t%mtimer_start() + call sleep(1) ! Perform operations here + call t%mtimer_stop(print=.false.) + call mpi_finalize(ierr) + + call ut%check(res=t%mpi_time, expected=1.0_rk, tol=1.0e-3_rk, msg='test22') + +#endif + +end program test22 + diff --git a/test/test23.f90 b/test/test23.f90 new file mode 100644 index 0000000..b94f9ef --- /dev/null +++ b/test/test23.f90 @@ -0,0 +1,31 @@ +program test23 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: nl, nloops=3 + integer :: ierr + + +#if defined(USE_MPI) + ! MPI time with nloops + call mpi_init(ierr) + call t%mtimer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%mtimer_stop(nloops = nloops, message = 'MPI time:', print=.false.) + call mpi_finalize(ierr) + call t%mtimer_write('test/test23_mtimes') ! Optionally, write the elapsed time to a file + + call ut%check(res=t%mpi_time, expected=1.0_rk, tol=1.0e-3_rk, msg='test23') + +#endif + +end program test23 + diff --git a/test/test23_mtimes b/test/test23_mtimes new file mode 100644 index 0000000..0772ac0 --- /dev/null +++ b/test/test23_mtimes @@ -0,0 +1 @@ +1.000359982597881 diff --git a/test/test24.f90 b/test/test24.f90 new file mode 100644 index 0000000..697aa16 --- /dev/null +++ b/test/test24.f90 @@ -0,0 +1,31 @@ +program test24 + + use kinds + use fortime + use forunittest + + implicit none + + type(timer) :: t + type(unit_test) :: ut + integer :: nl, nloops=3 + integer :: ierr + + +#if defined(USE_MPI) + ! MPI time with nloops + call mpi_init(ierr) + call t%mtimer_start() + do nl = 1, nloops + call sleep(1) ! Perform operations ntimes + end do + call t%mtimer_stop(message = 'MPI time:', print=.false.) + call mpi_finalize(ierr) + call t%mtimer_write('test/test24_mtimes') ! Optionally, write the elapsed time to a file + + call ut%check(res=t%mpi_time, expected=real(nloops,rk)*1.0_rk, tol=1.0e-3_rk, msg='test24') + +#endif + +end program test24 + diff --git a/test/test24_mtimes b/test/test24_mtimes new file mode 100644 index 0000000..7824dca --- /dev/null +++ b/test/test24_mtimes @@ -0,0 +1 @@ +3.000666676512537