10#ifndef BELOS_MINRES_SOLMGR_HPP
11#define BELOS_MINRES_SOLMGR_HPP
31#ifdef BELOS_TEUCHOS_TIME_MONITOR
32#include "Teuchos_TimeMonitor.hpp"
35#include "Teuchos_StandardParameterEntryValidators.hpp"
83 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
89 typedef Teuchos::ScalarTraits<ScalarType> SCT;
90 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
91 typedef Teuchos::ScalarTraits< MagnitudeType > MST;
153 const Teuchos::RCP<Teuchos::ParameterList> ¶ms);
159 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
174 if (defaultParams_.is_null()) {
177 return defaultParams_;
194 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
195 return Teuchos::tuple (timerSolve_);
231 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
override;
237 debugStatusTest_ = debugStatusTest;
241 sTest_ = Teuchos::null;
242 parametersSet_ =
false;
254 problem_->setProblem ();
292 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
295 Teuchos::RCP<OutputManager<ScalarType> > printer_;
296 Teuchos::RCP<std::ostream> outputStream_;
304 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
309 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
314 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > convTest_;
319 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > impConvTest_;
324 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > expConvTest_;
330 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
335 Teuchos::RCP<StatusTest<ScalarType, MV, OP, DM> > debugStatusTest_;
340 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
343 Teuchos::RCP<Teuchos::ParameterList> params_;
346 MagnitudeType convtol_;
349 MagnitudeType achievedTol_;
373 Teuchos::RCP<Teuchos::Time> timerSolve_;
388 template<
class ScalarType,
class MV,
class OP,
class DM>
389 Teuchos::RCP<const Teuchos::ParameterList>
392 using Teuchos::ParameterList;
393 using Teuchos::parameterList;
396 using Teuchos::rcpFromRef;
397 using Teuchos::EnhancedNumberValidator;
398 typedef MagnitudeType MT;
401 RCP<ParameterList> pl = parameterList (
"MINRES");
403 pl->set (
"Convergence Tolerance", MST::squareroot (MST::eps()),
404 "Relative residual tolerance that needs to be achieved by "
405 "the iterative solver, in order for the linear system to be "
406 "declared converged.",
407 rcp (
new EnhancedNumberValidator<MT> (MST::zero(), MST::rmax())));
408 pl->set (
"Maximum Iterations",
static_cast<int>(1000),
409 "Maximum number of iterations allowed for each right-hand "
411 rcp (
new EnhancedNumberValidator<int> (0, INT_MAX)));
412 pl->set (
"Num Blocks",
static_cast<int> (-1),
413 "Ignored, but permitted, for compatibility with other Belos "
415 pl->set (
"Block Size",
static_cast<int> (1),
416 "Number of vectors in each block. WARNING: The current "
417 "implementation of MINRES only accepts a block size of 1, "
418 "since it can only solve for 1 right-hand side at a time.",
419 rcp (
new EnhancedNumberValidator<int> (1, 1)));
421 "The type(s) of solver information that should "
422 "be written to the output stream.");
424 "What style is used for the solver information written "
425 "to the output stream.");
426 pl->set (
"Output Frequency",
static_cast<int>(-1),
427 "How often (in terms of number of iterations) intermediate "
428 "convergence information should be written to the output stream."
430 pl->set (
"Output Stream", rcpFromRef(std::cout),
431 "A reference-counted pointer to the output stream where all "
432 "solver output is sent. The output stream defaults to stdout.");
433 pl->set (
"Timer Label", std::string(
"Belos"),
434 "The string to use as a prefix for the timer labels.");
441 template<
class ScalarType,
class MV,
class OP,
class DM>
451 parametersSet_ (false)
457 template<
class ScalarType,
class MV,
class OP,
class DM>
460 const Teuchos::RCP<Teuchos::ParameterList>& params) :
463 parametersSet_ (false)
465 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
466 "MinresSolMgr: The version of the constructor "
467 "that takes a LinearProblem to solve was given a "
468 "null LinearProblem.");
472 template<
class ScalarType,
class MV,
class OP,
class DM>
476 TEUCHOS_TEST_FOR_EXCEPTION(problem.is_null(),
478 "MINRES requires that you have provided a nonnull LinearProblem to the "
479 "solver manager, before you call the solve() method.");
480 TEUCHOS_TEST_FOR_EXCEPTION(problem->getOperator().is_null(),
482 "MINRES requires a LinearProblem object with a non-null operator (the "
484 TEUCHOS_TEST_FOR_EXCEPTION(problem->getRHS().is_null(),
486 "MINRES requires a LinearProblem object with a non-null right-hand side.");
487 TEUCHOS_TEST_FOR_EXCEPTION( ! problem->isProblemSet(),
489 "MINRES requires that before you give it a LinearProblem to solve, you "
490 "must first call the linear problem's setProblem() method.");
493 template<
class ScalarType,
class MV,
class OP,
class DM>
495 setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
497 using Teuchos::ParameterList;
498 using Teuchos::parameterList;
501 using Teuchos::rcpFromRef;
503 using Teuchos::is_null;
508 if (params_.is_null()) {
511 RCP<ParameterList> pl = params;
512 pl->validateParametersAndSetDefaults (*params_);
518 blockSize_ = pl->get<
int> (
"Block Size");
519 verbosity_ = pl->get<
int> (
"Verbosity");
520 outputStyle_ = pl->get<
int> (
"Output Style");
521 outputFreq_ = pl->get<
int>(
"Output Frequency");
522 outputStream_ = pl->get<RCP<std::ostream> > (
"Output Stream");
523 convtol_ = pl->get<MagnitudeType> (
"Convergence Tolerance");
524 maxIters_ = pl->get<
int> (
"Maximum Iterations");
532 const string newLabel = pl->get<
string> (
"Timer Label");
534 if (newLabel != label_ || timerSolve_.is_null()) {
536#ifdef BELOS_TEUCHOS_TIME_MONITOR
537 const string solveLabel = label_ +
": MinresSolMgr total solve time";
539 if (! timerSolve_.is_null()) {
540 Teuchos::TimeMonitor::clearCounter (label_);
541 timerSolve_ = Teuchos::null;
543 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
549 bool recreatedPrinter =
false;
550 if (printer_.is_null()) {
552 recreatedPrinter =
true;
555 printer_->setVerbosity (verbosity_);
557 printer_->setOStream (outputStream_);
568 const bool allocatedConvergenceTests =
569 impConvTest_.is_null() || expConvTest_.is_null();
573 if (impConvTest_.is_null()) {
574 impConvTest_ = rcp (
new res_norm_type (convtol_));
575 impConvTest_->defineResForm (res_norm_type::Implicit,
TwoNorm);
580 impConvTest_->setTolerance (convtol_);
585 if (expConvTest_.is_null()) {
586 expConvTest_ = rcp (
new res_norm_type (convtol_));
587 expConvTest_->defineResForm (res_norm_type::Explicit,
TwoNorm);
592 expConvTest_->setTolerance (convtol_);
598 bool needToRecreateFullStatusTest = sTest_.is_null();
602 if (convTest_.is_null() || allocatedConvergenceTests) {
603 convTest_ = rcp (
new combo_type (combo_type::SEQ, impConvTest_, expConvTest_));
604 needToRecreateFullStatusTest =
true;
611 if (maxIterTest_.is_null()) {
613 needToRecreateFullStatusTest =
true;
615 maxIterTest_->setMaxIters (maxIters_);
626 if (needToRecreateFullStatusTest) {
627 sTest_ = rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
634 if (Teuchos::nonnull(debugStatusTest_)) {
635 sTest_ = rcp (
new combo_type (combo_type::OR, sTest_, debugStatusTest_));
642 if (outputTest_.is_null() || needToRecreateFullStatusTest || recreatedPrinter) {
644 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
647 outputTest_->setOutputFrequency (outputFreq_);
651 outputTest_->setSolverDesc (std::string (
" MINRES "));
654 parametersSet_ =
true;
656 if (verbosity_ &
Debug) {
659 std::ostream& dbg = printer_->stream (
Debug);
660 dbg <<
"MINRES parameters:" << endl << params_ << endl;
665 template<
class ScalarType,
class MV,
class OP,
class DM>
670 using Teuchos::rcp_const_cast;
675 if (! parametersSet_) {
678 std::ostream& dbg = printer_->stream (
Debug);
680#ifdef BELOS_TEUCHOS_TIME_MONITOR
681 Teuchos::TimeMonitor solveTimerMonitor (*timerSolve_);
685 validateProblem (problem_);
688 outputTest_->reset();
698 RCP<iter_type> minres_iter =
699 rcp (
new iter_type (problem_, printer_, outputTest_, *params_));
705 std::vector<int> notConverged;
706 std::vector<int> currentIndices(1);
711 for (
int currentRHS = 0; currentRHS < numRHS2Solve; ++currentRHS) {
716 currentIndices[0] = currentRHS;
717 problem_->setLSIndex (currentIndices);
719 dbg <<
"-- Current right-hand side index being solved: "
720 << currentRHS << endl;
723 minres_iter->resetNumIters();
725 outputTest_->resetNumCalls();
732 minres_iter->initializeMinres (newstate);
738 minres_iter->iterate();
741 if (convTest_->getStatus() ==
Passed) {
742 dbg <<
"---- Converged after " << maxIterTest_->getNumIters()
743 <<
" iterations" << endl;
747 else if (maxIterTest_->getStatus() ==
Passed) {
749 dbg <<
"---- Did not converge after " << maxIterTest_->getNumIters()
750 <<
" iterations" << endl;
752 notConverged.push_back (currentRHS);
756 else if (Teuchos::nonnull(debugStatusTest_) &&
757 debugStatusTest_->getStatus() ==
Passed) {
762 notConverged.push_back (currentRHS);
769 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
770 "Belos::MinresSolMgr::solve(): iterations neither converged, "
771 "nor reached the maximum number of iterations " << maxIters_
772 <<
". That means something went wrong.");
778 achievedTol_ = MST::one();
779 Teuchos::RCP<MV> X = problem_->getLHS();
781 printer_->stream(
Warnings) <<
"Belos::MinresSolMgr::solve(): Warning! NaN has been detected!"
785 catch (
const std::exception &e) {
788 <<
"Error! Caught std::exception in MinresIter::iterate() at "
789 <<
"iteration " << minres_iter->getNumIters() << endl
798 problem_->setCurrLS();
802 numIters_ += maxIterTest_->getNumIters();
810#ifdef BELOS_TEUCHOS_TIME_MONITOR
815 Teuchos::TimeMonitor::summarize (printer_->stream (
TimingDetails));
829 const std::vector<MagnitudeType>* pTestValues = expConvTest_->getTestValue();
830 if (pTestValues == NULL || pTestValues->size() < 1) {
831 pTestValues = impConvTest_->getTestValue();
833 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
834 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
835 "method returned NULL. Please report this bug to the Belos developers.");
836 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
837 "Belos::MinresSolMgr::solve(): The implicit convergence test's getTestValue() "
838 "method returned a vector of length zero. Please report this bug to the "
839 "Belos developers.");
844 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
847 if (notConverged.size() > 0) {
855 template<
class ScalarType,
class MV,
class OP,
class DM>
858 std::ostringstream oss;
859 oss <<
"Belos::MinresSolMgr< "
860 << Teuchos::ScalarTraits<ScalarType>::name()
870#ifdef HAVE_BELOS_TPETRA
873#define BELOS_TPETRA_MINRESSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
874 BELOS_TPETRA_CALL(Belos::MinresSolMgr, SC, LO, GO, NT)
876#define BELOS_TPETRA_MINRESSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
877 BELOS_TPETRA_EXTERN_CALL(Belos::MinresSolMgr, SC, LO, GO, NT)
879TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_MINRESSOLMGR_EXTERN_CALL)
Belos header file which uses auto-configuration information to include necessary C++ headers.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
Class which describes the linear problem to be solved by the iterative solver.
MINRES iteration implementation.
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.
BelosError(const std::string &what_arg)
MINRES linear solver solution manager.
static Teuchos::RCP< const Teuchos::ParameterList > defaultParameters()
List of valid MINRES parameters and their default values.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
void reset(const ResetType type) override
Reset the solver manager.
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.
bool isLOADetected() const override
Whether a loss of accuracy was detected in the solver.
virtual ~MinresSolMgr()
Destructor.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Return the list of current parameters for this object.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return all timers for this object.
std::string description() const override
int getNumIters() const override
Get the iteration count for the most recent call to solve().
MinresSolMgr()
Default constructor.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return the list of default parameters for this object.
ReturnType solve() override
Iterate until the status test tells us to stop.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return the linear problem to be solved.
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 to use when solving the linear problem.
This subclass of std::exception may be thrown from the MinresSolMgr::solve() method.
MinresSolMgrLinearProblemFailure(const std::string &what_arg)
Traits class which defines basic operations on multivectors.
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static int GetNumberVecs(const MV &mv)
Class which defines basic traits for the operator type.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
SolverManager()
Empty constructor.
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
A pure virtual class for defining the status tests for the Belos iterative solvers.
A Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP, DM > > create(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > test, int mod, int printStates)
Create the StatusTestOutput object specified by the outputStyle.
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Structure to contain pointers to MinresIteration state variables.
Teuchos::RCP< const MV > Y
The current residual.