Template Numerical Library version\ main:94209208
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
TNL::Solvers::IterativeSolverMonitor< Real, Index > Class Template Reference

Object for monitoring convergence of iterative solvers. More...

#include <TNL/Solvers/IterativeSolverMonitor.h>

Inheritance diagram for TNL::Solvers::IterativeSolverMonitor< Real, Index >:
Inheritance graph
[legend]
Collaboration diagram for TNL::Solvers::IterativeSolverMonitor< Real, Index >:
Collaboration graph
[legend]

Public Types

using IndexType = Index
 A type for indexing.
 
using RealType = Real
 A type of the floating-point arithmetics.
 

Public Member Functions

 IterativeSolverMonitor ()=default
 Construct with no parameters.
 
void refresh () override
 Causes that the monitor prints out the status of the solver.
 
void setIterations (const IndexType &iterations)
 Set number of the current iteration.
 
void setNodesPerIteration (const IndexType &nodes)
 Set the number of nodes of the numerical mesh or lattice.
 
void setResidue (const RealType &residue)
 Set residue of the current approximation of the solution.
 
void setStage (const std::string &stage)
 This method can be used for naming a stage of the monitored solver.
 
void setTime (const RealType &time)
 Set the time of the simulated evolution if it is time dependent.
 
void setTimeStep (const RealType &timeStep)
 Set the time step for time dependent iterative solvers.
 
void setVerbose (const IndexType &verbose)
 Set up the verbosity of the monitor.
 
- Public Member Functions inherited from TNL::Solvers::SolverMonitor
 SolverMonitor ()=default
 Basic construct with no arguments.
 
bool isStopped () const
 Checks whether the main loop was stopped.
 
void runMainLoop ()
 Starts the main loop from which the method SolverMonitor::refresh is called in given time periods.
 
void setRefreshRate (const int &refreshRate)
 Set the time interval between two consecutive calls of SolverMonitor::refresh.
 
void setTimer (Timer &timer)
 Set a timer object for the solver monitor.
 
void stopMainLoop ()
 Stops the main loop of the monitor. See runMainLoop.
 

Protected Member Functions

int getLineWidth ()
 
- Protected Member Functions inherited from TNL::Solvers::SolverMonitor
double getElapsedTime ()
 

Protected Attributes

std::atomic_bool attributes_changed { false }
 
RealType elapsed_time_before_refresh = 0
 
IndexType iterations = 0
 
IndexType iterations_before_refresh = 0
 
RealType last_mlups = 0
 
IndexType nodesPerIteration = 0
 
RealType residue = 0
 
std::atomic_bool saved { false }
 
IndexType saved_iterations = 0
 
RealType saved_residue = 0
 
std::string saved_stage
 
RealType saved_time = 0
 
RealType saved_timeStep = 0
 
std::string stage
 
RealType time = 0
 
RealType timeStep = 0
 
IndexType verbose = 2
 
- Protected Attributes inherited from TNL::Solvers::SolverMonitor
std::atomic_bool started { false }
 
std::atomic_bool stopped { false }
 
std::atomic_int timeout_milliseconds { 500 }
 
Timertimer = nullptr
 

Detailed Description

template<typename Real = double, typename Index = int>
class TNL::Solvers::IterativeSolverMonitor< Real, Index >

Object for monitoring convergence of iterative solvers.

Template Parameters
Realis a type of the floating-point arithmetics.
Indexis an indexing type.

The following example shows how to use the iterative solver monitor for monitoring convergence of linear iterative solver:

