|
virtual | ~LinearSolver () |
| Default destructor.
|
|
void | setMatrix (const MatrixPointer &matrix) |
| Set the matrix of the linear system.
|
|
void | setPreconditioner (const PreconditionerPointer &preconditioner) |
| Set the preconditioner.
|
|
virtual bool | setup (const Config::ParameterContainer ¶meters, const String &prefix="") |
| Method for setup of the linear iterative solver based on configuration parameters.
|
|
virtual bool | solve (ConstVectorViewType b, VectorViewType x)=0 |
| Method for solving a linear system.
|
|
| IterativeSolver ()=default |
| Default constructor.
|
|
bool | checkConvergence () |
| Checks whether the convergence occurred already.
|
|
bool | checkNextIteration () |
| Checks if the solver is allowed to do the next iteration.
|
|
const Matrix::RealType & | getConvergenceResidue () const |
| Gets the the convergence threshold.
|
|
const Matrix::RealType & | getDivergenceResidue () const |
| Gets the limit for the divergence criterion.
|
|
const Matrix::IndexType & | getIterations () const |
| Gets the number of iterations performed by the solver so far.
|
|
const Matrix::IndexType & | getMaxIterations () const |
| Gets the maximal number of iterations the solver is allowed to perform.
|
|
const Matrix::IndexType & | getMinIterations () const |
| Gets the minimal number of iterations the solver is supposed to do.
|
|
const Matrix::RealType & | getResidue () const |
| Gets the residue reached at the current iteration.
|
|
bool | nextIteration () |
| Proceeds to the next iteration.
|
|
void | resetIterations () |
| Sets the the number of the current iterations to zero.
|
|
void | setConvergenceResidue (const Matrix::RealType &convergenceResidue) |
| Sets the threshold for the convergence.
|
|
void | setDivergenceResidue (const Matrix::RealType &divergenceResidue) |
| Sets the residue limit for the divergence criterion.
|
|
void | setMaxIterations (const Matrix::IndexType &maxIterations) |
| Sets the maximal number of iterations the solver is allowed to perform.
|
|
void | setMinIterations (const Matrix::IndexType &minIterations) |
| Sets the minimal number of iterations the solver is supposed to do.
|
|
void | setRefreshRate (const Matrix::IndexType &refreshRate) |
| Sets the refresh rate (in milliseconds) for the solver monitor.
|
|
void | setResidue (const Matrix::RealType &residue) |
| Sets the residue reached at the current iteration.
|
|
void | setSolverMonitor (SolverMonitorType &solverMonitor) |
| Sets the solver monitor object.
|
|
bool | setup (const Config::ParameterContainer ¶meters, const std::string &prefix="") |
| Method for setup of the iterative solver based on configuration parameters.
|
|
template<typename Matrix>
class TNL::Solvers::Linear::LinearSolver< Matrix >
Base class for iterative solvers of systems of linear equations.
To use the linear solver, one needs to first set the matrix of the linear system by means of the method LinearSolver::setMatrix. Afterward, one may call the method LinearSolver::solve which accepts the right-hand side vector b and a vector x to which the solution will be stored. One may also use appropriate preconditioner to speed-up the convergence - see the method LinearSolver::setPreconditioner.
- Template Parameters
-
Matrix | is type of matrix representing the linear system. |
The following example demonstrates the use iterative linear solvers:
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
8template< typename Device >
9void
10iterativeLinearSolverExample()
11{
12
13
14
15
16
17
18
19
20
23 const int size( 5 );
25 matrix_ptr->setDimensions( size, size );
26 matrix_ptr->setRowCapacities( Vector( { 2, 3, 3, 3, 2 } ) );
27
29 {
30 const int rowIdx = row.getRowIndex();
31 if( rowIdx == 0 ) {
32 row.setElement( 0, rowIdx, 2.5 );
33 row.setElement( 1, rowIdx + 1, -1 );
34 }
35 else if( rowIdx == size - 1 ) {
36 row.setElement( 0, rowIdx - 1, -1.0 );
37 row.setElement( 1, rowIdx, 2.5 );
38 }
39 else {
40 row.setElement( 0, rowIdx - 1, -1.0 );
41 row.setElement( 1, rowIdx, 2.5 );
42 row.setElement( 2, rowIdx + 1, -1.0 );
43 }
44 };
45
46
47
48
49 matrix_ptr->forAllRows( f );
51
52
53
54
55 Vector x( size, 1.0 );
56 Vector b( size );
57 matrix_ptr->vectorProduct( x, b );
58 x = 0.0;
60
61
62
63
65 LinearSolver solver;
67 solver.setConvergenceResidue( 1.0e-6 );
68 solver.solve( b, x );
70}
71
72int
73main( int argc, char* argv[] )
74{
76 iterativeLinearSolverExample< TNL::Devices::Sequential >();
77
78#ifdef __CUDACC__
80 iterativeLinearSolverExample< TNL::Devices::Cuda >();
81#endif
82}
#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
void setMatrix(const MatrixPointer &matrix)
Set the matrix of the linear system.
Definition LinearSolver.h:120
Matrix MatrixType
Type of the matrix representing the linear system.
Definition LinearSolver.h:71
Iterative solver of linear systems based on the Transpose-free quasi-minimal residual (TFQMR) method.
Definition TFQMR.h:21
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 ]
See also TNL::Solvers::IterativeSolverMonitor for monitoring of iterative solvers.