Template Numerical Library version\ main:c4e3186
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/GPU.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#if defined( __CUDACC__ ) || defined( __HIP__ )
79 std::cout << "Solving linear system on GPU (" << TNL::Backend::getBackendName() << "):\n";
80 iterativeLinearSolverExample< TNL::Devices::GPU >();
81#endif
82}
#define __cuda_callable__
This macro serves for annotating functions which are supposed to be called even from the GPU device.
Definition Macros.h:50
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:59
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)
constexpr const char * getBackendName()
Returns the name of the GPU backend: "CUDA", "HIP", or "none" when no GPU backend is available.
Definition Functions.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 GPU (CUDA):
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/GPU.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#if defined( __CUDACC__ ) || defined( __HIP__ )
92 std::cout << "Solving linear system on GPU (" << TNL::Backend::getBackendName() << "):\n";
93 iterativeLinearSolverExample< TNL::Devices::GPU >();
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: 30595 RES: 0.006575
Jacobi stage: ITER: 60579 RES: 6.5723e-05
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]
Solving linear system on GPU (CUDA):
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: 659 RES: 0.81186
Jacobi stage: ITER: 1283 RES: 0.68674
Jacobi stage: ITER: 1727 RES: 0.61826
Jacobi stage: ITER: 2169 RES: 0.56159
Jacobi stage: ITER: 2615 RES: 0.51367
Jacobi stage: ITER: 3057 RES: 0.47233
Jacobi stage: ITER: 3501 RES: 0.43606
Jacobi stage: ITER: 3943 RES: 0.40407
Jacobi stage: ITER: 4391 RES: 0.37476
Jacobi stage: ITER: 4847 RES: 0.34772
Jacobi stage: ITER: 5309 RES: 0.32263
Jacobi stage: ITER: 5735 RES: 0.30156
Jacobi stage: ITER: 6195 RES: 0.28044
Jacobi stage: ITER: 6651 RES: 0.2611
Jacobi stage: ITER: 7059 RES: 0.24501
Jacobi stage: ITER: 7475 RES: 0.22968
Jacobi stage: ITER: 7675 RES: 0.22267
Jacobi stage: ITER: 7827 RES: 0.21749
Jacobi stage: ITER: 8141 RES: 0.20712
Jacobi stage: ITER: 8477 RES: 0.19665
Jacobi stage: ITER: 8755 RES: 0.18845
Jacobi stage: ITER: 9007 RES: 0.18127
Jacobi stage: ITER: 9185 RES: 0.17631
Jacobi stage: ITER: 9441 RES: 0.16949
Jacobi stage: ITER: 9831 RES: 0.15967
Jacobi stage: ITER: 10169 RES: 0.15153
Jacobi stage: ITER: 10639 RES: 0.141
Jacobi stage: ITER: 11095 RES: 0.13146
Jacobi stage: ITER: 11539 RES: 0.12279
Jacobi stage: ITER: 11985 RES: 0.11462
Jacobi stage: ITER: 12447 RES: 0.10679
Jacobi stage: ITER: 12907 RES: 0.099508
Jacobi stage: ITER: 13281 RES: 0.093922
Jacobi stage: ITER: 13733 RES: 0.087622
Jacobi stage: ITER: 14191 RES: 0.081694
Jacobi stage: ITER: 14647 RES: 0.076167
Jacobi stage: ITER: 15109 RES: 0.070928
Jacobi stage: ITER: 15563 RES: 0.06617
Jacobi stage: ITER: 16023 RES: 0.061656
Jacobi stage: ITER: 16495 RES: 0.057344
Jacobi stage: ITER: 16947 RES: 0.053498
Jacobi stage: ITER: 17411 RES: 0.049817
Jacobi stage: ITER: 17869 RES: 0.046419
Jacobi stage: ITER: 18335 RES: 0.043226
Jacobi stage: ITER: 18793 RES: 0.040277
Jacobi stage: ITER: 19251 RES: 0.037552
Jacobi stage: ITER: 19715 RES: 0.034969
Jacobi stage: ITER: 20179 RES: 0.032564
Jacobi stage: ITER: 20643 RES: 0.030324
Jacobi stage: ITER: 21095 RES: 0.02829
Jacobi stage: ITER: 21561 RES: 0.026327
Jacobi stage: ITER: 22023 RES: 0.024531
Jacobi stage: ITER: 22487 RES: 0.022844
Jacobi stage: ITER: 22945 RES: 0.021285
Jacobi stage: ITER: 23411 RES: 0.019821
Jacobi stage: ITER: 23869 RES: 0.018469
Jacobi stage: ITER: 24327 RES: 0.01722
Jacobi stage: ITER: 24787 RES: 0.016045
Jacobi stage: ITER: 25243 RES: 0.01496
Jacobi stage: ITER: 25703 RES: 0.013939
Jacobi stage: ITER: 26173 RES: 0.012964
Jacobi stage: ITER: 26607 RES: 0.012132
Jacobi stage: ITER: 27051 RES: 0.011332
Jacobi stage: ITER: 27509 RES: 0.010559
Jacobi stage: ITER: 27981 RES: 0.0098207
Jacobi stage: ITER: 28431 RES: 0.0091676
Jacobi stage: ITER: 28795 RES: 0.0086691
Jacobi stage: ITER: 29407 RES: 0.0078913
Jacobi stage: ITER: 30079 RES: 0.0071174
Jacobi stage: ITER: 32311 RES: 0.0050516
Jacobi stage: ITER: 33671 RES: 0.0040992
Jacobi stage: ITER: 34087 RES: 0.0038455
Jacobi stage: ITER: 34587 RES: 0.0035612
Jacobi stage: ITER: 34699 RES: 0.0035005
Jacobi stage: ITER: 35171 RES: 0.0032557
Jacobi stage: ITER: 35899 RES: 0.0029112
Jacobi stage: ITER: 36177 RES: 0.0027887
Jacobi stage: ITER: 36545 RES: 0.0026354
Jacobi stage: ITER: 37245 RES: 0.0023668
Jacobi stage: ITER: 37669 RES: 0.0022175
Jacobi stage: ITER: 38187 RES: 0.0020486
Jacobi stage: ITER: 38609 RES: 0.0019194
Jacobi stage: ITER: 40281 RES: 0.0014847
Jacobi stage: ITER: 40697 RES: 0.0013928
Jacobi stage: ITER: 41891 RES: 0.0011597
Jacobi stage: ITER: 42425 RES: 0.0010681
Jacobi stage: ITER: 43215 RES: 0.00094632
Jacobi stage: ITER: 43841 RES: 0.0008593
Jacobi stage: ITER: 44699 RES: 0.00075343
Jacobi stage: ITER: 45961 RES: 0.00062048
Jacobi stage: ITER: 46315 RES: 0.00058782
Jacobi stage: ITER: 46625 RES: 0.00056031
Jacobi stage: ITER: 46983 RES: 0.0005305
Jacobi stage: ITER: 47327 RES: 0.00050319
Jacobi stage: ITER: 47667 RES: 0.00047759
Jacobi stage: ITER: 47967 RES: 0.00045608
Jacobi stage: ITER: 48279 RES: 0.00043474
Jacobi stage: ITER: 48651 RES: 0.00041059
Jacobi stage: ITER: 48981 RES: 0.00039018
Jacobi stage: ITER: 49285 RES: 0.00037238
Jacobi stage: ITER: 49587 RES: 0.00035561
Jacobi stage: ITER: 50507 RES: 0.00030875
Jacobi stage: ITER: 51115 RES: 0.00028122
Jacobi stage: ITER: 51551 RES: 0.000263
Jacobi stage: ITER: 52033 RES: 0.00024416
Jacobi stage: ITER: 52603 RES: 0.00022376
Jacobi stage: ITER: 53319 RES: 0.00020046
Jacobi stage: ITER: 54181 RES: 0.00017554
Jacobi stage: ITER: 54835 RES: 0.00015881
Jacobi stage: ITER: 55457 RES: 0.0001443
Jacobi stage: ITER: 56923 RES: 0.00011524
Jacobi stage: ITER: 57447 RES: 0.00010633
Jacobi stage: ITER: 57971 RES: 9.8105e-05
Jacobi stage: ITER: 58419 RES: 9.1581e-05
Jacobi stage: ITER: 58859 RES: 8.5596e-05
Jacobi stage: ITER: 59349 RES: 7.9366e-05
Jacobi stage: ITER: 59791 RES: 7.4179e-05
Jacobi stage: ITER: 60255 RES: 6.9076e-05
Jacobi stage: ITER: 60787 RES: 6.3656e-05
Jacobi stage: ITER: 61299 RES: 5.8842e-05
Jacobi stage: ITER: 61819 RES: 5.4325e-05
Jacobi stage: ITER: 62323 RES: 5.0278e-05
Jacobi stage: ITER: 62887 RES: 4.6105e-05
Jacobi stage: ITER: 63413 RES: 4.2514e-05
Jacobi stage: ITER: 63935 RES: 3.925e-05
Jacobi stage: ITER: 64559 RES: 3.5663e-05
Jacobi stage: ITER: 65087 RES: 3.2885e-05
Jacobi stage: ITER: 65619 RES: 3.0304e-05
Jacobi stage: ITER: 66171 RES: 2.7841e-05
Jacobi stage: ITER: 66739 RES: 2.5515e-05
Jacobi stage: ITER: 67473 RES: 2.2787e-05
Jacobi stage: ITER: 67789 RES: 2.1708e-05
Jacobi stage: ITER: 68151 RES: 2.054e-05
Jacobi stage: ITER: 68699 RES: 1.8882e-05
Jacobi stage: ITER: 69095 RES: 1.7768e-05
Jacobi stage: ITER: 69479 RES: 1.675e-05
Jacobi stage: ITER: 69825 RES: 1.5878e-05
Jacobi stage: ITER: 70219 RES: 1.495e-05
Jacobi stage: ITER: 70593 RES: 1.4111e-05
Jacobi stage: ITER: 71019 RES: 1.3221e-05
Jacobi stage: ITER: 71263 RES: 1.2735e-05
Jacobi stage: ITER: 71767 RES: 1.1786e-05
Jacobi stage: ITER: 72275 RES: 1.0902e-05
Jacobi stage: ITER: 72667 RES: 1.0265e-05
Jacobi stage: ITER: 72975 RES: 9.7904e-06
Jacobi stage: ITER: 73437 RES: 9.1169e-06
Jacobi stage: ITER: 73903 RES: 8.4897e-06
Jacobi stage: ITER: 74243 RES: 8.0577e-06
Jacobi stage: ITER: 74685 RES: 7.5265e-06
Jacobi stage: ITER: 75135 RES: 7.026e-06
Jacobi stage: ITER: 75655 RES: 6.4866e-06
Jacobi stage: ITER: 76171 RES: 5.9924e-06
Jacobi stage: ITER: 76663 RES: 5.5562e-06
Jacobi stage: ITER: 77091 RES: 5.2027e-06
Jacobi stage: ITER: 77753 RES: 4.6982e-06
Jacobi stage: ITER: 78427 RES: 4.2375e-06
Jacobi stage: ITER: 79075 RES: 3.836e-06
Jacobi stage: ITER: 79671 RES: 3.5004e-06
Jacobi stage: ITER: 81871 RES: 2.4967e-06
Jacobi stage: ITER: 83177 RES: 2.0422e-06
Jacobi stage: ITER: 83893 RES: 1.8295e-06
Jacobi stage: ITER: 84287 RES: 1.7226e-06
Jacobi stage: ITER: 84851 RES: 1.5797e-06
Jacobi stage: ITER: 85515 RES: 1.4265e-06
Jacobi stage: ITER: 86177 RES: 1.2882e-06
Jacobi stage: ITER: 86839 RES: 1.164e-06
Jacobi stage: ITER: 87503 RES: 1.0511e-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/GPU.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#if defined( __CUDACC__ ) || defined( __HIP__ )
96 std::cout << "Solving linear system on GPU (" << TNL::Backend::getBackendName() << "):\n";
97 iterativeLinearSolverExample< TNL::Devices::GPU >();
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:8.919e-0
ELA: 0.1001 Jacobi stage: ITER: 30965 RES: 0.0062099
ELA: 0.2002 Jacobi stage: ITER: 62147 RES: 5.1655e-05
Vector x = [ 0.999999, 0.999999, 0.999998, 0.999999, 0.999999 ]
Solving linear system on GPU (CUDA):
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.8651e-
ELA: 0.10014 Jacobi stage: ITER: 1405 RES: 0.6662
ELA: 0.20133 Jacobi stage: ITER: 2103 RES: 0.56964
ELA: 0.30145 Jacobi stage: ITER: 2557 RES: 0.51931
ELA: 0.40166 Jacobi stage: ITER: 2999 RES: 0.47724
ELA: 0.50176 Jacobi stage: ITER: 3443 RES: 0.44071
ELA: 0.6019 Jacobi stage: ITER: 3887 RES: 0.40795
ELA: 0.70199 Jacobi stage: ITER: 4329 RES: 0.37852
ELA: 0.80291 Jacobi stage: ITER: 4775 RES: 0.35182
ELA: 0.903 Jacobi stage: ITER: 5219 RES: 0.32743
ELA: 1.0031 Jacobi stage: ITER: 5669 RES: 0.30463
ELA: 1.1032 Jacobi stage: ITER: 6131 RES: 0.28328
ELA: 1.2052 Jacobi stage: ITER: 6579 RES: 0.26406
ELA: 1.3053 Jacobi stage: ITER: 7221 RES: 0.23884
ELA: 1.4054 Jacobi stage: ITER: 7475 RES: 0.22968
ELA: 1.5055 Jacobi stage: ITER: 7885 RES: 0.21548
ELA: 1.6056 Jacobi stage: ITER: 8151 RES: 0.20686
ELA: 1.7057 Jacobi stage: ITER: 8635 RES: 0.19197
ELA: 1.8058 Jacobi stage: ITER: 8985 RES: 0.18183
ELA: 1.9059 Jacobi stage: ITER: 9291 RES: 0.17351
ELA: 2.006 Jacobi stage: ITER: 9631 RES: 0.16466
ELA: 2.1061 Jacobi stage: ITER: 9967 RES: 0.15636
ELA: 2.2062 Jacobi stage: ITER: 10249 RES: 0.14967
ELA: 2.3063 Jacobi stage: ITER: 10597 RES: 0.14187
ELA: 2.4063 Jacobi stage: ITER: 10929 RES: 0.13481
ELA: 2.5064 Jacobi stage: ITER: 11291 RES: 0.12756
ELA: 2.6065 Jacobi stage: ITER: 11495 RES: 0.12362
ELA: 2.7081 Jacobi stage: ITER: 11955 RES: 0.11518
ELA: 2.8096 Jacobi stage: ITER: 12389 RES: 0.10772
ELA: 2.9097 Jacobi stage: ITER: 12837 RES: 0.10055
ELA: 3.0098 Jacobi stage: ITER: 13279 RES: 0.09398
ELA: 3.1099 Jacobi stage: ITER: 13743 RES: 0.087514
ELA: 3.21 Jacobi stage: ITER: 14207 RES: 0.081494
ELA: 3.3101 Jacobi stage: ITER: 15047 RES: 0.071628
ELA: 3.4102 Jacobi stage: ITER: 15393 RES: 0.0679
ELA: 3.5103 Jacobi stage: ITER: 15865 RES: 0.063151
ELA: 3.6104 Jacobi stage: ITER: 16327 RES: 0.058843
ELA: 3.7105 Jacobi stage: ITER: 16783 RES: 0.054862
ELA: 3.8106 Jacobi stage: ITER: 17259 RES: 0.050994
ELA: 3.9107 Jacobi stage: ITER: 17703 RES: 0.047632
ELA: 4.0108 Jacobi stage: ITER: 18171 RES: 0.044329
ELA: 4.1109 Jacobi stage: ITER: 18631 RES: 0.041304
ELA: 4.211 Jacobi stage: ITER: 19099 RES: 0.038439
ELA: 4.3111 Jacobi stage: ITER: 19553 RES: 0.035839
ELA: 4.4112 Jacobi stage: ITER: 20029 RES: 0.033312
ELA: 4.5112 Jacobi stage: ITER: 20481 RES: 0.031078
ELA: 4.6113 Jacobi stage: ITER: 20943 RES: 0.028958
ELA: 4.7114 Jacobi stage: ITER: 21411 RES: 0.026949
ELA: 4.8115 Jacobi stage: ITER: 21871 RES: 0.025111
ELA: 4.9116 Jacobi stage: ITER: 22331 RES: 0.023398
ELA: 5.0117 Jacobi stage: ITER: 22795 RES: 0.021788
ELA: 5.1118 Jacobi stage: ITER: 23253 RES: 0.020302
ELA: 5.2119 Jacobi stage: ITER: 23721 RES: 0.018894
ELA: 5.312 Jacobi stage: ITER: 24187 RES: 0.017594
ELA: 5.4121 Jacobi stage: ITER: 24643 RES: 0.016404
ELA: 5.5122 Jacobi stage: ITER: 25107 RES: 0.015275
ELA: 5.6123 Jacobi stage: ITER: 25567 RES: 0.014233
ELA: 5.7124 Jacobi stage: ITER: 26029 RES: 0.013254
ELA: 5.8125 Jacobi stage: ITER: 26499 RES: 0.012335
ELA: 5.9126 Jacobi stage: ITER: 26945 RES: 0.011515
ELA: 6.0127 Jacobi stage: ITER: 27407 RES: 0.010729
ELA: 6.1128 Jacobi stage: ITER: 27877 RES: 0.0099788
ELA: 6.2129 Jacobi stage: ITER: 28335 RES: 0.0093038
ELA: 6.3131 Jacobi stage: ITER: 28783 RES: 0.0086851
ELA: 6.4132 Jacobi stage: ITER: 29251 RES: 0.0080827
ELA: 6.5133 Jacobi stage: ITER: 29719 RES: 0.007522
ELA: 6.6134 Jacobi stage: ITER: 30179 RES: 0.0070089
ELA: 6.7139 Jacobi stage: ITER: 31415 RES: 0.0057969
ELA: 6.814 Jacobi stage: ITER: 32201 RES: 0.0051361
ELA: 6.9141 Jacobi stage: ITER: 32879 RES: 0.0046295
ELA: 7.0159 Jacobi stage: ITER: 33031 RES: 0.0045227
ELA: 7.116 Jacobi stage: ITER: 33563 RES: 0.0041678
ELA: 7.2161 Jacobi stage: ITER: 35003 RES: 0.0033408
ELA: 7.3162 Jacobi stage: ITER: 35543 RES: 0.0030749
ELA: 7.4176 Jacobi stage: ITER: 37375 RES: 0.0023207
ELA: 7.5177 Jacobi stage: ITER: 39649 RES: 0.001636
ELA: 7.6178 Jacobi stage: ITER: 40753 RES: 0.0013808
ELA: 7.7179 Jacobi stage: ITER: 42887 RES: 0.00099522
ELA: 7.818 Jacobi stage: ITER: 44941 RES: 0.00072572
ELA: 7.9181 Jacobi stage: ITER: 45797 RES: 0.0006363
ELA: 8.0182 Jacobi stage: ITER: 46847 RES: 0.00054169
ELA: 8.1183 Jacobi stage: ITER: 47619 RES: 0.00048112
ELA: 8.2185 Jacobi stage: ITER: 48563 RES: 0.00041618
ELA: 8.3186 Jacobi stage: ITER: 48931 RES: 0.00039331
ELA: 8.4187 Jacobi stage: ITER: 49709 RES: 0.0003489
ELA: 8.5188 Jacobi stage: ITER: 50565 RES: 0.00030591
ELA: 8.6189 Jacobi stage: ITER: 51191 RES: 0.00027795
ELA: 8.719 Jacobi stage: ITER: 52037 RES: 0.00024401
ELA: 8.819 Jacobi stage: ITER: 52999 RES: 0.00021055
ELA: 8.9191 Jacobi stage: ITER: 53391 RES: 0.00019825
ELA: 9.0192 Jacobi stage: ITER: 53799 RES: 0.00018621
ELA: 9.1193 Jacobi stage: ITER: 54497 RES: 0.00016723
ELA: 9.2194 Jacobi stage: ITER: 55763 RES: 0.00013772
ELA: 9.3196 Jacobi stage: ITER: 56841 RES: 0.00011666
ELA: 9.4197 Jacobi stage: ITER: 57183 RES: 0.00011073
ELA: 9.5198 Jacobi stage: ITER: 57457 RES: 0.00010613
ELA: 9.6199 Jacobi stage: ITER: 57753 RES: 0.00010141
ELA: 9.72 Jacobi stage: ITER: 58079 RES: 9.6491e-05
ELA: 9.8201 Jacobi stage: ITER: 58509 RES: 9.0296e-05
ELA: 9.9202 Jacobi stage: ITER: 58813 RES: 8.6176e-05
ELA: 10.02 Jacobi stage: ITER: 59113 RES: 8.2296e-05
ELA: 10.122 Jacobi stage: ITER: 59397 RES: 7.8783e-05
ELA: 10.222 Jacobi stage: ITER: 59763 RES: 7.4499e-05
ELA: 10.324 Jacobi stage: ITER: 60307 RES: 6.8527e-05
ELA: 10.426 Jacobi stage: ITER: 61547 RES: 5.6642e-05
ELA: 10.526 Jacobi stage: ITER: 62559 RES: 4.8488e-05
ELA: 10.626 Jacobi stage: ITER: 63355 RES: 4.2907e-05
ELA: 10.727 Jacobi stage: ITER: 63763 RES: 4.0301e-05
ELA: 10.827 Jacobi stage: ITER: 64257 RES: 3.7345e-05
ELA: 10.927 Jacobi stage: ITER: 64701 RES: 3.4883e-05
ELA: 11.027 Jacobi stage: ITER: 65043 RES: 3.3108e-05
ELA: 11.127 Jacobi stage: ITER: 65337 RES: 3.1636e-05
ELA: 11.227 Jacobi stage: ITER: 65609 RES: 3.0342e-05
ELA: 11.327 Jacobi stage: ITER: 65927 RES: 2.8904e-05
ELA: 11.429 Jacobi stage: ITER: 66279 RES: 2.7383e-05
ELA: 11.534 Jacobi stage: ITER: 66747 RES: 2.5483e-05
ELA: 11.634 Jacobi stage: ITER: 67227 RES: 2.3672e-05
ELA: 11.734 Jacobi stage: ITER: 67677 RES: 2.2084e-05
ELA: 11.834 Jacobi stage: ITER: 68119 RES: 2.0641e-05
ELA: 11.939 Jacobi stage: ITER: 68659 RES: 1.8998e-05
ELA: 12.045 Jacobi stage: ITER: 69199 RES: 1.7486e-05
ELA: 12.147 Jacobi stage: ITER: 69731 RES: 1.6114e-05
ELA: 12.247 Jacobi stage: ITER: 70255 RES: 1.4868e-05
ELA: 12.347 Jacobi stage: ITER: 70755 RES: 1.3769e-05
ELA: 12.447 Jacobi stage: ITER: 71279 RES: 1.2704e-05
ELA: 12.547 Jacobi stage: ITER: 71793 RES: 1.1736e-05
ELA: 12.647 Jacobi stage: ITER: 72267 RES: 1.0915e-05
ELA: 12.748 Jacobi stage: ITER: 72791 RES: 1.0071e-05
ELA: 12.848 Jacobi stage: ITER: 73309 RES: 9.2979e-06
ELA: 12.948 Jacobi stage: ITER: 73797 RES: 8.6264e-06
ELA: 13.048 Jacobi stage: ITER: 74291 RES: 7.9985e-06
ELA: 13.148 Jacobi stage: ITER: 74737 RES: 7.4666e-06
ELA: 13.248 Jacobi stage: ITER: 75359 RES: 6.7884e-06
ELA: 13.348 Jacobi stage: ITER: 77079 RES: 5.2123e-06
ELA: 13.448 Jacobi stage: ITER: 77615 RES: 4.8003e-06
ELA: 13.548 Jacobi stage: ITER: 78409 RES: 4.2479e-06
ELA: 13.648 Jacobi stage: ITER: 78885 RES: 3.9484e-06
ELA: 13.748 Jacobi stage: ITER: 79265 RES: 3.7245e-06
ELA: 13.849 Jacobi stage: ITER: 79717 RES: 3.4747e-06
ELA: 13.949 Jacobi stage: ITER: 80227 RES: 3.2139e-06
ELA: 14.049 Jacobi stage: ITER: 80671 RES: 3.002e-06
ELA: 14.149 Jacobi stage: ITER: 81183 RES: 2.775e-06
ELA: 14.249 Jacobi stage: ITER: 81727 RES: 2.5525e-06
ELA: 14.349 Jacobi stage: ITER: 82239 RES: 2.3595e-06
ELA: 14.449 Jacobi stage: ITER: 82735 RES: 2.1864e-06
ELA: 14.549 Jacobi stage: ITER: 83489 RES: 1.9467e-06
ELA: 14.649 Jacobi stage: ITER: 83951 RES: 1.8139e-06
ELA: 14.749 Jacobi stage: ITER: 84259 RES: 1.7301e-06
ELA: 14.849 Jacobi stage: ITER: 84751 RES: 1.6041e-06
ELA: 14.95 Jacobi stage: ITER: 85199 RES: 1.4975e-06
ELA: 15.05 Jacobi stage: ITER: 85685 RES: 1.3893e-06
ELA: 15.15 Jacobi stage: ITER: 86183 RES: 1.2874e-06
ELA: 15.25 Jacobi stage: ITER: 86699 RES: 1.1893e-06
ELA: 15.35 Jacobi stage: ITER: 87175 RES: 1.1055e-06
ELA: 15.45 Jacobi stage: ITER: 87615 RES: 1.0332e-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/GPU.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#if defined( __CUDACC__ ) || defined( __HIP__ )
84 std::cout << "Solving linear system on GPU (" << TNL::Backend::getBackendName() << "):\n";
85 iterativeLinearSolverExample< TNL::Devices::GPU >();
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 GPU (CUDA):
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/GPU.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#if defined( __CUDACC__ ) || defined( __HIP__ )
81 std::cout << "Solving linear system on GPU (" << TNL::Backend::getBackendName() << "):\n";
82 iterativeLinearSolverExample< TNL::Devices::GPU >();
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 GPU (CUDA):
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 ]