1#include <iostream>
2#include <memory>
3#include <chrono>
4#include <thread>
5#include <TNL/Matrices/SparseMatrix.h>
6#include <TNL/Devices/Sequential.h>
7#include <TNL/Devices/Cuda.h>
8#include <TNL/Solvers/Linear/Jacobi.h>
9
10template< typename Device >
11void
12iterativeLinearSolverExample()
13{
14 /***
15 * Set the following matrix (dots represent zero matrix elements):
16 *
17 * / 2.5 -1 . . . \
18 * | -1 2.5 -1 . . |
19 * | . -1 2.5 -1. . |
20 * | . . -1 2.5 -1 |
21 * \ . . . -1 2.5 /
22 */
25 const int size( 5 );
26 auto matrix_ptr = std::make_shared< MatrixType >();
27 matrix_ptr->setDimensions( size, size );
28 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
29
30 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
31 {
32 const int rowIdx = row.getRowIndex();
33 if( rowIdx == 0 ) {
34 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
35 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
36 }
37 else if( rowIdx == size - 1 ) {
38 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
39 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
40 }
41 else {
42 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
43 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
44 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
45 }
46 };
47
48 /***
49 * Set the matrix elements.
50 */
51 matrix_ptr->forAllRows( f );
52 std::cout << *matrix_ptr << std::endl;
53
54 /***
55 * Set the right-hand side vector.
56 */
57 Vector x( size, 1.0 );
58 Vector b( size );
59 matrix_ptr->vectorProduct( x, b );
60 x = 0.0;
61 std::cout << "Vector b = " << b << std::endl;
62
63 /***
64 * Setup solver of the linear system.
65 */
67 LinearSolver solver;
68 solver.setMatrix( matrix_ptr );
69 solver.setOmega( 0.0005 );
70
71 /***
72 * Setup monitor of the iterative solver.
73 */
74 using IterativeSolverMonitorType = TNL::Solvers::IterativeSolverMonitor< double, int >;
75 IterativeSolverMonitorType monitor;
76 TNL::Solvers::SolverMonitorThread monitorThread( monitor );
77 monitor.setRefreshRate( 10 ); // refresh rate in milliseconds
78 monitor.setVerbose( 1 );
79 monitor.setStage( "Jacobi stage:" );
80 solver.setSolverMonitor( monitor );
81 solver.setConvergenceResidue( 1.0e-6 );
82 solver.solve( b, x );
83 monitor.stopMainLoop();
84 std::cout << "Vector x = " << x << std::endl;
85}
86
87int
88main( int argc, char* argv[] )
89{
90 std::cout << "Solving linear system on host: " << std::endl;
91 iterativeLinearSolverExample< TNL::Devices::Sequential >();
92
93#ifdef __CUDACC__
94 std::cout << "Solving linear system on CUDA device: " << std::endl;
95 iterativeLinearSolverExample< TNL::Devices::Cuda >();
96#endif
97}
#define __cuda_callable__
Definition Macros.h:49
Vector extends Array with algebraic operations.
Definition Vector.h:36
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:57
Iterative solver of linear systems based on the Jacobi method.
Definition Jacobi.h:21
void setMatrix(const MatrixPointer &matrix)
Set the matrix of the linear system.
Definition LinearSolver.h:120
A RAII wrapper for launching the SolverMonitor's main loop in a separate thread.
Definition SolverMonitor.h:133
T endl(T... args)

The result looks as follows:

