ampsci
High-precision calculations for one- and two-valence atomic systems
qip::ProgressBar< Length >

ok

template<std::size_t Length = 60>
class qip::ProgressBar< Length >

Thread-safe progress bar for OpenMP parallel loops.

Displays a progress bar with percentage. The progress counter uses std::atomic for thread-safe updates. Each call to update() increments the counter and prints the bar. The output is serialised via critical section to prevent garbled output from simultaneous writes.

Warning
This adds overhead. Prefer not to use if each OMP task is extremely small, since overhead may become noticable. If each task is large, overhead negligable.
  • If print set to false on construction, does nothing (does not print, does not track progress). Just a simple way of run-time turning off.
Note
When stdout is not a TTY (i.e., piped), prints comma-separated percentages instead of a bar: "0%, 10%, 20%, ... 100%\n". Prints approximately Length / 5 values, spaced evenly.

The Length template parameter controls output width. For TTY mode: total bar width in characters. For non-TTY mode: determines how many percentage values are printed (~Length/5).

Note
If the loop exits before the final iteration (i.e., early break;), then final the newline \n will not be printed. Output may be messy. Cannot break like this in OpenMP loop anyway.

For non-parallel loops, the simpler qip::progbar() function should work fine.

Typical usage in a parallel loop:

qip::ProgressBar bar(n_iterations);
#pragma omp parallel for schedule(dynamic)
for (std::size_t i = 0; i < n_iterations; ++i) {
// ... work ...
bar.update();
}
Thread-safe progress bar for OpenMP parallel loops.
Definition Widgets.hpp:85
  • Put .update(); after work, to avoid early "100% done" reporting
Template Parameters
LengthTotal character width of the output (default 60).
Note
Thread-safe; safe to call from multiple threads simultaneously. But may add overhead.

#include <Widgets.hpp>

Public Member Functions

 ProgressBar (int max, bool print)
 Construct progress bar for max iterations.
 
void update ()
 Atomically increment progress counter and print updated bar.
 

Constructor & Destructor Documentation

◆ ProgressBar()

template<std::size_t Length = 60>
qip::ProgressBar< Length >::ProgressBar ( int  max,
bool  print 
)
inline

Construct progress bar for max iterations.

Parameters
maxTotal number of iterations (denominator for percentage).
printRuntime switch; if false, class does nothing.

Member Function Documentation

◆ update()

template<std::size_t Length = 60>
void qip::ProgressBar< Length >::update ( )
inline

Atomically increment progress counter and print updated bar.


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