10#ifndef BELOS_BLOCK_CG_ITER_HPP
11#define BELOS_BLOCK_CG_ITER_HPP
29#include "Teuchos_ScalarTraits.hpp"
30#include "Teuchos_ParameterList.hpp"
31#include "Teuchos_TimeMonitor.hpp"
42 template <
class ScalarType,
class MV,
class DM = DefaultDenseMatrix<
int,ScalarType>>
54 void initialize(Teuchos::RCP<const MV> tmp,
int _numVectors) {
56 this->
R = MVT::Clone( *tmp, _numVectors );
57 this->
Z = MVT::Clone( *tmp, _numVectors );
58 this->
P = MVT::Clone( *tmp, _numVectors );
59 this->
AP = MVT::Clone(*tmp, _numVectors );
64 bool matches(Teuchos::RCP<const MV> tmp,
int _numVectors=1)
const {
78template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>,
79 const
bool lapackSupportsScalarType =
80 Belos::Details::LapackSupportsScalar<ScalarType>::value>
85 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
92 Teuchos::ParameterList & )
94 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
100 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
104 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
108 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
111 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState ()
const {
112 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
116 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
120 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
124 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
127 Teuchos::RCP<const MV>
129 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
133 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
137 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
141 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
145 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
149 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
153 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Stub");
162template<
class ScalarType,
class MV,
class OP,
class DM>
172 using SCT = Teuchos::ScalarTraits<ScalarType>;
187 Teuchos::ParameterList ¶ms );
241 Teuchos::RCP<CGIterationStateBase<ScalarType,MV, DM> >
getState()
const {
251 auto s = Teuchos::rcp_dynamic_cast<BlockCGIterationState<ScalarType,MV, DM> >(state,
true);
301 Teuchos::ArrayView<MagnitudeType> temp;
307 Teuchos::ArrayView<MagnitudeType> temp;
320 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
321 const Teuchos::RCP<OutputManager<ScalarType> > om_;
322 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
323 const Teuchos::RCP<OrthoManager<ScalarType,MV,DM> > ortho_;
355 Teuchos::RCP<MV> AP_;
359 template<
class ScalarType,
class MV,
class OP,
class DM>
365 Teuchos::ParameterList& params) :
375 int bs = params.get(
"Block Size", 1);
379 template<
class ScalarType,
class MV,
class OP,
class DM>
384 TEUCHOS_TEST_FOR_EXCEPTION
385 (blockSize <= 0, std::invalid_argument,
"Belos::BlockGmresIter::"
386 "setBlockSize: blockSize = " << blockSize <<
" <= 0.");
387 if (blockSize == blockSize_) {
390 blockSize_ = blockSize;
391 initialized_ =
false;
394 template <
class ScalarType,
class MV,
class OP,
class DM>
398 const char prefix[] =
"Belos::BlockCGIter::initialize: ";
401 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
402 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
403 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
404 TEUCHOS_ASSERT(!newstate.is_null());
406 newstate->initialize(tmp, blockSize_);
410 const char errstr[] =
"Specified multivectors must have a consistent "
415 TEUCHOS_TEST_FOR_EXCEPTION
417 std::invalid_argument, prefix << errstr );
418 TEUCHOS_TEST_FOR_EXCEPTION
420 std::invalid_argument, prefix << errstr );
430 if ( lp_->getLeftPrec() != Teuchos::null ) {
431 lp_->applyLeftPrec( *R_, *Z_ );
432 if ( lp_->getRightPrec() != Teuchos::null ) {
433 Teuchos::RCP<MV> tmp2 =
MVT::Clone( *Z_, blockSize_ );
434 lp_->applyRightPrec( *Z_, *tmp2 );
438 else if ( lp_->getRightPrec() != Teuchos::null ) {
439 lp_->applyRightPrec( *R_, *Z_ );
451 template<
class ScalarType,
class MV,
class OP,
class DM>
454 const char prefix[] =
"Belos::BlockCGIter::iterate: ";
460 if (initialized_ ==
false) {
465 Teuchos::RCP<DM> alpha = DMT::Create( blockSize_, blockSize_ );
466 Teuchos::RCP<DM> beta = DMT::Create( blockSize_, blockSize_ );
467 Teuchos::RCP<DM> pAp = DMT::Create( blockSize_, blockSize_ );
470 Teuchos::RCP<DenseSolver<ScalarType,DM>> lltSolver = DMT::createDenseSolver();
471 lltSolver->setSPD(
true );
474 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
477 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
480 TEUCHOS_TEST_FOR_EXCEPTION
482 prefix <<
"Current linear system does not have the right number of vectors!" );
483 int rank = ortho_->normalize( *P_, Teuchos::null );
484 TEUCHOS_TEST_FOR_EXCEPTION
486 prefix <<
"Failed to compute initial block of orthonormal direction vectors.");
491 while (stest_->checkStatus(
this) !=
Passed) {
496 lp_->applyOp( *P_, *AP_ );
507 lltSolver->setMatrix( pAp );
508 lltSolver->factorWithEquilibration(
true );
509 int info = lltSolver->factor();
510 TEUCHOS_TEST_FOR_EXCEPTION
512 prefix <<
"Failed to compute Cholesky factorization using LAPACK routine POTRF.");
516 lltSolver->setVectors (alpha, alpha);
517 info = lltSolver->solve();
518 TEUCHOS_TEST_FOR_EXCEPTION
520 prefix <<
"Failed to compute alpha using Cholesky factorization (POTRS).");
524 lp_->updateSolution();
530 if ( lp_->getLeftPrec() != Teuchos::null ) {
531 lp_->applyLeftPrec( *R_, *Z_ );
532 if ( lp_->getRightPrec() != Teuchos::null ) {
533 Teuchos::RCP<MV> tmp =
MVT::Clone( *Z_, blockSize_ );
534 lp_->applyRightPrec( *Z_, *tmp );
538 else if ( lp_->getRightPrec() != Teuchos::null ) {
539 lp_->applyRightPrec( *R_, *Z_ );
553 lltSolver->setVectors( beta, beta );
554 info = lltSolver->solve();
555 TEUCHOS_TEST_FOR_EXCEPTION
557 prefix <<
"Failed to compute beta using Cholesky factorization (POTRS).");
565 rank = ortho_->normalize( *P_, Teuchos::null );
566 TEUCHOS_TEST_FOR_EXCEPTION
568 prefix <<
"Failed to compute block of orthonormal direction vectors.");
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.
virtual ~BlockCGIter()=default
Destructor.
int getNumIters() const
Get the current iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
typename SCT::magnitudeType MagnitudeType
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
MultiVecTraits< ScalarType, MV, DM > MVT
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ArrayView< MagnitudeType > getDiag()
Gets the diagonal for condition estimation (NOT_IMPLEMENTED).
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
int getBlockSize() const
Get the block size to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Teuchos::ArrayView< MagnitudeType > getOffDiag()
Gets the off-diagonal for condition estimation (NOT_IMPLEMENTED).
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
bool isInitialized()
States whether the solver has been initialized or not.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
OperatorTraits< ScalarType, MV, OP > OPT
BlockCGIter(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< MatOrthoManager< ScalarType, MV, OP, DM > > &ortho, Teuchos::ParameterList ¶ms)
BlockCGIter constructor with linear problem, solver utilities, and parameter list of solver options.
void setDoCondEst(bool)
Sets whether or not to store the diagonal for condition estimation.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void resetNumIters(int iter=0)
Reset the iteration count to iter.
BlockCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &, const Teuchos::RCP< OutputManager< ScalarType > > &, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &, const Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP, DM > > &, Teuchos::ParameterList &)
Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > getState() const
Get the current state of the linear solver.
void iterate()
This method performs linear solver iterations until the status test indicates the need to stop or an ...
void setDoCondEst(bool val)
Sets whether or not to store the diagonal for condition estimation.
Teuchos::ScalarTraits< ScalarType > SCT
SCT::magnitudeType MagnitudeType
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
void setState(Teuchos::RCP< CGIterationStateBase< ScalarType, MV, DM > > state)
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
MultiVecTraits< ScalarType, MV, DM > MVT
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
void initializeCG(Teuchos::RCP< BlockCGIterationState< ScalarType, MV, DM > >, Teuchos::RCP< MV >)
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
int getNumIters() const
Get the current iteration count.
bool isInitialized()
States whether the solver has been initialized or not.
OperatorTraits< ScalarType, MV, OP > OPT
Structure to contain pointers to BlockCGIteration state variables.
BlockCGIterationState()=default
bool matches(Teuchos::RCP< const MV > tmp, int _numVectors=1) const
BlockCGIterationState(Teuchos::RCP< const MV > tmp)
void initialize(Teuchos::RCP< const MV > tmp, int _numVectors)
virtual ~BlockCGIterationState()=default
CGIterateFailure is thrown when the CGIteration object is unable to compute the next iterate in the C...
CGIterationLAPACKFailure is thrown when a nonzero return value is passed back from an LAPACK routine.
CGIterationOrthoFailure is thrown when the CGIteration object is unable to compute independent direct...
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.
Virtual base class which defines basic traits for the multi-vector type.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
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 MvTimesMatAddMv(const ScalarType alpha, const MV &A, const DM &B, const ScalarType beta, 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...
A pure virtual class for defining the status tests for the Belos iterative solvers.