Skip to content

Commit

Permalink
Add opttional logical print argument to timer_stop
Browse files Browse the repository at this point in the history
- Add new tests
- Update example2 and example4
- Update README.md
  • Loading branch information
gha3mi committed Jan 2, 2024
1 parent 7fb1aed commit 3735d87
Show file tree
Hide file tree
Showing 25 changed files with 423 additions and 14 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
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:') ! 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
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:') ! 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
60 changes: 60 additions & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
51 changes: 43 additions & 8 deletions src/fortime.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions test/test13.f90
Original file line number Diff line number Diff line change
@@ -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

25 changes: 25 additions & 0 deletions test/test14.f90
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions test/test14_etimes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.000266666666667
25 changes: 25 additions & 0 deletions test/test15.f90
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions test/test15_etimes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.000400000000000
17 changes: 17 additions & 0 deletions test/test16.f90
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions test/test17.f90
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions test/test17_ctimes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.2999999999999993E-04
Loading

0 comments on commit 3735d87

Please sign in to comment.