-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.f90
260 lines (260 loc) · 10.3 KB
/
timer.f90
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
! -
!
! SPDX-FileCopyrightText: Copyright (c) 2022 Pedro Costa. All rights reserved.
! SPDX-License-Identifier: MIT
!
! -
!
! a simple timer, see https://github.com/p-costa/first-timer
!
module mod_timer
use, intrinsic :: iso_fortran_env, only: dp => real64
use mpi
#if defined(_USE_NVTX)
use mod_nvtx
#endif
implicit none
private
public :: timer_tic,timer_toc,timer_print,timer_cleanup
!
logical, parameter :: GPU_DEFAULT_SYNC = .true.
integer, parameter :: max_name_len = 50
character(max_name_len), allocatable :: timer_names(:)
integer , allocatable :: timer_counts(:)
real(dp), allocatable :: timer_tictoc(:),timer_elapsed_acc(:), &
timer_elapsed_min(:), &
timer_elapsed_max(:)
logical , allocatable :: timer_is_nvtx(:)
integer :: ntimers = 0
contains
subroutine timer_print(myid_arg)
use, intrinsic :: iso_fortran_env, only: stdo => output_unit
integer , parameter :: MYID_PRINT = 0
logical , parameter :: is_verbose_level_1 = .false.
logical , parameter :: is_verbose_level_2 = .false.
integer , intent(in), optional :: myid_arg
real(dp), allocatable :: timing_results_acc(:,:), &
timing_results_min(:,:), &
timing_results_max(:,:)
integer :: i,myid,nproc,ierr,iend
!
if(present(myid_arg)) then
myid = myid_arg
else
call MPI_COMM_RANK(MPI_COMM_WORLD,myid,ierr)
end if
allocate(timing_results_acc(ntimers,3), &
timing_results_min(ntimers,3), &
timing_results_max(ntimers,3))
call MPI_COMM_SIZE(MPI_COMM_WORLD,nproc,ierr)
call MPI_ALLREDUCE(timer_elapsed_acc(:),timing_results_acc(:,1),ntimers,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(timer_elapsed_acc(:),timing_results_acc(:,2),ntimers,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(timer_elapsed_acc(:),timing_results_acc(:,3),ntimers,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
timing_results_acc(:,3) = timing_results_acc(:,3)/nproc
call MPI_ALLREDUCE(timer_elapsed_min(:),timing_results_min(:,1),ntimers,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(timer_elapsed_min(:),timing_results_min(:,2),ntimers,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(timer_elapsed_min(:),timing_results_min(:,3),ntimers,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
timing_results_min(:,3) = timing_results_min(:,3)/nproc
call MPI_ALLREDUCE(timer_elapsed_max(:),timing_results_max(:,1),ntimers,MPI_DOUBLE_PRECISION,MPI_MIN,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(timer_elapsed_max(:),timing_results_max(:,2),ntimers,MPI_DOUBLE_PRECISION,MPI_MAX,MPI_COMM_WORLD,ierr)
call MPI_ALLREDUCE(timer_elapsed_max(:),timing_results_max(:,3),ntimers,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD,ierr)
timing_results_max(:,3) = timing_results_max(:,3)/nproc
!
if(myid == MYID_PRINT) then
write(stdo,*) ''
write(stdo,*) '*** timing results [s] ***'
write(stdo,*) ''
if(nproc == 1.or..not.is_verbose_level_1) then
do i = 1,ntimers
write(stdo,'(3A)' ) 'Label: "',trim(timer_names(i)), '"'
write(stdo,'(A,3E15.7)') 'Elapsed time:', timing_results_acc(i,3:3)
write(stdo,'(A,I7)' ) 'Number of calls:', timer_counts(i)
write(stdo,'(A,1E15.7)') 'Average elapsed time per task (per call average):',timing_results_acc(i,3:3)/timer_counts(i)
if(is_verbose_level_2) then
write(stdo,'(A,1E15.7)') 'Average elapsed time per task (per call minimum):',timing_results_min(i,3:3)
write(stdo,'(A,1E15.7)') 'Average elapsed time per task (per call maximum):',timing_results_max(i,3:3)
endif
write(stdo,*) ''
end do
else
do i = 1,ntimers
write(stdo,'(3A)' ) 'Label: "',trim(timer_names(i)), '"'
write(stdo,'(A,3E15.7)') 'Maximum, minimum, average elapsed time per task:', timing_results_acc(i,1:3)
write(stdo,'(A,I7)' ) 'Number of calls:', timer_counts(i)
write(stdo,'(A,3E15.7)') 'Maximum, minimum, average elapsed time per task (per call average):', &
timing_results_acc(i,1:3)/timer_counts(i)
if(is_verbose_level_2) then
write(stdo,'(A,3E15.7)') 'Maximum, minimum, average elapsed time per task (per call minimum):', &
timing_results_min(i,1:3)
write(stdo,'(A,3E15.7)') 'Maximum, minimum, average elapsed time per task (per call maximum):', &
timing_results_max(i,1:3)
endif
write(stdo,*) ''
end do
end if
end if
end subroutine timer_print
subroutine timer_tic(timer_name,nvtx_id_fix,nvtx_color,nvtx_id_inc,nvtx_gpu_stream)
!@cuf use cudafor
character(*), intent(in) :: timer_name
integer , intent(in ), optional :: nvtx_id_fix ! if <= 0, only label and no color
character(len=1), intent(in ), optional :: nvtx_color ! g/b/y/m/c/r/w following matplotlib's convention
integer , intent(inout), optional :: nvtx_id_inc ! to increment the id, e.g.: call timer_tic(name,nvtx_id_inc=i_nvtx)
integer , intent(in ), optional :: nvtx_gpu_stream ! to optionally sync host/device over a stream/queue (asynchronous if < 0)
integer :: idx,nvtx_id
logical :: is_nvtx,is_gpu_sync
!@cuf integer :: istat
if(.not.allocated(timer_names)) then
allocate(timer_names( 0), &
timer_counts( 0), &
timer_tictoc( 0), &
timer_elapsed_acc(0), &
timer_elapsed_min(0), &
timer_elapsed_max(0), &
timer_is_nvtx( 0))
end if
!
idx = timer_search(timer_name)
if (idx <= 0) then
ntimers = ntimers + 1
call concatenate_c(timer_names,timer_name)
timer_counts = [timer_counts ,0 ]
timer_tictoc = [timer_tictoc ,0._dp ]
timer_elapsed_acc = [timer_elapsed_acc,0._dp ]
timer_elapsed_min = [timer_elapsed_min,huge(0._dp)]
timer_elapsed_max = [timer_elapsed_max,tiny(0._dp)]
timer_is_nvtx = [timer_is_nvtx ,.false. ]
idx = ntimers
end if
timer_tictoc(idx) = MPI_WTIME()
#if defined(_USE_NVTX)
is_nvtx = .false.
if( present(nvtx_id_inc)) then
nvtx_id = nvtx_id_inc
if(nvtx_id == huge(1)) nvtx_id_inc = 0 ! avoid overflow
nvtx_id_inc = nvtx_id_inc + 1
is_nvtx = .true.
else if(present(nvtx_id_fix)) then
nvtx_id = nvtx_id_fix
is_nvtx = .true.
else if(present(nvtx_color )) then
is_nvtx = .true.
end if
if(is_nvtx) then
is_gpu_sync = GPU_DEFAULT_SYNC
if(present(nvtx_gpu_stream)) then
if(nvtx_gpu_stream < 0) then
is_gpu_sync = .false.
end if
end if
if(is_gpu_sync) then
if(.not.present(nvtx_gpu_stream)) then
#if defined(_OPENACC)
!$acc wait
#elif defined(_CUDA)
!@cuf istat=cudaDeviceSynchronize()
#endif
else
#if defined(_OPENACC)
!$acc wait(nvtx_gpu_stream)
#elif defined(_CUDA)
!@cuf istat=cudaStreamSynchronize(nvtx_gpu_stream)
#endif
end if
end if
if( present(nvtx_color)) then
call nvtxStartRange(trim(timer_name),color=nvtx_color)
else if(nvtx_id > 0 ) then
call nvtxStartRange(trim(timer_name),id=nvtx_id)
else
call nvtxStartRange(trim(timer_name))
end if
timer_is_nvtx(idx) = .true.
end if
#endif
end subroutine timer_tic
subroutine timer_toc(timer_name,nvtx_gpu_stream,ierror)
!@cuf use cudafor
character(*), intent(in) :: timer_name
integer, intent(in), optional :: nvtx_gpu_stream
integer, intent(out), optional :: ierror
integer :: idx
logical :: is_gpu_sync
!@cuf integer :: istat
if(present(ierror)) ierror = 0
idx = timer_search(timer_name)
if (idx > 0) then
timer_tictoc(idx) = MPI_WTIME() - timer_tictoc(idx)
timer_elapsed_acc(idx) = (timer_elapsed_acc(idx)+timer_tictoc(idx))
timer_elapsed_min(idx) = min(timer_elapsed_min(idx),timer_tictoc(idx))
timer_elapsed_max(idx) = max(timer_elapsed_max(idx),timer_tictoc(idx))
timer_counts(idx) = timer_counts(idx) + 1
if(timer_is_nvtx(idx)) then
is_gpu_sync = GPU_DEFAULT_SYNC
if(present(nvtx_gpu_stream)) then
if(nvtx_gpu_stream < 0) then
is_gpu_sync = .false.
end if
end if
if(is_gpu_sync) then
if(.not.present(nvtx_gpu_stream)) then
#if defined(_OPENACC)
!$acc wait
#elif defined(_CUDA)
!@cuf istat=cudaDeviceSynchronize()
#endif
else
#if defined(_OPENACC)
!$acc wait(nvtx_gpu_stream)
#elif defined(_CUDA)
!@cuf istat=cudaStreamSynchronize(nvtx_gpu_stream)
#endif
end if
end if
#if defined(_USE_NVTX)
call nvtxEndRange
#endif
end if
else
if(present(ierror)) ierror = 1
end if
end subroutine timer_toc
subroutine timer_cleanup
if (.not.allocated(timer_names)) then
deallocate(timer_names,timer_counts,timer_elapsed_acc,timer_elapsed_min,timer_elapsed_max)
end if
end subroutine timer_cleanup
integer function timer_search(timer_name)
character(*), intent(in) :: timer_name
integer :: i
timer_search = -1
do i = 1,ntimers
if (timer_names(i) == timer_name) then
timer_search = i
end if
end do
end function timer_search
real(dp) function timer_time(timer_name,ierror)
character(*), intent(in) :: timer_name
integer, intent(out), optional :: ierror
integer :: idx
if(present(ierror)) ierror = 0
timer_time = -1._dp
idx = timer_search(timer_name)
if (idx > 0) then
timer_time = timer_elapsed_acc(idx)
else
if(present(ierror)) ierror = 1
end if
end function timer_time
subroutine concatenate_c(arr,val)
character(*), intent(inout), allocatable, dimension(:) :: arr
character(*), intent(in ) :: val
character(:), allocatable, dimension(:) :: arr_tmp
integer :: n
n = size(arr)
allocate(arr_tmp,source=arr)
deallocate(arr); allocate(arr(n+1))
arr(1:n) = arr_tmp(:); arr(n+1) = val
end subroutine concatenate_c
end module mod_timer