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 = DefaultDenseMatrix<
int,ScalarType>>
64 void initialize(Teuchos::RCP<const MV> tmp,
int _numVectors) {
67 TEUCHOS_ASSERT(_numVectors == 1);
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 );
106 bool matches(Teuchos::RCP<const MV> tmp,
int _numVectors=1)
const {
125template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
136 using SCT = Teuchos::ScalarTraits<ScalarType>;
151 Teuchos::ParameterList ¶ms );
205 Teuchos::RCP<CGIterationStateBase<ScalarType,MV,DM> >
getState()
const {
221 auto s = Teuchos::rcp_dynamic_cast<CGSingleRedIterationState<ScalarType,MV,DM> >(state,
true);
268 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
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 ¶ms ):
362 convTest_(convTester),
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();
377 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
378 TEUCHOS_ASSERT(!newstate.is_null());
380 newstate->initialize(tmp, 1);
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 );
401 if ( lp_->getLeftPrec() != Teuchos::null ) {
402 lp_->applyLeftPrec( *R_, *Z_ );
403 if ( lp_->getRightPrec() != Teuchos::null ) {
405 lp_->applyRightPrec( *Z_, *tmp2 );
409 else if ( lp_->getRightPrec() != Teuchos::null ) {
410 lp_->applyRightPrec( *R_, *Z_ );
417 lp_->applyOp( *Z_, *AZ_ );
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>
460 ScalarType rHz_old, alpha, beta, delta;
463 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
464 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
467 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
471 "Belos::CGSingleRedIter::iterate(): current linear system has more than one vector!" );
473 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
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) {
507 MVT::MvAddMv( one, *cur_soln_vec, alpha, *P_, *cur_soln_vec );
515 if ( lp_->getLeftPrec() != Teuchos::null ) {
516 lp_->applyLeftPrec( *R_, *Z_ );
517 if ( lp_->getRightPrec() != Teuchos::null ) {
519 lp_->applyRightPrec( *tmp, *Z_ );
522 else if ( lp_->getRightPrec() != Teuchos::null ) {
523 lp_->applyRightPrec( *R_, *Z_ );
530 lp_->applyOp( *Z_, *AZ_ );
547 if (stest_->checkStatus(
this) ==
Passed) {
551 beta = rHz_ / rHz_old;
552 alpha = rHz_ / (delta - (beta*rHz_ / alpha));
556 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
574 MVT::MvAddMv( one, *cur_soln_vec, alpha, *P_, *cur_soln_vec );
585 if (stest_->checkStatus(
this) ==
Passed) {
591 if ( lp_->getLeftPrec() != Teuchos::null ) {
592 lp_->applyLeftPrec( *R_, *Z_ );
593 if ( lp_->getRightPrec() != Teuchos::null ) {
595 lp_->applyRightPrec( *tmp, *Z_ );
598 else if ( lp_->getRightPrec() != Teuchos::null ) {
599 lp_->applyRightPrec( *R_, *Z_ );
606 lp_->applyOp( *Z_, *AZ_ );
617 beta = rHz_ / rHz_old;
618 alpha = rHz_ / (delta - (beta*rHz_ / alpha));
622 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
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.
virtual bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
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,...
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.
MultiVecTraits< ScalarType, MV, DM > MVT
OperatorTraits< ScalarType, MV, OP > OPT
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
DenseMatTraits< ScalarType, DM > DMT
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
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
Virtual base class which defines basic traits for the multi-vector type.
static const ScalarType & ValueConst(const DM &dm, const int i, const int j)
Access a const reference to the (i,j) entry of dm, e_i^T dm e_j.
static void SyncDeviceToHost(DM &dm)
static Teuchos::RCP< DM > Create()
Creates a new empty DM with no dimension.
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
static void Assign(const MV &A, MV &mv)
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, DM &B)
static ptrdiff_t GetGlobalLength(const MV &mv)
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...
An implementation of StatusTestResNorm using a family of residual norms.
A pure virtual class for defining the status tests for the Belos iterative solvers.