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:
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
16
17
18
19
20
21
22
25 const int size( 5 );
27 matrix_ptr->setDimensions( size, size );
28 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
29
31 {
32 const int rowIdx = row.getRowIndex();
33 if( rowIdx == 0 ) {
34 row.setElement( 0, rowIdx, 2.5 );
35 row.setElement( 1, rowIdx + 1, -1 );
36 }
37 else if( rowIdx == size - 1 ) {
38 row.setElement( 0, rowIdx - 1, -1.0 );
39 row.setElement( 1, rowIdx, 2.5 );
40 }
41 else {
42 row.setElement( 0, rowIdx - 1, -1.0 );
43 row.setElement( 1, rowIdx, 2.5 );
44 row.setElement( 2, rowIdx + 1, -1.0 );
45 }
46 };
47
48
49
50
51 matrix_ptr->forAllRows( f );
53
54
55
56
57 Vector x( size, 1.0 );
58 Vector b( size );
59 matrix_ptr->vectorProduct( x, b );
60 x = 0.0;
62
63
64
65
67 LinearSolver solver;
69 solver.setOmega( 0.0005 );
70
71
72
73
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 );
82 solver.solve( b, x );
83 monitor.stopMainLoop();
85}
86
87int
88main( int argc, char* argv[] )
89{
91 iterativeLinearSolverExample< TNL::Devices::Sequential >();
92
93#ifdef __CUDACC__
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
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 ]
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
17
18
19
20
21
22
23
26 const int size( 5 );
28 matrix_ptr->setDimensions( size, size );
29 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
30
32 {
33 const int rowIdx = row.getRowIndex();
34 if( rowIdx == 0 ) {
35 row.setElement( 0, rowIdx, 2.5 );
36 row.setElement( 1, rowIdx + 1, -1 );
37 }
38 else if( rowIdx == size - 1 ) {
39 row.setElement( 0, rowIdx - 1, -1.0 );
40 row.setElement( 1, rowIdx, 2.5 );
41 }
42 else {
43 row.setElement( 0, rowIdx - 1, -1.0 );
44 row.setElement( 1, rowIdx, 2.5 );
45 row.setElement( 2, rowIdx + 1, -1.0 );
46 }
47 };
48
49
50
51
52 matrix_ptr->forAllRows( f );
54
55
56
57
58 Vector x( size, 1.0 );
59 Vector b( size );
60 matrix_ptr->vectorProduct( x, b );
61 x = 0.0;
63
64
65
66
68 LinearSolver solver;
70 solver.setOmega( 0.0005 );
71
72
73
74
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 );
86 solver.solve( b, x );
87 monitor.stopMainLoop();
89}
90
91int
92main( int argc, char* argv[] )
93{
95 iterativeLinearSolverExample< TNL::Devices::Sequential >();
96
97#ifdef __CUDACC__
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:6.54e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]