Introduction
Solvers of linear systems are one of the most important algorithms in scientific computations. TNL offers the following iterative methods:
- Stationary methods
- Jacobi method (TNL::Solvers::Linear::Jacobi)
- Successive-overrelaxation method, SOR (TNL::Solvers::Linear::SOR)
- Krylov subspace methods
- Conjugate gradient method, CG (TNL::Solvers::Linear::CG)
- Biconjugate gradient stabilized method, BICGStab (TNL::Solvers::Linear::BICGStab)
- Biconjugate gradient stabilized method, BICGStab(l) (TNL::Solvers::Linear::BICGStabL)
- Transpose-free quasi-minimal residual method, TFQMR (TNL::Solvers::Linear::TFQMR)
- Generalized minimal residual method, GMRES (TNL::Solvers::Linear::GMRES) with various methods of orthogonalization:
- Classical Gramm-Schmidt, CGS
- Classical Gramm-Schmidt with reorthogonalization, CGSR
- Modified Gramm-Schmidt, MGS
- Modified Gramm-Schmidt with reorthogonalization, MGSR
- Compact WY form of the Householder reflections, CWY
The iterative solvers (not the stationary solvers like TNL::Solvers::Linear::Jacobi and TNL::Solvers::Linear::SOR) can be combined with the following preconditioners:
- Diagonal or Jacobi (TNL::Solvers::Linear::Preconditioners::Diagonal)
- ILU (Incomplete LU) - CPU only currently
- ILU(0) (TNL::Solvers::Linear::Preconditioners::ILU0)
- ILUT (ILU with thresholding) (TNL::Solvers::Linear::Preconditioners::ILUT)
Iterative solvers of linear systems
Basic setup
All iterative solvers for linear systems can be found in the namespace TNL::Solvers::Linear. The following example shows the use the iterative solvers:
1#include <iostream>
2#include <memory>
3#include <TNL/Matrices/SparseMatrix.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6#include <TNL/Solvers/Linear/TFQMR.h>
7
8template< typename Device >
9void
10iterativeLinearSolverExample()
11{
12
13
14
15
16
17
18
19
20
23 const int size( 5 );
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 );
33 row.setElement( 1, rowIdx + 1, -1 );
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 );
37 row.setElement( 1, rowIdx, 2.5 );
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 );
41 row.setElement( 1, rowIdx, 2.5 );
42 row.setElement( 2, rowIdx + 1, -1.0 );
43 }
44 };
45
46
47
48
49 matrix_ptr->forAllRows( f );
51
52
53
54
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
60
61
62
63
65 LinearSolver solver;
67 solver.setConvergenceResidue( 1.0e-6 );
68 solver.solve( b, x );
70}
71
72int
73main( int argc, char* argv[] )
74{
75 std::cout <<
"Solving linear system on host:\n";
76 iterativeLinearSolverExample< TNL::Devices::Sequential >();
77
78#ifdef __CUDACC__
79 std::cout <<
"Solving linear system on CUDA device:\n";
80 iterativeLinearSolverExample< TNL::Devices::Cuda >();
81#endif
82}
#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:55
virtual void setMatrix(const MatrixPointer &matrix)
Set the matrix of the linear system.
Definition LinearSolver.h:120
Iterative solver of linear systems based on the Transpose-free quasi-minimal residual (TFQMR) method.
Definition TFQMR.h:21
In this example we solve a linear system \( A \vec x = \vec b \) where
\[A = \left(
\begin{array}{cccc}
2.5 & -1 & & & \\
-1 & 2.5 & -1 & & \\
& -1 & 2.5 & -1 & \\
& & -1 & 2.5 & -1 \\
& & & -1 & 2.5 \\
\end{array}
\right)
\]
The right-hand side vector \(\vec b \) is set to \(( 1.5, 0.5, 0.5, 0.5, 1.5 )^T \) so that the exact solution is \( \vec x = ( 1, 1, 1, 1, 1 )^T \). The elements of the matrix \( A \) are set using the method TNL::Matrices::SparseMatrix::forAllRows. In this example, we use the sparse matrix but any other matrix type can be used as well (see the namespace TNL::Matrices). Next we set the solution vector \( \vec x = ( 1, 1, 1, 1, 1 )^T \) and multiply it with matrix \( A \) to get the right-hand side vector \( \vec b \). Finally, we reset the vector \( \vec x \) to zero.
To solve the linear system in the example, we use the TFQMR solver. Other solvers can be used as well (see the namespace TNL::Solvers::Linear). The solver needs only one template parameter which is the matrix type. Next we create an instance of the solver and set the matrix of the linear system. Note that the matrix is passed to the solver as a std::shared_ptr. Then we set the stopping criterion for the iterative method in terms of the relative residue, i.e. \( \lVert \vec b - A \vec x \rVert / \lVert b \rVert \). The solver is executed by calling the TNL::Solvers::Linear::LinearSolver::solve method which accepts the right-hand side vector \( \vec b \) and the solution vector \( \vec x \).
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 = [ 1, 1, 1, 1, 1 ]
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 ]
Vector x = [ 1, 1, 1, 1, 1 ]
Setup with a solver monitor
Solution of large linear systems may take a lot of time. In such situations, it is useful to be able to monitor the convergence of the solver or the solver status in general. For this purpose, TNL provides a solver monitor which can show various metrics in real time, such as current number of iterations, current residue of the approximate solution, etc. The solver monitor in TNL runs in a separate thread and it refreshes the status of the solver with a configurable refresh rate (once per 500 ms by default). The use of the solver monitor is demonstrated in the following example.
1#include <iostream>
2#include <memory>
3#include <TNL/Matrices/SparseMatrix.h>
4#include <TNL/Devices/Sequential.h>
5#include <TNL/Devices/Cuda.h>
6#include <TNL/Solvers/Linear/Jacobi.h>
7
8template< typename Device >
9void
10iterativeLinearSolverExample()
11{
12
13
14
15
16
17
18
19
20
23 const int size( 5 );
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 );
33 row.setElement( 1, rowIdx + 1, -1 );
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 );
37 row.setElement( 1, rowIdx, 2.5 );
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 );
41 row.setElement( 1, rowIdx, 2.5 );
42 row.setElement( 2, rowIdx + 1, -1.0 );
43 }
44 };
45
46
47
48
49 matrix_ptr->forAllRows( f );
51
52
53
54
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
60
61
62
63
65 LinearSolver solver;
67 solver.setOmega( 0.0005 );
68
69
70
71
73 IterativeSolverMonitorType monitor;
75 monitor.setRefreshRate( 10 );
76 monitor.setVerbose( 1 );
77 monitor.setStage( "Jacobi stage:" );
78 solver.setSolverMonitor( monitor );
79 solver.setConvergenceResidue( 1.0e-6 );
80 solver.solve( b, x );
81 monitor.stopMainLoop();
83}
84
85int
86main( int argc, char* argv[] )
87{
88 std::cout <<
"Solving linear system on host:\n";
89 iterativeLinearSolverExample< TNL::Devices::Sequential >();
90
91#ifdef __CUDACC__
92 std::cout <<
"Solving linear system on CUDA device:\n";
93 iterativeLinearSolverExample< TNL::Devices::Cuda >();
94#endif
95}
Iterative solver of linear systems based on the Jacobi method.
Definition Jacobi.h:21
A RAII wrapper for launching the SolverMonitor's main loop in a separate thread.
Definition SolverMonitor.h:142
First, we set up the same linear system as in the previous example, we create an instance of the Jacobi solver and we pass the matrix of the linear system to the solver. Then, we set the relaxation parameter \( \omega \) of the Jacobi solver to 0.0005. The reason is to artificially slow down the convergence, because we want to see some iterations in this example. Next, we create an instance of the solver monitor and a special thread for the monitor (an instance of the TNL::Solvers::SolverMonitorThread class). We use the following methods to configure the solver monitor:
Next, we call TNL::Solvers::IterativeSolver::setSolverMonitor to connect the solver with the monitor and we set the convergence criterion based on the relative residue. Finally, we start the solver using the TNL::Solvers::Linear::Jacobi::solve method and when the solver finishes, we stop the monitor using TNL::Solvers::SolverMonitor::stopMainLoop.
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: 21451 RES: 0.026768
Jacobi stage: ITER: 43029 RES: 0.00097345
Jacobi stage: ITER: 64599 RES: 3.5444e-05
Jacobi stage: ITER: 85993 RES: 1.3251e-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 ]
␛[2K
Jacobi stage: ITER: 6775 RES: 0.2561
Jacobi stage: ITER: 7103 RES: 0.24334
Jacobi stage: ITER: 7437 RES: 0.23097
Jacobi stage: ITER: 7767 RES: 0.21952
Jacobi stage: ITER: 8099 RES: 0.20853
Jacobi stage: ITER: 8453 RES: 0.19738
Jacobi stage: ITER: 8783 RES: 0.18764
Jacobi stage: ITER: 9115 RES: 0.17828
Jacobi stage: ITER: 9427 RES: 0.16991
Jacobi stage: ITER: 9747 RES: 0.16174
Jacobi stage: ITER: 10067 RES: 0.15397
Jacobi stage: ITER: 10331 RES: 0.14784
Jacobi stage: ITER: 10599 RES: 0.14187
Jacobi stage: ITER: 10845 RES: 0.13657
Jacobi stage: ITER: 11049 RES: 0.13235
Jacobi stage: ITER: 11181 RES: 0.12969
Jacobi stage: ITER: 11341 RES: 0.12654
Jacobi stage: ITER: 11699 RES: 0.1198
Jacobi stage: ITER: 11939 RES: 0.11547
Jacobi stage: ITER: 12151 RES: 0.11176
Jacobi stage: ITER: 12399 RES: 0.10759
Jacobi stage: ITER: 12607 RES: 0.1042
Jacobi stage: ITER: 12843 RES: 0.10049
Jacobi stage: ITER: 13029 RES: 0.09763
Jacobi stage: ITER: 13219 RES: 0.094851
Jacobi stage: ITER: 13419 RES: 0.091981
Jacobi stage: ITER: 13739 RES: 0.087568
Jacobi stage: ITER: 14071 RES: 0.083214
Jacobi stage: ITER: 14405 RES: 0.079028
Jacobi stage: ITER: 14739 RES: 0.075099
Jacobi stage: ITER: 15079 RES: 0.071277
Jacobi stage: ITER: 15399 RES: 0.067858
Jacobi stage: ITER: 15741 RES: 0.064366
Jacobi stage: ITER: 16067 RES: 0.061241
Jacobi stage: ITER: 16401 RES: 0.05816
Jacobi stage: ITER: 16711 RES: 0.055473
Jacobi stage: ITER: 17011 RES: 0.052974
Jacobi stage: ITER: 17335 RES: 0.050402
Jacobi stage: ITER: 17659 RES: 0.047955
Jacobi stage: ITER: 17999 RES: 0.045515
Jacobi stage: ITER: 18335 RES: 0.043226
Jacobi stage: ITER: 18663 RES: 0.041102
Jacobi stage: ITER: 18975 RES: 0.039179
Jacobi stage: ITER: 19307 RES: 0.037231
Jacobi stage: ITER: 19643 RES: 0.035358
Jacobi stage: ITER: 19959 RES: 0.033683
Jacobi stage: ITER: 20285 RES: 0.032028
Jacobi stage: ITER: 20603 RES: 0.03051
Jacobi stage: ITER: 20925 RES: 0.029029
Jacobi stage: ITER: 21253 RES: 0.027603
Jacobi stage: ITER: 21579 RES: 0.026263
Jacobi stage: ITER: 21915 RES: 0.024942
Jacobi stage: ITER: 22243 RES: 0.023716
Jacobi stage: ITER: 22571 RES: 0.022551
Jacobi stage: ITER: 22869 RES: 0.021535
Jacobi stage: ITER: 23203 RES: 0.020465
Jacobi stage: ITER: 23519 RES: 0.019495
Jacobi stage: ITER: 23841 RES: 0.018549
Jacobi stage: ITER: 24161 RES: 0.017659
Jacobi stage: ITER: 24495 RES: 0.016781
Jacobi stage: ITER: 24833 RES: 0.015927
Jacobi stage: ITER: 25139 RES: 0.015201
Jacobi stage: ITER: 25465 RES: 0.014454
Jacobi stage: ITER: 25791 RES: 0.013752
Jacobi stage: ITER: 26123 RES: 0.013068
Jacobi stage: ITER: 26443 RES: 0.012441
Jacobi stage: ITER: 26775 RES: 0.011823
Jacobi stage: ITER: 27107 RES: 0.011235
Jacobi stage: ITER: 27447 RES: 0.010663
Jacobi stage: ITER: 27781 RES: 0.010127
Jacobi stage: ITER: 28107 RES: 0.0096354
Jacobi stage: ITER: 28403 RES: 0.0092071
Jacobi stage: ITER: 28709 RES: 0.0087817
Jacobi stage: ITER: 29035 RES: 0.0083553
Jacobi stage: ITER: 29369 RES: 0.007935
Jacobi stage: ITER: 29713 RES: 0.0075266
Jacobi stage: ITER: 30045 RES: 0.0071524
Jacobi stage: ITER: 30357 RES: 0.0068178
Jacobi stage: ITER: 30683 RES: 0.0064868
Jacobi stage: ITER: 30995 RES: 0.0061832
Jacobi stage: ITER: 31331 RES: 0.0058722
Jacobi stage: ITER: 31651 RES: 0.0055905
Jacobi stage: ITER: 31987 RES: 0.0053093
Jacobi stage: ITER: 32333 RES: 0.005033
Jacobi stage: ITER: 32667 RES: 0.0047828
Jacobi stage: ITER: 32999 RES: 0.004545
Jacobi stage: ITER: 33333 RES: 0.0043164
Jacobi stage: ITER: 33667 RES: 0.0041018
Jacobi stage: ITER: 33999 RES: 0.0038978
Jacobi stage: ITER: 34333 RES: 0.0037018
Jacobi stage: ITER: 34667 RES: 0.0035177
Jacobi stage: ITER: 35001 RES: 0.0033408
Jacobi stage: ITER: 35335 RES: 0.0031747
Jacobi stage: ITER: 35667 RES: 0.0030168
Jacobi stage: ITER: 35999 RES: 0.0028669
Jacobi stage: ITER: 36333 RES: 0.0027227
Jacobi stage: ITER: 36667 RES: 0.0025873
Jacobi stage: ITER: 36999 RES: 0.0024587
Jacobi stage: ITER: 37333 RES: 0.002335
Jacobi stage: ITER: 37667 RES: 0.0022189
Jacobi stage: ITER: 37999 RES: 0.0021086
Jacobi stage: ITER: 38333 RES: 0.0020025
Jacobi stage: ITER: 38667 RES: 0.001903
Jacobi stage: ITER: 38999 RES: 0.0018083
Jacobi stage: ITER: 39333 RES: 0.0017174
Jacobi stage: ITER: 39667 RES: 0.001632
Jacobi stage: ITER: 39999 RES: 0.0015509
Jacobi stage: ITER: 40333 RES: 0.0014729
Jacobi stage: ITER: 40667 RES: 0.0013996
Jacobi stage: ITER: 40999 RES: 0.00133
Jacobi stage: ITER: 41331 RES: 0.0012639
Jacobi stage: ITER: 41665 RES: 0.0012003
Jacobi stage: ITER: 41999 RES: 0.0011407
Jacobi stage: ITER: 42331 RES: 0.0010839
Jacobi stage: ITER: 42665 RES: 0.0010294
Jacobi stage: ITER: 42999 RES: 0.00097825
Jacobi stage: ITER: 43331 RES: 0.00092961
Jacobi stage: ITER: 43665 RES: 0.00088285
Jacobi stage: ITER: 43999 RES: 0.00083896
Jacobi stage: ITER: 44331 RES: 0.00079725
Jacobi stage: ITER: 44665 RES: 0.00075714
Jacobi stage: ITER: 44999 RES: 0.0007195
Jacobi stage: ITER: 45331 RES: 0.00068373
Jacobi stage: ITER: 45663 RES: 0.00064974
Jacobi stage: ITER: 45997 RES: 0.00061705
Jacobi stage: ITER: 46331 RES: 0.00058638
Jacobi stage: ITER: 46663 RES: 0.00055722
Jacobi stage: ITER: 46997 RES: 0.00052919
Jacobi stage: ITER: 47331 RES: 0.00050288
Jacobi stage: ITER: 47663 RES: 0.00047788
Jacobi stage: ITER: 47997 RES: 0.00045384
Jacobi stage: ITER: 48331 RES: 0.00043128
Jacobi stage: ITER: 48663 RES: 0.00040984
Jacobi stage: ITER: 48997 RES: 0.00038922
Jacobi stage: ITER: 49331 RES: 0.00036987
Jacobi stage: ITER: 49663 RES: 0.00035148
Jacobi stage: ITER: 49995 RES: 0.00033401
Jacobi stage: ITER: 50329 RES: 0.00031721
Jacobi stage: ITER: 50663 RES: 0.00030144
Jacobi stage: ITER: 50995 RES: 0.00028645
Jacobi stage: ITER: 51329 RES: 0.00027204
Jacobi stage: ITER: 51663 RES: 0.00025852
Jacobi stage: ITER: 51995 RES: 0.00024566
Jacobi stage: ITER: 52329 RES: 0.00023331
Jacobi stage: ITER: 52663 RES: 0.00022171
Jacobi stage: ITER: 52995 RES: 0.00021068
Jacobi stage: ITER: 53329 RES: 0.00020009
Jacobi stage: ITER: 53663 RES: 0.00019014
Jacobi stage: ITER: 53995 RES: 0.00018069
Jacobi stage: ITER: 54329 RES: 0.0001716
Jacobi stage: ITER: 54663 RES: 0.00016307
Jacobi stage: ITER: 54995 RES: 0.00015496
Jacobi stage: ITER: 55327 RES: 0.00014725
Jacobi stage: ITER: 55661 RES: 0.00013985
Jacobi stage: ITER: 55995 RES: 0.00013289
Jacobi stage: ITER: 56327 RES: 0.00012629
Jacobi stage: ITER: 56661 RES: 0.00011993
Jacobi stage: ITER: 56995 RES: 0.00011397
Jacobi stage: ITER: 57327 RES: 0.00010831
Jacobi stage: ITER: 57661 RES: 0.00010286
Jacobi stage: ITER: 57995 RES: 9.7744e-05
Jacobi stage: ITER: 58327 RES: 9.2884e-05
Jacobi stage: ITER: 58659 RES: 8.8266e-05
Jacobi stage: ITER: 58995 RES: 8.3826e-05
Jacobi stage: ITER: 59327 RES: 7.9659e-05
Jacobi stage: ITER: 59659 RES: 7.5698e-05
Jacobi stage: ITER: 59993 RES: 7.1891e-05
Jacobi stage: ITER: 60327 RES: 6.8317e-05
Jacobi stage: ITER: 60755 RES: 6.397e-05
Jacobi stage: ITER: 61089 RES: 6.0752e-05
Jacobi stage: ITER: 61423 RES: 5.7732e-05
Jacobi stage: ITER: 61755 RES: 5.4861e-05
Jacobi stage: ITER: 62089 RES: 5.2102e-05
Jacobi stage: ITER: 62423 RES: 4.9511e-05
Jacobi stage: ITER: 62755 RES: 4.705e-05
Jacobi stage: ITER: 63089 RES: 4.4683e-05
Jacobi stage: ITER: 63423 RES: 4.2462e-05
Jacobi stage: ITER: 63755 RES: 4.0351e-05
Jacobi stage: ITER: 64089 RES: 3.8321e-05
Jacobi stage: ITER: 64423 RES: 3.6416e-05
Jacobi stage: ITER: 64759 RES: 3.4584e-05
Jacobi stage: ITER: 65091 RES: 3.2865e-05
Jacobi stage: ITER: 65425 RES: 3.1211e-05
Jacobi stage: ITER: 65759 RES: 2.966e-05
Jacobi stage: ITER: 66091 RES: 2.8185e-05
Jacobi stage: ITER: 66425 RES: 2.6767e-05
Jacobi stage: ITER: 66759 RES: 2.5437e-05
Jacobi stage: ITER: 67091 RES: 2.4172e-05
Jacobi stage: ITER: 67425 RES: 2.2956e-05
Jacobi stage: ITER: 67759 RES: 2.1815e-05
Jacobi stage: ITER: 68091 RES: 2.073e-05
Jacobi stage: ITER: 68425 RES: 1.9687e-05
Jacobi stage: ITER: 68759 RES: 1.8709e-05
Jacobi stage: ITER: 69091 RES: 1.7778e-05
Jacobi stage: ITER: 69425 RES: 1.6884e-05
Jacobi stage: ITER: 69759 RES: 1.6045e-05
Jacobi stage: ITER: 70091 RES: 1.5247e-05
Jacobi stage: ITER: 70425 RES: 1.448e-05
Jacobi stage: ITER: 70757 RES: 1.376e-05
Jacobi stage: ITER: 71091 RES: 1.3076e-05
Jacobi stage: ITER: 71423 RES: 1.2426e-05
Jacobi stage: ITER: 71757 RES: 1.1801e-05
Jacobi stage: ITER: 72091 RES: 1.1214e-05
Jacobi stage: ITER: 72423 RES: 1.0657e-05
Jacobi stage: ITER: 72757 RES: 1.0121e-05
Jacobi stage: ITER: 73091 RES: 9.6175e-06
Jacobi stage: ITER: 73423 RES: 9.1393e-06
Jacobi stage: ITER: 73757 RES: 8.6796e-06
Jacobi stage: ITER: 74089 RES: 8.2481e-06
Jacobi stage: ITER: 74423 RES: 7.838e-06
Jacobi stage: ITER: 74755 RES: 7.4483e-06
Jacobi stage: ITER: 75089 RES: 7.0737e-06
Jacobi stage: ITER: 75423 RES: 6.722e-06
Jacobi stage: ITER: 75755 RES: 6.3878e-06
Jacobi stage: ITER: 76089 RES: 6.0665e-06
Jacobi stage: ITER: 76423 RES: 5.7649e-06
Jacobi stage: ITER: 76755 RES: 5.4782e-06
Jacobi stage: ITER: 77089 RES: 5.2027e-06
Jacobi stage: ITER: 77423 RES: 4.944e-06
Jacobi stage: ITER: 77755 RES: 4.6982e-06
Jacobi stage: ITER: 78087 RES: 4.4646e-06
Jacobi stage: ITER: 78421 RES: 4.2401e-06
Jacobi stage: ITER: 78755 RES: 4.0293e-06
Jacobi stage: ITER: 79087 RES: 3.8289e-06
Jacobi stage: ITER: 79421 RES: 3.6363e-06
Jacobi stage: ITER: 79755 RES: 3.4555e-06
Jacobi stage: ITER: 80087 RES: 3.2837e-06
Jacobi stage: ITER: 80421 RES: 3.1186e-06
Jacobi stage: ITER: 80755 RES: 2.9635e-06
Jacobi stage: ITER: 81087 RES: 2.8162e-06
Jacobi stage: ITER: 81421 RES: 2.6745e-06
Jacobi stage: ITER: 81755 RES: 2.5416e-06
Jacobi stage: ITER: 82087 RES: 2.4152e-06
Jacobi stage: ITER: 82419 RES: 2.2951e-06
Jacobi stage: ITER: 82753 RES: 2.1797e-06
Jacobi stage: ITER: 83087 RES: 2.0713e-06
Jacobi stage: ITER: 83419 RES: 1.9683e-06
Jacobi stage: ITER: 83753 RES: 1.8693e-06
Jacobi stage: ITER: 84087 RES: 1.7764e-06
Jacobi stage: ITER: 84419 RES: 1.6881e-06
Jacobi stage: ITER: 84753 RES: 1.6032e-06
Jacobi stage: ITER: 85087 RES: 1.5234e-06
Jacobi stage: ITER: 85419 RES: 1.4477e-06
Jacobi stage: ITER: 85753 RES: 1.3749e-06
Jacobi stage: ITER: 86085 RES: 1.3065e-06
Jacobi stage: ITER: 86419 RES: 1.2416e-06
Jacobi stage: ITER: 86751 RES: 1.1798e-06
Jacobi stage: ITER: 87085 RES: 1.1205e-06
Jacobi stage: ITER: 87419 RES: 1.0648e-06
Jacobi stage: ITER: 87751 RES: 1.0118e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]
The monitoring of the solver can be improved by time elapsed since the beginning of the computation as demonstrated in the following example:
1#include <iostream>
2#include <memory>
3#include <TNL/Timer.h>
4#include <TNL/Matrices/SparseMatrix.h>
5#include <TNL/Devices/Sequential.h>
6#include <TNL/Devices/Cuda.h>
7#include <TNL/Solvers/Linear/Jacobi.h>
8
9template< typename Device >
10void
11iterativeLinearSolverExample()
12{
13
14
15
16
17
18
19
20
21
24 const int size( 5 );
26 matrix_ptr->setDimensions( size, size );
27 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
28
30 {
31 const int rowIdx = row.getRowIndex();
32 if( rowIdx == 0 ) {
33 row.setElement( 0, rowIdx, 2.5 );
34 row.setElement( 1, rowIdx + 1, -1 );
35 }
36 else if( rowIdx == size - 1 ) {
37 row.setElement( 0, rowIdx - 1, -1.0 );
38 row.setElement( 1, rowIdx, 2.5 );
39 }
40 else {
41 row.setElement( 0, rowIdx - 1, -1.0 );
42 row.setElement( 1, rowIdx, 2.5 );
43 row.setElement( 2, rowIdx + 1, -1.0 );
44 }
45 };
46
47
48
49
50 matrix_ptr->forAllRows( f );
52
53
54
55
56 Vector x( size, 1.0 );
57 Vector b( size );
58 matrix_ptr->vectorProduct( x, b );
59 x = 0.0;
61
62
63
64
66 LinearSolver solver;
68 solver.setOmega( 0.0005 );
69
70
71
72
74 IterativeSolverMonitorType monitor;
76 monitor.setRefreshRate( 10 );
77 monitor.setVerbose( 1 );
78 monitor.setStage( "Jacobi stage:" );
80 monitor.setTimer( timer );
82 solver.setSolverMonitor( monitor );
83 solver.setConvergenceResidue( 1.0e-6 );
84 solver.solve( b, x );
85 monitor.stopMainLoop();
87}
88
89int
90main( int argc, char* argv[] )
91{
92 std::cout <<
"Solving linear system on host:\n";
93 iterativeLinearSolverExample< TNL::Devices::Sequential >();
94
95#ifdef __CUDACC__
96 std::cout <<
"Solving linear system on CUDA device:\n";
97 iterativeLinearSolverExample< TNL::Devices::Cuda >();
98#endif
99}
Class for real time, CPU time and CPU cycles measuring.
Definition Timer.h:25
void start()
Starts timer.
The only changes are around the lines where we create an instance of TNL::Timer, connect it with the monitor using TNL::Solvers::SolverMonitor::setTimer and start the timer with TNL::Timer::start.
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:2.0946e-
ELA: 0.10014 Jacobi stage: ITER: 21995 RES: 0.024637
ELA: 0.20055 Jacobi stage: ITER: 44035 RES: 0.00083433
ELA: 0.30066 Jacobi stage: ITER: 66119 RES: 2.8064e-05
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:2.5237e-
ELA: 0.10147 Jacobi stage: ITER: 523 RES: 0.84514
ELA: 0.20158 Jacobi stage: ITER: 1023 RES: 0.73403
ELA: 0.30169 Jacobi stage: ITER: 1515 RES: 0.64925
ELA: 0.40181 Jacobi stage: ITER: 1863 RES: 0.59981
ELA: 0.50192 Jacobi stage: ITER: 2193 RES: 0.5588
ELA: 0.60203 Jacobi stage: ITER: 2519 RES: 0.52341
ELA: 0.70214 Jacobi stage: ITER: 2855 RES: 0.49061
ELA: 0.80225 Jacobi stage: ITER: 3183 RES: 0.46174
ELA: 0.90235 Jacobi stage: ITER: 3515 RES: 0.43514
ELA: 1.0025 Jacobi stage: ITER: 3801 RES: 0.41387
ELA: 1.1026 Jacobi stage: ITER: 4131 RES: 0.39141
ELA: 1.2027 Jacobi stage: ITER: 4461 RES: 0.37032
ELA: 1.3028 Jacobi stage: ITER: 4771 RES: 0.35205
ELA: 1.4029 Jacobi stage: ITER: 5087 RES: 0.33446
ELA: 1.503 Jacobi stage: ITER: 5411 RES: 0.31751
ELA: 1.6031 Jacobi stage: ITER: 5755 RES: 0.3006
ELA: 1.7032 Jacobi stage: ITER: 5937 RES: 0.29198
ELA: 1.8033 Jacobi stage: ITER: 6125 RES: 0.28345
ELA: 1.9034 Jacobi stage: ITER: 6791 RES: 0.25546
ELA: 2.0035 Jacobi stage: ITER: 7037 RES: 0.24578
ELA: 2.1037 Jacobi stage: ITER: 7281 RES: 0.23663
ELA: 2.2038 Jacobi stage: ITER: 7463 RES: 0.23011
ELA: 2.3039 Jacobi stage: ITER: 7709 RES: 0.22143
ELA: 2.404 Jacobi stage: ITER: 7921 RES: 0.21429
ELA: 2.5041 Jacobi stage: ITER: 8155 RES: 0.20674
ELA: 2.6042 Jacobi stage: ITER: 8375 RES: 0.19983
ELA: 2.7043 Jacobi stage: ITER: 8611 RES: 0.19268
ELA: 2.8044 Jacobi stage: ITER: 8803 RES: 0.18706
ELA: 2.9045 Jacobi stage: ITER: 8993 RES: 0.1816
ELA: 3.0065 Jacobi stage: ITER: 9189 RES: 0.1762
ELA: 3.1066 Jacobi stage: ITER: 9487 RES: 0.16835
ELA: 3.2067 Jacobi stage: ITER: 9817 RES: 0.15996
ELA: 3.3068 Jacobi stage: ITER: 10147 RES: 0.15209
ELA: 3.4069 Jacobi stage: ITER: 10483 RES: 0.14443
ELA: 3.507 Jacobi stage: ITER: 10821 RES: 0.13707
ELA: 3.6071 Jacobi stage: ITER: 11145 RES: 0.13041
ELA: 3.7072 Jacobi stage: ITER: 11475 RES: 0.124
ELA: 3.8073 Jacobi stage: ITER: 11801 RES: 0.1179
ELA: 3.9074 Jacobi stage: ITER: 12133 RES: 0.11204
ELA: 4.0075 Jacobi stage: ITER: 12441 RES: 0.10686
ELA: 4.1076 Jacobi stage: ITER: 12753 RES: 0.10186
ELA: 4.2084 Jacobi stage: ITER: 13059 RES: 0.097211
ELA: 4.3086 Jacobi stage: ITER: 13383 RES: 0.092491
ELA: 4.4087 Jacobi stage: ITER: 13715 RES: 0.087892
ELA: 4.5088 Jacobi stage: ITER: 14051 RES: 0.08347
ELA: 4.6089 Jacobi stage: ITER: 14381 RES: 0.07932
ELA: 4.709 Jacobi stage: ITER: 14695 RES: 0.075608
ELA: 4.8091 Jacobi stage: ITER: 15021 RES: 0.071893
ELA: 4.9092 Jacobi stage: ITER: 15357 RES: 0.068276
ELA: 5.0093 Jacobi stage: ITER: 15681 RES: 0.064962
ELA: 5.1105 Jacobi stage: ITER: 16003 RES: 0.061846
ELA: 5.2106 Jacobi stage: ITER: 16329 RES: 0.058807
ELA: 5.3107 Jacobi stage: ITER: 16641 RES: 0.056055
ELA: 5.4108 Jacobi stage: ITER: 16971 RES: 0.053301
ELA: 5.5115 Jacobi stage: ITER: 17295 RES: 0.050713
ELA: 5.6116 Jacobi stage: ITER: 17631 RES: 0.048162
ELA: 5.7146 Jacobi stage: ITER: 17977 RES: 0.045655
ELA: 5.8147 Jacobi stage: ITER: 18301 RES: 0.043439
ELA: 5.9153 Jacobi stage: ITER: 18597 RES: 0.041508
ELA: 6.0154 Jacobi stage: ITER: 18923 RES: 0.039493
ELA: 6.1155 Jacobi stage: ITER: 19243 RES: 0.037599
ELA: 6.2165 Jacobi stage: ITER: 19565 RES: 0.035773
ELA: 6.3166 Jacobi stage: ITER: 19887 RES: 0.034057
ELA: 6.4167 Jacobi stage: ITER: 20215 RES: 0.032384
ELA: 6.5168 Jacobi stage: ITER: 20551 RES: 0.030736
ELA: 6.6169 Jacobi stage: ITER: 20861 RES: 0.029316
ELA: 6.7175 Jacobi stage: ITER: 21183 RES: 0.02791
ELA: 6.8176 Jacobi stage: ITER: 21507 RES: 0.026555
ELA: 6.9177 Jacobi stage: ITER: 21843 RES: 0.025219
ELA: 7.0178 Jacobi stage: ITER: 22155 RES: 0.024039
ELA: 7.1179 Jacobi stage: ITER: 22487 RES: 0.022844
ELA: 7.218 Jacobi stage: ITER: 22819 RES: 0.021708
ELA: 7.3181 Jacobi stage: ITER: 23155 RES: 0.020616
ELA: 7.4182 Jacobi stage: ITER: 23491 RES: 0.019579
ELA: 7.5195 Jacobi stage: ITER: 23817 RES: 0.018617
ELA: 7.6215 Jacobi stage: ITER: 24139 RES: 0.017724
ELA: 7.7216 Jacobi stage: ITER: 24423 RES: 0.016968
ELA: 7.8217 Jacobi stage: ITER: 24747 RES: 0.016144
ELA: 7.9218 Jacobi stage: ITER: 25075 RES: 0.015351
ELA: 8.0219 Jacobi stage: ITER: 25413 RES: 0.01457
ELA: 8.1225 Jacobi stage: ITER: 25755 RES: 0.013828
ELA: 8.2226 Jacobi stage: ITER: 26069 RES: 0.013173
ELA: 8.3227 Jacobi stage: ITER: 26381 RES: 0.012557
ELA: 8.4228 Jacobi stage: ITER: 26695 RES: 0.011969
ELA: 8.5255 Jacobi stage: ITER: 27033 RES: 0.01136
ELA: 8.6265 Jacobi stage: ITER: 27351 RES: 0.010822
ELA: 8.7266 Jacobi stage: ITER: 27687 RES: 0.010277
ELA: 8.8267 Jacobi stage: ITER: 28033 RES: 0.0097425
ELA: 8.9275 Jacobi stage: ITER: 28367 RES: 0.0092582
ELA: 9.0276 Jacobi stage: ITER: 28701 RES: 0.0087925
ELA: 9.1277 Jacobi stage: ITER: 29035 RES: 0.0083553
ELA: 9.2278 Jacobi stage: ITER: 29367 RES: 0.0079399
ELA: 9.3279 Jacobi stage: ITER: 29701 RES: 0.0075405
ELA: 9.4285 Jacobi stage: ITER: 30035 RES: 0.0071656
ELA: 9.5286 Jacobi stage: ITER: 30371 RES: 0.0068052
ELA: 9.6287 Jacobi stage: ITER: 30703 RES: 0.0064669
ELA: 9.7288 Jacobi stage: ITER: 31037 RES: 0.0061416
ELA: 9.8289 Jacobi stage: ITER: 31371 RES: 0.0058362
ELA: 9.929 Jacobi stage: ITER: 31703 RES: 0.0055461
ELA: 10.029 Jacobi stage: ITER: 32035 RES: 0.0052703
ELA: 10.129 Jacobi stage: ITER: 32369 RES: 0.0050052
ELA: 10.229 Jacobi stage: ITER: 32703 RES: 0.0047564
ELA: 10.329 Jacobi stage: ITER: 33035 RES: 0.0045199
ELA: 10.429 Jacobi stage: ITER: 33369 RES: 0.0042926
ELA: 10.53 Jacobi stage: ITER: 33703 RES: 0.0040791
ELA: 10.63 Jacobi stage: ITER: 34035 RES: 0.0038763
ELA: 10.73 Jacobi stage: ITER: 34369 RES: 0.0036814
ELA: 10.83 Jacobi stage: ITER: 34703 RES: 0.0034983
ELA: 10.93 Jacobi stage: ITER: 35035 RES: 0.0033244
ELA: 11.03 Jacobi stage: ITER: 35369 RES: 0.0031572
ELA: 11.13 Jacobi stage: ITER: 35703 RES: 0.0030002
ELA: 11.23 Jacobi stage: ITER: 36035 RES: 0.002851
ELA: 11.331 Jacobi stage: ITER: 36369 RES: 0.0027076
ELA: 11.431 Jacobi stage: ITER: 36703 RES: 0.002573
ELA: 11.531 Jacobi stage: ITER: 37035 RES: 0.0024451
ELA: 11.631 Jacobi stage: ITER: 37369 RES: 0.0023221
ELA: 11.731 Jacobi stage: ITER: 37703 RES: 0.0022067
ELA: 11.831 Jacobi stage: ITER: 38035 RES: 0.002097
ELA: 11.931 Jacobi stage: ITER: 38369 RES: 0.0019915
ELA: 12.031 Jacobi stage: ITER: 38703 RES: 0.0018925
ELA: 12.131 Jacobi stage: ITER: 39035 RES: 0.0017984
ELA: 12.232 Jacobi stage: ITER: 39369 RES: 0.0017079
ELA: 12.332 Jacobi stage: ITER: 39703 RES: 0.001623
ELA: 12.432 Jacobi stage: ITER: 40035 RES: 0.0015423
ELA: 12.532 Jacobi stage: ITER: 40367 RES: 0.0014656
ELA: 12.632 Jacobi stage: ITER: 40703 RES: 0.0013919
ELA: 12.732 Jacobi stage: ITER: 41035 RES: 0.0013227
ELA: 12.832 Jacobi stage: ITER: 41367 RES: 0.0012569
ELA: 12.932 Jacobi stage: ITER: 41701 RES: 0.0011937
ELA: 13.032 Jacobi stage: ITER: 42035 RES: 0.0011344
ELA: 13.133 Jacobi stage: ITER: 42367 RES: 0.001078
ELA: 13.233 Jacobi stage: ITER: 42701 RES: 0.0010237
ELA: 13.333 Jacobi stage: ITER: 43037 RES: 0.00097225
ELA: 13.434 Jacobi stage: ITER: 43371 RES: 0.00092392
ELA: 13.534 Jacobi stage: ITER: 43703 RES: 0.00087798
ELA: 13.634 Jacobi stage: ITER: 44037 RES: 0.00083382
ELA: 13.734 Jacobi stage: ITER: 44371 RES: 0.00079236
ELA: 13.834 Jacobi stage: ITER: 44703 RES: 0.00075297
ELA: 13.934 Jacobi stage: ITER: 45037 RES: 0.00071509
ELA: 14.034 Jacobi stage: ITER: 45371 RES: 0.00067954
ELA: 14.134 Jacobi stage: ITER: 45703 RES: 0.00064576
ELA: 14.234 Jacobi stage: ITER: 46037 RES: 0.00061327
ELA: 14.335 Jacobi stage: ITER: 46371 RES: 0.00058278
ELA: 14.435 Jacobi stage: ITER: 46703 RES: 0.00055381
ELA: 14.535 Jacobi stage: ITER: 47037 RES: 0.00052595
ELA: 14.635 Jacobi stage: ITER: 47371 RES: 0.0004998
ELA: 14.735 Jacobi stage: ITER: 47703 RES: 0.00047495
ELA: 14.835 Jacobi stage: ITER: 48037 RES: 0.00045106
ELA: 14.935 Jacobi stage: ITER: 48371 RES: 0.00042864
ELA: 15.035 Jacobi stage: ITER: 48703 RES: 0.00040733
ELA: 15.135 Jacobi stage: ITER: 49035 RES: 0.00038708
ELA: 15.236 Jacobi stage: ITER: 49369 RES: 0.00036761
ELA: 15.336 Jacobi stage: ITER: 49703 RES: 0.00034933
ELA: 15.436 Jacobi stage: ITER: 50035 RES: 0.00033196
ELA: 15.536 Jacobi stage: ITER: 50369 RES: 0.00031526
ELA: 15.636 Jacobi stage: ITER: 50703 RES: 0.00029959
ELA: 15.736 Jacobi stage: ITER: 51035 RES: 0.00028469
ELA: 15.836 Jacobi stage: ITER: 51367 RES: 0.00027054
ELA: 15.936 Jacobi stage: ITER: 51701 RES: 0.00025693
ELA: 16.037 Jacobi stage: ITER: 52035 RES: 0.00024416
ELA: 16.137 Jacobi stage: ITER: 52367 RES: 0.00023202
ELA: 16.237 Jacobi stage: ITER: 52701 RES: 0.00022035
ELA: 16.337 Jacobi stage: ITER: 53035 RES: 0.00020939
ELA: 16.437 Jacobi stage: ITER: 53367 RES: 0.00019898
ELA: 16.537 Jacobi stage: ITER: 53701 RES: 0.00018897
ELA: 16.637 Jacobi stage: ITER: 54035 RES: 0.00017958
ELA: 16.737 Jacobi stage: ITER: 54367 RES: 0.00017065
ELA: 16.837 Jacobi stage: ITER: 54701 RES: 0.00016207
ELA: 16.938 Jacobi stage: ITER: 55035 RES: 0.00015401
ELA: 17.038 Jacobi stage: ITER: 55367 RES: 0.00014635
ELA: 17.138 Jacobi stage: ITER: 55701 RES: 0.00013899
ELA: 17.238 Jacobi stage: ITER: 56035 RES: 0.00013208
ELA: 17.391 Jacobi stage: ITER: 56543 RES: 0.00012217
ELA: 17.491 Jacobi stage: ITER: 56877 RES: 0.00011602
ELA: 17.592 Jacobi stage: ITER: 57209 RES: 0.00011025
ELA: 17.692 Jacobi stage: ITER: 57543 RES: 0.00010477
ELA: 17.792 Jacobi stage: ITER: 57875 RES: 9.9562e-05
ELA: 17.892 Jacobi stage: ITER: 58209 RES: 9.4554e-05
ELA: 17.992 Jacobi stage: ITER: 58543 RES: 8.9853e-05
ELA: 18.092 Jacobi stage: ITER: 58875 RES: 8.5386e-05
ELA: 18.192 Jacobi stage: ITER: 59209 RES: 8.1091e-05
ELA: 18.292 Jacobi stage: ITER: 59543 RES: 7.7059e-05
ELA: 18.392 Jacobi stage: ITER: 59875 RES: 7.3228e-05
ELA: 18.492 Jacobi stage: ITER: 60209 RES: 6.9545e-05
ELA: 18.593 Jacobi stage: ITER: 60543 RES: 6.6087e-05
ELA: 18.693 Jacobi stage: ITER: 60875 RES: 6.2801e-05
ELA: 18.793 Jacobi stage: ITER: 61209 RES: 5.9642e-05
ELA: 18.893 Jacobi stage: ITER: 61543 RES: 5.6677e-05
ELA: 18.993 Jacobi stage: ITER: 61875 RES: 5.3859e-05
ELA: 19.093 Jacobi stage: ITER: 62209 RES: 5.115e-05
ELA: 19.193 Jacobi stage: ITER: 62543 RES: 4.8607e-05
ELA: 19.293 Jacobi stage: ITER: 62875 RES: 4.6191e-05
ELA: 19.393 Jacobi stage: ITER: 63209 RES: 4.3867e-05
ELA: 19.493 Jacobi stage: ITER: 63541 RES: 4.1686e-05
ELA: 19.594 Jacobi stage: ITER: 63875 RES: 3.9614e-05
ELA: 19.694 Jacobi stage: ITER: 64207 RES: 3.7621e-05
ELA: 19.794 Jacobi stage: ITER: 64543 RES: 3.5751e-05
ELA: 19.894 Jacobi stage: ITER: 64875 RES: 3.3973e-05
ELA: 19.994 Jacobi stage: ITER: 65207 RES: 3.2284e-05
ELA: 20.094 Jacobi stage: ITER: 65543 RES: 3.066e-05
ELA: 20.194 Jacobi stage: ITER: 65875 RES: 2.9136e-05
ELA: 20.294 Jacobi stage: ITER: 66207 RES: 2.7687e-05
ELA: 20.394 Jacobi stage: ITER: 66541 RES: 2.6295e-05
ELA: 20.495 Jacobi stage: ITER: 66875 RES: 2.4987e-05
ELA: 20.595 Jacobi stage: ITER: 67211 RES: 2.373e-05
ELA: 20.695 Jacobi stage: ITER: 67543 RES: 2.2551e-05
ELA: 20.795 Jacobi stage: ITER: 67875 RES: 2.1429e-05
ELA: 20.895 Jacobi stage: ITER: 68209 RES: 2.0352e-05
ELA: 20.995 Jacobi stage: ITER: 68543 RES: 1.934e-05
ELA: 21.095 Jacobi stage: ITER: 68875 RES: 1.8378e-05
ELA: 21.195 Jacobi stage: ITER: 69209 RES: 1.7454e-05
ELA: 21.295 Jacobi stage: ITER: 69543 RES: 1.6586e-05
ELA: 21.396 Jacobi stage: ITER: 69875 RES: 1.5761e-05
ELA: 21.496 Jacobi stage: ITER: 70209 RES: 1.4969e-05
ELA: 21.596 Jacobi stage: ITER: 70543 RES: 1.4224e-05
ELA: 21.696 Jacobi stage: ITER: 70875 RES: 1.3517e-05
ELA: 21.796 Jacobi stage: ITER: 71209 RES: 1.2837e-05
ELA: 21.896 Jacobi stage: ITER: 71543 RES: 1.2199e-05
ELA: 21.996 Jacobi stage: ITER: 71875 RES: 1.1593e-05
ELA: 22.096 Jacobi stage: ITER: 72209 RES: 1.1009e-05
ELA: 22.196 Jacobi stage: ITER: 72543 RES: 1.0462e-05
ELA: 22.297 Jacobi stage: ITER: 72875 RES: 9.9419e-06
ELA: 22.397 Jacobi stage: ITER: 73209 RES: 9.4418e-06
ELA: 22.497 Jacobi stage: ITER: 73543 RES: 8.9724e-06
ELA: 22.597 Jacobi stage: ITER: 73875 RES: 8.5263e-06
ELA: 22.697 Jacobi stage: ITER: 74209 RES: 8.0974e-06
ELA: 22.797 Jacobi stage: ITER: 74543 RES: 7.6948e-06
ELA: 22.897 Jacobi stage: ITER: 74875 RES: 7.3123e-06
ELA: 22.997 Jacobi stage: ITER: 75209 RES: 6.9445e-06
ELA: 23.097 Jacobi stage: ITER: 75543 RES: 6.5992e-06
ELA: 23.198 Jacobi stage: ITER: 75875 RES: 6.2711e-06
ELA: 23.298 Jacobi stage: ITER: 76209 RES: 5.9557e-06
ELA: 23.398 Jacobi stage: ITER: 76541 RES: 5.6596e-06
ELA: 23.498 Jacobi stage: ITER: 76875 RES: 5.3782e-06
ELA: 23.598 Jacobi stage: ITER: 77207 RES: 5.1108e-06
ELA: 23.698 Jacobi stage: ITER: 77541 RES: 4.8537e-06
ELA: 23.798 Jacobi stage: ITER: 77875 RES: 4.6124e-06
ELA: 23.898 Jacobi stage: ITER: 78207 RES: 4.3831e-06
ELA: 23.998 Jacobi stage: ITER: 78541 RES: 4.1626e-06
ELA: 24.099 Jacobi stage: ITER: 78875 RES: 3.9557e-06
ELA: 24.199 Jacobi stage: ITER: 79207 RES: 3.759e-06
ELA: 24.299 Jacobi stage: ITER: 79541 RES: 3.5699e-06
ELA: 24.399 Jacobi stage: ITER: 79875 RES: 3.3924e-06
ELA: 24.499 Jacobi stage: ITER: 80207 RES: 3.2238e-06
ELA: 24.599 Jacobi stage: ITER: 80541 RES: 3.0616e-06
ELA: 24.699 Jacobi stage: ITER: 80875 RES: 2.9094e-06
ELA: 24.799 Jacobi stage: ITER: 81207 RES: 2.7647e-06
ELA: 24.9 Jacobi stage: ITER: 81541 RES: 2.6257e-06
ELA: 25 Jacobi stage: ITER: 81875 RES: 2.4951e-06
ELA: 25.1 Jacobi stage: ITER: 82207 RES: 2.3711e-06
ELA: 25.2 Jacobi stage: ITER: 82541 RES: 2.2518e-06
ELA: 25.3 Jacobi stage: ITER: 82875 RES: 2.1399e-06
ELA: 25.4 Jacobi stage: ITER: 83207 RES: 2.0335e-06
ELA: 25.5 Jacobi stage: ITER: 83539 RES: 1.9324e-06
ELA: 25.6 Jacobi stage: ITER: 84027 RES: 1.7928e-06
ELA: 25.701 Jacobi stage: ITER: 84531 RES: 1.6593e-06
ELA: 25.801 Jacobi stage: ITER: 85033 RES: 1.5357e-06
ELA: 25.901 Jacobi stage: ITER: 85535 RES: 1.4221e-06
ELA: 26.001 Jacobi stage: ITER: 86035 RES: 1.317e-06
ELA: 26.101 Jacobi stage: ITER: 86537 RES: 1.2189e-06
ELA: 26.201 Jacobi stage: ITER: 87047 RES: 1.1274e-06
ELA: 26.301 Jacobi stage: ITER: 87549 RES: 1.0434e-06
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]
Setup with preconditioner
Preconditioners of iterative solvers can significantly improve the performance of the solver. In the case of the linear systems, they are used mainly with the Krylov subspace methods. Preconditioners cannot be used with the starionary methods (TNL::Solvers::Linear::Jacobi and TNL::Solvers::Linear::SOR). The following example shows how to setup an iterative solver of linear systems with preconditioning.
1#include <iostream>
2#include <memory>
3#include <TNL/Matrices/SparseMatrix.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6#include <TNL/Solvers/Linear/TFQMR.h>
7#include <TNL/Solvers/Linear/Preconditioners/Diagonal.h>
8
9template< typename Device >
10void
11iterativeLinearSolverExample()
12{
13
14
15
16
17
18
19
20
21
24 const int size( 5 );
26 matrix_ptr->setDimensions( size, size );
27 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
28
30 {
31 const int rowIdx = row.getRowIndex();
32 if( rowIdx == 0 ) {
33 row.setElement( 0, rowIdx, 2.5 );
34 row.setElement( 1, rowIdx + 1, -1 );
35 }
36 else if( rowIdx == size - 1 ) {
37 row.setElement( 0, rowIdx - 1, -1.0 );
38 row.setElement( 1, rowIdx, 2.5 );
39 }
40 else {
41 row.setElement( 0, rowIdx - 1, -1.0 );
42 row.setElement( 1, rowIdx, 2.5 );
43 row.setElement( 2, rowIdx + 1, -1.0 );
44 }
45 };
46
47
48
49
50 matrix_ptr->forAllRows( f );
52
53
54
55
56 Vector x( size, 1.0 );
57 Vector b( size );
58 matrix_ptr->vectorProduct( x, b );
59 x = 0.0;
61
62
63
64
68 preconditioner_ptr->update( matrix_ptr );
69 LinearSolver solver;
70 solver.setMatrix( matrix_ptr );
71 solver.setPreconditioner( preconditioner_ptr );
72 solver.setConvergenceResidue( 1.0e-6 );
73 solver.solve( b, x );
75}
76
77int
78main( int argc, char* argv[] )
79{
80 std::cout <<
"Solving linear system on host:\n";
81 iterativeLinearSolverExample< TNL::Devices::Sequential >();
82
83#ifdef __CUDACC__
84 std::cout <<
"Solving linear system on CUDA device:\n";
85 iterativeLinearSolverExample< TNL::Devices::Cuda >();
86#endif
87}
Diagonal (Jacobi) preconditioner for iterative solvers of linear systems.
Definition Diagonal.h:21
In this example, we solve the same problem as in all other examples in this section. The only differences concerning the preconditioner happen in the solver setup. Similarly to the matrix of the linear system, the preconditioner needs to be passed to the solver as a std::shared_ptr. When the preconditioner object is created, we have to initialize it using the update method, which has to be called everytime the matrix of the linear system changes. This is important, for example, when solving time-dependent PDEs, but it does not happen in this example. Finally, we need to connect the solver with the preconditioner using the setPreconditioner method.
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 = [ 1, 1, 1, 1, 1 ]
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 ]
Vector x = [ 1, 1, 1, 1, 1 ]
Choosing the solver and preconditioner type at runtime
When developing a numerical solver, one often has to search for a combination of various methods and algorithms that fit given requirements the best. To make this easier, TNL provides the functions TNL::Solvers::getLinearSolver and TNL::Solvers::getPreconditioner for selecting the linear solver and preconditioner at runtime. The following example shows how to use these functions:
1#include <iostream>
2#include <memory>
3#include <TNL/Matrices/SparseMatrix.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6#include <TNL/Solvers/LinearSolverTypeResolver.h>
7
8template< typename Device >
9void
10iterativeLinearSolverExample()
11{
12
13
14
15
16
17
18
19
20
23 const int size( 5 );
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 );
33 row.setElement( 1, rowIdx + 1, -1 );
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 );
37 row.setElement( 1, rowIdx, 2.5 );
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 );
41 row.setElement( 1, rowIdx, 2.5 );
42 row.setElement( 2, rowIdx + 1, -1.0 );
43 }
44 };
45
46
47
48
49 matrix_ptr->forAllRows( f );
51
52
53
54
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
60
61
62
63
66 preconditioner_ptr->update( matrix_ptr );
67 solver_ptr->setMatrix( matrix_ptr );
68 solver_ptr->setPreconditioner( preconditioner_ptr );
69 solver_ptr->setConvergenceResidue( 1.0e-6 );
70 solver_ptr->solve( b, x );
72}
73
74int
75main( int argc, char* argv[] )
76{
77 std::cout <<
"Solving linear system on host:\n";
78 iterativeLinearSolverExample< TNL::Devices::Sequential >();
79
80#ifdef __CUDACC__
81 std::cout <<
"Solving linear system on CUDA device:\n";
82 iterativeLinearSolverExample< TNL::Devices::Cuda >();
83#endif
84}
std::shared_ptr< Linear::Preconditioners::Preconditioner< MatrixType > > getPreconditioner(const std::string &name)
Function returning shared pointer with linear preconditioner given by its name in a form of a string.
Definition LinearSolverTypeResolver.h:144
std::shared_ptr< Linear::LinearSolver< MatrixType > > getLinearSolver(const std::string &name)
Function returning shared pointer with linear solver given by its name in a form of a string.
Definition LinearSolverTypeResolver.h:87
We still stay with the same problem and the only changes are in the solver setup. We first use TNL::Solvers::getLinearSolver to get a shared pointer holding the solver and then TNL::Solvers::getPreconditioner to get a shared pointer holding the preconditioner. The rest of the code is the same as in the previous examples with the only difference that we work with the pointer solver_ptr instead of the direct instance solver of the solver type.
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 = [ 1, 1, 1, 1, 1 ]
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 ]
Vector x = [ 1, 1, 1, 1, 1 ]