43 #ifndef BELOS_GMRES_POLY_SOLMGR_HPP 44 #define BELOS_GMRES_POLY_SOLMGR_HPP 58 #include "Teuchos_as.hpp" 59 #ifdef BELOS_TEUCHOS_TIME_MONITOR 60 #include "Teuchos_TimeMonitor.hpp" 151 template<
class ScalarType,
class MV,
class OP>
156 typedef Teuchos::ScalarTraits<ScalarType> STS;
157 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
192 const Teuchos::RCP<Teuchos::ParameterList> &pl );
198 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
225 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
226 return Teuchos::tuple(timerPoly_);
248 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
264 problem_->setProblem ();
265 poly_Op_ = Teuchos::null;
306 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
309 Teuchos::RCP<std::ostream> outputStream_;
312 Teuchos::RCP<Teuchos::ParameterList> params_;
313 Teuchos::RCP<Teuchos::ParameterList> outerParams_;
316 static constexpr
int maxDegree_default_ = 25;
318 static constexpr
const char * label_default_ =
"Belos";
319 static constexpr
const char * outerSolverType_default_ =
"";
320 static constexpr
const char * polyType_default_ =
"Arnoldi";
321 static constexpr
const char * orthoType_default_ =
"ICGS";
322 static constexpr
bool addRoots_default_ =
true;
323 static constexpr
bool dampPoly_default_ =
false;
324 static constexpr
bool randomRHS_default_ =
true;
326 #if defined(_WIN32) && defined(__clang__) 327 static constexpr std::ostream * outputStream_default_ =
328 __builtin_constant_p(reinterpret_cast<const std::ostream*>(&std::cout));
330 static constexpr std::ostream * outputStream_default_ = &std::cout;
334 MagnitudeType polyTol_;
335 int maxDegree_, numIters_;
337 bool hasOuterSolver_;
341 std::string polyType_;
342 std::string outerSolverType_;
343 std::string orthoType_;
347 Teuchos::RCP<gmres_poly_t> poly_Op_;
351 Teuchos::RCP<Teuchos::Time> timerPoly_;
358 mutable Teuchos::RCP<const Teuchos::ParameterList> validPL_;
362 template<
class ScalarType,
class MV,
class OP>
364 outputStream_ (Teuchos::rcp(outputStream_default_,false)),
366 maxDegree_ (maxDegree_default_),
368 verbosity_ (verbosity_default_),
369 hasOuterSolver_ (false),
370 randomRHS_ (randomRHS_default_),
371 damp_ (dampPoly_default_),
372 addRoots_ (addRoots_default_),
373 polyType_ (polyType_default_),
374 outerSolverType_ (outerSolverType_default_),
375 orthoType_ (orthoType_default_),
377 label_ (label_default_),
383 template<
class ScalarType,
class MV,
class OP>
386 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
388 outputStream_ (Teuchos::rcp(outputStream_default_,false)),
390 maxDegree_ (maxDegree_default_),
392 verbosity_ (verbosity_default_),
393 hasOuterSolver_ (false),
394 randomRHS_ (randomRHS_default_),
395 damp_ (dampPoly_default_),
396 addRoots_ (addRoots_default_),
397 polyType_ (polyType_default_),
398 outerSolverType_ (outerSolverType_default_),
399 orthoType_ (orthoType_default_),
401 label_ (label_default_),
405 TEUCHOS_TEST_FOR_EXCEPTION(
406 problem_.is_null (), std::invalid_argument,
407 "Belos::GmresPolySolMgr: The given linear problem is null. " 408 "Please call this constructor with a nonnull LinearProblem argument, " 409 "or call the constructor that does not take a LinearProblem.");
413 if (! pl.is_null ()) {
419 template<
class ScalarType,
class MV,
class OP>
420 Teuchos::RCP<const Teuchos::ParameterList>
423 if (validPL_.is_null ()) {
424 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList ();
428 pl->set(
"Polynomial Type", static_cast<const char *>(polyType_default_),
429 "The type of GMRES polynomial that is used as a preconditioner: Roots, Arnoldi, or Gmres.");
431 "The relative residual tolerance that used to construct the GMRES polynomial.");
432 pl->set(
"Maximum Degree", static_cast<int>(maxDegree_default_),
433 "The maximum degree allowed for any GMRES polynomial.");
434 pl->set(
"Outer Solver", static_cast<const char *>(outerSolverType_default_),
435 "The outer solver that this polynomial is used to precondition.");
436 pl->set(
"Verbosity", static_cast<int>(verbosity_default_),
437 "What type(s) of solver information should be outputted\n" 438 "to the output stream.");
439 pl->set(
"Output Stream", Teuchos::rcp(outputStream_default_,
false),
440 "A reference-counted pointer to the output stream where all\n" 441 "solver output is sent.");
442 pl->set(
"Timer Label", static_cast<const char *>(label_default_),
443 "The string to use as a prefix for the timer labels.");
444 pl->set(
"Orthogonalization", static_cast<const char *>(orthoType_default_),
445 "The type of orthogonalization to use to generate polynomial: DGKS, ICGS, or IMGS.");
446 pl->set(
"Random RHS", static_cast<bool>(randomRHS_default_),
447 "Add roots to polynomial for stability.");
448 pl->set(
"Add Roots", static_cast<bool>(addRoots_default_),
449 "Add roots to polynomial for stability.");
450 pl->set(
"Damp Poly", static_cast<bool>(dampPoly_default_),
451 "Damp polynomial for ill-conditioned problems.");
458 template<
class ScalarType,
class MV,
class OP>
463 if (params_.is_null ()) {
464 params_ = Teuchos::parameterList (*getValidParameters ());
467 params->validateParameters (*getValidParameters ());
471 if (params->isParameter(
"Polynomial Type")) {
472 polyType_ = params->get(
"Polynomial Type", polyType_default_);
476 params_->set(
"Polynomial Type", polyType_);
479 if (params->isParameter(
"Outer Solver")) {
480 outerSolverType_ = params->get(
"Outer Solver", outerSolverType_default_);
484 params_->set(
"Outer Solver", outerSolverType_);
487 if (params->isSublist(
"Outer Solver Params")) {
488 outerParams_ = Teuchos::parameterList( params->get<Teuchos::ParameterList>(
"Outer Solver Params") );
492 if (params->isParameter(
"Maximum Degree")) {
493 maxDegree_ = params->get(
"Maximum Degree",maxDegree_default_);
497 params_->set(
"Maximum Degree", maxDegree_);
500 if (params->isParameter(
"Timer Label")) {
501 std::string tempLabel = params->get(
"Timer Label", label_default_);
504 if (tempLabel != label_) {
506 #ifdef BELOS_TEUCHOS_TIME_MONITOR 507 std::string polyLabel = label_ +
": GmresPolyOp creation time";
508 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(polyLabel);
514 params_->set(
"Timer Label", label_);
517 if (params->isParameter(
"Orthogonalization")) {
518 std::string tempOrthoType = params->get(
"Orthogonalization",orthoType_default_);
522 std::ostringstream os;
523 os <<
"Belos::GCRODRSolMgr: Invalid orthogonalization name \"" 524 << tempOrthoType <<
"\". The following are valid options " 525 <<
"for the \"Orthogonalization\" name parameter: ";
527 throw std::invalid_argument (os.str());
529 if (tempOrthoType != orthoType_) {
530 orthoType_ = tempOrthoType;
534 params_->set(
"Orthogonalization", orthoType_);
537 if (params->isParameter(
"Verbosity")) {
538 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
539 verbosity_ = params->get(
"Verbosity", verbosity_default_);
541 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
546 params_->set(
"Verbosity", verbosity_);
549 if (params->isParameter(
"Output Stream")) {
550 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
554 params_->set(
"Output Stream", outputStream_);
558 if (params->isParameter(
"Polynomial Tolerance")) {
559 if (params->isType<MagnitudeType> (
"Polynomial Tolerance")) {
560 polyTol_ = params->get (
"Polynomial Tolerance",
569 params_->set(
"Polynomial Tolerance", polyTol_);
572 if (params->isParameter(
"Random RHS")) {
573 randomRHS_ = params->get(
"Random RHS",randomRHS_default_);
577 params_->set(
"Random RHS", randomRHS_);
581 if (params->isParameter(
"Damped Poly")) {
582 damp_ = params->get(
"Damped Poly",dampPoly_default_);
585 params_->set(
"Damped Poly", damp_);
588 if (params->isParameter(
"Add Roots")) {
589 addRoots_ = params->get(
"Add Roots",addRoots_default_);
593 params_->set(
"Add Roots", addRoots_);
596 #ifdef BELOS_TEUCHOS_TIME_MONITOR 597 if (timerPoly_ == Teuchos::null) {
598 std::string polyLabel = label_ +
": GmresPolyOp creation time";
599 timerPoly_ = Teuchos::TimeMonitor::getNewCounter(polyLabel);
604 if (outerSolverType_ !=
"") {
605 hasOuterSolver_ =
true;
613 template<
class ScalarType,
class MV,
class OP>
618 using Teuchos::rcp_const_cast;
628 setParameters (Teuchos::parameterList (*getValidParameters ()));
631 TEUCHOS_TEST_FOR_EXCEPTION(
633 "Belos::GmresPolySolMgr::solve: The linear problem has not been set yet, " 634 "or was set to null. Please call setProblem() with a nonnull input before " 637 TEUCHOS_TEST_FOR_EXCEPTION(
639 "Belos::GmresPolySolMgr::solve: The linear problem is not ready. Please " 640 "call setProblem() on the LinearProblem object before calling solve().");
644 if (!poly_dim_ && maxDegree_) {
645 #ifdef BELOS_TEUCHOS_TIME_MONITOR 646 Teuchos::TimeMonitor slvtimer(*timerPoly_);
648 poly_Op_ = Teuchos::rcp(
new gmres_poly_t( problem_, params_ ) );
649 poly_dim_ = poly_Op_->polyDegree();
652 "Belos::GmresPolyOp: Failed to generate polynomial that satisfied requirements.");
657 if (hasOuterSolver_ && maxDegree_) {
662 RCP<SolverManager<ScalarType, MultiVec<ScalarType>,
Operator<ScalarType> > > solver = factory.
create( outerSolverType_, outerParams_ );
663 TEUCHOS_TEST_FOR_EXCEPTION( solver == Teuchos::null, std::invalid_argument,
664 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
668 RCP<gmres_poly_mv_t> new_lhs = rcp(
new gmres_poly_mv_t( problem_->getLHS() ) );
669 RCP<gmres_poly_mv_t> new_rhs = rcp(
new gmres_poly_mv_t( rcp_const_cast<MV>( problem_->getRHS() ) ) );
670 RCP<gmres_poly_t> A = rcp(
new gmres_poly_t( problem_ ) );
673 std::string solverLabel = label_ +
": Hybrid Gmres";
674 newProblem->setLabel(solverLabel);
677 if (problem_->getLeftPrec() != Teuchos::null)
678 newProblem->setLeftPrec( poly_Op_ );
680 newProblem->setRightPrec( poly_Op_ );
683 if (problem_->getInitResVec() != Teuchos::null)
684 newProblem->setInitResVec( rcp(
new gmres_poly_mv_t( rcp_const_cast<MV>( problem_->getInitResVec() ) ) ) );
685 newProblem->setProblem();
687 solver->setProblem( newProblem );
689 ret = solver->solve();
690 numIters_ = solver->getNumIters();
691 loaDetected_ = solver->isLOADetected();
694 else if (hasOuterSolver_) {
698 RCP<SolverManager<ScalarType, MV, OP> > solver = factory.create( outerSolverType_, outerParams_ );
699 TEUCHOS_TEST_FOR_EXCEPTION( solver == Teuchos::null, std::invalid_argument,
700 "Belos::GmresPolySolMgr::solve(): Selected solver is not valid.");
702 solver->setProblem( problem_ );
704 ret = solver->solve();
705 numIters_ = solver->getNumIters();
706 loaDetected_ = solver->isLOADetected();
709 else if (maxDegree_) {
712 poly_Op_->ApplyPoly( *problem_->getRHS(), *problem_->getLHS() );
720 template<
class ScalarType,
class MV,
class OP>
723 std::ostringstream out;
725 out <<
"\"Belos::GmresPolySolMgr\": {" 726 <<
"ScalarType: " << Teuchos::TypeNameTraits<ScalarType>::name ()
727 <<
", Poly Degree: " << poly_dim_
728 <<
", Poly Max Degree: " << maxDegree_
729 <<
", Poly Tol: " << polyTol_;
736 #endif // BELOS_GMRES_POLY_SOLMGR_HPP Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
Collection of types and exceptions used within the Belos solvers.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
virtual ~GmresPolySolMgr()
Destructor.
Defines the GMRES polynomial operator hybrid-GMRES iterative linear solver.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve...
std::ostream & printValidNames(std::ostream &out) const
Print all recognized MatOrthoManager names to the given ostream.
static const double polyTol
Relative residual tolerance for matrix polynomial construction.
Traits class which defines basic operations on multivectors.
ResetType
How to reset the solver.
Alternative run-time polymorphic interface for operators.
GmresPolySolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
Pure virtual base class which describes the basic interface for a solver manager. ...
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Get current linear problem being solved for in this object.
A linear system to solve, and its associated information.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Class which describes the linear problem to be solved by the iterative solver.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
The GMRES polynomial can be created in conjunction with any standard preconditioner.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
bool isValidName(const std::string &name) const
Whether this factory recognizes the MatOrthoManager with the given name.
ReturnType
Whether the Belos solve converged for all linear systems.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Belos's class for applying the GMRES polynomial operator that is used by the hybrid-GMRES linear solv...
void reset(const ResetType type) override
Reset the solver.
std::string description() const override
Method to return description of the hybrid block GMRES solver manager.
GmresPolySolMgrPolynomialFailure is thrown when their is a problem generating the GMRES polynomial fo...
virtual Teuchos::RCP< solver_base_type > create(const std::string &solverName, const Teuchos::RCP< Teuchos::ParameterList > &solverParams)
Create, configure, and return the specified solver.
GmresPolySolMgrPolynomialFailure(const std::string &what_arg)
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Interface for multivectors used by Belos' linear solvers.
Parent class to all Belos exceptions.
Default parameters common to most Belos solvers.
GmresPolySolMgr()
Empty constructor for GmresPolySolMgr. This constructor takes no arguments and sets the default value...
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
typename ::Belos::Impl::SolverFactorySelector< SC, MV, OP >::type SolverFactory
GmresPolySolMgrLinearProblemFailure(const std::string &what_arg)