10#ifndef BELOS_LSQR_SOLMGR_HPP
11#define BELOS_LSQR_SOLMGR_HPP
31#include "Teuchos_as.hpp"
33#ifdef BELOS_TEUCHOS_TIME_MONITOR
34#include "Teuchos_TimeMonitor.hpp"
187template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>,
188 const
bool scalarTypeIsComplex = Teuchos::ScalarTraits<ScalarType>::isComplex>
191 Teuchos::ScalarTraits<ScalarType>::isComplex>
193 static const bool isComplex = Teuchos::ScalarTraits<ScalarType>::isComplex;
201 const Teuchos::RCP<Teuchos::ParameterList> &
pl) :
207 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
216template<
class ScalarType,
class MV,
class OP,
class DM>
222 typedef Teuchos::ScalarTraits<ScalarType> STS;
223 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
224 typedef Teuchos::ScalarTraits<MagnitudeType> STM;
267 const Teuchos::RCP<Teuchos::ParameterList>&
pl);
273 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
288 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const override;
301 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers ()
const {
302 return Teuchos::tuple (timerSolve_);
364 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>&
params)
override;
376 problem_->setProblem ();
409 std::string description ()
const override;
416 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
418 Teuchos::RCP<OutputManager<ScalarType> > printer_;
420 Teuchos::RCP<std::ostream> outputStream_;
423 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
424 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
425 Teuchos::RCP<LSQRStatusTest<ScalarType,MV,OP,DM> > convTest_;
426 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
429 Teuchos::RCP<Teuchos::ParameterList> params_;
436 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
439 MagnitudeType lambda_;
440 MagnitudeType relRhsErr_;
441 MagnitudeType relMatErr_;
442 MagnitudeType condMax_;
443 int maxIters_, termIterMax_;
444 int verbosity_, outputStyle_, outputFreq_;
448 MagnitudeType matCondNum_;
449 MagnitudeType matNorm_;
450 MagnitudeType resNorm_;
451 MagnitudeType matResNorm_;
455 Teuchos::RCP<Teuchos::Time> timerSolve_;
462template<
class ScalarType,
class MV,
class OP,
class DM>
464 lambda_ (STM::
zero ()),
465 relRhsErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
466 relMatErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
467 condMax_ (STM::
one () / STM::
eps ()),
474 matCondNum_ (STM::
zero ()),
475 matNorm_ (STM::
zero ()),
476 resNorm_ (STM::
zero ()),
477 matResNorm_ (STM::
zero ()),
482template<
class ScalarType,
class MV,
class OP,
class DM>
485 const Teuchos::RCP<Teuchos::ParameterList>&
pl) :
487 lambda_ (STM::
zero ()),
488 relRhsErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
489 relMatErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
490 condMax_ (STM::
one () / STM::
eps ()),
497 matCondNum_ (STM::
zero ()),
498 matNorm_ (STM::
zero ()),
499 resNorm_ (STM::
zero ()),
500 matResNorm_ (STM::
zero ()),
511 if (!
pl.is_null ()) {
517template<
class ScalarType,
class MV,
class OP,
class DM>
518Teuchos::RCP<const Teuchos::ParameterList>
521 using Teuchos::ParameterList;
522 using Teuchos::parameterList;
525 using Teuchos::rcpFromRef;
528 if (validParams_.is_null ()) {
532 const MagnitudeType
ten = Teuchos::as<MagnitudeType> (10);
533 const MagnitudeType
sqrtEps = STM::squareroot (STM::eps());
535 const MagnitudeType lambda = STM::zero();
539 const MagnitudeType
condMax = STM::one() / STM::eps();
545 const std::string
label (
"Belos");
548 pl->set (
"Output Stream",
outputStream,
"Teuchos::RCP<std::ostream> "
549 "(reference-counted pointer to the output stream) receiving "
550 "all solver output");
551 pl->set (
"Lambda", lambda,
"Damping parameter");
552 pl->set (
"Rel RHS Err",
relRhsErr,
"Estimates the error in the data "
553 "defining the right-hand side");
554 pl->set (
"Rel Mat Err",
relMatErr,
"Estimates the error in the data "
555 "defining the matrix.");
556 pl->set (
"Condition Limit",
condMax,
"Bounds the estimated condition "
558 pl->set (
"Maximum Iterations",
maxIters,
"Maximum number of iterations");
559 pl->set (
"Term Iter Max",
termIterMax,
"The number of consecutive "
560 "iterations must that satisfy all convergence criteria in order "
561 "for LSQR to stop iterating");
562 pl->set (
"Verbosity",
verbosity,
"Type(s) of solver information written to "
563 "the output stream");
564 pl->set (
"Output Style",
outputStyle,
"Style of solver output");
565 pl->set (
"Output Frequency",
outputFreq,
"Frequency at which information "
566 "is written to the output stream (-1 means \"not at all\")");
567 pl->set (
"Timer Label",
label,
"String to use as a prefix for the timer "
569 pl->set (
"Block Size", 1,
"Block size parameter (currently, LSQR requires "
570 "this must always be 1)");
577template<
class ScalarType,
class MV,
class OP,
class DM>
582 using Teuchos::isParameterType;
583 using Teuchos::getParameter;
585 using Teuchos::ParameterList;
586 using Teuchos::parameterList;
589 using Teuchos::rcp_dynamic_cast;
590 using Teuchos::rcpFromRef;
592 using Teuchos::TimeMonitor;
593 using Teuchos::Exceptions::InvalidParameter;
594 using Teuchos::Exceptions::InvalidParameterName;
595 using Teuchos::Exceptions::InvalidParameterType;
598 (
params.is_null (), std::invalid_argument,
599 "Belos::LSQRSolMgr::setParameters: The input ParameterList is null.");
621 if (
params->isParameter (
"Lambda")) {
622 lambda_ =
params->get<MagnitudeType> (
"Lambda");
623 }
else if (
params->isParameter (
"lambda")) {
624 lambda_ =
params->get<MagnitudeType> (
"lambda");
628 if (
params->isParameter (
"Maximum Iterations")) {
629 maxIters_ =
params->get<
int> (
"Maximum Iterations");
632 (maxIters_ < 0, std::invalid_argument,
"Belos::LSQRSolMgr::setParameters: "
633 "\"Maximum Iterations\" = " << maxIters_ <<
" < 0.");
638 params->isParameter (
"Timer Label") ?
639 params->get<std::string> (
"Timer Label") :
647#ifdef BELOS_TEUCHOS_TIME_MONITOR
649 (
newLabel +
": Belos::LSQRSolMgr total solve time") :
650 std::string (
"Belos::LSQRSolMgr total solve time");
651 if (timerSolve_.is_null ()) {
675 if (
params->isParameter (
"Verbosity")) {
684 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
693 if (
params->isParameter (
"Output Style")) {
694 outputStyle_ =
params->get<
int> (
"Output Style");
701 if (
params->isParameter (
"Output Stream")) {
709 if (outputStream_.is_null ()) {
710 outputStream_ =
rcp (
new Teuchos::oblackholestream ());
715 if (
params->isParameter (
"Output Frequency")) {
716 outputFreq_ =
params->get<
int> (
"Output Frequency");
722 if (printer_.is_null ()) {
725 printer_->setVerbosity (verbosity_);
726 printer_->setOStream (outputStream_);
733 if (
params->isParameter (
"Condition Limit")) {
734 condMax_ =
params->get<MagnitudeType> (
"Condition Limit");
736 if (
params->isParameter (
"Term Iter Max")) {
737 termIterMax_ =
params->get<
int> (
"Term Iter Max");
739 if (
params->isParameter (
"Rel RHS Err")) {
740 relRhsErr_ =
params->get<MagnitudeType> (
"Rel RHS Err");
742 else if (
params->isParameter (
"Convergence Tolerance")) {
745 relRhsErr_ =
params->get<MagnitudeType> (
"Convergence Tolerance");
748 if (
params->isParameter (
"Rel Mat Err")) {
749 relMatErr_ =
params->get<MagnitudeType> (
"Rel Mat Err");
754 if (convTest_.is_null ()) {
757 relRhsErr_, relMatErr_));
759 convTest_->setCondLim (condMax_);
760 convTest_->setTermIterMax (termIterMax_);
761 convTest_->setRelRhsErr (relRhsErr_);
762 convTest_->setRelMatErr (relMatErr_);
769 if (maxIterTest_.is_null()) {
772 maxIterTest_->setMaxIters (maxIters_);
784 if (sTest_.is_null()) {
785 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
788 if (outputTest_.is_null ()) {
792 outputTest_ =
stoFactory.create (printer_, sTest_, outputFreq_,
800 outputTest_->setOutputManager (printer_);
801 outputTest_->setChild (sTest_);
802 outputTest_->setOutputFrequency (outputFreq_);
818template<
class ScalarType,
class MV,
class OP,
class DM>
832 this->setParameters (Teuchos::parameterList (* (getValidParameters ())));
837 "Belos::LSQRSolMgr::solve: The linear problem to solve is null.");
840 "Belos::LSQRSolMgr::solve: The linear problem is not ready, "
841 "as its setProblem() method has not been called.");
843 (MVT::GetNumberVecs (*(problem_->getRHS ())) != 1,
845 "The current implementation of LSQR only knows how to solve problems "
846 "with one right-hand side, but the linear problem to solve has "
847 << MVT::GetNumberVecs (* (problem_->getRHS ()))
848 <<
" right-hand sides.");
868 outputTest_->reset ();
889 Teuchos::ParameterList
plist;
896 plist.set (
"Lambda", lambda_);
901#ifdef BELOS_TEUCHOS_TIME_MONITOR
902 Teuchos::TimeMonitor
slvtimer (*timerSolve_);
908 outputTest_->resetNumCalls ();
927 std::logic_error,
"Belos::LSQRSolMgr::solve: "
928 "LSQRIteration::iterate returned without either the convergence test "
929 "or the maximum iteration count test passing. "
930 "Please report this bug to the Belos developers.");
932 }
catch (
const std::exception&
e) {
935 <<
"Error! Caught std::exception in LSQRIter::iterate at iteration "
936 <<
lsqr_iter->getNumIters () << std::endl <<
e.what () << std::endl;
941 problem_->setCurrLS();
947#ifdef BELOS_TEUCHOS_TIME_MONITOR
956 numIters_ = maxIterTest_->getNumIters();
957 matCondNum_ = convTest_->getMatCondNum();
958 matNorm_ = convTest_->getMatNorm();
959 resNorm_ = convTest_->getResidNorm();
960 matResNorm_ = convTest_->getLSResidNorm();
970template<
class ScalarType,
class MV,
class OP,
class DM>
973 std::ostringstream
oss;
974 oss <<
"LSQRSolMgr<...," << STS::name () <<
">";
976 oss <<
"Lambda: " << lambda_;
977 oss <<
", condition number limit: " << condMax_;
978 oss <<
", relative RHS Error: " << relRhsErr_;
979 oss <<
", relative Matrix Error: " << relMatErr_;
980 oss <<
", maximum number of iterations: " << maxIters_;
981 oss <<
", termIterMax: " << termIterMax_;
Belos header file which uses auto-configuration information to include necessary C++ headers.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
Belos concrete class that iterates LSQR.
IterationState contains the data that defines the state of the LSQR solver at any given time.
Belos::StatusTest class defining LSQR convergence.
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::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.
Base class for Belos::SolverManager subclasses which normally can only compile for real ScalarType.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
bool isLOADetected() const override
Whether a loss of accuracy was detected during the last solve.
MagnitudeType getMatNorm() const
Estimated matrix Frobenius norm from the last solve.
virtual ~LSQRSolMgr()
Destructor (declared virtual for memory safety of base classes).
int getNumIters() const override
Iteration count from the last solve.
MagnitudeType getMatResNorm() const
Estimate of (residual vector ) from the last solve.
MagnitudeType getResNorm() const
Estimated residual norm from the last solve.
MagnitudeType getMatCondNum() const
Estimated matrix condition number from the last solve.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
void reset(const ResetType type) override
reset the solver manager as specified by the ResetType, informs the solver manager that the solver sh...
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.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Get current linear problem being solved for in this object.
LSQRSolMgrBlockSizeFailure is thrown when the linear problem has more than one RHS.
LSQRSolMgrBlockSizeFailure(const std::string &what_arg)
LSQR method (for linear systems and linear least-squares problems).
LSQRSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
Belos::LSQRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
LSQRSolMgrLinearProblemFailure(const std::string &what_arg)
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
MsgType
Available message types recognized by the linear solvers.
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.