11 #ifndef BOGUS_KRYLOV_METHODS_HPP
12 #define BOGUS_KRYLOV_METHODS_HPP
14 #include "../BlockSolvers.fwd.hpp"
16 #include "../Utils/LinearSolverBase.hpp"
17 #include "../Utils/Signal.hpp"
18 #include "../Utils/NumTraits.hpp"
19 #include "../Utils/CppTools.hpp"
21 #define BOGUS_KRYLOV_METHODS \
22 BOGUS_PROCESS_KRYLOV_METHOD(CG )\
23 BOGUS_PROCESS_KRYLOV_METHOD(BiCG )\
24 BOGUS_PROCESS_KRYLOV_METHOD(BiCGSTAB)\
25 BOGUS_PROCESS_KRYLOV_METHOD(CGS )\
26 BOGUS_PROCESS_KRYLOV_METHOD(GMRES )\
27 BOGUS_PROCESS_KRYLOV_METHOD(TFQMR )
41 template<
template <
typename,
typename,
typename >
class Method,
42 typename Matrix,
typename Preconditioner,
typename Traits >
48 typedef typename Traits::Scalar Scalar ;
52 const Preconditioner *m_P;
59 unsigned maxIters, Scalar tol,
60 const Preconditioner *P,
63 : m_A( &A ), m_P( P ), m_callback( callback ),
64 m_tol( tol ), m_maxIters( maxIters ),
65 m_parallelizeRhs(
false ), m_enableResCaching(
false )
69 : m_A( BOGUS_NULL_PTR(
const Matrix) ),
70 m_P( BOGUS_NULL_PTR(
const Preconditioner) ),
71 m_callback( BOGUS_NULL_PTR(
const SignalType) ),
72 m_tol( 0 ), m_maxIters( 0 ),
73 m_parallelizeRhs(
false ), m_enableResCaching(
false )
77 template <
typename RhsT >
82 static ReturnType s_cachedRes ;
84 if( !m_enableResCaching ) {
85 ReturnType x( m_A->rows(), rhs.cols() ) ;
91 if( s_cachedRes.rows() != m_A->rows() || s_cachedRes.cols() != rhs.cols() ) {
92 s_cachedRes.resize( m_A->rows(), rhs.cols() ) ;
93 s_cachedRes.setZero() ;
101 template <
typename ResT,
typename RhsT >
102 Scalar
solve(
const RhsT& rhs, ResT& x )
const
105 #ifndef BOGUS_DONT_PARALLELIZE
106 #pragma omp parallel for reduction( +:res ) if ( m_parallelizeRhs )
108 for( std::ptrdiff_t c = 0 ; c < (std::ptrdiff_t) rhs.cols() ; ++c )
110 res += Base::derived().vectorSolve( rhs.col( c ), x.col( c ) ) ;
119 m_parallelizeRhs = parallelize ;
120 return Base::derived() ;
127 m_enableResCaching = doCache ;
128 return Base::derived() ;
132 bool m_parallelizeRhs ;
133 bool m_enableResCaching ;
142 #define BOGUS_MAKE_KRYLOV_SOLVER_TYPEDEFS( MethodName ) \
143 typedef KrylovSolverBase< solvers::MethodName, Matrix, Preconditioner, Traits > Base ; \
144 typedef typename Traits::Scalar Scalar ; \
148 using Base::m_maxIters ; \
149 using Base::m_tol ; \
150 using Base::m_callback ; \
152 #define BOGUS_MAKE_KRYLOV_SOLVER_HEADER( MethodName ) \
153 BOGUS_MAKE_KRYLOV_SOLVER_TYPEDEFS( MethodName ) \
154 MethodName( const Matrix &A, \
156 Scalar tol = NumTraits< Scalar >::epsilon(), \
157 const Preconditioner *P = BOGUS_NULL_PTR(const Preconditioner), \
158 const typename Base::SignalType *callback = BOGUS_NULL_PTR(const typename Base::SignalType ) )\
159 : Base( A, maxIters, tol, P, callback ) \
162 MethodName():Base() {} \
164 template < typename RhsT, typename ResT > \
165 Scalar vectorSolve( const RhsT &b, ResT x ) const ; \
177 template <
typename Matrix,
184 BOGUS_MAKE_KRYLOV_SOLVER_HEADER(
CG )
196 template <
typename Matrix,
202 BOGUS_MAKE_KRYLOV_SOLVER_HEADER(
BiCG )
217 template <
typename Matrix,
223 BOGUS_MAKE_KRYLOV_SOLVER_HEADER(
BiCGSTAB )
235 template <
typename Matrix,
241 BOGUS_MAKE_KRYLOV_SOLVER_HEADER(
CGS )
260 template <
typename Matrix,
265 BOGUS_MAKE_KRYLOV_SOLVER_TYPEDEFS(
GMRES )
270 GMRES(
const Matrix &A,
273 const Preconditioner *P = BOGUS_NULL_PTR(
const Preconditioner),
275 unsigned restart = 0 )
276 :
Base( A, maxIters, tol, P, callback ),
280 GMRES &setRestart(
unsigned restart )
282 m_restart = restart ;
286 template <
typename RhsT,
typename ResT >
287 Scalar vectorSolve(
const RhsT &b, ResT x )
const ;
303 template <
typename Matrix,
308 BOGUS_MAKE_KRYLOV_SOLVER_HEADER(
TFQMR )
319 #define BOGUS_PROCESS_KRYLOV_METHOD( MethodName ) \
320 template< typename Matrix, typename Preconditioner, class Traits > \
321 struct LinearSolverTraits< krylov::solvers::MethodName< Matrix, Preconditioner, Traits > > \
323 typedef Matrix MatrixType ; \
324 template < typename RhsT > struct Result { \
325 typedef typename Traits::template MutableClone< RhsT >::Type Type ; \
328 template< typename Matrix, typename Preconditioner, class Traits, \
329 typename RhsBlockT, bool TransposeLhs, bool TransposeRhs > \
330 struct BlockBlockProductTraits < krylov::solvers::MethodName< Matrix, Preconditioner, Traits >, RhsBlockT, TransposeLhs, TransposeRhs > \
332 typedef typename BlockBlockProductTraits < Matrix, RhsBlockT, TransposeLhs, TransposeRhs >::ReturnType \
337 #undef BOGUS_PROCESS_KRYLOV_METHOD
Definition: LinearSolverBase.hpp:22
Solves ( m_A * x = b ) using the BiConjugate Gradient algorithm.
Definition: KrylovMethods.hpp:199
Solves ( m_A * x = b ) using the Conjugate Gradient Squared algorithm.
Definition: KrylovMethods.hpp:238
Base class for linear solvers on base ( i.e. non-block ) matrices.
Definition: LinearSolverBase.hpp:26
Derived & enableResCaching(bool doCache=true)
Whether to enable caching of solve(rhs) result for warmstarting purposes.
Definition: KrylovMethods.hpp:125
Definition: BlockSolvers.fwd.hpp:33
Solves ( m_A * x = b ) using the transpose-free Quasi Minimal Reisual method.
Definition: KrylovMethods.hpp:306
Solves ( m_A * x = b ) using the BiConjugate Gradient stabilized algorithm.
Definition: KrylovMethods.hpp:220
Definition: NumTraits.hpp:22
Definition: KrylovMethods.hpp:43
Method
Definition: BlockSolvers.fwd.hpp:20
Solves ( m_A * x = b ) using the Conjugate Gradient algorithm.
Definition: KrylovMethods.hpp:180
Derived & parallelizeRhs(bool parallelize=true)
Whether to process multiple rhs in parallel.
Definition: KrylovMethods.hpp:117
Solves ( m_A * x = b ) using the (restarted) Generalized Minimum Residual.
Definition: KrylovMethods.hpp:263
Scalar solve(const RhsT &rhs, ResT &x) const
Returns the solution x of the linear system M * x = rhs.
Definition: KrylovMethods.hpp:102
Trivial ( identity ) preconditioner. Does nothing.
Definition: Preconditioners.hpp:18
LinearSolverTraits< Derived >::template Result< RhsT >::Type solve(const RhsT &rhs) const
Returns the solution x of the linear system M * x = rhs.
Definition: KrylovMethods.hpp:79
LinearSolverTraits< Derived >::template Result< RhsT >::Type solve(const RhsT &rhs) const
Returns the solution x of the linear system M * x = rhs.
Definition: LinearSolverBase.hpp:32