10 #ifndef BELOS_CG_ITER_HPP
11 #define BELOS_CG_ITER_HPP
53 template <
class ScalarType,
class MV>
71 S = MVT::Clone( *tmp, 2 );
72 std::vector<int> index(1,0);
74 this->
R = MVT::CloneViewNonConst( *
S, index );
76 this->
Z = MVT::CloneViewNonConst( *
S, index );
78 this->
P = MVT::Clone( *tmp, 1 );
79 this->
AP = MVT::Clone(*tmp, 1);
93 template<
class ScalarType,
class MV,
class OP>
206 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
208 return Teuchos::null;
232 "Belos::CGIter::setBlockSize(): Cannot use a block size that is not one.");
241 if (numEntriesForCondEst_ != 0) doCondEst_=val;
250 if (static_cast<size_type> (iter_) >= diag_.
size ()) {
253 return diag_ (0, iter_);
265 if (static_cast<size_type> (iter_) >= offdiag_.
size ()) {
268 return offdiag_ (0, iter_);
298 bool foldConvergenceDetectionIntoAllreduce_;
304 bool assertPositiveDefiniteness_;
308 int numEntriesForCondEst_;
334 template<
class ScalarType,
class MV,
class OP>
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>
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 );
385 MVT::Assign( *R_0, *R_ );
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_ );
402 MVT::Assign( *R_, *Z_ );
404 MVT::Assign( *Z_, *P_ );
414 template <
class ScalarType,
class MV,
class OP>
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);
437 ScalarType pAp_old = one;
438 ScalarType beta_old = one;
439 ScalarType rHz_old2 = one;
446 "Belos::CGIter::iterate(): current linear system has more than one vector!" );
449 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
450 MVT::MvTransMv( one, *R_, *S_, rHs );
454 MVT::MvDot( *R_, *Z_, rHz );
459 while (stest_->checkStatus(
this) !=
Passed) {
465 lp_->applyOp( *P_, *AP_ );
468 MVT::MvDot( *P_, *AP_, pAp );
469 alpha[0] = rHz[0] / pAp[0];
472 if(assertPositiveDefiniteness_) {
474 "Belos::CGIter::iterate(): non-positive value for p^H*A*p encountered!" );
480 MVT::MvAddMv( one, *cur_soln_vec, alpha[0], *P_, *cur_soln_vec );
481 lp_->updateSolution();
489 MVT::MvAddMv( one, *R_, -alpha[0], *AP_, *R_ );
494 if ( lp_->getLeftPrec() != Teuchos::null ) {
495 lp_->applyLeftPrec( *R_, *Z_ );
496 if ( lp_->getRightPrec() != Teuchos::null ) {
498 lp_->applyRightPrec( *tmp, *Z_ );
501 else if ( lp_->getRightPrec() != Teuchos::null ) {
502 lp_->applyRightPrec( *R_, *Z_ );
505 MVT::Assign( *R_, *Z_ );
508 if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
509 MVT::MvTransMv( one, *R_, *S_, rHs );
513 MVT::MvDot( *R_, *Z_, rHz );
515 beta[0] = rHz[0] / rHz_old[0];
517 MVT::MvAddMv( one, *Z_, beta[0], *P_, *P_ );
528 rHz_old2 = rHz_old[0];
Collection of types and exceptions used within the Belos solvers.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
Class which manages the output and verbosity of the Belos solvers.
void initializeCG(Teuchos::RCP< CGIterationStateBase< ScalarType, MV > > newstate, Teuchos::RCP< MV > R_0)
Initialize the solver to an iterate, providing a complete state.
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
This class implements the preconditioned Conjugate Gradient (CG) iteration.
Pure virtual base class which augments the basic interface for a conjugate gradient linear solver ite...
T & get(const std::string &name, T def_value)
Teuchos::RCP< CGIterationStateBase< ScalarType, MV > > getState() const
Get the current state of the linear solver.
virtual ~CGIterationState()=default
Pure virtual base class for defining the status testing capabilities of Belos.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static magnitudeType real(T a)
Declaration of basic traits for the multivector type.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
An implementation of StatusTestResNorm using a family of residual norms.
A pure virtual class for defining the status tests for the Belos iterative solvers.
CGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, const Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > &convTester, Teuchos::ParameterList ¶ms)
CGIter constructor with linear problem, solver utilities, and parameter list of solver options...
Class which defines basic traits for the operator type.
CGIterateFailure is thrown when the CGIteration object is unable to compute the next iterate in the C...
Teuchos::RCP< MV > P
The current decent direction vector.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
bool isInitialized()
States whether the solver has been initialized or not.
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
int getNumIters() const
Get the current iteration count.
Traits class which defines basic operations on multivectors.
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero...
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual ~CGIter()=default
Destructor.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
virtual void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
CGIterationState()=default
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV > > state)
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation.
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< MV > Z
The current preconditioned residual.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
Teuchos::RCP< MV > AP
The matrix A applied to current decent direction vector.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
Structure to contain pointers to CGIteration state variables.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
typename SCT::magnitudeType MagnitudeType
void resetNumIters(int iter=0)
Reset the iteration count.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
Class which defines basic traits for the operator type.
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::RCP< MV > R
The current residual.
Belos header file which uses auto-configuration information to include necessary C++ headers...
CGIterationState(Teuchos::RCP< const MV > tmp)