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_);
367 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>&
params)
override;
376 sTest_ = Teuchos::null;
390 problem_->setProblem ();
423 std::string description ()
const override;
430 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
432 Teuchos::RCP<OutputManager<ScalarType> > printer_;
434 Teuchos::RCP<std::ostream> outputStream_;
437 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
438 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
439 Teuchos::RCP<LSQRStatusTest<ScalarType,MV,OP,DM> > convTest_;
440 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
441 Teuchos::RCP<StatusTest<ScalarType, MV, OP, DM> > debugStatusTest_;
444 Teuchos::RCP<Teuchos::ParameterList> params_;
451 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
454 MagnitudeType lambda_;
455 MagnitudeType relRhsErr_;
456 MagnitudeType relMatErr_;
457 MagnitudeType condMax_;
458 int maxIters_, termIterMax_;
459 int verbosity_, outputStyle_, outputFreq_;
463 MagnitudeType matCondNum_;
464 MagnitudeType matNorm_;
465 MagnitudeType resNorm_;
466 MagnitudeType matResNorm_;
470 Teuchos::RCP<Teuchos::Time> timerSolve_;
477template<
class ScalarType,
class MV,
class OP,
class DM>
479 lambda_ (STM::
zero ()),
480 relRhsErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
481 relMatErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
482 condMax_ (STM::
one () / STM::
eps ()),
489 matCondNum_ (STM::
zero ()),
490 matNorm_ (STM::
zero ()),
491 resNorm_ (STM::
zero ()),
492 matResNorm_ (STM::
zero ()),
497template<
class ScalarType,
class MV,
class OP,
class DM>
500 const Teuchos::RCP<Teuchos::ParameterList>&
pl) :
502 lambda_ (STM::
zero ()),
503 relRhsErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
504 relMatErr_ (Teuchos::
as<MagnitudeType> (10) * STM::
squareroot (STM::
eps ())),
505 condMax_ (STM::
one () / STM::
eps ()),
512 matCondNum_ (STM::
zero ()),
513 matNorm_ (STM::
zero ()),
514 resNorm_ (STM::
zero ()),
515 matResNorm_ (STM::
zero ()),
526 if (!
pl.is_null ()) {
532template<
class ScalarType,
class MV,
class OP,
class DM>
533Teuchos::RCP<const Teuchos::ParameterList>
536 using Teuchos::ParameterList;
537 using Teuchos::parameterList;
540 using Teuchos::rcpFromRef;
543 if (validParams_.is_null ()) {
547 const MagnitudeType
ten = Teuchos::as<MagnitudeType> (10);
548 const MagnitudeType
sqrtEps = STM::squareroot (STM::eps());
550 const MagnitudeType lambda = STM::zero();
554 const MagnitudeType
condMax = STM::one() / STM::eps();
560 const std::string
label (
"Belos");
563 pl->set (
"Output Stream",
outputStream,
"Teuchos::RCP<std::ostream> "
564 "(reference-counted pointer to the output stream) receiving "
565 "all solver output");
566 pl->set (
"Lambda", lambda,
"Damping parameter");
567 pl->set (
"Rel RHS Err",
relRhsErr,
"Estimates the error in the data "
568 "defining the right-hand side");
569 pl->set (
"Rel Mat Err",
relMatErr,
"Estimates the error in the data "
570 "defining the matrix.");
571 pl->set (
"Condition Limit",
condMax,
"Bounds the estimated condition "
573 pl->set (
"Maximum Iterations",
maxIters,
"Maximum number of iterations");
574 pl->set (
"Term Iter Max",
termIterMax,
"The number of consecutive "
575 "iterations must that satisfy all convergence criteria in order "
576 "for LSQR to stop iterating");
577 pl->set (
"Verbosity",
verbosity,
"Type(s) of solver information written to "
578 "the output stream");
579 pl->set (
"Output Style",
outputStyle,
"Style of solver output");
580 pl->set (
"Output Frequency",
outputFreq,
"Frequency at which information "
581 "is written to the output stream (-1 means \"not at all\")");
582 pl->set (
"Timer Label",
label,
"String to use as a prefix for the timer "
584 pl->set (
"Block Size", 1,
"Block size parameter (currently, LSQR requires "
585 "this must always be 1)");
592template<
class ScalarType,
class MV,
class OP,
class DM>
597 using Teuchos::isParameterType;
598 using Teuchos::getParameter;
600 using Teuchos::ParameterList;
601 using Teuchos::parameterList;
604 using Teuchos::rcp_dynamic_cast;
605 using Teuchos::rcpFromRef;
607 using Teuchos::TimeMonitor;
608 using Teuchos::Exceptions::InvalidParameter;
609 using Teuchos::Exceptions::InvalidParameterName;
610 using Teuchos::Exceptions::InvalidParameterType;
613 (
params.is_null (), std::invalid_argument,
614 "Belos::LSQRSolMgr::setParameters: The input ParameterList is null.");
636 if (
params->isParameter (
"Lambda")) {
637 lambda_ =
params->get<MagnitudeType> (
"Lambda");
638 }
else if (
params->isParameter (
"lambda")) {
639 lambda_ =
params->get<MagnitudeType> (
"lambda");
643 if (
params->isParameter (
"Maximum Iterations")) {
644 maxIters_ =
params->get<
int> (
"Maximum Iterations");
647 (maxIters_ < 0, std::invalid_argument,
"Belos::LSQRSolMgr::setParameters: "
648 "\"Maximum Iterations\" = " << maxIters_ <<
" < 0.");
653 params->isParameter (
"Timer Label") ?
654 params->get<std::string> (
"Timer Label") :
662#ifdef BELOS_TEUCHOS_TIME_MONITOR
664 (
newLabel +
": Belos::LSQRSolMgr total solve time") :
665 std::string (
"Belos::LSQRSolMgr total solve time");
666 if (timerSolve_.is_null ()) {
690 if (
params->isParameter (
"Verbosity")) {
699 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
708 if (
params->isParameter (
"Output Style")) {
709 outputStyle_ =
params->get<
int> (
"Output Style");
716 if (
params->isParameter (
"Output Stream")) {
724 if (outputStream_.is_null ()) {
725 outputStream_ =
rcp (
new Teuchos::oblackholestream ());
730 if (
params->isParameter (
"Output Frequency")) {
731 outputFreq_ =
params->get<
int> (
"Output Frequency");
737 if (printer_.is_null ()) {
740 printer_->setVerbosity (verbosity_);
741 printer_->setOStream (outputStream_);
748 if (
params->isParameter (
"Condition Limit")) {
749 condMax_ =
params->get<MagnitudeType> (
"Condition Limit");
751 if (
params->isParameter (
"Term Iter Max")) {
752 termIterMax_ =
params->get<
int> (
"Term Iter Max");
754 if (
params->isParameter (
"Rel RHS Err")) {
755 relRhsErr_ =
params->get<MagnitudeType> (
"Rel RHS Err");
757 else if (
params->isParameter (
"Convergence Tolerance")) {
760 relRhsErr_ =
params->get<MagnitudeType> (
"Convergence Tolerance");
763 if (
params->isParameter (
"Rel Mat Err")) {
764 relMatErr_ =
params->get<MagnitudeType> (
"Rel Mat Err");
769 if (convTest_.is_null ()) {
772 relRhsErr_, relMatErr_));
774 convTest_->setCondLim (condMax_);
775 convTest_->setTermIterMax (termIterMax_);
776 convTest_->setRelRhsErr (relRhsErr_);
777 convTest_->setRelMatErr (relMatErr_);
784 if (maxIterTest_.is_null()) {
787 maxIterTest_->setMaxIters (maxIters_);
799 if (sTest_.is_null()) {
800 sTest_ =
rcp (
new combo_type (combo_type::OR, maxIterTest_, convTest_));
807 if (Teuchos::nonnull(debugStatusTest_)) {
808 sTest_ =
rcp (
new combo_type (combo_type::OR, sTest_, debugStatusTest_));
811 if (outputTest_.is_null ()) {
815 outputTest_ =
stoFactory.create (printer_, sTest_, outputFreq_,
823 outputTest_->setOutputManager (printer_);
824 outputTest_->setChild (sTest_);
825 outputTest_->setOutputFrequency (outputFreq_);
841template<
class ScalarType,
class MV,
class OP,
class DM>
855 this->setParameters (Teuchos::parameterList (* (getValidParameters ())));
860 "Belos::LSQRSolMgr::solve: The linear problem to solve is null.");
863 "Belos::LSQRSolMgr::solve: The linear problem is not ready, "
864 "as its setProblem() method has not been called.");
866 (MVT::GetNumberVecs (*(problem_->getRHS ())) != 1,
868 "The current implementation of LSQR only knows how to solve problems "
869 "with one right-hand side, but the linear problem to solve has "
870 << MVT::GetNumberVecs (* (problem_->getRHS ()))
871 <<
" right-hand sides.");
891 outputTest_->reset ();
912 Teuchos::ParameterList
plist;
919 plist.set (
"Lambda", lambda_);
924#ifdef BELOS_TEUCHOS_TIME_MONITOR
925 Teuchos::TimeMonitor
slvtimer (*timerSolve_);
931 outputTest_->resetNumCalls ();
947 }
else if (Teuchos::nonnull(debugStatusTest_) &&
957 std::logic_error,
"Belos::LSQRSolMgr::solve: "
958 "LSQRIteration::iterate returned without either the convergence test "
959 "or the maximum iteration count test passing. "
960 "Please report this bug to the Belos developers.");
962 }
catch (
const std::exception&
e) {
965 <<
"Error! Caught std::exception in LSQRIter::iterate at iteration "
966 <<
lsqr_iter->getNumIters () << std::endl <<
e.what () << std::endl;
971 problem_->setCurrLS();
977#ifdef BELOS_TEUCHOS_TIME_MONITOR
986 numIters_ = maxIterTest_->getNumIters();
987 matCondNum_ = convTest_->getMatCondNum();
988 matNorm_ = convTest_->getMatNorm();
989 resNorm_ = convTest_->getResidNorm();
990 matResNorm_ = convTest_->getLSResidNorm();
1000template<
class ScalarType,
class MV,
class OP,
class DM>
1003 std::ostringstream
oss;
1004 oss <<
"LSQRSolMgr<...," << STS::name () <<
">";
1006 oss <<
"Lambda: " << lambda_;
1007 oss <<
", condition number limit: " << condMax_;
1008 oss <<
", relative RHS Error: " << relRhsErr_;
1009 oss <<
", relative Matrix Error: " << relMatErr_;
1010 oss <<
", maximum number of iterations: " << maxIters_;
1011 oss <<
", termIterMax: " << termIterMax_;
1018#ifdef HAVE_BELOS_TPETRA
1021#define BELOS_TPETRA_LSQRSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
1022 BELOS_TPETRA_CALL(Belos::LSQRSolMgr, SC, LO, GO, NT)
1024#define BELOS_TPETRA_LSQRSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
1025 BELOS_TPETRA_EXTERN_CALL(Belos::LSQRSolMgr, SC, LO, GO, NT)
1027TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_LSQRSOLMGR_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....
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 setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &debugStatusTest) override
Set a debug status test, OR-combined into the top-level status test.
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.