Globals Library 1.0
Loading...
Searching...
No Matches
modTiming.f90
Go to the documentation of this file.
1!> @brief Defines vars and procs for cpu timing of code sections
2module timing
3
4 use globals
5
6 implicit none
7
8 private
9
10 type, public :: tim
11 !> Actual value of wall-clock-time
12 real(kind=double) :: wct
13 !> Actual value of user (cpu) time (summed across threads)
14 real(kind=double) :: usr
15 !> Cumulated value of wall-clock-time
16 real(kind=double) :: cumwct
17 !> Cumulated value of user (cpu) time (summed across threads)
18 real(kind=double) :: cumusr
19 !> Status variable: both command and stutus
20 !> can be: `start`, `stop`, `stac`, `accumulate` and `reset`.
21 !> Can be abbreviated using the first 3 characters of the word.
22 character(len=15) :: status
23 contains
24 !> Static constructor (public for type `timing::tim`)
25 procedure, public, pass :: init => tim_construct
26 !> Static destructor (public for type `timing::tim`)
27 procedure, public, pass :: kill => tim_destroy
28 !> Outputs the content of the variable (public for type `timing::tim`)
29 procedure, public, pass :: info => tim_print
30 !> Sets the current `timing::tim` vars (public for type `timing::tim`)
31 procedure, public, pass :: set => tim_set
32 end type tim
33
34contains
35
36 !>-------------------------------------------------------------
37 !> @brief Static constructor (procedure public for type `timing::tim`)
38 !> @details Instantiate (allocate if necessary) and initilize
39 !> (by also reading from input file) variable of type `timing::tim`.
40 !>
41 !> @param[out] this Object of type `timing::tim` calling the constructor
42 !<-------------------------------------------------------------
43 subroutine tim_construct(this)
44 implicit none
45 class(tim), intent(out) :: this
46
47 this%wct = zero
48 this%usr = zero
49 this%status = "start"
50
51 end subroutine tim_construct
52
53 !>-------------------------------------------------------------
54 !> @brief Static destructor (procedure public for type `timing::tim`).
55 !<-----------------------------------------------------------
56 subroutine tim_destroy(this)
57 implicit none
58 class(tim), intent(inout) :: this
59
60 this%status = 'destroy'
61
62 end subroutine tim_destroy
63
64 !>-------------------------------------------------------------
65 !> @brief Info procedure (public procedure for type `timing::tim`).
66 !> @details Prints content of a variable of type `timing::tim`.
67 !>
68 !> @param[in] lun: integer. output unit
69 !> @param[in] add_msg: character, optional.
70 !> additional message to be printed.
71 !> @param[in] add_msg1: character, optional.
72 !> additional message to be printed.
73 !> @param[in] add_msg2: character, optional.
74 !> additional message to be printed.
75 !<-------------------------------------------------------------
76 subroutine tim_print(this, lun, add_msg, add_msg1, add_msg2)
77 implicit none
78 class(tim), intent(in) :: this
79 integer, intent(in) :: lun
80 character(len=*), optional, intent(in) :: add_msg, add_msg1, add_msg2
81 !local vars
82 character(len=256) :: msg, msg1, msg2
83
84 if (present(add_msg)) then
85 msg = add_msg
86 else
87 msg = ''
88 end if
89
90 if (present(add_msg1)) then
91 msg1 = add_msg1
92 else
93 msg1 = ''
94 end if
95
96 if (present(add_msg2)) then
97 msg2 = add_msg2
98 else
99 msg2 = ''
100 end if
101
102 write (lun, '(a,f7.3,a,f7.3,a)') trim(msg)//' WCT: ', this%CUMwct, &
103 ' (s) '//trim(msg1)//'; USR: ', this%CUMusr, ' (s) '//trim(msg2)
104
105 end subroutine tim_print
106
107 !>-------------------------------------------------------------
108 !> @brief Set procedure (public procedure for type `timing::tim`).
109 !> Sets the values of var of type `timing::tim`.
110 !>
111 !> @param[in] code: character, optional.
112 !> Commands to start/stop/reset/accumulate stopwatch.
113 !> If the variable is not given, the value of `status` will be used.
114 !> Admissible values for code are two up to now are:
115 !> 1. `start`: starts stopwatch.
116 !> 2. `stop`: stops stopwatch and accumulates measured timing.
117 !<-------------------------------------------------------------
118 subroutine tim_set(this, code)
119 implicit none
120 class(tim), intent(inout) :: this
121 character(len=*), optional, intent(in) :: code
122 ! local vars
123 real(kind=double) :: time
124 integer :: itime, tick_per_sec
125 character(len=3) :: com
126
127 call system_clock(itime, tick_per_sec)
128
129 if (present(code)) then
130 com = code(1:3)
131 else
132 select case (this%status(1:3))
133 case ('sta')
134 com = 'sta'
135 case ('sto')
136 com = 'sto'
137 case default
138 com = 'non'
139 end select
140 end if
141 select case (com)
142 case ('sta')
143 ! initial setting
144 call cpu_time(this%usr)
145 call system_clock(itime)
146 this%wct = dble(itime)/dble(tick_per_sec)
147 this%status = 'sto'
148 case ('sto')
149 ! periodic measure
150 call cpu_time(time)
151 this%usr = time - this%usr
152 call system_clock(itime)
153 this%wct = dble(itime)/dble(tick_per_sec) - this%wct
154 this%CUMusr = this%CUMusr + this%usr
155 this%CUMwct = this%CUMwct + this%wct
156 this%status = 'sta'
157 case default
158 this%usr = -999
159 this%wct = -999
160 end select
161
162 end subroutine tim_set
163
164end module timing
Defines vars and procs for cpu timing of code sections.
Definition modTiming.f90:2
subroutine tim_print(this, lun, add_msg, add_msg1, add_msg2)
Info procedure (public procedure for type timing::tim).
Definition modTiming.f90:77
subroutine tim_construct(this)
Static constructor (procedure public for type timing::tim).
Definition modTiming.f90:44
subroutine tim_set(this, code)
Set procedure (public procedure for type timing::tim). Sets the values of var of type timing::tim.
subroutine tim_destroy(this)
Static destructor (procedure public for type timing::tim).
Definition modTiming.f90:57