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::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
223 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
226 Teuchos::RCP<OutputManager<ScalarType> > printer_;
228 Teuchos::RCP<std::ostream> outputStream_;
234 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
237 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
240 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP,DM> > convTest_;
243 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
246 Teuchos::RCP<Teuchos::ParameterList> params_;
251 static constexpr int maxIters_default_ = 1000;
252 static constexpr bool showMaxResNormOnly_default_ =
false;
253 static constexpr int blockSize_default_ = 1;
256 static constexpr int outputFreq_default_ = -1;
257 static constexpr const char * label_default_ =
"Belos";
264 MagnitudeType convtol_;
271 MagnitudeType achievedTol_;
279 int blockSize_, verbosity_, outputStyle_, outputFreq_;
280 bool showMaxResNormOnly_;
286 Teuchos::RCP<Teuchos::Time> timerSolve_;
294template<
class ScalarType,
class MV,
class OP,
class DM>
299 maxIters_(maxIters_default_),
301 blockSize_(blockSize_default_),
302 verbosity_(verbosity_default_),
303 outputStyle_(outputStyle_default_),
304 outputFreq_(outputFreq_default_),
305 showMaxResNormOnly_(showMaxResNormOnly_default_),
306 label_(label_default_),
312template<
class ScalarType,
class MV,
class OP,
class DM>
315 const Teuchos::RCP<Teuchos::ParameterList> &
pl) :
320 maxIters_(maxIters_default_),
322 blockSize_(blockSize_default_),
323 verbosity_(verbosity_default_),
324 outputStyle_(outputStyle_default_),
325 outputFreq_(outputFreq_default_),
326 showMaxResNormOnly_(showMaxResNormOnly_default_),
327 label_(label_default_),
331 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
336 if (!
pl.is_null()) {
341template<
class ScalarType,
class MV,
class OP,
class DM>
347 if (params_ == Teuchos::null) {
348 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
351 params->validateParameters(*getValidParameters());
355 if (
params->isParameter(
"Maximum Iterations")) {
356 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
359 params_->set(
"Maximum Iterations", maxIters_);
360 if (maxIterTest_!=Teuchos::null)
361 maxIterTest_->setMaxIters( maxIters_ );
365 if (
params->isParameter(
"Block Size")) {
366 blockSize_ =
params->get(
"Block Size",blockSize_default_);
368 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
371 params_->set(
"Block Size", blockSize_);
375 if (
params->isParameter(
"Timer Label")) {
381 params_->set(
"Timer Label", label_);
382 std::string
solveLabel = label_ +
": FixedPointSolMgr total solve time";
383#ifdef BELOS_TEUCHOS_TIME_MONITOR
384 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
390 if (
params->isParameter(
"Verbosity")) {
391 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
392 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
394 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
398 params_->set(
"Verbosity", verbosity_);
399 if (printer_ != Teuchos::null)
400 printer_->setVerbosity(verbosity_);
404 if (
params->isParameter(
"Output Style")) {
405 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
406 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
408 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
412 params_->set(
"Output Style", outputStyle_);
413 outputTest_ = Teuchos::null;
417 if (
params->isParameter(
"Output Stream")) {
418 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
421 params_->set(
"Output Stream", outputStream_);
422 if (printer_ != Teuchos::null)
423 printer_->setOStream( outputStream_ );
428 if (
params->isParameter(
"Output Frequency")) {
429 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
433 params_->set(
"Output Frequency", outputFreq_);
434 if (outputTest_ != Teuchos::null)
435 outputTest_->setOutputFrequency( outputFreq_ );
439 if (printer_ == Teuchos::null) {
448 if (
params->isParameter(
"Convergence Tolerance")) {
449 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
450 convtol_ =
params->get (
"Convergence Tolerance",
458 params_->set(
"Convergence Tolerance", convtol_);
459 if (convTest_ != Teuchos::null)
460 convTest_->setTolerance( convtol_ );
463 if (
params->isParameter(
"Show Maximum Residual Norm Only")) {
464 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*
params,
"Show Maximum Residual Norm Only");
467 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
468 if (convTest_ != Teuchos::null)
469 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
475 if (maxIterTest_ == Teuchos::null)
479 if (convTest_ == Teuchos::null)
480 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
482 if (sTest_ == Teuchos::null)
483 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
485 if (outputTest_ == Teuchos::null) {
499 if (timerSolve_ == Teuchos::null) {
500 std::string
solveLabel = label_ +
": FixedPointSolMgr total solve time";
501#ifdef BELOS_TEUCHOS_TIME_MONITOR
502 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
511template<
class ScalarType,
class MV,
class OP,
class DM>
512Teuchos::RCP<const Teuchos::ParameterList>
515 static Teuchos::RCP<const Teuchos::ParameterList>
validPL;
519 Teuchos::RCP<Teuchos::ParameterList>
pl = Teuchos::parameterList();
524 "The relative residual tolerance that needs to be achieved by the\n"
525 "iterative solver in order for the linear system to be declared converged.");
526 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
527 "The maximum number of block iterations allowed for each\n"
528 "set of RHS solved.");
529 pl->set(
"Block Size",
static_cast<int>(blockSize_default_),
530 "The number of vectors in each block.");
531 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
532 "What type(s) of solver information should be outputted\n"
533 "to the output stream.");
534 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
535 "What style is used for the solver information outputted\n"
536 "to the output stream.");
537 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
538 "How often convergence information should be outputted\n"
539 "to the output stream.");
540 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
541 "A reference-counted pointer to the output stream where all\n"
542 "solver output is sent.");
543 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
544 "When convergence information is printed, only show the maximum\n"
545 "relative residual norm when the block size is greater than one.");
546 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
547 "The string to use as a prefix for the timer labels.");
555template<
class ScalarType,
class MV,
class OP,
class DM>
559 using Teuchos::rcp_const_cast;
560 using Teuchos::rcp_dynamic_cast;
569 setParameters(Teuchos::parameterList(*getValidParameters()));
574 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
575 "has not been called.");
579 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
591 problem_->setLSIndex(
currIdx );
595 Teuchos::ParameterList
plist;
596 plist.set(
"Block Size",blockSize_);
599 outputTest_->reset();
613#ifdef BELOS_TEUCHOS_TIME_MONITOR
614 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
628 outputTest_->resetNumCalls();
646 if (convTest_->getStatus() ==
Passed) {
650 std::vector<int>
convIdx = convTest_->convIndices();
660 problem_->setCurrLS();
668 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
688 std::vector<MagnitudeType>
norms;
704 else if (maxIterTest_->getStatus() ==
Passed) {
716 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
717 "the maximum iteration count test passed. Please report this bug "
718 "to the Belos developers.");
724 achievedTol_ = MT::one();
725 Teuchos::RCP<MV>
X = problem_->getLHS();
726 MVT::MvInit( *
X, SCT::zero() );
727 printer_->stream(
Warnings) <<
"Belos::FixedPointSolMgr::solve(): Warning! NaN has been detected!"
731 catch (
const std::exception &
e) {
733 std::ostream&
err = printer_->stream (
Errors);
734 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at "
736 <<
e.what() << std::endl;
743 problem_->setCurrLS();
760 problem_->setLSIndex(
currIdx );
777#ifdef BELOS_TEUCHOS_TIME_MONITOR
783 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
788 numIters_ = maxIterTest_->getNumIters();
793 const std::vector<MagnitudeType>*
pTestValues = convTest_->getTestValue();
796 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
797 "method returned NULL. Please report this bug to the Belos developers.");
800 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
801 "method returned a vector of length zero. Please report this bug to the "
802 "Belos developers.");
817template<
class ScalarType,
class MV,
class OP,
class DM>
820 std::ostringstream
oss;
821 oss <<
"Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
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.
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.