Repository navigation
Expand file tree
/
Copy pathtimer_implementation.f90
More file actions
151 lines (128 loc) · 4.73 KB
/
Copy pathtimer_implementation.f90
File metadata and controls
151 lines (128 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
submodule(timer_interface) timer_implementation
implicit none
contains
!>-----------------------------------
!! Start the timer
!!
!! Sets the internal start_time and marks the timer as running
!!
!------------------------------------
module subroutine start(this, use_cpu_time)
class(timer_t), intent(inout) :: this
logical, intent(in), optional :: use_cpu_time
logical :: cpu_only
if (present(use_cpu_time)) then
cpu_only = use_cpu_time
else
cpu_only = .false.
end if
this%is_running = .True.
this%use_cpu_time = cpu_only
if (cpu_only) then
call cpu_time(this%start_time)
else
call system_clock(this%counter, this%count_rate, this%count_max)
endif
end subroutine start
!>-----------------------------------
!! Stop the timer
!!
!! Sets the internal stop_time and marks the timer as not running
!!
!! Also computes the total_time the timer has been running
!!
!------------------------------------
module subroutine stop(this)
class(timer_t), intent(inout) :: this
integer :: count_end
! do this before we even test anything else because we want the timer to "stop" as soon as possible
if (this%use_cpu_time) then
call cpu_time(this%end_time)
else
call system_clock(count_end)
if (count_end<this%counter) then
this%end_time = (count_end + (this%count_max - this%counter)) / real(this%count_rate)
else
this%end_time = (count_end - this%counter) / real(this%count_rate)
endif
endif
if (this%is_running) then
this%is_running = .False.
this%total_time = this%total_time + (this%end_time - this%start_time)
endif
end subroutine stop
!>-----------------------------------
!! Reset the timer
!!
!! Resets timer internal variables as if it was never running
!!
!------------------------------------
module subroutine reset(this)
class(timer_t), intent(inout) :: this
this%total_time = 0
this%start_time = 0
this%end_time = 0
this%is_running = .False.
end subroutine reset
!>-----------------------------------
!! Return the time as a real
!!
!! If the timer is running, it includes the current time in the total reported
!!
!------------------------------------
module function get_time(this) result(time)
class(timer_t), intent(inout) :: this
real :: time ! return value
real :: current_time
integer :: count_end
if (this%is_running) then
if (this%use_cpu_time) then
call cpu_time(current_time)
else
call system_clock(count_end)
if (count_end<this%counter) then
current_time = (count_end + (this%count_max - this%counter)) / real(this%count_rate)
else
current_time = (count_end - this%counter) / real(this%count_rate)
endif
endif
time = this%total_time + (current_time - this%start_time)
else
time = this%total_time
endif
end function get_time
!>-----------------------------------
!! Return the time as a string
!!
!! If the timer is running, it includes the current time in the total reported
!!
!------------------------------------
module function as_string(this, format) result(time)
class(timer_t), intent(inout) :: this
character(len=*), intent(in), optional :: format
character(len=25) :: time ! return value
real :: temporary_time, current_time
integer :: count_end
if (this%is_running) then
if (this%use_cpu_time) then
call cpu_time(current_time)
else
call system_clock(count_end)
if (count_end<this%counter) then
current_time = (count_end + (this%count_max - this%counter)) / real(this%count_rate)
else
current_time = (count_end - this%counter) / real(this%count_rate)
endif
endif
temporary_time = this%total_time + (current_time - this%start_time)
else
temporary_time = this%total_time
endif
! if the user specified a format string, use that when creating the output
if (present(format)) then
write(time,format) temporary_time
else
write(time,*) temporary_time
endif
end function as_string
end submodule timer_implementation