42 #ifndef BELOS_BLOCK_FGMRES_ITER_HPP
43 #define BELOS_BLOCK_FGMRES_ITER_HPP
82 template<
class ScalarType,
class MV,
class OP>
227 if (!initialized_)
return 0;
261 void setSize(
int blockSize,
int numBlocks);
308 bool stateStorageInitialized_;
333 template<
class ScalarType,
class MV,
class OP>
346 stateStorageInitialized_(false),
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_) {
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) {
426 "Belos::BlockFGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
427 V_ = MVT::Clone( *tmp, newsd );
431 if (MVT::GetNumberVecs(*V_) < newsd) {
433 V_ = MVT::Clone( *tmp, newsd );
437 if (Z_ == Teuchos::null) {
441 "Belos::BlockFGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
442 Z_ = MVT::Clone( *tmp, newsd );
446 if (MVT::GetNumberVecs(*Z_) < newsd) {
448 Z_ = MVT::Clone( *tmp, newsd );
453 if (H_ == Teuchos::null)
456 H_->shapeUninitialized( newsd, newsd-blockSize_ );
461 if (z_ == Teuchos::null)
464 z_->shapeUninitialized( newsd, blockSize_ );
467 stateStorageInitialized_ =
true;
474 template <
class ScalarType,
class MV,
class OP>
483 return currentUpdate;
488 currentUpdate = MVT::Clone( *Z_, blockSize_ );
502 std::vector<int> index(curDim_);
503 for (
int i=0; i<curDim_; i++ ) {
507 MVT::MvTimesMatAddMv( one, *Zjp1, y, zero, *currentUpdate );
509 return currentUpdate;
516 template <
class ScalarType,
class MV,
class OP>
522 if ( norms && (
int)norms->size() < blockSize_ )
523 norms->resize( blockSize_ );
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_)
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 );
563 std::invalid_argument, errstr );
565 curDim_ = newstate.
curDim;
566 int lclDim = MVT::GetNumberVecs(*newstate.
V);
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;
584 MVT::MvAddMv( 1.0, *newV, 0.0, *newV, *lclV );
587 lclV = Teuchos::null;
591 if (newstate.
z != z_) {
599 lclz = Teuchos::null;
606 "Belos::BlockFGmresIter::initialize(): BlockFGmresStateIterState does not have initial kernel V_0.");
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; }
679 Vprev = MVT::CloneView(*V_,prevind);
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;
739 int curDim = curDim_;
740 if (dim >= curDim_ && dim < getMaxSubspaceDim()) {
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;
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;
824 if (dim >= curDim_ && dim < getMaxSubspaceDim()) {
825 curDim_ = dim + blockSize_;
ScalarType * values() const
Collection of types and exceptions used within the Belos solvers.
OrdinalType IAMAX(const OrdinalType n, const ScalarType *x, const OrdinalType incx) const
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.
Array< T > & append(const T &x)
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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.
Traits class which defines basic operations on multivectors.
bool isParameter(const std::string &name) const
void ROTG(ScalarType *da, ScalarType *db, MagnitudeType *c, ScalarType *s) const
GmresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
void AXPY(const OrdinalType n, const alpha_type alpha, const x_type *x, const OrdinalType incx, ScalarType *y, const OrdinalType incy) const
void ROT(const OrdinalType n, ScalarType *dx, const OrdinalType incx, ScalarType *dy, const OrdinalType incy, MagnitudeType *c, ScalarType *s) const
bool isInitialized()
States whether the solver has been initialized or not.
MultiVecTraits< ScalarType, MV > MVT
OrdinalType numCols() const
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
void initializeGmres(GmresIterationState< ScalarType, MV > newstate)
Initialize the solver to an iterate, providing a complete state.
void TRSM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const OrdinalType m, const OrdinalType n, const alpha_type alpha, const A_type *A, const OrdinalType lda, ScalarType *B, const OrdinalType ldb) const
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
ScalarType DOT(const OrdinalType n, const x_type *x, const OrdinalType incx, const y_type *y, const OrdinalType incy) const
OrdinalType stride() const
OrdinalType numRows() const
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 ...
ScalarTraits< ScalarType >::magnitudeType NRM2(const OrdinalType n, const ScalarType *x, const OrdinalType incx) const
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > H
The current Hessenberg matrix.