00001
00002
00003
00004
00005
00006 module shr_timer_mod
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 use shr_kind_mod
00028 use shr_log_mod, only: s_loglev => shr_log_Level
00029 use shr_log_mod, only: s_logunit => shr_log_Unit
00030
00031 implicit none
00032
00033 private
00034
00035 public :: shr_timer_init
00036 public :: shr_timer_get
00037 public :: shr_timer_start
00038 public :: shr_timer_stop
00039 public :: shr_timer_print
00040 public :: shr_timer_print_all
00041 public :: shr_timer_check
00042 public :: shr_timer_check_all
00043 public :: shr_timer_zero
00044 public :: shr_timer_zero_all
00045 public :: shr_timer_free
00046 public :: shr_timer_free_all
00047 public :: shr_timer_sleep
00048
00049 integer(SHR_KIND_IN),parameter :: stat_free = 0
00050 integer(SHR_KIND_IN),parameter :: stat_inuse = 1
00051 integer(SHR_KIND_IN),parameter :: stat_started = 2
00052 integer(SHR_KIND_IN),parameter :: stat_stopped = 3
00053 integer(SHR_KIND_IN),parameter :: max_timers = 200
00054
00055 integer(SHR_KIND_IN) :: status (max_timers)
00056
00057
00058
00059 #if (defined UNICOSMP)
00060 integer(kind=8) :: cycles1(max_timers)
00061 integer(kind=8) :: cycles2(max_timers)
00062 #else
00063 integer(SHR_KIND_IN) :: cycles1(max_timers)
00064 integer(SHR_KIND_IN) :: cycles2(max_timers)
00065 #endif
00066 integer(SHR_KIND_IN) :: cycles_max = -1
00067 character (len=80) :: name (max_timers)
00068 real (SHR_KIND_R8) :: dt (max_timers)
00069 integer(SHR_KIND_IN) :: calls (max_timers)
00070 real (SHR_KIND_R8) :: clock_rate
00071
00072 save
00073
00074
00075 contains
00076
00077
00078 subroutine shr_timer_init
00079
00080
00081 integer(SHR_KIND_IN) :: cycles
00082 #if (defined UNICOSMP)
00083 integer(kind=8) :: irtc_rate
00084 #endif
00085
00086
00087 character(len=*),parameter :: F00 = "('(shr_timer_init) ',a,i5)"
00088
00089
00090
00091
00092
00093
00094
00095
00096 call shr_timer_free_all
00097
00098 #if (defined UNICOSMP)
00099 cycles = irtc_rate()
00100 #else
00101 call system_clock(count_rate=cycles, count_max=cycles_max)
00102 #endif
00103
00104 if (cycles /= 0) then
00105 clock_rate = 1.0_SHR_KIND_R8/real(cycles,SHR_KIND_R8)
00106 else
00107 clock_rate = 0._SHR_KIND_R8
00108 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: no system clock available'
00109 endif
00110
00111 end subroutine shr_timer_init
00112
00113
00114
00115 subroutine shr_timer_get(n, str)
00116
00117
00118 integer(SHR_KIND_IN),intent(out) :: n
00119 character (*) ,intent( in) :: str
00120
00121
00122 character(len=*),parameter :: F00 = "('(shr_timer_get) ',a,i5)"
00123
00124
00125
00126
00127
00128 do n=1,max_timers
00129 if (status(n) == stat_free) then
00130 status(n) = stat_inuse
00131 name (n) = str
00132 calls (n) = 0
00133 return
00134 endif
00135 end do
00136
00137 n=max_timers
00138 name (n) = "<invalid - undefined - overloaded>"
00139 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: exceeded maximum number of timers'
00140
00141 end subroutine shr_timer_get
00142
00143
00144
00145 subroutine shr_timer_start(n)
00146
00147
00148 integer(SHR_KIND_IN), intent(in) :: n
00149
00150
00151 #if (defined UNICOSMP)
00152 integer(kind=8) :: irtc
00153 #endif
00154
00155
00156 character(len=*),parameter :: F00 = "('(shr_timer_start) ',a,i5)"
00157
00158
00159
00160
00161
00162 if ( n>0 .and. n<=max_timers) then
00163 if (status(n) == stat_started) call shr_timer_stop(n)
00164
00165 status(n) = stat_started
00166 #if (defined UNICOSMP)
00167 cycles1(n) = irtc()
00168 #else
00169 call system_clock(count=cycles1(n))
00170 #endif
00171 else
00172 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: invalid timer number: ',n
00173 end if
00174
00175 end subroutine shr_timer_start
00176
00177
00178
00179 subroutine shr_timer_stop(n)
00180
00181
00182 integer(SHR_KIND_IN), intent(in) :: n
00183
00184
00185 real (SHR_KIND_R8) :: elapse
00186 #if (defined UNICOSMP)
00187 integer(kind=8) :: irtc
00188 #endif
00189
00190
00191 character(len=*),parameter :: F00 = "('(shr_timer_stop) ',a,i5)"
00192
00193
00194
00195
00196
00197
00198 if ( n>0 .and. n<=max_timers) then
00199 if ( status(n) == stat_started) then
00200 #if (defined UNICOSMP)
00201 cycles2(n) = irtc()
00202 #else
00203 call system_clock(count=cycles2(n))
00204 #endif
00205 if (cycles2(n) >= cycles1(n)) then
00206 dt(n) = dt(n) + clock_rate*(cycles2(n) - cycles1(n))
00207 else
00208 dt(n) = dt(n) + clock_rate*(cycles_max + cycles2(n) - cycles1(n))
00209 endif
00210 calls (n) = calls(n) + 1
00211 status(n) = stat_stopped
00212 end if
00213 else
00214 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: invalid timer number: ',n
00215 end if
00216
00217 end subroutine shr_timer_stop
00218
00219
00220
00221 subroutine shr_timer_print(n)
00222
00223
00224 integer(SHR_KIND_IN), intent(in) :: n
00225
00226
00227 character(len=*),parameter :: F00 = "('(shr_timer_print) ',a,i5)"
00228 character(len=*),parameter :: F01 =
00229 "('(shr_timer_print) timer',i3,& & ':',i8,' calls,',f10.3,'s, id: ',a)"
00230
00231
00232
00233
00234 if ( n>0 .and. n<=max_timers) then
00235 if (status(n) == stat_started) then
00236 call shr_timer_stop(n)
00237 if (s_loglev > 0) write(s_logunit,F01) n,calls(n),dt(n),trim(name(n))
00238 call shr_timer_start(n)
00239 else
00240 if (s_loglev > 0) write(s_logunit,F01) n,calls(n),dt(n),trim(name(n))
00241 endif
00242 else
00243 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: invalid timer number: ',n
00244 end if
00245
00246 end subroutine shr_timer_print
00247
00248
00249
00250 subroutine shr_timer_print_all
00251
00252
00253 integer(SHR_KIND_IN) :: n
00254
00255
00256 character(len=*),parameter :: F00 = "('(shr_timer_print_all) ',a,i5)"
00257
00258
00259
00260
00261
00262 if (s_loglev > 0) write(s_logunit,F00) 'print all timing info:'
00263
00264 do n=1,max_timers
00265 if (status(n) /= stat_free) call shr_timer_print(n)
00266 end do
00267
00268 end subroutine shr_timer_print_all
00269
00270
00271
00272 subroutine shr_timer_zero(n)
00273
00274
00275 integer(SHR_KIND_IN), intent(in) :: n
00276
00277
00278 character(len=*),parameter :: F00 = "('(shr_timer_zero) ',a,i5)"
00279
00280
00281
00282
00283
00284 if ( n>0 .and. n<=max_timers) then
00285 dt(n) = 0.0_SHR_KIND_R8
00286 calls(n) = 0
00287 else
00288 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: invalid timer number: ',n
00289 end if
00290
00291 end subroutine shr_timer_zero
00292
00293
00294
00295 subroutine shr_timer_zero_all
00296
00297
00298 character(len=*),parameter :: F00 = "('(shr_timer_zero_all) ',a,i5)"
00299
00300
00301
00302
00303
00304 dt = 0.0_SHR_KIND_R8
00305 calls = 0
00306
00307 end subroutine shr_timer_zero_all
00308
00309
00310
00311 subroutine shr_timer_check(n)
00312
00313
00314 integer(SHR_KIND_IN), intent(in) :: n
00315
00316
00317 character(len=*),parameter :: F00 = "('(shr_timer_check) ',a,i5)"
00318
00319
00320
00321
00322
00323
00324
00325 if ( n>0 .and. n<=max_timers) then
00326 if (status(n) == stat_started) then
00327 call shr_timer_stop (n)
00328 call shr_timer_start(n)
00329 endif
00330 else
00331 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: invalid timer number: ',n
00332 end if
00333
00334 end subroutine shr_timer_check
00335
00336
00337
00338 subroutine shr_timer_check_all
00339
00340
00341 integer(SHR_KIND_IN) :: n
00342
00343
00344 character(len=*),parameter :: F00 = "('(shr_timer_check_all) ',a,i5)"
00345
00346
00347
00348
00349
00350 do n=1,max_timers
00351 if (status(n) == stat_started) then
00352 call shr_timer_stop (n)
00353 call shr_timer_start(n)
00354 endif
00355 end do
00356
00357 end subroutine shr_timer_check_all
00358
00359
00360
00361 subroutine shr_timer_free(n)
00362
00363
00364 integer(SHR_KIND_IN),intent(in) :: n
00365
00366
00367 character(len=*),parameter :: F00 = "('(shr_timer_free) ',a,i5)"
00368
00369
00370
00371
00372
00373 if ( n>0 .and. n<=max_timers) then
00374 status (n) = stat_free
00375 name (n) = "<invalid - undefined>"
00376 dt (n) = 0.0_SHR_KIND_R8
00377 cycles1(n) = 0
00378 cycles2(n) = 0
00379 else
00380 if (s_loglev > 0) write(s_logunit,F00) 'ERROR: invalid timer number: ',n
00381 end if
00382
00383 end subroutine shr_timer_free
00384
00385
00386
00387 subroutine shr_timer_free_all
00388
00389
00390 integer(SHR_KIND_IN) :: n
00391
00392
00393 character(len=*),parameter :: F00 = "('(shr_timer_free_all) ',a,i5)"
00394
00395
00396
00397
00398
00399 do n=1,max_timers
00400 call shr_timer_free(n)
00401 end do
00402
00403 end subroutine shr_timer_free_all
00404
00405
00406
00407 subroutine shr_timer_sleep(sec)
00408
00409 use shr_sys_mod
00410
00411
00412 real (SHR_KIND_R8),intent(in) :: sec
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422 call shr_sys_sleep(sec)
00423
00424 end subroutine shr_timer_sleep
00425
00426
00427 end module shr_timer_mod
00428