10#ifndef BELOS_CG_ITER_HPP
11#define BELOS_CG_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) {
66 TEUCHOS_ASSERT(_numVectors == 1);
70 S = MVT::Clone( *tmp, 2 );
71 std::vector<int> index(1,0);
73 this->
R = MVT::CloneViewNonConst( *
S, index );
75 this->
Z = MVT::CloneViewNonConst( *
S, index );
77 this->
P = MVT::Clone( *tmp, 1 );
78 this->
AP = MVT::Clone(*tmp, 1);
83 bool matches(Teuchos::RCP<const MV> tmp,
int _numVectors=1)
const {
92template<
class ScalarType,
class MV,
class OP,
class DM>
103 using SCT = Teuchos::ScalarTraits<ScalarType>;
118 Teuchos::ParameterList ¶ms );
172 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState()
const {
183 auto s = Teuchos::rcp_dynamic_cast<CGIterationState<ScalarType,MV,DM> >(state,
true);
206 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
207 (*norms)[0] = std::sqrt(Teuchos::ScalarTraits<ScalarType>::magnitude(rHr_));
208 return Teuchos::null;
231 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
232 "Belos::CGIter::setBlockSize(): Cannot use a block size that is not one.");
241 if (numEntriesForCondEst_ != 0) doCondEst_=val;
249 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
250 if (
static_cast<size_type
> (iter_) >= diag_.size ()) {
253 return diag_ (0, iter_);
264 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
265 if (
static_cast<size_type
> (iter_) >= offdiag_.size ()) {
268 return offdiag_ (0, iter_);
281 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
282 const Teuchos::RCP<OutputManager<ScalarType> > om_;
283 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
284 const Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
298 bool foldConvergenceDetectionIntoAllreduce_;
304 bool assertPositiveDefiniteness_;
307 Teuchos::ArrayRCP<MagnitudeType> diag_, offdiag_;
308 int numEntriesForCondEst_;
326 Teuchos::RCP<MV> AP_;
334 template<
class ScalarType,
class MV,
class OP,
class DM>
339 Teuchos::ParameterList ¶ms ):
343 convTest_(convTester),
346 assertPositiveDefiniteness_( params.get(
"Assert Positive Definiteness", true) ),
347 numEntriesForCondEst_(params.get(
"Max Size For Condest",0) ),
350 foldConvergenceDetectionIntoAllreduce_ = params.get<
bool>(
"Fold Convergence Detection Into Allreduce",
false);
356 template <
class ScalarType,
class MV,
class OP,
class DM>
360 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
361 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
362 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
363 TEUCHOS_ASSERT(!newstate.is_null());
365 newstate->initialize(tmp, 1);
369 if(numEntriesForCondEst_ > 0) {
370 diag_.resize(numEntriesForCondEst_);
371 offdiag_.resize(numEntriesForCondEst_-1);
374 std::string errstr(
"Belos::CGIter::initialize(): Specified multivectors must have a consistent length and width.");
378 std::invalid_argument, errstr );
380 std::invalid_argument, errstr );
391 if ( lp_->getLeftPrec() != Teuchos::null ) {
392 lp_->applyLeftPrec( *R_, *Z_ );
393 if ( lp_->getRightPrec() != Teuchos::null ) {
395 lp_->applyRightPrec( *tmp2, *Z_ );
398 else if ( lp_->getRightPrec() != Teuchos::null ) {
399 lp_->applyRightPrec( *R_, *Z_ );
414 template<
class ScalarType,
class MV,
class OP,
class DM>
425 std::vector<ScalarType> alpha(1);
426 std::vector<ScalarType> beta(1);
427 std::vector<ScalarType> rHz(1);
428 std::vector<ScalarType> rHz_old(1);
429 std::vector<ScalarType> pAp(1);
433 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
434 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
437 ScalarType pAp_old = one;
438 ScalarType beta_old = one;
439 ScalarType rHz_old2 = one;
442 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
446 "Belos::CGIter::iterate(): current linear system has more than one vector!" );
449 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
460 while (stest_->checkStatus(
this) !=
Passed) {
466 lp_->applyOp( *P_, *AP_ );
470 alpha[0] = rHz[0] / pAp[0];
473 if(assertPositiveDefiniteness_) {
475 "Belos::CGIter::iterate(): non-positive value for p^H*A*p encountered!" );
481 MVT::MvAddMv( one, *cur_soln_vec, alpha[0], *P_, *cur_soln_vec );
482 lp_->updateSolution();
495 if ( lp_->getLeftPrec() != Teuchos::null ) {
496 lp_->applyLeftPrec( *R_, *Z_ );
497 if ( lp_->getRightPrec() != Teuchos::null ) {
499 lp_->applyRightPrec( *tmp, *Z_ );
502 else if ( lp_->getRightPrec() != Teuchos::null ) {
503 lp_->applyRightPrec( *R_, *Z_ );
509 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
517 beta[0] = rHz[0] / rHz_old[0];
524 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real((beta_old * beta_old * pAp_old + pAp[0]) / rHz_old[0]);
525 offdiag_[iter_-2] = -Teuchos::ScalarTraits<ScalarType>::real(beta_old * pAp_old / (sqrt( rHz_old[0] * rHz_old2)));
528 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real(pAp[0] / rHz_old[0]);
530 rHz_old2 = rHz_old[0];
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.
bool isInitialized()
States whether the solver has been initialized or not.
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation.
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
int getNumIters() const
Get the current iteration count.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
void resetNumIters(int iter=0)
Reset the iteration count.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
OperatorTraits< ScalarType, MV, OP > OPT
CGIter(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)
CGIter constructor with linear problem, solver utilities, and parameter list of solver options.
virtual ~CGIter()=default
Destructor.
Teuchos::ScalarTraits< ScalarType > SCT
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.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
DenseMatTraits< ScalarType, DM > DMT
MultiVecTraits< ScalarType, MV, DM > MVT
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
typename SCT::magnitudeType MagnitudeType
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
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.
Structure to contain pointers to CGIteration state variables.
CGIterationState()=default
virtual ~CGIterationState()=default
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
CGIterationState(Teuchos::RCP< const MV > tmp)
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
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 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 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 void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
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.