Mantid
Loading...
Searching...
No Matches
ProgressBase.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include <algorithm>
10#include <sstream>
11#include <stdexcept>
12
13namespace Mantid::Kernel {
14
15//----------------------------------------------------------------------------------------------
19 : m_start(0), m_end(1.0), m_ifirst(0), m_numSteps(1), m_notifyStep(1), m_notifyStepPct(1), m_step(1), m_i(0),
20 m_last_reported(-1), m_timeElapsed(std::make_unique<Timer>()), m_notifyStepPrecision(0) {
21 m_timeElapsed->reset();
22}
23
24//----------------------------------------------------------------------------------------------
31ProgressBase::ProgressBase(double start, double end, int64_t numSteps)
32 : m_start(start), m_end(end), m_ifirst(0), m_numSteps(numSteps), m_notifyStep(1), m_notifyStepPct(1), m_step(1),
33 m_i(0), m_last_reported(-1), m_timeElapsed(std::make_unique<Timer>()), m_notifyStepPrecision(0) {
34 if (start < 0. || start >= end) {
35 std::stringstream msg;
36 msg << "Progress range invalid 0 <= start=" << start << " <= end=" << end;
37 throw std::invalid_argument(msg.str());
38 }
39 this->setNumSteps(numSteps);
41 m_timeElapsed->reset();
42}
43//----------------------------------------------------------------------------------------------
51 : m_timeElapsed(std::make_unique<Timer>()) // new object, new timer
52{
53 *this = source;
54}
55
57 if (this != &rhs) {
58 m_start = rhs.m_start;
59 m_end = rhs.m_end;
60 m_ifirst = rhs.m_ifirst;
61 m_numSteps = rhs.m_numSteps;
62 m_notifyStep = rhs.m_notifyStep;
63 m_notifyStepPct = rhs.m_notifyStepPct;
64 m_step = rhs.m_step;
65 m_i.store(rhs.m_i.load());
66 m_last_reported.store(rhs.m_last_reported.load());
67 // copy the timer state, being careful only to copy state & not the actual
68 // pointer
69 *m_timeElapsed = *rhs.m_timeElapsed;
70 m_notifyStepPrecision = rhs.m_notifyStepPrecision;
71 }
72 return *this;
73}
74
75//----------------------------------------------------------------------------------------------
79
80//----------------------------------------------------------------------------------------------
86void ProgressBase::report(const std::string &msg) {
88 return;
89 m_last_reported.store(m_i.load());
90 this->doReport(msg);
91}
92
93//----------------------------------------------------------------------------------------------
100void ProgressBase::report(int64_t i, const std::string &msg) {
101 // Set the loop coutner to the spot specified.
102 m_i = i;
104 return;
105 m_last_reported.store(m_i.load());
106 this->doReport(msg);
107}
108
109//----------------------------------------------------------------------------------------------
116void ProgressBase::reportIncrement(int inc, const std::string &msg) {
117 // Increment the loop counter
118 m_i += int64_t(inc);
120 return;
121 m_last_reported.store(m_i.load());
122 this->doReport(msg);
123}
124
125//----------------------------------------------------------------------------------------------
132void ProgressBase::reportIncrement(size_t inc, const std::string &msg) {
133 m_i += static_cast<int64_t>(inc);
135 return;
136 m_last_reported.store(m_i.load());
137 this->doReport(msg);
138}
139
140//----------------------------------------------------------------------------------------------
145void ProgressBase::setNumSteps(int64_t nsteps) {
146 m_numSteps = std::max(nsteps, int64_t{1}); // Minimum of 1
147
148 auto numSteps = static_cast<double>(m_numSteps);
149 m_step = (m_end - m_start) / numSteps;
150
151 m_notifyStep = static_cast<int64_t>(numSteps * m_notifyStepPct * 0.01 / (m_end - m_start));
152 m_notifyStep = std::max(m_notifyStep, int64_t{1}); // Minimum of 1
153}
154
155//----------------------------------------------------------------------------------------------
162void ProgressBase::resetNumSteps(int64_t nsteps, double start, double end) {
163 if (start < 0. || start >= end) {
164 std::stringstream msg;
165 msg << "Progress range invalid 0 <= start=" << start << " <= end=" << end;
166 throw std::invalid_argument(msg.str());
167 }
168 m_start = start;
169 m_end = end;
170 m_i = 0;
171 m_last_reported = 0;
172 m_timeElapsed->reset();
173 setNumSteps(nsteps);
174}
175
176//----------------------------------------------------------------------------------------------
184void ProgressBase::setNotifyStep(double notifyStepPct) {
185 m_notifyStepPct = notifyStepPct;
186 m_notifyStep = (static_cast<int64_t>(double(m_numSteps) * m_notifyStepPct / 100 / (m_end - m_start)));
187 if (m_notifyStep < 0)
188 m_notifyStep = 1;
190 if (m_notifyStepPct < 1.0)
192 if (m_notifyStepPct < 0.09)
194}
195
196//----------------------------------------------------------------------------------------------
201 auto elapsed = double(m_timeElapsed->elapsed_no_reset());
202 double prog = double(m_i) * m_step;
203 if (prog <= 1e-4)
204 return 0.0; // unknown
205 else {
206 double total = elapsed / prog;
207 return total - elapsed;
208 }
209}
210
211} // namespace Mantid::Kernel
const std::vector< double > & rhs
double m_end
Ending progress.
Definition: ProgressBase.h:73
ProgressBase & operator=(const ProgressBase &rhs)
int64_t m_numSteps
Loop counter upper bound.
Definition: ProgressBase.h:77
int64_t m_ifirst
Loop counter initial value.
Definition: ProgressBase.h:75
void resetNumSteps(int64_t nsteps, double start, double end)
Change the number of steps between start/end.
virtual void doReport(const std::string &msg="")=0
Pure virtual method that does the progress reporting, to be overridden.
std::unique_ptr< Kernel::Timer > m_timeElapsed
Timer that is started when the progress bar is constructed.
Definition: ProgressBase.h:90
double m_step
Progress increment at each loop.
Definition: ProgressBase.h:84
void reportIncrement(int inc, const std::string &msg="")
Sends the progress notification and increment the loop counter by more than one.
double m_notifyStepPct
Frequency of sending the notification (as a min percentage step, e.g.
Definition: ProgressBase.h:82
int m_notifyStepPrecision
Digits of precision in the reporting.
Definition: ProgressBase.h:92
double getEstimatedTime() const
Returns the estimated number of seconds until the algorithm completes.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
void setNumSteps(int64_t nsteps)
Change the number of steps between start/end.
double m_start
Starting progress.
Definition: ProgressBase.h:71
int64_t m_notifyStep
Frequency of sending the notification (every m_step times)
Definition: ProgressBase.h:79
std::atomic< int64_t > m_i
Loop counter.
Definition: ProgressBase.h:86
virtual ~ProgressBase()
Destructor.
ProgressBase()
Default constructor.
std::atomic< int64_t > m_last_reported
Last loop counter value the was a peport.
Definition: ProgressBase.h:88
void setNotifyStep(double notifyStepPct)
Override the frequency at which notifications are sent out.
A simple class that provides a wall-clock (not processor time) timer.
Definition: Timer.h:27
STL namespace.