Line data Source code
1 : /*
2 : * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Martin Rupp, Torbjoern Klatt
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : /**
34 : * \file common/stopwatch.h
35 : * \author Martin Rupp
36 : * \date 2010-03-03
37 : * \author Torbjoern Klatt
38 : * \date 2012-05-07
39 : * \copyright 2010-2015 G-CSC, University of Frankfurt. All rights reserved.
40 : * \brief stopwatch class for quickly taking times
41 : */
42 :
43 : #ifndef __H__UG__STOPWATCH_H__
44 : #define __H__UG__STOPWATCH_H__
45 :
46 : // The following define is a leftover from pre-C++11 times.
47 : // Def or undef to switch between the two implementations.
48 : #define UG_CXX11
49 :
50 : #include <iostream>
51 :
52 : #ifdef UG_CXX11
53 : #include <chrono>
54 : #else
55 : #include <ctime>
56 : #endif
57 :
58 : #ifdef UG_POSIX
59 : #include <sys/time.h>
60 : #endif
61 :
62 : namespace ug
63 : {
64 :
65 :
66 :
67 : class CuckooClock{
68 :
69 : public:
70 0 : CuckooClock() : m_tlaunch(0), m_tdone(0), m_ttotal(0) {}
71 :
72 0 : void tic(){m_tlaunch = clock();}
73 :
74 : //! returns time since last tic
75 0 : double toc()
76 : {
77 0 : m_tdone = clock();
78 : //std::cerr << m_tdone;
79 0 : clock_t delta= (m_tdone-m_tlaunch);
80 0 : m_ttotal += delta;
81 0 : return 1.0*delta/CLOCKS_PER_SEC;
82 : }
83 :
84 : //! returns total time (in seconds)
85 0 : double cuckoo() {return 1.0*m_ttotal/ CLOCKS_PER_SEC;}
86 :
87 : protected:
88 :
89 : clock_t m_tlaunch;
90 : clock_t m_tdone;
91 : clock_t m_ttotal;
92 : };
93 :
94 :
95 : #ifdef UG_POSIX
96 : inline double get_clock_s()
97 : {
98 : timeval time;
99 0 : gettimeofday(&time, NULL);
100 0 : return time.tv_sec + time.tv_usec/1000000.0;
101 : }
102 : #else
103 : inline double get_clock_s()
104 : {
105 : return clock() / ((double)CLOCKS_PER_SEC);
106 : }
107 : #endif
108 :
109 : /// \addtogroup ugbase_common
110 : /// \{
111 :
112 : /**
113 : * \brief Stopwatch class for quickly taking times
114 : *
115 : * Depending on \c CXX11 flag, two different versions are compiled.
116 : * If \c CXX11=ON, <tt>std::chrono</tt> from C++11's STL is used providing high
117 : * resolution (microseconds) time measuring.
118 : * Otherwise <tt>std::ctime</tt> is used providing millisecond resolution.
119 : *
120 : * \note If \c CXX11=OFF timings shorter than 100ms seem to be rather
121 : * inaccurate.
122 : */
123 : class Stopwatch
124 : {
125 : public:
126 : /**
127 : * \brief Default constructor for the Stopwatch
128 : */
129 0 : Stopwatch() {
130 : // you cant be really sure when constructor is called
131 : #ifdef UG_CXX11
132 0 : begin = std::chrono::high_resolution_clock::now();
133 0 : end = std::chrono::high_resolution_clock::now();
134 : #else
135 : beg = end = get_clock_s();
136 : #endif
137 0 : bRunning = false;
138 : }
139 :
140 : /**
141 : * \brief Starts the Stopwatch
142 : */
143 : void start() {
144 0 : std::cout.flush();
145 : #ifdef UG_CXX11
146 0 : begin = std::chrono::high_resolution_clock::now();
147 : #else
148 : beg = get_clock_s();
149 : #endif
150 0 : bRunning = true;
151 0 : }
152 :
153 : /**
154 : * \brief Stops the Stopwatch
155 : */
156 : void stop() {
157 : #ifdef UG_CXX11
158 0 : end = std::chrono::high_resolution_clock::now();
159 : #else
160 : end = get_clock_s();
161 : #endif
162 0 : bRunning = false;
163 0 : }
164 :
165 : /**
166 : * \brief Prints number of milliseconds since call of start() to ostream
167 : *
168 : * Pretty prints the amount of milliseconds passed between calls of
169 : * Stopwatch::start() and Stopwatch::stop() or this function call
170 : * to the specified std::ostream.
171 : *
172 : * \param[out] out std::ostream to print number of milliseconds to
173 : * \param[in] sw a Stopwatch instance (usualy 'this')
174 : */
175 : friend std::ostream &operator << ( std::ostream &out, Stopwatch &s ) {
176 : out << s.ms() << " ms";
177 : return out;
178 : }
179 :
180 : /**
181 : * \brief Returns milliseconds since call of start
182 : *
183 : * Returns the amount of milliseconds passed between calls of
184 : * Stopwatch::start() and Stopwatch::stop() or this function call.
185 : *
186 : * \note If compiled with \c CXX11=ON returned milliseconds have microsecond
187 : * resolution.
188 : *
189 : * \return milliseconds
190 : */
191 : double ms() {
192 : #ifdef UG_CXX11
193 0 : if ( bRunning ) end = std::chrono::high_resolution_clock::now();
194 0 : return std::chrono::duration_cast<std::chrono::milliseconds> (end-begin).count();
195 : #else
196 : if( bRunning ) end = get_clock_s();
197 : return ( end - beg ) * 1000.0;
198 : #endif
199 : }
200 :
201 : private:
202 : #ifdef UG_CXX11
203 : /// Time point of the start of Stopwatch
204 : std::chrono::high_resolution_clock::time_point begin;
205 : /// Number of microseconds since \c begin
206 :
207 : std::chrono::high_resolution_clock::time_point end;
208 : #else
209 : /// Time point of the start of Stopwatch
210 : double beg;
211 : /// Time point of the end of Stopwatch
212 : double end;
213 : #endif
214 : /// Flag indicating state of Stopwatch
215 : bool bRunning;
216 : };
217 :
218 : // end group ugbase_common
219 : /// \}
220 :
221 : } // namespace ug
222 :
223 :
224 : #endif // __H__UG__STOPWATCH_H__
|