DUNE-FEM (unstable)

femtimer.hh
1#ifndef DUNE_FEM_FEMTIMER_HH
2#define DUNE_FEM_FEMTIMER_HH
3
4#include <stack>
5#include <vector>
6#include <string>
7#include <fstream>
8#include <iomanip>
9#include <limits>
10
12#include <dune/common/timer.hh>
13
14#include <dune/fem/io/parameter.hh>
15#include <dune/fem/solver/timeprovider.hh>
16#include <dune/fem/storage/singleton.hh>
17
18namespace Dune
19{
20
21 namespace Fem
22 {
23
24 // Timer
25 // -----
26
27 template< bool enable >
28 struct Timer;
29
30
31 template<>
32 struct Timer< false >
33 {
34 typedef enum { max, sum } operation;
35
36 static unsigned int addTo ( const std::string &name, int nr = 0 ) { return 0; }
37 static void removeFrom ( unsigned int id ) {}
38 static void removeAll () {}
39
40 static void start ( int id, int nr = 0 ) {}
41 static double stop ( int id, int nr = 0, operation op = sum ) { return -1; }
42 static double stop ( int id, operation op ) { return -1; }
43
44 static void reset() {}
45 static void reset( int id ) {}
46 static void reset( int id, int nr ) {}
47
48 static void print ( std::ostream &out, int id ) {}
49 static void print ( std::ostream &out, const std::string &msg = "" ) {}
50
51 static void printFile ( const std::string &fileName, int step = 1 ) {}
52 static void printFile ( const TimeProviderBase &tp,
53 const std::string &fileName, int step = 1 ) {}
54 };
55
56
57 template<>
58 struct Timer< true >
59 {
60 typedef enum { max, sum } operation;
61
62 private:
63 struct TimerInfo
64 {
65 std::vector< double > startTimes, times;
66 std::string name;
67
68 TimerInfo ( const std::string &n, const unsigned int nr )
69 : startTimes( nr ), times( nr ), name( n )
70 {}
71 };
72
73 public:
74 Timer ();
75 ~Timer ();
76
77 private:
78 void push_time() { timesS_.push( timer_.elapsed() ); }
79
80 double pop_time()
81 {
82 const double elapsed = timer_.elapsed() - timesS_.top();
83 timesS_.pop();
84 return elapsed;
85 }
86
87 unsigned int add ( const std::string &name, int nr );
88 void remove ( unsigned int id );
89 void remove ();
90
91 void start_timer( int id, int nr )
92 {
93 timers_[ id ].startTimes[ nr ] = timer_.elapsed();
94 assert( timers_[ id ].startTimes[ 0 ] >= double( 0 ) );
95 }
96
97 double stop_timer ( int id, int nr, operation op )
98 {
99 TimerInfo &info = timers_[ id ];
100 assert( (info.startTimes[ nr ] >= double( 0 )) && (info.startTimes[ 0 ] >= double( 0 )) );
101 double elapsed = timer_.elapsed() - info.startTimes[ nr ];
102 info.startTimes[ nr ] = double( -1 );
103 switch( op )
104 {
105 case sum:
106 info.times[ nr ] += elapsed;
107 break;
108 case max:
109 info.times[ nr ] = std::max( info.times[ nr ], elapsed );
110 break;
111 }
112 return elapsed;
113 }
114
115 void reset_timer ( int id, int nr )
116 {
117 timers_[ id ].times[ nr ] = double( 0 );
118 timers_[ id ].startTimes[ nr ] = double( -1 );
119 }
120
121 void reset_timer ( int id )
122 {
123 for( unsigned int i = 0; i < timers_[ id ].times.size(); ++i )
124 reset_timer( id, i );
125 }
126
127 void reset_timer ()
128 {
129 for( unsigned int i = 0; i < timers_.size(); ++i )
130 reset_timer( i );
131 }
132
133 void print_timer ( std::ostream &out, int id );
134 void print_timer ( std::ostream &out, const std::string &msg );
135
136 size_t inMS ( const double t )
137 {
138 return (size_t (t * 1e3));
139 }
140
141 size_t inProz ( const double p, double rel )
142 {
143 size_t ret = (size_t)((p / rel) * 100.);
144 return std :: min( ret, size_t(100) );
145 }
146
147 void printToFile ();
148 void printToFile ( const std::string &fileName, int step );
149 void printToFile ( const TimeProviderBase &tp, const std::string &fileName, int step );
150
151 friend class Dune::Fem::Singleton< Timer >;
152
153 static Timer &instance ()
154 {
156 }
157
158 public:
160 static void start () { instance().push_time(); }
161
163 static double stop () { return instance().pop_time(); }
164
169 static unsigned int addTo ( const std::string &name, int nr = 0 )
170 {
171 return instance().add(name,nr+1);
172 }
173
175 static void removeFrom ( unsigned int id ) { instance().remove( id ); }
176
178 static void removeAll () { instance().remove(); }
179
183 static void start ( int id, int nr = 0 ) { instance().start_timer( id, nr ); }
184
190 static double stop ( int id, int nr = 0, operation op = sum )
191 {
192 return instance().stop_timer( id, nr, op );
193 }
194
199 static double stop ( int id, operation op )
200 {
201 return instance().stop_timer( id, 0, op );
202 }
203
205 static void reset () { instance().reset_timer(); }
206
208 static void reset ( int id ) { instance().reset_timer( id ); }
209
211 static void reset ( int id, int nr ) { instance().reset_timer( id, nr ); }
212
214 static void print ( std::ostream &out, int id ) { instance().print_timer( out, id ); }
215
217 static void print ( std::ostream &out, const std::string &msg = "" )
218 {
219 instance().print_timer(out,msg);
220 }
221
226 static void printFile ( const std::string &fileName, int step = 1 )
227 {
228 instance().printToFile(rankName(fileName, MPIManager::rank()),step);
229 }
230
237 static void printFile ( const TimeProviderBase &tp,
238 const std::string &fileName, int step = 1 )
239 {
240 instance().printToFile(tp,rankName(fileName, MPIManager::rank()),step);
241 }
242
243 private:
244 static std::string rankName( const std::string &fileName, const int rank )
245 {
246 std::stringstream newfilename;
247 newfilename << fileName << "." << rank ;
248 return newfilename.str();
249 }
250
251 Dune::Timer timer_;
252 std::stack< double > timesS_;
253 std::vector< TimerInfo > timers_;
254 std::ofstream output_;
255 int stepCount_;
256 bool changed_;
257 };
258
259 // this method is defined inline
260 // because is uses MPI stuff which
261 // does not work when compiled into the lib
262 inline Timer< true >::~Timer ()
263 {
264 double totalTime = pop_time();
265
266 if( output_.is_open() )
267 {
268 output_ << "# ******** TOTAL RUNTIME: " << totalTime
269 << " ******** " << std::endl;
270 output_.close();
271 }
272
273 const MPIManager::Communication &comm = MPIManager::comm();
274 if( comm.rank() == 0 )
275 {
276 double *totalTimes = new double[ comm.size() ];
277 comm.gather( &totalTime, totalTimes, 1, 0 );
278 double avgTime = 0.0;
279 double minTime = std::numeric_limits< double >::max();
280 double maxTime = std::numeric_limits< double >::min();
281 for( int i = 0; i < comm.size(); ++i )
282 {
283 avgTime += totalTimes[ i ];
284 minTime = std::min( minTime, totalTimes[ i ] );
285 maxTime = std::max( maxTime, totalTimes[ i ] );
286 }
287 avgTime /= comm.size();
288 delete[] totalTimes;
289
290 std::cerr << "# ******** TOTAL RUNTIME: average = " << avgTime
291 << ", minimum = " << minTime << ", maximum = " << maxTime
292 << " ******** " << std::endl;
293 }
294 else
295 comm.gather( &totalTime, (double *)0, 1, 0 );
296 }
297
298 } // namespace Fem
299
300
301
413#ifdef FEMTIMER
414 typedef Fem::Timer< true > FemTimer;
415#else
416 typedef Fem::Timer< false > FemTimer;
417#endif
418
419
420
421#define TIMEDEXECUTION(command) \
422 (femTimer.start(),command,femTimer.stop())
429 public:
430 ExecutionTimer() : total_(0) {
431 }
432 void start() {
433 start_=time_.elapsed();
434 }
435 void end() {
436 total_=start_-time_.elapsed();
437 }
438 double read() {
439 return total_;
440 }
441 void reset() {
442 total_=0;
443 }
444 double total_;
445 double start_;
446 Timer time_;
447 };
448
449} // namespace Dune
450
451#endif // #ifndef DUNE_FEM_FEMTIMER_HH
class with a start and stop method for timing parts of a program.
Definition: femtimer.hh:428
return singleton instance of given Object type.
Definition: singleton.hh:93
static DUNE_EXPORT Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:123
A simple stop watch.
Definition: timer.hh:43
double elapsed() const noexcept
Get elapsed user-time from last reset until now/last stop in seconds.
Definition: timer.hh:77
class with singleton instance managing timing for parts of program.
A few common exception classes.
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
Dune namespace.
Definition: alignedallocator.hh:13
A simple timing class.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)