42#ifndef BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP
43#define BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP
63#include "Teuchos_LAPACK.hpp"
64#ifdef BELOS_TEUCHOS_TIME_MONITOR
65#include "Teuchos_TimeMonitor.hpp"
102 template<
class ScalarType,
class MV,
class OP,
103 const bool supportsScalarType =
107 Belos::Details::LapackSupportsScalar<ScalarType>::value>
119 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
124 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
129 template<
class ScalarType,
class MV,
class OP>
136 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
137 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
MagnitudeType;
138 typedef Teuchos::ScalarTraits<MagnitudeType>
MT;
168 const Teuchos::RCP<Teuchos::ParameterList> &pl );
174 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
188 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const override;
199 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
235 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
247 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
288 std::string description()
const override;
293 void compute_condnum_tridiag_sym(Teuchos::ArrayView<MagnitudeType> diag,
294 Teuchos::ArrayView<MagnitudeType> offdiag,
295 Teuchos::ArrayRCP<MagnitudeType>& lambdas,
296 ScalarType & lambda_min,
297 ScalarType & lambda_max,
298 ScalarType & ConditionNumber );
301 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
308 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
sTest_;
310 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
convTest_;
357template<
class ScalarType,
class MV,
class OP>
378template<
class ScalarType,
class MV,
class OP>
381 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
400 TEUCHOS_TEST_FOR_EXCEPTION(
401 problem_.is_null (), std::invalid_argument,
402 "Belos::PseudoBlockCGSolMgr two-argument constructor: "
403 "'problem' is null. You must supply a non-null Belos::LinearProblem "
404 "instance when calling this constructor.");
406 if (! pl.is_null ()) {
412template<
class ScalarType,
class MV,
class OP>
415setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
417 using Teuchos::ParameterList;
418 using Teuchos::parameterList;
443 params_ = parameterList (defaultParams->name ());
445 params->validateParameters (*defaultParams);
449 if (params->isParameter (
"Maximum Iterations")) {
460 if (params->isParameter (
"Assert Positive Definiteness")) {
462 params->get (
"Assert Positive Definiteness",
469 if (params->isParameter(
"Fold Convergence Detection Into Allreduce")) {
475 if (params->isParameter (
"Timer Label")) {
476 const std::string tempLabel = params->get (
"Timer Label",
label_default_);
479 if (tempLabel !=
label_) {
482 const std::string solveLabel =
483 label_ +
": PseudoBlockCGSolMgr total solve time";
484#ifdef BELOS_TEUCHOS_TIME_MONITOR
485 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
491 if (params->isParameter (
"Verbosity")) {
492 if (Teuchos::isParameterType<int> (*params,
"Verbosity")) {
495 verbosity_ = (int) Teuchos::getParameter<Belos::MsgType> (*params,
"Verbosity");
506 if (params->isParameter (
"Output Style")) {
507 if (Teuchos::isParameterType<int> (*params,
"Output Style")) {
511 outputStyle_ = (int) Teuchos::getParameter<Belos::OutputType> (*params,
"Output Style");
521 if (params->isParameter (
"Output Stream")) {
522 outputStream_ = params->get<RCP<std::ostream> > (
"Output Stream");
533 if (params->isParameter (
"Output Frequency")) {
545 if (params->isParameter (
"Estimate Condition Number")) {
559 if (params->isParameter (
"Convergence Tolerance")) {
560 if (params->isType<
MagnitudeType> (
"Convergence Tolerance")) {
561 convtol_ = params->get (
"Convergence Tolerance",
575 if (params->isParameter (
"Show Maximum Residual Norm Only")) {
586 bool newResTest =
false;
592 bool implicitResidualScalingName =
false;
593 if (params->isParameter (
"Residual Scaling")) {
594 tempResScale = params->get<std::string> (
"Residual Scaling");
596 else if (params->isParameter (
"Implicit Residual Scaling")) {
597 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
598 implicitResidualScalingName =
true;
609 if (implicitResidualScalingName) {
620 catch (std::exception& e) {
629 if (params->isParameter (
"Deflation Quorum")) {
645 if (
convTest_.is_null () || newResTest) {
650 if (
sTest_.is_null () || newResTest) {
662 const std::string solverDesc =
" Pseudo Block CG ";
668 const std::string solveLabel =
669 label_ +
": PseudoBlockCGSolMgr total solve time";
670#ifdef BELOS_TEUCHOS_TIME_MONITOR
671 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
680template<
class ScalarType,
class MV,
class OP>
681Teuchos::RCP<const Teuchos::ParameterList>
684 using Teuchos::ParameterList;
685 using Teuchos::parameterList;
690 RCP<ParameterList> pl = parameterList ();
692 "The relative residual tolerance that needs to be achieved by the\n"
693 "iterative solver in order for the linear system to be declared converged.");
695 "The maximum number of block iterations allowed for each\n"
696 "set of RHS solved.");
698 "Whether or not to assert that the linear operator\n"
699 "and the preconditioner are indeed positive definite.");
701 "What type(s) of solver information should be outputted\n"
702 "to the output stream.");
704 "What style is used for the solver information outputted\n"
705 "to the output stream.");
707 "How often convergence information should be outputted\n"
708 "to the output stream.");
710 "The number of linear systems that need to converge before\n"
711 "they are deflated. This number should be <= block size.");
712 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
713 "A reference-counted pointer to the output stream where all\n"
714 "solver output is sent.");
716 "When convergence information is printed, only show the maximum\n"
717 "relative residual norm when the block size is greater than one.");
719 "The type of scaling used in the residual convergence test.");
721 "Whether or not to estimate the condition number of the preconditioned system.");
728 "The type of scaling used in the residual convergence test. This "
729 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
731 "The string to use as a prefix for the timer labels.");
733 "Merge the allreduce for convergence detection with the one for CG.\n"
734 "This saves one all-reduce, but incurs more computation.");
742template<
class ScalarType,
class MV,
class OP>
745 const char prefix[] =
"Belos::PseudoBlockCGSolMgr::solve: ";
752 TEUCHOS_TEST_FOR_EXCEPTION
754 prefix <<
"The linear problem to solve is not ready. You must call "
755 "setProblem() on the Belos::LinearProblem instance before telling the "
756 "Belos solver to solve it.");
761 int numCurrRHS = numRHS2Solve;
763 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
764 for (
int i=0; i<numRHS2Solve; ++i) {
765 currIdx[i] = startPtr+i;
774 Teuchos::ParameterList plist;
783 bool isConverged =
true;
787 Teuchos::RCP<CGIteration<ScalarType,MV,OP> > block_cg_iter;
788 if (numRHS2Solve == 1) {
789 plist.set(
"Fold Convergence Detection Into Allreduce",
800 bool condEstPerf =
false;
804#ifdef BELOS_TEUCHOS_TIME_MONITOR
808 while ( numRHS2Solve > 0 ) {
811 std::vector<int> convRHSIdx;
812 std::vector<int> currRHSIdx( currIdx );
813 currRHSIdx.resize(numCurrRHS);
816 block_cg_iter->resetNumIters();
827 block_cg_iter->initializeCG(newState);
834 block_cg_iter->iterate();
844 std::vector<int> convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP> >(
convTest_)->convIndices();
848 if (convIdx.size() == currRHSIdx.size())
856 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
858 for (
unsigned int j=0; j<convIdx.size(); ++j) {
859 if (currRHSIdx[i] == convIdx[j]) {
865 currIdx2[have] = currIdx2[i];
866 currRHSIdx[have++] = currRHSIdx[i];
869 currRHSIdx.resize(have);
870 currIdx2.resize(have);
873 if (currRHSIdx[0] != 0 &&
genCondEst_ && !condEstPerf)
876 ScalarType l_min, l_max;
877 Teuchos::ArrayView<MagnitudeType> diag = block_cg_iter->getDiag();
878 Teuchos::ArrayView<MagnitudeType> offdiag = block_cg_iter->getOffDiag();
882 block_cg_iter->setDoCondEst(
false);
890 std::vector<MagnitudeType> norms;
891 R_0 =
MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
892 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
897 block_cg_iter->initializeCG(defstate);
919 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
920 "Belos::PseudoBlockCGSolMgr::solve(): Invalid return from PseudoBlockCGIter::iterate().");
923 catch (
const std::exception &e) {
924 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockCGIter::iterate() at iteration "
925 << block_cg_iter->getNumIters() << std::endl
926 << e.what() << std::endl;
935 startPtr += numCurrRHS;
936 numRHS2Solve -= numCurrRHS;
938 if ( numRHS2Solve > 0 ) {
940 numCurrRHS = numRHS2Solve;
941 currIdx.resize( numCurrRHS );
942 currIdx2.resize( numCurrRHS );
943 for (
int i=0; i<numCurrRHS; ++i)
944 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
950 currIdx.resize( numRHS2Solve );
961#ifdef BELOS_TEUCHOS_TIME_MONITOR
974 const std::vector<MagnitudeType>* pTestValues =
convTest_->getTestValue();
975 if (pTestValues != NULL && pTestValues->size () > 0) {
976 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
981 ScalarType l_min, l_max;
982 Teuchos::ArrayView<MagnitudeType> diag = block_cg_iter->getDiag();
983 Teuchos::ArrayView<MagnitudeType> offdiag = block_cg_iter->getOffDiag();
995template<
class ScalarType,
class MV,
class OP>
998 std::ostringstream oss;
999 oss <<
"Belos::PseudoBlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
1006template<
class ScalarType,
class MV,
class OP>
1010 Teuchos::ArrayView<MagnitudeType> offdiag,
1011 Teuchos::ArrayRCP<MagnitudeType>& lambdas,
1012 ScalarType & lambda_min,
1013 ScalarType & lambda_max,
1014 ScalarType & ConditionNumber )
1016 typedef Teuchos::ScalarTraits<ScalarType> STS;
1025 const int N = diag.size ();
1026 ScalarType scalar_dummy;
1027 std::vector<MagnitudeType> mag_dummy(4*N);
1029 Teuchos::LAPACK<int,ScalarType> lapack;
1031 lambdas.resize(N, 0.0);
1032 lambda_min = STS::one ();
1033 lambda_max = STS::one ();
1035 lapack.PTEQR (char_N, N, diag.getRawPtr (), offdiag.getRawPtr (),
1036 &scalar_dummy, 1, &mag_dummy[0], &info);
1037 TEUCHOS_TEST_FOR_EXCEPTION
1038 (info < 0, std::logic_error,
"Belos::PseudoBlockCGSolMgr::"
1039 "compute_condnum_tridiag_sym: LAPACK's _PTEQR failed with info = "
1040 << info <<
" < 0. This suggests there might be a bug in the way Belos "
1041 "is calling LAPACK. Please report this to the Belos developers.");
1042 for (
int k = 0; k < N; k++) {
1043 lambdas[k] = diag[N - 1 - k];
1045 lambda_min = Teuchos::as<ScalarType> (diag[N-1]);
1046 lambda_max = Teuchos::as<ScalarType> (diag[0]);
1053 if (STS::real (lambda_max) < STS::real (lambda_min)) {
1054 ConditionNumber = STS::one ();
1058 ConditionNumber = lambda_max / lambda_min;
Belos concrete class for performing the conjugate-gradient (CG) iteration.
Belos concrete class for performing a single-reduction conjugate-gradient (CG) iteration.
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.
BelosError(const std::string &what_arg)
This class implements the preconditioned Conjugate Gradient (CG) iteration.
Base class for Belos::SolverManager subclasses which normally can only compile with ScalarType types ...
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
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 pseudo-block CG iteration, where the basic CG algorithm is performed on all...
PseudoBlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
PseudoBlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
Teuchos::RCP< const Teuchos::ParameterList > validParams_
List of valid parameters and their default values.
Teuchos::ArrayRCP< MagnitudeType > getEigenEstimates() const
Teuchos::RCP< Teuchos::ParameterList > params_
Teuchos::ArrayRCP< MagnitudeType > eigenEstimates_
OperatorTraits< ScalarType, MV, OP > OPT
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
Teuchos::RCP< std::ostream > outputStream_
static constexpr int verbosity_default_
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 > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Teuchos::ScalarTraits< ScalarType > SCT
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
bool assertPositiveDefiniteness_
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > getResidualStatusTest() const
Return the residual status test.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
void compute_condnum_tridiag_sym(Teuchos::ArrayView< MagnitudeType > diag, Teuchos::ArrayView< MagnitudeType > offdiag, Teuchos::ArrayRCP< MagnitudeType > &lambdas, ScalarType &lambda_min, ScalarType &lambda_max, ScalarType &ConditionNumber)
static constexpr int outputFreq_default_
MagnitudeType achievedTol_
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
ScalarType getConditionEstimate() const
Gets the estimated condition number.
MultiVecTraits< ScalarType, MV > MVT
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
static constexpr int defQuorum_default_
Teuchos::ScalarTraits< MagnitudeType > MT
int getNumIters() const override
Get the iteration count for the most recent call to solve().
virtual ~PseudoBlockCGSolMgr()
Destructor.
bool foldConvergenceDetectionIntoAllreduce_
static constexpr bool assertPositiveDefiniteness_default_
static constexpr int outputStyle_default_
static constexpr bool genCondEst_default_
Teuchos::RCP< Teuchos::Time > timerSolve_
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
static constexpr bool showMaxResNormOnly_default_
static constexpr bool foldConvergenceDetectionIntoAllreduce_default_
static constexpr int maxIters_default_
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > convTest_
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
Teuchos::RCP< OutputManager< ScalarType > > printer_
PseudoBlockCGSolMgr()
Empty constructor for BlockCGSolMgr. This constructor takes no arguments and sets the default values ...
static constexpr const char * label_default_
static constexpr const char * resScale_default_
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > getResidualStatusTest() const
static const bool scalarTypeIsSupported
virtual ~PseudoBlockCGSolMgr()
PseudoBlockCGSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Details::SolverManagerRequiresLapack< ScalarType, MV, OP, scalarTypeIsSupported > base_type
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
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.
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< const MV > R
The current residual.
Default parameters common to most Belos solvers.
static const double convTol
Default convergence tolerance.