dune-fem 2.8-git
femtimer.hh
Go to the documentation of this file.
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
11#include <dune/common/exceptions.hh>
12#include <dune/common/timer.hh>
13
18
19namespace Dune
20{
21
22 namespace Fem
23 {
24
25 // Timer
26 // -----
27
28 template< bool enable >
29 struct Timer;
30
31
32 template<>
33 struct Timer< false >
34 {
35 typedef enum { max, sum } operation;
36
37 static unsigned int addTo ( const std::string &name, int nr = 0 ) { return 0; }
38 static void removeFrom ( unsigned int id ) {}
39 static void removeAll () {}
40
41 static void start ( int id, int nr = 0 ) {}
42 static double stop ( int id, int nr = 0, operation op = sum ) { return -1; }
43 static double stop ( int id, operation op ) { return -1; }
44
45 static void reset() {}
46 static void reset( int id ) {}
47 static void reset( int id, int nr ) {}
48
49 static void print ( std::ostream &out, int id ) {}
50 static void print ( std::ostream &out, const std::string &msg = "" ) {}
51
52 static void printFile ( const std::string &fileName, int step = 1 ) {}
53 static void printFile ( const TimeProviderBase &tp,
54 const std::string &fileName, int step = 1 ) {}
55 };
56
57
58 template<>
59 struct Timer< true >
60 {
61 typedef enum { max, sum } operation;
62
63 private:
64 struct TimerInfo
65 {
66 std::vector< double > startTimes, times;
67 std::string name;
68
69 TimerInfo ( const std::string &n, const unsigned int nr )
70 : startTimes( nr ), times( nr ), name( n )
71 {}
72 };
73
74 public:
75 Timer ();
76 ~Timer ();
77
78 private:
79 void push_time() { timesS_.push( timer_.elapsed() ); }
80
81 double pop_time()
82 {
83 const double elapsed = timer_.elapsed() - timesS_.top();
84 timesS_.pop();
85 return elapsed;
86 }
87
88 unsigned int add ( const std::string &name, int nr );
89 void remove ( unsigned int id );
90 void remove ();
91
92 void start_timer( int id, int nr )
93 {
94 timers_[ id ].startTimes[ nr ] = timer_.elapsed();
95 assert( timers_[ id ].startTimes[ 0 ] >= double( 0 ) );
96 }
97
98 double stop_timer ( int id, int nr, operation op )
99 {
100 TimerInfo &info = timers_[ id ];
101 assert( (info.startTimes[ nr ] >= double( 0 )) && (info.startTimes[ 0 ] >= double( 0 )) );
102 double elapsed = timer_.elapsed() - info.startTimes[ nr ];
103 info.startTimes[ nr ] = double( -1 );
104 switch( op )
105 {
106 case sum:
107 info.times[ nr ] += elapsed;
108 break;
109 case max:
110 info.times[ nr ] = std::max( info.times[ nr ], elapsed );
111 break;
112 }
113 return elapsed;
114 }
115
116 void reset_timer ( int id, int nr )
117 {
118 timers_[ id ].times[ nr ] = double( 0 );
119 timers_[ id ].startTimes[ nr ] = double( -1 );
120 }
121
122 void reset_timer ( int id )
123 {
124 for( unsigned int i = 0; i < timers_[ id ].times.size(); ++i )
125 reset_timer( id, i );
126 }
127
128 void reset_timer ()
129 {
130 for( unsigned int i = 0; i < timers_.size(); ++i )
131 reset_timer( i );
132 }
133
134 void print_timer ( std::ostream &out, int id );
135 void print_timer ( std::ostream &out, const std::string &msg );
136
137 size_t inMS ( const double t )
138 {
139 return (size_t (t * 1e3));
140 }
141
142 size_t inProz ( const double p, double rel )
143 {
144 size_t ret = (size_t)((p / rel) * 100.);
145 return std :: min( ret, size_t(100) );
146 }
147
148 void printToFile ();
149 void printToFile ( const std::string &fileName, int step );
150 void printToFile ( const TimeProviderBase &tp, const std::string &fileName, int step );
151
152 friend class Dune::Fem::Singleton< Timer >;
153
154 static Timer &instance ()
155 {
157 }
158
159 public:
161 static void start () { instance().push_time(); }
162
164 static double stop () { return instance().pop_time(); }
165
170 static unsigned int addTo ( const std::string &name, int nr = 0 )
171 {
172 return instance().add(name,nr+1);
173 }
174
176 static void removeFrom ( unsigned int id ) { instance().remove( id ); }
177
179 static void removeAll () { instance().remove(); }
180
184 static void start ( int id, int nr = 0 ) { instance().start_timer( id, nr ); }
185
191 static double stop ( int id, int nr = 0, operation op = sum )
192 {
193 return instance().stop_timer( id, nr, op );
194 }
195
200 static double stop ( int id, operation op )
201 {
202 return instance().stop_timer( id, 0, op );
203 }
204
206 static void reset () { instance().reset_timer(); }
207
209 static void reset ( int id ) { instance().reset_timer( id ); }
210
212 static void reset ( int id, int nr ) { instance().reset_timer( id, nr ); }
213
215 static void print ( std::ostream &out, int id ) { instance().print_timer( out, id ); }
216
218 static void print ( std::ostream &out, const std::string &msg = "" )
219 {
220 instance().print_timer(out,msg);
221 }
222
227 static void printFile ( const std::string &fileName, int step = 1 )
228 {
229 instance().printToFile(rankName(fileName, MPIManager::rank()),step);
230 }
231
238 static void printFile ( const TimeProviderBase &tp,
239 const std::string &fileName, int step = 1 )
240 {
241 instance().printToFile(tp,rankName(fileName, MPIManager::rank()),step);
242 }
243
244 private:
245 static std::string rankName( const std::string &fileName, const int rank )
246 {
247 std::stringstream newfilename;
248 newfilename << fileName << "." << rank ;
249 return newfilename.str();
250 }
251
252 Dune::Timer timer_;
253 std::stack< double > timesS_;
254 std::vector< TimerInfo > timers_;
255 std::ofstream output_;
256 int stepCount_;
257 bool changed_;
258 };
259
260 // this method is defined inline
261 // because is uses MPI stuff which
262 // does not work when compiled into the lib
264 {
265 double totalTime = pop_time();
266
267 if( output_.is_open() )
268 {
269 output_ << "# ******** TOTAL RUNTIME: " << totalTime
270 << " ******** " << std::endl;
271 output_.close();
272 }
273
275 if( comm.rank() == 0 )
276 {
277 double *totalTimes = new double[ comm.size() ];
278 comm.gather( &totalTime, totalTimes, 1, 0 );
279 double avgTime = 0.0;
280 double minTime = std::numeric_limits< double >::max();
281 double maxTime = std::numeric_limits< double >::min();
282 for( int i = 0; i < comm.size(); ++i )
283 {
284 avgTime += totalTimes[ i ];
285 minTime = std::min( minTime, totalTimes[ i ] );
286 maxTime = std::max( maxTime, totalTimes[ i ] );
287 }
288 avgTime /= comm.size();
289 delete[] totalTimes;
290
291 std::cerr << "# ******** TOTAL RUNTIME: average = " << avgTime
292 << ", minimum = " << minTime << ", maximum = " << maxTime
293 << " ******** " << std::endl;
294 }
295 else
296 comm.gather( &totalTime, (double *)0, 1, 0 );
297 }
298
299 } // namespace Fem
300
301
302
414#ifdef FEMTIMER
416#else
418#endif
419
420
421
422#define TIMEDEXECUTION(command) \
423 (femTimer.start(),command,femTimer.stop())
430 public:
432 }
433 void start() {
434 start_=time_.elapsed();
435 }
436 void end() {
437 total_=start_-time_.elapsed();
438 }
439 double read() {
440 return total_;
441 }
442 void reset() {
443 total_=0;
444 }
445 double total_;
446 double start_;
447 Timer time_;
448 };
449
450} // namespace Dune
451
452#endif // #ifndef DUNE_FEM_FEMTIMER_HH
double max(const Dune::Fem::Double &v, const double p)
Definition: double.hh:965
double min(const Dune::Fem::Double &v, const double p)
Definition: double.hh:953
Definition: bindguard.hh:11
Fem::Timer< false > FemTimer
Definition: femtimer.hh:417
static double max(const Double &v, const double p)
Definition: double.hh:398
static constexpr T max(T a)
Definition: utility.hh:77
static constexpr std::decay_t< T > sum(T a)
Definition: utility.hh:33
static constexpr T min(T a)
Definition: utility.hh:93
Definition: grcommon.hh:31
Definition: femtimer.hh:29
Definition: femtimer.hh:34
static void print(std::ostream &out, int id)
Definition: femtimer.hh:49
static void printFile(const std::string &fileName, int step=1)
Definition: femtimer.hh:52
static void reset(int id)
Definition: femtimer.hh:46
static double stop(int id, int nr=0, operation op=sum)
Definition: femtimer.hh:42
operation
Definition: femtimer.hh:35
@ max
Definition: femtimer.hh:35
static void start(int id, int nr=0)
Definition: femtimer.hh:41
static void reset(int id, int nr)
Definition: femtimer.hh:47
static void print(std::ostream &out, const std::string &msg="")
Definition: femtimer.hh:50
static void removeAll()
Definition: femtimer.hh:39
static unsigned int addTo(const std::string &name, int nr=0)
Definition: femtimer.hh:37
static void printFile(const TimeProviderBase &tp, const std::string &fileName, int step=1)
Definition: femtimer.hh:53
static void removeFrom(unsigned int id)
Definition: femtimer.hh:38
static double stop(int id, operation op)
Definition: femtimer.hh:43
static void reset()
Definition: femtimer.hh:45
Definition: femtimer.hh:60
static void reset(int id, int nr)
rest a given subtimer
Definition: femtimer.hh:212
static double stop(int id, operation op)
Definition: femtimer.hh:200
static void reset()
reset all timers to zero
Definition: femtimer.hh:206
static double stop(int id, int nr=0, operation op=sum)
Definition: femtimer.hh:191
static void printFile(const std::string &fileName, int step=1)
Definition: femtimer.hh:227
static void start(int id, int nr=0)
Definition: femtimer.hh:184
static unsigned int addTo(const std::string &name, int nr=0)
Definition: femtimer.hh:170
static void printFile(const TimeProviderBase &tp, const std::string &fileName, int step=1)
Definition: femtimer.hh:238
static void removeFrom(unsigned int id)
remove a timer with given id
Definition: femtimer.hh:176
static double stop()
retrieve a timer from the stack
Definition: femtimer.hh:164
static void print(std::ostream &out, const std::string &msg="")
print the values of all timers to a stream
Definition: femtimer.hh:218
static void start()
push a new timer to the stack
Definition: femtimer.hh:161
static void removeAll()
remove all timers
Definition: femtimer.hh:179
static void print(std::ostream &out, int id)
print the values of a given timer (plus subtimers) to a stream
Definition: femtimer.hh:215
operation
Definition: femtimer.hh:61
@ max
Definition: femtimer.hh:61
static void reset(int id)
reset a given timer with all its subtimers
Definition: femtimer.hh:209
class with a start and stop method for timing parts of a program.
Definition: femtimer.hh:429
void end()
Definition: femtimer.hh:436
void reset()
Definition: femtimer.hh:442
void start()
Definition: femtimer.hh:433
ExecutionTimer()
Definition: femtimer.hh:431
double total_
Definition: femtimer.hh:445
Timer time_
Definition: femtimer.hh:447
double start_
Definition: femtimer.hh:446
double read()
Definition: femtimer.hh:439
static const CollectiveCommunication & comm()
Definition: mpimanager.hh:147
Dune::CollectiveCommunication< MPIHelper::MPICommunicator > CollectiveCommunication
Definition: mpimanager.hh:26
static int rank()
Definition: mpimanager.hh:155
general base for time providers
Definition: timeprovider.hh:36
return singleton instance of given Object type.
Definition: singleton.hh:71
static Object & instance(Args &&... args)
return singleton instance of given Object type.
Definition: singleton.hh:101