42 #ifndef BELOS_CG_SINGLE_RED_ITER_HPP
43 #define BELOS_CG_SINGLE_RED_ITER_HPP
78 template<
class ScalarType,
class MV,
class OP>
202 "Belos::CGSingleRedIter::setBlockSize(): Cannot use a block size that is not one.");
252 bool stateStorageInitialized_;
258 bool foldConvergenceDetectionIntoAllreduce_;
297 template<
class ScalarType,
class MV,
class OP>
306 convTest_(convTester),
308 stateStorageInitialized_(false),
311 foldConvergenceDetectionIntoAllreduce_ = params.
get<
bool>(
"Fold Convergence Detection Into Allreduce",
false);
316 template <
class ScalarType,
class MV,
class OP>
319 if (!stateStorageInitialized_) {
324 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
325 stateStorageInitialized_ =
false;
332 if (R_ == Teuchos::null) {
336 "Belos::CGSingleRedIter::setStateSize(): linear problem does not specify multivectors to clone from.");
337 S_ = MVT::Clone( *tmp, 2 );
338 if (foldConvergenceDetectionIntoAllreduce_) {
339 T_ = MVT::Clone( *tmp, 2 );
341 std::vector<int> index(1,0);
342 Z_ = MVT::CloneViewNonConst( *T_, index );
344 R2_ = MVT::CloneViewNonConst( *T_, index );
347 Z_ = MVT::Clone( *tmp, 1 );
348 P_ = MVT::Clone( *tmp, 1 );
349 AP_ = MVT::Clone( *tmp, 1 );
352 std::vector<int> index(1,0);
353 R_ = MVT::CloneViewNonConst( *S_, index );
355 AZ_ = MVT::CloneViewNonConst( *S_, index );
359 stateStorageInitialized_ =
true;
367 template <
class ScalarType,
class MV,
class OP>
371 if (!stateStorageInitialized_)
375 "Belos::CGSingleRedIter::initialize(): Cannot initialize state storage!");
379 std::string errstr(
"Belos::CGSingleRedIter::initialize(): Specified multivectors must have a consistent length and width.");
381 if (newstate.
R != Teuchos::null) {
384 std::invalid_argument, errstr );
386 std::invalid_argument, errstr );
389 if (newstate.
R != R_) {
391 MVT::Assign( *newstate.
R, *R_ );
397 if ( lp_->getLeftPrec() != Teuchos::null ) {
398 lp_->applyLeftPrec( *R_, *Z_ );
399 if ( lp_->getRightPrec() != Teuchos::null ) {
401 lp_->applyRightPrec( *Z_, *tmp );
402 MVT::Assign( *tmp, *Z_ );
405 else if ( lp_->getRightPrec() != Teuchos::null ) {
406 lp_->applyRightPrec( *R_, *Z_ );
409 MVT::Assign( *R_, *Z_ );
411 MVT::Assign( *Z_, *P_ );
414 lp_->applyOp( *Z_, *AZ_ );
417 MVT::Assign( *AZ_, *AP_ );
422 "Belos::CGSingleRedIter::initialize(): CGIterationState does not have initial residual.");
432 template <
class ScalarType,
class MV,
class OP>
437 return Teuchos::null;
438 }
else if (foldConvergenceDetectionIntoAllreduce_ && convTest_->getResNormType() ==
Belos::TwoNorm) {
440 return Teuchos::null;
448 template <
class ScalarType,
class MV,
class OP>
454 if (initialized_ ==
false) {
461 ScalarType rHz_old, alpha, beta, delta;
472 "Belos::CGSingleRedIter::iterate(): current linear system has more than one vector!" );
474 if (foldConvergenceDetectionIntoAllreduce_) {
476 MVT::Assign( *R_, *R2_ );
477 MVT::MvTransMv( one, *S_, *T_, sHt );
483 MVT::MvTransMv( one, *S_, *Z_, sHz );
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_) {
507 MVT::MvAddMv( one, *cur_soln_vec, alpha, *P_, *cur_soln_vec );
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 ) {
519 lp_->applyRightPrec( *Z_, *tmp );
520 MVT::Assign( *tmp, *Z_ );
523 else if ( lp_->getRightPrec() != Teuchos::null ) {
524 lp_->applyRightPrec( *R_, *Z_ );
527 MVT::Assign( *R_, *Z_ );
531 lp_->applyOp( *Z_, *AZ_ );
534 MVT::Assign( *R_, *R2_ );
535 MVT::MvTransMv( one, *S_, *T_, sHt );
545 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!" );
560 MVT::MvAddMv( one, *Z_, beta, *P_, *P_ );
566 MVT::MvAddMv( one, *AZ_, beta, *AP_, *AP_ );
577 MVT::MvAddMv( one, *cur_soln_vec, alpha, *P_, *cur_soln_vec );
581 MVT::MvAddMv( one, *R_, -alpha, *AP_, *R_ );
585 if (stest_->checkStatus(
this) ==
Passed) {
593 if ( lp_->getLeftPrec() != Teuchos::null ) {
594 lp_->applyLeftPrec( *R_, *Z_ );
595 if ( lp_->getRightPrec() != Teuchos::null ) {
597 lp_->applyRightPrec( *Z_, *tmp );
601 else if ( lp_->getRightPrec() != Teuchos::null ) {
602 lp_->applyRightPrec( *R_, *Z_ );
609 lp_->applyOp( *Z_, *AZ_ );
612 MVT::MvTransMv( one, *S_, *Z_, sHz );
619 beta = rHz_ / rHz_old;
620 alpha = rHz_ / (delta - (beta*rHz_ / alpha));
624 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
628 MVT::MvAddMv( one, *Z_, beta, *P_, *P_ );
634 MVT::MvAddMv( one, *AZ_, beta, *AP_, *AP_ );
Teuchos::RCP< const MV > R
The current residual.
Collection of types and exceptions used within the Belos solvers.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
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.
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 > & getProblem() const
Get a constant reference to the linear problem.
Structure to contain pointers to CGIteration state variables.
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)
Pure virtual base class for defining the status testing capabilities of Belos.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Declaration of basic traits for the multivector type.
An implementation of StatusTestResNorm using a family of residual norms.
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
A pure virtual class for defining the status tests for the Belos iterative solvers.
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< const MV > AP
The matrix A applied to current decent direction vector.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
MultiVecTraits< ScalarType, MV > MVT
Traits class which defines basic operations on multivectors.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation (NOT_IMPLEMENTED)
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
bool isInitialized()
States whether the solver has been initialized or not.
virtual ~CGSingleRedIter()
Destructor.
SCT::magnitudeType MagnitudeType
void initializeCG(CGIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
Teuchos::RCP< const MV > Z
The current preconditioned residual.
CGSingleRedIter(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)
CGSingleRedIter constructor with linear problem, solver utilities, and parameter list of solver optio...
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Class which defines basic traits for the operator type.
This class implements the preconditioned single-reduction Conjugate Gradient (CG) iteration...
Teuchos::ScalarTraits< ScalarType > SCT
Belos header file which uses auto-configuration information to include necessary C++ headers...
void resetNumIters(int iter=0)
Reset the iteration count.
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::RCP< const MV > P
The current decent direction vector.
CGIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
void setDoCondEst(bool)
Sets whether or not to store the diagonal for condition estimation.
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation (NOT_IMPLEMENTED)