Commit bb332424 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Writting documentation on solver monitor.

parent 86ae79d4
Loading
Loading
Loading
Loading
+119 −84
Original line number Diff line number Diff line
@@ -18,9 +18,19 @@
namespace TNL {
   namespace Solvers {

/**
 * \brief Base class for solver monitors.
 *
 * The solver monitors serve for monitoring a convergence and status of various solvers.
 * The solver monitor uses separate thread for monitoring the solver status in preset time period.
 */
class SolverMonitor
{
   public:

      /**
       * \brief Basic construct with no arguments
       */
      SolverMonitor()
         : timeout_milliseconds( 500 ),
         started( false ),
@@ -28,18 +38,30 @@ public:
         timer( nullptr )
      {}

      /**
       * \brief This abstract method is responsible for printing or visualizing the status of the solver.
       */
      virtual void refresh() = 0;

   void setRefreshRate( const int& refreshRate )
   {
      timeout_milliseconds = refreshRate;
   }

   void setTimer( Timer& timer )
   {
      this->timer = &timer;
   }

      /**
       * \brief Set the time interval between two consecutive calls of \ref SolverMonitor::refresh.
       *
       * \param refreshRate refresh rate in miliseconds.
       */
      void setRefreshRate( const int& refreshRate ) { timeout_milliseconds = refreshRate; }

      /**
       * \brief Set a timer object for the solver monitor.
       *
       * If a timer is set, the monitor can measure real elapsed time since the start of the solver.
       *
       * \param timer is an instance of \ref TNL::Timer.
       */
      void setTimer( Timer& timer ) { this->timer = &timer; }

      /**
       * \brief Starts the main loop from which the method \ref SolverMonitor::refresh is called in given time periods.
       */
      void runMainLoop()
      {
         // We need to use both 'started' and 'stopped' to avoid a deadlock
@@ -70,15 +92,18 @@ public:
         stopped = false;
      }

   void stopMainLoop()
   {
      stopped = true;
   }
      /**
       * \brief Stops the main loop of the monitor. See \ref SolverMonitor::runMainLoop.
       */
      void stopMainLoop() { stopped = true; }

   bool isStopped() const
   {
      return stopped;
   }
      /**
       * \brief Checks whether the main loop was stopped.
       *
       * \return true if the main loop was stopped.
       * \return false if the mian loop was not stopped yet.
       */
      bool isStopped() const { return stopped; }

   protected:
      double getElapsedTime()
@@ -95,16 +120,26 @@ protected:
      Timer* timer;
};

// a RAII wrapper for launching the SolverMonitor's main loop in a separate thread
/**
 * \brief A RAII wrapper for launching the SolverMonitor's main loop in a separate thread.
 */
class SolverMonitorThread
{
   public:

      /**
       * \brief Constructor with instance of solver monitor.
       *
       * \param solverMonitor is a reference to an instance of a solver monitor.
       */
      SolverMonitorThread( SolverMonitor& solverMonitor )
         : solverMonitor( solverMonitor ),
         t( &SolverMonitor::runMainLoop, &solverMonitor )
      {}

      /**
       * \brief Destructor.
       */
      ~SolverMonitorThread()
      {
         solverMonitor.stopMainLoop();