Template Numerical Library version main:e6bb26f
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

struct  ConfigTagDevice
 
struct  ConfigTagExplicitSolver
 
struct  ConfigTagIndex
 
struct  ConfigTagIndex< FastBuildConfigTag, long int >
 
struct  ConfigTagIndex< FastBuildConfigTag, short int >
 
struct  ConfigTagMeshResolve
 
struct  ConfigTagReal
 
struct  ConfigTagReal< FastBuildConfigTag, float >
 
struct  ConfigTagReal< FastBuildConfigTag, long double >
 
struct  ConfigTagTimeDiscretisation
 
struct  ConfigTagTimeDiscretisation< FastBuildConfigTag, ExplicitTimeDiscretisationTag >
 
struct  ConfigTagTimeDiscretisation< FastBuildConfigTag, ImplicitTimeDiscretisationTag >
 
struct  ConfigTagTimeDiscretisation< FastBuildConfigTag, SemiImplicitTimeDiscretisationTag >
 
class  DefaultBuildConfigTag
 
class  ExplicitEulerSolverTag
 
class  ExplicitMersonSolverTag
 
class  ExplicitTimeDiscretisationTag
 
class  FastBuildConfigTag
 
class  GinkgoConvergenceLoggerMonitor
 A Ginkgo Convergence logger with a TNL iterative solver monitor. More...
 
class  ImplicitTimeDiscretisationTag
 
class  IterativeSolver
 Base class for iterative solvers. More...
 
class  IterativeSolverMonitor
 Object for monitoring convergence of iterative solvers. More...
 
class  SemiImplicitTimeDiscretisationTag
 
struct  Solver
 
struct  SolverConfig
 
class  SolverInitiator
 
class  SolverInitiatorDeviceResolver
 
class  SolverInitiatorDeviceResolver< ProblemSetter, Real, Device, ConfigTag, false >
 
class  SolverInitiatorDeviceResolver< ProblemSetter, Real, Device, ConfigTag, true >
 
class  SolverInitiatorIndexResolver
 
class  SolverInitiatorIndexResolver< ProblemSetter, Real, Device, Index, ConfigTag, false >
 
class  SolverInitiatorIndexResolver< ProblemSetter, Real, Device, Index, ConfigTag, true >
 
class  SolverInitiatorMeshResolver
 
class  SolverInitiatorMeshResolver< ProblemSetter, Real, Device, Index, ConfigTag, false >
 
class  SolverInitiatorMeshResolver< ProblemSetter, Real, Device, Index, ConfigTag, true >
 
class  SolverInitiatorRealResolver
 
class  SolverInitiatorRealResolver< ProblemSetter, Real, ConfigTag, false >
 
class  SolverInitiatorRealResolver< ProblemSetter, Real, ConfigTag, true >
 
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  SolverStarter
 
class  SolverStarterExplicitSolverSetter
 
class  SolverStarterExplicitSolverSetter< Problem, ExplicitSolverTag, ConfigTag, false >
 
class  SolverStarterExplicitSolverSetter< Problem, ExplicitSolverTag, ConfigTag, true >
 
class  SolverStarterTimeDiscretisationSetter
 
class  SolverStarterTimeDiscretisationSetter< Problem, ExplicitTimeDiscretisationTag, ConfigTag, true >
 
class  SolverStarterTimeDiscretisationSetter< Problem, ImplicitTimeDiscretisationTag, ConfigTag, true >
 
class  SolverStarterTimeDiscretisationSetter< Problem, SemiImplicitTimeDiscretisationTag, ConfigTag, true >
 
class  SolverStarterTimeDiscretisationSetter< Problem, TimeDiscretisationTag, ConfigTag, false >
 
class  StaticIterativeSolver
 Base class for iterative solvers. More...
 
class  TimeDependencyResolver
 
class  TimeDependencyResolver< Problem, ConfigTag, false >
 
class  TimeDependencyResolver< Problem, ConfigTag, true >
 
class  UserDefinedTimeDiscretisationSetter
 
class  UserDefinedTimeDiscretisationSetter< Problem, ConfigTag, void >
 

Functions

template<typename MatrixType >
std::shared_ptr< Linear::LinearSolver< MatrixType > > getLinearSolver (std::string name)
 Function returning shared pointer with linear solver given by its name in a form of a string. More...
 
std::vector< std::stringgetLinearSolverOptions ()
 Function returning available linear solvers. More...
 
template<typename MatrixType >
std::shared_ptr< Linear::Preconditioners::Preconditioner< MatrixType > > getPreconditioner (std::string name)
 Function returning shared pointer with linear preconditioner given by its name in a form of a string. More...
 
std::vector< std::stringgetPreconditionerOptions ()
 Function returning available linear preconditioners. More...
 

Detailed Description

Namespace for solvers.

Function Documentation

◆ getLinearSolver()

template<typename MatrixType >
std::shared_ptr< Linear::LinearSolver< MatrixType > > TNL::Solvers::getLinearSolver ( std::string  name)

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

