From 4a4189a4b83af2f32cd1453f2d5084f82c87d22d Mon Sep 17 00:00:00 2001 From: Seyed Ali Ghasemi Date: Mon, 22 Apr 2024 11:41:38 +0200 Subject: [PATCH] Add optional color argument to `print_time`. --- README.md | 10 +++++----- example/example2.f90 | 2 +- example/example4.f90 | 2 +- example/example6.f90 | 2 +- fpm.toml | 1 + src/fortime.f90 | 45 +++++++++++++++++++++++++++----------------- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index f5e2dd1..533f3af 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ type(timer) :: t call t%timer_start() ! Your code or section to be timed -call t%timer_stop(nloops, message, print) ! nloops, message and print are optional +call t%timer_stop(nloops, message, print, color) ! nloops, message, print and color are optional call t%timer_write(file_name) ! Optionally, write the result to a file ``` @@ -41,7 +41,7 @@ type(timer) :: t call t%dtimer_start() ! Your code or section to be timed -call t%dtimer_stop(nloops, message, print) ! nloops, message and print are optional +call t%dtimer_stop(nloops, message, print, color) ! nloops, message, print and color are optional call t%dtimer_write(file_name) ! Optionally, write the result to a file ``` @@ -54,7 +54,7 @@ type(timer) :: t call t%ctimer_start() ! Your code or section to be timed -call t%ctimer_stop(nloops, message, print) ! nloops, message and print are optional +call t%ctimer_stop(nloops, message, print, color) ! nloops, message, print and color are optional call t%ctimer_write(file_name) ! Optionally, write the result to a file ``` @@ -67,7 +67,7 @@ type(timer) :: t call t%otimer_start() ! Your code or section to be timed -call t%otimer_stop(nloops, message, print) ! nloops, message and print are optional +call t%otimer_stop(nloops, message, print, color) ! nloops, message, print and color are optional call t%otimer_write(file_name) ! Optionally, write the result to a file ``` @@ -82,7 +82,7 @@ type(timer) :: t call t%mtimer_start() ! Your code or section to be timed -call t%mtimer_stop(nloops, message, print) ! nloops, message and print are optional +call t%mtimer_stop(nloops, message, print, color) ! nloops, message, print and color are optional call t%mtimer_write(file_name) ! Optionally, write the result to a file ``` diff --git a/example/example2.f90 b/example/example2.f90 index 92522dc..5363f1a 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:', print = .true.) ! nloops, message and print are optional. + call t%timer_stop(nloops = nloops, message = 'Elapsed time:', print = .true., color='green') ! nloops, message, print and color 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 fd415b7..4c5662e 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:', print = .true.) ! nloops, message and print are optional + call t%ctimer_stop(nloops = nloops, message = 'CPU time:', print = .true., color='yellow') ! nloops, message, print and color are optional. call t%ctimer_write('example/example4_ctimes') ! Optionally, write the elapsed time to a file end program example4 diff --git a/example/example6.f90 b/example/example6.f90 index 0272bbc..7373018 100644 --- a/example/example6.f90 +++ b/example/example6.f90 @@ -11,7 +11,7 @@ program example6 do nl = 1, nloops call sleep(1) ! Perform operations ntimes end do - call t%dtimer_stop(nloops = nloops, message = 'Elapsed time:', print = .true.) ! nloops, message and print are optional. + call t%dtimer_stop(nloops = nloops, message = 'Elapsed time:', print = .true., color='red') ! nloops, message, print and color are optional. call t%dtimer_write('example/example6_etimes') ! Optionally, write the elapsed time to a file end program example6 diff --git a/fpm.toml b/fpm.toml index 8bad114..b057b37 100644 --- a/fpm.toml +++ b/fpm.toml @@ -8,6 +8,7 @@ license = "LICENSE" [dependencies] kinds = {git="https://github.com/gha3mi/kinds.git"} +FACE = {git="https://github.com/szaghi/FACE.git"} forunittest = {git="https://github.com/gha3mi/forunittest.git"} [build] diff --git a/src/fortime.f90 b/src/fortime.f90 index 419107c..2669be6 100644 --- a/src/fortime.f90 +++ b/src/fortime.f90 @@ -92,12 +92,13 @@ 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, print) + impure subroutine timer_stop(this, nloops, message, print, color) class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg logical, intent(in), optional :: print + character(*), intent(in), optional :: color ! Stop the timer call system_clock(count=this%clock_end) @@ -119,9 +120,9 @@ impure subroutine timer_stop(this, nloops, message, print) end if if (present(print)) then - if (print) call print_time(this%elapsed_time, msg) + if (print) call print_time(this%elapsed_time, msg, color) else - call print_time(this%elapsed_time, msg) + call print_time(this%elapsed_time, msg, color) end if ! Deallocate the message @@ -178,12 +179,13 @@ 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, print) + impure subroutine ctimer_stop(this, nloops, message, print, color) class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg logical, intent(in), optional :: print + character(*), intent(in), optional :: color ! Stop the timer call cpu_time(this%cpu_end) @@ -203,9 +205,9 @@ impure subroutine ctimer_stop(this, nloops, message, print) end if if (present(print)) then - if (print) call print_time(this%cpu_time, msg) + if (print) call print_time(this%cpu_time, msg, color) else - call print_time(this%cpu_time, msg) + call print_time(this%cpu_time, msg, color) end if ! Deallocate the message @@ -266,13 +268,14 @@ 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, print) + impure subroutine otimer_stop(this, nloops, message, print, color) 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 + character(*), intent(in), optional :: color ! Stop the timer this%omp_end = omp_get_wtime() @@ -292,9 +295,9 @@ impure subroutine otimer_stop(this, nloops, message, print) end if if (present(print)) then - if (print) call print_time(this%omp_time, msg) + if (print) call print_time(this%omp_time, msg, color) else - call print_time(this%omp_time, msg) + call print_time(this%omp_time, msg, color) end if ! Deallocate the message @@ -366,13 +369,14 @@ 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, print) + impure subroutine mtimer_stop(this, nloops, message, print, color) ! 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 + character(*), intent(in), optional :: color interface function mpi_wtime() @@ -399,9 +403,9 @@ end function mpi_wtime end if if (present(print)) then - if (print) call print_time(this%mpi_time, msg) + if (print) call print_time(this%mpi_time, msg, color) else - call print_time(this%mpi_time, msg) + call print_time(this%mpi_time, msg, color) end if ! Deallocate the message @@ -461,13 +465,14 @@ end subroutine dtimer_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 dtimer_stop(this, nloops, message, print) + impure subroutine dtimer_stop(this, nloops, message, print, color) class(timer), intent(inout) :: this integer, intent(in), optional :: nloops character(*), intent(in), optional :: message character(:), allocatable :: msg logical, intent(in), optional :: print real(rk) :: values_elapsed_sec + character(*), intent(in), optional :: color ! Stop the timer call date_and_time(values=this%values_end) @@ -489,9 +494,9 @@ impure subroutine dtimer_stop(this, nloops, message, print) end if if (present(print)) then - if (print) call print_time(this%elapsed_dtime, msg) + if (print) call print_time(this%elapsed_dtime, msg, color) else - call print_time(this%elapsed_dtime, msg) + call print_time(this%elapsed_dtime, msg, color) end if ! Deallocate the message @@ -548,11 +553,17 @@ end function to_seconds !=============================================================================== !> author: Seyed Ali Ghasemi - impure subroutine print_time(time, message) + impure subroutine print_time(time, message, color) + use face real(rk), intent(in) :: time character(*), intent(in) :: message + character(*), intent(in), optional :: color - print '(A, F16.9, " [s]")', trim(message), time + if (present(color)) then + print '(A, F16.9, A)', colorize(trim(message), color_fg=trim(color)), time, colorize(" [s]", color_fg=trim(color)) + else + print '(A, F16.9, A)', colorize(trim(message), color_fg='blue'), time, colorize(" [s]", color_fg='blue') + end if end subroutine print_time !===============================================================================