template<typename
Real = double, typename Index = int>
class TNL::Solvers::IterativeSolverMonitor< Real, Index >
Object for monitoring convergence of iterative solvers.
- Template Parameters
-
Real | is a type of the floating-point arithmetics. |
Index | is an indexing type. |
The following example shows how to use the iterative solver monitor for monitoring convergence of linear iterative solver:
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>
10template<
typename Device >
12iterativeLinearSolverExample()
26 auto matrix_ptr = std::make_shared< MatrixType >();
27 matrix_ptr->setDimensions( size, size );
28 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
32 const int rowIdx = row.getRowIndex();
34 row.setElement( 0, rowIdx, 2.5 );
35 row.setElement( 1, rowIdx + 1, -1 );
37 else if( rowIdx == size - 1 ) {
38 row.setElement( 0, rowIdx - 1, -1.0 );
39 row.setElement( 1, rowIdx, 2.5 );
42 row.setElement( 0, rowIdx - 1, -1.0 );
43 row.setElement( 1, rowIdx, 2.5 );
44 row.setElement( 2, rowIdx + 1, -1.0 );
51 matrix_ptr->forAllRows( f );
57 Vector x( size, 1.0 );
59 matrix_ptr->vectorProduct( x, b );
69 solver.setOmega( 0.0005 );
75 IterativeSolverMonitorType monitor;
77 monitor.setRefreshRate( 10 );
78 monitor.setVerbose( 1 );
79 monitor.setStage(
"Jacobi stage:" );
80 solver.setSolverMonitor( monitor );
81 solver.setConvergenceResidue( 1.0e-6 );
83 monitor.stopMainLoop();
88main(
int argc,
char* argv[] )
91 iterativeLinearSolverExample< TNL::Devices::Sequential >();
95 iterativeLinearSolverExample< TNL::Devices::Cuda >();
#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
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:
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>
11template<
typename Device >
13iterativeLinearSolverExample()
27 auto matrix_ptr = std::make_shared< MatrixType >();
28 matrix_ptr->setDimensions( size, size );
29 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
33 const int rowIdx = row.getRowIndex();
35 row.setElement( 0, rowIdx, 2.5 );
36 row.setElement( 1, rowIdx + 1, -1 );
38 else if( rowIdx == size - 1 ) {
39 row.setElement( 0, rowIdx - 1, -1.0 );
40 row.setElement( 1, rowIdx, 2.5 );
43 row.setElement( 0, rowIdx - 1, -1.0 );
44 row.setElement( 1, rowIdx, 2.5 );
45 row.setElement( 2, rowIdx + 1, -1.0 );
52 matrix_ptr->forAllRows( f );
58 Vector x( size, 1.0 );
60 matrix_ptr->vectorProduct( x, b );
70 solver.setOmega( 0.0005 );
76 IterativeSolverMonitorType monitor;
78 monitor.setRefreshRate( 10 );
79 monitor.setVerbose( 1 );
80 monitor.setStage(
"Jacobi stage:" );
82 monitor.setTimer( timer );
84 solver.setSolverMonitor( monitor );
85 solver.setConvergenceResidue( 1.0e-6 );
87 monitor.stopMainLoop();
92main(
int argc,
char* argv[] )
95 iterativeLinearSolverExample< TNL::Devices::Sequential >();
99 iterativeLinearSolverExample< TNL::Devices::Cuda >();
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 ]