Solving linear system on host:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
Jacobi stage: ITER: 11071 RES: 0.13186
Jacobi stage: ITER: 22231 RES: 0.02376
Jacobi stage: ITER: 33309 RES: 0.0043323
Jacobi stage: ITER: 44471 RES: 0.00078028
Jacobi stage: ITER: 55631 RES: 0.00014054
Jacobi stage: ITER: 66793 RES: 2.5296e-05
Jacobi stage: ITER: 77995 RES: 4.5282e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]
Solving linear system on CUDA device:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
Jacobi stage: ITER: 3439 RES: 0.44102
Jacobi stage: ITER: 6905 RES: 0.25088
Jacobi stage: ITER: 10105 RES: 0.15303
Jacobi stage: ITER: 13673 RES: 0.088433
Jacobi stage: ITER: 17095 RES: 0.052295
Jacobi stage: ITER: 20291 RES: 0.032008
Jacobi stage: ITER: 23755 RES: 0.018801
Jacobi stage: ITER: 27215 RES: 0.01105
Jacobi stage: ITER: 30685 RES: 0.0064828
Jacobi stage: ITER: 34183 RES: 0.0037892
Jacobi stage: ITER: 37689 RES: 0.0022107
Jacobi stage: ITER: 41155 RES: 0.0012985
Jacobi stage: ITER: 44621 RES: 0.00076228
Jacobi stage: ITER: 48087 RES: 0.00044775
Jacobi stage: ITER: 51563 RES: 0.00026252
Jacobi stage: ITER: 55035 RES: 0.00015401
Jacobi stage: ITER: 58511 RES: 9.0296e-05
Jacobi stage: ITER: 60483 RES: 6.6699e-05
Jacobi stage: ITER: 61279 RES: 5.9023e-05
Jacobi stage: ITER: 62095 RES: 5.207e-05
Jacobi stage: ITER: 62911 RES: 4.5936e-05
Jacobi stage: ITER: 63727 RES: 4.0524e-05
Jacobi stage: ITER: 64545 RES: 3.5729e-05
Jacobi stage: ITER: 65363 RES: 3.152e-05
Jacobi stage: ITER: 66181 RES: 2.779e-05
Jacobi stage: ITER: 66999 RES: 2.4516e-05
Jacobi stage: ITER: 67817 RES: 2.1615e-05
Jacobi stage: ITER: 68653 RES: 1.901e-05
Jacobi stage: ITER: 69471 RES: 1.677e-05
Jacobi stage: ITER: 70289 RES: 1.4786e-05
Jacobi stage: ITER: 71107 RES: 1.3044e-05
Jacobi stage: ITER: 71923 RES: 1.1507e-05
Jacobi stage: ITER: 72741 RES: 1.0146e-05
Jacobi stage: ITER: 73559 RES: 8.9504e-06
Jacobi stage: ITER: 74377 RES: 7.8911e-06
Jacobi stage: ITER: 75195 RES: 6.9616e-06
Jacobi stage: ITER: 75983 RES: 6.1679e-06
Jacobi stage: ITER: 76799 RES: 5.4413e-06
Jacobi stage: ITER: 77615 RES: 4.8003e-06
Jacobi stage: ITER: 78433 RES: 4.2322e-06
Jacobi stage: ITER: 79251 RES: 3.7337e-06
Jacobi stage: ITER: 80165 RES: 3.2436e-06
Jacobi stage: ITER: 80987 RES: 2.8598e-06
Jacobi stage: ITER: 81807 RES: 2.5213e-06
Jacobi stage: ITER: 82623 RES: 2.2243e-06
Jacobi stage: ITER: 83443 RES: 1.9611e-06
Jacobi stage: ITER: 84267 RES: 1.7279e-06
Jacobi stage: ITER: 85087 RES: 1.5234e-06
Jacobi stage: ITER: 85905 RES: 1.3432e-06
Jacobi stage: ITER: 86719 RES: 1.1857e-06
Jacobi stage: ITER: 87539 RES: 1.0453e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]

The following example shows how to employ timer (TNL::Timer) to the monitor of iterative solvers:

1#include <iostream>
2#include <memory>
3#include <chrono>
4#include <thread>
5#include <TNL/Timer.h>
6#include <TNL/Matrices/SparseMatrix.h>
7#include <TNL/Devices/Sequential.h>
8#include <TNL/Devices/Cuda.h>
9#include <TNL/Solvers/Linear/Jacobi.h>
10
11template< typename Device >
12void
13iterativeLinearSolverExample()
14{
15 /***
16 * Set the following matrix (dots represent zero matrix elements):
17 *
18 * / 2.5 -1 . . . \
19 * | -1 2.5 -1 . . |
20 * | . -1 2.5 -1. . |
21 * | . . -1 2.5 -1 |
22 * \ . . . -1 2.5 /
23 */
26 const int size( 5 );
27 auto matrix_ptr = std::make_shared< MatrixType >();
28 matrix_ptr->setDimensions( size, size );
29 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
30
31 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
32 {
33 const int rowIdx = row.getRowIndex();
34 if( rowIdx == 0 ) {
35 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
36 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
37 }
38 else if( rowIdx == size - 1 ) {
39 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
40 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
41 }
42 else {
43 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
44 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
45 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
46 }
47 };
48
49 /***
50 * Set the matrix elements.
51 */
52 matrix_ptr->forAllRows( f );
53 std::cout << *matrix_ptr << std::endl;
54
55 /***
56 * Set the right-hand side vector.
57 */
58 Vector x( size, 1.0 );
59 Vector b( size );
60 matrix_ptr->vectorProduct( x, b );
61 x = 0.0;
62 std::cout << "Vector b = " << b << std::endl;
63
64 /***
65 * Setup solver of the linear system.
66 */
68 LinearSolver solver;
69 solver.setMatrix( matrix_ptr );
70 solver.setOmega( 0.0005 );
71
72 /***
73 * Setup monitor of the iterative solver.
74 */
75 using IterativeSolverMonitorType = TNL::Solvers::IterativeSolverMonitor< double, int >;
76 IterativeSolverMonitorType monitor;
77 TNL::Solvers::SolverMonitorThread mmonitorThread( monitor );
78 monitor.setRefreshRate( 10 ); // refresh rate in milliseconds
79 monitor.setVerbose( 1 );
80 monitor.setStage( "Jacobi stage:" );
81 TNL::Timer timer;
82 monitor.setTimer( timer );
83 timer.start();
84 solver.setSolverMonitor( monitor );
85 solver.setConvergenceResidue( 1.0e-6 );
86 solver.solve( b, x );
87 monitor.stopMainLoop();
88 std::cout << "Vector x = " << x << std::endl;
89}
90
91int
92main( int argc, char* argv[] )
93{
94 std::cout << "Solving linear system on host: " << std::endl;
95 iterativeLinearSolverExample< TNL::Devices::Sequential >();
96
97#ifdef __CUDACC__
98 std::cout << "Solving linear system on CUDA device: " << std::endl;
99 iterativeLinearSolverExample< TNL::Devices::Cuda >();
100#endif
101}
Class for real time, CPU time and CPU cycles measuring.
Definition Timer.h:25
void start()
Starts timer.
Definition Timer.hpp:42

