10#ifndef BELOS_FIXEDPOINT_SOLMGR_HPP
11#define BELOS_FIXEDPOINT_SOLMGR_HPP
30#ifdef BELOS_TEUCHOS_TIME_MONITOR
31# include "Teuchos_TimeMonitor.hpp"
58 template<
class ScalarType,
class MV,
class OP>
64 typedef Teuchos::ScalarTraits<ScalarType> SCT;
65 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
66 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
98 const Teuchos::RCP<Teuchos::ParameterList> &pl );
104 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
129 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
130 return Teuchos::tuple(timerSolve_);
159 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
165 convTest_ = userConvStatusTest;
168 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
173 std::string solverDesc =
" Fixed Point ";
174 outputTest_->setSolverDesc( solverDesc );
221 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
224 Teuchos::RCP<OutputManager<ScalarType> > printer_;
226 Teuchos::RCP<std::ostream> outputStream_;
232 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
235 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
238 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > convTest_;
241 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
244 Teuchos::RCP<Teuchos::ParameterList> params_;
249 static constexpr int maxIters_default_ = 1000;
250 static constexpr bool showMaxResNormOnly_default_ =
false;
251 static constexpr int blockSize_default_ = 1;
254 static constexpr int outputFreq_default_ = -1;
255 static constexpr const char * label_default_ =
"Belos";
262 MagnitudeType convtol_;
269 MagnitudeType achievedTol_;
277 int blockSize_, verbosity_, outputStyle_, outputFreq_;
278 bool showMaxResNormOnly_;
284 Teuchos::RCP<Teuchos::Time> timerSolve_;
292template<
class ScalarType,
class MV,
class OP>
294 outputStream_(Teuchos::rcpFromRef(std::cout)),
296 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
297 maxIters_(maxIters_default_),
299 blockSize_(blockSize_default_),
300 verbosity_(verbosity_default_),
301 outputStyle_(outputStyle_default_),
302 outputFreq_(outputFreq_default_),
303 showMaxResNormOnly_(showMaxResNormOnly_default_),
304 label_(label_default_),
310template<
class ScalarType,
class MV,
class OP>
313 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
315 outputStream_(Teuchos::rcpFromRef(std::cout)),
317 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
318 maxIters_(maxIters_default_),
320 blockSize_(blockSize_default_),
321 verbosity_(verbosity_default_),
322 outputStyle_(outputStyle_default_),
323 outputFreq_(outputFreq_default_),
324 showMaxResNormOnly_(showMaxResNormOnly_default_),
325 label_(label_default_),
328 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
329 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
334 if (! pl.is_null()) {
339template<
class ScalarType,
class MV,
class OP>
342setParameters (
const Teuchos::RCP<Teuchos::ParameterList> ¶ms)
345 if (params_ == Teuchos::null) {
353 if (params->isParameter(
"Maximum Iterations")) {
354 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
357 params_->set(
"Maximum Iterations", maxIters_);
358 if (maxIterTest_!=Teuchos::null)
359 maxIterTest_->setMaxIters( maxIters_ );
363 if (params->isParameter(
"Block Size")) {
364 blockSize_ = params->get(
"Block Size",blockSize_default_);
365 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
366 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
369 params_->set(
"Block Size", blockSize_);
373 if (params->isParameter(
"Timer Label")) {
374 std::string tempLabel = params->get(
"Timer Label", label_default_);
377 if (tempLabel != label_) {
379 params_->set(
"Timer Label", label_);
380 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
381#ifdef BELOS_TEUCHOS_TIME_MONITOR
382 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
388 if (params->isParameter(
"Verbosity")) {
389 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
390 verbosity_ = params->get(
"Verbosity", verbosity_default_);
392 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
396 params_->set(
"Verbosity", verbosity_);
397 if (printer_ != Teuchos::null)
398 printer_->setVerbosity(verbosity_);
402 if (params->isParameter(
"Output Style")) {
403 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
404 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
406 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
410 params_->set(
"Output Style", outputStyle_);
411 outputTest_ = Teuchos::null;
415 if (params->isParameter(
"Output Stream")) {
416 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
419 params_->set(
"Output Stream", outputStream_);
420 if (printer_ != Teuchos::null)
421 printer_->setOStream( outputStream_ );
426 if (params->isParameter(
"Output Frequency")) {
427 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
431 params_->set(
"Output Frequency", outputFreq_);
432 if (outputTest_ != Teuchos::null)
433 outputTest_->setOutputFrequency( outputFreq_ );
437 if (printer_ == Teuchos::null) {
446 if (params->isParameter(
"Convergence Tolerance")) {
447 if (params->isType<MagnitudeType> (
"Convergence Tolerance")) {
448 convtol_ = params->get (
"Convergence Tolerance",
456 params_->set(
"Convergence Tolerance", convtol_);
457 if (convTest_ != Teuchos::null)
458 convTest_->setTolerance( convtol_ );
461 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
462 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
465 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
466 if (convTest_ != Teuchos::null)
467 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
473 if (maxIterTest_ == Teuchos::null)
477 if (convTest_ == Teuchos::null)
478 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
480 if (sTest_ == Teuchos::null)
481 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
483 if (outputTest_ == Teuchos::null) {
491 std::string solverDesc =
" Fixed Point ";
492 outputTest_->setSolverDesc( solverDesc );
497 if (timerSolve_ == Teuchos::null) {
498 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
499#ifdef BELOS_TEUCHOS_TIME_MONITOR
500 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
509template<
class ScalarType,
class MV,
class OP>
510Teuchos::RCP<const Teuchos::ParameterList>
513 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
516 if(is_null(validPL)) {
517 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
522 "The relative residual tolerance that needs to be achieved by the\n"
523 "iterative solver in order for the linear system to be declared converged.");
524 pl->set(
"Maximum Iterations",
static_cast<int>(maxIters_default_),
525 "The maximum number of block iterations allowed for each\n"
526 "set of RHS solved.");
527 pl->set(
"Block Size",
static_cast<int>(blockSize_default_),
528 "The number of vectors in each block.");
529 pl->set(
"Verbosity",
static_cast<int>(verbosity_default_),
530 "What type(s) of solver information should be outputted\n"
531 "to the output stream.");
532 pl->set(
"Output Style",
static_cast<int>(outputStyle_default_),
533 "What style is used for the solver information outputted\n"
534 "to the output stream.");
535 pl->set(
"Output Frequency",
static_cast<int>(outputFreq_default_),
536 "How often convergence information should be outputted\n"
537 "to the output stream.");
538 pl->set(
"Output Stream", Teuchos::rcpFromRef(std::cout),
539 "A reference-counted pointer to the output stream where all\n"
540 "solver output is sent.");
541 pl->set(
"Show Maximum Residual Norm Only",
static_cast<bool>(showMaxResNormOnly_default_),
542 "When convergence information is printed, only show the maximum\n"
543 "relative residual norm when the block size is greater than one.");
544 pl->set(
"Timer Label",
static_cast<const char *
>(label_default_),
545 "The string to use as a prefix for the timer labels.");
553template<
class ScalarType,
class MV,
class OP>
557 using Teuchos::rcp_const_cast;
558 using Teuchos::rcp_dynamic_cast;
568 TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
570 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() "
571 "has not been called.");
576 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
578 std::vector<int> currIdx, currIdx2;
579 currIdx.resize( blockSize_ );
580 currIdx2.resize( blockSize_ );
581 for (
int i=0; i<numCurrRHS; ++i)
582 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
583 for (
int i=numCurrRHS; i<blockSize_; ++i)
584 { currIdx[i] = -1; currIdx2[i] = i; }
587 problem_->setLSIndex( currIdx );
591 Teuchos::ParameterList plist;
592 plist.set(
"Block Size",blockSize_);
595 outputTest_->reset();
599 bool isConverged =
true;
604 RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
609#ifdef BELOS_TEUCHOS_TIME_MONITOR
610 Teuchos::TimeMonitor slvtimer(*timerSolve_);
613 while ( numRHS2Solve > 0 ) {
616 std::vector<int> convRHSIdx;
617 std::vector<int> currRHSIdx( currIdx );
618 currRHSIdx.resize(numCurrRHS);
621 block_fp_iter->resetNumIters();
624 outputTest_->resetNumCalls();
632 block_fp_iter->initializeFixedPoint(newstate);
638 block_fp_iter->iterate();
642 if (convTest_->getStatus() ==
Passed) {
646 std::vector<int> convIdx = convTest_->convIndices();
651 if (convIdx.size() == currRHSIdx.size())
656 problem_->setCurrLS();
661 std::vector<int> unconvIdx(currRHSIdx.size());
662 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
664 for (
unsigned int j=0; j<convIdx.size(); ++j) {
665 if (currRHSIdx[i] == convIdx[j]) {
671 currIdx2[have] = currIdx2[i];
672 currRHSIdx[have++] = currRHSIdx[i];
677 currRHSIdx.resize(have);
678 currIdx2.resize(have);
681 problem_->setLSIndex( currRHSIdx );
684 std::vector<MagnitudeType> norms;
685 R_0 =
MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
686 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
689 block_fp_iter->setBlockSize( have );
694 block_fp_iter->initializeFixedPoint(defstate);
700 else if (maxIterTest_->getStatus() ==
Passed) {
709 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
710 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor "
711 "the maximum iteration count test passed. Please report this bug "
712 "to the Belos developers.");
717 achievedTol_ = MT::one();
718 Teuchos::RCP<MV> X = problem_->getLHS();
720 printer_->stream(
Warnings) <<
"Belos::FixedPointSolMgr::solve(): Warning! NaN has been detected!"
724 catch (
const std::exception &e) {
725 std::ostream& err = printer_->stream (
Errors);
726 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at "
727 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
728 << e.what() << std::endl;
735 problem_->setCurrLS();
738 startPtr += numCurrRHS;
739 numRHS2Solve -= numCurrRHS;
740 if ( numRHS2Solve > 0 ) {
741 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
744 currIdx.resize( blockSize_ );
745 currIdx2.resize( blockSize_ );
746 for (
int i=0; i<numCurrRHS; ++i)
747 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
748 for (
int i=numCurrRHS; i<blockSize_; ++i)
749 { currIdx[i] = -1; currIdx2[i] = i; }
752 problem_->setLSIndex( currIdx );
755 block_fp_iter->setBlockSize( blockSize_ );
758 currIdx.resize( numRHS2Solve );
769#ifdef BELOS_TEUCHOS_TIME_MONITOR
775 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
780 numIters_ = maxIterTest_->getNumIters();
785 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
787 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
788 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
789 "method returned NULL. Please report this bug to the Belos developers.");
791 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
792 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() "
793 "method returned a vector of length zero. Please report this bug to the "
794 "Belos developers.");
799 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
809template<
class ScalarType,
class MV,
class OP>
812 std::ostringstream oss;
813 oss <<
"Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
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.
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)
This class implements the preconditioned fixed point iteration.
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
FixedPointSolMgrLinearProblemFailure(const std::string &what_arg)
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.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
void replaceUserConvStatusTest(const Teuchos::RCP< StatusTestResNorm< ScalarType, MV, OP > > &userConvStatusTest)
Set user-defined convergence status test.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > 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.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
virtual ~FixedPointSolMgr()
Destructor.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
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 void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
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...
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 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.
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.