10#ifndef BELOS_RCG_ITER_HPP
11#define BELOS_RCG_ITER_HPP
29#include "Teuchos_LAPACK.hpp"
30#include "Teuchos_ScalarTraits.hpp"
31#include "Teuchos_ParameterList.hpp"
32#include "Teuchos_TimeMonitor.hpp"
54 template <
class ScalarType,
class MV,
class DM>
78 Teuchos::RCP<MV>
U,
AU;
82 Teuchos::RCP<std::vector<ScalarType> >
Alpha;
83 Teuchos::RCP<std::vector<ScalarType> >
Beta;
85 Teuchos::RCP<std::vector<ScalarType> >
D;
86 Teuchos::RCP<std::vector<ScalarType> >
rTz_old;
94 Teuchos::RCP<std::vector<int> >
ipiv;
109 template<
class ScalarType,
class MV,
class OP,
class DM>
120 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
136 Teuchos::ParameterList &
params );
210 if (!initialized_)
return 0;
238 "Belos::RCGIter::setBlockSize(): Cannot use a block size that is not one.");
258 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
259 const Teuchos::RCP<OutputManager<ScalarType> > om_;
260 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
289 Teuchos::RCP<MV> Ap_;
300 Teuchos::RCP<MV> U_, AU_;
303 Teuchos::RCP<std::vector<ScalarType> > Alpha_;
304 Teuchos::RCP<std::vector<ScalarType> > Beta_;
306 Teuchos::RCP<std::vector<ScalarType> > D_;
309 Teuchos::RCP<DM> Delta_;
312 Teuchos::RCP<DM> LUUTAU_;
315 Teuchos::RCP<std::vector<int> > ipiv_;
318 Teuchos::RCP<std::vector<ScalarType> > rTz_old_;
323 template<
class ScalarType,
class MV,
class OP,
class DM>
327 Teuchos::ParameterList &
params ):
340 "Belos::RCGIter::constructor: mandatory parameter \"Num Blocks\" is not specified.");
341 int nb = Teuchos::getParameter<int>(
params,
"Num Blocks");
344 "Belos::RCGIter::constructor: mandatory parameter \"Recycled Blocks\" is not specified.");
345 int rb = Teuchos::getParameter<int>(
params,
"Recycled Blocks");
353 template <
class ScalarType,
class MV,
class OP,
class DM>
368 template <
class ScalarType,
class MV,
class OP,
class DM>
384 newstate.rTz_old != Teuchos::null) {
406 "Belos::RCGIter::initialize(): RCGIterState does not have P initialized.");
409 "Belos::RCGIter::initialize(): RCGIterState does not have Ap initialized.");
412 "Belos::RCGIter::initialize(): RCGIterState does not have r initialized.");
415 "Belos::RCGIter::initialize(): RCGIterState does not have z initialized.");
418 "Belos::RCGIter::initialize(): RCGIterState does not have U initialized.");
421 "Belos::RCGIter::initialize(): RCGIterState does not have AU initialized.");
424 "Belos::RCGIter::initialize(): RCGIterState does not have Alpha initialized.");
427 "Belos::RCGIter::initialize(): RCGIterState does not have Beta initialized.");
430 "Belos::RCGIter::initialize(): RCGIterState does not have D initialized.");
433 "Belos::RCGIter::initialize(): RCGIterState does not have Delta initialized.");
436 "Belos::RCGIter::initialize(): RCGIterState does not have LUUTAU initialized.");
439 "Belos::RCGIter::initialize(): RCGIterState does not have ipiv initialized.");
442 "Belos::RCGIter::initialize(): RCGIterState does not have rTz_old initialized.");
453 template <
class ScalarType,
class MV,
class OP,
class DM>
457 "Belos::RCGIter::iterate(): RCGIter class not initialized." );
460 Teuchos::LAPACK<int,ScalarType> lapack;
467 std::vector<int> index(1);
468 std::vector<ScalarType>
pAp(1);
469 std::vector<ScalarType>
rTz(1);
476 "Belos::RCGIter::iterate(): current linear system has more than one std::vector!" );
489 Teuchos::RCP<const MV>
p_ = Teuchos::null;
490 Teuchos::RCP<MV>
pnext_ = Teuchos::null;
491 while (stest_->checkStatus(
this) !=
Passed && curDim_+1 <=
searchDim) {
496 p_ = MVT::CloneView( *P_, index );
497 lp_->applyOp( *
p_, *Ap_ );
500 MVT::MvDot( *
p_, *Ap_,
pAp );
504 (*Alpha_)[
i_] = (*rTz_old_)[0] /
pAp[0];
508 "Belos::RCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
512 lp_->updateSolution();
515 MVT::MvAddMv(
one, *r_, -(*Alpha_)[
i_], *Ap_, *r_ );
517 std::vector<MagnitudeType> norm(1);
518 MVT::MvNorm( *r_, norm );
522 if ( lp_->getLeftPrec() != Teuchos::null ) {
523 lp_->applyLeftPrec( *r_, *z_ );
525 else if ( lp_->getRightPrec() != Teuchos::null ) {
526 lp_->applyRightPrec( *r_, *z_ );
533 MVT::MvDot( *r_, *z_,
rTz );
536 (*Beta_)[Beta_i_] =
rTz[0] / (*rTz_old_)[0];
544 pnext_ = MVT::CloneViewNonConst( *P_, index );
548 Teuchos::RCP<DM>
mu = DMT::Subview( *Delta_, recycleBlocks_, 1, 0,
i_+1 );
549 MVT::MvTransMv(
one, *AU_, *z_, *
mu );
551 DMT::SyncDeviceToHost( *
mu );
552 DMT::SyncDeviceToHost( *LUUTAU_ );
555 lapack.GETRS(
TRANS, recycleBlocks_, 1, DMT::GetConstRawHostPtr(*LUUTAU_), DMT::GetStride(*LUUTAU_),
556 &(*ipiv_)[0], DMT::GetRawHostPtr(*
mu), DMT::GetStride(*
mu), &
info );
558 "Belos::RCGIter::solve(): LAPACK GETRS failed to compute a solution.");
559 DMT::SyncHostToDevice( *
mu );
562 MVT::MvAddMv( (*Beta_)[Beta_i_], *
p_,
one, *z_, *
pnext_ );
568 MVT::MvAddMv( (*Beta_)[Beta_i_], *
p_,
one, *z_, *
pnext_ );
Pure virtual base class which augments the basic interface for a conjugate gradient linear solver ite...
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
CGIterateFailure is thrown when the CGIteration object is unable to compute the next iterate in the C...
CGIterationLAPACKFailure is thrown when a nonzero return value is passed back from an LAPACK routine.
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero,...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
This class implements the RCG iteration, where a single-std::vector Krylov subspace is constructed.
RCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, Teuchos::ParameterList ¶ms)
RCGIter constructor with linear problem, solver utilities, and parameter list of solver options.
void setSize(int recycleBlocks, int numBlocks)
Set the maximum number of blocks used by the iterative solver and the number of recycled vectors.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
int getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem.
virtual ~RCGIter()
Destructor.
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem.
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::ScalarTraits< ScalarType > SCT
bool isInitialized()
States whether the solver has been initialized or not.
void setBlockSize(int blockSize)
Set the blocksize.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Get the norms of the residuals native to the solver.
int getNumIters() const
Get the current iteration count.
MultiVecTraits< ScalarType, MV, DM > MVT
void resetNumIters(int iter=0)
Reset the iteration count.
void iterate()
This method performs RCG iterations until the status test indicates the need to stop or an error occu...
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
SCT::magnitudeType MagnitudeType
DenseMatTraits< ScalarType, DM > DMT
Structure to contain pointers to RCGIter state variables.
int curDim
The current dimension of the reduction.
Teuchos::RCP< MV > r
The current residual.
bool existU
Flag to indicate the recycle space should be used.
Teuchos::RCP< MV > P
The current Krylov basis.
Teuchos::RCP< MV > U
The recycled subspace and its image.
Teuchos::RCP< std::vector< ScalarType > > D
Teuchos::RCP< std::vector< ScalarType > > Beta
Teuchos::RCP< std::vector< ScalarType > > rTz_old
Teuchos::RCP< std::vector< ScalarType > > Alpha
Coefficients arising in RCG iteration.
Teuchos::RCP< DM > Delta
Solutions to local least-squares problems.
Teuchos::RCP< std::vector< int > > ipiv
Data from LU factorization of U^T A U.
Teuchos::RCP< MV > z
The current preconditioned residual.
Teuchos::RCP< DM > LUUTAU
The LU factorization of the matrix U^T A U
Teuchos::RCP< MV > Ap
A times current search vector.