10#ifndef BELOS_CG_SINGLE_RED_ITER_HPP
11#define BELOS_CG_SINGLE_RED_ITER_HPP
29#include "Teuchos_ScalarTraits.hpp"
30#include "Teuchos_ParameterList.hpp"
31#include "Teuchos_TimeMonitor.hpp"
52 template <
class ScalarType,
class MV,
class DM>
70 W = MVT::Clone( *
tmp, 3 );
71 std::vector<int>
index2(2,0);
72 std::vector<int> index(1,0);
77 S = MVT::CloneViewNonConst( *
W,
index2 );
82 U = MVT::CloneViewNonConst( *
W,
index2 );
85 this->
R = MVT::CloneViewNonConst( *
W, index );
87 AZ = MVT::CloneViewNonConst( *
W, index );
89 this->
Z = MVT::CloneViewNonConst( *
W, index );
94 T = MVT::CloneViewNonConst( *
W,
index2 );
97 V = MVT::Clone( *
tmp, 2 );
99 this->
AP = MVT::CloneViewNonConst( *
V, index );
101 this->
P = MVT::CloneViewNonConst( *
V, index );
125template<
class ScalarType,
class MV,
class OP,
class DM>
136 using SCT = Teuchos::ScalarTraits<ScalarType>;
151 Teuchos::ParameterList &
params );
205 Teuchos::RCP<CGIterationStateBase<ScalarType,MV,DM> >
getState()
const {
221 auto s = Teuchos::rcp_dynamic_cast<CGSingleRedIterationState<ScalarType,MV,DM> >(
state,
true);
269 "Belos::CGSingleRedIter::setBlockSize(): Cannot use a block size that is not one.");
280 Teuchos::ArrayView<MagnitudeType>
temp;
286 Teuchos::ArrayView<MagnitudeType>
temp;
300 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
301 const Teuchos::RCP<OutputManager<ScalarType> > om_;
302 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
303 const Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
317 bool foldConvergenceDetectionIntoAllreduce_;
332 Teuchos::RCP<MV> AZ_;
336 Teuchos::RCP<MV> AP_;
353 template<
class ScalarType,
class MV,
class OP,
class DM>
358 Teuchos::ParameterList &
params ):
366 foldConvergenceDetectionIntoAllreduce_ =
params.get<
bool>(
"Fold Convergence Detection Into Allreduce",
false);
371 template<
class ScalarType,
class MV,
class OP,
class DM>
375 Teuchos::RCP<const MV>
lhsMV = lp_->getLHS();
376 Teuchos::RCP<const MV>
rhsMV = lp_->getRHS();
383 std::string
errstr(
"Belos::CGSingleRedIter::initialize(): Specified multivectors must have a consistent length and width.");
388 std::invalid_argument,
errstr );
390 std::invalid_argument,
errstr );
395 MVT::Assign( *
R_0, *R_ );
401 if ( lp_->getLeftPrec() != Teuchos::null ) {
402 lp_->applyLeftPrec( *R_, *Z_ );
403 if ( lp_->getRightPrec() != Teuchos::null ) {
404 Teuchos::RCP<MV>
tmp2 = MVT::Clone( *Z_, 1 );
405 lp_->applyRightPrec( *Z_, *
tmp2 );
406 MVT::Assign( *
tmp2, *Z_ );
409 else if ( lp_->getRightPrec() != Teuchos::null ) {
410 lp_->applyRightPrec( *R_, *Z_ );
413 MVT::Assign( *R_, *Z_ );
417 lp_->applyOp( *Z_, *AZ_ );
421 MVT::Assign( *U_, *V_);
431 template<
class ScalarType,
class MV,
class OP,
class DM>
432 Teuchos::RCP<const MV>
435 (*norms)[0] = std::sqrt(Teuchos::ScalarTraits<ScalarType>::magnitude(rHz_));
436 return Teuchos::null;
437 }
else if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
438 (*norms)[0] = std::sqrt(Teuchos::ScalarTraits<ScalarType>::magnitude(rHr_));
439 return Teuchos::null;
447 template<
class ScalarType,
class MV,
class OP,
class DM>
458 Teuchos::RCP<DM>
sHz = DMT::Create( 2, 1 );
459 Teuchos::RCP<DM>
sHt = DMT::Create( 2, 2 );
463 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
471 "Belos::CGSingleRedIter::iterate(): current linear system has more than one vector!" );
473 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
475 MVT::MvTransMv(
one, *S_, *T_, *
sHt );
476 DMT::SyncDeviceToHost( *
sHt );
477 rHz_ = DMT::ValueConst(*
sHt,1,1);
479 rHr_ = DMT::ValueConst(*
sHt,1,0);
482 MVT::MvTransMv(
one, *S_, *Z_, *
sHz );
483 DMT::SyncDeviceToHost( *
sHz );
484 rHz_ = DMT::ValueConst(*
sHz,1,0);
487 if ((Teuchos::ScalarTraits<ScalarType>::magnitude(
delta) < Teuchos::ScalarTraits<ScalarType>::eps()) &&
488 (stest_->checkStatus(
this) ==
Passed))
490 alpha = rHz_ /
delta;
494 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
499 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
511 MVT::MvAddMv(
one, *R_, -alpha, *AP_, *R_ );
515 if ( lp_->getLeftPrec() != Teuchos::null ) {
516 lp_->applyLeftPrec( *R_, *Z_ );
517 if ( lp_->getRightPrec() != Teuchos::null ) {
518 Teuchos::RCP<MV>
tmp = MVT::CloneCopy( *Z_ );
519 lp_->applyRightPrec( *
tmp, *Z_ );
522 else if ( lp_->getRightPrec() != Teuchos::null ) {
523 lp_->applyRightPrec( *R_, *Z_ );
526 MVT::Assign( *R_, *Z_ );
530 lp_->applyOp( *Z_, *AZ_ );
533 MVT::MvTransMv(
one, *S_, *T_, *
sHt );
534 DMT::SyncDeviceToHost( *
sHt );
538 rHz_ = DMT::ValueConst(*
sHt,1,1);
540 rHr_ = DMT::ValueConst(*
sHt,1,0);
547 if (stest_->checkStatus(
this) ==
Passed) {
552 alpha = rHz_ / (
delta - (beta*rHz_ / alpha));
556 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
563 MVT::MvAddMv(
one, *U_, beta, *V_, *V_ );
578 MVT::MvAddMv(
one, *R_, -alpha, *AP_, *R_ );
585 if (stest_->checkStatus(
this) ==
Passed) {
591 if ( lp_->getLeftPrec() != Teuchos::null ) {
592 lp_->applyLeftPrec( *R_, *Z_ );
593 if ( lp_->getRightPrec() != Teuchos::null ) {
594 Teuchos::RCP<MV>
tmp = MVT::CloneCopy( *Z_ );
595 lp_->applyRightPrec( *
tmp, *Z_ );
598 else if ( lp_->getRightPrec() != Teuchos::null ) {
599 lp_->applyRightPrec( *R_, *Z_ );
602 MVT::Assign( *R_, *Z_ );
606 lp_->applyOp( *Z_, *AZ_ );
609 MVT::MvTransMv(
one, *S_, *Z_, *
sHz );
610 DMT::SyncDeviceToHost( *
sHz );
614 rHz_ = DMT::ValueConst(*
sHz,1,0);
618 alpha = rHz_ / (
delta - (beta*rHz_ / alpha));
622 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
629 MVT::MvAddMv(
one, *U_, beta, *V_, *V_ );
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.
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.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
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...
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< MV > AP
The matrix A applied to current decent direction vector.
Teuchos::RCP< MV > P
The current decent direction vector.
Teuchos::RCP< MV > R
The current residual.
virtual void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
Teuchos::RCP< MV > Z
The current preconditioned residual.
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero,...
This class implements the preconditioned single-reduction Conjugate Gradient (CG) iteration.
typename SCT::magnitudeType MagnitudeType
bool isInitialized()
States whether the solver has been initialized or not.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
int getNumIters() const
Get the current iteration count.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
virtual ~CGSingleRedIter()=default
Destructor.
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation (NOT_IMPLEMENTED)
CGSingleRedIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, const Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP, DM > > &convTester, Teuchos::ParameterList ¶ms)
CGSingleRedIter constructor with linear problem, solver utilities, and parameter list of solver optio...
void setDoCondEst(bool)
Sets whether or not to store the diagonal for condition estimation.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
void initializeCG(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > newstate, Teuchos::RCP< MV > R_0)
Initialize the solver to an iterate, providing a complete state.
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation (NOT_IMPLEMENTED)
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
Structure to contain pointers to CGSingleRedIteration state variables.
CGSingleRedIterationState(Teuchos::RCP< const MV > tmp)
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
CGSingleRedIterationState()=default
virtual ~CGSingleRedIterationState()=default
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).