10#ifndef BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP
11#define BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP
32#ifdef BELOS_TEUCHOS_TIME_MONITOR
33#include "Teuchos_TimeMonitor.hpp"
60 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>>
66 typedef Teuchos::ScalarTraits<ScalarType> SCT;
67 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
68 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
92 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
98 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
123 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
124 return Teuchos::tuple(timerSolve_);
197 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
200 Teuchos::RCP<OutputManager<ScalarType> > printer_;
201 Teuchos::RCP<std::ostream> outputStream_;
204 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
205 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
206 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
207 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
210 Teuchos::RCP<Teuchos::ParameterList> params_;
217 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
220 static constexpr int maxIters_default_ = 1000;
221 static constexpr bool assertPositiveDefiniteness_default_ =
true;
222 static constexpr bool showMaxResNormOnly_default_ =
false;
225 static constexpr int outputFreq_default_ = -1;
226 static constexpr int defQuorum_default_ = 1;
227 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
228 static constexpr const char * label_default_ =
"Belos";
231 MagnitudeType convtol_;
232 int maxIters_, numIters_;
233 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
234 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
235 std::string resScale_;
239 Teuchos::RCP<Teuchos::Time> timerSolve_;
251template<
class ScalarType,
class MV,
class OP,
class DM>
255 maxIters_(maxIters_default_),
257 verbosity_(verbosity_default_),
258 outputStyle_(outputStyle_default_),
259 outputFreq_(outputFreq_default_),
260 defQuorum_(defQuorum_default_),
261 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
262 showMaxResNormOnly_(showMaxResNormOnly_default_),
263 resScale_(resScale_default_),
264 label_(label_default_),
269template<
class ScalarType,
class MV,
class OP,
class DM>
272 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
276 maxIters_(maxIters_default_),
278 verbosity_(verbosity_default_),
279 outputStyle_(outputStyle_default_),
280 outputFreq_(outputFreq_default_),
281 defQuorum_(defQuorum_default_),
282 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
283 showMaxResNormOnly_(showMaxResNormOnly_default_),
284 resScale_(resScale_default_),
285 label_(label_default_),
289 problem_.is_null (), std::invalid_argument,
290 "Belos::PseudoBlockStochasticCGSolMgr two-argument constructor: "
291 "'problem' is null. You must supply a non-null Belos::LinearProblem "
292 "instance when calling this constructor.");
294 if (!
pl.is_null ()) {
300template<
class ScalarType,
class MV,
class OP,
class DM>
303 using Teuchos::ParameterList;
304 using Teuchos::parameterList;
310 if (params_.is_null()) {
317 if (
params->isParameter(
"Maximum Iterations")) {
318 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
321 params_->set(
"Maximum Iterations", maxIters_);
322 if (maxIterTest_!=Teuchos::null)
323 maxIterTest_->setMaxIters( maxIters_ );
327 if (
params->isParameter(
"Assert Positive Definiteness")) {
328 assertPositiveDefiniteness_ =
params->get(
"Assert Positive Definiteness",assertPositiveDefiniteness_default_);
331 params_->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_);
335 if (
params->isParameter(
"Timer Label")) {
341 params_->set(
"Timer Label", label_);
342 std::string
solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
343#ifdef BELOS_TEUCHOS_TIME_MONITOR
344 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
350 if (
params->isParameter(
"Verbosity")) {
351 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
352 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
354 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
358 params_->set(
"Verbosity", verbosity_);
359 if (printer_ != Teuchos::null)
360 printer_->setVerbosity(verbosity_);
364 if (
params->isParameter(
"Output Style")) {
365 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
366 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
368 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
372 params_->set(
"Output Style", outputStyle_);
373 outputTest_ = Teuchos::null;
377 if (
params->isParameter(
"Output Stream")) {
378 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
381 params_->set(
"Output Stream", outputStream_);
382 if (printer_ != Teuchos::null)
383 printer_->setOStream( outputStream_ );
388 if (
params->isParameter(
"Output Frequency")) {
389 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
393 params_->set(
"Output Frequency", outputFreq_);
394 if (outputTest_ != Teuchos::null)
395 outputTest_->setOutputFrequency( outputFreq_ );
399 if (printer_ == Teuchos::null) {
408 if (
params->isParameter(
"Convergence Tolerance")) {
409 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
410 convtol_ =
params->get (
"Convergence Tolerance",
418 params_->set(
"Convergence Tolerance", convtol_);
419 if (convTest_ != Teuchos::null)
420 convTest_->setTolerance( convtol_ );
423 if (
params->isParameter(
"Show Maximum Residual Norm Only")) {
424 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*
params,
"Show Maximum Residual Norm Only");
427 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
428 if (convTest_ != Teuchos::null)
429 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
440 if (
params->isParameter (
"Residual Scaling")) {
443 else if (
params->isParameter (
"Implicit Residual Scaling")) {
456 params_->set (
"Implicit Residual Scaling", resScale_);
459 params_->set (
"Residual Scaling", resScale_);
462 if (! convTest_.is_null()) {
466 catch (std::exception&
e) {
475 if (
params->isParameter(
"Deflation Quorum")) {
476 defQuorum_ =
params->get(
"Deflation Quorum", defQuorum_);
477 params_->set(
"Deflation Quorum", defQuorum_);
478 if (convTest_ != Teuchos::null)
479 convTest_->setQuorum( defQuorum_ );
485 if (maxIterTest_ == Teuchos::null)
489 if (convTest_ == Teuchos::null ||
newResTest) {
490 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
495 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
497 if (outputTest_ == Teuchos::null ||
newResTest) {
511 if (timerSolve_ == Teuchos::null) {
512 std::string
solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
513#ifdef BELOS_TEUCHOS_TIME_MONITOR
514 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
523template<
class ScalarType,
class MV,
class OP,
class DM>
524Teuchos::RCP<const Teuchos::ParameterList>
527 using Teuchos::ParameterList;
528 using Teuchos::parameterList;
531 if (validParams_.is_null()) {
538 "The relative residual tolerance that needs to be achieved by the\n"
539 "iterative solver in order for the linera system to be declared converged.");
540 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
541 "The maximum number of block iterations allowed for each\n"
542 "set of RHS solved.");
543 pl->set(
"Assert Positive Definiteness",
static_cast<bool>(assertPositiveDefiniteness_default_),
544 "Whether or not to assert that the linear operator\n"
545 "and the preconditioner are indeed positive definite.");
546 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
547 "What type(s) of solver information should be outputted\n"
548 "to the output stream.");
549 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
550 "What style is used for the solver information outputted\n"
551 "to the output stream.");
552 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
553 "How often convergence information should be outputted\n"
554 "to the output stream.");
555 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
556 "The number of linear systems that need to converge before\n"
557 "they are deflated. This number should be <= block size.");
558 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
559 "A reference-counted pointer to the output stream where all\n"
560 "solver output is sent.");
561 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
562 "When convergence information is printed, only show the maximum\n"
563 "relative residual norm when the block size is greater than one.");
564 pl->set(
"Implicit Residual Scaling", resScale_default_,
565 "The type of scaling used in the residual convergence test.");
571 pl->set(
"Residual Scaling", resScale_default_,
572 "The type of scaling used in the residual convergence test. This "
573 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
574 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
575 "The string to use as a prefix for the timer labels.");
583template<
class ScalarType,
class MV,
class OP,
class DM>
590 if (!isSet_) { setParameters( params_ ); }
593 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
597 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
607 problem_->setLSIndex(
currIdx );
611 Teuchos::ParameterList
plist;
613 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
616 outputTest_->reset();
624 Teuchos::RCP<PseudoBlockStochasticCGIter<ScalarType,MV,OP,DM> >
block_cg_iter
629#ifdef BELOS_TEUCHOS_TIME_MONITOR
630 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
644 outputTest_->resetNumCalls();
647 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())),
currIdx );
665 if ( convTest_->getStatus() ==
Passed ) {
668 std::vector<int>
convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP,DM> >(convTest_)->convIndices();
676 problem_->setCurrLS();
683 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
701 std::vector<MagnitudeType>
norms;
716 else if ( maxIterTest_->getStatus() ==
Passed ) {
733 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Invalid return from PseudoBlockStochasticCGIter::iterate().");
736 catch (
const std::exception &
e) {
738 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockStochasticCGIter::iterate() at iteration "
740 <<
e.what() << std::endl;
746 problem_->setCurrLS();
761 problem_->setLSIndex(
currIdx );
779#ifdef BELOS_TEUCHOS_TIME_MONITOR
784 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
788 numIters_ = maxIterTest_->getNumIters();
797template<
class ScalarType,
class MV,
class OP,
class DM>
800 std::ostringstream
oss;
801 oss <<
"Belos::PseudoBlockStochasticCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
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.
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.
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.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
The Belos::PseudoBlockStochasticCGSolMgr provides a powerful and fully-featured solver manager over t...
std::string description() const override
Method to return description of the block CG solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
Teuchos::RCP< MV > getStochasticVector()
Get a copy of the final stochastic vector.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
PseudoBlockStochasticCGSolMgr()
Empty constructor for BlockStochasticCGSolMgr. This constructor takes no arguments and sets the defau...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
virtual ~PseudoBlockStochasticCGSolMgr()
Destructor.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
PseudoBlockStochasticCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
PseudoBlockStochasticCGSolMgrLinearProblemFailure(const std::string &what_arg)
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.