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 );
147 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const;
158 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
159 return Teuchos::tuple(timerSolve_);
189 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
230 std::string description()
const;
237 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
240 Teuchos::RCP<OutputManager<ScalarType> > printer_;
242 Teuchos::RCP<std::ostream> outputStream_;
248 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
251 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
254 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
257 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
260 Teuchos::RCP<Teuchos::ParameterList> params_;
265 static const MagnitudeType convtol_default_;
266 static const int maxIters_default_;
267 static const bool showMaxResNormOnly_default_;
268 static const int blockSize_default_;
269 static const int verbosity_default_;
270 static const int outputStyle_default_;
271 static const int outputFreq_default_;
272 static const std::string label_default_;
273 static const Teuchos::RCP<std::ostream> outputStream_default_;
280 MagnitudeType convtol_;
287 MagnitudeType achievedTol_;
295 int blockSize_, verbosity_, outputStyle_, outputFreq_;
296 bool showMaxResNormOnly_;
302 Teuchos::RCP<Teuchos::Time> timerSolve_;
310 template<
class ScalarType,
class MV,
class OP>
313 template<
class ScalarType,
class MV,
class OP>
316 template<
class ScalarType,
class MV,
class OP>
319 template<
class ScalarType,
class MV,
class OP>
322 template<
class ScalarType,
class MV,
class OP>
325 template<
class ScalarType,
class MV,
class OP>
328 template<
class ScalarType,
class MV,
class OP>
331 template<
class ScalarType,
class MV,
class OP>
334 template<
class ScalarType,
class MV,
class OP>
339 template<
class ScalarType,
class MV,
class OP>
341 outputStream_(outputStream_default_),
342 convtol_(convtol_default_),
343 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
344 maxIters_(maxIters_default_),
346 blockSize_(blockSize_default_),
347 verbosity_(verbosity_default_),
348 outputStyle_(outputStyle_default_),
349 outputFreq_(outputFreq_default_),
350 showMaxResNormOnly_(showMaxResNormOnly_default_),
351 label_(label_default_),
357 template<
class ScalarType,
class MV,
class OP>
360 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
362 outputStream_(outputStream_default_),
363 convtol_(convtol_default_),
364 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
365 maxIters_(maxIters_default_),
367 blockSize_(blockSize_default_),
368 verbosity_(verbosity_default_),
369 outputStyle_(outputStyle_default_),
370 outputFreq_(outputFreq_default_),
371 showMaxResNormOnly_(showMaxResNormOnly_default_),
372 label_(label_default_),
375 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
376 "FixedPointSolMgr's constructor requires a nonnull LinearProblem instance.");
381 if (! pl.is_null()) {
386 template<
class ScalarType,
class MV,
class OP>
392 if (params_ == Teuchos::null) {
400 if (params->isParameter(
"Maximum Iterations")) {
401 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
404 params_->set(
"Maximum Iterations", maxIters_);
405 if (maxIterTest_!=Teuchos::null)
406 maxIterTest_->setMaxIters( maxIters_ );
410 if (params->isParameter(
"Block Size")) {
411 blockSize_ = params->get(
"Block Size",blockSize_default_);
412 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
413 "Belos::FixedPointSolMgr: \"Block Size\" must be strictly positive.");
416 params_->set(
"Block Size", blockSize_);
420 if (params->isParameter(
"Timer Label")) {
421 std::string tempLabel = params->get(
"Timer Label", label_default_);
424 if (tempLabel != label_) {
426 params_->set(
"Timer Label", label_);
427 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
428 #ifdef BELOS_TEUCHOS_TIME_MONITOR 429 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
435 if (params->isParameter(
"Verbosity")) {
436 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
437 verbosity_ = params->get(
"Verbosity", verbosity_default_);
439 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
443 params_->set(
"Verbosity", verbosity_);
444 if (printer_ != Teuchos::null)
445 printer_->setVerbosity(verbosity_);
449 if (params->isParameter(
"Output Style")) {
450 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
451 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
453 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
457 params_->set(
"Output Style", outputStyle_);
458 outputTest_ = Teuchos::null;
462 if (params->isParameter(
"Output Stream")) {
463 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
466 params_->set(
"Output Stream", outputStream_);
467 if (printer_ != Teuchos::null)
468 printer_->setOStream( outputStream_ );
473 if (params->isParameter(
"Output Frequency")) {
474 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
478 params_->set(
"Output Frequency", outputFreq_);
479 if (outputTest_ != Teuchos::null)
480 outputTest_->setOutputFrequency( outputFreq_ );
484 if (printer_ == Teuchos::null) {
493 if (params->isParameter(
"Convergence Tolerance")) {
494 convtol_ = params->get(
"Convergence Tolerance",convtol_default_);
497 params_->set(
"Convergence Tolerance", convtol_);
498 if (convTest_ != Teuchos::null)
499 convTest_->setTolerance( convtol_ );
502 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
503 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
506 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
507 if (convTest_ != Teuchos::null)
508 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
514 if (maxIterTest_ == Teuchos::null)
518 if (convTest_ == Teuchos::null)
519 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
521 if (sTest_ == Teuchos::null)
522 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
524 if (outputTest_ == Teuchos::null) {
532 std::string solverDesc =
" Fixed Point ";
533 outputTest_->setSolverDesc( solverDesc );
538 if (timerSolve_ == Teuchos::null) {
539 std::string solveLabel = label_ +
": FixedPointSolMgr total solve time";
540 #ifdef BELOS_TEUCHOS_TIME_MONITOR 541 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
550 template<
class ScalarType,
class MV,
class OP>
551 Teuchos::RCP<const Teuchos::ParameterList>
554 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
557 if(is_null(validPL)) {
558 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
559 pl->set(
"Convergence Tolerance", convtol_default_,
560 "The relative residual tolerance that needs to be achieved by the\n" 561 "iterative solver in order for the linear system to be declared converged.");
562 pl->set(
"Maximum Iterations", maxIters_default_,
563 "The maximum number of block iterations allowed for each\n" 564 "set of RHS solved.");
565 pl->set(
"Block Size", blockSize_default_,
566 "The number of vectors in each block.");
567 pl->set(
"Verbosity", verbosity_default_,
568 "What type(s) of solver information should be outputted\n" 569 "to the output stream.");
570 pl->set(
"Output Style", outputStyle_default_,
571 "What style is used for the solver information outputted\n" 572 "to the output stream.");
573 pl->set(
"Output Frequency", outputFreq_default_,
574 "How often convergence information should be outputted\n" 575 "to the output stream.");
576 pl->set(
"Output Stream", outputStream_default_,
577 "A reference-counted pointer to the output stream where all\n" 578 "solver output is sent.");
579 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
580 "When convergence information is printed, only show the maximum\n" 581 "relative residual norm when the block size is greater than one.");
582 pl->set(
"Timer Label", label_default_,
583 "The string to use as a prefix for the timer labels.");
592 template<
class ScalarType,
class MV,
class OP>
596 using Teuchos::rcp_const_cast;
597 using Teuchos::rcp_dynamic_cast;
607 Teuchos::BLAS<int,ScalarType> blas;
608 Teuchos::LAPACK<int,ScalarType> lapack;
610 TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
612 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() " 613 "has not been called.");
618 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
620 std::vector<int> currIdx, currIdx2;
621 currIdx.resize( blockSize_ );
622 currIdx2.resize( blockSize_ );
623 for (
int i=0; i<numCurrRHS; ++i)
624 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
625 for (
int i=numCurrRHS; i<blockSize_; ++i)
626 { currIdx[i] = -1; currIdx2[i] = i; }
629 problem_->setLSIndex( currIdx );
633 Teuchos::ParameterList plist;
634 plist.set(
"Block Size",blockSize_);
637 outputTest_->reset();
641 bool isConverged =
true;
646 RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
651 #ifdef BELOS_TEUCHOS_TIME_MONITOR 652 Teuchos::TimeMonitor slvtimer(*timerSolve_);
655 while ( numRHS2Solve > 0 ) {
658 std::vector<int> convRHSIdx;
659 std::vector<int> currRHSIdx( currIdx );
660 currRHSIdx.resize(numCurrRHS);
663 block_fp_iter->resetNumIters();
666 outputTest_->resetNumCalls();
674 block_fp_iter->initializeFixedPoint(newstate);
680 block_fp_iter->iterate();
684 if (convTest_->getStatus() ==
Passed) {
689 std::vector<int> convIdx =
690 rcp_dynamic_cast<conv_test_type>(convTest_)->convIndices();
695 if (convIdx.size() == currRHSIdx.size())
700 problem_->setCurrLS();
705 std::vector<int> unconvIdx(currRHSIdx.size());
706 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
708 for (
unsigned int j=0; j<convIdx.size(); ++j) {
709 if (currRHSIdx[i] == convIdx[j]) {
715 currIdx2[have] = currIdx2[i];
716 currRHSIdx[have++] = currRHSIdx[i];
721 currRHSIdx.resize(have);
722 currIdx2.resize(have);
725 problem_->setLSIndex( currRHSIdx );
728 std::vector<MagnitudeType> norms;
729 R_0 =
MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
730 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
733 block_fp_iter->setBlockSize( have );
738 block_fp_iter->initializeFixedPoint(defstate);
744 else if (maxIterTest_->getStatus() ==
Passed) {
753 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
754 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor " 755 "the maximum iteration count test passed. Please report this bug " 756 "to the Belos developers.");
759 catch (
const std::exception &e) {
760 std::ostream& err = printer_->stream (
Errors);
761 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at " 762 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
763 << e.what() << std::endl;
770 problem_->setCurrLS();
773 startPtr += numCurrRHS;
774 numRHS2Solve -= numCurrRHS;
775 if ( numRHS2Solve > 0 ) {
776 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
779 currIdx.resize( blockSize_ );
780 currIdx2.resize( blockSize_ );
781 for (
int i=0; i<numCurrRHS; ++i)
782 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
783 for (
int i=numCurrRHS; i<blockSize_; ++i)
784 { currIdx[i] = -1; currIdx2[i] = i; }
787 problem_->setLSIndex( currIdx );
790 block_fp_iter->setBlockSize( blockSize_ );
793 currIdx.resize( numRHS2Solve );
804 #ifdef BELOS_TEUCHOS_TIME_MONITOR 810 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
815 numIters_ = maxIterTest_->getNumIters();
821 const std::vector<MagnitudeType>* pTestValues =
822 rcp_dynamic_cast<conv_test_type>(convTest_)->getTestValue();
824 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
825 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() " 826 "method returned NULL. Please report this bug to the Belos developers.");
828 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
829 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() " 830 "method returned a vector of length zero. Please report this bug to the " 831 "Belos developers.");
836 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
846 template<
class ScalarType,
class MV,
class OP>
849 std::ostringstream oss;
850 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...
Class which manages the output and verbosity of the Belos solvers.
Belos concrete class for performing fixed point iteration iteration.
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.
std::string description() const
Method to return description of the block CG solver manager.
An implementation of StatusTestResNorm using a family of residual norms.
Belos::StatusTest class for specifying a maximum number of iterations.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
A factory class for generating StatusTestOutput objects.
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...
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...
A Belos::StatusTest class for specifying a maximum number of iterations.
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).
ResetType
How to reset the solver.
Pure virtual base class which describes the basic interface for a solver manager. ...
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
virtual ~FixedPointSolMgr()
Destructor.
A linear system to solve, and its associated information.
int getNumIters() const
Get the iteration count for the most recent call to solve().
Class which describes the linear problem to be solved by the iterative solver.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
ReturnType solve()
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
ReturnType
Whether the Belos solve converged for all linear systems.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const MV > R
The current residual.
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< 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.
Belos header file which uses auto-configuration information to include necessary C++ headers...
This class implements the preconditioned fixed point iteration.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.