10#ifndef BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP
11#define BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP
31#include "Teuchos_LAPACK.hpp"
32#ifdef BELOS_TEUCHOS_TIME_MONITOR
33#include "Teuchos_TimeMonitor.hpp"
73 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int,ScalarType>,
74 const
bool supportsScalarType =
75 Details::LapackSupportsScalar<ScalarType>::value>
78 Details::LapackSupportsScalar<ScalarType>::value>
80 static const bool scalarTypeIsSupported =
89 const Teuchos::RCP<Teuchos::ParameterList> &
pl) :
94 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> >
99 template<
class ScalarType,
class MV,
class OP,
class DM>
106 using SCT = Teuchos::ScalarTraits<ScalarType>;
107 using MagnitudeType =
typename Teuchos::ScalarTraits<ScalarType>::magnitudeType;
108 using MT = Teuchos::ScalarTraits<MagnitudeType>;
138 const Teuchos::RCP<Teuchos::ParameterList> &
pl );
144 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
158 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const override;
169 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
170 return Teuchos::tuple(timerSolve_);
205 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> >
217 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> &
params )
override;
258 std::string description()
const override;
263 void compute_condnum_tridiag_sym(Teuchos::ArrayView<MagnitudeType>
diag,
264 Teuchos::ArrayView<MagnitudeType>
offdiag,
265 Teuchos::ArrayRCP<MagnitudeType>&
lambdas,
271 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
274 Teuchos::RCP<OutputManager<ScalarType> > printer_;
275 Teuchos::RCP<std::ostream> outputStream_;
278 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
279 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
280 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
281 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
284 Teuchos::RCP<Teuchos::ParameterList> params_;
291 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
294 static constexpr int maxIters_default_ = 1000;
295 static constexpr bool assertPositiveDefiniteness_default_ =
true;
296 static constexpr bool showMaxResNormOnly_default_ =
false;
299 static constexpr int outputFreq_default_ = -1;
300 static constexpr int defQuorum_default_ = 1;
301 static constexpr bool foldConvergenceDetectionIntoAllreduce_default_ =
false;
302 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
303 static constexpr const char * label_default_ =
"Belos";
304 static constexpr bool genCondEst_default_ =
false;
307 MagnitudeType convtol_,achievedTol_;
308 int maxIters_, numIters_;
309 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
310 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
311 bool foldConvergenceDetectionIntoAllreduce_;
312 std::string resScale_;
315 Teuchos::ArrayRCP<MagnitudeType> eigenEstimates_;
317 Teuchos::RCP<CGIterationStateBase<ScalarType, MV, DM> > state_;
321 Teuchos::RCP<Teuchos::Time> timerSolve_;
329template<
class ScalarType,
class MV,
class OP,
class DM>
333 maxIters_(maxIters_default_),
335 verbosity_(verbosity_default_),
336 outputStyle_(outputStyle_default_),
337 outputFreq_(outputFreq_default_),
338 defQuorum_(defQuorum_default_),
339 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
340 showMaxResNormOnly_(showMaxResNormOnly_default_),
341 foldConvergenceDetectionIntoAllreduce_(foldConvergenceDetectionIntoAllreduce_default_),
342 resScale_(resScale_default_),
343 genCondEst_(genCondEst_default_),
345 label_(label_default_),
350template<
class ScalarType,
class MV,
class OP,
class DM>
353 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
357 maxIters_(maxIters_default_),
359 verbosity_(verbosity_default_),
360 outputStyle_(outputStyle_default_),
361 outputFreq_(outputFreq_default_),
362 defQuorum_(defQuorum_default_),
363 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
364 showMaxResNormOnly_(showMaxResNormOnly_default_),
365 foldConvergenceDetectionIntoAllreduce_(foldConvergenceDetectionIntoAllreduce_default_),
366 resScale_(resScale_default_),
367 genCondEst_(genCondEst_default_),
369 label_(label_default_),
373 problem_.is_null (), std::invalid_argument,
374 "Belos::PseudoBlockCGSolMgr two-argument constructor: "
375 "'problem' is null. You must supply a non-null Belos::LinearProblem "
376 "instance when calling this constructor.");
378 if (!
pl.is_null ()) {
384template<
class ScalarType,
class MV,
class OP,
class DM>
388 using Teuchos::ParameterList;
389 using Teuchos::parameterList;
412 if (params_.is_null ()) {
420 if (
params->isParameter (
"Maximum Iterations")) {
421 maxIters_ =
params->get (
"Maximum Iterations", maxIters_default_);
424 params_->set (
"Maximum Iterations", maxIters_);
425 if (! maxIterTest_.is_null ()) {
426 maxIterTest_->setMaxIters (maxIters_);
431 if (
params->isParameter (
"Assert Positive Definiteness")) {
432 assertPositiveDefiniteness_ =
433 params->get (
"Assert Positive Definiteness",
434 assertPositiveDefiniteness_default_);
437 params_->set (
"Assert Positive Definiteness", assertPositiveDefiniteness_);
440 if (
params->isParameter(
"Fold Convergence Detection Into Allreduce")) {
441 foldConvergenceDetectionIntoAllreduce_ =
params->get(
"Fold Convergence Detection Into Allreduce",
442 foldConvergenceDetectionIntoAllreduce_default_);
446 if (
params->isParameter (
"Timer Label")) {
447 const std::string
tempLabel =
params->get (
"Timer Label", label_default_);
452 params_->set (
"Timer Label", label_);
454 label_ +
": PseudoBlockCGSolMgr total solve time";
455#ifdef BELOS_TEUCHOS_TIME_MONITOR
456 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (
solveLabel);
462 if (
params->isParameter (
"Verbosity")) {
463 if (Teuchos::isParameterType<int> (*
params,
"Verbosity")) {
464 verbosity_ =
params->get (
"Verbosity", verbosity_default_);
466 verbosity_ = (
int) Teuchos::getParameter<Belos::MsgType> (*
params,
"Verbosity");
470 params_->set (
"Verbosity", verbosity_);
471 if (! printer_.is_null ()) {
472 printer_->setVerbosity (verbosity_);
477 if (
params->isParameter (
"Output Style")) {
478 if (Teuchos::isParameterType<int> (*
params,
"Output Style")) {
479 outputStyle_ =
params->get (
"Output Style", outputStyle_default_);
482 outputStyle_ = (
int) Teuchos::getParameter<Belos::OutputType> (*
params,
"Output Style");
487 params_->set (
"Output Style", outputStyle_);
488 outputTest_ = Teuchos::null;
492 if (
params->isParameter (
"Output Stream")) {
496 params_->set (
"Output Stream", outputStream_);
497 if (! printer_.is_null ()) {
498 printer_->setOStream (outputStream_);
504 if (
params->isParameter (
"Output Frequency")) {
505 outputFreq_ =
params->get (
"Output Frequency", outputFreq_default_);
509 params_->set (
"Output Frequency", outputFreq_);
510 if (! outputTest_.is_null ()) {
511 outputTest_->setOutputFrequency (outputFreq_);
516 if (
params->isParameter (
"Estimate Condition Number")) {
517 genCondEst_ =
params->get (
"Estimate Condition Number", genCondEst_default_);
521 if (printer_.is_null ()) {
530 if (
params->isParameter (
"Convergence Tolerance")) {
531 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
532 convtol_ =
params->get (
"Convergence Tolerance",
540 params_->set (
"Convergence Tolerance", convtol_);
541 if (! convTest_.is_null ()) {
542 convTest_->setTolerance (convtol_);
546 if (
params->isParameter (
"Show Maximum Residual Norm Only")) {
547 showMaxResNormOnly_ =
params->get<
bool> (
"Show Maximum Residual Norm Only");
550 params_->set (
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
551 if (! convTest_.is_null ()) {
552 convTest_->setShowMaxResNormOnly (showMaxResNormOnly_);
564 if (
params->isParameter (
"Residual Scaling")) {
567 else if (
params->isParameter (
"Implicit Residual Scaling")) {
581 params_->set (
"Implicit Residual Scaling", resScale_);
584 params_->set (
"Residual Scaling", resScale_);
587 if (! convTest_.is_null ()) {
591 catch (std::exception&
e) {
600 if (
params->isParameter (
"Deflation Quorum")) {
601 defQuorum_ =
params->get (
"Deflation Quorum", defQuorum_);
602 params_->set (
"Deflation Quorum", defQuorum_);
603 if (! convTest_.is_null ()) {
604 convTest_->setQuorum( defQuorum_ );
611 if (maxIterTest_.is_null ()) {
617 convTest_ =
rcp (
new StatusTestResNorm_t (convtol_, defQuorum_, showMaxResNormOnly_));
622 sTest_ =
rcp (
new StatusTestCombo_t (StatusTestCombo_t::OR, maxIterTest_, convTest_));
629 outputTest_ =
stoFactory.create (printer_, sTest_, outputFreq_,
633 const std::string
solverDesc =
" Pseudo Block CG ";
638 if (timerSolve_.is_null ()) {
640 label_ +
": PseudoBlockCGSolMgr total solve time";
641#ifdef BELOS_TEUCHOS_TIME_MONITOR
642 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (
solveLabel);
651template<
class ScalarType,
class MV,
class OP,
class DM>
652Teuchos::RCP<const Teuchos::ParameterList>
655 using Teuchos::ParameterList;
656 using Teuchos::parameterList;
659 if (validParams_.is_null()) {
663 "The relative residual tolerance that needs to be achieved by the\n"
664 "iterative solver in order for the linear system to be declared converged.");
665 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
666 "The maximum number of block iterations allowed for each\n"
667 "set of RHS solved.");
668 pl->set(
"Assert Positive Definiteness",
static_cast<bool>(assertPositiveDefiniteness_default_),
669 "Whether or not to assert that the linear operator\n"
670 "and the preconditioner are indeed positive definite.");
671 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
672 "What type(s) of solver information should be outputted\n"
673 "to the output stream.");
674 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
675 "What style is used for the solver information outputted\n"
676 "to the output stream.");
677 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
678 "How often convergence information should be outputted\n"
679 "to the output stream.");
680 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
681 "The number of linear systems that need to converge before\n"
682 "they are deflated. This number should be <= block size.");
683 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
684 "A reference-counted pointer to the output stream where all\n"
685 "solver output is sent.");
686 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
687 "When convergence information is printed, only show the maximum\n"
688 "relative residual norm when the block size is greater than one.");
689 pl->set(
"Implicit Residual Scaling", resScale_default_,
690 "The type of scaling used in the residual convergence test.");
691 pl->set(
"Estimate Condition Number",
static_cast<bool>(genCondEst_default_),
692 "Whether or not to estimate the condition number of the preconditioned system.");
698 pl->set(
"Residual Scaling", resScale_default_,
699 "The type of scaling used in the residual convergence test. This "
700 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
701 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
702 "The string to use as a prefix for the timer labels.");
703 pl->set(
"Fold Convergence Detection Into Allreduce",
static_cast<bool>(foldConvergenceDetectionIntoAllreduce_default_),
704 "Merge the allreduce for convergence detection with the one for CG.\n"
705 "This saves one all-reduce, but incurs more computation.");
713template<
class ScalarType,
class MV,
class OP,
class DM>
716 const char prefix[] =
"Belos::PseudoBlockCGSolMgr::solve: ";
723 if (!isSet_) { setParameters( params_ ); }
727 prefix <<
"The linear problem to solve is not ready. You must call "
728 "setProblem() on the Belos::LinearProblem instance before telling the "
729 "Belos solver to solve it.");
733 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
743 problem_->setLSIndex(
currIdx );
747 Teuchos::ParameterList
plist;
749 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
750 if(genCondEst_)
plist.set(
"Max Size For Condest",maxIters_);
753 outputTest_->reset();
760 Teuchos::RCP<CGIteration<ScalarType,MV,OP,DM> >
block_cg_iter;
762 plist.set(
"Fold Convergence Detection Into Allreduce",
763 foldConvergenceDetectionIntoAllreduce_);
781#ifdef BELOS_TEUCHOS_TIME_MONITOR
782 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
796 outputTest_->resetNumCalls();
799 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())),
currIdx );
816 if ( convTest_->getStatus() ==
Passed ) {
819 std::vector<int>
convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP,DM> >(convTest_)->convIndices();
827 problem_->setCurrLS();
833 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
865 std::vector<MagnitudeType>
norms;
878 else if ( maxIterTest_->getStatus() ==
Passed ) {
895 "Belos::PseudoBlockCGSolMgr::solve(): Invalid return from PseudoBlockCGIter::iterate().");
901 achievedTol_ = MT::one();
902 Teuchos::RCP<MV>
X = problem_->getLHS();
903 MVT::MvInit( *
X, SCT::zero() );
904 printer_->stream(
Warnings) <<
"Belos::PseudoBlockCGSolMgr::solve(): Warning! NaN has been detected!"
908 catch (
const std::exception &
e) {
910 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockCGIter::iterate() at iteration "
912 <<
e.what() << std::endl;
918 problem_->setCurrLS();
933 problem_->setLSIndex(
currIdx );
947#ifdef BELOS_TEUCHOS_TIME_MONITOR
952 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
956 numIters_ = maxIterTest_->getNumIters();
960 const std::vector<MagnitudeType>*
pTestValues = convTest_->getTestValue();
981template<
class ScalarType,
class MV,
class OP,
class DM>
984 std::ostringstream
oss;
985 oss <<
"Belos::PseudoBlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
992template<
class ScalarType,
class MV,
class OP,
class DM>
995 Teuchos::ArrayView<MagnitudeType>
offdiag,
996 Teuchos::ArrayRCP<MagnitudeType>&
lambdas,
1001 using STS = Teuchos::ScalarTraits<ScalarType>;
1010 const int N =
diag.size ();
1014 Teuchos::LAPACK<int,ScalarType> lapack;
1023 (
info < 0, std::logic_error,
"Belos::PseudoBlockCGSolMgr::"
1024 "compute_condnum_tridiag_sym: LAPACK's _PTEQR failed with info = "
1025 <<
info <<
" < 0. This suggests there might be a bug in the way Belos "
1026 "is calling LAPACK. Please report this to the Belos developers.");
1027 for (
int k = 0;
k <
N;
k++) {
Belos concrete class for performing the conjugate-gradient (CG) iteration.
Pure virtual base class which augments the basic interface for a conjugate gradient linear solver ite...
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 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.
Type traits class that says whether Teuchos::LAPACK has a valid implementation for the given ScalarTy...
Base class for Belos::SolverManager subclasses which normally can only compile with ScalarType types ...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
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 reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP, DM > > getResidualStatusTest() const
Return the residual status test.
Teuchos::ArrayRCP< MagnitudeType > getEigenEstimates() const
virtual ~PseudoBlockCGSolMgr()=default
Destructor.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
ScalarType getConditionEstimate() const
Gets the estimated condition number.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
virtual ~PseudoBlockCGSolMgr()=default
PseudoBlockCGSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP, DM > > getResidualStatusTest() const
PseudoBlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
PseudoBlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
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.