TimerRegistrar Class Reference

CPP API: mio::timing::TimerRegistrar Class Reference

TimerRegistrar is a singleton to keep track of and print timers. Does not manage storage. More...

#include <timer_registrar.h>

Public Member Functions

void add_timer (TimerRegistration &&registration)
 Add a new timer to the TimerRegistrar. More...
 
void disable_final_timer_summary ()
 Prevent the TimerRegistrar from calling print_timers on exit from main. More...
 
const auto & get_register () const
 Returns a read only list of all TimerRegistrations. Can be used to print or evaluate timers. More...
 
void print_gathered_timers (mpi::Comm comm, std::ostream &out=std::cout)
 Print all timers gathered on the given MPI Comm using a Printer. More...
 
void print_timers (std::ostream &out=std::cout) const
 Print all timers using a Printer. More...
 
void set_printer (std::unique_ptr< Printer > &&printer)
 Replace the internal printer used for print_timers. More...
 
 TimerRegistrar (TimerRegistrar &)=delete
 TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it. More...
 
 TimerRegistrar (TimerRegistrar &&)=delete
 TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it. More...
 
void operator= (TimerRegistrar &)=delete
 TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it. More...
 
void operator= (TimerRegistrar &&)=delete
 TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it. More...
 

Static Public Member Functions

static TimerRegistrarget_instance ()
 Access the TimerRegistrar. More...
 

Private Member Functions

void gather_timers (mpi::Comm comm, std::list< BasicTimer > &external_timers, std::list< TimerRegistration > &gathered_register) const
 Gather timers from all ranks, using external_timers as temporary timer storage for gathered_register. More...
 
 TimerRegistrar ()=default
 Instead of constructing a TimerRegistrar, use its static method TimerRegistrar::get_instance(). More...
 
 ~TimerRegistrar ()
 Specify a destructor to allow printing all timers after exit from main. More...
 

Private Attributes

bool m_print_on_death = true
 Whether to call m_printer during the destructor. More...
 
std::unique_ptr< Printerm_printer = std::make_unique<TablePrinter>()
 A printer to visualize all timers. More...
 
std::list< TimerRegistrationm_register
 List that allows access to timers without having their name. More...
 
std::mutex m_registration_lock
 Lock to safeguard m_register against concurrent writes. More...
 

Detailed Description

TimerRegistrar is a singleton to keep track of and print timers. Does not manage storage.

Constructor & Destructor Documentation

◆ TimerRegistrar() [1/3]

mio::timing::TimerRegistrar::TimerRegistrar ( TimerRegistrar )
delete

TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it.

◆ TimerRegistrar() [2/3]

mio::timing::TimerRegistrar::TimerRegistrar ( TimerRegistrar &&  )
delete

TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it.

◆ TimerRegistrar() [3/3]

mio::timing::TimerRegistrar::TimerRegistrar ( )
privatedefault

Instead of constructing a TimerRegistrar, use its static method TimerRegistrar::get_instance().

◆ ~TimerRegistrar()

mio::timing::TimerRegistrar::~TimerRegistrar ( )
inlineprivate

Specify a destructor to allow printing all timers after exit from main.

Member Function Documentation

◆ add_timer()

void mio::timing::TimerRegistrar::add_timer ( TimerRegistration &&  registration)
inline

Add a new timer to the TimerRegistrar.

Note that the TimerRegistrar does not manage any storage, it only keeps a list of the timers and their names. Therefore, if you manually call add_timer, you have to make sure that the registered timer lives long enough for e.g. the last print_timer call. You may want to disable_final_timer_summary, and look at NamedTimer as an example that does guarantee correct object lifetime.

Parameters
registrationA new registration with a name, a reference to a timer, and the thread id it is created on.

◆ disable_final_timer_summary()

void mio::timing::TimerRegistrar::disable_final_timer_summary ( )
inline

Prevent the TimerRegistrar from calling print_timers on exit from main.

◆ gather_timers()

void mio::timing::TimerRegistrar::gather_timers ( mpi::Comm  comm,
std::list< BasicTimer > &  external_timers,
std::list< TimerRegistration > &  gathered_register 
) const
inlineprivate

Gather timers from all ranks, using external_timers as temporary timer storage for gathered_register.

◆ get_instance()

static TimerRegistrar& mio::timing::TimerRegistrar::get_instance ( )
inlinestatic

Access the TimerRegistrar.

TimerRegistrar acts as a singleton, using this method to access the instance. This makes TimerRegistrar accessible everywhere, without having to manage a TimerRegistrar object or pass an instance to any class or function that uses timers. In particular, it allows e.g. NamedTimer to register itself automatically, while guaranteeing a correct destruction order.

Returns
A reference to the TimerRegistrar instance.

◆ get_register()

const auto& mio::timing::TimerRegistrar::get_register ( ) const
inline

Returns a read only list of all TimerRegistrations. Can be used to print or evaluate timers.

◆ operator=() [1/2]

void mio::timing::TimerRegistrar::operator= ( TimerRegistrar &&  )
delete

TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it.

◆ operator=() [2/2]

void mio::timing::TimerRegistrar::operator= ( TimerRegistrar )
delete

TimerRegistrar must not be copied or moved, use TimerRegistrar::get_instance() to access it.

◆ print_gathered_timers()

void mio::timing::TimerRegistrar::print_gathered_timers ( mpi::Comm  comm,
std::ostream &  out = std::cout 
)
inline

Print all timers gathered on the given MPI Comm using a Printer.

By default, uses TablePrinter to write to stdout. The printer can be changed using the set_printer member.

Parameters
commAn MPI communicator. Make sure this method is called by all ranks on comm!
outAny output stream, defaults to std::cout.

◆ print_timers()

void mio::timing::TimerRegistrar::print_timers ( std::ostream &  out = std::cout) const
inline

Print all timers using a Printer.

By default, uses TablePrinter to write to stdout. The printer can be changed using the set_printer member.

Parameters
outAny output stream, defaults to std::cout.

◆ set_printer()

void mio::timing::TimerRegistrar::set_printer ( std::unique_ptr< Printer > &&  printer)
inline

Replace the internal printer used for print_timers.

To allow making a final timer summary on exit from main, the TimerRegistrar has to take ownership of the printer. Usage may look as follows:

auto printer = std::make_unique<mio::timing::TablePrinter>();
void set_printer(std::unique_ptr< Printer > &&printer)
Replace the internal printer used for print_timers.
Definition: timer_registrar.h:140
static TimerRegistrar & get_instance()
Access the TimerRegistrar.
Definition: timer_registrar.h:56

Note that after passing the printer using std::move, it will contain a nullptr and can not be used again.

Parameters
printerA unique pointer to something that implements Printer.

Member Data Documentation

◆ m_print_on_death

bool mio::timing::TimerRegistrar::m_print_on_death = true
private

Whether to call m_printer during the destructor.

◆ m_printer

std::unique_ptr<Printer> mio::timing::TimerRegistrar::m_printer = std::make_unique<TablePrinter>()
private

A printer to visualize all timers.

◆ m_register

std::list<TimerRegistration> mio::timing::TimerRegistrar::m_register
private

List that allows access to timers without having their name.

◆ m_registration_lock

std::mutex mio::timing::TimerRegistrar::m_registration_lock
private

Lock to safeguard m_register against concurrent writes.