42#ifndef BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP
43#define BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP
61#ifdef BELOS_TEUCHOS_TIME_MONITOR
62#include "Teuchos_TimeMonitor.hpp"
89 template<
class ScalarType,
class MV,
class OP>
95 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
96 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
MagnitudeType;
97 typedef Teuchos::ScalarTraits<MagnitudeType>
MT;
121 const Teuchos::RCP<Teuchos::ParameterList> &pl );
127 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
152 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
175 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
226 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
233 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
sTest_;
235 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
convTest_;
280template<
class ScalarType,
class MV,
class OP>
282 outputStream_(Teuchos::rcpFromRef(std::cout)),
284 maxIters_(maxIters_default_),
286 verbosity_(verbosity_default_),
287 outputStyle_(outputStyle_default_),
288 outputFreq_(outputFreq_default_),
289 defQuorum_(defQuorum_default_),
290 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
291 showMaxResNormOnly_(showMaxResNormOnly_default_),
292 resScale_(resScale_default_),
293 label_(label_default_),
298template<
class ScalarType,
class MV,
class OP>
301 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
303 outputStream_(Teuchos::rcpFromRef(std::cout)),
305 maxIters_(maxIters_default_),
307 verbosity_(verbosity_default_),
308 outputStyle_(outputStyle_default_),
309 outputFreq_(outputFreq_default_),
310 defQuorum_(defQuorum_default_),
311 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
312 showMaxResNormOnly_(showMaxResNormOnly_default_),
313 resScale_(resScale_default_),
314 label_(label_default_),
317 TEUCHOS_TEST_FOR_EXCEPTION(
318 problem_.is_null (), std::invalid_argument,
319 "Belos::PseudoBlockStochasticCGSolMgr two-argument constructor: "
320 "'problem' is null. You must supply a non-null Belos::LinearProblem "
321 "instance when calling this constructor.");
323 if (! pl.is_null ()) {
329template<
class ScalarType,
class MV,
class OP>
332 using Teuchos::ParameterList;
333 using Teuchos::parameterList;
336 RCP<const ParameterList> defaultParams = getValidParameters();
339 if (params_.is_null()) {
340 params_ = parameterList (*defaultParams);
342 params->validateParameters (*defaultParams);
346 if (params->isParameter(
"Maximum Iterations")) {
347 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
350 params_->set(
"Maximum Iterations", maxIters_);
351 if (maxIterTest_!=Teuchos::null)
352 maxIterTest_->setMaxIters( maxIters_ );
356 if (params->isParameter(
"Assert Positive Definiteness")) {
357 assertPositiveDefiniteness_ = params->get(
"Assert Positive Definiteness",assertPositiveDefiniteness_default_);
360 params_->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_);
364 if (params->isParameter(
"Timer Label")) {
365 std::string tempLabel = params->get(
"Timer Label", label_default_);
368 if (tempLabel != label_) {
370 params_->set(
"Timer Label", label_);
371 std::string solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
372#ifdef BELOS_TEUCHOS_TIME_MONITOR
373 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
379 if (params->isParameter(
"Verbosity")) {
380 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
381 verbosity_ = params->get(
"Verbosity", verbosity_default_);
383 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
387 params_->set(
"Verbosity", verbosity_);
388 if (printer_ != Teuchos::null)
389 printer_->setVerbosity(verbosity_);
393 if (params->isParameter(
"Output Style")) {
394 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
395 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
397 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
401 params_->set(
"Output Style", outputStyle_);
402 outputTest_ = Teuchos::null;
406 if (params->isParameter(
"Output Stream")) {
407 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
410 params_->set(
"Output Stream", outputStream_);
411 if (printer_ != Teuchos::null)
412 printer_->setOStream( outputStream_ );
417 if (params->isParameter(
"Output Frequency")) {
418 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
422 params_->set(
"Output Frequency", outputFreq_);
423 if (outputTest_ != Teuchos::null)
424 outputTest_->setOutputFrequency( outputFreq_ );
428 if (printer_ == Teuchos::null) {
437 if (params->isParameter(
"Convergence Tolerance")) {
438 if (params->isType<
MagnitudeType> (
"Convergence Tolerance")) {
439 convtol_ = params->get (
"Convergence Tolerance",
447 params_->set(
"Convergence Tolerance", convtol_);
448 if (convTest_ != Teuchos::null)
452 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
453 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
456 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
457 if (convTest_ != Teuchos::null)
458 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
462 bool newResTest =
false;
467 std::string tempResScale = resScale_;
468 bool implicitResidualScalingName =
false;
469 if (params->isParameter (
"Residual Scaling")) {
470 tempResScale = params->get<std::string> (
"Residual Scaling");
472 else if (params->isParameter (
"Implicit Residual Scaling")) {
473 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
474 implicitResidualScalingName =
true;
478 if (resScale_ != tempResScale) {
480 resScale_ = tempResScale;
484 if (implicitResidualScalingName) {
485 params_->set (
"Implicit Residual Scaling", resScale_);
488 params_->set (
"Residual Scaling", resScale_);
491 if (! convTest_.is_null()) {
495 catch (std::exception& e) {
504 if (params->isParameter(
"Deflation Quorum")) {
505 defQuorum_ = params->get(
"Deflation Quorum", defQuorum_);
506 params_->set(
"Deflation Quorum", defQuorum_);
507 if (convTest_ != Teuchos::null)
508 convTest_->setQuorum( defQuorum_ );
514 if (maxIterTest_ == Teuchos::null)
518 if (convTest_ == Teuchos::null || newResTest) {
519 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
523 if (sTest_ == Teuchos::null || newResTest)
524 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
526 if (outputTest_ == Teuchos::null || newResTest) {
534 std::string solverDesc =
" Pseudo Block CG ";
535 outputTest_->setSolverDesc( solverDesc );
540 if (timerSolve_ == Teuchos::null) {
541 std::string solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
542#ifdef BELOS_TEUCHOS_TIME_MONITOR
543 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
552template<
class ScalarType,
class MV,
class OP>
553Teuchos::RCP<const Teuchos::ParameterList>
556 using Teuchos::ParameterList;
557 using Teuchos::parameterList;
560 if (validParams_.is_null()) {
565 RCP<ParameterList> pl = parameterList ();
567 "The relative residual tolerance that needs to be achieved by the\n"
568 "iterative solver in order for the linera system to be declared converged.");
569 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
570 "The maximum number of block iterations allowed for each\n"
571 "set of RHS solved.");
572 pl->set(
"Assert Positive Definiteness",
static_cast<bool>(assertPositiveDefiniteness_default_),
573 "Whether or not to assert that the linear operator\n"
574 "and the preconditioner are indeed positive definite.");
575 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
576 "What type(s) of solver information should be outputted\n"
577 "to the output stream.");
578 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
579 "What style is used for the solver information outputted\n"
580 "to the output stream.");
581 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
582 "How often convergence information should be outputted\n"
583 "to the output stream.");
584 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
585 "The number of linear systems that need to converge before\n"
586 "they are deflated. This number should be <= block size.");
587 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
588 "A reference-counted pointer to the output stream where all\n"
589 "solver output is sent.");
590 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
591 "When convergence information is printed, only show the maximum\n"
592 "relative residual norm when the block size is greater than one.");
593 pl->set(
"Implicit Residual Scaling", resScale_default_,
594 "The type of scaling used in the residual convergence test.");
600 pl->set(
"Residual Scaling", resScale_default_,
601 "The type of scaling used in the residual convergence test. This "
602 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
603 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
604 "The string to use as a prefix for the timer labels.");
612template<
class ScalarType,
class MV,
class OP>
618 if (!isSet_) { setParameters( params_ ); }
621 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
625 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
626 int numCurrRHS = numRHS2Solve;
628 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
629 for (
int i=0; i<numRHS2Solve; ++i) {
630 currIdx[i] = startPtr+i;
635 problem_->setLSIndex( currIdx );
639 Teuchos::ParameterList plist;
641 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
644 outputTest_->reset();
647 bool isConverged =
true;
652 Teuchos::RCP<PseudoBlockStochasticCGIter<ScalarType,MV,OP> > block_cg_iter
657#ifdef BELOS_TEUCHOS_TIME_MONITOR
658 Teuchos::TimeMonitor slvtimer(*timerSolve_);
661 while ( numRHS2Solve > 0 ) {
664 std::vector<int> convRHSIdx;
665 std::vector<int> currRHSIdx( currIdx );
666 currRHSIdx.resize(numCurrRHS);
669 block_cg_iter->resetNumIters();
672 outputTest_->resetNumCalls();
675 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
680 block_cg_iter->initializeCG(newState);
686 block_cg_iter->iterate();
693 if ( convTest_->getStatus() ==
Passed ) {
696 std::vector<int> convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP> >(convTest_)->convIndices();
700 if (convIdx.size() == currRHSIdx.size())
704 problem_->setCurrLS();
708 std::vector<int> unconvIdx(currRHSIdx.size());
709 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
711 for (
unsigned int j=0; j<convIdx.size(); ++j) {
712 if (currRHSIdx[i] == convIdx[j]) {
718 currIdx2[have] = currIdx2[i];
719 currRHSIdx[have++] = currRHSIdx[i];
722 currRHSIdx.resize(have);
723 currIdx2.resize(have);
726 problem_->setLSIndex( currRHSIdx );
729 std::vector<MagnitudeType> norms;
730 R_0 = MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
731 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
736 block_cg_iter->initializeCG(defstate);
744 else if ( maxIterTest_->getStatus() ==
Passed ) {
758 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
759 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Invalid return from PseudoBlockStochasticCGIter::iterate().");
762 catch (
const std::exception &e) {
763 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockStochasticCGIter::iterate() at iteration "
764 << block_cg_iter->getNumIters() << std::endl
765 << e.what() << std::endl;
771 problem_->setCurrLS();
774 startPtr += numCurrRHS;
775 numRHS2Solve -= numCurrRHS;
777 if ( numRHS2Solve > 0 ) {
779 numCurrRHS = numRHS2Solve;
780 currIdx.resize( numCurrRHS );
781 currIdx2.resize( numCurrRHS );
782 for (
int i=0; i<numCurrRHS; ++i)
783 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
786 problem_->setLSIndex( currIdx );
789 currIdx.resize( numRHS2Solve );
797 Y_=block_cg_iter->getStochasticVector();
804#ifdef BELOS_TEUCHOS_TIME_MONITOR
809 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
813 numIters_ = maxIterTest_->getNumIters();
822template<
class ScalarType,
class MV,
class OP>
825 std::ostringstream oss;
826 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.
A linear system to solve, and its associated information.
Traits class which defines basic operations on multivectors.
Class which defines basic traits for the operator type.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
This class implements the stochastic pseudo-block CG iteration, where the basic stochastic CG algorit...
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...
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
static constexpr int outputFreq_default_
Teuchos::RCP< Teuchos::ParameterList > params_
Teuchos::RCP< OutputManager< ScalarType > > printer_
PseudoBlockStochasticCGSolMgr()
Empty constructor for BlockStochasticCGSolMgr. This constructor takes no arguments and sets the defau...
Teuchos::RCP< std::ostream > outputStream_
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...
static constexpr int defQuorum_default_
bool assertPositiveDefiniteness_
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
static constexpr const char * label_default_
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
static constexpr bool assertPositiveDefiniteness_default_
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > convTest_
virtual ~PseudoBlockStochasticCGSolMgr()
Destructor.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
static constexpr int maxIters_default_
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
MultiVecTraits< ScalarType, MV > MVT
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
OperatorTraits< ScalarType, MV, OP > OPT
static constexpr const char * resScale_default_
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< Teuchos::Time > timerSolve_
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::RCP< const Teuchos::ParameterList > validParams_
List of valid parameters and their default values.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
static constexpr bool showMaxResNormOnly_default_
Teuchos::ScalarTraits< MagnitudeType > MT
static constexpr int verbosity_default_
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
static constexpr int outputStyle_default_
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 ...
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
A Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
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.
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.
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< const MV > R
The current residual.