10#ifndef BELOS_FIXEDPOINT_SOLMGR_HPP
11#define BELOS_FIXEDPOINT_SOLMGR_HPP
32#ifdef BELOS_TEUCHOS_TIME_MONITOR
33# include "Teuchos_TimeMonitor.hpp"
60 template<
class ScalarType,
class MV,
class OP,
class DM = DefaultDenseMatrix<
int, ScalarType>>
66 typedef Teuchos::ScalarTraits<ScalarType> SCT;
67 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
68 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
100 const Teuchos::RCP<Teuchos::ParameterList> &pl );
106 Teuchos::RCP<SolverManager<ScalarType, MV, OP, DM> >
clone ()
const override {
131 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
132 return Teuchos::tuple(timerSolve_);
161 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
166 debugStatusTest_ = debugStatusTest;
170 sTest_ = Teuchos::null;
171 outputTest_ = Teuchos::null;
179 convTest_ = userConvStatusTest;
182 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
187 std::string solverDesc =
" Fixed Point ";
188 outputTest_->setSolverDesc( solverDesc );
235 Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > problem_;
238 Teuchos::RCP<OutputManager<ScalarType> > printer_;
240 Teuchos::RCP<std::ostream> outputStream_;
246 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > sTest_;
249 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP,DM> > maxIterTest_;
252 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP,DM> > convTest_;
255 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP,DM> > outputTest_;
258 Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > debugStatusTest_;
261 Teuchos::RCP<Teuchos::ParameterList> params_;
266 static constexpr int maxIters_default_ = 1000;
267 static constexpr bool showMaxResNormOnly_default_ =
false;
268 static constexpr int blockSize_default_ = 1;
271 static constexpr int outputFreq_default_ = -1;
272 static constexpr const char * label_default_ =
"Belos";
279 MagnitudeType convtol_;
286 MagnitudeType achievedTol_;
294 int blockSize_, verbosity_, outputStyle_, outputFreq_;
295 bool showMaxResNormOnly_;
301 Teuchos::RCP<Teuchos::Time> timerSolve_;
309template<
class ScalarType,
class MV,
class OP,
class DM>
311 outputStream_(Teuchos::rcpFromRef(std::cout)),
313 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
314 maxIters_(maxIters_default_),
316 blockSize_(blockSize_default_),
317 verbosity_(verbosity_default_),
318 outputStyle_(outputStyle_default_),
319 outputFreq_(outputFreq_default_),
320 showMaxResNormOnly_(showMaxResNormOnly_default_),
321 label_(label_default_),
327template<
class ScalarType,
class MV,
class OP,
class DM>
330 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
332 outputStream_(Teuchos::rcpFromRef(std::cout)),
334 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
335 maxIters_(maxIters_default_),
337 blockSize_(blockSize_default_),
338 verbosity_(verbosity_default_),
339 outputStyle_(outputStyle_default_),
340 outputFreq_(outputFreq_default_),
341 showMaxResNormOnly_(showMaxResNormOnly_default_),
342 label_(label_default_),
345 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
346 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
351 if (! pl.is_null()) {
356template<
class ScalarType,
class MV,
class OP,
class DM>
359setParameters (
const Teuchos::RCP<Teuchos::ParameterList> ¶ms)
362 if (params_ == Teuchos::null) {
370 if (params->isParameter(
"Maximum Iterations")) {
371 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
374 params_->set(
"Maximum Iterations", maxIters_);
375 if (maxIterTest_!=Teuchos::null)
376 maxIterTest_->setMaxIters( maxIters_ );
380 if (params->isParameter(
"Block Size")) {
381 blockSize_ = params->get(
"Block Size",blockSize_default_);
382 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
383 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
386 params_->set(
"Block Size", blockSize_);
390 if (params->isParameter(
"Timer Label")) {
391 std::string tempLabel = params->get(
"Timer Label", label_default_);
394 if (tempLabel != label_) {
396 params_->set(
"Timer Label", label_);
397 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
398#ifdef BELOS_TEUCHOS_TIME_MONITOR
399 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
405 if (params->isParameter(
"Verbosity")) {
406 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
407 verbosity_ = params->get(
"Verbosity", verbosity_default_);
409 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
413 params_->set(
"Verbosity", verbosity_);
414 if (printer_ != Teuchos::null)
415 printer_->setVerbosity(verbosity_);
419 if (params->isParameter(
"Output Style")) {
420 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
421 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
423 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
427 params_->set(
"Output Style", outputStyle_);
428 outputTest_ = Teuchos::null;
432 if (params->isParameter(
"Output Stream")) {
433 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
436 params_->set(
"Output Stream", outputStream_);
437 if (printer_ != Teuchos::null)
438 printer_->setOStream( outputStream_ );
443 if (params->isParameter(
"Output Frequency")) {
444 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
448 params_->set(
"Output Frequency", outputFreq_);
449 if (outputTest_ != Teuchos::null)
450 outputTest_->setOutputFrequency( outputFreq_ );
454 if (printer_ == Teuchos::null) {
463 if (params->isParameter(
"Convergence Tolerance")) {
464 if (params->isType<MagnitudeType> (
"Convergence Tolerance")) {
465 convtol_ = params->get (
"Convergence Tolerance",
473 params_->set(
"Convergence Tolerance", convtol_);
474 if (convTest_ != Teuchos::null)
475 convTest_->setTolerance( convtol_ );
478 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
479 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
482 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
483 if (convTest_ != Teuchos::null)
484 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
490 if (maxIterTest_ == Teuchos::null)
494 if (convTest_ == Teuchos::null)
495 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
497 if (sTest_ == Teuchos::null) {
498 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
504 if (Teuchos::nonnull(debugStatusTest_)) {
505 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, sTest_, debugStatusTest_ ) );
509 if (outputTest_ == Teuchos::null) {
517 std::string solverDesc =
" Fixed Point ";
518 outputTest_->setSolverDesc( solverDesc );
523 if (timerSolve_ == Teuchos::null) {
524 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
525#ifdef BELOS_TEUCHOS_TIME_MONITOR
526 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
535template<
class ScalarType,
class MV,
class OP,
class DM>
536Teuchos::RCP<const Teuchos::ParameterList>
539 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
542 if(is_null(validPL)) {
543 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
548 "The relative residual tolerance that needs to be achieved by the\n"
549 "iterative solver in order for the linear system to be declared converged.");
550 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
551 "The maximum number of block iterations allowed for each\n"
552 "set of RHS solved.");
553 pl->set(
"Block Size",
static_cast<int>(blockSize_default_),
554 "The number of vectors in each block.");
555 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
556 "What type(s) of solver information should be outputted\n"
557 "to the output stream.");
558 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
559 "What style is used for the solver information outputted\n"
560 "to the output stream.");
561 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
562 "How often convergence information should be outputted\n"
563 "to the output stream.");
564 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
565 "A reference-counted pointer to the output stream where all\n"
566 "solver output is sent.");
567 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
568 "When convergence information is printed, only show the maximum\n"
569 "relative residual norm when the block size is greater than one.");
570 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
571 "The string to use as a prefix for the timer labels.");
579template<
class ScalarType,
class MV,
class OP,
class DM>
583 using Teuchos::rcp_const_cast;
584 using Teuchos::rcp_dynamic_cast;
596 TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
598 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
599 "has not been called.");
604 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
606 std::vector<int> currIdx, currIdx2;
607 currIdx.resize( blockSize_ );
608 currIdx2.resize( blockSize_ );
609 for (
int i=0; i<numCurrRHS; ++i)
610 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
611 for (
int i=numCurrRHS; i<blockSize_; ++i)
612 { currIdx[i] = -1; currIdx2[i] = i; }
615 problem_->setLSIndex( currIdx );
619 Teuchos::ParameterList plist;
620 plist.set(
"Block Size",blockSize_);
623 outputTest_->reset();
627 bool isConverged =
true;
632 RCP<FixedPointIteration<ScalarType,MV,OP,DM> > block_fp_iter;
637#ifdef BELOS_TEUCHOS_TIME_MONITOR
638 Teuchos::TimeMonitor slvtimer(*timerSolve_);
641 while ( numRHS2Solve > 0 ) {
644 std::vector<int> convRHSIdx;
645 std::vector<int> currRHSIdx( currIdx );
646 currRHSIdx.resize(numCurrRHS);
649 block_fp_iter->resetNumIters();
652 outputTest_->resetNumCalls();
660 block_fp_iter->initializeFixedPoint(newstate);
666 block_fp_iter->iterate();
670 if (convTest_->getStatus() ==
Passed) {
674 std::vector<int> convIdx = convTest_->convIndices();
679 if (convIdx.size() == currRHSIdx.size())
684 problem_->setCurrLS();
689 std::vector<int> unconvIdx(currRHSIdx.size());
690 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
692 for (
unsigned int j=0; j<convIdx.size(); ++j) {
693 if (currRHSIdx[i] == convIdx[j]) {
699 currIdx2[have] = currIdx2[i];
700 currRHSIdx[have++] = currRHSIdx[i];
705 currRHSIdx.resize(have);
706 currIdx2.resize(have);
709 problem_->setLSIndex( currRHSIdx );
712 std::vector<MagnitudeType> norms;
713 R_0 =
MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
714 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
717 block_fp_iter->setBlockSize( have );
722 block_fp_iter->initializeFixedPoint(defstate);
728 else if (maxIterTest_->getStatus() ==
Passed) {
742 else if (Teuchos::nonnull(debugStatusTest_) &&
743 debugStatusTest_->getStatus() ==
Passed) {
750 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
751 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
752 "the maximum iteration count test passed. Please report this bug "
753 "to the Belos developers.");
759 achievedTol_ = MT::one();
760 Teuchos::RCP<MV> X = problem_->getLHS();
762 printer_->stream(
Warnings) <<
"Belos::FixedPointSolMgr::solve(): Warning! NaN has been detected!"
766 catch (
const std::exception &e) {
768 std::ostream& err = printer_->stream (
Errors);
769 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at "
770 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
771 << e.what() << std::endl;
778 problem_->setCurrLS();
781 startPtr += numCurrRHS;
782 numRHS2Solve -= numCurrRHS;
783 if ( numRHS2Solve > 0 ) {
784 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
787 currIdx.resize( blockSize_ );
788 currIdx2.resize( blockSize_ );
789 for (
int i=0; i<numCurrRHS; ++i)
790 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
791 for (
int i=numCurrRHS; i<blockSize_; ++i)
792 { currIdx[i] = -1; currIdx2[i] = i; }
795 problem_->setLSIndex( currIdx );
798 block_fp_iter->setBlockSize( blockSize_ );
801 currIdx.resize( numRHS2Solve );
812#ifdef BELOS_TEUCHOS_TIME_MONITOR
818 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
823 numIters_ = maxIterTest_->getNumIters();
828 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
830 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
831 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
832 "method returned NULL. Please report this bug to the Belos developers.");
834 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
835 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
836 "method returned a vector of length zero. Please report this bug to the "
837 "Belos developers.");
842 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
852template<
class ScalarType,
class MV,
class OP,
class DM>
855 std::ostringstream oss;
856 oss <<
"Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
862#ifdef HAVE_BELOS_TPETRA
865#define BELOS_TPETRA_FIXEDPOINTSOLMGR_NOEXTERN_CALL(SC, LO, GO, NT) \
866 BELOS_TPETRA_CALL(Belos::FixedPointSolMgr, SC, LO, GO, NT)
868#define BELOS_TPETRA_FIXEDPOINTSOLMGR_EXTERN_CALL(SC, LO, GO, NT) \
869 BELOS_TPETRA_EXTERN_CALL(Belos::FixedPointSolMgr, SC, LO, GO, NT)
871TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR(BELOS_TPETRA_FIXEDPOINTSOLMGR_EXTERN_CALL)
Belos concrete class for performing the conjugate-gradient (CG) iteration.
Belos header file which uses auto-configuration information to include necessary C++ headers.
Belos concrete class for performing fixed point iteration iteration.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
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.
Full specialization of Belos::DenseMatTraits for Teuchos::SerialDenseMatrix with ordinal type int and...
Collection of types and exceptions used within the Belos solvers.
BelosError(const std::string &what_arg)
This class implements the preconditioned fixed point iteration.
Teuchos::RCP< SolverManager< ScalarType, MV, OP, DM > > clone() const override
clone for Inverted Injection (DII)
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
virtual ~FixedPointSolMgr()
Destructor.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
void replaceUserConvStatusTest(const Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP, DM > > &userConvStatusTest)
Set user-defined convergence status test.
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::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem) override
Set the linear problem that needs to be solved.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
FixedPointSolMgr()
Empty constructor for FixedPointSolMgr. This constructor takes no arguments and sets the default valu...
std::string description() const override
Method to return description of the block CG solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
FixedPointSolMgrLinearProblemFailure(const std::string &what_arg)
Traits class which defines basic operations on multivectors.
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static int GetNumberVecs(const MV &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...
SolverManager()
Empty constructor.
A class for extending the status testing capabilities of Belos via logical combinations.
An implementation of StatusTestResNorm using a family of residual norms.
A pure virtual class for defining the status tests for the Belos iterative solvers.
A Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP, DM > > create(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > test, int mod, int printStates)
Create the StatusTestOutput object specified by the outputStyle.
An abstract class of StatusTest for stopping criteria using residual norms.
ReturnType
Whether the Belos solve converged for all linear systems.
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double convTol
Default convergence tolerance.
Structure to contain pointers to FixedPointIteration state variables.
Teuchos::RCP< const MV > R
The current residual.