42 #ifndef BELOS_BLOCK_CG_ITER_HPP 43 #define BELOS_BLOCK_CG_ITER_HPP 60 #include "Teuchos_BLAS.hpp" 61 #include "Teuchos_LAPACK.hpp" 62 #include "Teuchos_SerialDenseMatrix.hpp" 63 #include "Teuchos_SerialDenseVector.hpp" 64 #include "Teuchos_SerialSymDenseMatrix.hpp" 65 #include "Teuchos_SerialSpdDenseSolver.hpp" 66 #include "Teuchos_ScalarTraits.hpp" 67 #include "Teuchos_ParameterList.hpp" 68 #include "Teuchos_TimeMonitor.hpp" 82 template<
class ScalarType,
class MV,
class OP>
93 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
108 Teuchos::ParameterList ¶ms );
223 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
224 const Teuchos::RCP<OutputManager<ScalarType> > om_;
225 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
226 const Teuchos::RCP<OrthoManager<ScalarType,MV> > ortho_;
245 bool stateStorageInitialized_;
263 Teuchos::RCP<MV> AP_;
269 template<
class ScalarType,
class MV,
class OP>
274 Teuchos::ParameterList ¶ms ):
281 stateStorageInitialized_(false),
285 int bs = params.get(
"Block Size", 1);
291 template <
class ScalarType,
class MV,
class OP>
294 if (!stateStorageInitialized_) {
297 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
298 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
299 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
300 stateStorageInitialized_ =
false;
309 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
310 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
311 "Belos::BlockCGIter::setStateSize(): linear problem does not specify multivectors to clone from.");
319 stateStorageInitialized_ =
true;
326 template <
class ScalarType,
class MV,
class OP>
332 TEUCHOS_TEST_FOR_EXCEPTION(blockSize <= 0, std::invalid_argument,
"Belos::BlockGmresIter::setBlockSize was passed a non-positive argument.");
333 if (blockSize == blockSize_) {
338 if (blockSize!=blockSize_)
339 stateStorageInitialized_ =
false;
341 blockSize_ = blockSize;
343 initialized_ =
false;
352 template <
class ScalarType,
class MV,
class OP>
356 if (!stateStorageInitialized_)
359 TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
360 "Belos::BlockCGIter::initialize(): Cannot initialize state storage!");
364 std::string errstr(
"Belos::BlockCGIter::initialize(): Specified multivectors must have a consistent length and width.");
367 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
368 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
370 if (newstate.
R != Teuchos::null) {
373 std::invalid_argument, errstr );
375 std::invalid_argument, errstr );
378 if (newstate.
R != R_) {
385 if ( lp_->getLeftPrec() != Teuchos::null ) {
386 lp_->applyLeftPrec( *R_, *Z_ );
387 if ( lp_->getRightPrec() != Teuchos::null ) {
388 Teuchos::RCP<MV> tmp =
MVT::Clone( *Z_, blockSize_ );
389 lp_->applyRightPrec( *Z_, *tmp );
393 else if ( lp_->getRightPrec() != Teuchos::null ) {
394 lp_->applyRightPrec( *R_, *Z_ );
403 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
R == Teuchos::null,std::invalid_argument,
404 "Belos::BlockCGIter::initialize(): BlockCGStateIterState does not have initial residual.");
414 template <
class ScalarType,
class MV,
class OP>
420 if (initialized_ ==
false) {
428 Teuchos::LAPACK<int,ScalarType> lapack;
431 Teuchos::SerialDenseMatrix<int,ScalarType> alpha( blockSize_, blockSize_ );
432 Teuchos::SerialDenseMatrix<int,ScalarType> beta( blockSize_, blockSize_ );
433 Teuchos::SerialDenseMatrix<int,ScalarType> rHz( blockSize_, blockSize_ ),
434 rHz_old( blockSize_, blockSize_ ), pAp( blockSize_, blockSize_ );
435 Teuchos::SerialSymDenseMatrix<int,ScalarType> pApHerm(Teuchos::View, uplo, pAp.values(), blockSize_, blockSize_);
438 Teuchos::SerialSpdDenseSolver<int,ScalarType> lltSolver;
441 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
444 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
448 "Belos::BlockCGIter::iterate(): current linear system does not have the right number of vectors!" );
449 int rank = ortho_->normalize( *P_, Teuchos::null );
451 "Belos::BlockCGIter::iterate(): Failed to compute initial block of orthonormal direction vectors.");
457 while (stest_->checkStatus(
this) !=
Passed) {
463 lp_->applyOp( *P_, *AP_ );
474 lltSolver.setMatrix( Teuchos::rcp(&pApHerm,
false) );
475 lltSolver.factorWithEquilibration(
true );
476 info = lltSolver.factor();
478 "Belos::BlockCGIter::iterate(): Failed to compute Cholesky factorization using LAPACK routine POTRF.");
481 lltSolver.setVectors( Teuchos::rcp( &alpha,
false ), Teuchos::rcp( &alpha,
false ) );
482 info = lltSolver.solve();
484 "Belos::BlockCGIter::iterate(): Failed to compute alpha using Cholesky factorization (POTRS).");
490 lp_->updateSolution();
497 if ( lp_->getLeftPrec() != Teuchos::null ) {
498 lp_->applyLeftPrec( *R_, *Z_ );
499 if ( lp_->getRightPrec() != Teuchos::null ) {
500 Teuchos::RCP<MV> tmp =
MVT::Clone( *Z_, blockSize_ );
501 lp_->applyRightPrec( *Z_, *tmp );
505 else if ( lp_->getRightPrec() != Teuchos::null ) {
506 lp_->applyRightPrec( *R_, *Z_ );
520 lltSolver.setVectors( Teuchos::rcp( &beta,
false ), Teuchos::rcp( &beta,
false ) );
521 info = lltSolver.solve();
523 "Belos::BlockCGIter::iterate(): Failed to compute beta using Cholesky factorization (POTRS).");
532 rank = ortho_->normalize( *P_, Teuchos::null );
534 "Belos::BlockCGIter::iterate(): Failed to compute block of orthonormal direction vectors.");
Teuchos::RCP< const MV > R
The current residual.
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...
void resetNumIters(int iter=0)
Reset the iteration count.
Class which manages the output and verbosity of the Belos solvers.
OperatorTraits< ScalarType, MV, OP > OPT
Structure to contain pointers to CGIteration state variables.
Pure virtual base class which augments the basic interface for a conjugate gradient linear solver ite...
Pure virtual base class for defining the status testing capabilities of Belos.
Declaration of basic traits for the multivector type.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
An extension of the MultiVecTraits class that adds a new vector length method.
MultiVecTraitsExt< ScalarType, MV > MVText
BlockCGIter(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< MatOrthoManager< ScalarType, MV, OP > > &ortho, Teuchos::ParameterList ¶ms)
BlockCGIter constructor with linear problem, solver utilities, and parameter list of solver options...
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
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.
bool isInitialized()
States whether the solver has been initialized or not.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
Traits class which defines basic operations on multivectors.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
MultiVecTraits< ScalarType, MV > MVT
CGIterationLAPACKFailure is thrown when a nonzero return value is passed back from an LAPACK routine...
Teuchos::ScalarTraits< ScalarType > SCT
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
SCT::magnitudeType MagnitudeType
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
void iterate()
This method performs BlockCG iterations until the status test indicates the need to stop or an error ...
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
virtual ~BlockCGIter()
Destructor.
Teuchos::RCP< const MV > Z
The current preconditioned residual.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
void setBlockSize(int blockSize)
Set the block size to be used by the iterative solver in solving this linear problem.
CGIterationOrthoFailure is thrown when the CGIteration object is unable to compute independent direct...
int getNumIters() const
Get the current iteration count.
CGIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
static ptrdiff_t GetGlobalLength(const MV &mv)
Obtain the vector length of mv.
Class which defines basic traits for the operator type.
void initializeCG(CGIterationState< ScalarType, MV > newstate)
Initialize the solver to an iterate, providing a complete state.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
int getBlockSize() const
Get the block size to be used by the iterative solver in solving this linear problem.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< const MV > P
The current decent direction vector.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
This class implements the block, preconditioned Conjugate Gradient (CG) iteration.