10#ifndef BELOS_BICGSTAB_SOLMGR_HPP
11#define BELOS_BICGSTAB_SOLMGR_HPP
32#ifdef BELOS_TEUCHOS_TIME_MONITOR
33#include "Teuchos_TimeMonitor.hpp"
67 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int, ScalarType>>
73 typedef Teuchos::ScalarTraits<ScalarType> SCT;
74 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
75 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
99 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
105 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
130 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
131 return Teuchos::tuple(timerSolve_);
177 sTest_ = Teuchos::null;
178 outputTest_ = Teuchos::null;
227 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
230 Teuchos::RCP<OutputManager<ScalarType> > printer_;
231 Teuchos::RCP<std::ostream> outputStream_;
234 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
235 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
236 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
237 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
238 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > debugStatusTest_;
241 Teuchos::RCP<Teuchos::ParameterList> params_;
248 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
251 static constexpr int maxIters_default_ = 1000;
252 static constexpr bool showMaxResNormOnly_default_ =
false;
255 static constexpr int outputFreq_default_ = -1;
256 static constexpr int defQuorum_default_ = 1;
257 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
258 static constexpr const char * label_default_ =
"Belos";
261 MagnitudeType convtol_,achievedTol_;
262 int maxIters_, numIters_;
263 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
264 bool showMaxResNormOnly_;
265 std::string resScale_;
269 Teuchos::RCP<Teuchos::Time> timerSolve_;
276template<
class ScalarType,
class MV,
class OP,
class DM>
280 maxIters_(maxIters_default_),
282 verbosity_(verbosity_default_),
283 outputStyle_(outputStyle_default_),
284 outputFreq_(outputFreq_default_),
285 defQuorum_(defQuorum_default_),
286 showMaxResNormOnly_(showMaxResNormOnly_default_),
287 resScale_(resScale_default_),
288 label_(label_default_),
293template<
class ScalarType,
class MV,
class OP,
class DM>
296 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
300 maxIters_(maxIters_default_),
302 verbosity_(verbosity_default_),
303 outputStyle_(outputStyle_default_),
304 outputFreq_(outputFreq_default_),
305 defQuorum_(defQuorum_default_),
306 showMaxResNormOnly_(showMaxResNormOnly_default_),
307 resScale_(resScale_default_),
308 label_(label_default_),
312 problem_.is_null (), std::invalid_argument,
313 "Belos::BiCGStabSolMgr two-argument constructor: "
314 "'problem' is null. You must supply a non-null Belos::LinearProblem "
315 "instance when calling this constructor.");
317 if (!
pl.is_null ()) {
323template<
class ScalarType,
class MV,
class OP,
class DM>
326 using Teuchos::ParameterList;
327 using Teuchos::parameterList;
333 if (params_.is_null()) {
340 if (
params->isParameter(
"Maximum Iterations")) {
341 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
344 params_->set(
"Maximum Iterations", maxIters_);
345 if (maxIterTest_!=Teuchos::null)
346 maxIterTest_->setMaxIters( maxIters_ );
350 if (
params->isParameter(
"Timer Label")) {
356 params_->set(
"Timer Label", label_);
357 std::string
solveLabel = label_ +
": BiCGStabSolMgr total solve time";
358#ifdef BELOS_TEUCHOS_TIME_MONITOR
359 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
365 if (
params->isParameter(
"Verbosity")) {
366 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
367 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
369 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
373 params_->set(
"Verbosity", verbosity_);
374 if (printer_ != Teuchos::null)
375 printer_->setVerbosity(verbosity_);
379 if (
params->isParameter(
"Output Style")) {
380 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
381 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
383 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
387 params_->set(
"Output Style", outputStyle_);
388 outputTest_ = Teuchos::null;
392 if (
params->isParameter(
"Output Stream")) {
393 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
396 params_->set(
"Output Stream", outputStream_);
397 if (printer_ != Teuchos::null)
398 printer_->setOStream( outputStream_ );
403 if (
params->isParameter(
"Output Frequency")) {
404 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
408 params_->set(
"Output Frequency", outputFreq_);
409 if (outputTest_ != Teuchos::null)
410 outputTest_->setOutputFrequency( outputFreq_ );
414 if (printer_ == Teuchos::null) {
423 if (
params->isParameter(
"Convergence Tolerance")) {
424 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
425 convtol_ =
params->get (
"Convergence Tolerance",
433 params_->set(
"Convergence Tolerance", convtol_);
434 if (convTest_ != Teuchos::null)
435 convTest_->setTolerance( convtol_ );
438 if (
params->isParameter(
"Show Maximum Residual Norm Only")) {
439 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*
params,
"Show Maximum Residual Norm Only");
442 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
443 if (convTest_ != Teuchos::null)
444 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
455 if (
params->isParameter (
"Residual Scaling")) {
458 else if (
params->isParameter (
"Implicit Residual Scaling")) {
471 params_->set (
"Implicit Residual Scaling", resScale_);
474 params_->set (
"Residual Scaling", resScale_);
477 if (! convTest_.is_null()) {
481 catch (std::exception&
e) {
490 if (
params->isParameter(
"Deflation Quorum")) {
491 defQuorum_ =
params->get(
"Deflation Quorum", defQuorum_);
492 params_->set(
"Deflation Quorum", defQuorum_);
493 if (convTest_ != Teuchos::null)
494 convTest_->setQuorum( defQuorum_ );
500 if (maxIterTest_ == Teuchos::null)
504 if (convTest_ == Teuchos::null ||
newResTest) {
505 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
510 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
516 if (Teuchos::nonnull(debugStatusTest_)) {
517 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, sTest_, debugStatusTest_ ) );
521 if (outputTest_ == Teuchos::null ||
newResTest) {
529 std::string
solverDesc =
" Pseudo Block BiCGStab ";
535 if (timerSolve_ == Teuchos::null) {
536 std::string
solveLabel = label_ +
": BiCGStabSolMgr total solve time";
537#ifdef BELOS_TEUCHOS_TIME_MONITOR
538 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
547template<
class ScalarType,
class MV,
class OP,
class DM>
548Teuchos::RCP<const Teuchos::ParameterList>
551 using Teuchos::ParameterList;
552 using Teuchos::parameterList;
555 if (validParams_.is_null()) {
562 "The relative residual tolerance that needs to be achieved by the\n"
563 "iterative solver in order for the linera system to be declared converged.");
564 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
565 "The maximum number of block iterations allowed for each\n"
566 "set of RHS solved.");
567 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
568 "What type(s) of solver information should be outputted\n"
569 "to the output stream.");
570 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
571 "What style is used for the solver information outputted\n"
572 "to the output stream.");
573 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
574 "How often convergence information should be outputted\n"
575 "to the output stream.");
576 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
577 "The number of linear systems that need to converge before\n"
578 "they are deflated. This number should be <= block size.");
579 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
580 "A reference-counted pointer to the output stream where all\n"
581 "solver output is sent.");
582 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
583 "When convergence information is printed, only show the maximum\n"
584 "relative residual norm when the block size is greater than one.");
585 pl->set(
"Implicit Residual Scaling",
static_cast<const char *
>(resScale_default_),
586 "The type of scaling used in the residual convergence test.");
592 pl->set(
"Residual Scaling",
static_cast<const char *
>(resScale_default_),
593 "The type of scaling used in the residual convergence test. This "
594 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
595 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
596 "The string to use as a prefix for the timer labels.");
603template<
class ScalarType,
class MV,
class OP,
class DM>
612 setParameters (params_);
617 "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. "
618 "You must call setProblem() on the LinearProblem before you may solve it.");
620 (problem_->isLeftPrec (), std::logic_error,
"Belos::BiCGStabSolMgr::solve: "
621 "The left-preconditioned case has not yet been implemented. Please use "
622 "right preconditioning for now. If you need to use left preconditioning, "
623 "please contact the Belos developers. Left preconditioning is more "
624 "interesting in BiCGStab because whether it works depends on the initial "
625 "guess (e.g., an initial guess of all zeros might NOT work).");
629 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
639 problem_->setLSIndex(
currIdx );
643 Teuchos::ParameterList
plist;
646 outputTest_->reset();
654 Teuchos::RCP<BiCGStabIter<ScalarType,MV,OP,DM> >
bicgstab_iter
659#ifdef BELOS_TEUCHOS_TIME_MONITOR
660 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
674 outputTest_->resetNumCalls();
677 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())),
currIdx );
696 if ( convTest_->getStatus() ==
Passed ) {
699 std::vector<int>
convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP,DM> >(convTest_)->convIndices();
707 problem_->setCurrLS();
714 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
732 std::vector<MagnitudeType>
norms;
747 else if ( maxIterTest_->getStatus() ==
Passed ) {
766 "Belos::BiCGStabSolMgr::solve(): Warning! Solver has experienced a breakdown!" << std::endl;
780 else if (Teuchos::nonnull(debugStatusTest_) &&
781 debugStatusTest_->getStatus() ==
Passed) {
797 "Belos::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
803 achievedTol_ = MT::one();
804 Teuchos::RCP<MV>
X = problem_->getLHS();
805 MVT::MvInit( *
X, SCT::zero() );
806 printer_->stream(
Warnings) <<
"Belos::BiCGStabSolMgr::solve(): Warning! NaN has been detected!"
810 catch (
const std::exception &
e) {
812 printer_->stream(
Errors) <<
"Error! Caught std::exception in BiCGStabIter::iterate() at iteration "
814 <<
e.what() << std::endl;
820 problem_->setCurrLS();
835 problem_->setLSIndex(
currIdx );
850#ifdef BELOS_TEUCHOS_TIME_MONITOR
855 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
859 numIters_ = maxIterTest_->getNumIters();
864 const std::vector<MagnitudeType>*
pTestValues = convTest_->getTestValue();
875template<
class ScalarType,
class MV,
class OP,
class DM>
878 std::ostringstream
oss;
879 oss <<
"Belos::BiCGStabSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
889#ifdef HAVE_BELOS_TPETRA
892#define BELOS_TPETRA_BICGSTABSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
893 BELOS_TPETRA_CALL(Belos::BiCGStabSolMgr, SC, LO, GO, NT)
895#define BELOS_TPETRA_BICGSTABSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
896 BELOS_TPETRA_EXTERN_CALL(Belos::BiCGStabSolMgr, SC, LO, GO, NT)
898TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_BICGSTABSOLMGR_EXTERN_CALL)
Belos concrete class for performing the pseudo-block BiCGStab iteration.
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.
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::BiCGStabSolMgr provides a powerful and fully-featured solver manager over the pseudo-block...
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
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.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
std::string description() const override
Method to return description of the block BiCGStab solver manager.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
virtual ~BiCGStabSolMgr()
Destructor.
BiCGStabSolMgr()
Empty constructor for BiCGStabSolMgr. This constructor takes no arguments and sets the default values...
BiCGStabSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
BiCGStabSolMgrLinearProblemFailure(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 ...
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
ReturnType
Whether the Belos solve converged for all linear systems.
ScaleType
The type of scaling to use on the residual norm value.
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double convTol
Default convergence tolerance.