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 );
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 );
246 std::string description()
const;
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>
329 template<
class ScalarType,
class MV,
class OP>
332 template<
class ScalarType,
class MV,
class OP>
335 template<
class ScalarType,
class MV,
class OP>
338 template<
class ScalarType,
class MV,
class OP>
341 template<
class ScalarType,
class MV,
class OP>
344 template<
class ScalarType,
class MV,
class OP>
347 template<
class ScalarType,
class MV,
class OP>
350 template<
class ScalarType,
class MV,
class OP>
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) {
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)
515 convTest_->setTolerance( convtol_ );
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.");
607 template<
class ScalarType,
class MV,
class OP>
611 using Teuchos::rcp_const_cast;
612 using Teuchos::rcp_dynamic_cast;
622 Teuchos::BLAS<int,ScalarType> blas;
623 Teuchos::LAPACK<int,ScalarType> lapack;
625 TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
627 "Belos::FixedPointSolMgr::solve(): Linear problem is not ready, setProblem() " 628 "has not been called.");
633 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
635 std::vector<int> currIdx, currIdx2;
636 currIdx.resize( blockSize_ );
637 currIdx2.resize( blockSize_ );
638 for (
int i=0; i<numCurrRHS; ++i)
639 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
640 for (
int i=numCurrRHS; i<blockSize_; ++i)
641 { currIdx[i] = -1; currIdx2[i] = i; }
644 problem_->setLSIndex( currIdx );
648 Teuchos::ParameterList plist;
649 plist.set(
"Block Size",blockSize_);
652 outputTest_->reset();
656 bool isConverged =
true;
661 RCP<FixedPointIteration<ScalarType,MV,OP> > block_fp_iter;
666 #ifdef BELOS_TEUCHOS_TIME_MONITOR 667 Teuchos::TimeMonitor slvtimer(*timerSolve_);
670 while ( numRHS2Solve > 0 ) {
673 std::vector<int> convRHSIdx;
674 std::vector<int> currRHSIdx( currIdx );
675 currRHSIdx.resize(numCurrRHS);
678 block_fp_iter->resetNumIters();
681 outputTest_->resetNumCalls();
689 block_fp_iter->initializeFixedPoint(newstate);
695 block_fp_iter->iterate();
699 if (convTest_->getStatus() ==
Passed) {
703 std::vector<int> convIdx = convTest_->convIndices();
708 if (convIdx.size() == currRHSIdx.size())
713 problem_->setCurrLS();
718 std::vector<int> unconvIdx(currRHSIdx.size());
719 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
721 for (
unsigned int j=0; j<convIdx.size(); ++j) {
722 if (currRHSIdx[i] == convIdx[j]) {
728 currIdx2[have] = currIdx2[i];
729 currRHSIdx[have++] = currRHSIdx[i];
734 currRHSIdx.resize(have);
735 currIdx2.resize(have);
738 problem_->setLSIndex( currRHSIdx );
741 std::vector<MagnitudeType> norms;
742 R_0 =
MVT::CloneCopy( *(block_fp_iter->getNativeResiduals(&norms)),currIdx2 );
743 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
746 block_fp_iter->setBlockSize( have );
751 block_fp_iter->initializeFixedPoint(defstate);
757 else if (maxIterTest_->getStatus() ==
Passed) {
766 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
767 "Belos::FixedPointSolMgr::solve(): Neither the convergence test nor " 768 "the maximum iteration count test passed. Please report this bug " 769 "to the Belos developers.");
772 catch (
const std::exception &e) {
773 std::ostream& err = printer_->stream (
Errors);
774 err <<
"Error! Caught std::exception in FixedPointIteration::iterate() at " 775 <<
"iteration " << block_fp_iter->getNumIters() << std::endl
776 << e.what() << std::endl;
783 problem_->setCurrLS();
786 startPtr += numCurrRHS;
787 numRHS2Solve -= numCurrRHS;
788 if ( numRHS2Solve > 0 ) {
789 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
792 currIdx.resize( blockSize_ );
793 currIdx2.resize( blockSize_ );
794 for (
int i=0; i<numCurrRHS; ++i)
795 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
796 for (
int i=numCurrRHS; i<blockSize_; ++i)
797 { currIdx[i] = -1; currIdx2[i] = i; }
800 problem_->setLSIndex( currIdx );
803 block_fp_iter->setBlockSize( blockSize_ );
806 currIdx.resize( numRHS2Solve );
817 #ifdef BELOS_TEUCHOS_TIME_MONITOR 823 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
828 numIters_ = maxIterTest_->getNumIters();
833 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
835 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
836 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() " 837 "method returned NULL. Please report this bug to the Belos developers.");
839 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
840 "Belos::FixedPointSolMgr::solve(): The convergence test's getTestValue() " 841 "method returned a vector of length zero. Please report this bug to the " 842 "Belos developers.");
847 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
857 template<
class ScalarType,
class MV,
class OP>
860 std::ostringstream oss;
861 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.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
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.
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. ...
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...
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.
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...
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.