Template Numerical Library: TNL::Solvers::Linear::Preconditioners::Preconditioner< Matrix > Class Template Reference

Template Numerical Library version\ main:0b2c40fb1

Loading...
Searching...
No Matches
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 >:
[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 auxiliary data will alloacted on.
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 ()=default
| 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.
virtual void | solve (ConstVectorViewType b, VectorViewType x) const
| This method applies the preconditioner.
virtual void | update (const MatrixPointer &matrixPointer)
| This method updates the preconditioner with respect to given matrix.

## 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.

## 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

  Matrix | is 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/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 */
22 using MatrixType = TNL::Matrices::SparseMatrix< double, Device >;
23 using Vector = TNL::Containers::Vector< double, Device >;
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 */
65 using LinearSolver = TNL::Solvers::Linear::TFQMR< MatrixType >;
66 using Preconditioner = TNL::Solvers::Linear::Preconditioners::Diagonal< MatrixType >;
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}
__cuda_callable__
#define __cuda_callable__
Definition Macros.h:49
std::cout
TNL::Containers::Vector
Vector extends Array with algebraic operations.
Definition Vector.h:37
TNL::Matrices::SparseMatrix
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:57
TNL::Solvers::Linear::Preconditioners::Diagonal
Diagonal (Jacobi) preconditioner for iterative solvers of linear systems.
Definition Diagonal.h:21
TNL::Solvers::Linear::Preconditioners::Preconditioner::MatrixType
Matrix MatrixType
Type of the matrix representing the linear system.
Definition Preconditioner.h:62
TNL::Solvers::Linear::TFQMR
Iterative solver of linear systems based on the Transpose-free quasi-minimal residual (TFQMR) method.
Definition TFQMR.h:21
std::make_shared
T make_shared(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 ]

## 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 auxiliary data will alloacted on.

See Devices::Host or Devices::Cuda.

## Member Function Documentation

## ◆ configSetup()

template<typename Matrix>

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

  config | contains description of configuration parameters.
  prefix | is 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

  parameters | contains values of the define configuration entries.
  prefix | is 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

This method applies the preconditioner.

Parameters

  b | is the input vector the preconditioner is applied on.
  x | is the result of the preconditioning.

Reimplemented in TNL::Solvers::Linear::Preconditioners::Diagonal< Matrix >, TNL::Solvers::Linear::Preconditioners::Diagonal< Matrices::DistributedMatrix< Matrix > >, TNL::Solvers::Linear::Preconditioners::ILU0_impl< Matrix, Real, Devices::Cuda, Index >, TNL::Solvers::Linear::Preconditioners::ILU0_impl< Matrix, Real, Devices::Host, Index >, TNL::Solvers::Linear::Preconditioners::ILUT_impl< Matrix, Real, Devices::Cuda, Index >, and TNL::Solvers::Linear::Preconditioners::ILUT_impl< Matrix, Real, Devices::Host, Index >.

## ◆ update()

template<typename Matrix>

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

This method updates the preconditioner with respect to given matrix.

Parameters

  matrixPointer | smart pointer (std::shared_ptr) to matrix the preconditioner is related to.

Reimplemented in TNL::Solvers::Linear::Preconditioners::Diagonal< Matrix >, TNL::Solvers::Linear::Preconditioners::Diagonal< Matrices::DistributedMatrix< Matrix > >, TNL::Solvers::Linear::Preconditioners::ILU0_impl< Matrix, Real, Devices::Cuda, Index >, TNL::Solvers::Linear::Preconditioners::ILU0_impl< Matrix, Real, Devices::Host, Index >, TNL::Solvers::Linear::Preconditioners::ILUT_impl< Matrix, Real, Devices::Cuda, Index >, and TNL::Solvers::Linear::Preconditioners::ILUT_impl< Matrix, Real, Devices::Host, Index >.

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

- src/TNL/Solvers/Linear/Preconditioners/Preconditioner.h