The result looks as follows:

Solving linear system on host:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
ELA: 0
ELA: 0.10139 Jacobi stage: ITER: 7061 RES: 0.24486
ELA: 0.20154 Jacobi stage: ITER: 17493 RES: 0.049179
ELA: 0.30167 Jacobi stage: ITER: 28235 RES: 0.0094478
ELA: 0.4018 Jacobi stage: ITER: 39079 RES: 0.0017852
ELA: 0.50193 Jacobi stage: ITER: 49917 RES: 0.00033793
ELA: 0.60205 Jacobi stage: ITER: 60709 RES: 6.4404e-05
ELA: 0.70218 Jacobi stage: ITER: 71559 RES: 1.2169e-05
ELA: 0.80231 Jacobi stage: ITER: 82295 RES: 2.3393e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]
Solving linear system on CUDA device:
Row: 0 -> 0:2.5 1:-1
Row: 1 -> 0:-1 1:2.5 2:-1
Row: 2 -> 1:-1 2:2.5 3:-1
Row: 3 -> 2:-1 3:2.5 4:-1
Row: 4 -> 3:-1 4:2.5
Vector b = [ 1.5, 0.5, 0.5, 0.5, 1.5 ]
ELA:1.6994e-
ELA: 0.10012 Jacobi stage: ITER: 753 RES: 0.78983
ELA: 0.20213 Jacobi stage: ITER: 1589 RES: 0.6378
ELA: 0.30226 Jacobi stage: ITER: 2407 RES: 0.53519
ELA: 0.40239 Jacobi stage: ITER: 3223 RES: 0.45841
ELA: 0.50255 Jacobi stage: ITER: 4039 RES: 0.39753
ELA: 0.60269 Jacobi stage: ITER: 4857 RES: 0.34704
ELA: 0.70283 Jacobi stage: ITER: 5675 RES: 0.30444
ELA: 0.80294 Jacobi stage: ITER: 6493 RES: 0.26755
ELA: 0.90305 Jacobi stage: ITER: 7311 RES: 0.2356
ELA: 1.0032 Jacobi stage: ITER: 8123 RES: 0.20776
ELA: 1.1033 Jacobi stage: ITER: 8941 RES: 0.18306
ELA: 1.2034 Jacobi stage: ITER: 9759 RES: 0.16144
ELA: 1.3035 Jacobi stage: ITER: 10575 RES: 0.1424
ELA: 1.4036 Jacobi stage: ITER: 11393 RES: 0.12553
ELA: 1.5037 Jacobi stage: ITER: 12211 RES: 0.11074
ELA: 1.6039 Jacobi stage: ITER: 13027 RES: 0.09769
ELA: 1.704 Jacobi stage: ITER: 13847 RES: 0.086127
ELA: 1.8041 Jacobi stage: ITER: 14663 RES: 0.07598
ELA: 1.9043 Jacobi stage: ITER: 15479 RES: 0.067029
ELA: 2.0044 Jacobi stage: ITER: 16421 RES: 0.057982
ELA: 2.1045 Jacobi stage: ITER: 17243 RES: 0.05112
ELA: 2.2046 Jacobi stage: ITER: 18059 RES: 0.045098
ELA: 2.3055 Jacobi stage: ITER: 18883 RES: 0.039736
ELA: 2.4056 Jacobi stage: ITER: 19671 RES: 0.035206
ELA: 2.5057 Jacobi stage: ITER: 20487 RES: 0.031059
ELA: 2.6059 Jacobi stage: ITER: 21307 RES: 0.027383
ELA: 2.706 Jacobi stage: ITER: 22125 RES: 0.024143
ELA: 2.8061 Jacobi stage: ITER: 22943 RES: 0.021299
ELA: 2.9062 Jacobi stage: ITER: 23759 RES: 0.01879
ELA: 3.0064 Jacobi stage: ITER: 24579 RES: 0.016566
ELA: 3.1065 Jacobi stage: ITER: 25399 RES: 0.014605
ELA: 3.2066 Jacobi stage: ITER: 26215 RES: 0.012885
ELA: 3.3068 Jacobi stage: ITER: 27041 RES: 0.011346
ELA: 3.4069 Jacobi stage: ITER: 28197 RES: 0.0095002
ELA: 3.507 Jacobi stage: ITER: 31191 RES: 0.0059998
ELA: 3.6071 Jacobi stage: ITER: 34191 RES: 0.0037846
ELA: 3.7072 Jacobi stage: ITER: 37375 RES: 0.0023207
ELA: 3.8074 Jacobi stage: ITER: 40521 RES: 0.0014309
ELA: 3.9075 Jacobi stage: ITER: 43539 RES: 0.00090038
ELA: 4.0076 Jacobi stage: ITER: 46903 RES: 0.00053705
ELA: 4.1077 Jacobi stage: ITER: 50595 RES: 0.0003046
ELA: 4.2079 Jacobi stage: ITER: 54267 RES: 0.00017329
ELA: 4.308 Jacobi stage: ITER: 57945 RES: 9.8467e-05
ELA: 4.4081 Jacobi stage: ITER: 61623 RES: 5.5985e-05
ELA: 4.5082 Jacobi stage: ITER: 65307 RES: 3.1792e-05
ELA: 4.6083 Jacobi stage: ITER: 68989 RES: 1.8054e-05
ELA: 4.7085 Jacobi stage: ITER: 72673 RES: 1.0252e-05
ELA: 4.8086 Jacobi stage: ITER: 76355 RES: 5.8254e-06
ELA: 4.9087 Jacobi stage: ITER: 80017 RES: 3.3182e-06
ELA: 5.0088 Jacobi stage: ITER: 83675 RES: 1.8924e-06
ELA: 5.1089 Jacobi stage: ITER: 87171 RES: 1.1061e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]

