WAVEWATCH III  beta 0.0.1
scrip_timers.f
Go to the documentation of this file.
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 ! This code has been modified from the version available from
34 ! Los Alamos National Laboratory, for the purpose of running it
35 ! within WW3.
36 !
37 !***********************************************************************
38 
39  module scrip_timers
40 
41 !-----------------------------------------------------------------------
42 
43  use scrip_kindsmod ! defines common data types
44 
45  implicit none
46 
47  integer (SCRIP_i4), parameter ::
48  & max_timers = 99 ! max number of timers allowed
49 
50  integer (SCRIP_i4), save ::
51  & cycles_max ! max value of clock allowed by system
52 
53  integer (SCRIP_i4), dimension(max_timers), save ::
54  & cycles1, ! cycle number at start for each timer
55  & cycles2 ! cycle number at stop for each timer
56 
57  real (scrip_r4), save ::
58  & clock_rate ! clock_rate in seconds for each cycle
59 
60  real (scrip_r4), dimension(max_timers), save ::
61  & cputime ! accumulated cpu time in each timer
62 
63  character (len=8), dimension(max_timers), save ::
64  & status ! timer status string
65 
66 !***********************************************************************
67 
68  contains
69 
70 !***********************************************************************
71 
72  subroutine timer_check(timer)
73 
74 !-----------------------------------------------------------------------
75 !
76 ! This routine checks a given timer. This is primarily used to
77 ! periodically accumulate time in the timer to prevent timer cycles
78 ! from wrapping around max_cycles.
79 !
80 !-----------------------------------------------------------------------
81 
82 !-----------------------------------------------------------------------
83 !
84 ! Input Variables:
85 !
86 !-----------------------------------------------------------------------
87 
88  integer (SCRIP_i4), intent(in) ::
89  & timer ! timer number
90 
91 !-----------------------------------------------------------------------
92 
93  if (status(timer) .eq. 'running') then
94  call timer_stop (timer)
95  call timer_start(timer)
96  endif
97 
98 !-----------------------------------------------------------------------
99 
100  end subroutine timer_check
101 
102 !***********************************************************************
103 
104  subroutine timer_clear(timer)
105 
106 !-----------------------------------------------------------------------
107 !
108 ! This routine resets a given timer.
109 !
110 !-----------------------------------------------------------------------
111 
112 !-----------------------------------------------------------------------
113 !
114 ! Input Variables:
115 !
116 !-----------------------------------------------------------------------
117 
118  integer (SCRIP_i4), intent(in) ::
119  & timer ! timer number
120 
121 !-----------------------------------------------------------------------
122 
123  cputime(timer) = 0.0_scrip_r4 ! clear the timer
124 
125 !-----------------------------------------------------------------------
126 
127  end subroutine timer_clear
128 
129 !***********************************************************************
130 
131  function timer_get(timer)
132 
133 !-----------------------------------------------------------------------
134 !
135 ! This routine returns the result of a given timer. This can be
136 ! called instead of timer_print so that the calling routine can
137 ! print it in desired format.
138 !
139 !-----------------------------------------------------------------------
140 
141 !-----------------------------------------------------------------------
142 !
143 ! Input Variables:
144 !
145 !-----------------------------------------------------------------------
146 
147  integer (SCRIP_i4), intent(in) ::
148  & timer ! timer number
149 
150 !-----------------------------------------------------------------------
151 !
152 ! Output Variables:
153 !
154 !-----------------------------------------------------------------------
155 
156  real (scrip_r4) ::
157  & timer_get ! accumulated cputime in given timer
158 
159 !-----------------------------------------------------------------------
160 
161  if (status(timer) .eq. 'stopped') then
162  timer_get = cputime(timer)
163  else
164  call timer_stop(timer)
165  timer_get = cputime(timer)
166  call timer_start(timer)
167  endif
168 
169 !-----------------------------------------------------------------------
170 
171  end function timer_get
172 
173 !***********************************************************************
174 
175  subroutine timer_print(timer)
176 
177 !-----------------------------------------------------------------------
178 !
179 ! This routine prints the accumulated cpu time in given timer.
180 !
181 !-----------------------------------------------------------------------
182 
183 !-----------------------------------------------------------------------
184 !
185 ! Input Variables:
186 !
187 !-----------------------------------------------------------------------
188 
189  integer (SCRIP_i4), intent(in) ::
190  & timer ! timer number
191 
192 !-----------------------------------------------------------------------
193 
194  !---
195  !--- print the cputime accumulated for timer
196  !--- make sure timer is stopped
197  !---
198 
199  if (status(timer) .eq. 'stopped') then
200  write(*,"(' CPU time for timer',i3,':',1p,e16.8)")
201  & timer,cputime(timer)
202  else
203  call timer_stop(timer)
204  write(*,"(' CPU time for timer',i3,':',1p,e16.8)")
205  & timer,cputime(timer)
206  call timer_start(timer)
207  endif
208 
209 !-----------------------------------------------------------------------
210 
211  end subroutine timer_print
212 
213 !***********************************************************************
214 
215  subroutine timer_start(timer)
216 
217 !-----------------------------------------------------------------------
218 !
219 ! This routine starts a given timer.
220 !
221 !-----------------------------------------------------------------------
222 
223 !-----------------------------------------------------------------------
224 !
225 ! Input Variables:
226 !
227 !-----------------------------------------------------------------------
228 
229  integer (SCRIP_i4), intent(in) ::
230  & timer ! timer number
231 
232 !-----------------------------------------------------------------------
233 
234  !---
235  !--- Start the timer and change timer status.
236  !---
237 
238  if (status(timer) .eq. 'stopped') then
239  call system_clock(count=cycles1(timer))
240  status(timer) = 'running'
241  endif
242 
243 !-----------------------------------------------------------------------
244 
245  end subroutine timer_start
246 
247 !***********************************************************************
248 
249  subroutine timer_stop(timer)
250 
251 !-----------------------------------------------------------------------
252 !
253 ! This routine stops a given timer.
254 !
255 !-----------------------------------------------------------------------
256 
257 !-----------------------------------------------------------------------
258 !
259 ! Input Variables:
260 !
261 !-----------------------------------------------------------------------
262 
263  integer (SCRIP_i4), intent(in) ::
264  & timer ! timer number
265 
266 !-----------------------------------------------------------------------
267 
268  if (status(timer) .eq. 'running') then
269 
270  !---
271  !--- Stop the desired timer.
272  !---
273 
274  call system_clock(count=cycles2(timer))
275 
276  !---
277  !--- check and correct for cycle wrapping
278  !---
279 
280  if (cycles2(timer) .ge. cycles1(timer)) then
281  cputime(timer) = cputime(timer) + clock_rate*
282  & (cycles2(timer) - cycles1(timer))
283  else
284  cputime(timer) = cputime(timer) + clock_rate*
285  & (cycles2(timer) - cycles1(timer) + cycles_max)
286  endif
287 
288  !---
289  !--- Change timer status.
290  !---
291 
292  status(timer)='stopped'
293 
294  endif
295 
296 !-----------------------------------------------------------------------
297 
298  end subroutine timer_stop
299 
300 !***********************************************************************
301 
302  subroutine timers_init
303 
304 !-----------------------------------------------------------------------
305 !
306 ! This routine initializes some machine parameters necessary for
307 ! computing cpu time from F90 intrinsics.
308 !
309 !-----------------------------------------------------------------------
310 
311  integer (SCRIP_i4) :: cycles ! count rate return by sys_clock
312 
313 !-----------------------------------------------------------------------
314 
315  !---
316  !--- Initialize timer arrays and clock_rate.
317  !---
318 
319  clock_rate = 0.0_scrip_r4
320  cycles1 = 0
321  cycles2 = 0
322  cputime = 0.0_scrip_r4
323  status = 'stopped'
324 
325  !---
326  !--- Call F90 intrinsic system_clock to determine clock rate
327  !--- and maximum cycles. If no clock available, print message.
328  !---
329 
330  call system_clock(count_rate=cycles, count_max=cycles_max)
331 
332  if (cycles /= 0) then
333  clock_rate = 1.0_scrip_r4/real(cycles)
334  else
335  clock_rate = 0.0_scrip_r4
336  print *, '--- No system clock available ---'
337  endif
338 
339 !-----------------------------------------------------------------------
340 
341  end subroutine timers_init
342 
343 !***********************************************************************
344 
345  end module scrip_timers
346 
347 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
scrip_timers::timer_get
real(scrip_r4) function timer_get(timer)
Definition: scrip_timers.f:132
scrip_timers::cputime
real(scrip_r4), dimension(max_timers), save cputime
Definition: scrip_timers.f:60
scrip_timers::timer_start
subroutine timer_start(timer)
Definition: scrip_timers.f:216
scrip_timers::clock_rate
real(scrip_r4), save clock_rate
Definition: scrip_timers.f:57
scrip_timers::max_timers
integer(scrip_i4), parameter max_timers
Definition: scrip_timers.f:47
scrip_timers::timer_print
subroutine timer_print(timer)
Definition: scrip_timers.f:176
scrip_timers::timer_check
subroutine timer_check(timer)
Definition: scrip_timers.f:73
scrip_timers::cycles_max
integer(scrip_i4), save cycles_max
Definition: scrip_timers.f:50
scrip_timers::status
character(len=8), dimension(max_timers), save status
Definition: scrip_timers.f:63
scrip_timers::cycles2
integer(scrip_i4), dimension(max_timers), save cycles2
Definition: scrip_timers.f:53
scrip_timers::timer_clear
subroutine timer_clear(timer)
Definition: scrip_timers.f:105
scrip_kindsmod
Definition: scrip_kindsmod.f90:3
scrip_kindsmod::scrip_r4
integer, parameter, public scrip_r4
Definition: scrip_kindsmod.f90:38
scrip_timers
Definition: scrip_timers.f:39
scrip_timers::cycles1
integer(scrip_i4), dimension(max_timers), save cycles1
Definition: scrip_timers.f:53
scrip_timers::timer_stop
subroutine timer_stop(timer)
Definition: scrip_timers.f:250
scrip_timers::timers_init
subroutine timers_init
Definition: scrip_timers.f:303