10#ifndef BELOS_PSEUDO_BLOCK_GMRES_ITER_HPP
11#define BELOS_PSEUDO_BLOCK_GMRES_ITER_HPP
30#include "Teuchos_BLAS.hpp"
31#include "Teuchos_ScalarTraits.hpp"
32#include "Teuchos_ParameterList.hpp"
33#include "Teuchos_TimeMonitor.hpp"
50 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
61 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
79 Teuchos::ParameterList ¶ms );
154 state.
V.resize(numRHS_);
155 state.
H.resize(numRHS_);
156 state.
Z.resize(numRHS_);
157 state.
sn.resize(numRHS_);
158 state.
cs.resize(numRHS_);
159 for (
int i=0; i<numRHS_; ++i) {
163 state.
sn[i] = sn_[i];
164 state.
cs[i] = cs_[i];
216 if (!initialized_)
return 0;
237 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
238 "Belos::PseudoBlockGmresIter::setBlockSize(): Cannot use a block size that is not one.");
257 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
258 const Teuchos::RCP<OutputManager<ScalarType> > om_;
259 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
260 const Teuchos::RCP<OrthoManager<ScalarType,MV,DM> > ortho_;
271 std::vector<Teuchos::RCP<std::vector<ScalarType> > > sn_;
272 std::vector<Teuchos::RCP<std::vector<MagnitudeType> > > cs_;
275 Teuchos::RCP<MV> U_vec_, AU_vec_;
278 Teuchos::RCP<MV> cur_block_rhs_, cur_block_sol_;
294 std::vector<Teuchos::RCP<MV> > V_;
299 std::vector<Teuchos::RCP<DM> > H_;
304 std::vector<Teuchos::RCP<DM> > R_;
305 std::vector<Teuchos::RCP<DM> > Z_;
310 template<
class ScalarType,
class MV,
class OP,
class DM>
315 Teuchos::ParameterList ¶ms ):
327 TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter(
"Num Blocks"), std::invalid_argument,
328 "Belos::PseudoBlockGmresIter::constructor: mandatory parameter 'Num Blocks' is not specified.");
329 int nb = Teuchos::getParameter<int>(params,
"Num Blocks");
336 template <
class ScalarType,
class MV,
class OP,
class DM>
342 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0, std::invalid_argument,
"Belos::PseudoBlockGmresIter::setNumBlocks was passed a non-positive argument.");
344 numBlocks_ = numBlocks;
347 initialized_ =
false;
352 template <
class ScalarType,
class MV,
class OP,
class DM>
359 Teuchos::RCP<MV> currentUpdate = Teuchos::null;
361 return currentUpdate;
363 currentUpdate =
MVT::Clone(*(V_[0]), numRHS_);
364 std::vector<int> index(1), index2(curDim_);
365 for (
int i=0; i<curDim_; ++i) {
368 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
369 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
370 Teuchos::BLAS<int,ScalarType> blas;
372 for (
int i=0; i<numRHS_; ++i) {
384 blas.TRSM( Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
385 Teuchos::NON_UNIT_DIAG, curDim_, 1, one,
395 return currentUpdate;
402 template <
class ScalarType,
class MV,
class OP,
class DM>
403 Teuchos::RCP<const MV>
411 if (
static_cast<int> (norms->size()) < numRHS_)
412 norms->resize (numRHS_);
414 for (
int j = 0; j < numRHS_; ++j)
418 (*norms)[j] = SCT::magnitude (curNativeResid);
421 return Teuchos::null;
425 template <
class ScalarType,
class MV,
class OP,
class DM>
440 std::string errstr (
"Belos::PseudoBlockGmresIter::initialize(): "
441 "Specified multivectors must have a consistent "
442 "length and width.");
445 TEUCHOS_TEST_FOR_EXCEPTION((
int)newstate.
V.size()==0 || (
int)newstate.
Z.size()==0,
446 std::invalid_argument,
447 "Belos::PseudoBlockGmresIter::initialize(): "
448 "V and/or Z was not specified in the input state; "
449 "the V and/or Z arrays have length zero.");
460 RCP<const MV> lhsMV = lp_->getLHS();
461 RCP<const MV> rhsMV = lp_->getRHS();
465 RCP<const MV> vectorInBasisSpace = rhsMV.is_null() ? lhsMV : rhsMV;
468 TEUCHOS_TEST_FOR_EXCEPTION(vectorInBasisSpace.is_null(),
469 std::invalid_argument,
470 "Belos::PseudoBlockGmresIter::initialize(): "
471 "The linear problem to solve does not specify multi"
472 "vectors from which we can clone basis vectors. The "
473 "right-hand side(s), left-hand side(s), or both should "
478 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
curDim > numBlocks_+1,
479 std::invalid_argument,
481 curDim_ = newstate.
curDim;
487 for (
int i=0; i<numRHS_; ++i) {
492 V_[i] =
MVT::Clone (*vectorInBasisSpace, numBlocks_ + 1);
497 std::invalid_argument, errstr );
499 std::invalid_argument, errstr );
505 if (newstate.
V[i] != V_[i]) {
507 if (curDim_ == 0 && lclDim > 1) {
509 <<
"Belos::PseudoBlockGmresIter::initialize(): the solver was "
510 <<
"initialized with a kernel of " << lclDim
512 <<
"The block size however is only " << 1
514 <<
"The last " << lclDim - 1 <<
" vectors will be discarded."
517 std::vector<int> nevind (curDim_ + 1);
518 for (
int j = 0; j < curDim_ + 1; ++j)
526 lclV = Teuchos::null;
533 for (
int i=0; i<numRHS_; ++i) {
535 if (Z_[i] == Teuchos::null) {
543 TEUCHOS_TEST_FOR_EXCEPTION(
DMT::GetNumRows(*newstate.
Z[i]) < curDim_, std::invalid_argument, errstr);
546 if (newstate.
Z[i] != Z_[i]) {
551 Teuchos::RCP<DM> lclZ =
DMT::Subview(*Z_[i],curDim_+1,1);
555 lclZ = Teuchos::null;
562 for (
int i=0; i<numRHS_; ++i) {
564 if (H_[i] == Teuchos::null) {
572 if ((
int)newstate.
H.size() == numRHS_) {
576 "Belos::PseudoBlockGmresIter::initialize(): Specified Hessenberg matrices must have a consistent size to the current subspace dimension");
578 if (newstate.
H[i] != H_[i]) {
581 Teuchos::RCP<DM> lclH =
DMT::Subview(*H_[i],curDim_+1, curDim_);
585 lclH = Teuchos::null;
597 if ((
int)newstate.
cs.size() == numRHS_ && (
int)newstate.
sn.size() == numRHS_) {
598 for (
int i=0; i<numRHS_; ++i) {
599 if (cs_[i] != newstate.
cs[i])
600 cs_[i] = Teuchos::rcp(
new std::vector<MagnitudeType>(*newstate.
cs[i]) );
601 if (sn_[i] != newstate.
sn[i])
602 sn_[i] = Teuchos::rcp(
new std::vector<ScalarType>(*newstate.
sn[i]) );
607 for (
int i=0; i<numRHS_; ++i) {
608 if (cs_[i] == Teuchos::null)
609 cs_[i] = Teuchos::rcp(
new std::vector<MagnitudeType>(numBlocks_+1) );
611 cs_[i]->resize(numBlocks_+1);
612 if (sn_[i] == Teuchos::null)
613 sn_[i] = Teuchos::rcp(
new std::vector<ScalarType>(numBlocks_+1) );
615 sn_[i]->resize(numBlocks_+1);
626 template <
class ScalarType,
class MV,
class OP,
class DM>
632 if (initialized_ ==
false) {
637 int searchDim = numBlocks_;
642 std::vector<int> index(1);
643 std::vector<int> index2(1);
645 Teuchos::RCP<MV> U_vec =
MVT::Clone( *V_[0], numRHS_ );
648 Teuchos::RCP<MV> AU_vec =
MVT::Clone( *V_[0], numRHS_ );
650 for (
int i=0; i<numRHS_; ++i) {
662 while (stest_->checkStatus(
this) !=
Passed && curDim_ < searchDim) {
668 lp_->apply( *U_vec, *AU_vec );
673 int num_prev = curDim_+1;
674 index.resize( num_prev );
675 for (
int i=0; i<num_prev; ++i) {
681 for (
int i=0; i<numRHS_; ++i) {
686 Teuchos::Array< Teuchos::RCP<const MV> > V_array( 1, V_prev );
695 Teuchos::RCP<DM> h_new =
DMT::Subview(*H_[i], num_prev, 1, 0, curDim_);
696 Teuchos::Array< Teuchos::RCP<DM> > h_array( 1, h_new );
698 Teuchos::RCP<DM> r_new =
DMT::Subview(*H_[i], 1, 1, num_prev, curDim_);
703 ortho_->projectAndNormalize( *V_new, h_array, r_new, V_array );
708 index2[0] = curDim_+1;
716 Teuchos::RCP<MV> tmp_AU_vec = U_vec;
736 template<
class ScalarType,
class MV,
class OP,
class DM>
742 int curDim = curDim_;
748 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
750 Teuchos::BLAS<int, ScalarType> blas;
752 for (i=0; i<numRHS_; ++i) {
761 for (j=0; j<curDim; j++) {
766 1, &(*cs_[i])[j], &(*sn_[i])[j] );
772 &(*cs_[i])[curDim], &(*sn_[i])[curDim] );
778 1, &(*cs_[i])[curDim], &(*sn_[i])[curDim] );
Belos header file which uses auto-configuration information to include necessary C++ headers.
Pure virtual base class which augments the basic interface for a Gmres linear solver iteration.
Pure virtual base class which describes the basic interface to the linear solver iteration.
Class which describes the linear problem to be solved by the iterative solver.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
Virtual base class which defines basic traits for the multi-vector type.
static Teuchos::RCP< DM > Subview(DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
Marks host data modified to avoid device sync errors.
static Teuchos::RCP< DM > SubviewCopy(const DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
Returns a deep copy of the requested subview.
static void SyncHostToDevice(DM &dm)
static void PutScalar(DM &dm, ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Fill all entries with value. Value is zero if not specified.
static int GetNumRows(const DM &dm)
Obtain the number of rows of dm.
static ScalarType * GetRawHostPtr(DM &dm)
Returns a raw pointer to the (non-const) data on the host.
static const ScalarType & ValueConst(const DM &dm, const int i, const int j)
Access a const reference to the (i,j) entry of dm, e_i^T dm e_j.
static void SyncDeviceToHost(DM &dm)
static void Assign(DM &thisDM, const DM &sourceDM)
Copies entries of sourceDM to thisDM (deep copy).
static Teuchos::RCP< const DM > SubviewConst(const DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
static void Reshape(DM &dm, const int numrows, const int numcols, bool initZero=false)
static ScalarType const * GetConstRawHostPtr(const DM &dm)
Returns a raw pointer to const data on the host.
static ScalarType & Value(DM &dm, const int i, const int j)
Access a reference to the (i,j) entry of dm, e_i^T dm e_j.
static int GetStride(const DM &dm)
Obtain the stride between the columns of dm.
static Teuchos::RCP< DM > Create()
Creates a new empty DM with no dimension.
static int GetNumCols(const DM &dm)
Obtain the number of columns of dm.
Iteration()
Default Constructor.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const DM &B, const ScalarType beta, MV &mv)
static void Assign(const MV &A, MV &mv)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
static ptrdiff_t GetGlobalLength(const MV &mv)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static int GetNumberVecs(const MV &mv)
Class which defines basic traits for the operator type.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
OperatorTraits< ScalarType, MV, OP > OPT
void setBlockSize(int blockSize)
Set the blocksize.
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
void iterate()
This method performs block Gmres iterations until the status test indicates the need to stop or an er...
PseudoBlockGmresIterState< ScalarType, MV, DM > getState() const
Get the current state of the linear solver.
SCT::magnitudeType MagnitudeType
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
int getNumIters() const
Get the current iteration count.
DenseMatTraits< ScalarType, DM > DMT
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
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.
PseudoBlockGmresIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, const Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP, DM > > &ortho, Teuchos::ParameterList ¶ms)
PseudoBlockGmresIter constructor with linear problem, solver utilities, and parameter list of solver ...
virtual ~PseudoBlockGmresIter()
Destructor.
void initialize(const PseudoBlockGmresIterState< ScalarType, MV, DM > &newstate)
Initialize the solver to an iterate, providing a complete state.
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the "native" residual vectors.
bool isInitialized()
States whether the solver has been initialized or not.
MultiVecTraits< ScalarType, MV, DM > MVT
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem.
Teuchos::ScalarTraits< ScalarType > SCT
int getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem.
A pure virtual class for defining the status tests for the Belos iterative solvers.
Structure to contain pointers to PseudoBlockGmresIter state variables.
int curDim
The current dimension of the reduction.
std::vector< Teuchos::RCP< const std::vector< ScalarType > > > sn
The current Given's rotation coefficients.
std::vector< Teuchos::RCP< const DM > > Z
The current right-hand side of the least squares system RY = Z.
std::vector< Teuchos::RCP< const DM > > H
The current Hessenberg matrix.
std::vector< Teuchos::RCP< const std::vector< MagnitudeType > > > cs
std::vector< Teuchos::RCP< const MV > > V
The current Krylov basis.