Member Function Documentation

◆ refresh()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::refresh ( )
overridevirtual

Causes that the monitor prints out the status of the solver.

Implements TNL::Solvers::SolverMonitor.

◆ setIterations()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setIterations ( const IndexType & iterations)

Set number of the current iteration.

Parameters
iterationsis number of the current iteration.

◆ setNodesPerIteration()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setNodesPerIteration ( const IndexType & nodes)

Set the number of nodes of the numerical mesh or lattice.

This can be used to compute the number of nodes processed per one second.

Parameters
nodesis number of nodes of the numerical mesh or lattice.

◆ setResidue()

template<typename Real = double, typename Index = int>
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setResidue ( const RealType & residue)

Set residue of the current approximation of the solution.

Parameters
residueis a residue of the current approximation of the solution.

◆ setStage()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setStage ( const std::string & stage)

This method can be used for naming a stage of the monitored solver.

The stage name can be used to differ between various stages of iterative solvers.

Parameters
stageis name of the solver stage.

◆ setTime()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setTime ( const RealType & time)

Set the time of the simulated evolution if it is time dependent.

This can be used for example when solving parabolic or hyperbolic PDEs.

Parameters
timetime of the simulated evolution.

◆ setTimeStep()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setTimeStep ( const RealType & timeStep)

Set the time step for time dependent iterative solvers.

Parameters
timeSteptime step of the time dependent iterative solver.

◆ setVerbose()

template<typename Real , typename Index >
void TNL::Solvers::IterativeSolverMonitor< Real, Index >::setVerbose ( const IndexType & verbose)

Set up the verbosity of the monitor.

Parameters
verboseis the new value of the verbosity of the monitor.

The documentation for this class was generated from the following files: