10#ifndef BELOS_PSEUDO_BLOCK_CG_ITER_HPP
11#define BELOS_PSEUDO_BLOCK_CG_ITER_HPP
29#include "Teuchos_ScalarTraits.hpp"
30#include "Teuchos_ParameterList.hpp"
31#include "Teuchos_TimeMonitor.hpp"
53 template <
class ScalarType,
class MV,
class DM>
80 template<
class ScalarType,
class MV,
class OP,
class DM>
91 using SCT = Teuchos::ScalarTraits<ScalarType>;
105 Teuchos::ParameterList &
params );
167 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState()
const {
177 auto s = Teuchos::rcp_dynamic_cast<PseudoBlockCGIterationState<ScalarType,MV, DM> >(
state,
true);
219 "Belos::PseudoBlockCGIter::setBlockSize(): Cannot use a block size that is not one.");
229 if (numEntriesForCondEst_ != 0) doCondEst_=
val;
237 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
238 if (
static_cast<size_type
> (iter_) >= diag_.size ()) {
241 return diag_ (0, iter_);
252 using size_type =
typename Teuchos::ArrayView<MagnitudeType>::size_type;
253 if (
static_cast<size_type
> (iter_) >= offdiag_.size ()) {
256 return offdiag_ (0, iter_);
265 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
266 const Teuchos::RCP<OutputManager<ScalarType> > om_;
267 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
287 bool assertPositiveDefiniteness_;
290 Teuchos::ArrayRCP<MagnitudeType> diag_, offdiag_;
292 int numEntriesForCondEst_;
308 Teuchos::RCP<MV> AP_;
314 template<
class ScalarType,
class MV,
class OP,
class DM>
318 Teuchos::ParameterList &
params ):
325 assertPositiveDefiniteness_(
params.
get(
"Assert Positive Definiteness",
true) ),
326 numEntriesForCondEst_(
params.
get(
"Max Size For Condest",0) ),
334 template<
class ScalarType,
class MV,
class OP,
class DM>
338 Teuchos::RCP<const MV>
lhsMV = lp_->getCurrLHSVec();
339 Teuchos::RCP<const MV>
rhsMV = lp_->getCurrRHSVec();
341 "Belos::PseudoBlockCGIter::initialize(): Cannot initialize state storage!");
357 if(numEntriesForCondEst_ > 0) {
358 diag_.resize(numEntriesForCondEst_);
359 offdiag_.resize(numEntriesForCondEst_-1);
362 std::string
errstr(
"Belos::BlockPseudoCGIter::initialize(): Specified multivectors must have a consistent length and width.");
366 std::invalid_argument,
errstr );
368 std::invalid_argument,
errstr );
373 MVT::Assign( *
R_0, *R_ );
379 if ( lp_->getLeftPrec() != Teuchos::null ) {
380 lp_->applyLeftPrec( *R_, *Z_ );
381 if ( lp_->getRightPrec() != Teuchos::null ) {
382 Teuchos::RCP<MV>
tmp1 = MVT::Clone( *Z_, numRHS_ );
383 lp_->applyRightPrec( *Z_, *
tmp1 );
387 else if ( lp_->getRightPrec() != Teuchos::null ) {
388 lp_->applyRightPrec( *R_, *Z_ );
391 MVT::Assign( *R_, *Z_ );
393 MVT::Assign( *Z_, *P_ );
403 template<
class ScalarType,
class MV,
class OP,
class DM>
415 std::vector<int> index(1);
416 std::vector<ScalarType>
rHz( numRHS_ );
417 std::vector<ScalarType>
rHz_old( numRHS_ );
418 std::vector<ScalarType>
pAp( numRHS_ );
419 std::vector<ScalarType> beta( numRHS_ );
420 Teuchos::RCP<DM> alpha = DMT::Create( numRHS_,numRHS_ );
423 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
430 MVT::MvDot( *R_, *Z_,
rHz );
432 if ( assertPositiveDefiniteness_ )
433 for (
i=0;
i<numRHS_; ++
i)
436 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
441 while (stest_->checkStatus(
this) !=
Passed) {
447 lp_->applyOp( *P_, *AP_ );
450 MVT::MvDot( *P_, *AP_,
pAp );
452 for (
i=0;
i<numRHS_; ++
i) {
453 if ( assertPositiveDefiniteness_ )
457 "Belos::PseudoBlockCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
461 DMT::SyncHostToDevice( *alpha );
467 lp_->updateSolution();
471 for (
i=0;
i<numRHS_; ++
i) {
477 MVT::MvTimesMatAddMv( -
one, *AP_, *alpha,
one, *R_ );
482 if ( lp_->getLeftPrec() != Teuchos::null ) {
483 lp_->applyLeftPrec( *R_, *Z_ );
484 if ( lp_->getRightPrec() != Teuchos::null ) {
485 Teuchos::RCP<MV>
tmp = MVT::Clone( *Z_, numRHS_ );
486 lp_->applyRightPrec( *Z_, *
tmp );
490 else if ( lp_->getRightPrec() != Teuchos::null ) {
491 lp_->applyRightPrec( *R_, *Z_ );
497 MVT::MvDot( *R_, *Z_,
rHz );
498 if ( assertPositiveDefiniteness_ )
499 for (
i=0;
i<numRHS_; ++
i)
502 "Belos::PseudoBlockCGIter::iterate(): negative value for r^H*M*r encountered!" );
505 for (
i=0;
i<numRHS_; ++
i) {
508 Teuchos::RCP<const MV>
Z_i = MVT::CloneView( *Z_, index );
509 Teuchos::RCP<MV>
P_i = MVT::CloneViewNonConst( *P_, index );
514 if (doCondEst_ && (iter_ - 1) < diag_.size()) {
516 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real((beta_old_ * beta_old_ * pAp_old_ +
pAp[0]) /
rHz_old[0]);
517 offdiag_[iter_-2] = -Teuchos::ScalarTraits<ScalarType>::real(beta_old_ * pAp_old_ / (
sqrt(
rHz_old[0] * rHz_old2_)));
520 diag_[iter_-1] = Teuchos::ScalarTraits<ScalarType>::real(
pAp[0] /
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.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
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.
Collection of types and exceptions used within the Belos solvers.
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,...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
This class implements the pseudo-block CG iteration, where the basic CG algorithm is performed on all...
PseudoBlockCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, Teuchos::ParameterList ¶ms)
PseudoBlockCGIter constructor with linear problem, solver utilities, and parameter list of solver opt...
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
void iterate()
This method performs CG iterations on each linear system until the status test indicates the need to ...
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation.
int getNumIters() const
Get the current iteration count.
void initializeCG(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > newstate, Teuchos::RCP< MV > R_0)
Initialize the solver to an iterate, providing a complete state.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Get the norms of the residuals native to the solver.
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation.
typename SCT::magnitudeType MagnitudeType
virtual ~PseudoBlockCGIter()=default
Destructor.
void setBlockSize(int blockSize)
Set the blocksize.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
bool isInitialized()
States whether the solver has been initialized or not.
Structure to contain pointers to PseudoBlockCGIteration state variables.
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
PseudoBlockCGIterationState(Teuchos::RCP< const MV > tmp)
virtual ~PseudoBlockCGIterationState()=default
PseudoBlockCGIterationState()=default