|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// This library contains utilities for reporting the progress of the execution |
| 4 | +// of an algorithm. |
| 5 | + |
| 6 | +#include <utility> |
| 7 | + |
| 8 | +#include "absl/functional/any_invocable.h" |
| 9 | +#include "absl/status/status.h" |
| 10 | + |
| 11 | +namespace gbbs { |
| 12 | + |
| 13 | +// Callable object to be used for reporting the progress of the execution of an |
| 14 | +// algorithm, as a number in the closed interval [0.0, 1.0]. Algorithms must |
| 15 | +// report non-decreasing values in this range; values closer to 1.0 indicate |
| 16 | +// that the algorithm is closer to completion. |
| 17 | +// |
| 18 | +// Note: beyond the non-decreasing requirement, there's no requirement on the |
| 19 | +// concrete meaning of the reported numbers (in particular, the progress numbers |
| 20 | +// reported need not be proportional to time elapsed since the start of the |
| 21 | +// algorithm). There's also no requirement on how many times, or how often, the |
| 22 | +// object is called. |
| 23 | +// |
| 24 | +// Typically, an algorithm will make at least two calls; the first one reporting |
| 25 | +// a value of 0.0 (at the very beginning of the execution of the algorithm), and |
| 26 | +// the final one reporting a value of 1.0 (at the very end of the execution of |
| 27 | +// the algorithm). |
| 28 | +using ReportProgressT = absl::AnyInvocable<void(float progress)>; |
| 29 | + |
| 30 | +// Wrapper for a `ReportProgress` instance that's useful for algorithms with the |
| 31 | +// following structure: |
| 32 | +// |
| 33 | +// - Preprocessing (optional) |
| 34 | +// - One or more iterations |
| 35 | +// - Postprocessing (optional) |
| 36 | +// |
| 37 | +// This class provides a convenient way to report the progress of the algorithm |
| 38 | +// execution. It makes the simplifying assumption that the preprocessing step |
| 39 | +// (if any), postprocessing step (if any), and each iteration all represent |
| 40 | +// equal amounts of progress. |
| 41 | +class IterationProgressReporter { |
| 42 | + public: |
| 43 | + // Creates a new `IterationProgressReporter` instance. |
| 44 | + // |
| 45 | + // Arguments: |
| 46 | + // * `report_progress`: the instance to be used for reporting the progress of |
| 47 | + // the algorithm execution. |
| 48 | + // * `num_iterations`: the number of iterations (must be strictly positive). |
| 49 | + // * `has_preprocessing`: indicates whether the algorithm has a preprocessing |
| 50 | + // step. |
| 51 | + // * `has_postprocessing`: indicates whether the algorithm has a |
| 52 | + // postprocessing step. |
| 53 | + IterationProgressReporter(ReportProgressT report_progress, int num_iterations, |
| 54 | + bool has_preprocessing = false, |
| 55 | + bool has_postprocessing = false) |
| 56 | + : report_progress_(std::move(report_progress)), |
| 57 | + num_iterations_(num_iterations), |
| 58 | + has_preprocessing_(has_preprocessing), |
| 59 | + has_postprocessing_(has_postprocessing), |
| 60 | + total_steps_(num_iterations + (has_preprocessing ? 1 : 0) + |
| 61 | + (has_postprocessing ? 1 : 0)) {} |
| 62 | + |
| 63 | + // Indicate that the preprocessing step is complete. Returns an error status |
| 64 | + // if `has_preprocessing_` is false or if any call of `IterationComplete()` |
| 65 | + // has been made. |
| 66 | + absl::Status PreprocessingComplete(); |
| 67 | + |
| 68 | + // Indicates that the `iteration`-th iteration is complete. The `iteration` |
| 69 | + // value must be in the half-open interval [0, `num_iterations_`) (i.e., |
| 70 | + // iteration indices are 0-based). At least one call of |
| 71 | + // this method (with argument 0) must be made. |
| 72 | + // |
| 73 | + // Returns an error status if `num_iterations` is non-positive, if |
| 74 | + // preprocessing was indicated but `PreprocessingComplete()` has not been |
| 75 | + // called, or if successive calls of this method do not pass monotonically |
| 76 | + // ascending `iteration` values of 1, 2, ..., `num_iterations`. |
| 77 | + absl::Status IterationComplete(int iteration); |
| 78 | + |
| 79 | + // Indicates that all iterations are complete. This is equivalent to calling |
| 80 | + // `IterationComplete(num_iterations)`, but can be used in cases where the |
| 81 | + // algorithm detects that it has converged early and not all expected |
| 82 | + // iterations are required |
| 83 | + // |
| 84 | + // Returns an error status if `num_iterations` is non-positive or if |
| 85 | + // preprocessing was indicated but `PreprocessingComplete()` has not been |
| 86 | + // called. |
| 87 | + absl::Status IterationsDoneEarly(); |
| 88 | + |
| 89 | + // Indicate that the postprocessing step is complete. Returns an error status |
| 90 | + // if `has_postprocessing_` is false or if no call of `IterationComplete()` |
| 91 | + // has been made. |
| 92 | + absl::Status PostprocessingComplete(); |
| 93 | + |
| 94 | + private: |
| 95 | + /*const*/ ReportProgressT report_progress_; |
| 96 | + const int num_iterations_; |
| 97 | + const bool has_preprocessing_; |
| 98 | + const bool has_postprocessing_; |
| 99 | + const int total_steps_; |
| 100 | + int num_steps_completed_ = 0; |
| 101 | + |
| 102 | + // Invokes the `report_progress_` callback so as to reflect |
| 103 | + // `num_steps_completed_` worth of progress out of `total_steps_`. |
| 104 | + void ReportProgress(); |
| 105 | +}; |
| 106 | + |
| 107 | +} // namespace gbbs |
0 commit comments