42 #ifndef BELOS_LSQR_SOLMGR_HPP 43 #define BELOS_LSQR_SOLMGR_HPP 62 #include "Teuchos_as.hpp" 63 #include "Teuchos_BLAS.hpp" 64 #include "Teuchos_LAPACK.hpp" 66 #ifdef BELOS_TEUCHOS_TIME_MONITOR 67 #include "Teuchos_TimeMonitor.hpp" 217 template<
class ScalarType,
class MV,
class OP>
223 typedef Teuchos::ScalarTraits<ScalarType> SCT;
224 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
225 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
268 const Teuchos::RCP<Teuchos::ParameterList> &pl );
285 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const;
296 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
297 return Teuchos::tuple(timerSolve_);
357 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
399 std::string description()
const;
406 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
409 Teuchos::RCP<OutputManager<ScalarType> > printer_;
410 Teuchos::RCP<std::ostream> outputStream_;
413 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
414 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
415 Teuchos::RCP<LSQRStatusTest<ScalarType,MV,OP> > convTest_;
416 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
419 Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > ortho_;
422 Teuchos::RCP<Teuchos::ParameterList> params_;
428 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
431 MagnitudeType lambda_;
432 MagnitudeType relRhsErr_;
433 MagnitudeType relMatErr_;
434 MagnitudeType condMax_;
435 int maxIters_, termIterMax_;
436 std::string orthoType_;
437 MagnitudeType orthoKappa_;
438 int verbosity_, outputStyle_, outputFreq_;
442 MagnitudeType matCondNum_;
443 MagnitudeType matNorm_;
444 MagnitudeType resNorm_;
445 MagnitudeType matResNorm_;
449 Teuchos::RCP<Teuchos::Time> timerSolve_;
457 template<
class ScalarType,
class MV,
class OP>
465 template<
class ScalarType,
class MV,
class OP>
468 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
480 if (! is_null (pl)) {
486 template<
class ScalarType,
class MV,
class OP>
487 Teuchos::RCP<const Teuchos::ParameterList>
490 using Teuchos::ParameterList;
491 using Teuchos::parameterList;
494 using Teuchos::rcpFromRef;
495 typedef Teuchos::ScalarTraits<MagnitudeType> STM;
498 if (is_null (validParams_)) {
502 const MagnitudeType ten = Teuchos::as<MagnitudeType> (10);
503 const MagnitudeType sqrtEps = STM::squareroot (STM::eps());
505 const MagnitudeType lambda = STM::zero();
506 RCP<std::ostream> outputStream = rcpFromRef (std::cout);
507 const MagnitudeType relRhsErr = ten * sqrtEps;
508 const MagnitudeType relMatErr = ten * sqrtEps;
509 const MagnitudeType condMax = STM::one() / STM::eps();
510 const int maxIters = 1000;
511 const int termIterMax = 1;
512 const std::string orthoType (
"DGKS");
513 const MagnitudeType orthoKappa = Teuchos::as<MagnitudeType> (-1.0);
516 const int outputFreq = -1;
517 const std::string label (
"Belos");
519 RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
520 pl->set(
"Output Stream", outputStream,
521 "is a reference-counted pointer to the output stream receiving\n" 522 "all solver output.");
523 pl->set(
"Lambda", lambda,
"is the damping parameter.");
524 pl->set(
"Rel RHS Err", relRhsErr,
525 "estimates the error in the data defining the right-\n" 527 pl->set(
"Rel Mat Err", relMatErr,
528 "estimates the error in the data defining the matrix.");
529 pl->set(
"Condition Limit", condMax,
530 "bounds the estimated condition number of Abar.");
531 pl->set(
"Maximum Iterations", maxIters,
532 "allows at most the maximum number of iterations.");
533 pl->set(
"Term Iter Max", termIterMax,
534 "consecutive iterations meeting thresholds are necessary for\n" 536 pl->set(
"Orthogonalization", orthoType,
537 "uses orthogonalization of either DGKS, ICGS, IMGS, or TSQR.");
540 pl->set(
"Orthogonalization", orthoType,
541 "refers to the orthogonalization method to use. Valid " 543 RCP<const ParameterList> orthoParams =
545 pl->set (
"Orthogonalization Parameters", *orthoParams,
546 "Parameters specific to the type of orthogonalization used.");
548 pl->set(
"Orthogonalization Constant", orthoKappa,
549 "is the threshold used by DGKS orthogonalization to determine\n" 550 "whether or not to repeat classical Gram-Schmidt. This parameter\n" 551 "is ignored if \"Orthogonalization\" is not \"DGKS\".");
552 pl->set(
"Verbosity", verbosity,
553 "type(s) of solver information are outputted to the output\n" 555 pl->set(
"Output Style", outputStyle,
556 "the style used for the solver information outputted to the\n" 558 pl->set(
"Output Frequency", outputFreq,
559 "is the frequency at which information is written to the\n" 561 pl->set(
"Timer Label", label,
562 "is the string to use as a prefix for the timer labels.");
564 pl->set(
"Block Size", 1,
"Block size parameter (currently, this must always be 1).");
572 template<
class ScalarType,
class MV,
class OP>
577 using Teuchos::isParameterType;
578 using Teuchos::getParameter;
580 using Teuchos::ParameterList;
581 using Teuchos::parameterList;
584 using Teuchos::rcp_dynamic_cast;
585 using Teuchos::rcpFromRef;
587 using Teuchos::TimeMonitor;
588 using Teuchos::Exceptions::InvalidParameter;
589 using Teuchos::Exceptions::InvalidParameterName;
590 using Teuchos::Exceptions::InvalidParameterType;
592 TEUCHOS_TEST_FOR_EXCEPTION(params.is_null(), std::invalid_argument,
593 "Belos::LSQRSolMgr::setParameters: " 594 "the input ParameterList is null.");
596 params->validateParametersAndSetDefaults (*defaultParams);
604 lambda_ = params->get<MagnitudeType> (
"Lambda");
607 maxIters_ = params->get<
int>(
"Maximum Iterations");
611 const std::string newLabel = params->get<std::string>(
"Timer Label");
614 if (newLabel != label_) {
618 #ifdef BELOS_TEUCHOS_TIME_MONITOR 619 std::string newSolveLabel = label_ +
": LSQRSolMgr total solve time";
620 if (timerSolve_.is_null()) {
622 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
631 const std::string oldSolveLabel = timerSolve_->name ();
633 if (oldSolveLabel != newSolveLabel) {
636 TimeMonitor::clearCounter (oldSolveLabel);
637 timerSolve_ = TimeMonitor::getNewCounter (newSolveLabel);
640 #endif // BELOS_TEUCHOS_TIME_MONITOR 645 int newVerbosity = 0;
653 }
catch (Teuchos::Exceptions::InvalidParameterType&) {
654 newVerbosity = params->get<
int> (
"Verbosity");
656 if (newVerbosity != verbosity_) {
657 verbosity_ = newVerbosity;
662 outputStyle_ = params->get<
int> (
"Output Style");
669 outputStream_ = params->get<RCP<std::ostream> > (
"Output Stream");
676 if (outputStream_.is_null())
677 outputStream_ = rcp (
new Teuchos::oblackholestream);
682 outputFreq_ = params->get<
int> (
"Output Frequency");
688 if (printer_.is_null()) {
691 printer_->setVerbosity (verbosity_);
692 printer_->setOStream (outputStream_);
698 factory_type factory;
699 bool mustMakeOrtho =
false;
701 std::string tempOrthoType;
703 tempOrthoType = params_->get<std::string> (
"Orthogonalization");
704 }
catch (InvalidParameterName&) {
705 tempOrthoType = orthoType_;
707 if (ortho_.is_null() || tempOrthoType != orthoType_) {
708 mustMakeOrtho =
true;
711 if (! factory.isValidName (tempOrthoType)) {
712 std::ostringstream os;
713 os <<
"Belos::LSQRSolMgr: Invalid orthogonalization name \"" 714 << tempOrthoType <<
"\". The following are valid options " 715 <<
"for the \"Orthogonalization\" name parameter: ";
716 factory.printValidNames (os);
717 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument, os.str());
719 orthoType_ = tempOrthoType;
720 params_->set (
"Orthogonalization", orthoType_);
736 RCP<ParameterList> orthoParams;
739 using Teuchos::sublist;
741 const std::string paramName (
"Orthogonalization Parameters");
744 orthoParams = sublist (params_, paramName,
true);
745 }
catch (InvalidParameter&) {
751 params_->set (paramName, factory.getDefaultParameters (orthoType_));
752 orthoParams = sublist (params_, paramName,
true);
755 TEUCHOS_TEST_FOR_EXCEPTION(orthoParams.is_null(), std::logic_error,
756 "Failed to get orthogonalization parameters. " 757 "Please report this bug to the Belos developers.");
784 RCP<const OP> M = null;
785 ortho_ = factory.makeMatOrthoManager (orthoType_, M, printer_,
786 label_, orthoParams);
788 TEUCHOS_TEST_FOR_EXCEPTION(ortho_.is_null(), std::logic_error,
789 "The MatOrthoManager is not yet initialized, but " 790 "should be by this point. " 791 "Please report this bug to the Belos developers.");
796 if (orthoType_ ==
"DGKS") {
797 if (params->isParameter (
"Orthogonalization Constant")) {
798 orthoKappa_ = params_->get<MagnitudeType> (
"Orthogonalization Constant");
800 if (orthoKappa_ > 0 && ! ortho_.is_null()) {
802 rcp_dynamic_cast<ortho_impl_type> (ortho_)->setDepTol (orthoKappa_);
811 condMax_ = params->get<MagnitudeType> (
"Condition Limit");
812 termIterMax_ = params->get<
int>(
"Term Iter Max");
813 relRhsErr_ = params->get<MagnitudeType> (
"Rel RHS Err");
814 relMatErr_ = params->get<MagnitudeType> (
"Rel Mat Err");
818 if (convTest_.is_null()) {
821 relRhsErr_, relMatErr_));
823 convTest_->setCondLim (condMax_);
824 convTest_->setTermIterMax (termIterMax_);
825 convTest_->setRelRhsErr (relRhsErr_);
826 convTest_->setRelMatErr (relMatErr_);
833 if (maxIterTest_.is_null()) {
836 maxIterTest_->setMaxIters (maxIters_);
848 if (sTest_.is_null()) {
849 sTest_ = rcp (
new StatusTestCombo_t (StatusTestCombo_t::OR,
854 if (outputTest_.is_null()) {
858 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
861 const std::string solverDesc =
" LSQR ";
862 outputTest_->setSolverDesc (solverDesc);
866 outputTest_->setOutputManager (printer_);
867 outputTest_->setChild (sTest_);
868 outputTest_->setOutputFrequency (outputFreq_);
880 template<
class ScalarType,
class MV,
class OP>
894 "The linear problem to solve is null.");
896 "LSQRSolMgr::solve(): The linear problem is not ready, " 897 "as its setProblem() method has not been called.");
900 "LSQRSolMgr::solve(): The current implementation of LSQR " 901 "only knows how to solve problems with one right-hand " 902 "side, but the linear problem to solve has " 904 <<
" right-hand sides.");
920 std::vector<int> currRHSIdx(1, 0);
921 problem_->setLSIndex(currRHSIdx);
924 outputTest_->reset();
928 bool isConverged =
false;
945 Teuchos::ParameterList plist;
952 plist.set (
"Lambda", lambda_);
955 RCP<iter_type> lsqr_iter =
956 rcp (
new iter_type (problem_, printer_, outputTest_, plist));
957 #ifdef BELOS_TEUCHOS_TIME_MONITOR 958 Teuchos::TimeMonitor slvtimer(*timerSolve_);
962 lsqr_iter->resetNumIters();
964 outputTest_->resetNumCalls();
967 lsqr_iter->initializeLSQR(newstate);
970 lsqr_iter->iterate();
980 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
981 "LSQRSolMgr::solve(): LSQRIteration::iterate() " 982 "returned without either the convergence test or " 983 "the maximum iteration count test passing." 984 "Please report this bug to the Belos developers.");
986 }
catch (
const std::exception &e) {
987 printer_->stream(
Belos::Errors) <<
"Error! Caught std::exception in LSQRIter::iterate() at iteration " 988 << lsqr_iter->getNumIters() << std::endl
989 << e.what() << std::endl;
994 problem_->setCurrLS();
1000 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1006 #endif // BELOS_TEUCHOS_TIME_MONITOR 1009 numIters_ = maxIterTest_->getNumIters();
1010 matCondNum_ = convTest_->getMatCondNum();
1011 matNorm_ = convTest_->getMatNorm();
1012 resNorm_ = convTest_->getResidNorm();
1013 matResNorm_ = convTest_->getLSResidNorm();
1015 if (! isConverged) {
1023 template<
class ScalarType,
class MV,
class OP>
1026 std::ostringstream oss;
1027 oss <<
"LSQRSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
1029 oss <<
"Ortho Type='"<<orthoType_<<
"'";
1030 oss <<
", Lambda="<< lambda_;
1031 oss <<
", condition number limit="<< condMax_;
1032 oss <<
", relative RHS Error="<< relRhsErr_;
1033 oss <<
", relative Matrix Error="<< relMatErr_;
1034 oss <<
", maximum number of iterations="<< maxIters_;
1035 oss <<
", termIterMax="<<termIterMax_;
Collection of types and exceptions used within the Belos solvers.
IterationState contains the data that defines the state of the LSQR solver at any given time...
Belos concrete class that iterates LSQR.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
Class which manages the output and verbosity of the Belos solvers.
LSQRSolMgrBlockSizeFailure(const std::string &what_arg)
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
MagnitudeType getMatResNorm() const
Estimate of (residual vector ) from the last solve.
LSQRSolMgrBlockSizeFailure is thrown when the linear problem has more than one RHS.
MsgType
Available message types recognized by the linear solvers.
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< const Teuchos::ParameterList > getDefaultParameters(const std::string &name) const
Default parameters for the given MatOrthoManager subclass.
Belos::StatusTest class for specifying a maximum number of iterations.
Belos::StatusTest class defining LSQR convergence.
std::string description() const
Method to return description of the LSQR solver manager.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
A factory class for generating StatusTestOutput objects.
std::string validNamesString() const
List (as a string) of recognized MatOrthoManager names.
void reset(const ResetType type)
reset the solver manager as specified by the ResetType, informs the solver manager that the solver sh...
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
Belos::LSQRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
LSQRSolMgrOrthoFailure(const std::string &what_arg)
LSQRSolMgr()
Empty constructor for LSQRSolMgr. This constructor takes no arguments and sets the default values for...
Pure virtual base class which describes the basic interface for a solver manager. ...
int getNumIters() const
Iteration count from the last solve.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
ReturnType
Whether the Belos solve converged for all linear systems.
virtual ~LSQRSolMgr()
Destructor.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
ReturnType solve()
method that performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
MagnitudeType getResNorm() const
Estimated residual norm from the last solve.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > create(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test, int mod, int printStates)
Create the StatusTestOutput object specified by the outputStyle.
LSQRSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to generate orthonormal...
bool isLOADetected() const
Whether a loss of accuracy was detected during the last solve.
An implementation of the Belos::MatOrthoManager that performs orthogonalization using (potentially) m...
A class for extending the status testing capabilities of Belos via logical combinations.
MagnitudeType getMatCondNum() const
Estimated matrix condition number from the last solve.
LSQR method (for linear systems and linear least-squares problems).
Class which defines basic traits for the operator type.
Implementation of the LSQR iteration.
Parent class to all Belos exceptions.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters for this object.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get current linear problem being solved for in this object.
Structure to contain pointers to LSQRIteration state variables, ...
Belos header file which uses auto-configuration information to include necessary C++ headers...
MagnitudeType getMatNorm() const
Estimated matrix Frobenius norm from the last solve.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
LSQRSolMgrLinearProblemFailure(const std::string &what_arg)
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters the solver manager should use to solve the linear problem.