42 #ifndef BELOS_FIXEDPOINT_SOLMGR_HPP 43 #define BELOS_FIXEDPOINT_SOLMGR_HPP 62 #include "Teuchos_BLAS.hpp" 63 #include "Teuchos_LAPACK.hpp" 64 #ifdef BELOS_TEUCHOS_TIME_MONITOR 65 # include "Teuchos_TimeMonitor.hpp" 92 template<
class ScalarType,
class MV,
class OP>
98 typedef Teuchos::ScalarTraits<ScalarType> SCT;
99 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
100 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
132 const Teuchos::RCP<Teuchos::ParameterList> &pl );
158 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
159 return Teuchos::tuple(timerSolve_);
189 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
195 convTest_ = userConvStatusTest;
198 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
203 std::string solverDesc =
" Fixed Point ";
204 outputTest_->setSolverDesc( solverDesc );
253 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
256 Teuchos::RCP<OutputManager<ScalarType> > printer_;
258 Teuchos::RCP<std::ostream> outputStream_;
264 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
267 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
270 Teuchos::RCP<StatusTestResNorm<ScalarType,MV,OP> > convTest_;
273 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
276 Teuchos::RCP<Teuchos::ParameterList> params_;
281 static const MagnitudeType convtol_default_;
282 static const int maxIters_default_;
283 static const bool showMaxResNormOnly_default_;
284 static const int blockSize_default_;
285 static const int verbosity_default_;
286 static const int outputStyle_default_;
287 static const int outputFreq_default_;
288 static const std::string label_default_;
289 static const Teuchos::RCP<std::ostream> outputStream_default_;
296 MagnitudeType convtol_;
303 MagnitudeType achievedTol_;
311 int blockSize_, verbosity_, outputStyle_, outputFreq_;
312 bool showMaxResNormOnly_;
318 Teuchos::RCP<Teuchos::Time> timerSolve_;
326 template<
class ScalarType,
class MV,
class OP>
327 const typename FixedPointSolMgr<ScalarType,MV,OP>::MagnitudeType FixedPointSolMgr<ScalarType,MV,OP>::convtol_default_ = 1e-8;
329 template<
class ScalarType,
class MV,
class OP>
330 const int FixedPointSolMgr<ScalarType,MV,OP>::maxIters_default_ = 1000;
332 template<
class ScalarType,
class MV,
class OP>
333 const bool FixedPointSolMgr<ScalarType,MV,OP>::showMaxResNormOnly_default_ =
false;
335 template<
class ScalarType,
class MV,
class OP>
336 const int FixedPointSolMgr<ScalarType,MV,OP>::blockSize_default_ = 1;
338 template<
class ScalarType,
class MV,
class OP>
339 const int FixedPointSolMgr<ScalarType,MV,OP>::verbosity_default_ =
Belos::Errors;
341 template<
class ScalarType,
class MV,
class OP>
342 const int FixedPointSolMgr<ScalarType,MV,OP>::outputStyle_default_ =
Belos::General;
344 template<
class ScalarType,
class MV,
class OP>
345 const int FixedPointSolMgr<ScalarType,MV,OP>::outputFreq_default_ = -1;
347 template<
class ScalarType,
class MV,
class OP>
348 const std::string FixedPointSolMgr<ScalarType,MV,OP>::label_default_ =
"Belos";
350 template<
class ScalarType,
class MV,
class OP>
351 const Teuchos::RCP<std::ostream> FixedPointSolMgr<ScalarType,MV,OP>::outputStream_default_ = Teuchos::rcp(&std::cout,
false);
355 template<
class ScalarType,
class MV,
class OP>
357 outputStream_(outputStream_default_),
358 convtol_(convtol_default_),
359 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
360 maxIters_(maxIters_default_),
362 blockSize_(blockSize_default_),
363 verbosity_(verbosity_default_),
364 outputStyle_(outputStyle_default_),
365 outputFreq_(outputFreq_default_),
366 showMaxResNormOnly_(showMaxResNormOnly_default_),
367 label_(label_default_),
373 template<
class ScalarType,
class MV,
class OP>
376 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
378 outputStream_(outputStream_default_),
379 convtol_(convtol_default_),
380 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
381 maxIters_(maxIters_default_),
383 blockSize_(blockSize_default_),
384 verbosity_(verbosity_default_),
385 outputStyle_(outputStyle_default_),
386 outputFreq_(outputFreq_default_),
387 showMaxResNormOnly_(showMaxResNormOnly_default_),
388 label_(label_default_),
391 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
392 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
397 if (! pl.is_null()) {
402 template<
class ScalarType,
class MV,
class OP>
408 if (params_ == Teuchos::null) {
409 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
412 params->validateParameters(*getValidParameters());
416 if (params->isParameter(
"Maximum Iterations")) {
417 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
420 params_->set(
"Maximum Iterations", maxIters_);
421 if (maxIterTest_!=Teuchos::null)
422 maxIterTest_->setMaxIters( maxIters_ );
426 if (params->isParameter(
"Block Size")) {
427 blockSize_ = params->get(
"Block Size",blockSize_default_);
428 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
429 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
432 params_->set(
"Block Size", blockSize_);
436 if (params->isParameter(
"Timer Label")) {
437 std::string tempLabel = params->get(
"Timer Label", label_default_);
440 if (tempLabel != label_) {
442 params_->set(
"Timer Label", label_);
443 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
444 #ifdef BELOS_TEUCHOS_TIME_MONITOR 445 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
451 if (params->isParameter(
"Verbosity")) {
452 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
453 verbosity_ = params->get(
"Verbosity", verbosity_default_);
455 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
459 params_->set(
"Verbosity", verbosity_);
460 if (printer_ != Teuchos::null)
461 printer_->setVerbosity(verbosity_);
465 if (params->isParameter(
"Output Style")) {
466 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
467 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
469 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
473 params_->set(
"Output Style", outputStyle_);
474 outputTest_ = Teuchos::null;
478 if (params->isParameter(
"Output Stream")) {
479 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
482 params_->set(
"Output Stream", outputStream_);
483 if (printer_ != Teuchos::null)
484 printer_->setOStream( outputStream_ );
489 if (params->isParameter(
"Output Frequency")) {
490 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
494 params_->set(
"Output Frequency", outputFreq_);
495 if (outputTest_ != Teuchos::null)
496 outputTest_->setOutputFrequency( outputFreq_ );
500 if (printer_ == Teuchos::null) {
509 if (params->isParameter(
"Convergence Tolerance")) {
510 convtol_ = params->get(
"Convergence Tolerance",convtol_default_);
513 params_->set(
"Convergence Tolerance", convtol_);
514 if (convTest_ != Teuchos::null)
518 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
519 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
522 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
523 if (convTest_ != Teuchos::null)
524 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
530 if (maxIterTest_ == Teuchos::null)
534 if (convTest_ == Teuchos::null)
535 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
537 if (sTest_ == Teuchos::null)
538 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
540 if (outputTest_ == Teuchos::null) {
548 std::string solverDesc =
" Fixed Point ";
549 outputTest_->setSolverDesc( solverDesc );
554 if (timerSolve_ == Teuchos::null) {
555 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
556 #ifdef BELOS_TEUCHOS_TIME_MONITOR 557 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
566 template<
class ScalarType,
class MV,
class OP>
567 Teuchos::RCP<const Teuchos::ParameterList>
570 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
573 if(is_null(validPL)) {
574 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
575 pl->set(
"Convergence Tolerance", convtol_default_,
576 "The relative residual tolerance that needs to be achieved by the\n" 577 "iterative solver in order for the linear system to be declared converged.");
578 pl->set(
"Maximum Iterations", maxIters_default_,
579 "The maximum number of block iterations allowed for each\n" 580 "set of RHS solved.");
581 pl->set(
"Block Size", blockSize_default_,
582 "The number of vectors in each block.");
583 pl->set(
"Verbosity", verbosity_default_,
584 "What type(s) of solver information should be outputted\n" 585 "to the output stream.");
586 pl->set(
"Output Style", outputStyle_default_,
587 "What style is used for the solver information outputted\n" 588 "to the output stream.");
589 pl->set(
"Output Frequency", outputFreq_default_,
590 "How often convergence information should be outputted\n" 591 "to the output stream.");
592 pl->set(
"Output Stream", outputStream_default_,
593 "A reference-counted pointer to the output stream where all\n" 594 "solver output is sent.");
595 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
596 "When convergence information is printed, only show the maximum\n" 597 "relative residual norm when the block size is greater than one.");
598 pl->set(
"Timer Label", label_default_,
599 "The string to use as a prefix for the timer labels.");
608 template<
class ScalarType,
class MV,
class OP>
612 using Teuchos::rcp_const_cast;
613 using Teuchos::rcp_dynamic_cast;
620 setParameters(Teuchos::parameterList(*getValidParameters()));
623 Teuchos::BLAS<int,ScalarType> blas;
624 Teuchos::LAPACK<int,ScalarType> lapack;
626 TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
628 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() " 629 "has not been called.");
633 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
634 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
636 std::vector<int> currIdx, currIdx2;
637 currIdx.resize( blockSize_ );
638 currIdx2.resize( blockSize_ );
639 for (
int i=0; i<numCurrRHS; ++i)
640 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
641 for (
int i=numCurrRHS; i<blockSize_; ++i)
642 { currIdx[i] = -1; currIdx2[i] = i; }
645 problem_->setLSIndex( currIdx );
649 Teuchos::ParameterList plist;
650 plist.set(
"Block Size",blockSize_);
653 outputTest_->reset();
657 bool isConverged =
true;
662 RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
667 #ifdef BELOS_TEUCHOS_TIME_MONITOR 668 Teuchos::TimeMonitor slvtimer(*timerSolve_);
671 while ( numRHS2Solve > 0 ) {
674 std::vector<int> convRHSIdx;
675 std::vector<int> currRHSIdx( currIdx );
676 currRHSIdx.resize(numCurrRHS);
679 block_fp_iter->resetNumIters();
682 outputTest_->resetNumCalls();
685 RCP<MV> R_0 = MVT::CloneViewNonConst( *(rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
690 block_fp_iter->initializeFixedPoint(newstate);
696 block_fp_iter->iterate();
700 if (convTest_->getStatus() ==
Passed) {
704 std::vector<int> convIdx = convTest_->convIndices();
709 if (convIdx.size() == currRHSIdx.size())
714 problem_->setCurrLS();
719 std::vector<int> unconvIdx(currRHSIdx.size());
720 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
722 for (
unsigned int j=0; j<convIdx.size(); ++j) {
723 if (currRHSIdx[i] == convIdx[j]) {
729 currIdx2[have] = currIdx2[i];
730 currRHSIdx[have++] = currRHSIdx[i];
735 currRHSIdx.resize(have);
736 currIdx2.resize(have);
739 problem_->setLSIndex( currRHSIdx );
742 std::vector<MagnitudeType> norms;
743 R_0 = MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
744 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
747 block_fp_iter->setBlockSize( have );
752 block_fp_iter->initializeFixedPoint(defstate);
758 else if (maxIterTest_->getStatus() ==
Passed) {
767 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
768 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor " 769 "the maximum iteration count test passed. Please report this bug " 770 "to the Belos developers.");
773 catch (
const std::exception &e) {
774 std::ostream& err = printer_->stream (
Errors);
775 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at " 776 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
777 << e.what() << std::endl;
784 problem_->setCurrLS();
787 startPtr += numCurrRHS;
788 numRHS2Solve -= numCurrRHS;
789 if ( numRHS2Solve > 0 ) {
790 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
793 currIdx.resize( blockSize_ );
794 currIdx2.resize( blockSize_ );
795 for (
int i=0; i<numCurrRHS; ++i)
796 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
797 for (
int i=numCurrRHS; i<blockSize_; ++i)
798 { currIdx[i] = -1; currIdx2[i] = i; }
801 problem_->setLSIndex( currIdx );
804 block_fp_iter->setBlockSize( blockSize_ );
807 currIdx.resize( numRHS2Solve );
818 #ifdef BELOS_TEUCHOS_TIME_MONITOR 824 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
829 numIters_ = maxIterTest_->getNumIters();
834 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
836 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
837 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() " 838 "method returned NULL. Please report this bug to the Belos developers.");
840 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
841 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() " 842 "method returned a vector of length zero. Please report this bug to the " 843 "Belos developers.");
848 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
858 template<
class ScalarType,
class MV,
class OP>
861 std::ostringstream oss;
862 oss <<
"Belos::FixedPointSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
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...
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
Class which manages the output and verbosity of the Belos solvers.
Belos concrete class for performing fixed point iteration iteration.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
FixedPointSolMgr()
Empty constructor for FixedPointSolMgr. This constructor takes no arguments and sets the default valu...
Belos concrete class for performing the conjugate-gradient (CG) iteration.
A factory class for generating StatusTestOutput objects.
An abstract class of StatusTest for stopping criteria using residual norms.
An implementation of StatusTestResNorm using a family of residual norms.
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...
Belos::StatusTest class for specifying a maximum number of iterations.
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
void reset(const ResetType type)
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
Pure virtual base class which describes the basic interface for a solver manager. ...
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.
ReturnType solve()
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.
int getNumIters() const
Get the iteration count for the most recent call to solve().
FixedPointSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters 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)
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters the solver manager should use to solve the linear problem.
A class for extending the status testing capabilities of Belos via logical combinations.
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
std::string description() const
Method to return description of the block CG solver manager.
Belos header file which uses auto-configuration information to include necessary C++ headers...
This class implements the preconditioned fixed point iteration.
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.