source: timer.F90@ 9f146fa

Last change on this file since 9f146fa was 7137e5d, checked in by Jan Meinke <j.meinke@…>, 14 years ago

Added timer and enyshe using double loop.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1! timer.f90
2!
3! (c)2007 Jan H. Meinke
4!
5#ifdef BGL
6module rts
7! interface definition of rts_get_timebase, Fortran2003, available in XLF Version 9
8 interface
9 function rts_get_timebase() bind(c)
10 use, intrinsic :: iso_c_binding
11 integer(c_long_long) :: rts_get_timebase
12 end function rts_get_timebase
13 end interface
14end module rts
15#endif
16
17!! This module contains a minimal set of routines to time sections of code.
18! It can keep track of several timers at once. Each timer is associated with
19! an integer ID. Individual timing events are not stored.
20!
21! Currently only timerIDs between 1 and 10 are valid.
22module timer
23#ifdef BGL
24 use rts
25 use, intrinsic :: iso_c_binding
26#endif
27 implicit none
28
29 !! Contains the minimum time ever measured, the maximum time ever measured,
30 ! the average time, the standard deviation, and the number of measurements.
31 type TimingStructure
32 integer :: counter
33#ifdef BGL
34 integer(c_long_long) :: t0, t1
35#else
36 real(kind=8) :: t0, t1
37#endif
38 real(kind=8) :: minTime, maxTime
39 real(kind=8) :: sumOfTimes, sumOfSquares
40 real(kind=8) :: average, sigma
41 end type TimingStructure
42
43 !! Contains the timing data for all timers.
44 type(TimingStructure), allocatable :: timingData(:)
45
46 integer :: initialTimerCount = 10
47#ifdef BGL
48 real(kind=8) :: clockspeed = 700.0d6
49#endif
50
51 contains
52 !! Initializes timer
53 ! This routine must be called before starting any timer.
54 !
55 ! @author Jan H. Meinke
56 subroutine init_timer()
57 integer :: i
58
59 ! Initialize timers
60 allocate (timingData(initialTimerCount))
61 forall(i = 1:initialTimerCount)
62 timingData(i) = TimingStructure(0, -1.0, -1.0, -1.0, -1.0, 0.0, &
63 0.0, -1.0, -1.0)
64 end forall
65 end subroutine init_timer
66
67 !! Start a new measurement.
68 !
69 ! @param id ID of the timer
70 !
71 ! @author Jan H. Meinke
72 subroutine start_timer(id)
73 integer, intent(in) :: id
74 timingData(id)%t0 = getCurrentTime()
75 end subroutine start_timer
76
77 !! Take a measurement and stop the timer with ID id
78 !
79 ! @param id ID of the timer
80 !
81 ! @author Jan H. Meinke
82 subroutine stop_timer(id)
83 integer, intent(in) :: id
84 real(kind=8) :: dt
85
86 timingData(id)%t1 = getCurrentTime()
87#ifdef BGL
88 dt = (timingData(id)%t1 - timingData(id)%t0) / clockspeed
89#else
90 dt = (timingData(id)%t1 - timingData(id)%t0)
91#endif
92 timingData(id)%counter = timingData(id)%counter + 1
93 timingData(id)%maxTime = max(dt, timingData(id)%maxTime)
94 if (timingData(id)%minTime > 0) then
95 timingData(id)%minTime = min(dt, timingData(id)%minTime)
96 else
97 timingData(id)%minTime = dt
98 end if
99 timingData(id)%sumOfTimes = timingData(id)%sumOfTimes + dt
100 timingData(id)%sumOfSquares = timingData(id)%sumOfSquares + dt ** 2
101
102 end subroutine stop_timer
103
104 subroutine evaluate(id)
105 integer, intent(in) :: id
106 if (timingData(id)%counter > 0) then
107 timingData(id)%average = timingData(id)%sumOfTimes / timingData(id)%counter
108 end if
109 if (timingData(id)%counter > 1) then
110! write (*,*) timingData(id)%average, timingData(id)%sumOfSquares,&
111! timingData(id)%counter
112 timingData(id)%sigma = sqrt((abs(timingData(id)%average ** 2 &
113 - timingData(id)%sumOfSquares / timingData(id)%counter)) &
114 / (timingData(id)%counter - 1))
115 end if
116 end subroutine evaluate
117
118 !! Wrapper around the timing function used.
119 ! If we are using MPI, we can use MPI_WTime, otherwise we use cpu_time(time)
120#ifdef BGL
121 integer(c_long_long) function getCurrentTime()
122 integer(c_long_long) :: time
123 time = rts_get_timebase()
124 getCurrentTime = time
125 end function getCurrentTime
126#elif MPI
127 real(kind=8) function getCurrentTime()
128 real(kind=8) :: time
129 include 'mpif.h'
130 time = MPI_Wtime()
131 getCurrentTime = time
132 end function getCurrentTime
133#else
134 real(kind=8) function getCurrentTime()
135 real(kind=8) :: time
136 call cpu_time(time)
137 getCurrentTime = time
138 end function getCurrentTime
139#endif
140
141end module timer
Note: See TracBrowser for help on using the repository browser.