deal.II version GIT relicensing-6766-gb5b3195129 2026-09-21 11:00:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
Timer Class Reference

#include <deal.II/base/timer.h>

Detailed Description

The Timer class provides a way to measure both the amount of wall time (i.e., the amount of time elapsed on a wall clock) and the amount of CPU time that certain sections of an application have used. This class also offers facilities for synchronizing the elapsed time across an MPI communicator.

Usage

The Timer class can be started and stopped several times. It stores both the amount of time elapsed over the last start-stop cycle, or lap, as well as the total time elapsed over all laps. Here is an example:

Timer timer; // creating a timer also starts it
// do some complicated computations here
// ...
timer.stop();
std::cout << "Elapsed CPU time: " << timer.cpu_time() << " seconds.\n";
std::cout << "Elapsed wall time: " << timer.wall_time() << " seconds.\n";
// reset timer for the next thing it shall do
timer.reset();
Definition timer.h:128
double cpu_time() const
Definition timer.cc:241
void stop()
Definition timer.cc:212
double wall_time() const
Definition timer.cc:274
void reset()
Definition timer.cc:360

Alternatively, you can also start the timer again instead of resetting it. The times between successive calls to start() and stop() (i.e., the laps) will then be accumulated. The usage of this class is also explained in the step-28 tutorial program.

Note
The TimerOutput (combined with TimerOutput::Scope) classes provide a convenient way to time multiple named sections and summarize the output.
Implementation of this class is system dependent. In particular, CPU times are accumulated from summing across all threads and will usually exceed the wall times.
If this class is constructed with an MPI communicator and sync_lap_times is set to true, then all of the operations of this class are collective operations that have to be performed on all MPI ranks. It is impossible to query a timer object on only some of the MPI ranks. This in particular means that you should not query information from this class in destructors of other objects, because destructors may be triggered during exception handling. If only some of the MPI ranks threw an exception the communication will cause a deadlock and your program will hang without output. The only two safe operations you can do with this class in a destructor are to destroy the object or to call stop().

Definition at line 127 of file timer.h.

Classes

struct  ClockMeasurements
 

Public Member Functions

 Timer ()
 
 Timer (const MPI_Comm mpi_communicator, const bool sync_lap_times=false)
 
const Utilities::MPI::MinMaxAvgget_last_lap_wall_time_data () const
 
const Utilities::MPI::MinMaxAvgget_accumulated_wall_time_data () const
 
template<typename StreamType >
void print_last_lap_wall_time_data (StreamType &stream) const
 
template<typename StreamType >
void print_accumulated_wall_time_data (StreamType &stream) const
 
void start ()
 
void stop ()
 
void reset ()
 
void restart ()
 
bool is_running () const
 
double wall_time () const
 
double last_wall_time () const
 
double cpu_time () const
 
double last_cpu_time () const
 
unsigned int n_laps () const
 

Private Types

using wall_clock_type = std::chrono::steady_clock
 
using cpu_clock_type = CPUClock
 

Private Member Functions

void synchronize_and_update () const
 

Private Attributes

ClockMeasurements< wall_clock_typewall_times
 
ClockMeasurements< cpu_clock_typecpu_times
 
bool running
 
bool is_synchronized
 
MPI_Comm mpi_communicator
 
bool sync_lap_times
 
Utilities::MPI::MinMaxAvg last_lap_wall_time_data
 
Utilities::MPI::MinMaxAvg accumulated_wall_time_data
 
unsigned int n_timed_laps
 
Threads::Mutex mutex
 

Member Typedef Documentation

◆ wall_clock_type

using Timer::wall_clock_type = std::chrono::steady_clock
private

Alias for the wall clock.

Definition at line 348 of file timer.h.

◆ cpu_clock_type

Alias for the CPU clock.

Definition at line 353 of file timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/2]

Timer::Timer ( )

Constructor. Sets the accumulated times to zero and starts the timer.

Definition at line 159 of file timer.cc.

◆ Timer() [2/2]

Timer::Timer ( const MPI_Comm  mpi_communicator,
const bool  sync_lap_times = false 
)

