11#ifndef BELOS_GMRES_POLY_SOLMGR_HPP
12#define BELOS_GMRES_POLY_SOLMGR_HPP
26#include "Teuchos_as.hpp"
27#ifdef BELOS_TEUCHOS_TIME_MONITOR
28#include "Teuchos_TimeMonitor.hpp"
121template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
125 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
127 typedef Teuchos::ScalarTraits<MagnitudeType> MTS;
162 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
168 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
215 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
216 return Teuchos::tuple(timerPoly_);
259 problem_->setProblem ();
260 poly_Op_ = Teuchos::null;
301 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
304 Teuchos::RCP<std::ostream> outputStream_;
310 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > debugStatusTest_;
313 Teuchos::RCP<Teuchos::ParameterList> params_;
314 Teuchos::RCP<Teuchos::ParameterList> outerParams_;
317 static constexpr int maxDegree_default_ = 25;
319 static constexpr const char * label_default_ =
"Belos";
320 static constexpr const char * outerSolverType_default_ =
"";
321 static constexpr const char * polyType_default_ =
"Arnoldi";
322 static constexpr const char * orthoType_default_ =
"ICGS";
323 static constexpr bool addRoots_default_ =
true;
324 static constexpr bool dampPoly_default_ =
false;
325 static constexpr bool randomRHS_default_ =
true;
328 MagnitudeType polyTol_, achievedTol_;
329 int maxDegree_, numIters_;
331 bool hasOuterSolver_;
335 std::string polyType_;
336 std::string outerSolverType_;
337 std::string orthoType_;
341 Teuchos::RCP<gmres_poly_t> poly_Op_;
345 Teuchos::RCP<Teuchos::Time> timerPoly_;
352 mutable Teuchos::RCP<const Teuchos::ParameterList> validPL_;
356template<
class ScalarType,
class MV,
class OP,
class DM>
360 achievedTol_(MTS::
zero()),
361 maxDegree_ (maxDegree_default_),
363 verbosity_ (verbosity_default_),
364 hasOuterSolver_ (
false),
365 randomRHS_ (randomRHS_default_),
366 damp_ (dampPoly_default_),
367 addRoots_ (addRoots_default_),
368 polyType_ (polyType_default_),
369 outerSolverType_ (outerSolverType_default_),
370 orthoType_ (orthoType_default_),
372 label_ (label_default_),
378template<
class ScalarType,
class MV,
class OP,
class DM>
381 const Teuchos::RCP<Teuchos::ParameterList> &
pl) :
385 maxDegree_ (maxDegree_default_),
387 verbosity_ (verbosity_default_),
388 hasOuterSolver_ (
false),
389 randomRHS_ (randomRHS_default_),
390 damp_ (dampPoly_default_),
391 addRoots_ (addRoots_default_),
392 polyType_ (polyType_default_),
393 outerSolverType_ (outerSolverType_default_),
394 orthoType_ (orthoType_default_),
396 label_ (label_default_),
401 problem_.is_null (), std::invalid_argument,
402 "Belos::GmresPolySolMgr: The given linear problem is null. "
403 "Please call this constructor with a nonnull LinearProblem argument, "
404 "or call the constructor that does not take a LinearProblem.");
408 if (!
pl.is_null ()) {
414template<
class ScalarType,
class MV,
class OP,
class DM>
415Teuchos::RCP<const Teuchos::ParameterList>
418 if (validPL_.is_null ()) {
419 Teuchos::RCP<Teuchos::ParameterList>
pl = Teuchos::parameterList ();
423 pl->set(
"Polynomial Type",
static_cast<const char *
>(polyType_default_),
424 "The type of GMRES polynomial that is used as a preconditioner: Roots, Arnoldi, or Gmres.");
426 "The relative residual tolerance that used to construct the GMRES polynomial.");
427 pl->set(
"Maximum Degree",
static_cast<int>(maxDegree_default_),
428 "The maximum degree allowed for any GMRES polynomial.");
429 pl->set(
"Outer Solver",
static_cast<const char *
>(outerSolverType_default_),
430 "The outer solver that this polynomial is used to precondition.");
431 pl->set(
"Outer Solver Params", Teuchos::ParameterList(),
432 "Parameter list for the outer solver.");
433 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
434 "What type(s) of solver information should be outputted\n"
435 "to the output stream.");
436 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
437 "A reference-counted pointer to the output stream where all\n"
438 "solver output is sent.");
439 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
440 "The string to use as a prefix for the timer labels.");
441 pl->set(
"Orthogonalization",
static_cast<const char *
>(orthoType_default_),
442 "The type of orthogonalization to use to generate polynomial: DGKS, ICGS, or IMGS.");
443 pl->set(
"Random RHS",
static_cast<bool>(randomRHS_default_),
444 "Add roots to polynomial for stability.");
445 pl->set(
"Add Roots",
static_cast<bool>(addRoots_default_),
446 "Add roots to polynomial for stability.");
447 pl->set(
"Damp Poly",
static_cast<bool>(dampPoly_default_),
448 "Damp polynomial for ill-conditioned problems.");
455template<
class ScalarType,
class MV,
class OP,
class DM>
460 if (params_.is_null ()) {
461 params_ = Teuchos::parameterList (*getValidParameters ());
464 params->validateParameters (*getValidParameters (),0);
468 if (
params->isParameter(
"Polynomial Type")) {
469 polyType_ =
params->get(
"Polynomial Type", polyType_default_);
473 params_->set(
"Polynomial Type", polyType_);
476 if (
params->isParameter(
"Outer Solver")) {
477 outerSolverType_ =
params->get(
"Outer Solver", outerSolverType_default_);
481 params_->set(
"Outer Solver", outerSolverType_);
484 if (
params->isSublist(
"Outer Solver Params")) {
485 outerParams_ = Teuchos::parameterList(
params->get<Teuchos::ParameterList>(
"Outer Solver Params") );
489 if (
params->isParameter(
"Maximum Degree")) {
490 maxDegree_ =
params->get(
"Maximum Degree",maxDegree_default_);
494 params_->set(
"Maximum Degree", maxDegree_);
497 if (
params->isParameter(
"Timer Label")) {
503#ifdef BELOS_TEUCHOS_TIME_MONITOR
504 std::string
polyLabel = label_ +
": GmresPolyOp creation time";
505 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(
polyLabel);
511 params_->set(
"Timer Label", label_);
514 if (
params->isParameter(
"Orthogonalization")) {
519 std::ostringstream
os;
520 os <<
"Belos::GmresPolySolMgr: Invalid orthogonalization name \""
522 <<
"for the \"Orthogonalization\" name parameter: ";
524 throw std::invalid_argument (
os.str());
531 params_->set(
"Orthogonalization", orthoType_);
534 if (
params->isParameter(
"Verbosity")) {
535 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
536 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
538 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
543 params_->set(
"Verbosity", verbosity_);
546 if (
params->isParameter(
"Output Stream")) {
547 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
551 params_->set(
"Output Stream", outputStream_);
555 if (
params->isParameter(
"Polynomial Tolerance")) {
556 if (
params->isType<MagnitudeType> (
"Polynomial Tolerance")) {
557 polyTol_ =
params->get (
"Polynomial Tolerance",
566 params_->set(
"Polynomial Tolerance", polyTol_);
569 if (
params->isParameter(
"Random RHS")) {
570 randomRHS_ =
params->get(
"Random RHS",randomRHS_default_);
574 params_->set(
"Random RHS", randomRHS_);
578 if (
params->isParameter(
"Damped Poly")) {
579 damp_ =
params->get(
"Damped Poly",dampPoly_default_);
582 params_->set(
"Damped Poly", damp_);
585 if (
params->isParameter(
"Add Roots")) {
586 addRoots_ =
params->get(
"Add Roots",addRoots_default_);
590 params_->set(
"Add Roots", addRoots_);
593#ifdef BELOS_TEUCHOS_TIME_MONITOR
594 if (timerPoly_ == Teuchos::null) {
595 std::string
polyLabel = label_ +
": GmresPolyOp creation time";
596 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(
polyLabel);
601 if (outerSolverType_ !=
"") {
602 hasOuterSolver_ =
true;
610template<
class ScalarType,
class MV,
class OP,
class DM>
615 using Teuchos::rcp_const_cast;
625 setParameters (Teuchos::parameterList (*getValidParameters ()));
630 "Belos::GmresPolySolMgr::solve: The linear problem has not been set yet, "
631 "or was set to null. Please call setProblem() with a nonnull input before "
636 "Belos::GmresPolySolMgr::solve: The linear problem is not ready. Please "
637 "call setProblem() on the LinearProblem object before calling solve().");
641 if (!poly_dim_ && maxDegree_) {
642#ifdef BELOS_TEUCHOS_TIME_MONITOR
643 Teuchos::TimeMonitor
slvtimer(*timerPoly_);
645 poly_Op_ = Teuchos::rcp(
new gmres_poly_t( problem_, params_ ) );
646 poly_dim_ = poly_Op_->polyDegree();
649 "Belos::GmresPolyOp: Failed to generate polynomial that satisfied requirements.");
654 if (hasOuterSolver_ && maxDegree_) {
661 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
670 std::string
solverLabel = label_ +
": Hybrid Gmres";
674 if (problem_->getLeftPrec() != Teuchos::null)
680 if (problem_->getInitResVec() != Teuchos::null)
687 numIters_ =
solver->getNumIters();
688 loaDetected_ =
solver->isLOADetected();
689 achievedTol_ =
solver->achievedTol();
692 else if (hasOuterSolver_) {
698 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
700 solver->setProblem( problem_ );
704 if (Teuchos::nonnull(debugStatusTest_)) {
705 solver->setDebugStatusTest(debugStatusTest_);
709 numIters_ =
solver->getNumIters();
710 loaDetected_ =
solver->isLOADetected();
711 achievedTol_ =
solver->achievedTol();
714 else if (maxDegree_) {
717 poly_Op_->ApplyPoly( *problem_->getRHS(), *problem_->getLHS() );
718 achievedTol_ = MTS::one();
726template<
class ScalarType,
class MV,
class OP,
class DM>
729 std::ostringstream
out;
731 out <<
"\"Belos::GmresPolySolMgr\": {"
732 <<
"ScalarType: " << Teuchos::TypeNameTraits<ScalarType>::name ()
733 <<
", Poly Degree: " << poly_dim_
734 <<
", Poly Max Degree: " << maxDegree_
735 <<
", Poly Tol: " << polyTol_;
742#ifdef HAVE_BELOS_TPETRA
745#define BELOS_TPETRA_GMRESPOLYSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
746 BELOS_TPETRA_CALL(Belos::GmresPolySolMgr, SC, LO, GO, NT)
748#define BELOS_TPETRA_GMRESPOLYSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
749 BELOS_TPETRA_EXTERN_CALL(Belos::GmresPolySolMgr, SC, LO, GO, NT)
751TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_GMRESPOLYSOLMGR_EXTERN_CALL)
Belos header file which uses auto-configuration information to include necessary C++ headers.
Defines the GMRES polynomial operator hybrid-GMRES iterative linear solver.
Class which describes the linear problem to be solved by the iterative solver.
Pure virtual base class which describes the basic interface for a solver manager.
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
The GMRES polynomial can be created in conjunction with any standard preconditioner.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
std::string description() const override
Method to return description of the hybrid block GMRES solver manager.
GmresPolySolMgr()
Empty constructor for GmresPolySolMgr. This constructor takes no arguments and sets the default value...
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
void reset(const ResetType type) override
Reset the solver.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
void setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &debugStatusTest) override
Set a debug status test, forwarded to the outer solver manager (if any).
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
virtual ~GmresPolySolMgr()
Destructor.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Get current linear problem being solved for in this object.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
GmresPolySolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
GmresPolySolMgrLinearProblemFailure(const std::string &what_arg)
GmresPolySolMgrPolynomialFailure is thrown when their is a problem generating the GMRES polynomial fo...
GmresPolySolMgrPolynomialFailure(const std::string &what_arg)
A linear system to solve, and its associated information.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double polyTol
Relative residual tolerance for matrix polynomial construction.