BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ProgressHandler.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Core/Computation/ProgressHandler.cpp
6 //! @brief Implements class ProgressHandler.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
16 #include <mutex>
17 #include <stdexcept>
18 
20 {
21  if (m_inform)
22  throw std::runtime_error("Invalid call of ProgressHandler::subscribe: "
23  "currently, no more than one subscriber is allowed");
24  m_inform = inform;
25 }
26 
27 //! Increments number of completed computation steps (ticks).
28 //! Performs callback (method m_inform) to inform the subscriber about
29 //! the state of the computation and to obtain as return value a flag
30 //! that indicates whether to continue the computation.
31 void ProgressHandler::incrementDone(size_t ticks_done)
32 {
33  static std::mutex single_mutex;
34  std::unique_lock<std::mutex> single_lock(single_mutex);
35 
36  m_completed_nticks += ticks_done;
39 
40  int percentage_done = (int)(100. * m_completed_nticks / m_expected_nticks);
41  // fractional part is discarded, which is fine here:
42  // the value 100 is only returned if everything is done
43 
44  m_continuation_flag = (!m_inform || m_inform(percentage_done)) && m_continuation_flag;
45 }
Defines class ProgressHandler.
size_t m_completed_nticks
void subscribe(ProgressHandler::Callback_t callback)
Callback_t m_inform
void incrementDone(size_t ticks_done)
Increments number of completed computation steps (ticks).
std::function< bool(size_t)> Callback_t
size_t m_expected_nticks