KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
kamping::measurements::Timer< CommunicatorType > Class Template Reference

Distributed timer object. More...

#include <timer.hpp>

Public Types

using Duration = double
 

Public Member Functions

 Timer ()
 Constructs a timer using the MPI_COMM_WORLD communicator.
 
 Timer (CommunicatorType const &comm)
 Constructs a timer using a given communicator.
 
void synchronize_and_start (std::string const &key)
 Synchronizes all ranks in the underlying communicator via a barrier and then start the measurement with the given key.
 
void start (std::string const &key)
 Starts the measurement with the given key.
 
void stop (std::vector< GlobalAggregationMode > const &global_aggregation_modes=std::vector< GlobalAggregationMode >{})
 Stops the currently active measurement and stores the result.
 
void stop_and_add (std::vector< GlobalAggregationMode > const &global_aggregation_modes=std::vector< GlobalAggregationMode >{})
 Stops the currently active measurement and stores the result. If the key associated with the measurement that is stopped has already been used at the current hierarchy level the duration is added to the last measured duration at this hierarchy level with the same key.
 
void stop_and_append (std::vector< GlobalAggregationMode > const &global_aggregation_modes=std::vector< GlobalAggregationMode >{})
 Stops the currently active measurement and stores the result. If the key associated with the measurement that is stopped has already been used at the current hierarchy level the duration is appended to a list of previously measured durations at this hierarchy level with the same key.
 
auto aggregate ()
 Evaluates the time measurements represented by the timer tree for which the current node is the root node globally. The measured durations are aggregated over all participating ranks and the result is stored at the root rank of the given communicator. The used aggregation operations can be specified via TimerTreeNode::data_aggregation_operations(). The durations are aggregated node by node.
 
void clear ()
 Clears all stored measurements.
 
void enable ()
 (Re-)Enable start/stop operations.
 
void disable ()
 Disable start/stop operations, i.e., start()/stop() operations do not have any effect.
 
template<typename Printer >
void aggregate_and_print (Printer &&printer)
 Aggregates and outputs the executed measurements. The output is done via the print() method of a given Printer object.
 

Detailed Description

template<typename CommunicatorType = Communicator<>>
class kamping::measurements::Timer< CommunicatorType >

Distributed timer object.

Timer hierarchy:

The timer is able to execute time measurements in a hierarchical manner. Time measurements are executed with a matching pair of calls to Timer::start() and Timer::stop(). Each call to start() enters a new level in the hierarchy which is left again with the corresponding call to stop(). Time measurements can be nested and the parent time measurement remains active while its child time measurement take place.

See the following pseudocode example:

timer.start("algorithm");
timer.start("preprocessing");
timer.stop(); // stops "preprocessing" measurement
timer.start("core_algorithm");
timer.start("subroutine");
timer.stop(); // stops "subroutine" measurement
timer.stop(); // stops "core_algorithm" measurement
timer.start("postprocessing");
timer.stop(); // stops "postprocessing" measurement
timer.stop(); // stops "algorithm" measurement
timer.aggregate_and_print(Printer{}); // aggregates measurements across all participating ranks and print results
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
Distributed timer object.
Definition timer.hpp:122
Timer< Communicator<> > & timer()
Gets a reference to a kamping::measurements::BasicTimer.
Definition timer.hpp:280

This corresponds to the following timing hierarchy:

// Measurement key Duration
// ----------------------------------
// algorithm:...............6.0 sec
// |-- preprocessing:.......1.0 sec
// |-- core_algorithm:......4.0 sec
// | `-- subroutine:......2.0 sec
// `-- postprocessing:......2.0 sec

Aggregation operations

There are two types of aggregation operations:

1) Local aggregation operations - It is possible to execute measurements with the same key multiple times. Local aggregation operations specify how these repeated time measurements will be stored. Currently, there are two options - stop_and_add() and stop_and_append(). See the following examples:

timer.start("foo");
timer.stop_and_add();
timer.start("foo");
timer.stop_and_add();

The result of this program is one stored duration for the key "foo" which is the sum of the durations for the two measurements with key "foo".

The other option to handle repeated measurements with the same key is stop_and_append():

timer.start("bar");
timer.stop_and_append();
timer.start("bar");
timer.stop_and_append();

This program results in a list with two durations for the key "bar". We call the number of durations stored for a key its dimension.

2) Global (communicator-wide) aggregation operations specify how the stored duration(s) for a specific measurement key shall be aggregated. The default operation (if no operations are specified via the stop() method) is to take the maximum duration over all ranks in the communicator. If there are multiple durations for a certain key, the aggregation operation is applied element-wise and the result remains a list with the same size as the number of input durations. The communicator-wide aggregation operation are applied in the evaluation phase started with calls to aggregate() or aggregate_and_print().

