dune-fem  2.4.1-rc
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 
14 #include <dune/fem/io/parameter.hh>
17 
18 namespace 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  Timer ();
74  ~Timer ();
75 
76  void push_time() { timesS_.push( timer_.elapsed() ); }
77 
78  double pop_time()
79  {
80  const double elapsed = timer_.elapsed() - timesS_.top();
81  timesS_.pop();
82  return elapsed;
83  }
84 
85  unsigned int add ( const std::string &name, int nr );
86  void remove ( unsigned int id );
87  void remove ();
88 
89  void start_timer( int id, int nr )
90  {
91  timers_[ id ].startTimes[ nr ] = timer_.elapsed();
92  assert( timers_[ id ].startTimes[ 0 ] >= double( 0 ) );
93  }
94 
95  double stop_timer ( int id, int nr, operation op )
96  {
97  TimerInfo &info = timers_[ id ];
98  assert( (info.startTimes[ nr ] >= double( 0 )) && (info.startTimes[ 0 ] >= double( 0 )) );
99  double elapsed = timer_.elapsed() - info.startTimes[ nr ];
100  info.startTimes[ nr ] = double( -1 );
101  switch( op )
102  {
103  case sum:
104  info.times[ nr ] += elapsed;
105  break;
106  case max:
107  info.times[ nr ] = std::max( info.times[ nr ], elapsed );
108  break;
109  }
110  return elapsed;
111  }
112 
113  void reset_timer ( int id, int nr )
114  {
115  timers_[ id ].times[ nr ] = double( 0 );
116  timers_[ id ].startTimes[ nr ] = double( -1 );
117  }
118 
119  void reset_timer ( int id )
120  {
121  for( unsigned int i = 0; i < timers_[ id ].times.size(); ++i )
122  reset_timer( id, i );
123  }
124 
125  void reset_timer ()
126  {
127  for( unsigned int i = 0; i < timers_.size(); ++i )
128  reset_timer( i );
129  }
130 
131  void print_timer ( std::ostream &out, int id );
132  void print_timer ( std::ostream &out, const std::string &msg );
133 
134  size_t inMS ( const double t )
135  {
136  return (size_t (t * 1e3));
137  }
138 
139  size_t inProz ( const double p, double rel )
140  {
141  size_t ret = (size_t)((p / rel) * 100.);
142  return std :: min( ret, size_t(100) );
143  }
144 
145  void printToFile ();
146  void printToFile ( const std::string &fileName, int step );
147  void printToFile ( const TimeProviderBase &tp, const std::string &fileName, int step );
148 
149  static Timer &instance ()
150  {
151  static Timer instance_;
152  // don't use this class in multi thread environment
154  return instance_;
155  }
156 
157  public:
159  static void start () { instance().push_time(); }
160 
162  static double stop () { return instance().pop_time(); }
163 
168  static unsigned int addTo ( const std::string &name, int nr = 0 )
169  {
170  return instance().add(name,nr+1);
171  }
172 
174  static void removeFrom ( unsigned int id ) { instance().remove( id ); }
175 
177  static void removeAll () { instance().remove(); }
178 
182  static void start ( int id, int nr = 0 ) { instance().start_timer( id, nr ); }
183 
189  static double stop ( int id, int nr = 0, operation op = sum )
190  {
191  return instance().stop_timer( id, nr, op );
192  }
193 
198  static double stop ( int id, operation op )
199  {
200  return instance().stop_timer( id, 0, op );
201  }
202 
204  static void reset () { instance().reset_timer(); }
205 
207  static void reset ( int id ) { instance().reset_timer( id ); }
208 
210  static void reset ( int id, int nr ) { instance().reset_timer( id, nr ); }
211 
213  static void print ( std::ostream &out, int id ) { instance().print_timer( out, id ); }
214 
216  static void print ( std::ostream &out, const std::string &msg = "" )
217  {
218  instance().print_timer(out,msg);
219  }
220 
225  static void printFile ( const std::string &fileName, int step = 1 )
226  {
227  instance().printToFile(rankName(fileName, MPIManager::rank()),step);
228  }
229 
236  static void printFile ( const TimeProviderBase &tp,
237  const std::string &fileName, int step = 1 )
238  {
239  instance().printToFile(tp,rankName(fileName, MPIManager::rank()),step);
240  }
241 
242  private:
243  static std::string rankName( const std::string &fileName, const int rank )
244  {
245  std::stringstream newfilename;
246  newfilename << fileName << "." << rank ;
247  return newfilename.str();
248  }
249 
250  Dune::Timer timer_;
251  std::stack< double > timesS_;
252  std::vector< TimerInfo > timers_;
253  std::ofstream output_;
254  int stepCount_;
255  bool changed_;
256  };
257 
258  // this method is defined inline
259  // because is uses MPI stuff which
260  // does not work when compiled into the lib
261  inline Timer< true >::~Timer ()
262  {
263  double totalTime = pop_time();
264 
265  if( output_.is_open() )
266  {
267  output_ << "# ******** TOTAL RUNTIME: " << totalTime
268  << " ******** " << std::endl;
269  output_.close();
270  }
271 
273  if( comm.rank() == 0 )
274  {
275  double *totalTimes = new double[ comm.size() ];
276  comm.gather( &totalTime, totalTimes, 1, 0 );
277  double avgTime = 0.0;
278  double minTime = std::numeric_limits< double >::max();
279  double maxTime = std::numeric_limits< double >::min();
280  for( int i = 0; i < comm.size(); ++i )
281  {
282  avgTime += totalTimes[ i ];
283  minTime = std::min( minTime, totalTimes[ i ] );
284  maxTime = std::max( maxTime, totalTimes[ i ] );
285  }
286  avgTime /= comm.size();
287  delete[] totalTimes;
288 
289  std::cerr << "# ******** TOTAL RUNTIME: average = " << avgTime
290  << ", minimum = " << minTime << ", maximum = " << maxTime
291  << " ******** " << std::endl;
292  }
293  else
294  comm.gather( &totalTime, (double *)0, 1, 0 );
295  }
296 
297  } // namespace Fem
298 
299 
300 
412 #ifdef FEMTIMER
414 #else
416 #endif
417 
418 
419 
420 #define TIMEDEXECUTION(command) \
421  (femTimer.start(),command,femTimer.stop())
422 
428  public:
429  ExecutionTimer() : total_(0) {
430  }
431  void start() {
432  start_=time_.elapsed();
433  }
434  void end() {
435  total_=start_-time_.elapsed();
436  }
437  double read() {
438  return total_;
439  }
440  void reset() {
441  total_=0;
442  }
443  double total_;
444  double start_;
445  Timer time_;
446  };
447 
448 } // namespace Dune
449 
450 #endif // #ifndef DUNE_FEM_FEMTIMER_HH
static void removeFrom(unsigned int id)
Definition: femtimer.hh:37
static void print(std::ostream &out, const std::string &msg="")
print the values of all timers to a stream
Definition: femtimer.hh:216
static int rank()
Definition: mpimanager.hh:116
static double stop(int id, operation op)
Definition: femtimer.hh:198
static constexpr T min(T a)
Definition: utility.hh:81
void end()
Definition: femtimer.hh:434
ExecutionTimer()
Definition: femtimer.hh:429
double start_
Definition: femtimer.hh:444
static void removeAll()
Definition: femtimer.hh:38
static const CollectiveCommunication & comm()
Definition: mpimanager.hh:108
static void printFile(const std::string &fileName, int step=1)
Definition: femtimer.hh:51
static constexpr T max(T a)
Definition: utility.hh:65
static void start(int id, int nr=0)
Definition: femtimer.hh:182
static void reset(int id)
Definition: femtimer.hh:45
void start()
Definition: femtimer.hh:431
static void printFile(const TimeProviderBase &tp, const std::string &fileName, int step=1)
Definition: femtimer.hh:236
static constexpr T sum(T a)
Definition: utility.hh:33
static unsigned int addTo(const std::string &name, int nr=0)
Definition: femtimer.hh:168
Definition: femtimer.hh:58
Timer time_
Definition: femtimer.hh:445
static unsigned int addTo(const std::string &name, int nr=0)
Definition: femtimer.hh:36
static double max(const Double &v, const double p)
Definition: double.hh:387
static void print(std::ostream &out, const std::string &msg="")
Definition: femtimer.hh:49
double read()
Definition: femtimer.hh:437
static double stop()
retrieve a timer from the stack
Definition: femtimer.hh:162
static void print(std::ostream &out, int id)
print the values of a given timer (plus subtimers) to a stream
Definition: femtimer.hh:213
double total_
Definition: femtimer.hh:443
general base for time providers
Definition: timeprovider.hh:35
class with a start and stop method for timing parts of a program.
Definition: femtimer.hh:427
Fem::Timer< false > FemTimer
Definition: femtimer.hh:415
static double stop(int id, operation op)
Definition: femtimer.hh:42
Definition: coordinate.hh:4
static void start()
push a new timer to the stack
Definition: femtimer.hh:159
static void printFile(const std::string &fileName, int step=1)
Definition: femtimer.hh:225
void reset()
Definition: femtimer.hh:440
static void print(std::ostream &out, int id)
Definition: femtimer.hh:48
Definition: femtimer.hh:32
operation
Definition: femtimer.hh:60
Dune::CollectiveCommunication< MPIHelper::MPICommunicator > CollectiveCommunication
Definition: mpimanager.hh:22
static double stop(int id, int nr=0, operation op=sum)
Definition: femtimer.hh:189
static void reset(int id, int nr)
rest a given subtimer
Definition: femtimer.hh:210
operation
Definition: femtimer.hh:34
static void start(int id, int nr=0)
Definition: femtimer.hh:40
static void reset()
Definition: femtimer.hh:44
Definition: femtimer.hh:28
static void reset(int id)
reset a given timer with all its subtimers
Definition: femtimer.hh:207
static bool singleThreadMode()
returns true if program is operating on one thread currently
Definition: threadmanager.hh:217
static void printFile(const TimeProviderBase &tp, const std::string &fileName, int step=1)
Definition: femtimer.hh:52
static void removeFrom(unsigned int id)
remove a timer with given id
Definition: femtimer.hh:174
static void removeAll()
remove all timers
Definition: femtimer.hh:177
static void reset(int id, int nr)
Definition: femtimer.hh:46
static double stop(int id, int nr=0, operation op=sum)
Definition: femtimer.hh:41
static void reset()
reset all timers to zero
Definition: femtimer.hh:204