Template Numerical Library version main:e6bb26f
Public Types | Public Member Functions | Static Public Member Functions | List of all members
TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix > Class Template Reference

Base class for preconditioners of of iterative solvers of linear systems. More...

#include <TNL/Solvers/Linear/Preconditioners/Preconditioner.h>

Inheritance diagram for TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >:
Inheritance graph
[legend]

Public Types

using ConstVectorViewType = typename Traits< Matrix >::ConstVectorViewType
 Type for constant vector view.
 
using DeviceType = typename Matrix::DeviceType
 Device where the solver will run on and auxillary data will alloacted on. More...
 
using IndexType = typename Matrix::IndexType
 Type for indexing.
 
using MatrixPointer = std::shared_ptr< std::add_const_t< MatrixType > >
 Type of shared pointer to the matrix.
 
using MatrixType = Matrix
 Type of the matrix representing the linear system.
 
using RealType = typename Matrix::RealType
 Floating point type used for computations.
 
using VectorViewType = typename Traits< Matrix >::VectorViewType
 Type for vector view.
 

Public Member Functions

virtual ~Preconditioner ()
 Destructor of the preconditioner.
 
virtual bool setup (const Config::ParameterContainer &parameters, const String &prefix="")
 Method for setup of the preconditioner of linear iterative solver based on configuration parameters. More...
 
virtual void solve (ConstVectorViewType b, VectorViewType x) const
 This method applies the preconditioner. More...
 
virtual void update (const MatrixPointer &matrixPointer)
 This method updates the preconditioner with respect to given matrix. More...
 

Static Public Member Functions

static void configSetup (Config::ConfigDescription &config, const String &prefix="")
 This method defines configuration entries for setup of the preconditioner of linear iterative solver. More...
 

Detailed Description

template<typename Matrix>
class TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >

Base class for preconditioners of of iterative solvers of linear systems.

Template Parameters
Matrixis type of matrix describing the linear system.

The following example shows how to setup an iterative solver of linear systems with preconditioning:

1#include <iostream>
2#include <memory>
3#include <TNL/Algorithms/ParallelFor.h>
4#include <TNL/Matrices/SparseMatrix.h>
5#include <TNL/Devices/Host.h>
6#include <TNL/Devices/Cuda.h>
7#include <TNL/Solvers/Linear/TFQMR.h>
8#include <TNL/Solvers/Linear/Preconditioners/Diagonal.h>
9
10template< typename Device >
11void iterativeLinearSolverExample()
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 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 )
32 {
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 {
38 row.setElement( 0, rowIdx-1, -1.0 ); // element below the diagonal
39 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
40 }
41 else
42 {
43 row.setElement( 0, rowIdx-1, -1.0 ); // element below the diagonal
44 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
45 row.setElement( 2, rowIdx+1, -1.0 ); // element above the diagonal
46 }
47 };
48
49 /***
50 * Set the matrix elements.
51 */
52 matrix_ptr->forAllRows( f );
53 std::cout << *matrix_ptr << std::endl;
54
55 /***
56 * Set the right-hand side vector.
57 */
58 Vector x( size, 1.0 );
59 Vector b( size );
60 matrix_ptr->vectorProduct( x, b );
61 x = 0.0;
62 std::cout << "Vector b = " << b << std::endl;
63
64 /***
65 * Solve the linear system using diagonal (Jacobi) preconditioner.
66 */
69 auto preconditioner_ptr = std::make_shared< Preconditioner >();
70 preconditioner_ptr->update( matrix_ptr );
71 LinearSolver solver;
72 solver.setMatrix( matrix_ptr );
73 solver.setPreconditioner( preconditioner_ptr );
74 solver.setConvergenceResidue( 1.0e-6 );
75 solver.solve( b, x );
76 std::cout << "Vector x = " << x << std::endl;
77}
78
79int main( int argc, char* argv[] )
80{
81 std::cout << "Solving linear system on host: " << std::endl;
82 iterativeLinearSolverExample< TNL::Devices::Sequential >();
83
84#ifdef HAVE_CUDA
85 std::cout << "Solving linear system on CUDA device: " << std::endl;
86 iterativeLinearSolverExample< TNL::Devices::Cuda >();
87#endif
88}
#define __cuda_callable__
Definition: CudaCallable.h:22
Vector extends Array with algebraic operations.
Definition: Vector.h:40
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition: SparseMatrix.h:57
Diagonal (Jacobi) preconditioner for iterative solvers of linear systems.
Definition: Diagonal.h:29
Matrix MatrixType
Type of the matrix representing the linear system.
Definition: Preconditioner.h:70
Iterative solver of linear systems based on the Transpose-free quasi-minimal residual (TFQMR) method.
Definition: TFQMR.h:26
T endl(T... args)

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 ]

Member Typedef Documentation

◆ DeviceType

template<typename Matrix >
using TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >::DeviceType = typename Matrix::DeviceType

Device where the solver will run on and auxillary data will alloacted on.

See Devices::Host or Devices::Cuda.

Member Function Documentation

◆ configSetup()

template<typename Matrix >
static void TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >::configSetup ( Config::ConfigDescription config,
const String prefix = "" 
)
inlinestatic

This method defines configuration entries for setup of the preconditioner of linear iterative solver.

Parameters
configcontains description of configuration parameters.
prefixis a prefix of particular configuration entries.

◆ setup()

template<typename Matrix >
virtual bool TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >::setup ( const Config::ParameterContainer parameters,
const String prefix = "" 
)
inlinevirtual

Method for setup of the preconditioner of linear iterative solver based on configuration parameters.

Parameters
parameterscontains values of the define configuration entries.
prefixis a prefix of particular configuration entries.

Reimplemented in TNL::Solvers::Linear::Preconditioners::ILUT_impl< Matrix, Real, Devices::Host, Index >.

◆ solve()

template<typename Matrix >
virtual void TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >::solve ( ConstVectorViewType  b,
VectorViewType  x 
) const
inlinevirtual

◆ update()

template<typename Matrix >
virtual void TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix >::update ( const MatrixPointer matrixPointer)
inlinevirtual

The documentation for this class was generated from the following file: