(git:ec11232)
Loading...
Searching...
No Matches
timings_report.F
Go to the documentation of this file.
1!--------------------------------------------------------------------------------------------------!
2! CP2K: A general program to perform molecular dynamics simulations !
3! Copyright 2000-2025 CP2K developers group <https://cp2k.org> !
4! !
5! SPDX-License-Identifier: GPL-2.0-or-later !
6!--------------------------------------------------------------------------------------------------!
7
8! **************************************************************************************************
9!> \brief Timing routines for accounting
10!> \par History
11!> 02.2004 made a stacked version (of stacks...) [Joost VandeVondele]
12!> 11.2004 storable timer_envs (for f77 interface) [fawzi]
13!> 10.2005 binary search to speed up lookup in timeset [fawzi]
14!> 12.2012 Complete rewrite based on dictionaries. [ole]
15!> 01.2014 Collect statistics from all MPI ranks. [ole]
16!> \author JGH
17! **************************************************************************************************
21 USE cp_files, ONLY: close_file,&
23 USE kinds, ONLY: default_string_length,&
24 dp,&
25 int_8
26 USE list, ONLY: list_destroy,&
27 list_get,&
28 list_init,&
30 list_pop,&
31 list_push,&
35 USE routine_map, ONLY: routine_map_get,&
37 USE timings, ONLY: get_timer_env
42 USE util, ONLY: sort
43#include "../base/base_uses.f90"
44
45 IMPLICIT NONE
46 PRIVATE
47
48 INTEGER, PUBLIC, PARAMETER :: cost_type_time = 17, cost_type_energy = 18
49
51
52CONTAINS
53
54! **************************************************************************************************
55!> \brief Print accumulated information on timers
56!> \param iw ...
57!> \param r_timings ...
58!> \param sort_by_self_time ...
59!> \param cost_type ...
60!> \param report_maxloc ...
61!> \param para_env is needed to collect statistics from other nodes.
62!> \par History
63!> none
64!> \author JGH
65! **************************************************************************************************
66 SUBROUTINE timings_report_print(iw, r_timings, sort_by_self_time, cost_type, report_maxloc, para_env)
67 INTEGER, INTENT(IN) :: iw
68 REAL(kind=dp), INTENT(IN) :: r_timings
69 LOGICAL, INTENT(IN) :: sort_by_self_time
70 INTEGER, INTENT(IN) :: cost_type
71 LOGICAL, INTENT(IN) :: report_maxloc
72 TYPE(mp_para_env_type), INTENT(IN) :: para_env
73
74 TYPE(list_routinereport_type) :: reports
75 TYPE(routine_report_type), POINTER :: r_report
76
77 CALL list_init(reports)
78 CALL collect_reports_from_ranks(reports, cost_type, para_env)
79
80 IF (list_size(reports) > 0 .AND. iw > 0) &
81 CALL print_reports(reports, iw, r_timings, sort_by_self_time, cost_type, report_maxloc, para_env)
82
83 ! deallocate reports
84 DO WHILE (list_size(reports) > 0)
85 r_report => list_pop(reports)
86 DEALLOCATE (r_report)
87 END DO
88 CALL list_destroy(reports)
89
90 END SUBROUTINE timings_report_print
91
92! **************************************************************************************************
93!> \brief Collects the timing or energy reports from all MPI ranks.
94!> \param reports ...
95!> \param cost_type ...
96!> \param para_env ...
97!> \author Ole Schuett
98! **************************************************************************************************
99 SUBROUTINE collect_reports_from_ranks(reports, cost_type, para_env)
100 TYPE(list_routinereport_type), INTENT(INOUT) :: reports
101 INTEGER, INTENT(IN) :: cost_type
102 TYPE(mp_para_env_type), INTENT(IN) :: para_env
103
104 CHARACTER(LEN=default_string_length) :: routinen
105 INTEGER :: local_routine_id, sending_rank
106 INTEGER, ALLOCATABLE, DIMENSION(:) :: collected
107 REAL(kind=dp) :: foobar
108 REAL(kind=dp), DIMENSION(2) :: dbuf
109 TYPE(routine_report_type), POINTER :: r_report
110 TYPE(routine_stat_type), POINTER :: r_stat
111 TYPE(timer_env_type), POINTER :: timer_env
112
113 NULLIFY (r_stat, r_report, timer_env)
114 IF (.NOT. list_isready(reports)) &
115 cpabort("BUG")
116
117 timer_env => get_timer_env()
118
119 ! make sure all functions have been called so that list_size(timer_env%routine_stats)
120 ! and the actual dictionary are consistent in the loop below, preventing out of bounds.
121 ! this hack makes sure they are called before
122 routinen = ""
123 CALL para_env%bcast(routinen, 0)
124 sending_rank = 0
125 CALL para_env%max(sending_rank)
126 CALL para_env%sum(sending_rank)
127 foobar = 0.0_dp
128 CALL para_env%max(foobar)
129 dbuf = 0.0_dp
130 CALL para_env%maxloc(dbuf)
131 CALL para_env%sum(foobar)
132 ! end hack
133
134 ! Array collected is used as a bit field.
135 ! It's of type integer in order to use the convenient MINLOC routine.
136 ALLOCATE (collected(list_size(timer_env%routine_stats)), source=0)
137
138 DO
139 ! does any rank have uncollected stats?
140 sending_rank = -1
141 IF (.NOT. all(collected == 1)) sending_rank = para_env%mepos
142 CALL para_env%max(sending_rank)
143 IF (sending_rank < 0) EXIT ! every rank got all routines collected
144 IF (sending_rank == para_env%mepos) THEN
145 local_routine_id = minloc(collected, dim=1)
146 r_stat => list_get(timer_env%routine_stats, local_routine_id)
147 routinen = r_stat%routineN
148 END IF
149 CALL para_env%bcast(routinen, sending_rank)
150
151 ! Create new report for routineN
152 ALLOCATE (r_report)
153 CALL list_push(reports, r_report)
154 r_report%routineN = routinen
155
156 ! If routineN was called on local node, add local stats
157 IF (routine_map_haskey(timer_env%routine_names, routinen)) THEN
158 local_routine_id = routine_map_get(timer_env%routine_names, routinen)
159 collected(local_routine_id) = 1
160 r_stat => list_get(timer_env%routine_stats, local_routine_id)
161 r_report%max_total_calls = r_stat%total_calls
162 r_report%sum_total_calls = r_stat%total_calls
163 r_report%sum_stackdepth = r_stat%stackdepth_accu
164 SELECT CASE (cost_type)
165 CASE (cost_type_energy)
166 r_report%max_icost = r_stat%incl_energy_accu
167 r_report%sum_icost = r_stat%incl_energy_accu
168 r_report%max_ecost = r_stat%excl_energy_accu
169 r_report%sum_ecost = r_stat%excl_energy_accu
170 CASE (cost_type_time)
171 r_report%max_icost = r_stat%incl_walltime_accu
172 r_report%sum_icost = r_stat%incl_walltime_accu
173 r_report%max_ecost = r_stat%excl_walltime_accu
174 r_report%sum_ecost = r_stat%excl_walltime_accu
175 CASE DEFAULT
176 cpabort("BUG")
177 END SELECT
178 END IF
179
180 ! collect stats of routineN via MPI
181 CALL para_env%max(r_report%max_total_calls)
182 CALL para_env%sum(r_report%sum_total_calls)
183 CALL para_env%sum(r_report%sum_stackdepth)
184
185 ! get value and rank of the maximum inclusive cost
186 dbuf = [r_report%max_icost, real(para_env%mepos, kind=dp)]
187 CALL para_env%maxloc(dbuf)
188 r_report%max_icost = dbuf(1)
189 r_report%max_irank = int(dbuf(2))
190
191 CALL para_env%sum(r_report%sum_icost)
192
193 ! get value and rank of the maximum exclusive cost
194 dbuf = [r_report%max_ecost, real(para_env%mepos, kind=dp)]
195 CALL para_env%maxloc(dbuf)
196 r_report%max_ecost = dbuf(1)
197 r_report%max_erank = int(dbuf(2))
198
199 CALL para_env%sum(r_report%sum_ecost)
200 END DO
201
202 END SUBROUTINE collect_reports_from_ranks
203
204! **************************************************************************************************
205!> \brief Print the collected reports
206!> \param reports ...
207!> \param iw ...
208!> \param threshold ...
209!> \param sort_by_exclusiv_cost ...
210!> \param cost_type ...
211!> \param report_maxloc ...
212!> \param para_env ...
213!> \par History
214!> 01.2014 Refactored (Ole Schuett)
215!> \author JGH
216! **************************************************************************************************
217 SUBROUTINE print_reports(reports, iw, threshold, sort_by_exclusiv_cost, cost_type, report_maxloc, para_env)
218 TYPE(list_routinereport_type), INTENT(IN) :: reports
219 INTEGER, INTENT(IN) :: iw
220 REAL(kind=dp), INTENT(IN) :: threshold
221 LOGICAL, INTENT(IN) :: sort_by_exclusiv_cost
222 INTEGER, INTENT(IN) :: cost_type
223 LOGICAL, INTENT(IN) :: report_maxloc
224 TYPE(mp_para_env_type), INTENT(IN) :: para_env
225
226 CHARACTER(LEN=4) :: label
227 CHARACTER(LEN=default_string_length) :: fmt, title
228 INTEGER :: decimals, i, j, num_routines
229 INTEGER, ALLOCATABLE, DIMENSION(:) :: indices
230 REAL(kind=dp) :: asd, maxcost, mincost
231 REAL(kind=dp), ALLOCATABLE, DIMENSION(:) :: max_costs
232 TYPE(routine_report_type), POINTER :: r_report_i, r_report_j
233
234 NULLIFY (r_report_i, r_report_j)
235 IF (.NOT. list_isready(reports)) &
236 cpabort("BUG")
237
238 ! are we printing timing or energy ?
239 SELECT CASE (cost_type)
240 CASE (cost_type_energy)
241 title = "E N E R G Y"
242 label = "ENER"
243 CASE (cost_type_time)
244 title = "T I M I N G"
245 label = "TIME"
246 CASE DEFAULT
247 cpabort("BUG")
248 END SELECT
249
250 ! write banner
251 WRITE (unit=iw, fmt="(/,T2,A)") repeat("-", 79)
252 WRITE (unit=iw, fmt="(T2,A,T80,A)") "-", "-"
253 WRITE (unit=iw, fmt="(T2,A,T35,A,T80,A)") "-", trim(title), "-"
254 WRITE (unit=iw, fmt="(T2,A,T80,A)") "-", "-"
255 WRITE (unit=iw, fmt="(T2,A)") repeat("-", 79)
256 IF (report_maxloc) THEN
257 WRITE (unit=iw, fmt="(T2,A,T35,A,T41,A,T45,2A18,A8)") &
258 "SUBROUTINE", "CALLS", " ASD", "SELF "//label, "TOTAL "//label, "MAXRANK"
259 ELSE
260 WRITE (unit=iw, fmt="(T2,A,T35,A,T41,A,T45,2A18)") &
261 "SUBROUTINE", "CALLS", " ASD", "SELF "//label, "TOTAL "//label
262 END IF
263
264 WRITE (unit=iw, fmt="(T33,A)") &
265 "MAXIMUM AVERAGE MAXIMUM AVERAGE MAXIMUM"
266
267 ! sort statistics
268 num_routines = list_size(reports)
269 ALLOCATE (max_costs(num_routines))
270 DO i = 1, num_routines
271 r_report_i => list_get(reports, i)
272 IF (sort_by_exclusiv_cost) THEN
273 max_costs(i) = r_report_i%max_ecost
274 ELSE
275 max_costs(i) = r_report_i%max_icost
276 END IF
277 END DO
278 ALLOCATE (indices(num_routines))
279 CALL sort(max_costs, num_routines, indices)
280
281 maxcost = maxval(max_costs)
282 mincost = maxcost*threshold
283
284 ! adjust fmt dynamically based on the max walltime.
285 ! few clocks have more than 3 digits resolution, so stop there
286 decimals = 3
287 IF (maxcost >= 10000) decimals = 2
288 IF (maxcost >= 100000) decimals = 1
289 IF (maxcost >= 1000000) decimals = 0
290 IF (report_maxloc) THEN
291 WRITE (unit=fmt, fmt="(A,I0,A)") &
292 "(T2,A30,1X,I7,1X,F4.1,4(1X,F8.", decimals, "),I8)"
293 ELSE
294 WRITE (unit=fmt, fmt="(A,I0,A)") &
295 "(T2,A30,1X,I7,1X,F4.1,4(1X,F8.", decimals, "))"
296 END IF
297
298 !write output
299 DO i = num_routines, 1, -1
300 IF (max_costs(i) >= mincost) THEN
301 j = indices(i)
302 r_report_j => list_get(reports, j)
303 ! average stack depth
304 asd = real(r_report_j%sum_stackdepth, kind=dp)/ &
305 REAL(max(1_int_8, r_report_j%sum_total_calls), kind=dp)
306 IF (report_maxloc) THEN
307 WRITE (unit=iw, fmt=fmt) &
308 adjustl(r_report_j%routineN(1:31)), &
309 r_report_j%max_total_calls, &
310 asd, &
311 r_report_j%sum_ecost/para_env%num_pe, &
312 r_report_j%max_ecost, &
313 r_report_j%sum_icost/para_env%num_pe, &
314 r_report_j%max_icost, &
315 r_report_j%max_erank
316 ELSE
317 WRITE (unit=iw, fmt=fmt) &
318 adjustl(r_report_j%routineN(1:31)), &
319 r_report_j%max_total_calls, &
320 asd, &
321 r_report_j%sum_ecost/para_env%num_pe, &
322 r_report_j%max_ecost, &
323 r_report_j%sum_icost/para_env%num_pe, &
324 r_report_j%max_icost
325 END IF
326 END IF
327 END DO
328 WRITE (unit=iw, fmt="(T2,A,/)") repeat("-", 79)
329
330 END SUBROUTINE print_reports
331
332! **************************************************************************************************
333!> \brief Write accumulated callgraph information as cachegrind-file.
334!> http://kcachegrind.sourceforge.net/cgi-bin/show.cgi/KcacheGrindCalltreeFormat
335!> \param filename ...
336!> \par History
337!> 12.2012 initial version[ole]
338!> \author Ole Schuett
339! **************************************************************************************************
340 SUBROUTINE timings_report_callgraph(filename)
341 CHARACTER(len=*), INTENT(in) :: filename
342
343 INTEGER, PARAMETER :: e = 1000, t = 100000
344
345 INTEGER :: i, unit
346 TYPE(call_stat_type), POINTER :: c_stat
347 TYPE(callgraph_item_type), DIMENSION(:), POINTER :: ct_items
348 TYPE(routine_stat_type), POINTER :: r_stat
349 TYPE(timer_env_type), POINTER :: timer_env
350
351 CALL open_file(file_name=filename, file_status="REPLACE", file_action="WRITE", &
352 file_form="FORMATTED", unit_number=unit)
353 timer_env => get_timer_env()
354
355 ! use outermost routine as total runtime
356 r_stat => list_get(timer_env%routine_stats, 1)
357 WRITE (unit=unit, fmt="(A)") "events: Walltime Energy"
358 WRITE (unit=unit, fmt="(A,I0,1X,I0)") "summary: ", &
359 int(t*r_stat%incl_walltime_accu, kind=int_8), &
360 int(e*r_stat%incl_energy_accu, kind=int_8)
361
362 DO i = 1, list_size(timer_env%routine_stats)
363 r_stat => list_get(timer_env%routine_stats, i)
364 WRITE (unit=unit, fmt="(A,I0,A,A)") "fn=(", r_stat%routine_id, ") ", r_stat%routineN
365 WRITE (unit=unit, fmt="(A,I0,1X,I0)") "1 ", &
366 int(t*r_stat%excl_walltime_accu, kind=int_8), &
367 int(e*r_stat%excl_energy_accu, kind=int_8)
368 END DO
369
370 ct_items => callgraph_items(timer_env%callgraph)
371 DO i = 1, SIZE(ct_items)
372 c_stat => ct_items(i)%value
373 WRITE (unit=unit, fmt="(A,I0,A)") "fn=(", ct_items(i)%key(1), ")"
374 WRITE (unit=unit, fmt="(A,I0,A)") "cfn=(", ct_items(i)%key(2), ")"
375 WRITE (unit=unit, fmt="(A,I0,A)") "calls=", c_stat%total_calls, " 1"
376 WRITE (unit=unit, fmt="(A,I0,1X,I0)") "1 ", &
377 int(t*c_stat%incl_walltime_accu, kind=int_8), &
378 int(e*c_stat%incl_energy_accu, kind=int_8)
379 END DO
380 DEALLOCATE (ct_items)
381
382 CALL close_file(unit_number=unit, file_status="KEEP")
383
384 END SUBROUTINE timings_report_callgraph
385END MODULE timings_report
386
type(callgraph_item_type) function, dimension(:), pointer, public callgraph_items(hash_map)
Returns a pointer to an array of all key/value-items stored in the hash map. Caution: The caller is r...
Definition callgraph.F:383
Utility routines to open and close files. Tracking of preconnections.
Definition cp_files.F:16
subroutine, public open_file(file_name, file_status, file_form, file_action, file_position, file_pad, unit_number, debug, skip_get_unit_number, file_access)
Opens the requested file using a free unit number.
Definition cp_files.F:311
subroutine, public close_file(unit_number, file_status, keep_preconnection)
Close an open file given by its logical unit number. Optionally, keep the file and unit preconnected.
Definition cp_files.F:122
Defines the basic variable types.
Definition kinds.F:23
integer, parameter, public int_8
Definition kinds.F:54
integer, parameter, public dp
Definition kinds.F:34
integer, parameter, public default_string_length
Definition kinds.F:57
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
An array-based list which grows on demand. When the internal array is full, a new array of twice the ...
Definition list.F:24
Interface to the message passing library MPI.
integer(kind=int_4) function, public routine_map_get(hash_map, key, default_value)
Gets a value for a given key from the hash map. If the key is not found the default_value will be ret...
logical function, public routine_map_haskey(hash_map, key)
Checks whether a given key is currently stored in the hash_map.
Types used by timings.F and timings_report.F The types in this module are used within dict or list,...
Timing routines for accounting.
integer, parameter, public cost_type_energy
subroutine, public timings_report_callgraph(filename)
Write accumulated callgraph information as cachegrind-file. http://kcachegrind.sourceforge....
integer, parameter, public cost_type_time
subroutine, public timings_report_print(iw, r_timings, sort_by_self_time, cost_type, report_maxloc, para_env)
Print accumulated information on timers.
Types used by timings.F and timings_report.F Due to the fortran restriction on cicular module-depende...
Timing routines for accounting.
Definition timings.F:17
type(timer_env_type) function, pointer, public get_timer_env()
returns the current timer env from the stack
Definition timings.F:147
All kind of helpful little routines.
Definition util.F:14
stores all the informations relevant to an mpi environment