Constructor. Sets the accumulated times to zero and starts the timer.

This constructor specifies that CPU times should be summed over the given mpi_communicator. If sync_lap_times is true then the Timer will set the elapsed wall and CPU times over the last lap to their maximum values across the provided communicator. This synchronization is only performed if Timer::stop() is called before the timer is queried for time duration values.

Note
The timer is stopped before the synchronization over the communicator occurs; the extra cost of the synchronization is not measured.

Definition at line 165 of file timer.cc.

Member Function Documentation

◆ get_last_lap_wall_time_data()

const Utilities::MPI::MinMaxAvg & Timer::get_last_lap_wall_time_data ( ) const
inline

Return a reference to the data structure containing basic statistics on the last lap's wall time measured across all MPI processes in the given communicator. This structure does not contain meaningful values until Timer::stop() has been called.

Definition at line 962 of file timer.h.

◆ get_accumulated_wall_time_data()

const Utilities::MPI::MinMaxAvg & Timer::get_accumulated_wall_time_data ( ) const
inline

Return a reference to the data structure containing basic statistics on the accumulated wall time measured across all MPI processes in the given communicator. This structure does not contain meaningful values until Timer::stop() has been called.

Definition at line 973 of file timer.h.

◆ print_last_lap_wall_time_data()

template<typename StreamType >
void Timer::print_last_lap_wall_time_data ( StreamType &  stream) const
inline

Print the data returned by Timer::get_last_lap_wall_time_data() to the given stream.

Definition at line 985 of file timer.h.

◆ print_accumulated_wall_time_data()

template<typename StreamType >
void Timer::print_accumulated_wall_time_data ( StreamType &  stream) const
inline

Print the data returned by Timer::get_accumulated_wall_time_data() to the given stream.

Definition at line 997 of file timer.h.

◆ start()

void Timer::start ( )

Begin measuring a new lap. If sync_lap_times is true then an MPI barrier is used to ensure that all processes begin the lap at the same wall time. If the timer is already running, the start time of the current lap is reset to the current time.

Definition at line 178 of file timer.cc.

◆ stop()

void Timer::stop ( )

Stop the timer. This updates the lap times and accumulated times. If sync_lap_times is true then the lap times are synchronized over all processors in the communicator (i.e., the lap times are set to the maximum lap time).

Definition at line 212 of file timer.cc.

◆ reset()

void Timer::reset ( )

Stop the timer, if it is running. Reset all measured values to their default states.

Definition at line 360 of file timer.cc.

◆ restart()

void Timer::restart ( )
inline

Equivalent to calling Timer::reset() followed by calling Timer::start().

Definition at line 953 of file timer.h.

◆ is_running()

bool Timer::is_running ( ) const

Returns true if the timer is currently running, false otherwise.

Definition at line 233 of file timer.cc.

◆ wall_time()

double Timer::wall_time ( ) const

Return the current accumulated wall time (including the current lap, if the timer is running) in seconds without stopping the timer.

If the timer is running, and an MPI communicator was provided to the constructor of this class and the lap times are synchronized, the portion of the wall time up to the end of the last lap is synchronized between processors. However, the currently running lap is not synchronized and can therefore vary between processors. This avoids introducing unnecessary synchronization in this function.

Definition at line 274 of file timer.cc.

◆ last_wall_time()

double Timer::last_wall_time ( ) const

Return the wall time of the last lap in seconds. The timer is not stopped by this function.

If an MPI communicator was provided to the constructor and sync_lap_times is true, then the returned lap time is synchronized over all processors in the communicator (i.e., the lap time was set to the maximum wall time of all processors).

Definition at line 296 of file timer.cc.

◆ cpu_time()

double Timer::cpu_time ( ) const

Return the accumulated CPU time (including the current lap, if the timer is running) in seconds without stopping the timer.

If an MPI communicator was provided to the constructor then the returned value is the sum of all accumulated CPU times over all processors in the communicator.

Definition at line 241 of file timer.cc.

◆ last_cpu_time()

double Timer::last_cpu_time ( ) const

