1#ifndef DUNE_FEM_FEMTIMER_HH
2#define DUNE_FEM_FEMTIMER_HH
14#include <dune/fem/io/parameter.hh>
15#include <dune/fem/solver/timeprovider.hh>
16#include <dune/fem/storage/singleton.hh>
27 template<
bool enable >
34 typedef enum {
max, sum } operation;
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 () {}
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; }
44 static void reset() {}
45 static void reset(
int id ) {}
46 static void reset(
int id,
int nr ) {}
48 static void print ( std::ostream &out,
int id ) {}
49 static void print ( std::ostream &out,
const std::string &msg =
"" ) {}
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 ) {}
60 typedef enum {
max, sum } operation;
65 std::vector< double > startTimes, times;
68 TimerInfo (
const std::string &n,
const unsigned int nr )
69 : startTimes( nr ), times( nr ), name( n )
78 void push_time() { timesS_.push( timer_.elapsed() ); }
82 const double elapsed = timer_.elapsed() - timesS_.top();
87 unsigned int add (
const std::string &name,
int nr );
88 void remove (
unsigned int id );
91 void start_timer(
int id,
int nr )
93 timers_[ id ].startTimes[ nr ] = timer_.elapsed();
94 assert( timers_[
id ].startTimes[ 0 ] >=
double( 0 ) );
97 double stop_timer (
int id,
int nr, operation op )
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 );
106 info.times[ nr ] += elapsed;
109 info.times[ nr ] = std::max( info.times[ nr ], elapsed );
115 void reset_timer (
int id,
int nr )
117 timers_[ id ].times[ nr ] = double( 0 );
118 timers_[ id ].startTimes[ nr ] = double( -1 );
121 void reset_timer (
int id )
123 for(
unsigned int i = 0; i < timers_[ id ].times.size(); ++i )
124 reset_timer(
id, i );
129 for(
unsigned int i = 0; i < timers_.size(); ++i )
133 void print_timer ( std::ostream &out,
int id );
134 void print_timer ( std::ostream &out,
const std::string &msg );
136 size_t inMS (
const double t )
138 return (
size_t (t * 1e3));
141 size_t inProz (
const double p,
double rel )
143 size_t ret = (size_t)((p / rel) * 100.);
148 void printToFile (
const std::string &fileName,
int step );
149 void printToFile (
const TimeProviderBase &tp,
const std::string &fileName,
int step );
160 static void start () {
instance().push_time(); }
163 static double stop () {
return instance().pop_time(); }
169 static unsigned int addTo (
const std::string &name,
int nr = 0 )
175 static void removeFrom (
unsigned int id ) {
instance().remove(
id ); }
178 static void removeAll () {
instance().remove(); }
183 static void start (
int id,
int nr = 0 ) {
instance().start_timer(
id, nr ); }
190 static double stop (
int id,
int nr = 0, operation op = sum )
192 return instance().stop_timer(
id, nr, op );
199 static double stop (
int id, operation op )
201 return instance().stop_timer(
id, 0, op );
205 static void reset () {
instance().reset_timer(); }
208 static void reset (
int id ) {
instance().reset_timer(
id ); }
211 static void reset (
int id,
int nr ) {
instance().reset_timer(
id, nr ); }
214 static void print ( std::ostream &out,
int id ) {
instance().print_timer( out,
id ); }
217 static void print ( std::ostream &out,
const std::string &msg =
"" )
226 static void printFile (
const std::string &fileName,
int step = 1 )
228 instance().printToFile(rankName(fileName, MPIManager::rank()),step);
237 static void printFile (
const TimeProviderBase &tp,
238 const std::string &fileName,
int step = 1 )
240 instance().printToFile(tp,rankName(fileName, MPIManager::rank()),step);
244 static std::string rankName(
const std::string &fileName,
const int rank )
246 std::stringstream newfilename;
247 newfilename << fileName <<
"." << rank ;
248 return newfilename.str();
252 std::stack< double > timesS_;
253 std::vector< TimerInfo > timers_;
254 std::ofstream output_;
262 inline Timer< true >::~Timer ()
264 double totalTime = pop_time();
266 if( output_.is_open() )
268 output_ <<
"# ******** TOTAL RUNTIME: " << totalTime
269 <<
" ******** " << std::endl;
273 const MPIManager::Communication &comm = MPIManager::comm();
274 if( comm.rank() == 0 )
276 double *totalTimes =
new double[ comm.size() ];
277 comm.gather( &totalTime, totalTimes, 1, 0 );
278 double avgTime = 0.0;
281 for(
int i = 0; i < comm.size(); ++i )
283 avgTime += totalTimes[ i ];
284 minTime = std::min( minTime, totalTimes[ i ] );
285 maxTime = std::max( maxTime, totalTimes[ i ] );
287 avgTime /= comm.size();
290 std::cerr <<
"# ******** TOTAL RUNTIME: average = " << avgTime
291 <<
", minimum = " << minTime <<
", maximum = " << maxTime
292 <<
" ******** " << std::endl;
295 comm.gather( &totalTime, (
double *)0, 1, 0 );
414 typedef Fem::Timer< true >
FemTimer;
416 typedef Fem::Timer< false >
FemTimer;
421#define TIMEDEXECUTION(command) \
422 (femTimer.start(),command,femTimer.stop())
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