timers.f 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2. !
  3. ! This module uses F90 cpu time routines to allowing setting of
  4. ! multiple CPU timers.
  5. !
  6. !-----------------------------------------------------------------------
  7. !
  8. ! CVS:$Id: timers.f,v 1.2 2000/04/19 21:56:26 pwjones Exp $
  9. !
  10. ! Copyright (c) 1997, 1998 the Regents of the University of
  11. ! California.
  12. !
  13. ! This software and ancillary information (herein called software)
  14. ! called SCRIP is made available under the terms described here.
  15. ! The software has been approved for release with associated
  16. ! LA-CC Number 98-45.
  17. !
  18. ! Unless otherwise indicated, this software has been authored
  19. ! by an employee or employees of the University of California,
  20. ! operator of the Los Alamos National Laboratory under Contract
  21. ! No. W-7405-ENG-36 with the U.S. Department of Energy. The U.S.
  22. ! Government has rights to use, reproduce, and distribute this
  23. ! software. The public may copy and use this software without
  24. ! charge, provided that this Notice and any statement of authorship
  25. ! are reproduced on all copies. Neither the Government nor the
  26. ! University makes any warranty, express or implied, or assumes
  27. ! any liability or responsibility for the use of this software.
  28. !
  29. ! If software is modified to produce derivative works, such modified
  30. ! software should be clearly marked, so as not to confuse it with
  31. ! the version available from Los Alamos National Laboratory.
  32. !
  33. !***********************************************************************
  34. module timers
  35. !-----------------------------------------------------------------------
  36. use kinds_mod
  37. implicit none
  38. integer (kind=int_kind), parameter ::
  39. & max_timers = 99 ! max number of timers allowed
  40. integer (kind=int_kind), save ::
  41. & cycles_max ! max value of clock allowed by system
  42. integer (kind=int_kind), dimension(max_timers), save ::
  43. & cycles1, ! cycle number at start for each timer
  44. & cycles2 ! cycle number at stop for each timer
  45. real (kind=real_kind), save ::
  46. & clock_rate ! clock_rate in seconds for each cycle
  47. real (kind=real_kind), dimension(max_timers), save ::
  48. & cputime ! accumulated cpu time in each timer
  49. character (len=8), dimension(max_timers), save ::
  50. & status ! timer status string
  51. !***********************************************************************
  52. contains
  53. !***********************************************************************
  54. subroutine timer_check(timer)
  55. !-----------------------------------------------------------------------
  56. !
  57. ! This routine checks a given timer. This is primarily used to
  58. ! periodically accumulate time in the timer to prevent timer cycles
  59. ! from wrapping around max_cycles.
  60. !
  61. !-----------------------------------------------------------------------
  62. !-----------------------------------------------------------------------
  63. !
  64. ! Input Variables:
  65. !
  66. !-----------------------------------------------------------------------
  67. integer (kind=int_kind), intent(in) ::
  68. & timer ! timer number
  69. !-----------------------------------------------------------------------
  70. if (status(timer) .eq. 'running') then
  71. call timer_stop (timer)
  72. call timer_start(timer)
  73. endif
  74. !-----------------------------------------------------------------------
  75. end subroutine timer_check
  76. !***********************************************************************
  77. subroutine timer_clear(timer)
  78. !-----------------------------------------------------------------------
  79. !
  80. ! This routine resets a given timer.
  81. !
  82. !-----------------------------------------------------------------------
  83. !-----------------------------------------------------------------------
  84. !
  85. ! Input Variables:
  86. !
  87. !-----------------------------------------------------------------------
  88. integer (kind=int_kind), intent(in) ::
  89. & timer ! timer number
  90. !-----------------------------------------------------------------------
  91. cputime(timer) = 0.0_real_kind ! clear the timer
  92. !-----------------------------------------------------------------------
  93. end subroutine timer_clear
  94. !***********************************************************************
  95. function timer_get(timer)
  96. !-----------------------------------------------------------------------
  97. !
  98. ! This routine returns the result of a given timer. This can be
  99. ! called instead of timer_print so that the calling routine can
  100. ! print it in desired format.
  101. !
  102. !-----------------------------------------------------------------------
  103. !-----------------------------------------------------------------------
  104. !
  105. ! Input Variables:
  106. !
  107. !-----------------------------------------------------------------------
  108. integer (kind=int_kind), intent(in) ::
  109. & timer ! timer number
  110. !-----------------------------------------------------------------------
  111. !
  112. ! Output Variables:
  113. !
  114. !-----------------------------------------------------------------------
  115. real (kind=real_kind) ::
  116. & timer_get ! accumulated cputime in given timer
  117. !-----------------------------------------------------------------------
  118. if (status(timer) .eq. 'stopped') then
  119. timer_get = cputime(timer)
  120. else
  121. call timer_stop(timer)
  122. timer_get = cputime(timer)
  123. call timer_start(timer)
  124. endif
  125. !-----------------------------------------------------------------------
  126. end function timer_get
  127. !***********************************************************************
  128. subroutine timer_print(timer)
  129. !-----------------------------------------------------------------------
  130. !
  131. ! This routine prints the accumulated cpu time in given timer.
  132. !
  133. !-----------------------------------------------------------------------
  134. !-----------------------------------------------------------------------
  135. !
  136. ! Input Variables:
  137. !
  138. !-----------------------------------------------------------------------
  139. integer (kind=int_kind), intent(in) ::
  140. & timer ! timer number
  141. !-----------------------------------------------------------------------
  142. !---
  143. !--- print the cputime accumulated for timer
  144. !--- make sure timer is stopped
  145. !---
  146. if (status(timer) .eq. 'stopped') then
  147. write(*,"(' CPU time for timer',i3,':',1p,e16.8)")
  148. & timer,cputime(timer)
  149. else
  150. call timer_stop(timer)
  151. write(*,"(' CPU time for timer',i3,':',1p,e16.8)")
  152. & timer,cputime(timer)
  153. call timer_start(timer)
  154. endif
  155. !-----------------------------------------------------------------------
  156. end subroutine timer_print
  157. !***********************************************************************
  158. subroutine timer_start(timer)
  159. !-----------------------------------------------------------------------
  160. !
  161. ! This routine starts a given timer.
  162. !
  163. !-----------------------------------------------------------------------
  164. !-----------------------------------------------------------------------
  165. !
  166. ! Input Variables:
  167. !
  168. !-----------------------------------------------------------------------
  169. integer (kind=int_kind), intent(in) ::
  170. & timer ! timer number
  171. !-----------------------------------------------------------------------
  172. !---
  173. !--- Start the timer and change timer status.
  174. !---
  175. if (status(timer) .eq. 'stopped') then
  176. call system_clock(count=cycles1(timer))
  177. status(timer) = 'running'
  178. endif
  179. !-----------------------------------------------------------------------
  180. end subroutine timer_start
  181. !***********************************************************************
  182. subroutine timer_stop(timer)
  183. !-----------------------------------------------------------------------
  184. !
  185. ! This routine stops a given timer.
  186. !
  187. !-----------------------------------------------------------------------
  188. !-----------------------------------------------------------------------
  189. !
  190. ! Input Variables:
  191. !
  192. !-----------------------------------------------------------------------
  193. integer (kind=int_kind), intent(in) ::
  194. & timer ! timer number
  195. !-----------------------------------------------------------------------
  196. if (status(timer) .eq. 'running') then
  197. !---
  198. !--- Stop the desired timer.
  199. !---
  200. call system_clock(count=cycles2(timer))
  201. !---
  202. !--- check and correct for cycle wrapping
  203. !---
  204. if (cycles2(timer) .ge. cycles1(timer)) then
  205. cputime(timer) = cputime(timer) + clock_rate*
  206. & (cycles2(timer) - cycles1(timer))
  207. else
  208. cputime(timer) = cputime(timer) + clock_rate*
  209. & (cycles2(timer) - cycles1(timer) + cycles_max)
  210. endif
  211. !---
  212. !--- Change timer status.
  213. !---
  214. status(timer)='stopped'
  215. endif
  216. !-----------------------------------------------------------------------
  217. end subroutine timer_stop
  218. !***********************************************************************
  219. subroutine timers_init
  220. !-----------------------------------------------------------------------
  221. !
  222. ! This routine initializes some machine parameters necessary for
  223. ! computing cpu time from F90 intrinsics.
  224. !
  225. !-----------------------------------------------------------------------
  226. integer (kind=int_kind) :: cycles ! count rate return by sys_clock
  227. !-----------------------------------------------------------------------
  228. !---
  229. !--- Initialize timer arrays and clock_rate.
  230. !---
  231. clock_rate = 0.0_real_kind
  232. cycles1 = 0
  233. cycles2 = 0
  234. cputime = 0.0_real_kind
  235. status = 'stopped'
  236. !---
  237. !--- Call F90 intrinsic system_clock to determine clock rate
  238. !--- and maximum cycles. If no clock available, print message.
  239. !---
  240. call system_clock(count_rate=cycles, count_max=cycles_max)
  241. if (cycles /= 0) then
  242. clock_rate = 1.0_real_kind/real(cycles)
  243. else
  244. clock_rate = 0.0_real_kind
  245. print *, '--- No system clock available ---'
  246. endif
  247. !-----------------------------------------------------------------------
  248. end subroutine timers_init
  249. !***********************************************************************
  250. end module timers
  251. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!