Skip to content

Commit

Permalink
Add optional color argument to print_time.
Browse files Browse the repository at this point in the history
  • Loading branch information
gha3mi committed Apr 22, 2024
1 parent 2db5dde commit 4a4189a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 25 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
```
Expand All @@ -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
```
Expand All @@ -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
```
Expand All @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion example/example2.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion example/example4.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion example/example6.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
45 changes: 28 additions & 17 deletions src/fortime.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
!===============================================================================

Expand Down

0 comments on commit 4a4189a

Please sign in to comment.