Template Numerical Library version\ main:0b2c40f
Loading...
Searching...
No Matches
Linear solvers

Introduction

Solvers of linear systems are one of the most important algorithms in scientific computations. TNL offers the following iterative methods:

  1. Stationary methods
    1. Jacobi method (TNL::Solvers::Linear::Jacobi)
    2. Successive-overrelaxation method, SOR (TNL::Solvers::Linear::SOR)
  2. Krylov subspace methods
    1. Conjugate gradient method, CG (TNL::Solvers::Linear::CG)
    2. Biconjugate gradient stabilized method, BICGStab (TNL::Solvers::Linear::BICGStab)
    3. Biconjugate gradient stabilized method, BICGStab(l) (TNL::Solvers::Linear::BICGStabL)
    4. Transpose-free quasi-minimal residual method, TFQMR (TNL::Solvers::Linear::TFQMR)
    5. Generalized minimal residual method, GMRES (TNL::Solvers::Linear::GMRES) with various methods of orthogonalization:
      1. Classical Gramm-Schmidt, CGS
      2. Classical Gramm-Schmidt with reorthogonalization, CGSR
      3. Modified Gramm-Schmidt, MGS
      4. Modified Gramm-Schmidt with reorthogonalization, MGSR
      5. 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:

  1. Diagonal or Jacobi (TNL::Solvers::Linear::Preconditioners::Diagonal)
  2. ILU (Incomplete LU) - CPU only currently
    1. ILU(0) (TNL::Solvers::Linear::Preconditioners::ILU0)
    2. 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 * Set the following matrix (dots represent zero matrix elements):
