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 >
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; }
42 static double stop (
int id, operation op ) {
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 ) {}
65 std::vector< double > startTimes, times;
68 TimerInfo (
const std::string &n,
const unsigned int nr )
69 : startTimes( nr ), times( nr ), name( n )
76 void push_time() { timesS_.push( timer_.elapsed() ); }
80 const double elapsed = timer_.elapsed() - timesS_.top();
85 unsigned int add (
const std::string &name,
int nr );
86 void remove (
unsigned int id );
89 void start_timer(
int id,
int nr )
91 timers_[ id ].startTimes[ nr ] = timer_.elapsed();
92 assert( timers_[
id ].startTimes[ 0 ] >=
double( 0 ) );
95 double stop_timer (
int id,
int nr, operation op )
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 );
104 info.times[ nr ] += elapsed;
107 info.times[ nr ] =
std::max( info.times[ nr ], elapsed );
113 void reset_timer (
int id,
int nr )
115 timers_[ id ].times[ nr ] = double( 0 );
116 timers_[ id ].startTimes[ nr ] = double( -1 );
119 void reset_timer (
int id )
121 for(
unsigned int i = 0; i < timers_[ id ].times.size(); ++i )
122 reset_timer(
id, i );
127 for(
unsigned int i = 0; i < timers_.size(); ++i )
131 void print_timer ( std::ostream &out,
int id );
132 void print_timer ( std::ostream &out,
const std::string &msg );
134 size_t inMS (
const double t )
136 return (
size_t (t * 1e3));
139 size_t inProz (
const double p,
double rel )
141 size_t ret = (size_t)((p / rel) * 100.);
146 void printToFile (
const std::string &fileName,
int step );
147 void printToFile (
const TimeProviderBase &tp,
const std::string &fileName,
int step );
149 static Timer &instance ()
151 static Timer instance_;
159 static void start () { instance().push_time(); }
162 static double stop () {
return instance().pop_time(); }
168 static unsigned int addTo (
const std::string &name,
int nr = 0 )
170 return instance().add(name,nr+1);
174 static void removeFrom (
unsigned int id ) { instance().remove(
id ); }
182 static void start (
int id,
int nr = 0 ) { instance().start_timer(
id, nr ); }
189 static double stop (
int id,
int nr = 0, operation op =
sum )
191 return instance().stop_timer(
id, nr, op );
198 static double stop (
int id, operation op )
200 return instance().stop_timer(
id, 0, op );
204 static void reset () { instance().reset_timer(); }
207 static void reset (
int id ) { instance().reset_timer(
id ); }
210 static void reset (
int id,
int nr ) { instance().reset_timer(
id, nr ); }
213 static void print ( std::ostream &out,
int id ) { instance().print_timer( out,
id ); }
216 static void print ( std::ostream &out,
const std::string &msg =
"" )
218 instance().print_timer(out,msg);
225 static void printFile (
const std::string &fileName,
int step = 1 )
237 const std::string &fileName,
int step = 1 )
243 static std::string rankName(
const std::string &fileName,
const int rank )
245 std::stringstream newfilename;
246 newfilename << fileName <<
"." << rank ;
247 return newfilename.str();
251 std::stack< double > timesS_;
252 std::vector< TimerInfo > timers_;
253 std::ofstream output_;
263 double totalTime = pop_time();
265 if( output_.is_open() )
267 output_ <<
"# ******** TOTAL RUNTIME: " << totalTime
268 <<
" ******** " << std::endl;
273 if( comm.rank() == 0 )
275 double *totalTimes =
new double[ comm.size() ];
276 comm.gather( &totalTime, totalTimes, 1, 0 );
277 double avgTime = 0.0;
280 for(
int i = 0; i < comm.size(); ++i )
282 avgTime += totalTimes[ i ];
283 minTime =
std::min( minTime, totalTimes[ i ] );
284 maxTime =
std::max( maxTime, totalTimes[ i ] );
286 avgTime /= comm.size();
289 std::cerr <<
"# ******** TOTAL RUNTIME: average = " << avgTime
290 <<
", minimum = " << minTime <<
", maximum = " << maxTime
291 <<
" ******** " << std::endl;
294 comm.gather( &totalTime, (
double *)0, 1, 0 );
420 #define TIMEDEXECUTION(command) \ 421 (femTimer.start(),command,femTimer.stop()) 432 start_=time_.elapsed();
435 total_=start_-time_.elapsed();
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