1#ifndef DUNE_FEM_FEMTIMER_HH
2#define DUNE_FEM_FEMTIMER_HH
11#include <dune/common/exceptions.hh>
12#include <dune/common/timer.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; }
40 static void start (
int id,
int nr = 0 ) {}
41 static double stop (
int id,
int nr = 0,
operation op = sum ) {
return -1; }
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 ) {}
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;
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.);
144 return std :: min( ret,
size_t(100) );
148 void printToFile (
const std::string &fileName,
int step );
153 static Timer &instance ()
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 )
171 return instance().add(name,nr+1);
175 static void removeFrom (
unsigned int id ) { instance().remove(
id ); }
183 static void start (
int id,
int nr = 0 ) { instance().start_timer(
id, nr ); }
192 return instance().stop_timer(
id, nr, 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 =
"" )
219 instance().print_timer(
out,msg);
226 static void printFile (
const std::string &fileName,
int step = 1 )
238 const std::string &fileName,
int step = 1 )
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_;
264 double totalTime = pop_time();
266 if( output_.is_open() )
268 output_ <<
"# ******** TOTAL RUNTIME: " << totalTime
269 <<
" ******** " << std::endl;
274 if( comm.rank() == 0 )
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 )
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 );
421#define TIMEDEXECUTION(command) \
422 (femTimer.start(),command,femTimer.stop())
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:416
static double max(const Double &v, const double p)
Definition: double.hh:398
Definition: grcommon.hh:31
Definition: femtimer.hh:28
Definition: femtimer.hh:33
static void print(std::ostream &out, int id)
Definition: femtimer.hh:48
static void printFile(const std::string &fileName, int step=1)
Definition: femtimer.hh:51
static void reset(int id)
Definition: femtimer.hh:45
static double stop(int id, int nr=0, operation op=sum)
Definition: femtimer.hh:41
operation
Definition: femtimer.hh:34
@ max
Definition: femtimer.hh:34
static void start(int id, int nr=0)
Definition: femtimer.hh:40
static void reset(int id, int nr)
Definition: femtimer.hh:46
static void print(std::ostream &out, const std::string &msg="")
Definition: femtimer.hh:49
static void removeAll()
Definition: femtimer.hh:38
static unsigned int addTo(const std::string &name, int nr=0)
Definition: femtimer.hh:36
static void printFile(const TimeProviderBase &tp, const std::string &fileName, int step=1)
Definition: femtimer.hh:52
static void removeFrom(unsigned int id)
Definition: femtimer.hh:37
static double stop(int id, operation op)
Definition: femtimer.hh:42
static void reset()
Definition: femtimer.hh:44
Definition: femtimer.hh:59
static void reset(int id, int nr)
rest a given subtimer
Definition: femtimer.hh:211
static double stop(int id, operation op)
Definition: femtimer.hh:199
static void reset()
reset all timers to zero
Definition: femtimer.hh:205
static double stop(int id, int nr=0, operation op=sum)
Definition: femtimer.hh:190
static void printFile(const std::string &fileName, int step=1)
Definition: femtimer.hh:226
static void start(int id, int nr=0)
Definition: femtimer.hh:183
static unsigned int addTo(const std::string &name, int nr=0)
Definition: femtimer.hh:169
static void printFile(const TimeProviderBase &tp, const std::string &fileName, int step=1)
Definition: femtimer.hh:237
static void removeFrom(unsigned int id)
remove a timer with given id
Definition: femtimer.hh:175
static double stop()
retrieve a timer from the stack
Definition: femtimer.hh:163
static void print(std::ostream &out, const std::string &msg="")
print the values of all timers to a stream
Definition: femtimer.hh:217
static void start()
push a new timer to the stack
Definition: femtimer.hh:160
static void removeAll()
remove all timers
Definition: femtimer.hh:178
static void print(std::ostream &out, int id)
print the values of a given timer (plus subtimers) to a stream
Definition: femtimer.hh:214
operation
Definition: femtimer.hh:60
@ max
Definition: femtimer.hh:60
static void reset(int id)
reset a given timer with all its subtimers
Definition: femtimer.hh:208
class with a start and stop method for timing parts of a program.
Definition: femtimer.hh:428
void end()
Definition: femtimer.hh:435
void reset()
Definition: femtimer.hh:441
void start()
Definition: femtimer.hh:432
ExecutionTimer()
Definition: femtimer.hh:430
double total_
Definition: femtimer.hh:444
Timer time_
Definition: femtimer.hh:446
double start_
Definition: femtimer.hh:445
double read()
Definition: femtimer.hh:438
static const CollectiveCommunication & comm()
Definition: mpimanager.hh:393
Dune::CollectiveCommunication< MPIHelper::MPICommunicator > CollectiveCommunication
Definition: mpimanager.hh:339
static int rank()
Definition: mpimanager.hh:401
general base for time providers
Definition: timeprovider.hh:36
return singleton instance of given Object type.
Definition: singleton.hh:88