Template Numerical Library: TNL::Solvers Namespace Reference

Template Numerical Library version\ main:be7e16d88

Loading...
Searching...
No Matches
Namespaces | Classes | Functions
TNL::Solvers Namespace Reference

Namespace for solvers. More...

## Namespaces
namespace | Linear
| Namespace for linear system solvers.
namespace | ODE
| Namespace for solvers of ordinary differential equations.

## Classes
class | GinkgoConvergenceLoggerMonitor
| A Ginkgo Convergence logger with a TNL iterative solver monitor. More...
class | IterativeSolver
| Base class for iterative solvers. More...
class | IterativeSolverMonitor
| Object for monitoring convergence of iterative solvers. More...
class | SolverMonitor
| Base class for solver monitors. More...
class | SolverMonitorThread
| A RAII wrapper for launching the SolverMonitor's main loop in a separate thread. More...
class | StaticIterativeSolver
| Base class for iterative solvers. More...

## Functions
template<typename MatrixType>
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.
std::vector< std::string > | getLinearSolverOptions ()
| Function returning available linear solvers.
template<typename MatrixType>
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.
std::vector< std::string > | getPreconditionerOptions ()
| Function returning available linear preconditioners.

## Detailed Description

Namespace for solvers.

## Function Documentation

## ◆ getLinearSolver()

template<typename MatrixType>

std::shared_ptr< Linear::LinearSolver< MatrixType > > TNL::Solvers::getLinearSolver | ( | const std::string & | name | ) |

Function returning shared pointer with linear solver given by its name in a form of a string.

Template Parameters

  MatrixType | is a type of matrix defining the system of linear equations.

Parameters

  name | of the linear solver. The name can be one of the following: jacobi - for the Jacobi solver - TNL::Solvers::Linear::Jacobi. sor - for SOR solver - TNL::Solvers::Linear::SOR. cg - for CG solver - TNL::Solvers::Linear::CG. bicgstab - for BICGStab solver - TNL::Solvers::Linear::BICGStab. bicgstabl - for BICGStab(l) solver - TNL::Solvers::Linear::BICGStabL. gmres - for GMRES solver - TNL::Solvers::Linear::GMRES. tfqmr - for TFQMR solver - TNL::Solvers::Linear::TFQMR. idrs - for IDR(s) solver - TNL::Solvers::Linear::IDRs. gem - for GEM solver - TNL::Solvers::Linear::GEM.

Returns
  shared pointer with given linear solver.

The following example shows how to use this function:

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 */
21 using MatrixType = TNL::Matrices::SparseMatrix< double, Device >;
22 using Vector = TNL::Containers::Vector< double, Device >;
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}
__cuda_callable__
#define __cuda_callable__
Definition Macros.h:49
std::cout
TNL::Containers::Vector
Vector extends Array with algebraic operations.
Definition Vector.h:36
TNL::Matrices::SparseMatrix
Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
Definition SparseMatrix.h:55
std::make_shared
T make_shared(T... args)
TNL::Solvers::getPreconditioner
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
TNL::Solvers::getLinearSolver
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

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 ]

## ◆ getLinearSolverOptions()

std::vector< std::string > TNL::Solvers::getLinearSolverOptions | ( |  | ) |
| inline

Function returning available linear solvers.

Returns
  container with names of available linear solvers.

## ◆ getPreconditioner()

template<typename MatrixType>

std::shared_ptr< Linear::Preconditioners::Preconditioner< MatrixType > > TNL::Solvers::getPreconditioner | ( | const std::string & | name | ) |

Function returning shared pointer with linear preconditioner given by its name in a form of a string.

Template Parameters

  MatrixType | is a type of matrix defining the system of linear equations.

Parameters

  name | of the linear preconditioner. The name can be one of the following: none - for none preconditioner diagonal - for diagonal/Jacobi preconditioner - TNL::Solvers::Linear::Preconditioners::Diagonal ilu0 - for ILU(0) preconditioner - TNL::Solvers::Linear::Preconditioners::ILU0 ilut - for ILU with thresholding preconditioner - TNL::Solvers::Linear::Preconditioners::ILUT

Returns
  shared pointer with given linear preconditioner.

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 */
21 using MatrixType = TNL::Matrices::SparseMatrix< double, Device >;
22 using Vector = TNL::Containers::Vector< double, Device >;
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}

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 ]

## ◆ getPreconditionerOptions()

std::vector< std::string > TNL::Solvers::getPreconditionerOptions | ( |  | ) |
| inline

Function returning available linear preconditioners.

Returns
  container with names of available linear preconditioners.