-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_timer.f90
573 lines (401 loc) · 14.2 KB
/
mo_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
!> \file mo_timer.f90
!> \brief Timing routines
!> \details This module uses F90 cpu time routines to allowing setting of
!> multiple CPU timers.
!> \authors Matthias Cuntz - from timers.f (c) of the Regents of the University of California
!> \date Dec 2012
module mo_timer
! -----------------------------------------------------------------------
! This module uses F90 cpu time routines to allowing setting of
! multiple CPU timers.
! -----------------------------------------------------------------------
! CVS:$Id: timers.f,v 1.2 2000/04/19 21:56:26 pwjones Exp $
! Copyright (c) 1997, 1998 the Regents of the University of
! California.
! This software and ancillary information (herein called software)
! called SCRIP is made available under the terms described here.
! The software has been approved for release with associated
! LA-CC Number 98-45.
! Unless otherwise indicated, this software has been authored
! by an employee or employees of the University of California,
! operator of the Los Alamos National Laboratory under Contract
! No. W-7405-ENG-36 with the U.S. Department of Energy. The U.S.
! Government has rights to use, reproduce, and distribute this
! software. The public may copy and use this software without
! charge, provided that this Notice and any statement of authorship
! are reproduced on all copies. Neither the Government nor the
! University makes any warranty, express or implied, or assumes
! any liability or responsibility for the use of this software.
! If software is modified to produce derivative works, such modified
! software should be clearly marked, so as not to confuse it with
! the version available from Los Alamos National Laboratory.
! -----------------------------------------------------------------------
! Modified, Matthias Cuntz, Aug. 2012 - adapted to JAMS library, called mo_timer.f90
! Matthias Cuntz, Jan. 2013 - clear one or all timers
! Matthias Cuntz, Jan. 2022 - make all integer i8
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 1997-2013 The Regents University of California, Matthias Cuntz - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
use mo_kind, only: i8, sp
implicit none
private
! Variables
public :: max_timers ! max number of timers allowed
public :: cycles_max ! max value of clock allowed by system
public :: clock_rate ! clock_rate in seconds for each cycle
public :: cycles1 ! cycle number at start for each timer
public :: cycles2 ! cycle number at stop for each timer
public :: cputime ! accumulated cpu time in each timer
public :: status ! timer status string
! Routines
public :: timer_check ! Checks a given timer
public :: timer_clear ! Clears (a given) timer(s)
public :: timer_get ! Returns the result of a given timer
public :: timer_print ! Prints the accumulated cpu time of a given timer
public :: timer_start ! Stops a given timer
public :: timer_stop ! Starts a given timer
public :: timers_init ! Initialises the module
! Save variables
!> max number of timers allowed
integer(i8), parameter :: max_timers = 99
!> max value of clock allowed by system
integer(i8), save :: cycles_max
!> clock_rate in seconds for each cycle
real(sp), save :: clock_rate
!> cycle number at start for each timer
integer(i8), dimension(max_timers), save :: cycles1
!> cycle number at stop for each timer
integer(i8), dimension(max_timers), save :: cycles2
!> accumulated cpu time in each timer
real(sp), dimension(max_timers), save :: cputime
!> timer status string
character(len=8), dimension(max_timers), save :: status
! -----------------------------------------------------------------------
contains
! ------------------------------------------------------------------
! NAME
! timer_check
! PURPOSE
!> \brief Check a timer
!> \details This routine checks a given timer. This is primarily used to
!> periodically accumulate time in the timer to prevent timer cycles
!> from wrapping around max_cycles.
! INTENT(IN)
!> \param[in] "integer(i8) :: timer" timer number
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timer_check(3)
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
subroutine timer_check(timer)
implicit none
integer(i8), intent(in) :: timer ! timer number
if (status(timer) .eq. 'running') then
call timer_stop (timer)
call timer_start(timer)
endif
end subroutine timer_check
! ------------------------------------------------------------------
! NAME
! timer_clear
! PURPOSE
!> \brief Reset a timer
!> \details This routine resets a given timer or all timers to 0.
! INTENT(IN)
! None
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> \param[in] "integer(i8), optional :: timer" timer number if given.\n
!> If missing, all timers will be reset.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timer_clear(3)
! call timer_clear()
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
subroutine timer_clear(timer)
implicit none
integer(i8), intent(in), optional :: timer ! timer number
if (present(timer)) then
cputime(timer) = 0.0_sp ! clear the timer
else
cputime(:) = 0.0_sp ! clear all timers
endif
end subroutine timer_clear
! ------------------------------------------------------------------
! NAME
! timer_get
! PURPOSE
!> \brief Return a timer
!> \details This routine returns the result of a given timer. This can be
!> called instead of timer_print so that the calling routine can
!> print it in desired format.
! INTENT(IN)
!> \param[in] "integer(i8) :: timer" timer number
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timer_get(3)
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
function timer_get(timer)
implicit none
integer(i8), intent(in) :: timer ! timer number
real(sp) :: timer_get ! accumulated cputime in given timer
if (status(timer) .eq. 'stopped') then
timer_get = cputime(timer)
else
call timer_stop(timer)
timer_get = cputime(timer)
call timer_start(timer)
endif
end function timer_get
! ------------------------------------------------------------------
! NAME
! timer_print
! PURPOSE
!> \brief Print a timer
!> \details This routine prints the accumulated cpu time in given timer.
! INTENT(IN)
!> \param[in] "integer(i8) :: timer" timer number
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timer_print(3)
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
subroutine timer_print(timer)
implicit none
integer(i8), intent(in) :: timer ! timer number
!---
!--- print the cputime accumulated for timer
!--- make sure timer is stopped
!---
if (status(timer) .eq. 'stopped') then
write(*,"(' CPU time for timer',i3,':',1p,e16.8)") &
timer,cputime(timer)
else
call timer_stop(timer)
write(*,"(' CPU time for timer',i3,':',1p,e16.8)") &
timer,cputime(timer)
call timer_start(timer)
endif
end subroutine timer_print
! ------------------------------------------------------------------
! NAME
! timer_start
! PURPOSE
!> \brief Start a timer
!> \details This routine starts a given timer.
! INTENT(IN)
!> \param[in] "integer(i8) :: timer" timer number
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timer_start(3)
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
subroutine timer_start(timer)
implicit none
integer(i8), intent(in) :: timer ! timer number
!---
!--- Start the timer and change timer status.
!---
if (status(timer) .eq. 'stopped') then
call system_clock(count=cycles1(timer))
status(timer) = 'running'
endif
end subroutine timer_start
! ------------------------------------------------------------------
! NAME
! timer_stop
! PURPOSE
!> \brief Stop a timer
!> \details This routine stops a given timer.
! INTENT(IN)
!> \param[in] "integer(i8) :: timer" timer number
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timer_stop(3)
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
! Written Matthias Cuntz, Aug 2012
! Modified Matthias Cuntz, Jan 2024
! - assure correct conversion between integer and real
subroutine timer_stop(timer)
implicit none
integer(i8), intent(in) :: timer ! timer number
if (status(timer) .eq. 'running') then
!---
!--- Stop the desired timer.
!---
call system_clock(count=cycles2(timer))
!---
!--- check and correct for cycle wrapping
!---
if (cycles2(timer) .ge. cycles1(timer)) then
cputime(timer) = cputime(timer) + clock_rate * &
real(cycles2(timer) - cycles1(timer), sp)
else
cputime(timer) = cputime(timer) + clock_rate * &
real(cycles2(timer) - cycles1(timer) + cycles_max, sp)
endif
!---
!--- Change timer status.
!---
status(timer)='stopped'
endif
end subroutine timer_stop
!-----------------------------------------------------------------------
! This routine initializes some machine parameters necessary for
! computing cpu time from F90 intrinsics.
! ------------------------------------------------------------------
! NAME
! timers_init
! PURPOSE
!> \brief Initialise timer module
!> \details This routine initializes some machine parameters necessary for
!> computing cpu time from F90 intrinsics.
! INTENT(IN)
! None
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! None
! EXAMPLE
! call timers_init()
! LITERATURE
! None
! HISTORY
!> \author Matthias Cuntz
!> \date Aug 2012
subroutine timers_init
implicit none
integer(i8) :: cycles ! count rate return by sys_clock
!---
!--- Initialize timer arrays and clock_rate.
!---
clock_rate = 0.0_sp
cycles1(:) = 0
cycles2(:) = 0
cputime(:) = 0.0_sp
status(:) = 'stopped'
!---
!--- Call F90 intrinsic system_clock to determine clock rate
!--- and maximum cycles. If no clock available, print message.
!---
call system_clock(count_rate=cycles, count_max=cycles_max)
if (cycles /= 0) then
clock_rate = 1.0_sp/real(cycles)
else
clock_rate = 0.0_sp
print *, '--- No system clock available ---'
endif
end subroutine timers_init
end module mo_timer