10#ifndef BELOS_FIXEDPOINT_SOLMGR_HPP
11#define BELOS_FIXEDPOINT_SOLMGR_HPP
32#ifdef BELOS_TEUCHOS_TIME_MONITOR
33# include "Teuchos_TimeMonitor.hpp"
60 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int, ScalarType>>
66 typedef Teuchos::ScalarTraits<ScalarType> SCT;
67 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
68 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
100 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
106 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
131 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
132 return Teuchos::tuple(timerSolve_);
170 sTest_ = Teuchos::null;
171 outputTest_ = Teuchos::null;
182 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
235 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
238 Teuchos::RCP<OutputManager<ScalarType> > printer_;
240 Teuchos::RCP<std::ostream> outputStream_;
246 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
249 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
252 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP,DM> > convTest_;
255 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
258 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > debugStatusTest_;
261 Teuchos::RCP<Teuchos::ParameterList> params_;
266 static constexpr int maxIters_default_ = 1000;
267 static constexpr bool showMaxResNormOnly_default_ =
false;
268 static constexpr int blockSize_default_ = 1;
271 static constexpr int outputFreq_default_ = -1;
272 static constexpr const char * label_default_ =
"Belos";
279 MagnitudeType convtol_;
286 MagnitudeType achievedTol_;
294 int blockSize_, verbosity_, outputStyle_, outputFreq_;
295 bool showMaxResNormOnly_;
301 Teuchos::RCP<Teuchos::Time> timerSolve_;
309template<
class ScalarType,
class MV,
class OP,
class DM>
314 maxIters_(maxIters_default_),
316 blockSize_(blockSize_default_),
317 verbosity_(verbosity_default_),
318 outputStyle_(outputStyle_default_),
319 outputFreq_(outputFreq_default_),
320 showMaxResNormOnly_(showMaxResNormOnly_default_),
321 label_(label_default_),
327template<
class ScalarType,
class MV,
class OP,
class DM>
330 const Teuchos::RCP<Teuchos::ParameterList> &
pl) :
335 maxIters_(maxIters_default_),
337 blockSize_(blockSize_default_),
338 verbosity_(verbosity_default_),
339 outputStyle_(outputStyle_default_),
340 outputFreq_(outputFreq_default_),
341 showMaxResNormOnly_(showMaxResNormOnly_default_),
342 label_(label_default_),
346 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
351 if (!
pl.is_null()) {
356template<
class ScalarType,
class MV,
class OP,
class DM>
362 if (params_ == Teuchos::null) {
363 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
366 params->validateParameters(*getValidParameters());
370 if (
params->isParameter(
"Maximum Iterations")) {
371 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
374 params_->set(
"Maximum Iterations", maxIters_);
375 if (maxIterTest_!=Teuchos::null)
376 maxIterTest_->setMaxIters( maxIters_ );
380 if (
params->isParameter(
"Block Size")) {
381 blockSize_ =
params->get(
"Block Size",blockSize_default_);
383 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
386 params_->set(
"Block Size", blockSize_);
390 if (
params->isParameter(
"Timer Label")) {
396 params_->set(
"Timer Label", label_);
397 std::string
solveLabel = label_ +
": FixedPointSolMgr total solve time";
398#ifdef BELOS_TEUCHOS_TIME_MONITOR
399 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
405 if (
params->isParameter(
"Verbosity")) {
406 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
407 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
409 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
413 params_->set(
"Verbosity", verbosity_);
414 if (printer_ != Teuchos::null)
415 printer_->setVerbosity(verbosity_);
419 if (
params->isParameter(
"Output Style")) {
420 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
421 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
423 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
427 params_->set(
"Output Style", outputStyle_);
428 outputTest_ = Teuchos::null;
432 if (
params->isParameter(
"Output Stream")) {
433 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
436 params_->set(
"Output Stream", outputStream_);
437 if (printer_ != Teuchos::null)
438 printer_->setOStream( outputStream_ );
443 if (
params->isParameter(
"Output Frequency")) {
444 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
448 params_->set(
"Output Frequency", outputFreq_);
449 if (outputTest_ != Teuchos::null)
450 outputTest_->setOutputFrequency( outputFreq_ );
454 if (printer_ == Teuchos::null) {
463 if (
params->isParameter(
"Convergence Tolerance")) {
464 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
465 convtol_ =
params->get (
"Convergence Tolerance",
473 params_->set(
"Convergence Tolerance", convtol_);
474 if (convTest_ != Teuchos::null)
475 convTest_->setTolerance( convtol_ );
478 if (
params->isParameter(
"Show Maximum Residual Norm Only")) {
479 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*
params,
"Show Maximum Residual Norm Only");
482 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
483 if (convTest_ != Teuchos::null)
484 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
490 if (maxIterTest_ == Teuchos::null)
494 if (convTest_ == Teuchos::null)
495 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
497 if (sTest_ == Teuchos::null) {
498 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
504 if (Teuchos::nonnull(debugStatusTest_)) {
505 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, sTest_, debugStatusTest_ ) );
509 if (outputTest_ == Teuchos::null) {
523 if (timerSolve_ == Teuchos::null) {
524 std::string
solveLabel = label_ +
": FixedPointSolMgr total solve time";
525#ifdef BELOS_TEUCHOS_TIME_MONITOR
526 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
535template<
class ScalarType,
class MV,
class OP,
class DM>
536Teuchos::RCP<const Teuchos::ParameterList>
539 static Teuchos::RCP<const Teuchos::ParameterList>
validPL;
543 Teuchos::RCP<Teuchos::ParameterList>
pl = Teuchos::parameterList();
548 "The relative residual tolerance that needs to be achieved by the\n"
549 "iterative solver in order for the linear system to be declared converged.");
550 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
551 "The maximum number of block iterations allowed for each\n"
552 "set of RHS solved.");
553 pl->set(
"Block Size",
static_cast<int>(blockSize_default_),
554 "The number of vectors in each block.");
555 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
556 "What type(s) of solver information should be outputted\n"
557 "to the output stream.");
558 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
559 "What style is used for the solver information outputted\n"
560 "to the output stream.");
561 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
562 "How often convergence information should be outputted\n"
563 "to the output stream.");
564 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
565 "A reference-counted pointer to the output stream where all\n"
566 "solver output is sent.");
567 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
568 "When convergence information is printed, only show the maximum\n"
569 "relative residual norm when the block size is greater than one.");
570 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
571 "The string to use as a prefix for the timer labels.");
579template<
class ScalarType,
class MV,
class OP,
class DM>
583 using Teuchos::rcp_const_cast;
584 using Teuchos::rcp_dynamic_cast;
593 setParameters(Teuchos::parameterList(*getValidParameters()));
598 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
599 "has not been called.");
603 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
615 problem_->setLSIndex(
currIdx );
619 Teuchos::ParameterList
plist;
620 plist.set(
"Block Size",blockSize_);
623 outputTest_->reset();
637#ifdef BELOS_TEUCHOS_TIME_MONITOR
638 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
652 outputTest_->resetNumCalls();
670 if (convTest_->getStatus() ==
Passed) {
674 std::vector<int>
convIdx = convTest_->convIndices();
684 problem_->setCurrLS();
692 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
712 std::vector<MagnitudeType>
norms;
728 else if (maxIterTest_->getStatus() ==
Passed) {
742 else if (Teuchos::nonnull(debugStatusTest_) &&
743 debugStatusTest_->getStatus() ==
Passed) {
751 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
752 "the maximum iteration count test passed. Please report this bug "
753 "to the Belos developers.");
759 achievedTol_ = MT::one();
760 Teuchos::RCP<MV>
X = problem_->getLHS();
761 MVT::MvInit( *
X, SCT::zero() );
762 printer_->stream(
Warnings) <<
"Belos::FixedPointSolMgr::solve(): Warning! NaN has been detected!"
766 catch (
const std::exception &
e) {
768 std::ostream&
err = printer_->stream (
Errors);
769 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at "
771 <<
e.what() << std::endl;
778 problem_->setCurrLS();
795 problem_->setLSIndex(
currIdx );
812#ifdef BELOS_TEUCHOS_TIME_MONITOR
818 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
823 numIters_ = maxIterTest_->getNumIters();
828 const std::vector<MagnitudeType>*
pTestValues = convTest_->getTestValue();
831 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
832 "method returned NULL. Please report this bug to the Belos developers.");
835 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
836 "method returned a vector of length zero. Please report this bug to the "
837 "Belos developers.");
852template<
class ScalarType,
class MV,
class OP,
class DM>
855 std::ostringstream
oss;
856 oss <<
"Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
862#ifdef HAVE_BELOS_TPETRA
865#define BELOS_TPETRA_FIXEDPOINTSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
866 BELOS_TPETRA_CALL(Belos::FixedPointSolMgr, SC, LO, GO, NT)
868#define BELOS_TPETRA_FIXEDPOINTSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
869 BELOS_TPETRA_EXTERN_CALL(Belos::FixedPointSolMgr, SC, LO, GO, NT)
871TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_FIXEDPOINTSOLMGR_EXTERN_CALL)
Belos concrete class for performing the conjugate-gradient (CG) iteration.
Belos header file which uses auto-configuration information to include necessary C++ headers.
Belos concrete class for performing fixed point iteration iteration.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
Class which describes the linear problem to be solved by the iterative solver.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class which describes the basic interface for a solver manager.
Belos::StatusTest for logically combining several status tests.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Full specialization of Belos::DenseMatTraits for Teuchos::SerialDenseMatrix with ordinal type int and...
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
The Belos::FixedPointSolMgr provides a powerful and fully-featured solver manager over the FixedPoint...
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
virtual ~FixedPointSolMgr()
Destructor.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
void replaceUserConvStatusTest(const Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP, DM > > &userConvStatusTest)
Set user-defined convergence status test.
void setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &debugStatusTest) override
Set a debug status test, OR-combined into the top-level status test.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
FixedPointSolMgr()
Empty constructor for FixedPointSolMgr. This constructor takes no arguments and sets the default valu...
std::string description() const override
Method to return description of the block CG solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
FixedPointSolMgrLinearProblemFailure(const std::string &what_arg)
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 convTol
Default convergence tolerance.