diff --git a/Base/Progress/DelayedProgressCounter.cpp b/Base/Progress/DelayedProgressCounter.cpp deleted file mode 100644 index 1a97403d2e25863f3fa6c8cfe2be5c1c4e41bbff..0000000000000000000000000000000000000000 --- a/Base/Progress/DelayedProgressCounter.cpp +++ /dev/null @@ -1,35 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit reflection and scattering -// -//! @file Base/Progress/DelayedProgressCounter.cpp -//! @brief Implements class DelayedProgressCounter. -//! -//! @homepage http://www.bornagainproject.org -//! @license GNU General Public License v3 or higher (see COPYING) -//! @copyright Forschungszentrum Jülich GmbH 2018 -//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) -// -// ************************************************************************************************ - -#include "Base/Progress/DelayedProgressCounter.h" -#include "Base/Progress/ProgressHandler.h" -#include <stdexcept> - -DelayedProgressCounter::DelayedProgressCounter(ProgressHandler* progress, size_t interval) - : m_progress(progress) - , m_interval(interval) - , m_count(0) -{ - if (!progress->alive()) - throw std::runtime_error("dead process (user interrupt?)"); -} - -void DelayedProgressCounter::stepProgress() -{ - ++m_count; - if (m_count == m_interval) { - m_progress->incrementDone(m_interval); - m_count = 0; - } -} diff --git a/Base/Progress/DelayedProgressCounter.h b/Base/Progress/DelayedProgressCounter.h deleted file mode 100644 index ec75ed41ba3be142e05a8e7b23b28240b69ea4d3..0000000000000000000000000000000000000000 --- a/Base/Progress/DelayedProgressCounter.h +++ /dev/null @@ -1,41 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit reflection and scattering -// -//! @file Base/Progress/DelayedProgressCounter.h -//! @brief Defines class DelayedProgressCounter. -//! -//! @homepage http://www.bornagainproject.org -//! @license GNU General Public License v3 or higher (see COPYING) -//! @copyright Forschungszentrum Jülich GmbH 2018 -//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS) -// -// ************************************************************************************************ - -#ifdef SWIG -#error no need to expose this header to Swig -#endif // SWIG -#ifndef BORNAGAIN_BASE_PROGRESS_DELAYEDPROGRESSCOUNTER_H -#define BORNAGAIN_BASE_PROGRESS_DELAYEDPROGRESSCOUNTER_H - -#include <cstddef> - -class ProgressHandler; - -//! Counter for reporting progress (with delay interval) in a threaded computation. - -class DelayedProgressCounter { -public: - DelayedProgressCounter(ProgressHandler* progress, size_t interval); - ~DelayedProgressCounter() = default; - - //! Increments inner counter; at regular intervals updates progress handler. - void stepProgress(); - -private: - ProgressHandler* m_progress; - const size_t m_interval; - size_t m_count; -}; - -#endif // BORNAGAIN_BASE_PROGRESS_DELAYEDPROGRESSCOUNTER_H diff --git a/Base/Progress/ProgressHandler.cpp b/Base/Progress/ProgressHandler.cpp index a8ff5a31fa8db140c2d53ce980d9c66ea969dc00..2916aec57ae32b35554da1a0d5fb764439c42a4a 100644 --- a/Base/Progress/ProgressHandler.cpp +++ b/Base/Progress/ProgressHandler.cpp @@ -16,7 +16,7 @@ #include <mutex> #include <stdexcept> -void ProgressHandler::subscribe(ProgressHandler::Callback_t inform) +void ProgressHandler::subscribe(Callback_t inform) { if (m_inform) throw std::runtime_error("Invalid call of ProgressHandler::subscribe: " @@ -31,15 +31,17 @@ void ProgressHandler::subscribe(ProgressHandler::Callback_t inform) void ProgressHandler::incrementDone(size_t ticks_done) { static std::mutex single_mutex; - std::unique_lock<std::mutex> single_lock(single_mutex); + { + std::unique_lock<std::mutex> _(single_mutex); - m_completed_nticks += ticks_done; - if (m_completed_nticks > m_expected_nticks) - m_expected_nticks = m_completed_nticks + 1; + m_completed_nticks += ticks_done; + if (m_completed_nticks > m_expected_nticks) + m_expected_nticks = m_completed_nticks + 1; - int percentage_done = (int)(100. * m_completed_nticks / m_expected_nticks); - // fractional part is discarded, which is fine here: - // the value 100 is only returned if everything is done + int percentage_done = (int)(100. * m_completed_nticks / m_expected_nticks); + // fractional part is discarded, which is fine here: + // the value 100 is only returned if everything is done - m_continuation_flag = (!m_inform || m_inform(percentage_done)) && m_continuation_flag; + m_continuation_flag = (!m_inform || m_inform(percentage_done)) && m_continuation_flag; + } } diff --git a/Base/Progress/ProgressHandler.h b/Base/Progress/ProgressHandler.h index 5c8b734bb6d3846c11b5524ac333906b9673ba4d..3d2f8fb7b667ed4fa25a89c79956196978f74335 100644 --- a/Base/Progress/ProgressHandler.h +++ b/Base/Progress/ProgressHandler.h @@ -41,7 +41,7 @@ public: , m_completed_nticks(other.m_completed_nticks) { } - void subscribe(ProgressHandler::Callback_t inform); + void subscribe(Callback_t inform); void reset() { m_completed_nticks = 0; diff --git a/Sim/Computation/IComputation.cpp b/Sim/Computation/IComputation.cpp index e48a22f9faafd7ff6cd657f5bcdddc5fbc39151c..2fb8df3a9b39881312fd78c1a488e33e0143bc17 100644 --- a/Sim/Computation/IComputation.cpp +++ b/Sim/Computation/IComputation.cpp @@ -13,7 +13,6 @@ // ************************************************************************************************ #include "Sim/Computation/IComputation.h" -#include "Base/Progress/DelayedProgressCounter.h" #include "Base/Progress/ProgressHandler.h" #include "Resample/Processed/ReSample.h" @@ -21,7 +20,7 @@ IComputation::IComputation(const ReSample& re_sample, const SimulationOptions& o ProgressHandler& progress) : m_re_sample(re_sample) , m_options(options) - , m_progress_counter(std::make_unique<DelayedProgressCounter>(&progress, 100)) + , m_progress(progress) { } @@ -29,5 +28,5 @@ IComputation::~IComputation() = default; void IComputation::stepProgress() const { - m_progress_counter->stepProgress(); + m_progress.incrementDone(1); } diff --git a/Sim/Computation/IComputation.h b/Sim/Computation/IComputation.h index be27585d4c57bf434611f9a7ca3f8058195019e7..754c6fe568a586697fc93cde113bbc1efc7ba503 100644 --- a/Sim/Computation/IComputation.h +++ b/Sim/Computation/IComputation.h @@ -18,9 +18,6 @@ #ifndef BORNAGAIN_SIM_COMPUTATION_ICOMPUTATION_H #define BORNAGAIN_SIM_COMPUTATION_ICOMPUTATION_H -#include <memory> - -class DelayedProgressCounter; class ReSample; class ProgressHandler; class SimulationOptions; @@ -46,7 +43,7 @@ protected: const SimulationOptions& m_options; private: - mutable std::unique_ptr<DelayedProgressCounter> m_progress_counter; + ProgressHandler& m_progress; }; #endif // BORNAGAIN_SIM_COMPUTATION_ICOMPUTATION_H