From d0affdce385072235e7b31775959a2f44877cc02 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de> Date: Fri, 13 Jan 2023 14:25:56 +0100 Subject: [PATCH] rm class and header file ComputationStatus --- Sim/Computation/ComputationStatus.h | 50 ----------------------------- Sim/Computation/IComputation.cpp | 4 +-- Sim/Computation/IComputation.h | 9 +++--- Sim/Simulation/ISimulation.cpp | 6 ++-- 4 files changed, 7 insertions(+), 62 deletions(-) delete mode 100644 Sim/Computation/ComputationStatus.h diff --git a/Sim/Computation/ComputationStatus.h b/Sim/Computation/ComputationStatus.h deleted file mode 100644 index 396e303dc6d..00000000000 --- a/Sim/Computation/ComputationStatus.h +++ /dev/null @@ -1,50 +0,0 @@ -// ************************************************************************************************ -// -// BornAgain: simulate and fit reflection and scattering -// -//! @file Sim/Computation/ComputationStatus.h -//! @brief Defines and implements interface class ComputationStatus. -//! -//! @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_SIM_COMPUTATION_COMPUTATIONSTATUS_H -#define BORNAGAIN_SIM_COMPUTATION_COMPUTATIONSTATUS_H - -#include <string> - -//! Completion status (flag and text) of a numeric computation. - -class ComputationStatus { -public: - ComputationStatus() - : m_status(IDLE) - { - } - - bool isCompleted() const { return m_status == COMPLETED; } - std::string errorMessage() const { return m_error_message; } - - void setRunning() { m_status = RUNNING; } - void setCompleted() { m_status = COMPLETED; } - void setFailed(const std::string& message) - { - m_error_message = message; - m_status = FAILED; - } - -private: - enum ESimulationStatus { IDLE, RUNNING, COMPLETED, FAILED }; - - ESimulationStatus m_status; - std::string m_error_message; -}; - -#endif // BORNAGAIN_SIM_COMPUTATION_COMPUTATIONSTATUS_H diff --git a/Sim/Computation/IComputation.cpp b/Sim/Computation/IComputation.cpp index da177adccf1..ea9a5bad2b3 100644 --- a/Sim/Computation/IComputation.cpp +++ b/Sim/Computation/IComputation.cpp @@ -40,7 +40,6 @@ void IComputation::stepProgress() const void IComputation::compute() { - m_status.setRunning(); try { if (!m_progress->alive()) return; @@ -48,8 +47,7 @@ void IComputation::compute() runProtected(); // <--- here the main work is done - m_status.setCompleted(); } catch (const std::exception& ex) { - m_status.setFailed(ex.what()); + m_errorMessage = ex.what(); } } diff --git a/Sim/Computation/IComputation.h b/Sim/Computation/IComputation.h index 448e9aa7142..c745a298e95 100644 --- a/Sim/Computation/IComputation.h +++ b/Sim/Computation/IComputation.h @@ -18,8 +18,8 @@ #ifndef BORNAGAIN_SIM_COMPUTATION_ICOMPUTATION_H #define BORNAGAIN_SIM_COMPUTATION_ICOMPUTATION_H -#include "Sim/Computation/ComputationStatus.h" #include <memory> +#include <string> class DelayedProgressCounter; class MultiLayer; @@ -38,11 +38,10 @@ public: ProgressHandler& progress); virtual ~IComputation(); - //! Calls runProtected(), catches exceptions, sets m_status. + //! Calls runProtected(), catches exceptions, sets m_errorMessage. void compute(); - bool isCompleted() const { return m_status.isCompleted(); } - std::string errorMessage() const { return m_status.errorMessage(); } + std::string errorMessage() const { return m_errorMessage; } protected: void setProgressHandler(ProgressHandler* progress) const; @@ -57,7 +56,7 @@ private: virtual void runProtected() = 0; mutable std::unique_ptr<DelayedProgressCounter> m_progress_counter; - ComputationStatus m_status; + std::string m_errorMessage; }; #endif // BORNAGAIN_SIM_COMPUTATION_ICOMPUTATION_H diff --git a/Sim/Simulation/ISimulation.cpp b/Sim/Simulation/ISimulation.cpp index 30033397468..2285896b0d1 100644 --- a/Sim/Simulation/ISimulation.cpp +++ b/Sim/Simulation/ISimulation.cpp @@ -220,7 +220,7 @@ void ISimulation::runSingleSimulation(const ReSample& re_sample, size_t batch_st // Run computation in current thread. const auto& c = createComputation(re_sample, batch_start, batch_size); c->compute(); // <---- here most work is done (unthreaded case)! - if (!c->isCompleted()) + if (!c->errorMessage().empty()) throw std::runtime_error("Unexpected error in simulation:\n" + c->errorMessage()); } else { @@ -235,12 +235,10 @@ void ISimulation::runSingleSimulation(const ReSample& re_sample, size_t batch_st break; threads.emplace_back(new std::thread( [this, &re_sample, &failure_messages, &mutex, thread_start, thread_size]() { - mutex.lock(); const auto& c = createComputation(re_sample, thread_start, thread_size); - mutex.unlock(); c->compute(); // <---- here most work is done (threaded case)! mutex.lock(); - if (!c->isCompleted()) + if (!c->errorMessage().empty()) failure_messages.push_back(c->errorMessage()); mutex.unlock(); })); -- GitLab