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_);
155 sTest_ = Teuchos::null;
156 outputTest_ = Teuchos::null;
209 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
212 Teuchos::RCP<OutputManager<ScalarType> > printer_;
213 Teuchos::RCP<std::ostream> outputStream_;
216 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
217 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
218 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
219 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
220 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > debugStatusTest_;
223 Teuchos::RCP<Teuchos::ParameterList> params_;
230 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
233 static constexpr int maxIters_default_ = 1000;
234 static constexpr bool assertPositiveDefiniteness_default_ =
true;
235 static constexpr bool showMaxResNormOnly_default_ =
false;
238 static constexpr int outputFreq_default_ = -1;
239 static constexpr int defQuorum_default_ = 1;
240 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
241 static constexpr const char * label_default_ =
"Belos";
244 MagnitudeType convtol_;
245 int maxIters_, numIters_;
246 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
247 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
248 std::string resScale_;
252 Teuchos::RCP<Teuchos::Time> timerSolve_;
264template<
class ScalarType,
class MV,
class OP,
class DM>
268 maxIters_(maxIters_default_),
270 verbosity_(verbosity_default_),
271 outputStyle_(outputStyle_default_),
272 outputFreq_(outputFreq_default_),
273 defQuorum_(defQuorum_default_),
274 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
275 showMaxResNormOnly_(showMaxResNormOnly_default_),
276 resScale_(resScale_default_),
277 label_(label_default_),
282template<
class ScalarType,
class MV,
class OP,
class DM>
285 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
289 maxIters_(maxIters_default_),
291 verbosity_(verbosity_default_),
292 outputStyle_(outputStyle_default_),
293 outputFreq_(outputFreq_default_),
294 defQuorum_(defQuorum_default_),
295 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
296 showMaxResNormOnly_(showMaxResNormOnly_default_),
297 resScale_(resScale_default_),
298 label_(label_default_),
302 problem_.is_null (), std::invalid_argument,
303 "Belos::PseudoBlockStochasticCGSolMgr two-argument constructor: "
304 "'problem' is null. You must supply a non-null Belos::LinearProblem "
305 "instance when calling this constructor.");
307 if (!
pl.is_null ()) {
313template<
class ScalarType,
class MV,
class OP,
class DM>
316 using Teuchos::ParameterList;
317 using Teuchos::parameterList;
323 if (params_.is_null()) {
330 if (
params->isParameter(
"Maximum Iterations")) {
331 maxIters_ =
params->get(
"Maximum Iterations",maxIters_default_);
334 params_->set(
"Maximum Iterations", maxIters_);
335 if (maxIterTest_!=Teuchos::null)
336 maxIterTest_->setMaxIters( maxIters_ );
340 if (
params->isParameter(
"Assert Positive Definiteness")) {
341 assertPositiveDefiniteness_ =
params->get(
"Assert Positive Definiteness",assertPositiveDefiniteness_default_);
344 params_->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_);
348 if (
params->isParameter(
"Timer Label")) {
354 params_->set(
"Timer Label", label_);
355 std::string
solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
356#ifdef BELOS_TEUCHOS_TIME_MONITOR
357 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
363 if (
params->isParameter(
"Verbosity")) {
364 if (Teuchos::isParameterType<int>(*
params,
"Verbosity")) {
365 verbosity_ =
params->get(
"Verbosity", verbosity_default_);
367 verbosity_ = (
int)Teuchos::getParameter<Belos::MsgType>(*
params,
"Verbosity");
371 params_->set(
"Verbosity", verbosity_);
372 if (printer_ != Teuchos::null)
373 printer_->setVerbosity(verbosity_);
377 if (
params->isParameter(
"Output Style")) {
378 if (Teuchos::isParameterType<int>(*
params,
"Output Style")) {
379 outputStyle_ =
params->get(
"Output Style", outputStyle_default_);
381 outputStyle_ = (
int)Teuchos::getParameter<Belos::OutputType>(*
params,
"Output Style");
385 params_->set(
"Output Style", outputStyle_);
386 outputTest_ = Teuchos::null;
390 if (
params->isParameter(
"Output Stream")) {
391 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*
params,
"Output Stream");
394 params_->set(
"Output Stream", outputStream_);
395 if (printer_ != Teuchos::null)
396 printer_->setOStream( outputStream_ );
401 if (
params->isParameter(
"Output Frequency")) {
402 outputFreq_ =
params->get(
"Output Frequency", outputFreq_default_);
406 params_->set(
"Output Frequency", outputFreq_);
407 if (outputTest_ != Teuchos::null)
408 outputTest_->setOutputFrequency( outputFreq_ );
412 if (printer_ == Teuchos::null) {
421 if (
params->isParameter(
"Convergence Tolerance")) {
422 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
423 convtol_ =
params->get (
"Convergence Tolerance",
431 params_->set(
"Convergence Tolerance", convtol_);
432 if (convTest_ != Teuchos::null)
433 convTest_->setTolerance( convtol_ );
436 if (
params->isParameter(
"Show Maximum Residual Norm Only")) {
437 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*
params,
"Show Maximum Residual Norm Only");
440 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
441 if (convTest_ != Teuchos::null)
442 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
453 if (
params->isParameter (
"Residual Scaling")) {
456 else if (
params->isParameter (
"Implicit Residual Scaling")) {
469 params_->set (
"Implicit Residual Scaling", resScale_);
472 params_->set (
"Residual Scaling", resScale_);
475 if (! convTest_.is_null()) {
479 catch (std::exception&
e) {
488 if (
params->isParameter(
"Deflation Quorum")) {
489 defQuorum_ =
params->get(
"Deflation Quorum", defQuorum_);
490 params_->set(
"Deflation Quorum", defQuorum_);
491 if (convTest_ != Teuchos::null)
492 convTest_->setQuorum( defQuorum_ );
498 if (maxIterTest_ == Teuchos::null)
502 if (convTest_ == Teuchos::null ||
newResTest) {
503 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
508 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
509 if (Teuchos::nonnull(debugStatusTest_)) {
511 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, sTest_, debugStatusTest_ ) );
515 if (outputTest_ == Teuchos::null ||
newResTest) {
529 if (timerSolve_ == Teuchos::null) {
530 std::string
solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
531#ifdef BELOS_TEUCHOS_TIME_MONITOR
532 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(
solveLabel);
541template<
class ScalarType,
class MV,
class OP,
class DM>
542Teuchos::RCP<const Teuchos::ParameterList>
545 using Teuchos::ParameterList;
546 using Teuchos::parameterList;
549 if (validParams_.is_null()) {
556 "The relative residual tolerance that needs to be achieved by the\n"
557 "iterative solver in order for the linera system to be declared converged.");
558 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
559 "The maximum number of block iterations allowed for each\n"
560 "set of RHS solved.");
561 pl->set(
"Assert Positive Definiteness",
static_cast<bool>(assertPositiveDefiniteness_default_),
562 "Whether or not to assert that the linear operator\n"
563 "and the preconditioner are indeed positive definite.");
564 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
565 "What type(s) of solver information should be outputted\n"
566 "to the output stream.");
567 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
568 "What style is used for the solver information outputted\n"
569 "to the output stream.");
570 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
571 "How often convergence information should be outputted\n"
572 "to the output stream.");
573 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
574 "The number of linear systems that need to converge before\n"
575 "they are deflated. This number should be <= block size.");
576 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
577 "A reference-counted pointer to the output stream where all\n"
578 "solver output is sent.");
579 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
580 "When convergence information is printed, only show the maximum\n"
581 "relative residual norm when the block size is greater than one.");
582 pl->set(
"Implicit Residual Scaling", resScale_default_,
583 "The type of scaling used in the residual convergence test.");
589 pl->set(
"Residual Scaling", resScale_default_,
590 "The type of scaling used in the residual convergence test. This "
591 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
592 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
593 "The string to use as a prefix for the timer labels.");
601template<
class ScalarType,
class MV,
class OP,
class DM>
608 if (!isSet_) { setParameters( params_ ); }
611 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
615 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
625 problem_->setLSIndex(
currIdx );
629 Teuchos::ParameterList
plist;
631 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
634 outputTest_->reset();
642 Teuchos::RCP<PseudoBlockStochasticCGIter<ScalarType,MV,OP,DM> >
block_cg_iter
647#ifdef BELOS_TEUCHOS_TIME_MONITOR
648 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
662 outputTest_->resetNumCalls();
665 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())),
currIdx );
683 if ( convTest_->getStatus() ==
Passed ) {
686 std::vector<int>
convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP,DM> >(convTest_)->convIndices();
694 problem_->setCurrLS();
701 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
719 std::vector<MagnitudeType>
norms;
734 else if ( maxIterTest_->getStatus() ==
Passed ) {
746 else if (Teuchos::nonnull(debugStatusTest_) &&
747 debugStatusTest_->getStatus() ==
Passed) {
764 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Invalid return from PseudoBlockStochasticCGIter::iterate().");
767 catch (
const std::exception &
e) {
769 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockStochasticCGIter::iterate() at iteration "
771 <<
e.what() << std::endl;
777 problem_->setCurrLS();
792 problem_->setLSIndex(
currIdx );
810#ifdef BELOS_TEUCHOS_TIME_MONITOR
815 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
819 numIters_ = maxIterTest_->getNumIters();
828template<
class ScalarType,
class MV,
class OP,
class DM>
831 std::ostringstream
oss;
832 oss <<
"Belos::PseudoBlockStochasticCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
840#ifdef HAVE_BELOS_TPETRA
843#define BELOS_TPETRA_PSEUDOBLOCKSTOCHASTICCGSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
844 BELOS_TPETRA_CALL(Belos::PseudoBlockStochasticCGSolMgr, SC, LO, GO, NT)
846#define BELOS_TPETRA_PSEUDOBLOCKSTOCHASTICCGSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
847 BELOS_TPETRA_EXTERN_CALL(Belos::PseudoBlockStochasticCGSolMgr, SC, LO, GO, NT)
849TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_PSEUDOBLOCKSTOCHASTICCGSOLMGR_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....
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...
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.
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.