42#ifndef BELOS_BLOCK_CG_SOLMGR_HPP
43#define BELOS_BLOCK_CG_SOLMGR_HPP
64#include "Teuchos_LAPACK.hpp"
65#ifdef BELOS_TEUCHOS_TIME_MONITOR
66# include "Teuchos_TimeMonitor.hpp"
68#if defined(HAVE_TEUCHOSCORE_CXX11)
69# include <type_traits>
104 template<
class ScalarType,
class MV,
class OP,
105 const bool lapackSupportsScalarType =
120 const Teuchos::RCP<Teuchos::ParameterList>& pl) :
130 template<
class ScalarType,
class MV,
class OP>
157 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
158 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
MagnitudeType;
159 typedef Teuchos::ScalarTraits<MagnitudeType>
MT;
210 const Teuchos::RCP<Teuchos::ParameterList> &pl );
216 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
230 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const override;
241 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
272 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
313 std::string description()
const override;
320 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
331 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
sTest_;
337 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
convTest_;
343 Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> >
ortho_;
408template<
class ScalarType,
class MV,
class OP>
433template<
class ScalarType,
class MV,
class OP>
436 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
458 TEUCHOS_TEST_FOR_EXCEPTION(
problem_.is_null(), std::invalid_argument,
459 "BlockCGSolMgr's constructor requires a nonnull LinearProblem instance.");
464 if (! pl.is_null()) {
469template<
class ScalarType,
class MV,
class OP>
472setParameters (
const Teuchos::RCP<Teuchos::ParameterList> ¶ms)
475 if (
params_ == Teuchos::null) {
483 if (params->isParameter(
"Maximum Iterations")) {
493 if (params->isParameter(
"Block Size")) {
495 TEUCHOS_TEST_FOR_EXCEPTION(
blockSize_ <= 0, std::invalid_argument,
496 "Belos::BlockCGSolMgr: \"Block Size\" must be strictly positive.");
503 if (params->isParameter(
"Adaptive Block Size")) {
511 if (params->isParameter(
"Use Single Reduction")) {
515 if (params->isParameter(
"Fold Convergence Detection Into Allreduce")) {
521 if (params->isParameter(
"Timer Label")) {
522 std::string tempLabel = params->get(
"Timer Label",
label_default_);
525 if (tempLabel !=
label_) {
528 std::string solveLabel =
label_ +
": BlockCGSolMgr total solve time";
529#ifdef BELOS_TEUCHOS_TIME_MONITOR
530 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
532 if (
ortho_ != Teuchos::null) {
539 if (params->isParameter(
"Verbosity")) {
540 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
543 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
553 if (params->isParameter(
"Output Style")) {
554 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
557 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
566 if (params->isParameter(
"Output Stream")) {
567 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
577 if (params->isParameter(
"Output Frequency")) {
593 bool changedOrthoType =
false;
594 if (params->isParameter(
"Orthogonalization")) {
598 changedOrthoType =
true;
604 if (params->isParameter(
"Orthogonalization Constant")) {
605 if (params->isType<
MagnitudeType> (
"Orthogonalization Constant")) {
606 orthoKappa_ = params->get (
"Orthogonalization Constant",
610 orthoKappa_ = params->get (
"Orthogonalization Constant",
618 Teuchos::rcp_dynamic_cast<DGKSOrthoManager<ScalarType,MV,OP> >(
ortho_)->setDepTol(
orthoKappa_ );
624 if (
ortho_ == Teuchos::null || changedOrthoType) {
626 Teuchos::RCP<Teuchos::ParameterList> paramsOrtho;
639 if (params->isParameter(
"Convergence Tolerance")) {
640 if (params->isType<
MagnitudeType> (
"Convergence Tolerance")) {
641 convtol_ = params->get (
"Convergence Tolerance",
654 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
655 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
664 bool newResTest =
false;
667 if (params->isParameter (
"Implicit Residual Scaling")) {
668 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
683 if (params->isParameter(
"Residual Norm")) {
684 if (params->isType<std::string> (
"Residual Norm")) {
688 convTest_->defineResForm(StatusTestResNorm_t::Implicit, normType);
691 catch (std::exception& e) {
706 if (
convTest_.is_null () || newResTest) {
709 if (params->isParameter(
"Residual Norm")) {
710 if (params->isType<std::string> (
"Residual Norm")) {
716 convTest_->defineResForm(StatusTestResNorm_t::Implicit, normType);
720 if (
sTest_.is_null () || newResTest)
731 std::string solverDesc =
" Block CG ";
737 if (params->isParameter(
"Assert Positive Definiteness")) {
744 std::string solveLabel =
label_ +
": BlockCGSolMgr total solve time";
745#ifdef BELOS_TEUCHOS_TIME_MONITOR
746 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
755template<
class ScalarType,
class MV,
class OP>
756Teuchos::RCP<const Teuchos::ParameterList>
759 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
762 if(is_null(validPL)) {
763 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
765 "The relative residual tolerance that needs to be achieved by the\n"
766 "iterative solver in order for the linear system to be declared converged.");
768 "The maximum number of block iterations allowed for each\n"
769 "set of RHS solved.");
771 "The number of vectors in each block.");
773 "Whether the solver manager should adapt to the block size\n"
774 "based on the number of RHS to solve.");
776 "What type(s) of solver information should be outputted\n"
777 "to the output stream.");
779 "What style is used for the solver information outputted\n"
780 "to the output stream.");
782 "How often convergence information should be outputted\n"
783 "to the output stream.");
784 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
785 "A reference-counted pointer to the output stream where all\n"
786 "solver output is sent.");
788 "When convergence information is printed, only show the maximum\n"
789 "relative residual norm when the block size is greater than one.");
791 "Use single reduction iteration when the block size is one.");
793 "The type of scaling used in the residual convergence test.");
795 "The string to use as a prefix for the timer labels.");
797 "The type of orthogonalization to use: DGKS, ICGS, or IMGS.");
799 "Assert for positivity of p^H*A*p in CG iteration.");
801 "The constant used by DGKS orthogonalization to determine\n"
802 "whether another step of classical Gram-Schmidt is necessary.");
804 "Norm used for the convergence check on the residual.");
806 "Merge the allreduce for convergence detection with the one for CG.\n"
807 "This saves one all-reduce, but incurs more computation.");
815template<
class ScalarType,
class MV,
class OP>
819 using Teuchos::rcp_const_cast;
820 using Teuchos::rcp_dynamic_cast;
830 Teuchos::LAPACK<int,ScalarType> lapack;
832 TEUCHOS_TEST_FOR_EXCEPTION( !
problem_->isProblemSet(),
834 "Belos::BlockCGSolMgr::solve(): Linear problem is not ready, setProblem() "
835 "has not been called.");
842 std::vector<int> currIdx, currIdx2;
849 currIdx.resize( numCurrRHS );
850 currIdx2.resize( numCurrRHS );
851 for (
int i=0; i<numCurrRHS; ++i)
852 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
858 for (
int i=0; i<numCurrRHS; ++i)
859 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
861 { currIdx[i] = -1; currIdx2[i] = i; }
869 Teuchos::ParameterList plist;
877 bool isConverged =
true;
884 RCP<CGIteration<ScalarType,MV,OP> > block_cg_iter;
889 plist.set(
"Fold Convergence Detection Into Allreduce",
910#ifdef BELOS_TEUCHOS_TIME_MONITOR
914 while ( numRHS2Solve > 0 ) {
917 std::vector<int> convRHSIdx;
918 std::vector<int> currRHSIdx( currIdx );
919 currRHSIdx.resize(numCurrRHS);
922 block_cg_iter->resetNumIters();
933 block_cg_iter->initializeCG(newstate);
939 block_cg_iter->iterate();
948 std::vector<int> convIdx =
954 if (convIdx.size() == currRHSIdx.size())
964 std::vector<int> unconvIdx(currRHSIdx.size());
965 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
967 for (
unsigned int j=0; j<convIdx.size(); ++j) {
968 if (currRHSIdx[i] == convIdx[j]) {
974 currIdx2[have] = currIdx2[i];
975 currRHSIdx[have++] = currRHSIdx[i];
980 currRHSIdx.resize(have);
981 currIdx2.resize(have);
987 std::vector<MagnitudeType> norms;
988 R_0 =
MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
989 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
992 block_cg_iter->setBlockSize( have );
997 block_cg_iter->initializeCG(defstate);
1004 isConverged =
false;
1012 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
1013 "Belos::BlockCGSolMgr::solve(): Neither the convergence test nor "
1014 "the maximum iteration count test passed. Please report this bug "
1015 "to the Belos developers.");
1018 catch (
const std::exception &e) {
1020 err <<
"Error! Caught std::exception in CGIteration::iterate() at "
1021 <<
"iteration " << block_cg_iter->getNumIters() << std::endl
1022 << e.what() << std::endl;
1032 startPtr += numCurrRHS;
1033 numRHS2Solve -= numCurrRHS;
1034 if ( numRHS2Solve > 0 ) {
1039 currIdx.resize( numCurrRHS );
1040 currIdx2.resize( numCurrRHS );
1041 for (
int i=0; i<numCurrRHS; ++i)
1042 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
1047 for (
int i=0; i<numCurrRHS; ++i)
1048 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
1050 { currIdx[i] = -1; currIdx2[i] = i; }
1059 currIdx.resize( numRHS2Solve );
1070#ifdef BELOS_TEUCHOS_TIME_MONITOR
1087 const std::vector<MagnitudeType>* pTestValues =
1090 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
1091 "Belos::BlockCGSolMgr::solve(): The convergence test's getTestValue() "
1092 "method returned NULL. Please report this bug to the Belos developers.");
1094 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
1095 "Belos::BlockCGSolMgr::solve(): The convergence test's getTestValue() "
1096 "method returned a vector of length zero. Please report this bug to the "
1097 "Belos developers.");
1102 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
1112template<
class ScalarType,
class MV,
class OP>
1115 std::ostringstream oss;
1116 oss <<
"Belos::BlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
Belos concrete class for performing the block conjugate-gradient (CG) iteration.
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.
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)
Stub implementation of BlockCGIter, for ScalarType types for which Teuchos::LAPACK does NOT have a va...
BlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
BlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
static constexpr bool assertPositiveDefiniteness_default_
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
int blockSize_
Current solver values.
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
The linear problem to solve.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Teuchos::RCP< Teuchos::ParameterList > params_
Current parameter list.
static constexpr bool adaptiveBlockSize_default_
Teuchos::RCP< Teuchos::Time > timerSolve_
Solve timer.
MagnitudeType orthoKappa_
Orthogonalization parameter (read from parameter list).
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
Teuchos::ScalarTraits< MagnitudeType > MT
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
int maxIters_
Maximum iteration count (read from parameter list).
static constexpr const char * orthoType_default_
static constexpr const char * resScale_default_
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
Maximum iteration count stopping criterion.
Teuchos::RCP< std::ostream > outputStream_
Output stream to which the output manager prints.
static constexpr bool showMaxResNormOnly_default_
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
Aggregate stopping criterion.
static constexpr int outputFreq_default_
virtual ~BlockCGSolMgr()
Destructor.
MultiVecTraits< ScalarType, MV > MVT
static constexpr int blockSize_default_
Teuchos::ScalarTraits< ScalarType > SCT
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
static constexpr const char * label_default_
static constexpr int verbosity_default_
bool isSet_
Whether or not the parameters have been set (via setParameters()).
static constexpr const char * resNorm_default_
static constexpr int outputStyle_default_
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP > > ortho_
Orthogonalization manager.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
Output "status test" that controls all the other status tests.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
std::string label_
Prefix label for all the timers.
static constexpr bool foldConvergenceDetectionIntoAllreduce_default_
MagnitudeType achievedTol_
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
MagnitudeType convtol_
Convergence tolerance (read from parameter list).
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
BlockCGSolMgr()
Empty constructor for BlockCGSolMgr. This constructor takes no arguments and sets the default values ...
Teuchos::RCP< OutputManager< ScalarType > > printer_
Output manager, that handles printing of different kinds of messages.
static constexpr int maxIters_default_
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > convTest_
Convergence stopping criterion.
bool assertPositiveDefiniteness_
bool foldConvergenceDetectionIntoAllreduce_
int numIters_
Number of iterations taken by the last solve() invocation.
static constexpr bool useSingleReduction_default_
OperatorTraits< ScalarType, MV, OP > OPT
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
static const bool requiresLapack
BlockCGSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Details::SolverManagerRequiresLapack< ScalarType, MV, OP, requiresLapack > base_type
This class implements the preconditioned Conjugate Gradient (CG) iteration.
This class implements the preconditioned single-reduction 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.
Enumeration of all valid Belos (Mat)OrthoManager classes.
Teuchos::RCP< Belos::MatOrthoManager< Scalar, MV, OP > > makeMatOrthoManager(const std::string &ortho, const Teuchos::RCP< const OP > &M, const Teuchos::RCP< OutputManager< Scalar > > &, const std::string &label, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Return an instance of the specified MatOrthoManager subclass.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
std::vector< int > convIndices()
Returns the std::vector containing the indices of the residuals that passed the test.
const std::vector< MagnitudeType > * getTestValue() const
Returns the test value, , computed in most recent call to CheckStatus.
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.
NormType
The type of vector norm to compute.
NormType convertStringToNormType(const std::string &normType)
Convert the given string to its NormType 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.
static const double orthoKappa
DGKS orthogonalization constant.