14 *
15 * / 2.5 -1 . . . \
16 * | -1 2.5 -1 . . |
17 * | . -1 2.5 -1. . |
18 * | . . -1 2.5 -1 |
19 * \ . . . -1 2.5 /
20 */
23 const int size( 5 );
24 auto matrix_ptr = std::make_shared< MatrixType >();
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
28 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
33 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
37 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
41 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
42 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
43 }
44 };
45
46 /***
47 * Set the matrix elements.
48 */
49 matrix_ptr->forAllRows( f );
50 std::cout << *matrix_ptr << '\n';
51
52 /***
53 * Set the right-hand side vector.
54 */
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
59 std::cout << "Vector b = " << b << '\n';
60
61 /***
62 * Solve the linear system.
63 */
65 LinearSolver solver;
66 solver.setMatrix( matrix_ptr );
67 solver.setConvergenceResidue( 1.0e-6 );
68 solver.solve( b, x );
69 std::cout << "Vector x = " << x << '\n';
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:37
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:57
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
T make_shared(T... args)

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 * Set the following matrix (dots represent zero matrix elements):
14 *
15 * / 2.5 -1 . . . \
16 * | -1 2.5 -1 . . |
17 * | . -1 2.5 -1. . |
18 * | . . -1 2.5 -1 |
19 * \ . . . -1 2.5 /
20 */
23 const int size( 5 );
24 auto matrix_ptr = std::make_shared< MatrixType >();
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
28 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
33 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
37 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
41 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
42 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
43 }
44 };
45
46 /***
47 * Set the matrix elements.
48 */
49 matrix_ptr->forAllRows( f );
50 std::cout << *matrix_ptr << '\n';
51
52 /***
53 * Set the right-hand side vector.
54 */
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
59 std::cout << "Vector b = " << b << '\n';
60
61 /***
62 * Setup solver of the linear system.
63 */
65 LinearSolver solver;
66 solver.setMatrix( matrix_ptr );
67 solver.setOmega( 0.0005 );
68
69 /***
70 * Setup monitor of the iterative solver.
71 */
72 using IterativeSolverMonitorType = TNL::Solvers::IterativeSolverMonitor< double >;
73 IterativeSolverMonitorType monitor;
74 TNL::Solvers::SolverMonitorThread monitorThread( monitor );
75 monitor.setRefreshRate( 10 ); // refresh rate in milliseconds
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();
82 std::cout << "Vector x = " << x << '\n';
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: 33763 RES: 0.0040392
Jacobi stage: ITER: 62999 RES: 4.5319e-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 ]
Jacobi stage: ITER: 1015 RES: 0.73559
Jacobi stage: ITER: 1391 RES: 0.66878
Jacobi stage: ITER: 1773 RES: 0.61163
Jacobi stage: ITER: 2037 RES: 0.57741
Jacobi stage: ITER: 2289 RES: 0.54788
Jacobi stage: ITER: 2541 RES: 0.52095
Jacobi stage: ITER: 2793 RES: 0.49621
Jacobi stage: ITER: 3045 RES: 0.47337
Jacobi stage: ITER: 3297 RES: 0.45217
Jacobi stage: ITER: 3551 RES: 0.4324
Jacobi stage: ITER: 3803 RES: 0.41387
Jacobi stage: ITER: 4059 RES: 0.39619
Jacobi stage: ITER: 4279 RES: 0.38181
Jacobi stage: ITER: 4515 RES: 0.36715
Jacobi stage: ITER: 4665 RES: 0.35809
Jacobi stage: ITER: 4817 RES: 0.3493
Jacobi stage: ITER: 4967 RES: 0.34101
Jacobi stage: ITER: 5107 RES: 0.33338
Jacobi stage: ITER: 5251 RES: 0.32575
Jacobi stage: ITER: 5403 RES: 0.31792
Jacobi stage: ITER: 5535 RES: 0.31129
Jacobi stage: ITER: 5687 RES: 0.30386
Jacobi stage: ITER: 5841 RES: 0.29644
Jacobi stage: ITER: 5973 RES: 0.29032
Jacobi stage: ITER: 6125 RES: 0.28345
Jacobi stage: ITER: 6259 RES: 0.27763
Jacobi stage: ITER: 6409 RES: 0.27109
Jacobi stage: ITER: 6563 RES: 0.26472
Jacobi stage: ITER: 6691 RES: 0.25947
Jacobi stage: ITER: 6935 RES: 0.24979
Jacobi stage: ITER: 7187 RES: 0.24018
Jacobi stage: ITER: 7441 RES: 0.23082
Jacobi stage: ITER: 7695 RES: 0.22198
Jacobi stage: ITER: 7951 RES: 0.21336
Jacobi stage: ITER: 8175 RES: 0.2061
Jacobi stage: ITER: 8359 RES: 0.20032
Jacobi stage: ITER: 8555 RES: 0.19435
Jacobi stage: ITER: 8785 RES: 0.18752
Jacobi stage: ITER: 9037 RES: 0.18038
Jacobi stage: ITER: 9291 RES: 0.17351
Jacobi stage: ITER: 9547 RES: 0.1668
Jacobi stage: ITER: 9731 RES: 0.16214
Jacobi stage: ITER: 9975 RES: 0.15617
Jacobi stage: ITER: 10139 RES: 0.15228
Jacobi stage: ITER: 10379 RES: 0.14676
Jacobi stage: ITER: 10633 RES: 0.14109
Jacobi stage: ITER: 10889 RES: 0.13565
Jacobi stage: ITER: 11123 RES: 0.13089
Jacobi stage: ITER: 11345 RES: 0.12646
Jacobi stage: ITER: 11595 RES: 0.12173
Jacobi stage: ITER: 11851 RES: 0.11704
Jacobi stage: ITER: 12027 RES: 0.11391
Jacobi stage: ITER: 12277 RES: 0.10959
Jacobi stage: ITER: 12533 RES: 0.10536
Jacobi stage: ITER: 12763 RES: 0.10173
Jacobi stage: ITER: 12987 RES: 0.098292
Jacobi stage: ITER: 13235 RES: 0.094618
Jacobi stage: ITER: 13491 RES: 0.090969
Jacobi stage: ITER: 13675 RES: 0.088433
Jacobi stage: ITER: 13913 RES: 0.085232
Jacobi stage: ITER: 14169 RES: 0.081945
Jacobi stage: ITER: 14423 RES: 0.078834
Jacobi stage: ITER: 14659 RES: 0.076027
Jacobi stage: ITER: 14883 RES: 0.073456
Jacobi stage: ITER: 15131 RES: 0.07071
Jacobi stage: ITER: 15355 RES: 0.068318
Jacobi stage: ITER: 15603 RES: 0.065765
Jacobi stage: ITER: 15855 RES: 0.063268
Jacobi stage: ITER: 16113 RES: 0.060791
Jacobi stage: ITER: 16369 RES: 0.058447
Jacobi stage: ITER: 16573 RES: 0.056644
Jacobi stage: ITER: 16787 RES: 0.054829
Jacobi stage: ITER: 17039 RES: 0.052747
Jacobi stage: ITER: 17293 RES: 0.050713
Jacobi stage: ITER: 17515 RES: 0.049028
Jacobi stage: ITER: 17733 RES: 0.047399
Jacobi stage: ITER: 18001 RES: 0.045487
Jacobi stage: ITER: 18229 RES: 0.043922
Jacobi stage: ITER: 18425 RES: 0.042619
Jacobi stage: ITER: 18659 RES: 0.041127
Jacobi stage: ITER: 18911 RES: 0.039566
Jacobi stage: ITER: 19167 RES: 0.03804
Jacobi stage: ITER: 19423 RES: 0.036573
Jacobi stage: ITER: 19617 RES: 0.035489
Jacobi stage: ITER: 19803 RES: 0.0345
Jacobi stage: ITER: 20051 RES: 0.03321
Jacobi stage: ITER: 20307 RES: 0.03193
Jacobi stage: ITER: 20559 RES: 0.030717
Jacobi stage: ITER: 20811 RES: 0.029551
Jacobi stage: ITER: 21067 RES: 0.028412
Jacobi stage: ITER: 21319 RES: 0.027333
Jacobi stage: ITER: 21513 RES: 0.026522
Jacobi stage: ITER: 21727 RES: 0.025672
Jacobi stage: ITER: 21971 RES: 0.024728
Jacobi stage: ITER: 22227 RES: 0.023775
Jacobi stage: ITER: 22479 RES: 0.022872
Jacobi stage: ITER: 22735 RES: 0.02199
Jacobi stage: ITER: 22989 RES: 0.021142
Jacobi stage: ITER: 23207 RES: 0.020452
Jacobi stage: ITER: 23407 RES: 0.019833
Jacobi stage: ITER: 23647 RES: 0.019116
Jacobi stage: ITER: 23889 RES: 0.018412
Jacobi stage: ITER: 24143 RES: 0.017713
Jacobi stage: ITER: 24381 RES: 0.017072
Jacobi stage: ITER: 24635 RES: 0.016424
Jacobi stage: ITER: 24871 RES: 0.015839
Jacobi stage: ITER: 25083 RES: 0.015332
Jacobi stage: ITER: 25297 RES: 0.014832
Jacobi stage: ITER: 25539 RES: 0.014295
Jacobi stage: ITER: 25747 RES: 0.013845
Jacobi stage: ITER: 26003 RES: 0.013311
Jacobi stage: ITER: 26257 RES: 0.012798
Jacobi stage: ITER: 26487 RES: 0.012358
Jacobi stage: ITER: 26735 RES: 0.011896
Jacobi stage: ITER: 26955 RES: 0.0115
Jacobi stage: ITER: 27175 RES: 0.011118
Jacobi stage: ITER: 27421 RES: 0.010703
Jacobi stage: ITER: 27685 RES: 0.010277
Jacobi stage: ITER: 27943 RES: 0.0098812
Jacobi stage: ITER: 28177 RES: 0.0095294
Jacobi stage: ITER: 28427 RES: 0.0091732
Jacobi stage: ITER: 28643 RES: 0.0088739
Jacobi stage: ITER: 28881 RES: 0.0085527
Jacobi stage: ITER: 29129 RES: 0.008233
Jacobi stage: ITER: 29381 RES: 0.0079204
Jacobi stage: ITER: 29593 RES: 0.0076667
Jacobi stage: ITER: 29947 RES: 0.0072632
Jacobi stage: ITER: 30197 RES: 0.0069874
Jacobi stage: ITER: 30445 RES: 0.0067262
Jacobi stage: ITER: 30697 RES: 0.0064708
Jacobi stage: ITER: 30951 RES: 0.0062252
Jacobi stage: ITER: 31203 RES: 0.0059888
Jacobi stage: ITER: 31455 RES: 0.0057614
Jacobi stage: ITER: 31707 RES: 0.0055427
Jacobi stage: ITER: 31959 RES: 0.0053322
Jacobi stage: ITER: 32211 RES: 0.0051298
Jacobi stage: ITER: 32463 RES: 0.004935
Jacobi stage: ITER: 32715 RES: 0.0047476
Jacobi stage: ITER: 32967 RES: 0.0045674
Jacobi stage: ITER: 33219 RES: 0.004394
Jacobi stage: ITER: 33471 RES: 0.0042271
Jacobi stage: ITER: 33723 RES: 0.0040666
Jacobi stage: ITER: 33975 RES: 0.0039122
Jacobi stage: ITER: 34227 RES: 0.0037637
Jacobi stage: ITER: 34479 RES: 0.0036208
Jacobi stage: ITER: 34731 RES: 0.0034833
Jacobi stage: ITER: 34983 RES: 0.0033511
Jacobi stage: ITER: 35235 RES: 0.0032238
Jacobi stage: ITER: 35487 RES: 0.0031014
Jacobi stage: ITER: 35739 RES: 0.0029837
Jacobi stage: ITER: 35991 RES: 0.0028704
Jacobi stage: ITER: 36243 RES: 0.0027614
Jacobi stage: ITER: 36495 RES: 0.0026566
Jacobi stage: ITER: 36749 RES: 0.0025541
Jacobi stage: ITER: 37001 RES: 0.0024571
Jacobi stage: ITER: 37253 RES: 0.0023639
Jacobi stage: ITER: 37505 RES: 0.0022741
Jacobi stage: ITER: 37757 RES: 0.0021878
Jacobi stage: ITER: 38009 RES: 0.0021047
Jacobi stage: ITER: 38261 RES: 0.0020248
Jacobi stage: ITER: 38513 RES: 0.0019479
Jacobi stage: ITER: 38767 RES: 0.0018739
Jacobi stage: ITER: 39019 RES: 0.0018028
Jacobi stage: ITER: 39271 RES: 0.0017343
Jacobi stage: ITER: 39523 RES: 0.0016685
Jacobi stage: ITER: 39775 RES: 0.0016051
Jacobi stage: ITER: 40027 RES: 0.0015442
Jacobi stage: ITER: 40279 RES: 0.0014856
Jacobi stage: ITER: 40531 RES: 0.0014292
Jacobi stage: ITER: 40783 RES: 0.0013749
Jacobi stage: ITER: 41035 RES: 0.0013227
Jacobi stage: ITER: 41287 RES: 0.0012725
Jacobi stage: ITER: 41539 RES: 0.0012242
Jacobi stage: ITER: 41791 RES: 0.0011777
Jacobi stage: ITER: 42043 RES: 0.001133
Jacobi stage: ITER: 42295 RES: 0.00109
Jacobi stage: ITER: 42547 RES: 0.0010486
Jacobi stage: ITER: 42799 RES: 0.0010088
Jacobi stage: ITER: 43053 RES: 0.00096987
Jacobi stage: ITER: 43305 RES: 0.00093304
Jacobi stage: ITER: 43557 RES: 0.00089762
Jacobi stage: ITER: 43809 RES: 0.00086354
Jacobi stage: ITER: 44061 RES: 0.00083075
Jacobi stage: ITER: 44313 RES: 0.00079921
Jacobi stage: ITER: 44567 RES: 0.00076886
Jacobi stage: ITER: 44819 RES: 0.00073967
Jacobi stage: ITER: 45071 RES: 0.00071159
Jacobi stage: ITER: 45323 RES: 0.00068457
Jacobi stage: ITER: 45575 RES: 0.00065858
Jacobi stage: ITER: 45827 RES: 0.00063357
Jacobi stage: ITER: 46079 RES: 0.00060952
Jacobi stage: ITER: 46331 RES: 0.00058638
Jacobi stage: ITER: 46583 RES: 0.00056411
Jacobi stage: ITER: 46835 RES: 0.00054269
Jacobi stage: ITER: 47087 RES: 0.00052209
Jacobi stage: ITER: 47339 RES: 0.00050227
Jacobi stage: ITER: 47591 RES: 0.0004832
Jacobi stage: ITER: 47843 RES: 0.00046485
Jacobi stage: ITER: 48095 RES: 0.0004472
Jacobi stage: ITER: 48349 RES: 0.00042996
Jacobi stage: ITER: 48601 RES: 0.00041363
Jacobi stage: ITER: 48853 RES: 0.00039793
Jacobi stage: ITER: 49105 RES: 0.00038282
Jacobi stage: ITER: 49357 RES: 0.00036828
Jacobi stage: ITER: 49609 RES: 0.0003543
Jacobi stage: ITER: 49861 RES: 0.00034085
Jacobi stage: ITER: 50113 RES: 0.00032791
Jacobi stage: ITER: 50367 RES: 0.00031546
Jacobi stage: ITER: 50619 RES: 0.00030348
Jacobi stage: ITER: 50871 RES: 0.00029196
Jacobi stage: ITER: 51123 RES: 0.00028087
Jacobi stage: ITER: 51375 RES: 0.00027021
Jacobi stage: ITER: 51627 RES: 0.00025995
Jacobi stage: ITER: 51879 RES: 0.00025008
Jacobi stage: ITER: 52131 RES: 0.00024058
Jacobi stage: ITER: 52383 RES: 0.00023145
Jacobi stage: ITER: 52635 RES: 0.00022266
Jacobi stage: ITER: 52887 RES: 0.00021421
Jacobi stage: ITER: 53139 RES: 0.00020607
Jacobi stage: ITER: 53391 RES: 0.00019825
Jacobi stage: ITER: 53643 RES: 0.00019072
Jacobi stage: ITER: 53897 RES: 0.00018337
Jacobi stage: ITER: 54149 RES: 0.00017641
Jacobi stage: ITER: 54401 RES: 0.00016971
Jacobi stage: ITER: 54653 RES: 0.00016327
Jacobi stage: ITER: 54905 RES: 0.00015707
Jacobi stage: ITER: 55159 RES: 0.0001511
Jacobi stage: ITER: 55411 RES: 0.00014537
Jacobi stage: ITER: 55663 RES: 0.00013985
Jacobi stage: ITER: 55915 RES: 0.00013454
Jacobi stage: ITER: 56167 RES: 0.00012943
Jacobi stage: ITER: 56419 RES: 0.00012452
Jacobi stage: ITER: 56671 RES: 0.00011979
Jacobi stage: ITER: 56923 RES: 0.00011524
Jacobi stage: ITER: 57175 RES: 0.00011086
Jacobi stage: ITER: 57427 RES: 0.00010665
Jacobi stage: ITER: 57679 RES: 0.00010261
Jacobi stage: ITER: 57931 RES: 9.8709e-05
Jacobi stage: ITER: 58185 RES: 9.4903e-05
Jacobi stage: ITER: 58437 RES: 9.13e-05
Jacobi stage: ITER: 58689 RES: 8.7834e-05
Jacobi stage: ITER: 58941 RES: 8.4499e-05
Jacobi stage: ITER: 59193 RES: 8.129e-05
Jacobi stage: ITER: 59447 RES: 7.8204e-05
Jacobi stage: ITER: 59699 RES: 7.5235e-05
Jacobi stage: ITER: 59951 RES: 7.2378e-05
Jacobi stage: ITER: 60203 RES: 6.963e-05
Jacobi stage: ITER: 60455 RES: 6.6986e-05
Jacobi stage: ITER: 60707 RES: 6.4443e-05
Jacobi stage: ITER: 60959 RES: 6.1996e-05
Jacobi stage: ITER: 61211 RES: 5.9642e-05
Jacobi stage: ITER: 61463 RES: 5.7378e-05
Jacobi stage: ITER: 61715 RES: 5.5199e-05
Jacobi stage: ITER: 61967 RES: 5.3104e-05
Jacobi stage: ITER: 62217 RES: 5.1087e-05
Jacobi stage: ITER: 62467 RES: 4.9178e-05
Jacobi stage: ITER: 62719 RES: 4.7311e-05
Jacobi stage: ITER: 62969 RES: 4.5514e-05
Jacobi stage: ITER: 63219 RES: 4.3813e-05
Jacobi stage: ITER: 63471 RES: 4.215e-05
Jacobi stage: ITER: 63719 RES: 4.0574e-05
Jacobi stage: ITER: 63971 RES: 3.9034e-05
Jacobi stage: ITER: 64221 RES: 3.7552e-05
Jacobi stage: ITER: 64471 RES: 3.6148e-05
Jacobi stage: ITER: 64723 RES: 3.4776e-05
Jacobi stage: ITER: 64971 RES: 3.3476e-05
Jacobi stage: ITER: 65223 RES: 3.2205e-05
Jacobi stage: ITER: 65473 RES: 3.0982e-05
Jacobi stage: ITER: 65723 RES: 2.9824e-05
Jacobi stage: ITER: 65975 RES: 2.8692e-05
Jacobi stage: ITER: 66225 RES: 2.7602e-05
Jacobi stage: ITER: 66475 RES: 2.6571e-05
Jacobi stage: ITER: 66725 RES: 2.5562e-05
Jacobi stage: ITER: 66975 RES: 2.4606e-05
Jacobi stage: ITER: 67227 RES: 2.3672e-05
Jacobi stage: ITER: 67477 RES: 2.2773e-05
Jacobi stage: ITER: 67727 RES: 2.1922e-05
Jacobi stage: ITER: 67977 RES: 2.109e-05
Jacobi stage: ITER: 68227 RES: 2.0302e-05
Jacobi stage: ITER: 68479 RES: 1.9531e-05
Jacobi stage: ITER: 68729 RES: 1.8789e-05
Jacobi stage: ITER: 68991 RES: 1.8054e-05
Jacobi stage: ITER: 69247 RES: 1.7358e-05
Jacobi stage: ITER: 69415 RES: 1.6915e-05
Jacobi stage: ITER: 69659 RES: 1.6293e-05
Jacobi stage: ITER: 69911 RES: 1.5674e-05
Jacobi stage: ITER: 70161 RES: 1.5079e-05
Jacobi stage: ITER: 70411 RES: 1.4516e-05
Jacobi stage: ITER: 70663 RES: 1.3965e-05
Jacobi stage: ITER: 70913 RES: 1.3434e-05
Jacobi stage: ITER: 71163 RES: 1.2932e-05
Jacobi stage: ITER: 71415 RES: 1.2441e-05
Jacobi stage: ITER: 71663 RES: 1.1976e-05
Jacobi stage: ITER: 71915 RES: 1.1522e-05
Jacobi stage: ITER: 72165 RES: 1.1084e-05
Jacobi stage: ITER: 72415 RES: 1.067e-05
Jacobi stage: ITER: 72667 RES: 1.0265e-05
Jacobi stage: ITER: 72917 RES: 9.8749e-06
Jacobi stage: ITER: 73167 RES: 9.5058e-06
Jacobi stage: ITER: 73417 RES: 9.1449e-06
Jacobi stage: ITER: 73667 RES: 8.8031e-06
Jacobi stage: ITER: 73919 RES: 8.4689e-06
Jacobi stage: ITER: 74169 RES: 8.1473e-06
Jacobi stage: ITER: 74419 RES: 7.8428e-06
Jacobi stage: ITER: 74671 RES: 7.545e-06
Jacobi stage: ITER: 74921 RES: 7.2586e-06
Jacobi stage: ITER: 75171 RES: 6.9873e-06
Jacobi stage: ITER: 75421 RES: 6.722e-06
Jacobi stage: ITER: 75671 RES: 6.4707e-06
Jacobi stage: ITER: 75923 RES: 6.225e-06
Jacobi stage: ITER: 76173 RES: 5.9887e-06
Jacobi stage: ITER: 76423 RES: 5.7649e-06
Jacobi stage: ITER: 76675 RES: 5.546e-06
Jacobi stage: ITER: 76925 RES: 5.3354e-06
Jacobi stage: ITER: 77175 RES: 5.136e-06
Jacobi stage: ITER: 77425 RES: 4.941e-06
Jacobi stage: ITER: 77675 RES: 4.7563e-06
Jacobi stage: ITER: 77927 RES: 4.5757e-06
Jacobi stage: ITER: 78177 RES: 4.402e-06
Jacobi stage: ITER: 78427 RES: 4.2375e-06
Jacobi stage: ITER: 78679 RES: 4.0766e-06
Jacobi stage: ITER: 78929 RES: 3.9218e-06
Jacobi stage: ITER: 79179 RES: 3.7752e-06
Jacobi stage: ITER: 79431 RES: 3.6319e-06
Jacobi stage: ITER: 79681 RES: 3.494e-06
Jacobi stage: ITER: 79931 RES: 3.3634e-06
Jacobi stage: ITER: 80181 RES: 3.2357e-06
Jacobi stage: ITER: 80431 RES: 3.1147e-06
Jacobi stage: ITER: 80683 RES: 2.9965e-06
Jacobi stage: ITER: 80933 RES: 2.8827e-06
Jacobi stage: ITER: 81183 RES: 2.775e-06
Jacobi stage: ITER: 81435 RES: 2.6696e-06
Jacobi stage: ITER: 81685 RES: 2.5682e-06
Jacobi stage: ITER: 81935 RES: 2.4722e-06
Jacobi stage: ITER: 82187 RES: 2.3784e-06
Jacobi stage: ITER: 82437 RES: 2.2881e-06
Jacobi stage: ITER: 82687 RES: 2.2026e-06
Jacobi stage: ITER: 82937 RES: 2.1189e-06
Jacobi stage: ITER: 83187 RES: 2.0397e-06
Jacobi stage: ITER: 83439 RES: 1.9623e-06
Jacobi stage: ITER: 83689 RES: 1.8878e-06
Jacobi stage: ITER: 83939 RES: 1.8172e-06
Jacobi stage: ITER: 84191 RES: 1.7482e-06
Jacobi stage: ITER: 84441 RES: 1.6819e-06
Jacobi stage: ITER: 84691 RES: 1.619e-06
Jacobi stage: ITER: 84941 RES: 1.5575e-06
Jacobi stage: ITER: 85191 RES: 1.4993e-06
Jacobi stage: ITER: 85443 RES: 1.4424e-06
Jacobi stage: ITER: 85693 RES: 1.3876e-06
Jacobi stage: ITER: 85943 RES: 1.3357e-06
Jacobi stage: ITER: 86195 RES: 1.285e-06
Jacobi stage: ITER: 86445 RES: 1.2362e-06
Jacobi stage: ITER: 86695 RES: 1.19e-06
Jacobi stage: ITER: 86945 RES: 1.1449e-06
Jacobi stage: ITER: 87195 RES: 1.1021e-06
Jacobi stage: ITER: 87447 RES: 1.0602e-06
Jacobi stage: ITER: 87783 RES: 1.0069e-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 * Set the following matrix (dots represent zero matrix elements):
15 *
16 * / 2.5 -1 . . . \
17 * | -1 2.5 -1 . . |
18 * | . -1 2.5 -1. . |
19 * | . . -1 2.5 -1 |
20 * \ . . . -1 2.5 /
21 */
24 const int size( 5 );
25 auto matrix_ptr = std::make_shared< MatrixType >();
26 matrix_ptr->setDimensions( size, size );
27 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
28
29 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
30 {
31 const int rowIdx = row.getRowIndex();
32 if( rowIdx == 0 ) {
33 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
34 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
35 }
36 else if( rowIdx == size - 1 ) {
37 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
38 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
39 }
40 else {
41 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
42 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
43 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
44 }
45 };
46
47 /***
48 * Set the matrix elements.
49 */
50 matrix_ptr->forAllRows( f );
51 std::cout << *matrix_ptr << '\n';
52
53 /***
54 * Set the right-hand side vector.
55 */
56 Vector x( size, 1.0 );
57 Vector b( size );
58 matrix_ptr->vectorProduct( x, b );
59 x = 0.0;
60 std::cout << "Vector b = " << b << '\n';
61
62 /***
63 * Setup solver of the linear system.
64 */
66 LinearSolver solver;
67 solver.setMatrix( matrix_ptr );
68 solver.setOmega( 0.0005 );
69
70 /***
71 * Setup monitor of the iterative solver.
72 */
73 using IterativeSolverMonitorType = TNL::Solvers::IterativeSolverMonitor< double >;
74 IterativeSolverMonitorType monitor;
75 TNL::Solvers::SolverMonitorThread mmonitorThread( monitor );
76 monitor.setRefreshRate( 10 ); // refresh rate in milliseconds
77 monitor.setVerbose( 1 );
78 monitor.setStage( "Jacobi stage:" );
79 TNL::Timer timer;
80 monitor.setTimer( timer );
81 timer.start();
82 solver.setSolverMonitor( monitor );
83 solver.setConvergenceResidue( 1.0e-6 );
84 solver.solve( b, x );
85 monitor.stopMainLoop();
86 std::cout << "Vector x = " << x << '\n';
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:1.1737e-
ELA: 0.10011 Jacobi stage: ITER: 33759 RES: 0.0040442
ELA: 0.20021 Jacobi stage: ITER: 65673 RES: 3.0045e-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:1.5794e-
ELA: 0.10011 Jacobi stage: ITER: 427 RES: 0.87024
ELA: 0.2002 Jacobi stage: ITER: 807 RES: 0.77838
ELA: 0.30031 Jacobi stage: ITER: 1137 RES: 0.71218
ELA: 0.40041 Jacobi stage: ITER: 1389 RES: 0.66878
ELA: 0.50051 Jacobi stage: ITER: 1641 RES: 0.63019
ELA: 0.60062 Jacobi stage: ITER: 1893 RES: 0.59562
ELA: 0.70073 Jacobi stage: ITER: 2147 RES: 0.56441
ELA: 0.80079 Jacobi stage: ITER: 2399 RES: 0.53605
ELA: 0.90089 Jacobi stage: ITER: 2651 RES: 0.5101
ELA: 1.0011 Jacobi stage: ITER: 2903 RES: 0.48621
ELA: 1.1012 Jacobi stage: ITER: 3157 RES: 0.46376
ELA: 1.2013 Jacobi stage: ITER: 3411 RES: 0.44322
ELA: 1.3014 Jacobi stage: ITER: 3619 RES: 0.42728
ELA: 1.4015 Jacobi stage: ITER: 3815 RES: 0.41302
ELA: 1.5016 Jacobi stage: ITER: 3967 RES: 0.40242
ELA: 1.6017 Jacobi stage: ITER: 4117 RES: 0.3922
ELA: 1.7018 Jacobi stage: ITER: 4271 RES: 0.38232
ELA: 1.8019 Jacobi stage: ITER: 4399 RES: 0.37426
ELA: 1.902 Jacobi stage: ITER: 4551 RES: 0.36498
ELA: 2.0021 Jacobi stage: ITER: 4703 RES: 0.35598
ELA: 2.1022 Jacobi stage: ITER: 4833 RES: 0.3484
ELA: 2.2038 Jacobi stage: ITER: 4987 RES: 0.33991
ELA: 2.3039 Jacobi stage: ITER: 5119 RES: 0.33274
ELA: 2.404 Jacobi stage: ITER: 5265 RES: 0.32491
ELA: 2.5043 Jacobi stage: ITER: 5417 RES: 0.31711
ELA: 2.605 Jacobi stage: ITER: 5543 RES: 0.3109
ELA: 2.7051 Jacobi stage: ITER: 5695 RES: 0.30348
ELA: 2.8052 Jacobi stage: ITER: 5829 RES: 0.29701
ELA: 2.9053 Jacobi stage: ITER: 6011 RES: 0.28868
ELA: 3.0054 Jacobi stage: ITER: 6261 RES: 0.27746
ELA: 3.1055 Jacobi stage: ITER: 6515 RES: 0.26671
ELA: 3.2056 Jacobi stage: ITER: 6771 RES: 0.25625
ELA: 3.3057 Jacobi stage: ITER: 7025 RES: 0.24623
ELA: 3.4058 Jacobi stage: ITER: 7281 RES: 0.23663
ELA: 3.506 Jacobi stage: ITER: 7471 RES: 0.22982
ELA: 3.6061 Jacobi stage: ITER: 7657 RES: 0.22322
ELA: 3.7062 Jacobi stage: ITER: 7865 RES: 0.21615
ELA: 3.8063 Jacobi stage: ITER: 8107 RES: 0.20827
ELA: 3.9064 Jacobi stage: ITER: 8359 RES: 0.20032
ELA: 4.0065 Jacobi stage: ITER: 8615 RES: 0.19256
ELA: 4.1066 Jacobi stage: ITER: 8839 RES: 0.18602
ELA: 4.2067 Jacobi stage: ITER: 9047 RES: 0.18015
ELA: 4.3068 Jacobi stage: ITER: 9295 RES: 0.1734
ELA: 4.4069 Jacobi stage: ITER: 9455 RES: 0.16918
ELA: 4.507 Jacobi stage: ITER: 9687 RES: 0.16324
ELA: 4.6071 Jacobi stage: ITER: 9943 RES: 0.15694
ELA: 4.7072 Jacobi stage: ITER: 10197 RES: 0.15088
ELA: 4.8072 Jacobi stage: ITER: 10401 RES: 0.14622
ELA: 4.9073 Jacobi stage: ITER: 10643 RES: 0.14092
ELA: 5.0074 Jacobi stage: ITER: 10897 RES: 0.13548
ELA: 5.1075 Jacobi stage: ITER: 11083 RES: 0.1317
ELA: 5.2076 Jacobi stage: ITER: 11317 RES: 0.12701
ELA: 5.3077 Jacobi stage: ITER: 11571 RES: 0.12218
ELA: 5.4078 Jacobi stage: ITER: 11827 RES: 0.11747
ELA: 5.5079 Jacobi stage: ITER: 12053 RES: 0.11343
ELA: 5.608 Jacobi stage: ITER: 12273 RES: 0.10965
ELA: 5.7081 Jacobi stage: ITER: 12525 RES: 0.10549
ELA: 5.8082 Jacobi stage: ITER: 12735 RES: 0.10217
ELA: 5.9083 Jacobi stage: ITER: 12945 RES: 0.098898
ELA: 6.0084 Jacobi stage: ITER: 13195 RES: 0.095201
ELA: 6.1085 Jacobi stage: ITER: 13449 RES: 0.091529
ELA: 6.2086 Jacobi stage: ITER: 13703 RES: 0.088054
ELA: 6.3087 Jacobi stage: ITER: 13915 RES: 0.085232
ELA: 6.4088 Jacobi stage: ITER: 14155 RES: 0.082147
ELA: 6.5089 Jacobi stage: ITER: 14379 RES: 0.079369
ELA: 6.609 Jacobi stage: ITER: 14631 RES: 0.076355
ELA: 6.7091 Jacobi stage: ITER: 14877 RES: 0.073501
ELA: 6.8092 Jacobi stage: ITER: 15131 RES: 0.07071
ELA: 6.9093 Jacobi stage: ITER: 15387 RES: 0.067983
ELA: 7.0094 Jacobi stage: ITER: 15639 RES: 0.065402
ELA: 7.1095 Jacobi stage: ITER: 15823 RES: 0.063579
ELA: 7.2096 Jacobi stage: ITER: 16053 RES: 0.061354
ELA: 7.3097 Jacobi stage: ITER: 16307 RES: 0.059024
ELA: 7.412 Jacobi stage: ITER: 16553 RES: 0.056818
ELA: 7.5121 Jacobi stage: ITER: 16785 RES: 0.054829
ELA: 7.6122 Jacobi stage: ITER: 16967 RES: 0.053334
ELA: 7.7124 Jacobi stage: ITER: 17195 RES: 0.051498
ELA: 7.815 Jacobi stage: ITER: 17545 RES: 0.048788
ELA: 7.9151 Jacobi stage: ITER: 17877 RES: 0.046362
ELA: 8.016 Jacobi stage: ITER: 18131 RES: 0.044602
ELA: 8.117 Jacobi stage: ITER: 18387 RES: 0.042882
ELA: 8.22 Jacobi stage: ITER: 18647 RES: 0.041203
ELA: 8.3201 Jacobi stage: ITER: 18871 RES: 0.03981
ELA: 8.424 Jacobi stage: ITER: 19061 RES: 0.038653
ELA: 8.5241 Jacobi stage: ITER: 19279 RES: 0.037391
ELA: 8.6242 Jacobi stage: ITER: 19531 RES: 0.035972
ELA: 8.7243 Jacobi stage: ITER: 19783 RES: 0.034606
ELA: 8.8244 Jacobi stage: ITER: 20037 RES: 0.033271
ELA: 8.9246 Jacobi stage: ITER: 20291 RES: 0.032008
ELA: 9.0247 Jacobi stage: ITER: 20547 RES: 0.030774
ELA: 9.1248 Jacobi stage: ITER: 20783 RES: 0.029678
ELA: 9.2249 Jacobi stage: ITER: 20963 RES: 0.028869
ELA: 9.325 Jacobi stage: ITER: 21209 RES: 0.02779
ELA: 9.4251 Jacobi stage: ITER: 21459 RES: 0.026751
ELA: 9.528 Jacobi stage: ITER: 21719 RES: 0.025704
ELA: 9.6281 Jacobi stage: ITER: 21973 RES: 0.024713
ELA: 9.7282 Jacobi stage: ITER: 22227 RES: 0.023775
ELA: 9.8283 Jacobi stage: ITER: 22463 RES: 0.022928
ELA: 9.929 Jacobi stage: ITER: 22687 RES: 0.022153
ELA: 10.029 Jacobi stage: ITER: 22909 RES: 0.021404
ELA: 10.129 Jacobi stage: ITER: 23135 RES: 0.02068
ELA: 10.229 Jacobi stage: ITER: 23387 RES: 0.019894
ELA: 10.329 Jacobi stage: ITER: 23651 RES: 0.019104
ELA: 10.43 Jacobi stage: ITER: 23903 RES: 0.018379
ELA: 10.53 Jacobi stage: ITER: 24139 RES: 0.017724
ELA: 10.63 Jacobi stage: ITER: 24391 RES: 0.017051
ELA: 10.73 Jacobi stage: ITER: 24563 RES: 0.016607
ELA: 10.83 Jacobi stage: ITER: 24791 RES: 0.016035
ELA: 10.93 Jacobi stage: ITER: 25059 RES: 0.015388
ELA: 11.031 Jacobi stage: ITER: 25311 RES: 0.014804
ELA: 11.131 Jacobi stage: ITER: 25559 RES: 0.014251
ELA: 11.231 Jacobi stage: ITER: 25813 RES: 0.013701
ELA: 11.333 Jacobi stage: ITER: 26043 RES: 0.01323
ELA: 11.434 Jacobi stage: ITER: 26267 RES: 0.012782
ELA: 11.534 Jacobi stage: ITER: 26489 RES: 0.01235
ELA: 11.634 Jacobi stage: ITER: 26723 RES: 0.011918
ELA: 11.734 Jacobi stage: ITER: 26975 RES: 0.011465
ELA: 11.835 Jacobi stage: ITER: 27229 RES: 0.011023
ELA: 11.935 Jacobi stage: ITER: 27465 RES: 0.010631
ELA: 12.035 Jacobi stage: ITER: 27713 RES: 0.010233
ELA: 12.135 Jacobi stage: ITER: 27927 RES: 0.0099055
ELA: 12.235 Jacobi stage: ITER: 28169 RES: 0.0095411
ELA: 12.336 Jacobi stage: ITER: 28363 RES: 0.0092582
ELA: 12.436 Jacobi stage: ITER: 28613 RES: 0.0089121
ELA: 12.536 Jacobi stage: ITER: 28867 RES: 0.0085737
ELA: 12.636 Jacobi stage: ITER: 29417 RES: 0.0078768
ELA: 12.736 Jacobi stage: ITER: 30191 RES: 0.006996
ELA: 12.84 Jacobi stage: ITER: 30479 RES: 0.0066932
ELA: 12.942 Jacobi stage: ITER: 30735 RES: 0.0064352
ELA: 13.042 Jacobi stage: ITER: 30987 RES: 0.0061908
ELA: 13.142 Jacobi stage: ITER: 31239 RES: 0.0059558
ELA: 13.242 Jacobi stage: ITER: 31491 RES: 0.0057296
ELA: 13.342 Jacobi stage: ITER: 31743 RES: 0.0055121
ELA: 13.443 Jacobi stage: ITER: 31995 RES: 0.0053028
ELA: 13.543 Jacobi stage: ITER: 32247 RES: 0.0051015
ELA: 13.643 Jacobi stage: ITER: 32499 RES: 0.0049078
ELA: 13.743 Jacobi stage: ITER: 32751 RES: 0.0047214
ELA: 13.843 Jacobi stage: ITER: 33003 RES: 0.0045422
ELA: 13.943 Jacobi stage: ITER: 33255 RES: 0.0043697
ELA: 14.043 Jacobi stage: ITER: 33507 RES: 0.0042038
ELA: 14.144 Jacobi stage: ITER: 33759 RES: 0.0040442
ELA: 14.244 Jacobi stage: ITER: 34011 RES: 0.0038907
ELA: 14.344 Jacobi stage: ITER: 34265 RES: 0.0037406
ELA: 14.444 Jacobi stage: ITER: 34517 RES: 0.0035986
ELA: 14.544 Jacobi stage: ITER: 34769 RES: 0.003462
ELA: 14.644 Jacobi stage: ITER: 35021 RES: 0.0033305
ELA: 14.744 Jacobi stage: ITER: 35273 RES: 0.0032041
ELA: 14.844 Jacobi stage: ITER: 35525 RES: 0.0030824
ELA: 14.945 Jacobi stage: ITER: 35777 RES: 0.0029654
ELA: 15.045 Jacobi stage: ITER: 36031 RES: 0.0028528
ELA: 15.145 Jacobi stage: ITER: 36283 RES: 0.0027445
ELA: 15.245 Jacobi stage: ITER: 36535 RES: 0.0026403
ELA: 15.345 Jacobi stage: ITER: 36787 RES: 0.00254
ELA: 15.445 Jacobi stage: ITER: 37039 RES: 0.0024436
ELA: 15.545 Jacobi stage: ITER: 37291 RES: 0.0023508
ELA: 15.646 Jacobi stage: ITER: 37543 RES: 0.0022616
ELA: 15.746 Jacobi stage: ITER: 37795 RES: 0.0021757
ELA: 15.846 Jacobi stage: ITER: 38047 RES: 0.0020931
ELA: 15.946 Jacobi stage: ITER: 38299 RES: 0.0020136
ELA: 16.046 Jacobi stage: ITER: 38551 RES: 0.0019372
ELA: 16.146 Jacobi stage: ITER: 38803 RES: 0.0018636
ELA: 16.246 Jacobi stage: ITER: 39055 RES: 0.0017929
ELA: 16.346 Jacobi stage: ITER: 39307 RES: 0.0017248
ELA: 16.447 Jacobi stage: ITER: 39559 RES: 0.0016593
ELA: 16.547 Jacobi stage: ITER: 39811 RES: 0.0015963
ELA: 16.647 Jacobi stage: ITER: 40063 RES: 0.0015357
ELA: 16.747 Jacobi stage: ITER: 40317 RES: 0.0014765
ELA: 16.847 Jacobi stage: ITER: 40569 RES: 0.0014204
ELA: 16.947 Jacobi stage: ITER: 40821 RES: 0.0013665
ELA: 17.047 Jacobi stage: ITER: 41073 RES: 0.0013146
ELA: 17.147 Jacobi stage: ITER: 41325 RES: 0.0012647
ELA: 17.248 Jacobi stage: ITER: 41577 RES: 0.0012167
ELA: 17.348 Jacobi stage: ITER: 41829 RES: 0.0011705
ELA: 17.448 Jacobi stage: ITER: 42081 RES: 0.001126
ELA: 17.548 Jacobi stage: ITER: 42335 RES: 0.0010833
ELA: 17.648 Jacobi stage: ITER: 42587 RES: 0.0010422
ELA: 17.748 Jacobi stage: ITER: 42839 RES: 0.0010026
ELA: 17.848 Jacobi stage: ITER: 43091 RES: 0.00096452
ELA: 17.949 Jacobi stage: ITER: 43343 RES: 0.0009279
ELA: 18.049 Jacobi stage: ITER: 43595 RES: 0.00089267
ELA: 18.149 Jacobi stage: ITER: 43847 RES: 0.00085877
ELA: 18.249 Jacobi stage: ITER: 44099 RES: 0.00082617
ELA: 18.349 Jacobi stage: ITER: 44351 RES: 0.0007948
ELA: 18.449 Jacobi stage: ITER: 44603 RES: 0.00076462
ELA: 18.549 Jacobi stage: ITER: 44855 RES: 0.00073559
ELA: 18.649 Jacobi stage: ITER: 45107 RES: 0.00070766
ELA: 18.75 Jacobi stage: ITER: 45359 RES: 0.00068079
ELA: 18.85 Jacobi stage: ITER: 45611 RES: 0.00065495
ELA: 18.95 Jacobi stage: ITER: 45865 RES: 0.00062969
ELA: 19.05 Jacobi stage: ITER: 46117 RES: 0.00060578
ELA: 19.15 Jacobi stage: ITER: 46369 RES: 0.00058278
ELA: 19.25 Jacobi stage: ITER: 46621 RES: 0.00056066
ELA: 19.35 Jacobi stage: ITER: 46873 RES: 0.00053937
ELA: 19.451 Jacobi stage: ITER: 47125 RES: 0.00051889
ELA: 19.551 Jacobi stage: ITER: 47379 RES: 0.00049919
ELA: 19.651 Jacobi stage: ITER: 47631 RES: 0.00048024
ELA: 19.751 Jacobi stage: ITER: 47883 RES: 0.000462
ELA: 19.851 Jacobi stage: ITER: 48135 RES: 0.00044446
ELA: 19.951 Jacobi stage: ITER: 48387 RES: 0.00042759
ELA: 20.051 Jacobi stage: ITER: 48639 RES: 0.00041135
ELA: 20.152 Jacobi stage: ITER: 48891 RES: 0.00039573
ELA: 20.252 Jacobi stage: ITER: 49143 RES: 0.00038071
ELA: 20.352 Jacobi stage: ITER: 49395 RES: 0.00036625
ELA: 20.452 Jacobi stage: ITER: 49647 RES: 0.00035235
ELA: 20.552 Jacobi stage: ITER: 49899 RES: 0.00033897
ELA: 20.652 Jacobi stage: ITER: 50151 RES: 0.0003261
ELA: 20.752 Jacobi stage: ITER: 50403 RES: 0.00031372
ELA: 20.852 Jacobi stage: ITER: 50655 RES: 0.00030181
ELA: 20.953 Jacobi stage: ITER: 50907 RES: 0.00029035
ELA: 21.053 Jacobi stage: ITER: 51159 RES: 0.00027932
ELA: 21.153 Jacobi stage: ITER: 51411 RES: 0.00026872
ELA: 21.253 Jacobi stage: ITER: 51663 RES: 0.00025852
ELA: 21.353 Jacobi stage: ITER: 51915 RES: 0.0002487
ELA: 21.453 Jacobi stage: ITER: 52169 RES: 0.00023911
ELA: 21.553 Jacobi stage: ITER: 52421 RES: 0.00023003
ELA: 21.654 Jacobi stage: ITER: 52673 RES: 0.0002213
ELA: 21.754 Jacobi stage: ITER: 52925 RES: 0.0002129
ELA: 21.854 Jacobi stage: ITER: 53177 RES: 0.00020481
ELA: 21.954 Jacobi stage: ITER: 53429 RES: 0.00019704
ELA: 22.054 Jacobi stage: ITER: 53681 RES: 0.00018956
ELA: 22.154 Jacobi stage: ITER: 53933 RES: 0.00018236
ELA: 22.254 Jacobi stage: ITER: 54185 RES: 0.00017543
ELA: 22.354 Jacobi stage: ITER: 54439 RES: 0.00016877
ELA: 22.455 Jacobi stage: ITER: 54691 RES: 0.00016237
ELA: 22.555 Jacobi stage: ITER: 54943 RES: 0.0001562
ELA: 22.655 Jacobi stage: ITER: 55195 RES: 0.00015027
ELA: 22.755 Jacobi stage: ITER: 55447 RES: 0.00014456
ELA: 22.855 Jacobi stage: ITER: 55699 RES: 0.00013908
ELA: 22.955 Jacobi stage: ITER: 55951 RES: 0.0001338
ELA: 23.055 Jacobi stage: ITER: 56203 RES: 0.00012872
ELA: 23.155 Jacobi stage: ITER: 56455 RES: 0.00012383
ELA: 23.256 Jacobi stage: ITER: 56707 RES: 0.00011913
ELA: 23.356 Jacobi stage: ITER: 56961 RES: 0.00011453
ELA: 23.456 Jacobi stage: ITER: 57213 RES: 0.00011018
ELA: 23.556 Jacobi stage: ITER: 57465 RES: 0.000106
ELA: 23.656 Jacobi stage: ITER: 57717 RES: 0.00010198
ELA: 23.756 Jacobi stage: ITER: 57969 RES: 9.8105e-05
ELA: 23.856 Jacobi stage: ITER: 58221 RES: 9.438e-05
ELA: 23.956 Jacobi stage: ITER: 58475 RES: 9.0797e-05
ELA: 24.057 Jacobi stage: ITER: 58725 RES: 8.7349e-05
ELA: 24.157 Jacobi stage: ITER: 58979 RES: 8.4033e-05
ELA: 24.257 Jacobi stage: ITER: 59231 RES: 8.0842e-05
ELA: 24.357 Jacobi stage: ITER: 59483 RES: 7.7773e-05
ELA: 24.457 Jacobi stage: ITER: 59735 RES: 7.482e-05
ELA: 24.557 Jacobi stage: ITER: 59987 RES: 7.1979e-05
ELA: 24.657 Jacobi stage: ITER: 60239 RES: 6.9246e-05
ELA: 24.757 Jacobi stage: ITER: 60491 RES: 6.6617e-05
ELA: 24.858 Jacobi stage: ITER: 60743 RES: 6.4088e-05
ELA: 24.958 Jacobi stage: ITER: 60995 RES: 6.1654e-05
ELA: 25.058 Jacobi stage: ITER: 61247 RES: 5.9314e-05
ELA: 25.158 Jacobi stage: ITER: 61499 RES: 5.7062e-05
ELA: 25.258 Jacobi stage: ITER: 61751 RES: 5.4895e-05
ELA: 25.358 Jacobi stage: ITER: 62003 RES: 5.2811e-05
ELA: 25.458 Jacobi stage: ITER: 62255 RES: 5.0806e-05
ELA: 25.558 Jacobi stage: ITER: 62505 RES: 4.8877e-05
ELA: 25.659 Jacobi stage: ITER: 62755 RES: 4.705e-05
ELA: 25.759 Jacobi stage: ITER: 63005 RES: 4.5263e-05
ELA: 25.859 Jacobi stage: ITER: 63255 RES: 4.3572e-05
ELA: 25.959 Jacobi stage: ITER: 63507 RES: 4.1917e-05
ELA: 26.059 Jacobi stage: ITER: 63757 RES: 4.0326e-05
ELA: 26.159 Jacobi stage: ITER: 64007 RES: 3.8819e-05
ELA: 26.259 Jacobi stage: ITER: 64259 RES: 3.7345e-05
ELA: 26.36 Jacobi stage: ITER: 64507 RES: 3.5949e-05
ELA: 26.46 Jacobi stage: ITER: 64759 RES: 3.4584e-05
ELA: 26.56 Jacobi stage: ITER: 65009 RES: 3.3271e-05
ELA: 26.66 Jacobi stage: ITER: 65259 RES: 3.2027e-05
ELA: 26.76 Jacobi stage: ITER: 65511 RES: 3.0811e-05
ELA: 26.86 Jacobi stage: ITER: 65761 RES: 2.9641e-05
ELA: 26.96 Jacobi stage: ITER: 66011 RES: 2.8534e-05
ELA: 27.06 Jacobi stage: ITER: 66261 RES: 2.745e-05
ELA: 27.16 Jacobi stage: ITER: 66511 RES: 2.6424e-05
ELA: 27.261 Jacobi stage: ITER: 66763 RES: 2.5421e-05
ELA: 27.361 Jacobi stage: ITER: 67013 RES: 2.4456e-05
ELA: 27.461 Jacobi stage: ITER: 67263 RES: 2.3542e-05
ELA: 27.561 Jacobi stage: ITER: 67515 RES: 2.2648e-05
ELA: 27.661 Jacobi stage: ITER: 67765 RES: 2.1788e-05
ELA: 27.761 Jacobi stage: ITER: 68015 RES: 2.0974e-05
ELA: 27.861 Jacobi stage: ITER: 68265 RES: 2.0177e-05
ELA: 27.962 Jacobi stage: ITER: 68515 RES: 1.9423e-05
ELA: 28.062 Jacobi stage: ITER: 68767 RES: 1.8686e-05
ELA: 28.162 Jacobi stage: ITER: 69017 RES: 1.7976e-05
ELA: 28.262 Jacobi stage: ITER: 69285 RES: 1.7251e-05
ELA: 28.371 Jacobi stage: ITER: 69581 RES: 1.6484e-05
ELA: 28.472 Jacobi stage: ITER: 69881 RES: 1.5742e-05
ELA: 28.573 Jacobi stage: ITER: 70135 RES: 1.5144e-05
ELA: 28.673 Jacobi stage: ITER: 70387 RES: 1.4569e-05
ELA: 28.774 Jacobi stage: ITER: 70637 RES: 1.4016e-05
ELA: 28.874 Jacobi stage: ITER: 70887 RES: 1.3492e-05
ELA: 28.974 Jacobi stage: ITER: 71139 RES: 1.298e-05
ELA: 29.074 Jacobi stage: ITER: 71387 RES: 1.2495e-05
ELA: 29.174 Jacobi stage: ITER: 71639 RES: 1.202e-05
ELA: 29.274 Jacobi stage: ITER: 71889 RES: 1.1564e-05
ELA: 29.374 Jacobi stage: ITER: 72139 RES: 1.1132e-05
ELA: 29.474 Jacobi stage: ITER: 72391 RES: 1.0709e-05
ELA: 29.574 Jacobi stage: ITER: 72641 RES: 1.0303e-05
ELA: 29.675 Jacobi stage: ITER: 72891 RES: 9.9175e-06
ELA: 29.775 Jacobi stage: ITER: 73141 RES: 9.5409e-06
ELA: 29.875 Jacobi stage: ITER: 73391 RES: 9.1843e-06
ELA: 29.975 Jacobi stage: ITER: 73643 RES: 8.8356e-06
ELA: 30.075 Jacobi stage: ITER: 73893 RES: 8.5002e-06
ELA: 30.175 Jacobi stage: ITER: 74143 RES: 8.1824e-06
ELA: 30.275 Jacobi stage: ITER: 74395 RES: 7.8718e-06
ELA: 30.375 Jacobi stage: ITER: 74645 RES: 7.5729e-06
ELA: 30.476 Jacobi stage: ITER: 74895 RES: 7.2898e-06
ELA: 30.576 Jacobi stage: ITER: 75145 RES: 7.0131e-06
ELA: 30.676 Jacobi stage: ITER: 75395 RES: 6.7509e-06
ELA: 30.776 Jacobi stage: ITER: 75647 RES: 6.4946e-06
ELA: 30.876 Jacobi stage: ITER: 75897 RES: 6.248e-06
ELA: 30.976 Jacobi stage: ITER: 76147 RES: 6.0145e-06
ELA: 31.076 Jacobi stage: ITER: 76399 RES: 5.7861e-06
ELA: 31.176 Jacobi stage: ITER: 76649 RES: 5.5665e-06
ELA: 31.277 Jacobi stage: ITER: 76899 RES: 5.3584e-06
ELA: 31.377 Jacobi stage: ITER: 77151 RES: 5.155e-06
ELA: 31.477 Jacobi stage: ITER: 77401 RES: 4.9592e-06
ELA: 31.577 Jacobi stage: ITER: 77651 RES: 4.7739e-06
ELA: 31.677 Jacobi stage: ITER: 77903 RES: 4.5926e-06
ELA: 31.777 Jacobi stage: ITER: 78151 RES: 4.421e-06
ELA: 31.877 Jacobi stage: ITER: 78403 RES: 4.2531e-06
ELA: 31.977 Jacobi stage: ITER: 78653 RES: 4.0916e-06
ELA: 32.078 Jacobi stage: ITER: 78903 RES: 3.9387e-06
ELA: 32.178 Jacobi stage: ITER: 79155 RES: 3.7891e-06
ELA: 32.278 Jacobi stage: ITER: 79405 RES: 3.6453e-06
ELA: 32.378 Jacobi stage: ITER: 79655 RES: 3.509e-06
ELA: 32.478 Jacobi stage: ITER: 79907 RES: 3.3758e-06
ELA: 32.578 Jacobi stage: ITER: 80157 RES: 3.2476e-06
ELA: 32.678 Jacobi stage: ITER: 80407 RES: 3.1262e-06
ELA: 32.778 Jacobi stage: ITER: 80659 RES: 3.0075e-06
ELA: 32.879 Jacobi stage: ITER: 80909 RES: 2.8934e-06
ELA: 32.979 Jacobi stage: ITER: 81159 RES: 2.7852e-06
ELA: 33.079 Jacobi stage: ITER: 81411 RES: 2.6795e-06
ELA: 33.179 Jacobi stage: ITER: 81661 RES: 2.5777e-06
ELA: 33.279 Jacobi stage: ITER: 81911 RES: 2.4814e-06
ELA: 33.379 Jacobi stage: ITER: 82161 RES: 2.3872e-06
ELA: 33.479 Jacobi stage: ITER: 82411 RES: 2.2979e-06
ELA: 33.579 Jacobi stage: ITER: 82663 RES: 2.2107e-06
ELA: 33.679 Jacobi stage: ITER: 82913 RES: 2.1268e-06
ELA: 33.78 Jacobi stage: ITER: 83163 RES: 2.0473e-06
ELA: 33.88 Jacobi stage: ITER: 83415 RES: 1.9695e-06
ELA: 33.98 Jacobi stage: ITER: 83665 RES: 1.8948e-06
ELA: 34.08 Jacobi stage: ITER: 83915 RES: 1.8239e-06
ELA: 34.18 Jacobi stage: ITER: 84167 RES: 1.7547e-06
ELA: 34.28 Jacobi stage: ITER: 84417 RES: 1.6881e-06
ELA: 34.38 Jacobi stage: ITER: 84667 RES: 1.625e-06
ELA: 34.48 Jacobi stage: ITER: 84919 RES: 1.5633e-06
ELA: 34.581 Jacobi stage: ITER: 85167 RES: 1.5048e-06
ELA: 34.681 Jacobi stage: ITER: 85419 RES: 1.4477e-06
ELA: 34.781 Jacobi stage: ITER: 85669 RES: 1.3927e-06
ELA: 34.881 Jacobi stage: ITER: 85919 RES: 1.3407e-06
ELA: 34.981 Jacobi stage: ITER: 86171 RES: 1.2898e-06
ELA: 35.081 Jacobi stage: ITER: 86421 RES: 1.2408e-06
ELA: 35.181 Jacobi stage: ITER: 86671 RES: 1.1944e-06
ELA: 35.281 Jacobi stage: ITER: 86923 RES: 1.1491e-06
ELA: 35.382 Jacobi stage: ITER: 87173 RES: 1.1055e-06
ELA: 35.482 Jacobi stage: ITER: 87423 RES: 1.0641e-06
ELA: 35.582 Jacobi stage: ITER: 87675 RES: 1.0237e-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 * Set the following matrix (dots represent zero matrix elements):
15 *
16 * / 2.5 -1 . . . \
17 * | -1 2.5 -1 . . |
18 * | . -1 2.5 -1. . |
19 * | . . -1 2.5 -1 |
20 * \ . . . -1 2.5 /
21 */
24 const int size( 5 );
25 auto matrix_ptr = std::make_shared< MatrixType >();
26 matrix_ptr->setDimensions( size, size );
27 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
28
29 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
30 {
31 const int rowIdx = row.getRowIndex();
32 if( rowIdx == 0 ) {
33 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
34 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
35 }
36 else if( rowIdx == size - 1 ) {
37 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
38 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
39 }
40 else {
41 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
42 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
43 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
44 }
45 };
46
47 /***
48 * Set the matrix elements.
49 */
50 matrix_ptr->forAllRows( f );
51 std::cout << *matrix_ptr << '\n';
52
53 /***
54 * Set the right-hand side vector.
55 */
56 Vector x( size, 1.0 );
57 Vector b( size );
58 matrix_ptr->vectorProduct( x, b );
59 x = 0.0;
60 std::cout << "Vector b = " << b << '\n';
61
62 /***
63 * Solve the linear system using diagonal (Jacobi) preconditioner.
64 */
67 auto preconditioner_ptr = std::make_shared< Preconditioner >();
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 );
74 std::cout << "Vector x = " << x << '\n';
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 * Set the following matrix (dots represent zero matrix elements):
14 *
15 * / 2.5 -1 . . . \
16 * | -1 2.5 -1 . . |
17 * | . -1 2.5 -1. . |
18 * | . . -1 2.5 -1 |
19 * \ . . . -1 2.5 /
20 */
23 const int size( 5 );
24 auto matrix_ptr = std::make_shared< MatrixType >();
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
28 auto f = [ = ] __cuda_callable__( typename MatrixType::RowView & row ) mutable
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 ); // diagonal element
33 row.setElement( 1, rowIdx + 1, -1 ); // element above the diagonal
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
37 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 ); // element below the diagonal
41 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
42 row.setElement( 2, rowIdx + 1, -1.0 ); // element above the diagonal
43 }
44 };
45
46 /***
47 * Set the matrix elements.
48 */
49 matrix_ptr->forAllRows( f );
50 std::cout << *matrix_ptr << '\n';
51
52 /***
53 * Set the right-hand side vector.
54 */
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
59 std::cout << "Vector b = " << b << '\n';
60
61 /***
62 * Solve the linear system using diagonal (Jacobi) preconditioner.
63 */
64 auto solver_ptr = TNL::Solvers::getLinearSolver< MatrixType >( "tfqmr" );
65 auto preconditioner_ptr = TNL::Solvers::getPreconditioner< MatrixType >( "diagonal" );
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 );
71 std::cout << "Vector x = " << x << '\n';
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 ]