Template Numerical Library version\ main:df396df
Loading...
Searching...
No Matches
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 >:

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.
 
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.
 
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
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/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 << std::endl;
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 << std::endl;
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 << std::endl;
75}
76
77int
78main( int argc, char* argv[] )
79{
80 std::cout << "Solving linear system on host: " << std::endl;
81 iterativeLinearSolverExample< TNL::Devices::Sequential >();
82
83#ifdef __CUDACC__
84 std::cout << "Solving linear system on CUDA device: " << std::endl;
85 iterativeLinearSolverExample< TNL::Devices::Cuda >();
86#endif
87}
#define __cuda_callable__
Definition Macros.h:49
Vector extends Array with algebraic operations.
Definition Vector.h:36
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:57
Diagonal (Jacobi) preconditioner for iterative solvers of linear systems.
Definition Diagonal.h:21
Matrix MatrixType
Type of the matrix representing the linear system.
Definition Preconditioner.h:62
Iterative solver of linear systems based on the Transpose-free quasi-minimal residual (TFQMR) method.
Definition TFQMR.h:21
T endl(T... args)
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 ]
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()

◆ update()


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