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;
226 sTest_ = Teuchos::null;
227 outputTest_ = Teuchos::null;
270 std::string description()
const override;
275 void compute_condnum_tridiag_sym(Teuchos::ArrayView<MagnitudeType>
diag,
276 Teuchos::ArrayView<MagnitudeType>
offdiag,
277 Teuchos::ArrayRCP<MagnitudeType>&
lambdas,
283 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
286 Teuchos::RCP<OutputManager<ScalarType> > printer_;
287 Teuchos::RCP<std::ostream> outputStream_;
290 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
291 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
292 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP,DM> > convTest_;
293 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
294 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > debugStatusTest_;
297 Teuchos::RCP<Teuchos::ParameterList> params_;
304 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
307 static constexpr int maxIters_default_ = 1000;
308 static constexpr bool assertPositiveDefiniteness_default_ =
true;
309 static constexpr bool showMaxResNormOnly_default_ =
false;
312 static constexpr int outputFreq_default_ = -1;
313 static constexpr int defQuorum_default_ = 1;
314 static constexpr bool foldConvergenceDetectionIntoAllreduce_default_ =
false;
315 static constexpr const char * resScale_default_ =
"Norm of Initial Residual";
316 static constexpr const char * label_default_ =
"Belos";
317 static constexpr bool genCondEst_default_ =
false;
320 MagnitudeType convtol_,achievedTol_;
321 int maxIters_, numIters_;
322 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
323 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
324 bool foldConvergenceDetectionIntoAllreduce_;
325 std::string resScale_;
328 Teuchos::ArrayRCP<MagnitudeType> eigenEstimates_;
330 Teuchos::RCP<CGIterationStateBase<ScalarType, MV, DM> > state_;
334 Teuchos::RCP<Teuchos::Time> timerSolve_;
342template<
class ScalarType,
class MV,
class OP,
class DM>
346 maxIters_(maxIters_default_),
348 verbosity_(verbosity_default_),
349 outputStyle_(outputStyle_default_),
350 outputFreq_(outputFreq_default_),
351 defQuorum_(defQuorum_default_),
352 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
353 showMaxResNormOnly_(showMaxResNormOnly_default_),
354 foldConvergenceDetectionIntoAllreduce_(foldConvergenceDetectionIntoAllreduce_default_),
355 resScale_(resScale_default_),
356 genCondEst_(genCondEst_default_),
358 label_(label_default_),
363template<
class ScalarType,
class MV,
class OP,
class DM>
366 const Teuchos::RCP<Teuchos::ParameterList> &
pl ) :
370 maxIters_(maxIters_default_),
372 verbosity_(verbosity_default_),
373 outputStyle_(outputStyle_default_),
374 outputFreq_(outputFreq_default_),
375 defQuorum_(defQuorum_default_),
376 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
377 showMaxResNormOnly_(showMaxResNormOnly_default_),
378 foldConvergenceDetectionIntoAllreduce_(foldConvergenceDetectionIntoAllreduce_default_),
379 resScale_(resScale_default_),
380 genCondEst_(genCondEst_default_),
382 label_(label_default_),
386 problem_.is_null (), std::invalid_argument,
387 "Belos::PseudoBlockCGSolMgr two-argument constructor: "
388 "'problem' is null. You must supply a non-null Belos::LinearProblem "
389 "instance when calling this constructor.");
391 if (!
pl.is_null ()) {
397template<
class ScalarType,
class MV,
class OP,
class DM>
401 using Teuchos::ParameterList;
402 using Teuchos::parameterList;
425 if (params_.is_null ()) {
433 if (
params->isParameter (
"Maximum Iterations")) {
434 maxIters_ =
params->get (
"Maximum Iterations", maxIters_default_);
437 params_->set (
"Maximum Iterations", maxIters_);
438 if (! maxIterTest_.is_null ()) {
439 maxIterTest_->setMaxIters (maxIters_);
444 if (
params->isParameter (
"Assert Positive Definiteness")) {
445 assertPositiveDefiniteness_ =
446 params->get (
"Assert Positive Definiteness",
447 assertPositiveDefiniteness_default_);
450 params_->set (
"Assert Positive Definiteness", assertPositiveDefiniteness_);
453 if (
params->isParameter(
"Fold Convergence Detection Into Allreduce")) {
454 foldConvergenceDetectionIntoAllreduce_ =
params->get(
"Fold Convergence Detection Into Allreduce",
455 foldConvergenceDetectionIntoAllreduce_default_);
459 if (
params->isParameter (
"Timer Label")) {
460 const std::string
tempLabel =
params->get (
"Timer Label", label_default_);
465 params_->set (
"Timer Label", label_);
467 label_ +
": PseudoBlockCGSolMgr total solve time";
468#ifdef BELOS_TEUCHOS_TIME_MONITOR
469 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (
solveLabel);
475 if (
params->isParameter (
"Verbosity")) {
476 if (Teuchos::isParameterType<int> (*
params,
"Verbosity")) {
477 verbosity_ =
params->get (
"Verbosity", verbosity_default_);
479 verbosity_ = (
int) Teuchos::getParameter<Belos::MsgType> (*
params,
"Verbosity");
483 params_->set (
"Verbosity", verbosity_);
484 if (! printer_.is_null ()) {
485 printer_->setVerbosity (verbosity_);
490 if (
params->isParameter (
"Output Style")) {
491 if (Teuchos::isParameterType<int> (*
params,
"Output Style")) {
492 outputStyle_ =
params->get (
"Output Style", outputStyle_default_);
495 outputStyle_ = (
int) Teuchos::getParameter<Belos::OutputType> (*
params,
"Output Style");
500 params_->set (
"Output Style", outputStyle_);
501 outputTest_ = Teuchos::null;
505 if (
params->isParameter (
"Output Stream")) {
509 params_->set (
"Output Stream", outputStream_);
510 if (! printer_.is_null ()) {
511 printer_->setOStream (outputStream_);
517 if (
params->isParameter (
"Output Frequency")) {
518 outputFreq_ =
params->get (
"Output Frequency", outputFreq_default_);
522 params_->set (
"Output Frequency", outputFreq_);
523 if (! outputTest_.is_null ()) {
524 outputTest_->setOutputFrequency (outputFreq_);
529 if (
params->isParameter (
"Estimate Condition Number")) {
530 genCondEst_ =
params->get (
"Estimate Condition Number", genCondEst_default_);
534 if (printer_.is_null ()) {
543 if (
params->isParameter (
"Convergence Tolerance")) {
544 if (
params->isType<MagnitudeType> (
"Convergence Tolerance")) {
545 convtol_ =
params->get (
"Convergence Tolerance",
553 params_->set (
"Convergence Tolerance", convtol_);
554 if (! convTest_.is_null ()) {
555 convTest_->setTolerance (convtol_);
559 if (
params->isParameter (
"Show Maximum Residual Norm Only")) {
560 showMaxResNormOnly_ =
params->get<
bool> (
"Show Maximum Residual Norm Only");
563 params_->set (
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
564 if (! convTest_.is_null ()) {
565 convTest_->setShowMaxResNormOnly (showMaxResNormOnly_);
577 if (
params->isParameter (
"Residual Scaling")) {
580 else if (
params->isParameter (
"Implicit Residual Scaling")) {
594 params_->set (
"Implicit Residual Scaling", resScale_);
597 params_->set (
"Residual Scaling", resScale_);
600 if (! convTest_.is_null ()) {
604 catch (std::exception&
e) {
613 if (
params->isParameter (
"Deflation Quorum")) {
614 defQuorum_ =
params->get (
"Deflation Quorum", defQuorum_);
615 params_->set (
"Deflation Quorum", defQuorum_);
616 if (! convTest_.is_null ()) {
617 convTest_->setQuorum( defQuorum_ );
624 if (maxIterTest_.is_null ()) {
630 convTest_ =
rcp (
new StatusTestResNorm_t (convtol_, defQuorum_, showMaxResNormOnly_));
635 sTest_ =
rcp (
new StatusTestCombo_t (StatusTestCombo_t::OR, maxIterTest_, convTest_));
636 if (Teuchos::nonnull(debugStatusTest_)) {
638 sTest_ =
rcp (
new StatusTestCombo_t (StatusTestCombo_t::OR, sTest_, debugStatusTest_));
646 outputTest_ =
stoFactory.create (printer_, sTest_, outputFreq_,
650 const std::string
solverDesc =
" Pseudo Block CG ";
655 if (timerSolve_.is_null ()) {
657 label_ +
": PseudoBlockCGSolMgr total solve time";
658#ifdef BELOS_TEUCHOS_TIME_MONITOR
659 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (
solveLabel);
668template<
class ScalarType,
class MV,
class OP,
class DM>
669Teuchos::RCP<const Teuchos::ParameterList>
672 using Teuchos::ParameterList;
673 using Teuchos::parameterList;
676 if (validParams_.is_null()) {
680 "The relative residual tolerance that needs to be achieved by the\n"
681 "iterative solver in order for the linear system to be declared converged.");
682 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
683 "The maximum number of block iterations allowed for each\n"
684 "set of RHS solved.");
685 pl->set(
"Assert Positive Definiteness",
static_cast<bool>(assertPositiveDefiniteness_default_),
686 "Whether or not to assert that the linear operator\n"
687 "and the preconditioner are indeed positive definite.");
688 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
689 "What type(s) of solver information should be outputted\n"
690 "to the output stream.");
691 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
692 "What style is used for the solver information outputted\n"
693 "to the output stream.");
694 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
695 "How often convergence information should be outputted\n"
696 "to the output stream.");
697 pl->set(
"Deflation Quorum",
static_cast<int>(defQuorum_default_),
698 "The number of linear systems that need to converge before\n"
699 "they are deflated. This number should be <= block size.");
700 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
701 "A reference-counted pointer to the output stream where all\n"
702 "solver output is sent.");
703 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
704 "When convergence information is printed, only show the maximum\n"
705 "relative residual norm when the block size is greater than one.");
706 pl->set(
"Implicit Residual Scaling", resScale_default_,
707 "The type of scaling used in the residual convergence test.");
708 pl->set(
"Estimate Condition Number",
static_cast<bool>(genCondEst_default_),
709 "Whether or not to estimate the condition number of the preconditioned system.");
715 pl->set(
"Residual Scaling", resScale_default_,
716 "The type of scaling used in the residual convergence test. This "
717 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
718 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
719 "The string to use as a prefix for the timer labels.");
720 pl->set(
"Fold Convergence Detection Into Allreduce",
static_cast<bool>(foldConvergenceDetectionIntoAllreduce_default_),
721 "Merge the allreduce for convergence detection with the one for CG.\n"
722 "This saves one all-reduce, but incurs more computation.");
730template<
class ScalarType,
class MV,
class OP,
class DM>
733 const char prefix[] =
"Belos::PseudoBlockCGSolMgr::solve: ";
740 if (!isSet_) { setParameters( params_ ); }
744 prefix <<
"The linear problem to solve is not ready. You must call "
745 "setProblem() on the Belos::LinearProblem instance before telling the "
746 "Belos solver to solve it.");
750 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
760 problem_->setLSIndex(
currIdx );
764 Teuchos::ParameterList
plist;
766 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
767 if(genCondEst_)
plist.set(
"Max Size For Condest",maxIters_);
770 outputTest_->reset();
777 Teuchos::RCP<CGIteration<ScalarType,MV,OP,DM> >
block_cg_iter;
779 plist.set(
"Fold Convergence Detection Into Allreduce",
780 foldConvergenceDetectionIntoAllreduce_);
798#ifdef BELOS_TEUCHOS_TIME_MONITOR
799 Teuchos::TimeMonitor
slvtimer(*timerSolve_);
813 outputTest_->resetNumCalls();
816 Teuchos::RCP<MV>
R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())),
currIdx );
833 if ( convTest_->getStatus() ==
Passed ) {
836 std::vector<int>
convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP,DM> >(convTest_)->convIndices();
844 problem_->setCurrLS();
850 for (
unsigned int j=0;
j<
convIdx.size(); ++
j) {
882 std::vector<MagnitudeType>
norms;
895 else if ( maxIterTest_->getStatus() ==
Passed ) {
907 else if (Teuchos::nonnull(debugStatusTest_) &&
908 debugStatusTest_->getStatus() ==
Passed) {
925 "Belos::PseudoBlockCGSolMgr::solve(): Invalid return from PseudoBlockCGIter::iterate().");
931 achievedTol_ = MT::one();
932 Teuchos::RCP<MV>
X = problem_->getLHS();
933 MVT::MvInit( *
X, SCT::zero() );
934 printer_->stream(
Warnings) <<
"Belos::PseudoBlockCGSolMgr::solve(): Warning! NaN has been detected!"
938 catch (
const std::exception &
e) {
940 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockCGIter::iterate() at iteration "
942 <<
e.what() << std::endl;
948 problem_->setCurrLS();
963 problem_->setLSIndex(
currIdx );
977#ifdef BELOS_TEUCHOS_TIME_MONITOR
982 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
986 numIters_ = maxIterTest_->getNumIters();
990 const std::vector<MagnitudeType>*
pTestValues = convTest_->getTestValue();
1011template<
class ScalarType,
class MV,
class OP,
class DM>
1014 std::ostringstream
oss;
1015 oss <<
"Belos::PseudoBlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
1022template<
class ScalarType,
class MV,
class OP,
class DM>
1025 Teuchos::ArrayView<MagnitudeType>
offdiag,
1026 Teuchos::ArrayRCP<MagnitudeType>&
lambdas,
1031 using STS = Teuchos::ScalarTraits<ScalarType>;
1040 const int N =
diag.size ();
1044 Teuchos::LAPACK<int,ScalarType> lapack;
1053 (
info < 0, std::logic_error,
"Belos::PseudoBlockCGSolMgr::"
1054 "compute_condnum_tridiag_sym: LAPACK's _PTEQR failed with info = "
1055 <<
info <<
" < 0. This suggests there might be a bug in the way Belos "
1056 "is calling LAPACK. Please report this to the Belos developers.");
1057 for (
int k = 0;
k <
N;
k++) {
1084#ifdef HAVE_BELOS_TPETRA
1087#define BELOS_TPETRA_PSEUDOBLOCKCGSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
1088 BELOS_TPETRA_CALL(Belos::PseudoBlockCGSolMgr, SC, LO, GO, NT)
1090#define BELOS_TPETRA_PSEUDOBLOCKCGSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
1091 BELOS_TPETRA_EXTERN_CALL(Belos::PseudoBlockCGSolMgr, SC, LO, GO, NT)
1093TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_PSEUDOBLOCKCGSOLMGR_EXTERN_CALL)
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...
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.
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.