42 #ifndef BELOS_PSEUDO_BLOCK_GMRES_ITER_HPP 43 #define BELOS_PSEUDO_BLOCK_GMRES_ITER_HPP 60 #include "Teuchos_BLAS.hpp" 61 #include "Teuchos_SerialDenseMatrix.hpp" 62 #include "Teuchos_SerialDenseVector.hpp" 63 #include "Teuchos_ScalarTraits.hpp" 64 #include "Teuchos_ParameterList.hpp" 65 #include "Teuchos_TimeMonitor.hpp" 89 template <
class ScalarType,
class MV>
92 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
101 std::vector<Teuchos::RCP<const MV> >
V;
107 std::vector<Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> > >
H;
109 std::vector<Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> > >
R;
111 std::vector<Teuchos::RCP<const Teuchos::SerialDenseVector<int,ScalarType> > >
Z;
113 std::vector<Teuchos::RCP<const Teuchos::SerialDenseVector<int,ScalarType> > >
sn;
114 std::vector<Teuchos::RCP<const Teuchos::SerialDenseVector<int,MagnitudeType> > >
cs;
154 template<
class ScalarType,
class MV,
class OP>
165 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
183 Teuchos::ParameterList ¶ms );
258 state.
V.resize(numRHS_);
259 state.
H.resize(numRHS_);
260 state.
Z.resize(numRHS_);
261 state.
sn.resize(numRHS_);
262 state.
cs.resize(numRHS_);
263 for (
int i=0; i<numRHS_; ++i) {
267 state.
sn[i] = sn_[i];
268 state.
cs[i] = cs_[i];
302 Teuchos::RCP<const MV> getNativeResiduals( std::vector<MagnitudeType> *norms )
const;
310 Teuchos::RCP<MV> getCurrentUpdate()
const;
316 void updateLSQR(
int dim = -1 );
320 if (!initialized_)
return 0;
341 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
342 "Belos::PseudoBlockGmresIter::setBlockSize(): Cannot use a block size that is not one.");
349 void setNumBlocks(
int numBlocks);
361 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
362 const Teuchos::RCP<OutputManager<ScalarType> > om_;
363 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
364 const Teuchos::RCP<OrthoManager<ScalarType,MV> > ortho_;
375 std::vector<Teuchos::RCP<Teuchos::SerialDenseVector<int,ScalarType> > > sn_;
376 std::vector<Teuchos::RCP<Teuchos::SerialDenseVector<int,MagnitudeType> > > cs_;
379 Teuchos::RCP<MV> U_vec_, AU_vec_;
382 Teuchos::RCP<MV> cur_block_rhs_, cur_block_sol_;
398 std::vector<Teuchos::RCP<MV> > V_;
403 std::vector<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > H_;
408 std::vector<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > R_;
409 std::vector<Teuchos::RCP<Teuchos::SerialDenseVector<int,ScalarType> > > Z_;
414 template<
class ScalarType,
class MV,
class OP>
419 Teuchos::ParameterList ¶ms ):
431 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Num Blocks"), std::invalid_argument,
432 "Belos::PseudoBlockGmresIter::constructor: mandatory parameter 'Num Blocks' is not specified.");
433 int nb = Teuchos::getParameter<int>(params,
"Num Blocks");
440 template <
class ScalarType,
class MV,
class OP>
446 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0, std::invalid_argument,
"Belos::PseudoBlockGmresIter::setNumBlocks was passed a non-positive argument.");
448 numBlocks_ = numBlocks;
451 initialized_ =
false;
456 template <
class ScalarType,
class MV,
class OP>
463 Teuchos::RCP<MV> currentUpdate = Teuchos::null;
465 return currentUpdate;
467 currentUpdate =
MVT::Clone(*(V_[0]), numRHS_);
468 std::vector<int> index(1), index2(curDim_);
469 for (
int i=0; i<curDim_; ++i) {
472 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
473 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
474 Teuchos::BLAS<int,ScalarType> blas;
476 for (
int i=0; i<numRHS_; ++i) {
482 Teuchos::SerialDenseVector<int,ScalarType> y( Teuchos::Copy, Z_[i]->values(), curDim_ );
486 blas.TRSM( Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
487 Teuchos::NON_UNIT_DIAG, curDim_, 1, one,
488 H_[i]->values(), H_[i]->stride(), y.values(), y.stride() );
494 return currentUpdate;
501 template <
class ScalarType,
class MV,
class OP>
502 Teuchos::RCP<const MV>
506 typedef typename Teuchos::ScalarTraits<ScalarType> STS;
512 if (static_cast<int> (norms->size()) < numRHS_)
513 norms->resize (numRHS_);
515 Teuchos::BLAS<int, ScalarType> blas;
516 for (
int j = 0; j < numRHS_; ++j)
518 const ScalarType curNativeResid = (*Z_[j])(curDim_);
519 (*norms)[j] = STS::magnitude (curNativeResid);
522 return Teuchos::null;
526 template <
class ScalarType,
class MV,
class OP>
541 std::string errstr (
"Belos::PseudoBlockGmresIter::initialize(): " 542 "Specified multivectors must have a consistent " 543 "length and width.");
546 TEUCHOS_TEST_FOR_EXCEPTION((
int)newstate.
V.size()==0 || (int)newstate.
Z.size()==0,
547 std::invalid_argument,
548 "Belos::PseudoBlockGmresIter::initialize(): " 549 "V and/or Z was not specified in the input state; " 550 "the V and/or Z arrays have length zero.");
561 RCP<const MV> lhsMV = lp_->getLHS();
562 RCP<const MV> rhsMV = lp_->getRHS();
566 RCP<const MV> vectorInBasisSpace = rhsMV.is_null() ? lhsMV : rhsMV;
569 TEUCHOS_TEST_FOR_EXCEPTION(vectorInBasisSpace.is_null(),
570 std::invalid_argument,
571 "Belos::PseudoBlockGmresIter::initialize(): " 572 "The linear problem to solve does not specify multi" 573 "vectors from which we can clone basis vectors. The " 574 "right-hand side(s), left-hand side(s), or both should " 579 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
curDim > numBlocks_+1,
580 std::invalid_argument,
582 curDim_ = newstate.
curDim;
588 for (
int i=0; i<numRHS_; ++i) {
593 V_[i] =
MVT::Clone (*vectorInBasisSpace, numBlocks_ + 1);
598 std::invalid_argument, errstr );
600 std::invalid_argument, errstr );
606 if (newstate.
V[i] != V_[i]) {
608 if (curDim_ == 0 && lclDim > 1) {
610 <<
"Belos::PseudoBlockGmresIter::initialize(): the solver was " 611 <<
"initialized with a kernel of " << lclDim
613 <<
"The block size however is only " << 1
615 <<
"The last " << lclDim - 1 <<
" vectors will be discarded." 618 std::vector<int> nevind (curDim_ + 1);
619 for (
int j = 0; j < curDim_ + 1; ++j)
624 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
625 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
629 lclV = Teuchos::null;
636 for (
int i=0; i<numRHS_; ++i) {
638 if (Z_[i] == Teuchos::null) {
639 Z_[i] = Teuchos::rcp(
new Teuchos::SerialDenseVector<int,ScalarType>() );
641 if (Z_[i]->length() < numBlocks_+1) {
642 Z_[i]->shapeUninitialized(numBlocks_+1, 1);
646 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
Z[i]->numRows() < curDim_, std::invalid_argument, errstr);
649 if (newstate.
Z[i] != Z_[i]) {
653 Teuchos::SerialDenseVector<int,ScalarType> newZ(Teuchos::View,newstate.
Z[i]->values(),curDim_+1);
654 Teuchos::RCP<Teuchos::SerialDenseVector<int,ScalarType> > lclZ;
655 lclZ = Teuchos::rcp(
new Teuchos::SerialDenseVector<int,ScalarType>(Teuchos::View,Z_[i]->values(),curDim_+1) );
659 lclZ = Teuchos::null;
666 for (
int i=0; i<numRHS_; ++i) {
668 if (H_[i] == Teuchos::null) {
669 H_[i] = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>() );
671 if (H_[i]->numRows() < numBlocks_+1 || H_[i]->numCols() < numBlocks_) {
672 H_[i]->shapeUninitialized(numBlocks_+1, numBlocks_);
676 if ((
int)newstate.
H.size() == numRHS_) {
679 TEUCHOS_TEST_FOR_EXCEPTION((newstate.
H[i]->numRows() < curDim_ || newstate.
H[i]->numCols() < curDim_), std::invalid_argument,
680 "Belos::PseudoBlockGmresIter::initialize(): Specified Hessenberg matrices must have a consistent size to the current subspace dimension");
682 if (newstate.
H[i] != H_[i]) {
685 Teuchos::SerialDenseMatrix<int,ScalarType> newH(Teuchos::View,*newstate.
H[i],curDim_+1, curDim_);
686 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > lclH;
687 lclH = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*H_[i],curDim_+1, curDim_) );
691 lclH = Teuchos::null;
703 if ((
int)newstate.
cs.size() == numRHS_ && (int)newstate.
sn.size() == numRHS_) {
704 for (
int i=0; i<numRHS_; ++i) {
705 if (cs_[i] != newstate.
cs[i])
706 cs_[i] = Teuchos::rcp(
new Teuchos::SerialDenseVector<int,MagnitudeType>(*newstate.
cs[i]) );
707 if (sn_[i] != newstate.
sn[i])
708 sn_[i] = Teuchos::rcp(
new Teuchos::SerialDenseVector<int,ScalarType>(*newstate.
sn[i]) );
713 for (
int i=0; i<numRHS_; ++i) {
714 if (cs_[i] == Teuchos::null)
715 cs_[i] = Teuchos::rcp(
new Teuchos::SerialDenseVector<int,MagnitudeType>(numBlocks_+1) );
717 cs_[i]->resize(numBlocks_+1);
718 if (sn_[i] == Teuchos::null)
719 sn_[i] = Teuchos::rcp(
new Teuchos::SerialDenseVector<int,ScalarType>(numBlocks_+1) );
721 sn_[i]->resize(numBlocks_+1);
743 template <
class ScalarType,
class MV,
class OP>
749 if (initialized_ ==
false) {
753 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
754 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
757 int searchDim = numBlocks_;
762 std::vector<int> index(1);
763 std::vector<int> index2(1);
765 Teuchos::RCP<MV> U_vec =
MVT::Clone( *V_[0], numRHS_ );
768 Teuchos::RCP<MV> AU_vec =
MVT::Clone( *V_[0], numRHS_ );
770 for (
int i=0; i<numRHS_; ++i) {
774 MVT::MvAddMv( one, *tmp_vec, zero, *tmp_vec, *U_vec_view );
782 while (stest_->checkStatus(
this) !=
Passed && curDim_ < searchDim) {
788 lp_->apply( *U_vec, *AU_vec );
793 int num_prev = curDim_+1;
794 index.resize( num_prev );
795 for (
int i=0; i<num_prev; ++i) {
801 for (
int i=0; i<numRHS_; ++i) {
806 Teuchos::Array< Teuchos::RCP<const MV> > V_array( 1, V_prev );
815 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > h_new
816 = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
817 ( Teuchos::View, *H_[i], num_prev, 1, 0, curDim_ ) );
818 Teuchos::Array< Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > h_array( 1, h_new );
820 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > r_new
821 = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
822 ( Teuchos::View, *H_[i], 1, 1, num_prev, curDim_ ) );
827 ortho_->projectAndNormalize( *V_new, h_array, r_new, V_array );
832 index2[0] = curDim_+1;
840 Teuchos::RCP<MV> tmp_AU_vec = U_vec;
876 template<
class ScalarType,
class MV,
class OP>
882 int curDim = curDim_;
888 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
890 Teuchos::BLAS<int, ScalarType> blas;
892 for (i=0; i<numRHS_; ++i) {
898 for (j=0; j<curDim; j++) {
902 blas.ROT( 1, &(*H_[i])(j,curDim), 1, &(*H_[i])(j+1, curDim), 1, &(*cs_[i])[j], &(*sn_[i])[j] );
907 blas.ROTG( &(*H_[i])(curDim,curDim), &(*H_[i])(curDim+1,curDim), &(*cs_[i])[curDim], &(*sn_[i])[curDim] );
908 (*H_[i])(curDim+1,curDim) = zero;
912 blas.ROT( 1, &(*Z_[i])(curDim), 1, &(*Z_[i])(curDim+1), 1, &(*cs_[i])[curDim], &(*sn_[i])[curDim] );
std::vector< Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > > H
The current Hessenberg matrix.
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem...
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.
PseudoBlockGmresIterState< ScalarType, MV > getState() const
Get the current state of the linear solver.
std::vector< Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > > R
The current upper-triangular matrix from the QR reduction of H.
Teuchos::ScalarTraits< ScalarType > SCT
Pure virtual base class for defining the status testing capabilities of Belos.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
Declaration of basic traits for the multivector type.
int getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
virtual ~PseudoBlockGmresIter()
Destructor.
std::vector< Teuchos::RCP< const MV > > V
The current Krylov basis.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the "native" residual vectors.
An extension of the MultiVecTraits class that adds a new vector length method.
Pure virtual base class which describes the basic interface to the linear solver iteration.
A pure virtual class for defining the status tests for the Belos iterative solvers.
Class which defines basic traits for the operator type.
int getNumIters() const
Get the current iteration count.
Structure to contain pointers to PseudoBlockGmresIter state variables.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
bool isInitialized()
States whether the solver has been initialized or not.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
Traits class which defines basic operations on multivectors.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
void updateLSQR(int dim=-1)
Method for updating QR factorization of upper Hessenberg matrix.
PseudoBlockGmresIter(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)
PseudoBlockGmresIter constructor with linear problem, solver utilities, and parameter list of solver ...
PseudoBlockGmresIterState()
PseudoBlockGmresIterOrthoFailure(const std::string &what_arg)
std::vector< Teuchos::RCP< const Teuchos::SerialDenseVector< int, MagnitudeType > > > cs
MultiVecTraitsExt< ScalarType, MV > MVText
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
PseudoBlockGmresIterInitFailure(const std::string &what_arg)
SCT::magnitudeType MagnitudeType
Teuchos::ScalarTraits< ScalarType > SCT
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
void resetNumIters(int iter=0)
Reset the iteration count.
MultiVecTraits< ScalarType, MV > MVT
std::vector< Teuchos::RCP< const Teuchos::SerialDenseVector< int, ScalarType > > > Z
The current right-hand side of the least squares system RY = Z.
OperatorTraits< ScalarType, MV, OP > OPT
void iterate()
This method performs block Gmres iterations until the status test indicates the need to stop or an er...
static ptrdiff_t GetGlobalLength(const MV &mv)
Obtain the vector length of mv.
Class which defines basic traits for the operator type.
SCT::magnitudeType MagnitudeType
int curDim
The current dimension of the reduction.
Parent class to all Belos exceptions.
std::vector< Teuchos::RCP< const Teuchos::SerialDenseVector< int, ScalarType > > > sn
The current Given's rotation coefficients.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
Belos header file which uses auto-configuration information to include necessary C++ headers...
This class implements the pseudo-block GMRES iteration, where a block Krylov subspace is constructed ...
void setBlockSize(int blockSize)
Set the blocksize.
PseudoBlockGmresIterOrthoFailure is thrown when the orthogonalization manager is unable to generate o...
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
PseudoBlockGmresIterInitFailure is thrown when the PseudoBlockGmresIter object is unable to generate ...