10#ifndef BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP
11#define BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP
29#ifdef BELOS_TEUCHOS_TIME_MONITOR
30#include "Teuchos_TimeMonitor.hpp"
57 template<
class ScalarType,
class MV,
class OP>
63 typedef Teuchos::ScalarTraits<ScalarType> SCT;
64 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
65 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
89 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
95 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
120 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
121 return Teuchos::tuple(timerSolve_);
194 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
197 Teuchos::RCP<OutputManager<ScalarType> > printer_;
198 Teuchos::RCP<std::ostream> outputStream_;
201 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
202 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
203 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
204 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
207 Teuchos::RCP<Teuchos::ParameterList> params_;
214 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
217 static constexpr int maxIters_default_ = 1000;
218 static constexpr bool assertPositiveDefiniteness_default_ =
true;
219 static constexpr bool showMaxResNormOnly_default_ =
false;
222 static constexpr int outputFreq_default_ = -1;
223 static constexpr int defQuorum_default_ = 1;
224 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
225 static constexpr const char * label_default_ =
"Belos";
228 MagnitudeType convtol_;
229 int maxIters_, numIters_;
230 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
231 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
232 std::string resScale_;
236 Teuchos::RCP<Teuchos::Time> timerSolve_;
248template<
class ScalarType,
class MV,
class OP>
252 maxIters_(maxIters_default_),
254 verbosity_(verbosity_default_),
255 outputStyle_(outputStyle_default_),
256 outputFreq_(outputFreq_default_),
257 defQuorum_(defQuorum_default_),
258 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
259 showMaxResNormOnly_(showMaxResNormOnly_default_),
260 resScale_(resScale_default_),
261 label_(label_default_),
266template<
class ScalarType,
class MV,
class OP>
269 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
273 maxIters_(maxIters_default_),
275 verbosity_(verbosity_default_),
276 outputStyle_(outputStyle_default_),
277 outputFreq_(outputFreq_default_),
278 defQuorum_(defQuorum_default_),
279 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
280 showMaxResNormOnly_(showMaxResNormOnly_default_),
281 resScale_(resScale_default_),
282 label_(label_default_),
286 problem_.is_null (), std::invalid_argument,
287 "Belos::PseudoBlockStochasticCGSolMgr two-argument constructor: "
288 "'problem' is null. You must supply a non-null Belos::LinearProblem "
289 "instance when calling this constructor.");
291 if (!
pl.is_null ()) {
297template<
class ScalarType,
class MV,
class OP>
300 using Teuchos::ParameterList;
301 using Teuchos::parameterList;
307 if (params_.is_null()) {
314 if (
params->isParameter(
"Maximum Iterations")) {
315 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
318 params_->set(
"Maximum Iterations", maxIters_);
319 if (maxIterTest_!=Teuchos::null)
320 maxIterTest_->setMaxIters( maxIters_ );
324 if (
params->isParameter(
"Assert Positive Definiteness")) {
325 assertPositiveDefiniteness_ =
params->get(
"Assert Positive Definiteness",assertPositiveDefiniteness_default_);
328 params_->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_);
332 if (
params->isParameter(
"Timer Label")) {
338 params_->set(
"Timer Label", label_);
339 std::string
solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
340#ifdef BELOS_TEUCHOS_TIME_MONITOR
341 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
347 if (
params->isParameter(
"Verbosity")) {
348 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
349 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
351 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
355 params_->set(
"Verbosity", verbosity_);
356 if (printer_ != Teuchos::null)
357 printer_->setVerbosity(verbosity_);
361 if (
params->isParameter(
"Output Style")) {
362 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
363 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
365 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
369 params_->set(
"Output Style", outputStyle_);
370 outputTest_ = Teuchos::null;
374 if (
params->isParameter(
"Output Stream")) {
375 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
378 params_->set(
"Output Stream", outputStream_);
379 if (printer_ != Teuchos::null)
380 printer_->setOStream( outputStream_ );
385 if (
params->isParameter(
"Output Frequency")) {
386 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
390 params_->set(
"Output Frequency", outputFreq_);
391 if (outputTest_ != Teuchos::null)
392 outputTest_->setOutputFrequency( outputFreq_ );
396 if (printer_ == Teuchos::null) {
405 if (
params->isParameter(
"Convergence Tolerance")) {
406 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
407 convtol_ =
params->get (
"Convergence Tolerance",
415 params_->set(
"Convergence Tolerance", convtol_);
416 if (convTest_ != Teuchos::null)
417 convTest_->setTolerance( convtol_ );
420 if (
params->isParameter(
"Show Maximum Residual Norm Only")) {
421 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*
params,
"Show Maximum Residual Norm Only");
424 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
425 if (convTest_ != Teuchos::null)
426 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
437 if (
params->isParameter (
"Residual Scaling")) {
440 else if (
params->isParameter (
"Implicit Residual Scaling")) {
453 params_->set (
"Implicit Residual Scaling", resScale_);
456 params_->set (
"Residual Scaling", resScale_);
459 if (! convTest_.is_null()) {
463 catch (std::exception&
e) {
472 if (
params->isParameter(
"Deflation Quorum")) {
473 defQuorum_ =
params->get(
"Deflation Quorum", defQuorum_);
474 params_->set(
"Deflation Quorum", defQuorum_);
475 if (convTest_ != Teuchos::null)
476 convTest_->setQuorum( defQuorum_ );
482 if (maxIterTest_ == Teuchos::null)
486 if (convTest_ == Teuchos::null ||
newResTest) {
487 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
492 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
494 if (outputTest_ == Teuchos::null ||
newResTest) {
508 if (timerSolve_ == Teuchos::null) {
509 std::string
solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
510#ifdef BELOS_TEUCHOS_TIME_MONITOR
511 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
520template<
class ScalarType,
class MV,
class OP>
521Teuchos::RCP<const Teuchos::ParameterList>
524 using Teuchos::ParameterList;
525 using Teuchos::parameterList;
528 if (validParams_.is_null()) {
535 "The relative residual tolerance that needs to be achieved by the\n"
536 "iterative solver in order for the linera system to be declared converged.");
537 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
538 "The maximum number of block iterations allowed for each\n"
539 "set of RHS solved.");
540 pl->set(
"Assert Positive Definiteness",
static_cast<bool>(assertPositiveDefiniteness_default_),
541 "Whether or not to assert that the linear operator\n"
542 "and the preconditioner are indeed positive definite.");
543 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
544 "What type(s) of solver information should be outputted\n"
545 "to the output stream.");
546 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
547 "What style is used for the solver information outputted\n"
548 "to the output stream.");
549 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
550 "How often convergence information should be outputted\n"
551 "to the output stream.");
552 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
553 "The number of linear systems that need to converge before\n"
554 "they are deflated. This number should be <= block size.");
555 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
556 "A reference-counted pointer to the output stream where all\n"
557 "solver output is sent.");
558 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
559 "When convergence information is printed, only show the maximum\n"
560 "relative residual norm when the block size is greater than one.");
561 pl->set(
"Implicit Residual Scaling", resScale_default_,
562 "The type of scaling used in the residual convergence test.");
568 pl->set(
"Residual Scaling", resScale_default_,
569 "The type of scaling used in the residual convergence test. This "
570 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
571 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
572 "The string to use as a prefix for the timer labels.");
580template<
class ScalarType,
class MV,
class OP>
586 if (!isSet_) { setParameters( params_ ); }
589 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
593 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
603 problem_->setLSIndex(
currIdx );
607 Teuchos::ParameterList
plist;
609 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
612 outputTest_->reset();
620 Teuchos::RCP<PseudoBlockStochasticCGIter<ScalarType,MV,OP> >
block_cg_iter
625#ifdef BELOS_TEUCHOS_TIME_MONITOR
626 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
640 outputTest_->resetNumCalls();
643 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())),
currIdx );
661 if ( convTest_->getStatus() ==
Passed ) {
664 std::vector<int>
convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP> >(convTest_)->convIndices();
672 problem_->setCurrLS();
679 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
697 std::vector<MagnitudeType>
norms;
712 else if ( maxIterTest_->getStatus() ==
Passed ) {
727 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Invalid return from PseudoBlockStochasticCGIter::iterate().");
730 catch (
const std::exception &
e) {
731 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockStochasticCGIter::iterate() at iteration "
733 <<
e.what() << std::endl;
739 problem_->setCurrLS();
754 problem_->setLSIndex(
currIdx );
772#ifdef BELOS_TEUCHOS_TIME_MONITOR
777 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
781 numIters_ = maxIterTest_->getNumIters();
790template<
class ScalarType,
class MV,
class OP>
793 std::ostringstream
oss;
794 oss <<
"Belos::PseudoBlockStochasticCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
Class which manages the output and verbosity of the Belos solvers.
Belos concrete class for performing the stochastic pseudo-block CG iteration.
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.
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
PseudoBlockStochasticCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
PseudoBlockStochasticCGSolMgrLinearProblemFailure(const std::string &what_arg)
The Belos::PseudoBlockStochasticCGSolMgr provides a powerful and fully-featured solver manager over t...
PseudoBlockStochasticCGSolMgr()
Empty constructor for BlockStochasticCGSolMgr. This constructor takes no arguments and sets the defau...
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
std::string description() const override
Method to return description of the block CG solver manager.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
virtual ~PseudoBlockStochasticCGSolMgr()
Destructor.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
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.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
Teuchos::RCP< MV > getStochasticVector()
Get a copy of the final stochastic vector.
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.