Template Parameters
MatrixTypeis a type of matrix defining the system of linear equations.
Parameters
nameof the linear solver. The name can be one of the following:
  1. jacobi - for the Jacobi solver - TNL::Solvers::Linear::Jacobi.
  2. sor - for SOR solver - TNL::Solvers::Linear::SOR.
  3. cg - for CG solver - TNL::Solvers::Linear::CG.
  4. bicgstab - for BICGStab solver - TNL::Solvers::Linear::BICGStab.
  5. bicgstabl - for BICGStab(l) solver - TNL::Solvers::Linear::BICGStabL.
  6. gmres - for GMRES solver - TNL::Solvers::Linear::GMRES.
  7. tfqmr - for TFQMR solver - TNL::Solvers::Linear::TFQMR.
  8. idrs - for IDR(s) solver - TNL::Solvers::Linear::IDRs.
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/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/LinearSolverTypeResolver.h>
8
9template< typename Device >
10void iterativeLinearSolverExample()
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 const int rowIdx = row.getRowIndex();
30 if( rowIdx == 0 )
31 {
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 {
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 {
42 row.setElement( 0, rowIdx-1, -1.0 ); // element below the diagonal
43 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
44 row.setElement( 2, rowIdx+1, -1.0 ); // element above the diagonal
45 }
46 };
47
48 /***
49 * Set the matrix elements.
50 */
51 matrix_ptr->forAllRows( f );
52 std::cout << *matrix_ptr << std::endl;
53
54 /***
55 * Set the right-hand side vector.
56 */
57 Vector x( size, 1.0 );
58 Vector b( size );
59 matrix_ptr->vectorProduct( x, b );
60 x = 0.0;
61 std::cout << "Vector b = " << b << std::endl;
62
63 /***
64 * Solve the linear system using diagonal (Jacobi) preconditioner.
65 */
66 auto solver_ptr = TNL::Solvers::getLinearSolver< MatrixType >( "tfqmr" );
67 auto preconditioner_ptr = TNL::Solvers::getPreconditioner< MatrixType >( "diagonal");
68 preconditioner_ptr->update( matrix_ptr );
69 solver_ptr->setMatrix( matrix_ptr );
70 solver_ptr->setPreconditioner( preconditioner_ptr );
71 solver_ptr->setConvergenceResidue( 1.0e-6 );
72 solver_ptr->solve( b, x );
73 std::cout << "Vector x = " << x << std::endl;
74}
75
76int main( int argc, char* argv[] )
77{
78 std::cout << "Solving linear system on host: " << std::endl;
79 iterativeLinearSolverExample< TNL::Devices::Sequential >();
80
81#ifdef HAVE_CUDA
82 std::cout << "Solving linear system on CUDA device: " << std::endl;
83 iterativeLinearSolverExample< TNL::Devices::Cuda >();
84#endif
85}
#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
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 ]

◆ 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 ( std::string  name)

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

Template Parameters
MatrixTypeis a type of matrix defining the system of linear equations.
Parameters
nameof the linear preconditioner. The name can be one of the following:
  1. none - for none preconditioner
  2. diagonal - for diagonal/Jacobi preconditioner - TNL::Solvers::Linear::Preconditioners::Diagonal
  3. ilu0 - for ILU(0) preconditioner - TNL::Solvers::Linear::Preconditioners::ILU0
  4. 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/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/LinearSolverTypeResolver.h>
8
9template< typename Device >
10void iterativeLinearSolverExample()
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 const int rowIdx = row.getRowIndex();
30 if( rowIdx == 0 )
31 {
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 {
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 {
42 row.setElement( 0, rowIdx-1, -1.0 ); // element below the diagonal
43 row.setElement( 1, rowIdx, 2.5 ); // diagonal element
44 row.setElement( 2, rowIdx+1, -1.0 ); // element above the diagonal
45 }
46 };
47
48 /***
49 * Set the matrix elements.
50 */
51 matrix_ptr->forAllRows( f );
52 std::cout << *matrix_ptr << std::endl;
53
54 /***
55 * Set the right-hand side vector.
56 */
57 Vector x( size, 1.0 );
58 Vector b( size );
59 matrix_ptr->vectorProduct( x, b );
60 x = 0.0;
61 std::cout << "Vector b = " << b << std::endl;
62
63 /***
64 * Solve the linear system using diagonal (Jacobi) preconditioner.
65 */
66 auto solver_ptr = TNL::Solvers::getLinearSolver< MatrixType >( "tfqmr" );
67 auto preconditioner_ptr = TNL::Solvers::getPreconditioner< MatrixType >( "diagonal");
68 preconditioner_ptr->update( matrix_ptr );
69 solver_ptr->setMatrix( matrix_ptr );
70 solver_ptr->setPreconditioner( preconditioner_ptr );
71 solver_ptr->setConvergenceResidue( 1.0e-6 );
72 solver_ptr->solve( b, x );
73 std::cout << "Vector x = " << x << std::endl;
74}
75
76int main( int argc, char* argv[] )
77{
78 std::cout << "Solving linear system on host: " << std::endl;
79 iterativeLinearSolverExample< TNL::Devices::Sequential >();
80
81#ifdef HAVE_CUDA
82 std::cout << "Solving linear system on CUDA device: " << std::endl;
83 iterativeLinearSolverExample< TNL::Devices::Cuda >();
84#endif
85}

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 ]

◆ getPreconditionerOptions()

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

Function returning available linear preconditioners.

Returns
container with names of available linear preconditioners.