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>
61 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
79 Teuchos::ParameterList &
params );
153 state.curDim = curDim_;
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) {
216 if (!initialized_)
return 0;
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 &
params ):
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>
347 initialized_ =
false;
352 template <
class ScalarType,
class MV,
class OP,
class DM>
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) {
378 Teuchos::RCP<DM> y = DMT::SubviewCopy(*Z_[
i], curDim_, 1);
379 DMT::SyncDeviceToHost( *y );
380 DMT::SyncDeviceToHost( *H_[
i] );
384 blas.TRSM( Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
385 Teuchos::NON_UNIT_DIAG, curDim_, 1,
one,
386 DMT::GetConstRawHostPtr(*H_[
i]), DMT::GetStride(*H_[
i]),
387 DMT::GetRawHostPtr(*y), DMT::GetStride(*y) );
389 DMT::SyncHostToDevice( *y );
390 DMT::SyncHostToDevice( *H_[
i] );
391 Teuchos::RCP<const MV>
Vjp1 = MVT::CloneView( *V_[
i],
index2 );
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)
416 DMT::SyncDeviceToHost( *Z_[
j] );
421 return Teuchos::null;
425 template <
class ScalarType,
class MV,
class OP,
class DM>
434 this->numRHS_ = MVT::GetNumberVecs (*(lp_->getCurrLHSVec()));
440 std::string
errstr (
"Belos::PseudoBlockGmresIter::initialize(): "
441 "Specified multivectors must have a consistent "
442 "length and width.");
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.");
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 "
479 std::invalid_argument,
487 for (
int i=0;
i<numRHS_; ++
i) {
491 if (V_[
i].
is_null() || MVT::GetNumberVecs(*V_[
i]) < numBlocks_ + 1) {
497 std::invalid_argument,
errstr );
499 std::invalid_argument,
errstr );
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) {
536 Z_[
i] = DMT::Create();
538 if (DMT::GetNumRows(*Z_[
i]) < numBlocks_+1) {
539 DMT::Reshape(*Z_[
i], numBlocks_+1, 1,
false);
548 DMT::PutScalar(*Z_[
i]);
550 Teuchos::RCP<const DM>
newZ = DMT::SubviewConst(*
newstate.Z[
i],curDim_+1,1);
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) {
565 H_[
i] = DMT::Create();
567 if (DMT::GetNumRows(*H_[
i]) < numBlocks_+1 || DMT::GetNumCols(*H_[
i]) < numBlocks_) {
568 DMT::Reshape(*H_[
i], numBlocks_+1, numBlocks_,
false);
572 if ((
int)
newstate.H.size() == numRHS_) {
576 "Belos::PseudoBlockGmresIter::initialize(): Specified Hessenberg matrices must have a consistent size to the current subspace dimension");
580 Teuchos::RCP<const DM>
newH = DMT::SubviewConst(*
newstate.H[
i],curDim_+1, curDim_);
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) {
600 cs_[
i] = Teuchos::rcp(
new std::vector<MagnitudeType>(*
newstate.cs[
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) {
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) {
652 Teuchos::RCP<const MV>
tmp_vec = MVT::CloneView( *V_[
i], index );
681 for (
int i=0;
i<numRHS_; ++
i) {
685 Teuchos::RCP<const MV>
V_prev = MVT::CloneView( *V_[
i], index );
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_);
698 Teuchos::RCP<DM>
r_new = DMT::Subview(*H_[
i], 1, 1,
num_prev, curDim_);
709 Teuchos::RCP<MV>
tmp_vec = MVT::CloneViewNonConst( *V_[
i],
index2 );
736 template<
class ScalarType,
class MV,
class OP,
class DM>
742 int curDim = curDim_;
743 if (
dim >= curDim_ &&
dim < getMaxSubspaceDim()) {
748 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
750 Teuchos::BLAS<int, ScalarType>
blas;
752 for (
i=0;
i<numRHS_; ++
i) {
758 DMT::SyncDeviceToHost(*H_[
i]);
759 DMT::SyncDeviceToHost(*Z_[
i]);
761 for (
j=0;
j<curDim;
j++) {
765 blas.ROT( 1, &(DMT::Value(*H_[
i],
j,curDim)), 1, &(DMT::Value(*H_[
i],
j+1, curDim)),
766 1, &(*cs_[
i])[
j], &(*sn_[
i])[
j] );
771 blas.ROTG( &(DMT::Value(*H_[
i],curDim,curDim)), &(DMT::Value(*H_[
i],curDim+1,curDim)),
772 &(*cs_[
i])[curDim], &(*sn_[
i])[curDim] );
773 DMT::Value(*H_[
i],curDim+1,curDim) =
zero;
777 blas.ROT( 1, &(DMT::Value(*Z_[
i],curDim,0)), 1, &(DMT::Value(*Z_[
i],curDim+1,0)),
778 1, &(*cs_[
i])[curDim], &(*sn_[
i])[curDim] );
780 DMT::SyncHostToDevice(*H_[
i]);
781 DMT::SyncHostToDevice(*Z_[
i]);
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.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
This class implements the pseudo-block GMRES iteration, where a block Krylov subspace is constructed ...
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 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.