1#ifndef DUNE_FEM_FEMTIMER_HH
2#define DUNE_FEM_FEMTIMER_HH
11#include <dune/common/exceptions.hh>
12#include <dune/common/timer.hh>
28 template<
bool enable >
37 static unsigned int addTo (
const std::string &name,
int nr = 0 ) {
return 0; }
41 static void start (
int id,
int nr = 0 ) {}
47 static void reset(
int id,
int nr ) {}
49 static void print ( std::ostream &out,
int id ) {}
50 static void print ( std::ostream &out,
const std::string &msg =
"" ) {}
52 static void printFile (
const std::string &fileName,
int step = 1 ) {}
54 const std::string &fileName,
int step = 1 ) {}
66 std::vector< double > startTimes, times;
69 TimerInfo (
const std::string &n,
const unsigned int nr )
70 : startTimes( nr ), times( nr ), name( n )
79 void push_time() { timesS_.push( timer_.elapsed() ); }
83 const double elapsed = timer_.elapsed() - timesS_.top();
88 unsigned int add (
const std::string &name,
int nr );
89 void remove (
unsigned int id );
92 void start_timer(
int id,
int nr )
94 timers_[ id ].startTimes[ nr ] = timer_.elapsed();
95 assert( timers_[
id ].startTimes[ 0 ] >=
double( 0 ) );
98 double stop_timer (
int id,
int nr, operation op )
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 );
107 info.times[ nr ] += elapsed;
116 void reset_timer (
int id,
int nr )
118 timers_[ id ].times[ nr ] = double( 0 );
119 timers_[ id ].startTimes[ nr ] = double( -1 );
122 void reset_timer (
int id )
124 for(
unsigned int i = 0; i < timers_[ id ].times.size(); ++i )
125 reset_timer(
id, i );
130 for(
unsigned int i = 0; i < timers_.size(); ++i )
134 void print_timer ( std::ostream &out,
int id );
135 void print_timer ( std::ostream &out,
const std::string &msg );
137 size_t inMS (
const double t )
139 return (
size_t (t * 1e3));
142 size_t inProz (
const double p,
double rel )
144 size_t ret = (size_t)((p / rel) * 100.);
149 void printToFile (
const std::string &fileName,
int step );
170 static unsigned int addTo (
const std::string &name,
int nr = 0 )
184 static void start (
int id,
int nr = 0 ) {
instance().start_timer(
id, nr ); }
193 return instance().stop_timer(
id, nr, op );
202 return instance().stop_timer(
id, 0, op );
218 static void print ( std::ostream &out,
const std::string &msg =
"" )
227 static void printFile (
const std::string &fileName,
int step = 1 )
239 const std::string &fileName,
int step = 1 )
245 static std::string rankName(
const std::string &fileName,
const int rank )
247 std::stringstream newfilename;
248 newfilename << fileName <<
"." << rank ;
249 return newfilename.str();
253 std::stack< double > timesS_;
254 std::vector< TimerInfo > timers_;
255 std::ofstream output_;
265 double totalTime = pop_time();
267 if( output_.is_open() )
269 output_ <<
"# ******** TOTAL RUNTIME: " << totalTime
270 <<
" ******** " << std::endl;
275 if( comm.rank() == 0 )
277 double *totalTimes =
new double[ comm.size() ];
278 comm.gather( &totalTime, totalTimes, 1, 0 );
279 double avgTime = 0.0;
282 for(
int i = 0; i < comm.size(); ++i )
284 avgTime += totalTimes[ i ];
285 minTime =
std::min( minTime, totalTimes[ i ] );
286 maxTime =
std::max( maxTime, totalTimes[ i ] );
288 avgTime /= comm.size();
291 std::cerr <<
"# ******** TOTAL RUNTIME: average = " << avgTime
292 <<
", minimum = " << minTime <<
", maximum = " << maxTime
293 <<
" ******** " << std::endl;
296 comm.gather( &totalTime, (
double *)0, 1, 0 );
422#define TIMEDEXECUTION(command) \
423 (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: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