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 = DefaultDenseMatrix<
int,ScalarType>>
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;
100 U(Teuchos::null),
AU(Teuchos::null),
102 D(Teuchos::null),
rTz_old(Teuchos::null),
109 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
120 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
136 Teuchos::ParameterList ¶ms );
210 if (!initialized_)
return 0;
237 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
238 "Belos::RCGIter::setBlockSize(): Cannot use a block size that is not one.");
242 void setSize(
int recycleBlocks,
int numBlocks );
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 ¶ms ):
339 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Num Blocks"), std::invalid_argument,
340 "Belos::RCGIter::constructor: mandatory parameter \"Num Blocks\" is not specified.");
341 int nb = Teuchos::getParameter<int>(params,
"Num Blocks");
343 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Recycled Blocks"), std::invalid_argument,
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>
357 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0, std::invalid_argument,
"Belos::RCGIter::setSize() was passed a non-positive argument for \"Num Blocks\".");
358 TEUCHOS_TEST_FOR_EXCEPTION(recycleBlocks <= 0, std::invalid_argument,
"Belos::RCGIter::setSize() was passed a non-positive argument for \"Recycled Blocks\".");
359 TEUCHOS_TEST_FOR_EXCEPTION(recycleBlocks >= numBlocks, std::invalid_argument,
"Belos::RCGIter::setSize() the number of recycled blocks is larger than the allowable subspace.");
361 numBlocks_ = numBlocks;
362 recycleBlocks_ = recycleBlocks;
368 template <
class ScalarType,
class MV,
class OP,
class DM>
372 if (newstate.
P != Teuchos::null &&
373 newstate.
Ap != Teuchos::null &&
374 newstate.
r != Teuchos::null &&
375 newstate.
z != Teuchos::null &&
376 newstate.
U != Teuchos::null &&
377 newstate.
AU != Teuchos::null &&
378 newstate.
Alpha != Teuchos::null &&
379 newstate.
Beta != Teuchos::null &&
380 newstate.
D != Teuchos::null &&
381 newstate.
Delta != Teuchos::null &&
382 newstate.
LUUTAU != Teuchos::null &&
383 newstate.
ipiv != Teuchos::null &&
384 newstate.
rTz_old != Teuchos::null) {
386 curDim_ = newstate.
curDim;
391 existU_ = newstate.
existU;
394 Alpha_ = newstate.
Alpha;
395 Beta_ = newstate.
Beta;
396 Beta_i_ = newstate.
Beta_i;
398 Delta_ = newstate.
Delta;
399 LUUTAU_ = newstate.
LUUTAU;
400 ipiv_ = newstate.
ipiv;
405 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
P == Teuchos::null,std::invalid_argument,
406 "Belos::RCGIter::initialize(): RCGIterState does not have P initialized.");
408 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
Ap == Teuchos::null,std::invalid_argument,
409 "Belos::RCGIter::initialize(): RCGIterState does not have Ap initialized.");
411 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
r == Teuchos::null,std::invalid_argument,
412 "Belos::RCGIter::initialize(): RCGIterState does not have r initialized.");
414 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
z == Teuchos::null,std::invalid_argument,
415 "Belos::RCGIter::initialize(): RCGIterState does not have z initialized.");
417 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
U == Teuchos::null,std::invalid_argument,
418 "Belos::RCGIter::initialize(): RCGIterState does not have U initialized.");
420 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
AU == Teuchos::null,std::invalid_argument,
421 "Belos::RCGIter::initialize(): RCGIterState does not have AU initialized.");
423 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
Alpha == Teuchos::null,std::invalid_argument,
424 "Belos::RCGIter::initialize(): RCGIterState does not have Alpha initialized.");
426 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
Beta == Teuchos::null,std::invalid_argument,
427 "Belos::RCGIter::initialize(): RCGIterState does not have Beta initialized.");
429 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
D == Teuchos::null,std::invalid_argument,
430 "Belos::RCGIter::initialize(): RCGIterState does not have D initialized.");
432 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
Delta == Teuchos::null,std::invalid_argument,
433 "Belos::RCGIter::initialize(): RCGIterState does not have Delta initialized.");
435 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
LUUTAU == Teuchos::null,std::invalid_argument,
436 "Belos::RCGIter::initialize(): RCGIterState does not have LUUTAU initialized.");
438 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
ipiv == Teuchos::null,std::invalid_argument,
439 "Belos::RCGIter::initialize(): RCGIterState does not have ipiv initialized.");
441 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
rTz_old == Teuchos::null,std::invalid_argument,
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;
463 ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
464 ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
467 std::vector<int> index(1);
468 std::vector<ScalarType> pAp(1);
469 std::vector<ScalarType> rTz(1);
472 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
476 "Belos::RCGIter::iterate(): current linear system has more than one std::vector!" );
479 int searchDim = numBlocks_+1;
489 Teuchos::RCP<const MV> p_ = Teuchos::null;
490 Teuchos::RCP<MV> pnext_ = Teuchos::null;
491 while (stest_->checkStatus(
this) !=
Passed && curDim_+1 <= searchDim) {
497 lp_->applyOp( *p_, *Ap_ );
504 (*Alpha_)[i_] = (*rTz_old_)[0] / pAp[0];
508 "Belos::RCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
511 MVT::MvAddMv( one, *cur_soln_vec, (*Alpha_)[i_], *p_, *cur_soln_vec );
512 lp_->updateSolution();
517 std::vector<MagnitudeType> norm(1);
522 if ( lp_->getLeftPrec() != Teuchos::null ) {
523 lp_->applyLeftPrec( *r_, *z_ );
525 else if ( lp_->getRightPrec() != Teuchos::null ) {
526 lp_->applyRightPrec( *r_, *z_ );
536 (*Beta_)[Beta_i_] = rTz[0] / (*rTz_old_)[0];
548 Teuchos::RCP<DM> mu =
DMT::Subview( *Delta_, recycleBlocks_, 1, 0, i_+1 );
558 "Belos::RCGIter::solve(): LAPACK GETRS failed to compute a solution.");
562 MVT::MvAddMv( (*Beta_)[Beta_i_], *p_, one, *z_, *pnext_ );
568 MVT::MvAddMv( (*Beta_)[Beta_i_], *p_, one, *z_, *pnext_ );
573 pnext_ = Teuchos::null;
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,...
Virtual base class which defines basic traits for the multi-vector type.
static Teuchos::RCP< DM > Subview(DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
Marks host data modified to avoid device sync errors.
static void SyncHostToDevice(DM &dm)
static ScalarType * GetRawHostPtr(DM &dm)
Returns a raw pointer to the (non-const) data on the host.
static void SyncDeviceToHost(DM &dm)
static ScalarType const * GetConstRawHostPtr(const DM &dm)
Returns a raw pointer to const data on the host.
static int GetStride(const DM &dm)
Obtain the stride between the columns of dm.
Iteration()
Default Constructor.
Traits class which defines basic operations on multivectors.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const DM &B, const ScalarType beta, MV &mv)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, DM &B)
static void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static int GetNumberVecs(const MV &mv)
Class which defines basic traits for the operator type.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
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
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
A pure virtual class for defining the status tests for the Belos iterative solvers.
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.