mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time > Class Template Reference

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
class mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >

A class to track the number of iterations in an OMP parallelized loop.

Uses omp critical directives to deconflict updates by different loops. Example:

ompLoopWatcher<> watcher(1000, std::cout); //Uses defaults
#pragma omp parallel for
int( i=0; i<1000; ++i)
{
watcher.incrementAndOutputStatus();
//Do loop work
...
}

This will result in the following output

 1 / 1000 (0.1%)
 2 / 1000 (0.2%)
 3 / 1000 (0.3%)

and so on.

Note
This can reduce performance due to critical points it creates if used in fast loops (i.e. do not use inside the inner most loop!).

The behavior of the output is controlled through template parameters. A different output-type can be specified, which needs to accept size_t, and optionally float, and character input using the << operator.

Template Parameters
outputTa type which accepts size_t, float, and character input via the << operator (default is std::ostream).
_printPrettyflag to control whether the output is nicely formatted, if false then just the numbers are sent to the output with no spaces or delimiters (default is true).
_printLoopsflag to control whether the number of loops is sent to the output each time (default is true).
_printPercentflag to control whether the percentage complete is calculated (default is true).
_printNLineflag to control whether the newline '\n' is sent ot output (default is false). If false, '\r' is written at end of output.
_timeflag to control whether time is tracked. Default is true.

Definition at line 85 of file ompLoopWatcher.hpp.

#include <ipc/ompLoopWatcher.hpp>

Public Member Functions

 ompLoopWatcher (size_t nLoops, outputT &output)
 Constructor.
void increment ()
 Increment the counter.
void advance (size_t diff_count)
 Advance the counter by a number of steps.
void outputStatus ()
 Output current status.
void outputFinalStatus ()
 Output a final report about elapsed time.
void incrementAndOutputStatus ()
 Increment and output status.
void advanceAndOutputStatus (size_t diff_count)
 Advance and output status.
void clearOutput ()
 Clear the output line of any text.

Protected Member Functions

void _increment ()
 Increment the counter.
void _advance (size_t diff_count)
 Advance the counter by a number of steps.
void _outputStatus ()
 Perform the output.

Protected Attributes

size_t _nLoops
 The total number of loops.
size_t _counter
 The current counter.
outputT * _output
 Pointer to the instance of type outputT.

Constructor & Destructor Documentation

◆ ompLoopWatcher()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::ompLoopWatcher ( size_t nLoops,
outputT & output )
inline

Constructor.

Registers the output and sets the number of loops.

Parameters
nLoopsis the total number of loops.
outputis the instance of type outputT to which the output will be sent.

Definition at line 199 of file ompLoopWatcher.hpp.

References _counter, _nLoops, _output, and mx::sys::get_curr_time().

Member Function Documentation

◆ _advance()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::_advance ( size_t diff_count)
inlineprotected

Advance the counter by a number of steps.

Definition at line 113 of file ompLoopWatcher.hpp.

References _counter, and mx::sys::get_curr_time().

Referenced by advance(), and advanceAndOutputStatus().

◆ _increment()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::_increment ( )
inlineprotected

Increment the counter.

Definition at line 103 of file ompLoopWatcher.hpp.

References _counter, and mx::sys::get_curr_time().

Referenced by increment(), and incrementAndOutputStatus().

◆ _outputStatus()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::_outputStatus ( )
inlineprotected

Perform the output.

Definition at line 123 of file ompLoopWatcher.hpp.

References _counter, and _nLoops.

Referenced by advanceAndOutputStatus(), incrementAndOutputStatus(), and outputStatus().

◆ advance()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::advance ( size_t diff_count)
inline

Advance the counter by a number of steps.

Call this once per loop. It contains an omp critical directive.

Definition at line 221 of file ompLoopWatcher.hpp.

References _advance().

Referenced by unitTest::ipc_ompLoopWatcher_test::TEST_CASE().

◆ advanceAndOutputStatus()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::advanceAndOutputStatus ( size_t diff_count)
inline

Advance and output status.

Call this to advance and then give a status update. Has only one omp critical directive for the two steps.

Definition at line 286 of file ompLoopWatcher.hpp.

References _advance(), and _outputStatus().

Referenced by unitTest::ipc_ompLoopWatcher_test::TEST_CASE().

◆ clearOutput()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::clearOutput ( )
inline

Clear the output line of any text.

If printing pretty and not printing new lines, this writes a line of blank spaces and carriage returns to clear out the last status update.

Definition at line 299 of file ompLoopWatcher.hpp.

Referenced by unitTest::ipc_ompLoopWatcher_test::TEST_CASE(), and unitTest::ipc_ompLoopWatcher_test::TEST_CASE().

◆ increment()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::increment ( )
inline

Increment the counter.

Call this once per loop. It contains an omp critical directive.

Definition at line 212 of file ompLoopWatcher.hpp.

References _increment().

Referenced by unitTest::ipc_ompLoopWatcher_test::TEST_CASE().

◆ incrementAndOutputStatus()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::incrementAndOutputStatus ( )
inline

◆ outputFinalStatus()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::outputFinalStatus ( )
inline

Output a final report about elapsed time.

Definition at line 237 of file ompLoopWatcher.hpp.

References _counter.

Referenced by unitTest::ipc_ompLoopWatcher_test::TEST_CASE(), and unitTest::ipc_ompLoopWatcher_test::TEST_CASE().

◆ outputStatus()

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
void mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::outputStatus ( )
inline

Output current status.

Call this whenever you want a status update. It contains an omp critical directive.

Definition at line 230 of file ompLoopWatcher.hpp.

References _outputStatus().

Referenced by unitTest::ipc_ompLoopWatcher_test::TEST_CASE().

Member Data Documentation

◆ _counter

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
size_t mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::_counter
protected

The current counter.

Definition at line 92 of file ompLoopWatcher.hpp.

Referenced by ompLoopWatcher(), _advance(), _increment(), _outputStatus(), and outputFinalStatus().

◆ _nLoops

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
size_t mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::_nLoops
protected

The total number of loops.

Definition at line 91 of file ompLoopWatcher.hpp.

Referenced by ompLoopWatcher(), and _outputStatus().

◆ _output

template<class _outputT = std::ostream, bool _printPretty = true, bool _printLoops = true, bool _printPercent = true, bool _printNLine = false, bool _time = true>
outputT* mx::ipc::ompLoopWatcher< _outputT, _printPretty, _printLoops, _printPercent, _printNLine, _time >::_output
protected

Pointer to the instance of type outputT.

Definition at line 97 of file ompLoopWatcher.hpp.

Referenced by ompLoopWatcher().


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