The timer hierarchy that is implicitly created by start() and stop() must be the same on all ranks in the given communicator. The number of time measurements with a specific key may vary as long as the number of stored duration are the same: Consider for example a communicator of size two and rank 0 and 1 both measure the time of a function foo() each time it is called using the key "computation". This is only valid if either each rank calls foo() exactly the same number of times and local aggregation happens using the mode append, or if the measured time is locally aggregated using add, i.e. the value(s) stored for a single key at the same hierarchy level must have matching dimensions. Furthermore, global communicator-wide duration aggregation operations specified on ranks other than the root rank are ignored.

See also
The usage example for Timer provides some more information on how the class can be used.
Template Parameters
CommunicatorTypeCommunicator in which the time measurements are executed.

Member Typedef Documentation

◆ Duration

Type of durations.

Constructor & Destructor Documentation

◆ Timer()

template<typename CommunicatorType = Communicator<>>
kamping::measurements::Timer< CommunicatorType >::Timer ( CommunicatorType const & comm)
inline

Constructs a timer using a given communicator.

Parameters
commCommunicator in which the time measurements are executed.

Member Function Documentation

◆ aggregate()

template<typename CommunicatorType = Communicator<>>
auto kamping::measurements::Timer< CommunicatorType >::aggregate ( )
inline

Evaluates the time measurements represented by the timer tree for which the current node is the root node globally. The measured durations are aggregated over all participating ranks and the result is stored at the root rank of the given communicator. The used aggregation operations can be specified via TimerTreeNode::data_aggregation_operations(). The durations are aggregated node by node.

Returns
AggregatedTree object which encapsulated the aggregated data in a tree structure representing the measurements.

◆ aggregate_and_print()

template<typename CommunicatorType = Communicator<>>
template<typename Printer >
void kamping::measurements::Timer< CommunicatorType >::aggregate_and_print ( Printer && printer)
inline

Aggregates and outputs the executed measurements. The output is done via the print() method of a given Printer object.

The print() method must accept an object of type AggregatedTreeNode and receives the root of the evaluated timer tree as parameter. The print() method is only called on the root rank of the communicator. See AggregatedTreeNode for the accessible data. The AggregatedTreeNode::children() member function can be used to navigate the nested measurement structure.

Template Parameters
PrinterType of printer which is used to output the aggregated timing data. Printer must possess a member print() which accepts a AggregatedTreeNode as parameter.
Parameters
printerPrinter object used to output the aggregated timing data.

◆ start()

template<typename CommunicatorType = Communicator<>>
void kamping::measurements::Timer< CommunicatorType >::start ( std::string const & key)
inline

Starts the measurement with the given key.

Parameters
keyKey with which the started time measurement is associated. Note that the user is responsible for providing keys, which are valid in the used output format during printing.

◆ stop()

template<typename CommunicatorType = Communicator<>>
void kamping::measurements::Timer< CommunicatorType >::stop ( std::vector< GlobalAggregationMode > const & global_aggregation_modes = std::vector<GlobalAggregationMode>{})
inline

Stops the currently active measurement and stores the result.

Parameters
global_aggregation_modesSpecifies how the measurement duration is aggregated over all participating ranks when aggregate() is called.

◆ stop_and_add()

template<typename CommunicatorType = Communicator<>>
void kamping::measurements::Timer< CommunicatorType >::stop_and_add ( std::vector< GlobalAggregationMode > const & global_aggregation_modes = std::vector<GlobalAggregationMode>{})
inline

Stops the currently active measurement and stores the result. If the key associated with the measurement that is stopped has already been used at the current hierarchy level the duration is added to the last measured duration at this hierarchy level with the same key.

Parameters
global_aggregation_modesSpecifies how the measurement duration is aggregated over all participating ranks when aggregate() is called.

◆ stop_and_append()

template<typename CommunicatorType = Communicator<>>
void kamping::measurements::Timer< CommunicatorType >::stop_and_append ( std::vector< GlobalAggregationMode > const & global_aggregation_modes = std::vector<GlobalAggregationMode>{})
inline

Stops the currently active measurement and stores the result. If the key associated with the measurement that is stopped has already been used at the current hierarchy level the duration is appended to a list of previously measured durations at this hierarchy level with the same key.

Parameters
global_aggregation_modesSpecifies how the measurement duration is aggregated over all participating ranks when aggregate() is called.

◆ synchronize_and_start()

template<typename CommunicatorType = Communicator<>>
void kamping::measurements::Timer< CommunicatorType >::synchronize_and_start ( std::string const & key)
inline

Synchronizes all ranks in the underlying communicator via a barrier and then start the measurement with the given key.

Parameters
keyKey with which the started time measurement is associated. Note that the user is responsible for providing keys, which are valid in the used output format during printing.

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