Return the CPU time of the last lap in seconds. The timer is not stopped by this function.

If an MPI communicator was provided to the constructor and sync_lap_times is true, then the returned CPU time is synchronized across all processors (i.e., the lap time was set to the maximum CPU time of all processors).

Note, that unlike cpu_time() the result is not summed across processors.

Definition at line 263 of file timer.cc.

◆ n_laps()

unsigned int Timer::n_laps ( ) const

Return the number of laps that have been timed by calling start() since creation of the timer or the last call to reset(). If a timer is currently running the current lap is included in the count.

Definition at line 374 of file timer.cc.

◆ synchronize_and_update()

void Timer::synchronize_and_update ( ) const
private

After a lap has been stopped it is necessary to update the accumulated wall time and CPU time with the results of the last lap. However, if sync_lap_times is set to true this requires MPI communication in parallel models before the update. We delay this communication and the update until the results are needed instead of immediately updating after calling stop(). This is useful because it avoids MPI communication if the timer is stopped because of stack unwinding during exception handling. In serial models this function simply updates the accumulated wall time and CPU time with the last lap time.

Definition at line 307 of file timer.cc.

Member Data Documentation

◆ wall_times

ClockMeasurements<wall_clock_type> Timer::wall_times
mutableprivate

Collection of wall time measurements. Marked as mutable for the reasons outlined in the documentation of Timer::is_synchronized.

Definition at line 359 of file timer.h.

◆ cpu_times

ClockMeasurements<cpu_clock_type> Timer::cpu_times
mutableprivate

Collection of CPU time measurements. Marked as mutable for the reasons outlined in the documentation of Timer::is_synchronized.

Definition at line 365 of file timer.h.

◆ running

bool Timer::running
private

Whether or not the timer is presently running.

Definition at line 370 of file timer.h.

◆ is_synchronized

bool Timer::is_synchronized
mutableprivate

Store whether the timer results are currently synchronized across MPI processes. Marked as mutable, as synchronization is delayed until the results are needed for the reasons discussed in the documentation of synchronize_and_update(). This means output functions marked as const like cpu_time() will still update this variable if necessary.

Definition at line 396 of file timer.h.

◆ mpi_communicator

MPI_Comm Timer::mpi_communicator
private

The communicator over which various time values are synchronized and combined: see the documentation of the relevant constructor for additional information.

Definition at line 403 of file timer.h.

◆ sync_lap_times

bool Timer::sync_lap_times
private

Store whether or not the wall time and CPU time will be synchronized across the communicator in synchronize_and_update().

Definition at line 409 of file timer.h.

◆ last_lap_wall_time_data

Utilities::MPI::MinMaxAvg Timer::last_lap_wall_time_data
mutableprivate

A structure for parallel wall time measurement that includes the minimum, maximum, and average over all processors known to the MPI communicator of the last lap time. Marked as mutable for the reasons outlined in the documentation of Timer::is_synchronized.

Definition at line 417 of file timer.h.

◆ accumulated_wall_time_data

Utilities::MPI::MinMaxAvg Timer::accumulated_wall_time_data
mutableprivate

A structure for parallel wall time measurement that includes the minimum time recorded among all processes, the maximum time as well as the average time defined as the sum of all individual times divided by the number of MPI processes in the MPI_Comm for the total run time. Marked as mutable for the reasons outlined in the documentation of Timer::is_synchronized.

Definition at line 427 of file timer.h.

◆ n_timed_laps

unsigned int Timer::n_timed_laps
private

The number of laps that have been timed. If the timer is currently running the current lap is included in the count.

Definition at line 434 of file timer.h.

◆ mutex

Threads::Mutex Timer::mutex
mutableprivate

A lock that makes sure that this class gives reasonable results even when used with several threads. Note that thread-safety with a timer that is shared between different threads is hard to establish, and it is in particular discouraged to have multiple threads starting and stopping the timer. This case only works if each thread makes sure the timer is not already running before starting it, and stopping the timer before any other thread can use it.

Definition at line 445 of file timer.h.


The documentation for this class was generated from the following files: