42 #ifndef BELOS_FIXEDPOINT_SOLMGR_HPP
43 #define BELOS_FIXEDPOINT_SOLMGR_HPP
62 #ifdef BELOS_TEUCHOS_TIME_MONITOR
90 template<
class ScalarType,
class MV,
class OP>
162 return Teuchos::tuple(timerSolve_);
197 convTest_ = userConvStatusTest;
200 sTest_ =
Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
205 std::string solverDesc =
" Fixed Point ";
206 outputTest_->setSolverDesc( solverDesc );
281 static constexpr
int maxIters_default_ = 1000;
282 static constexpr
bool showMaxResNormOnly_default_ =
false;
283 static constexpr
int blockSize_default_ = 1;
286 static constexpr
int outputFreq_default_ = -1;
287 static constexpr
const char * label_default_ =
"Belos";
294 MagnitudeType convtol_;
301 MagnitudeType achievedTol_;
309 int blockSize_, verbosity_, outputStyle_, outputFreq_;
310 bool showMaxResNormOnly_;
324 template<
class ScalarType,
class MV,
class OP>
326 outputStream_(Teuchos::rcpFromRef(std::cout)),
328 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
329 maxIters_(maxIters_default_),
331 blockSize_(blockSize_default_),
332 verbosity_(verbosity_default_),
333 outputStyle_(outputStyle_default_),
334 outputFreq_(outputFreq_default_),
335 showMaxResNormOnly_(showMaxResNormOnly_default_),
336 label_(label_default_),
342 template<
class ScalarType,
class MV,
class OP>
347 outputStream_(Teuchos::rcpFromRef(std::cout)),
349 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
350 maxIters_(maxIters_default_),
352 blockSize_(blockSize_default_),
353 verbosity_(verbosity_default_),
354 outputStyle_(outputStyle_default_),
355 outputFreq_(outputFreq_default_),
356 showMaxResNormOnly_(showMaxResNormOnly_default_),
357 label_(label_default_),
361 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
371 template<
class ScalarType,
class MV,
class OP>
377 if (params_ == Teuchos::null) {
386 maxIters_ = params->
get(
"Maximum Iterations",maxIters_default_);
389 params_->set(
"Maximum Iterations", maxIters_);
390 if (maxIterTest_!=Teuchos::null)
391 maxIterTest_->setMaxIters( maxIters_ );
396 blockSize_ = params->
get(
"Block Size",blockSize_default_);
398 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
401 params_->set(
"Block Size", blockSize_);
406 std::string tempLabel = params->
get(
"Timer Label", label_default_);
409 if (tempLabel != label_) {
411 params_->set(
"Timer Label", label_);
412 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
413 #ifdef BELOS_TEUCHOS_TIME_MONITOR
421 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
422 verbosity_ = params->
get(
"Verbosity", verbosity_default_);
424 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
428 params_->set(
"Verbosity", verbosity_);
429 if (printer_ != Teuchos::null)
430 printer_->setVerbosity(verbosity_);
435 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
436 outputStyle_ = params->
get(
"Output Style", outputStyle_default_);
438 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
442 params_->set(
"Output Style", outputStyle_);
443 outputTest_ = Teuchos::null;
448 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
451 params_->set(
"Output Stream", outputStream_);
452 if (printer_ != Teuchos::null)
453 printer_->setOStream( outputStream_ );
459 outputFreq_ = params->
get(
"Output Frequency", outputFreq_default_);
463 params_->set(
"Output Frequency", outputFreq_);
464 if (outputTest_ != Teuchos::null)
465 outputTest_->setOutputFrequency( outputFreq_ );
469 if (printer_ == Teuchos::null) {
478 if (params->
isParameter(
"Convergence Tolerance")) {
479 if (params->
isType<MagnitudeType> (
"Convergence Tolerance")) {
480 convtol_ = params->
get (
"Convergence Tolerance",
488 params_->set(
"Convergence Tolerance", convtol_);
489 if (convTest_ != Teuchos::null)
490 convTest_->setTolerance( convtol_ );
493 if (params->
isParameter(
"Show Maximum Residual Norm Only")) {
494 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
497 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
498 if (convTest_ != Teuchos::null)
499 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
505 if (maxIterTest_ == Teuchos::null)
509 if (convTest_ == Teuchos::null)
510 convTest_ =
Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
512 if (sTest_ == Teuchos::null)
513 sTest_ =
Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
515 if (outputTest_ == Teuchos::null) {
523 std::string solverDesc =
" Fixed Point ";
524 outputTest_->setSolverDesc( solverDesc );
529 if (timerSolve_ == Teuchos::null) {
530 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
531 #ifdef BELOS_TEUCHOS_TIME_MONITOR
541 template<
class ScalarType,
class MV,
class OP>
554 "The relative residual tolerance that needs to be achieved by the\n"
555 "iterative solver in order for the linear system to be declared converged.");
556 pl->
set(
"Maximum Iterations", static_cast<int>(maxIters_default_),
557 "The maximum number of block iterations allowed for each\n"
558 "set of RHS solved.");
559 pl->
set(
"Block Size", static_cast<int>(blockSize_default_),
560 "The number of vectors in each block.");
561 pl->
set(
"Verbosity", static_cast<int>(verbosity_default_),
562 "What type(s) of solver information should be outputted\n"
563 "to the output stream.");
564 pl->
set(
"Output Style", static_cast<int>(outputStyle_default_),
565 "What style is used for the solver information outputted\n"
566 "to the output stream.");
567 pl->
set(
"Output Frequency", static_cast<int>(outputFreq_default_),
568 "How often convergence information should be outputted\n"
569 "to the output stream.");
570 pl->
set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
571 "A reference-counted pointer to the output stream where all\n"
572 "solver output is sent.");
573 pl->
set(
"Show Maximum Residual Norm Only", static_cast<bool>(showMaxResNormOnly_default_),
574 "When convergence information is printed, only show the maximum\n"
575 "relative residual norm when the block size is greater than one.");
576 pl->
set(
"Timer Label", static_cast<const char *>(label_default_),
577 "The string to use as a prefix for the timer labels.");
585 template<
class ScalarType,
class MV,
class OP>
589 using Teuchos::rcp_const_cast;
590 using Teuchos::rcp_dynamic_cast;
597 setParameters(Teuchos::parameterList(*getValidParameters()));
602 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
603 "has not been called.");
607 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
608 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
610 std::vector<int> currIdx, currIdx2;
611 currIdx.resize( blockSize_ );
612 currIdx2.resize( blockSize_ );
613 for (
int i=0; i<numCurrRHS; ++i)
614 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
615 for (
int i=numCurrRHS; i<blockSize_; ++i)
616 { currIdx[i] = -1; currIdx2[i] = i; }
619 problem_->setLSIndex( currIdx );
624 plist.
set(
"Block Size",blockSize_);
627 outputTest_->reset();
631 bool isConverged =
true;
636 RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
641 #ifdef BELOS_TEUCHOS_TIME_MONITOR
645 while ( numRHS2Solve > 0 ) {
648 std::vector<int> convRHSIdx;
649 std::vector<int> currRHSIdx( currIdx );
650 currRHSIdx.resize(numCurrRHS);
653 block_fp_iter->resetNumIters();
656 outputTest_->resetNumCalls();
659 RCP<MV> R_0 = MVT::CloneViewNonConst( *(rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
664 block_fp_iter->initializeFixedPoint(newstate);
670 block_fp_iter->iterate();
674 if (convTest_->getStatus() ==
Passed) {
678 std::vector<int> convIdx = convTest_->convIndices();
683 if (convIdx.size() == currRHSIdx.size())
688 problem_->setCurrLS();
693 std::vector<int> unconvIdx(currRHSIdx.size());
694 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
696 for (
unsigned int j=0; j<convIdx.size(); ++j) {
697 if (currRHSIdx[i] == convIdx[j]) {
703 currIdx2[have] = currIdx2[i];
704 currRHSIdx[have++] = currRHSIdx[i];
709 currRHSIdx.resize(have);
710 currIdx2.resize(have);
713 problem_->setLSIndex( currRHSIdx );
716 std::vector<MagnitudeType> norms;
717 R_0 = MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
718 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
721 block_fp_iter->setBlockSize( have );
726 block_fp_iter->initializeFixedPoint(defstate);
732 else if (maxIterTest_->getStatus() ==
Passed) {
742 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
743 "the maximum iteration count test passed. Please report this bug "
744 "to the Belos developers.");
749 achievedTol_ = MT::one();
751 MVT::MvInit( *X, SCT::zero() );
752 printer_->stream(
Warnings) <<
"Belos::FixedPointSolMgr::solve(): Warning! NaN has been detected!"
756 catch (
const std::exception &e) {
757 std::ostream& err = printer_->stream (
Errors);
758 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at "
759 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
760 << e.what() << std::endl;
767 problem_->setCurrLS();
770 startPtr += numCurrRHS;
771 numRHS2Solve -= numCurrRHS;
772 if ( numRHS2Solve > 0 ) {
773 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
776 currIdx.resize( blockSize_ );
777 currIdx2.resize( blockSize_ );
778 for (
int i=0; i<numCurrRHS; ++i)
779 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
780 for (
int i=numCurrRHS; i<blockSize_; ++i)
781 { currIdx[i] = -1; currIdx2[i] = i; }
784 problem_->setLSIndex( currIdx );
787 block_fp_iter->setBlockSize( blockSize_ );
790 currIdx.resize( numRHS2Solve );
801 #ifdef BELOS_TEUCHOS_TIME_MONITOR
812 numIters_ = maxIterTest_->getNumIters();
817 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
820 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
821 "method returned NULL. Please report this bug to the Belos developers.");
824 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
825 "method returned a vector of length zero. Please report this bug to the "
826 "Belos developers.");
831 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
841 template<
class ScalarType,
class MV,
class OP>
844 std::ostringstream oss;
Collection of types and exceptions used within the Belos solvers.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
The Belos::FixedPointSolMgr provides a powerful and fully-featured solver manager over the FixedPoint...
Class which manages the output and verbosity of the Belos solvers.
bool is_null(const boost::shared_ptr< T > &p)
Belos concrete class for performing fixed point iteration iteration.
FixedPointSolMgr()
Empty constructor for FixedPointSolMgr. This constructor takes no arguments and sets the default valu...
T & get(const std::string &name, T def_value)
Belos concrete class for performing the conjugate-gradient (CG) iteration.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
An abstract class of StatusTest for stopping criteria using residual norms.
An implementation of StatusTestResNorm using a family of residual norms.
static const double convTol
Default convergence tolerance.
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
bool isParameter(const std::string &name) const
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
virtual ~FixedPointSolMgr()
Destructor.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
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...
ReturnType
Whether the Belos solve converged for all linear systems.
void replaceUserConvStatusTest(const Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP > > &userConvStatusTest)
Set user-defined convergence status test.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const MV > R
The current residual.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
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.
Structure to contain pointers to FixedPointIteration state variables.
FixedPointSolMgrLinearProblemFailure(const std::string &what_arg)
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
bool isType(const std::string &name) const
A class for extending the status testing capabilities of Belos via logical combinations.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
Default parameters common to most Belos solvers.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Belos header file which uses auto-configuration information to include necessary C++ headers...
This class implements the preconditioned fixed point iteration.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.