42 #ifndef BELOS_BLOCK_GMRES_ITER_HPP 43 #define BELOS_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" 82 template<
class ScalarType,
class MV,
class OP>
93 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
112 Teuchos::ParameterList ¶ms );
226 if (!initialized_)
return 0;
260 void setSize(
int blockSize,
int numBlocks);
278 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
279 const Teuchos::RCP<OutputManager<ScalarType> > om_;
280 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
281 const Teuchos::RCP<OrthoManager<ScalarType,MV> > ortho_;
293 Teuchos::SerialDenseVector<int,ScalarType> beta, sn;
294 Teuchos::SerialDenseVector<int,MagnitudeType> cs;
307 bool stateStorageInitialized_;
312 bool keepHessenberg_;
316 bool initHessenberg_;
329 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > H_;
334 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > R_;
335 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > z_;
340 template<
class ScalarType,
class MV,
class OP>
345 Teuchos::ParameterList ¶ms ):
353 stateStorageInitialized_(false),
354 keepHessenberg_(false),
355 initHessenberg_(false),
360 keepHessenberg_ = params.get(
"Keep Hessenberg",
false);
363 initHessenberg_ = params.get(
"Initialize Hessenberg",
false);
366 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Num Blocks"), std::invalid_argument,
367 "Belos::BlockGmresIter::constructor: mandatory parameter 'Num Blocks' is not specified.");
368 int nb = Teuchos::getParameter<int>(params,
"Num Blocks");
371 int bs = params.get(
"Block Size", 1);
377 template <
class ScalarType,
class MV,
class OP>
383 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0 || blockSize <= 0, std::invalid_argument,
"Belos::BlockGmresIter::setSize was passed a non-positive argument.");
384 if (blockSize == blockSize_ && numBlocks == numBlocks_) {
389 if (blockSize!=blockSize_ || numBlocks!=numBlocks_)
390 stateStorageInitialized_ =
false;
392 blockSize_ = blockSize;
393 numBlocks_ = numBlocks;
395 initialized_ =
false;
405 template <
class ScalarType,
class MV,
class OP>
408 if (!stateStorageInitialized_) {
411 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
412 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
413 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
414 stateStorageInitialized_ =
false;
422 int newsd = blockSize_*(numBlocks_+1);
429 beta.resize( newsd );
433 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_*static_cast<ptrdiff_t>(numBlocks_) >
MVText::GetGlobalLength(*rhsMV),std::invalid_argument,
434 "Belos::BlockGmresIter::setStateSize(): Cannot generate a Krylov basis with dimension larger the operator!");
437 if (V_ == Teuchos::null) {
439 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
440 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
441 "Belos::BlockGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
447 Teuchos::RCP<const MV> tmp = V_;
453 if (R_ == Teuchos::null) {
454 R_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>() );
456 if (initHessenberg_) {
457 R_->shape( newsd, newsd-blockSize_ );
460 if (R_->numRows() < newsd || R_->numCols() < newsd-blockSize_) {
461 R_->shapeUninitialized( newsd, newsd-blockSize_ );
466 if (keepHessenberg_) {
467 if (H_ == Teuchos::null) {
468 H_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>() );
470 if (initHessenberg_) {
471 H_->shape( newsd, newsd-blockSize_ );
474 if (H_->numRows() < newsd || H_->numCols() < newsd-blockSize_) {
475 H_->shapeUninitialized( newsd, newsd-blockSize_ );
485 if (z_ == Teuchos::null) {
486 z_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>() );
488 if (z_-> numRows() < newsd || z_->numCols() < blockSize_) {
489 z_->shapeUninitialized( newsd, blockSize_ );
493 stateStorageInitialized_ =
true;
500 template <
class ScalarType,
class MV,
class OP>
507 Teuchos::RCP<MV> currentUpdate = Teuchos::null;
509 return currentUpdate;
511 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
512 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
513 Teuchos::BLAS<int,ScalarType> blas;
514 currentUpdate =
MVT::Clone( *V_, blockSize_ );
518 Teuchos::SerialDenseMatrix<int,ScalarType> y( Teuchos::Copy, *z_, curDim_, blockSize_ );
522 blas.TRSM( Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
523 Teuchos::NON_UNIT_DIAG, curDim_, blockSize_, one,
524 R_->values(), R_->stride(), y.values(), y.stride() );
528 std::vector<int> index(curDim_);
529 for (
int i=0; i<curDim_; i++ ) {
535 return currentUpdate;
542 template <
class ScalarType,
class MV,
class OP>
548 if ( norms && (
int)norms->size() < blockSize_ )
549 norms->resize( blockSize_ );
552 Teuchos::BLAS<int,ScalarType> blas;
553 for (
int j=0; j<blockSize_; j++) {
554 (*norms)[j] = blas.NRM2( blockSize_, &(*z_)(curDim_, j), 1);
557 return Teuchos::null;
564 template <
class ScalarType,
class MV,
class OP>
568 if (!stateStorageInitialized_)
571 TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
572 "Belos::BlockGmresIter::initialize(): Cannot initialize state storage!");
578 std::string errstr(
"Belos::BlockGmresIter::initialize(): Specified multivectors must have a consistent length and width.");
580 if (newstate.
V != Teuchos::null && newstate.
z != Teuchos::null) {
585 std::invalid_argument, errstr );
587 std::invalid_argument, errstr );
588 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
curDim > blockSize_*(numBlocks_+1),
589 std::invalid_argument, errstr );
591 curDim_ = newstate.
curDim;
595 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
z->numRows() < curDim_ || newstate.
z->numCols() < blockSize_, std::invalid_argument, errstr);
599 if (newstate.
V != V_) {
601 if (curDim_ == 0 && lclDim > blockSize_) {
602 om_->stream(
Warnings) <<
"Belos::BlockGmresIter::initialize(): the solver was initialized with a kernel of " << lclDim << std::endl
603 <<
"The block size however is only " << blockSize_ << std::endl
604 <<
"The last " << lclDim - blockSize_ <<
" vectors will be discarded." << std::endl;
606 std::vector<int> nevind(curDim_+blockSize_);
607 for (
int i=0; i<curDim_+blockSize_; i++) nevind[i] = i;
613 lclV = Teuchos::null;
617 if (newstate.
z != z_) {
619 Teuchos::SerialDenseMatrix<int,ScalarType> newZ(Teuchos::View,*newstate.
z,curDim_+blockSize_,blockSize_);
620 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > lclZ;
621 lclZ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*z_,curDim_+blockSize_,blockSize_) );
625 lclZ = Teuchos::null;
631 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
V == Teuchos::null,std::invalid_argument,
632 "Belos::BlockGmresIter::initialize(): BlockGmresStateIterState does not have initial kernel V_0.");
634 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
z == Teuchos::null,std::invalid_argument,
635 "Belos::BlockGmresIter::initialize(): BlockGmresStateIterState does not have initial norms z_0.");
657 template <
class ScalarType,
class MV,
class OP>
663 if (initialized_ ==
false) {
668 int searchDim = blockSize_*numBlocks_;
675 while (stest_->checkStatus(
this) !=
Passed && curDim_+blockSize_ <= searchDim) {
680 int lclDim = curDim_ + blockSize_;
683 std::vector<int> curind(blockSize_);
684 for (
int i=0; i<blockSize_; i++) { curind[i] = lclDim + i; }
689 for (
int i=0; i<blockSize_; i++) { curind[i] = curDim_ + i; }
693 lp_->apply(*Vprev,*Vnext);
694 Vprev = Teuchos::null;
698 std::vector<int> prevind(lclDim);
699 for (
int i=0; i<lclDim; i++) { prevind[i] = i; }
701 Teuchos::Array<Teuchos::RCP<const MV> > AVprev(1, Vprev);
704 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
705 subH = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
706 ( Teuchos::View,*H_,lclDim,blockSize_,0,curDim_ ) );
707 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > AsubH;
708 AsubH.append( subH );
711 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
712 subH2 = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
713 ( Teuchos::View,*H_,blockSize_,blockSize_,lclDim,curDim_ ) );
715 int rank = ortho_->projectAndNormalize(*Vnext,AsubH,subH2,AVprev);
719 if (keepHessenberg_) {
721 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
722 subR = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
723 ( Teuchos::View,*R_,lclDim,blockSize_,0,curDim_ ) );
727 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
728 subR2 = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
729 ( Teuchos::View,*R_,blockSize_,blockSize_,lclDim,curDim_ ) );
730 subR2->assign(*subH2);
734 "Belos::BlockGmresIter::iterate(): couldn't generate basis of full rank.");
744 Vnext = Teuchos::null;
745 curDim_ += blockSize_;
768 template<
class ScalarType,
class MV,
class OP>
772 ScalarType sigma, mu, vscale, maxelem;
773 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
778 int curDim = curDim_;
783 Teuchos::BLAS<int, ScalarType> blas;
788 if (blockSize_ == 1) {
792 for (i=0; i<curDim; i++) {
796 blas.ROT( 1, &(*R_)(i,curDim), 1, &(*R_)(i+1, curDim), 1, &cs[i], &sn[i] );
801 blas.ROTG( &(*R_)(curDim,curDim), &(*R_)(curDim+1,curDim), &cs[curDim], &sn[curDim] );
802 (*R_)(curDim+1,curDim) = zero;
806 blas.ROT( 1, &(*z_)(curDim,0), 1, &(*z_)(curDim+1,0), 1, &cs[curDim], &sn[curDim] );
812 for (j=0; j<blockSize_; j++) {
816 for (i=0; i<curDim+j; i++) {
817 sigma = blas.DOT( blockSize_, &(*R_)(i+1,i), 1, &(*R_)(i+1,curDim+j), 1);
818 sigma += (*R_)(i,curDim+j);
820 blas.AXPY(blockSize_, -sigma, &(*R_)(i+1,i), 1, &(*R_)(i+1,curDim+j), 1);
821 (*R_)(i,curDim+j) -= sigma;
826 maxidx = blas.IAMAX( blockSize_+1, &(*R_)(curDim+j,curDim+j), 1 );
827 maxelem = (*R_)(curDim+j+maxidx-1,curDim+j);
828 for (i=0; i<blockSize_+1; i++)
829 (*R_)(curDim+j+i,curDim+j) /= maxelem;
830 sigma = blas.DOT( blockSize_, &(*R_)(curDim+j+1,curDim+j), 1,
831 &(*R_)(curDim+j+1,curDim+j), 1 );
833 beta[curDim + j] = zero;
835 mu = Teuchos::ScalarTraits<ScalarType>::squareroot((*R_)(curDim+j,curDim+j)*(*R_)(curDim+j,curDim+j)+sigma);
836 if ( Teuchos::ScalarTraits<ScalarType>::real((*R_)(curDim+j,curDim+j))
837 < Teuchos::ScalarTraits<MagnitudeType>::zero() ) {
838 vscale = (*R_)(curDim+j,curDim+j) - mu;
840 vscale = -sigma / ((*R_)(curDim+j,curDim+j) + mu);
842 beta[curDim+j] = 2.0*vscale*vscale/(sigma + vscale*vscale);
843 (*R_)(curDim+j,curDim+j) = maxelem*mu;
844 for (i=0; i<blockSize_; i++)
845 (*R_)(curDim+j+1+i,curDim+j) /= vscale;
850 for (i=0; i<blockSize_; i++) {
851 sigma = blas.DOT( blockSize_, &(*R_)(curDim+j+1,curDim+j),
852 1, &(*z_)(curDim+j+1,i), 1);
853 sigma += (*z_)(curDim+j,i);
854 sigma *= beta[curDim+j];
855 blas.AXPY(blockSize_, -sigma, &(*R_)(curDim+j+1,curDim+j),
856 1, &(*z_)(curDim+j+1,i), 1);
857 (*z_)(curDim+j,i) -= sigma;
864 curDim_ = dim + blockSize_;
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.
MultiVecTraitsExt< ScalarType, MV > MVText
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
This class implements the block GMRES iteration, where a block Krylov subspace is constructed...
Teuchos::RCP< const MV > V
The current Krylov basis.
MultiVecTraits< ScalarType, MV > MVT
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).
void updateLSQR(int dim=-1)
Method for updating QR factorization of upper Hessenberg matrix.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
Declaration of basic traits for the multivector type.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > z
The current right-hand side of the least squares system RY = Z.
void iterate()
This method performs block Gmres iterations until the status test indicates the need to stop or an er...
Structure to contain pointers to GmresIteration state variables.
An extension of the MultiVecTraits class that adds a new vector length method.
void resetNumIters(int iter=0)
Reset the iteration count.
SCT::magnitudeType MagnitudeType
A pure virtual class for defining the status tests for the Belos iterative solvers.
Class which defines basic traits for the operator type.
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 setSize(int blockSize, int numBlocks)
Set the blocksize and number of blocks to be used by the iterative solver in solving this linear prob...
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
virtual ~BlockGmresIter()
Destructor.
Traits class which defines basic operations on multivectors.
bool isInitialized()
States whether the solver has been initialized or not.
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.
int curDim
The current dimension of the reduction.
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > R
The current upper-triangular matrix from the QR reduction of H.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
int getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
Teuchos::ScalarTraits< ScalarType > SCT
GmresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
void initializeGmres(GmresIterationState< ScalarType, MV > newstate)
Initialize the solver to an iterate, providing a complete state.
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
static ptrdiff_t GetGlobalLength(const MV &mv)
Obtain the vector length of mv.
Class which defines basic traits for the operator type.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
BlockGmresIter(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)
BlockGmresIter constructor with linear problem, solver utilities, and parameter list of solver option...
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
Pure virtual base class which augments the basic interface for a Gmres linear solver iteration...
GmresIterationOrthoFailure is thrown when the GmresIteration object is unable to compute independent ...
Belos header file which uses auto-configuration information to include necessary C++ headers...
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem...
void setBlockSize(int blockSize)
Set the blocksize.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
int getNumIters() const
Get the current iteration count.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > H
The current Hessenberg matrix.