42 #ifndef BELOS_BLOCK_FGMRES_ITER_HPP 43 #define BELOS_BLOCK_FGMRES_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 );
227 if (!initialized_)
return 0;
261 void setSize(
int blockSize,
int numBlocks);
279 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
280 const Teuchos::RCP<OutputManager<ScalarType> > om_;
281 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
282 const Teuchos::RCP<OrthoManager<ScalarType,MV> > ortho_;
294 Teuchos::SerialDenseVector<int,ScalarType> beta, sn;
295 Teuchos::SerialDenseVector<int,MagnitudeType> cs;
308 bool stateStorageInitialized_;
322 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > H_;
327 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > R_;
328 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > z_;
333 template<
class ScalarType,
class MV,
class OP>
338 Teuchos::ParameterList ¶ms ):
346 stateStorageInitialized_(false),
351 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Num Blocks"), std::invalid_argument,
352 "Belos::BlockFGmresIter::constructor: mandatory parameter 'Num Blocks' is not specified.");
353 int nb = Teuchos::getParameter<int>(params,
"Num Blocks");
356 int bs = params.get(
"Block Size", 1);
362 template <
class ScalarType,
class MV,
class OP>
368 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0 || blockSize <= 0, std::invalid_argument,
"Belos::BlockFGmresIter::setSize was passed a non-positive argument.");
369 if (blockSize == blockSize_ && numBlocks == numBlocks_) {
374 if (blockSize!=blockSize_ || numBlocks!=numBlocks_)
375 stateStorageInitialized_ =
false;
377 blockSize_ = blockSize;
378 numBlocks_ = numBlocks;
380 initialized_ =
false;
390 template <
class ScalarType,
class MV,
class OP>
393 if (!stateStorageInitialized_) {
396 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
397 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
398 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
399 stateStorageInitialized_ =
false;
407 int newsd = blockSize_*(numBlocks_+1);
414 beta.resize( newsd );
418 TEUCHOS_TEST_FOR_EXCEPTION( blockSize_*static_cast<ptrdiff_t>(numBlocks_) >
MVText::GetGlobalLength(*rhsMV),std::invalid_argument,
419 "Belos::BlockFGmresIter::setStateSize(): Cannot generate a Krylov basis with dimension larger the operator!");
422 if (V_ == Teuchos::null) {
424 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
425 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
426 "Belos::BlockFGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
432 Teuchos::RCP<const MV> tmp = V_;
437 if (Z_ == 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::BlockFGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
447 Teuchos::RCP<const MV> tmp = Z_;
453 if (H_ == Teuchos::null)
454 H_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>( newsd, newsd-blockSize_ ) );
456 H_->shapeUninitialized( newsd, newsd-blockSize_ );
461 if (z_ == Teuchos::null)
462 z_ = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(newsd,blockSize_) );
464 z_->shapeUninitialized( newsd, blockSize_ );
467 stateStorageInitialized_ =
true;
474 template <
class ScalarType,
class MV,
class OP>
481 Teuchos::RCP<MV> currentUpdate = Teuchos::null;
483 return currentUpdate;
485 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
486 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
487 Teuchos::BLAS<int,ScalarType> blas;
488 currentUpdate =
MVT::Clone( *Z_, blockSize_ );
492 Teuchos::SerialDenseMatrix<int,ScalarType> y( Teuchos::Copy, *z_, curDim_, blockSize_ );
496 blas.TRSM( Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
497 Teuchos::NON_UNIT_DIAG, curDim_, blockSize_, one,
498 H_->values(), H_->stride(), y.values(), y.stride() );
502 std::vector<int> index(curDim_);
503 for (
int i=0; i<curDim_; i++ ) {
509 return currentUpdate;
516 template <
class ScalarType,
class MV,
class OP>
522 if ( norms && (
int)norms->size() < blockSize_ )
523 norms->resize( blockSize_ );
526 Teuchos::BLAS<int,ScalarType> blas;
527 for (
int j=0; j<blockSize_; j++) {
528 (*norms)[j] = blas.NRM2( blockSize_, &(*z_)(curDim_, j), 1);
531 return Teuchos::null;
538 template <
class ScalarType,
class MV,
class OP>
542 if (!stateStorageInitialized_)
545 TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
546 "Belos::BlockFGmresIter::initialize(): Cannot initialize state storage!");
552 std::string errstr(
"Belos::BlockFGmresIter::initialize(): Specified multivectors must have a consistent length and width.");
554 if (newstate.
V != Teuchos::null && newstate.
z != Teuchos::null) {
559 std::invalid_argument, errstr );
561 std::invalid_argument, errstr );
562 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
curDim > blockSize_*(numBlocks_+1),
563 std::invalid_argument, errstr );
565 curDim_ = newstate.
curDim;
569 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
z->numRows() < curDim_ || newstate.
z->numCols() < blockSize_, std::invalid_argument, errstr);
573 if (newstate.
V != V_) {
575 if (curDim_ == 0 && lclDim > blockSize_) {
576 om_->stream(
Warnings) <<
"Belos::BlockFGmresIter::initialize(): the solver was initialized with a kernel of " << lclDim << std::endl
577 <<
"The block size however is only " << blockSize_ << std::endl
578 <<
"The last " << lclDim - blockSize_ <<
" vectors will be discarded." << std::endl;
580 std::vector<int> nevind(curDim_+blockSize_);
581 for (
int i=0; i<curDim_+blockSize_; i++) nevind[i] = i;
587 lclV = Teuchos::null;
591 if (newstate.
z != z_) {
593 Teuchos::SerialDenseMatrix<int,ScalarType> newZ(Teuchos::View,*newstate.
z,curDim_+blockSize_,blockSize_);
594 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > lclz;
595 lclz = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*z_,curDim_+blockSize_,blockSize_) );
599 lclz = Teuchos::null;
605 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
V == Teuchos::null,std::invalid_argument,
606 "Belos::BlockFGmresIter::initialize(): BlockFGmresStateIterState does not have initial kernel V_0.");
608 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
z == Teuchos::null,std::invalid_argument,
609 "Belos::BlockFGmresIter::initialize(): BlockFGmresStateIterState does not have initial norms z_0.");
631 template <
class ScalarType,
class MV,
class OP>
637 if (initialized_ ==
false) {
642 int searchDim = blockSize_*numBlocks_;
649 while (stest_->checkStatus(
this) !=
Passed && curDim_+blockSize_ <= searchDim) {
654 int lclDim = curDim_ + blockSize_;
657 std::vector<int> curind(blockSize_);
658 for (
int i=0; i<blockSize_; i++) { curind[i] = lclDim + i; }
663 for (
int i=0; i<blockSize_; i++) { curind[i] = curDim_ + i; }
668 lp_->applyRightPrec(*Vprev,*Znext);
669 Vprev = Teuchos::null;
672 lp_->applyOp(*Znext,*Vnext);
673 Znext = Teuchos::null;
677 std::vector<int> prevind(lclDim);
678 for (
int i=0; i<lclDim; i++) { prevind[i] = i; }
680 Teuchos::Array<Teuchos::RCP<const MV> > AVprev(1, Vprev);
683 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
684 subH = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
685 ( Teuchos::View,*H_,lclDim,blockSize_,0,curDim_ ) );
686 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > AsubH;
687 AsubH.append( subH );
690 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
691 subR = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>
692 ( Teuchos::View,*H_,blockSize_,blockSize_,lclDim,curDim_ ) );
693 int rank = ortho_->projectAndNormalize(*Vnext,AsubH,subR,AVprev);
695 "Belos::BlockFGmresIter::iterate(): couldn't generate basis of full rank.");
705 Vnext = Teuchos::null;
706 curDim_ += blockSize_;
729 template<
class ScalarType,
class MV,
class OP>
733 ScalarType sigma, mu, vscale, maxelem;
734 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
739 int curDim = curDim_;
744 Teuchos::BLAS<int, ScalarType> blas;
749 if (blockSize_ == 1) {
753 for (i=0; i<curDim; i++) {
757 blas.ROT( 1, &(*H_)(i,curDim), 1, &(*H_)(i+1, curDim), 1, &cs[i], &sn[i] );
762 blas.ROTG( &(*H_)(curDim,curDim), &(*H_)(curDim+1,curDim), &cs[curDim], &sn[curDim] );
763 (*H_)(curDim+1,curDim) = zero;
767 blas.ROT( 1, &(*z_)(curDim,0), 1, &(*z_)(curDim+1,0), 1, &cs[curDim], &sn[curDim] );
773 for (j=0; j<blockSize_; j++) {
777 for (i=0; i<curDim+j; i++) {
778 sigma = blas.DOT( blockSize_, &(*H_)(i+1,i), 1, &(*H_)(i+1,curDim+j), 1);
779 sigma += (*H_)(i,curDim+j);
781 blas.AXPY(blockSize_, -sigma, &(*H_)(i+1,i), 1, &(*H_)(i+1,curDim+j), 1);
782 (*H_)(i,curDim+j) -= sigma;
787 maxidx = blas.IAMAX( blockSize_+1, &(*H_)(curDim+j,curDim+j), 1 );
788 maxelem = (*H_)(curDim+j+maxidx-1,curDim+j);
789 for (i=0; i<blockSize_+1; i++)
790 (*H_)(curDim+j+i,curDim+j) /= maxelem;
791 sigma = blas.DOT( blockSize_, &(*H_)(curDim+j+1,curDim+j), 1,
792 &(*H_)(curDim+j+1,curDim+j), 1 );
794 beta[curDim + j] = zero;
796 mu = Teuchos::ScalarTraits<ScalarType>::squareroot((*H_)(curDim+j,curDim+j)*(*H_)(curDim+j,curDim+j)+sigma);
797 if ( Teuchos::ScalarTraits<ScalarType>::real((*H_)(curDim+j,curDim+j))
798 < Teuchos::ScalarTraits<MagnitudeType>::zero() ) {
799 vscale = (*H_)(curDim+j,curDim+j) - mu;
801 vscale = -sigma / ((*H_)(curDim+j,curDim+j) + mu);
803 beta[curDim+j] = 2.0*vscale*vscale/(sigma + vscale*vscale);
804 (*H_)(curDim+j,curDim+j) = maxelem*mu;
805 for (i=0; i<blockSize_; i++)
806 (*H_)(curDim+j+1+i,curDim+j) /= vscale;
811 for (i=0; i<blockSize_; i++) {
812 sigma = blas.DOT( blockSize_, &(*H_)(curDim+j+1,curDim+j),
813 1, &(*z_)(curDim+j+1,i), 1);
814 sigma += (*z_)(curDim+j,i);
815 sigma *= beta[curDim+j];
816 blas.AXPY(blockSize_, -sigma, &(*H_)(curDim+j+1,curDim+j),
817 1, &(*z_)(curDim+j+1,i), 1);
818 (*z_)(curDim+j,i) -= sigma;
825 curDim_ = dim + blockSize_;
Collection of types and exceptions used within the Belos solvers.
void iterate()
This method performs block FGmres iterations until the status test indicates the need to stop or an e...
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
void setBlockSize(int blockSize)
Set the blocksize.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
Class which manages the output and verbosity of the Belos solvers.
BlockFGmresIter(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)
BlockFGmresIter constructor with linear problem, solver utilities, and parameter list of solver optio...
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< const MV > V
The current Krylov basis.
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).
int getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
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.
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem...
Structure to contain pointers to GmresIteration state variables.
An extension of the MultiVecTraits class that adds a new vector length method.
A pure virtual class for defining the status tests for the Belos iterative solvers.
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...
void resetNumIters(int iter=0)
Reset the iteration count.
Class which defines basic traits for the operator type.
int getNumIters() const
Get the current iteration count.
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.
GmresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
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.
Teuchos::RCP< const MV > Z
The current preconditioned Krylov basis (only used in flexible GMRES).
int curDim
The current dimension of the reduction.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
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.
This class implements the block flexible GMRES iteration, where a block Krylov subspace is constructe...
virtual ~BlockFGmresIter()
Destructor.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
bool isInitialized()
States whether the solver has been initialized or not.
MultiVecTraits< ScalarType, MV > MVT
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
static ptrdiff_t GetGlobalLength(const MV &mv)
Obtain the vector length of mv.
void initializeGmres(GmresIterationState< ScalarType, MV > newstate)
Initialize the solver to an iterate, providing a complete state.
Class which defines basic traits for the operator type.
SCT::magnitudeType MagnitudeType
void updateLSQR(int dim=-1)
Method for updating QR factorization of upper Hessenberg matrix.
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
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...
MultiVecTraitsExt< ScalarType, MV > MVText
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > H
The current Hessenberg matrix.