42 #ifndef BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP 43 #define BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_SOLMGR_HPP 61 #include "Teuchos_BLAS.hpp" 62 #ifdef BELOS_TEUCHOS_TIME_MONITOR 63 #include "Teuchos_TimeMonitor.hpp" 100 template<
class ScalarType,
class MV,
class OP>
106 typedef Teuchos::ScalarTraits<ScalarType> SCT;
107 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
108 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_);
181 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
225 std::string description()
const;
232 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
235 Teuchos::RCP<OutputManager<ScalarType> > printer_;
236 Teuchos::RCP<std::ostream> outputStream_;
239 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
240 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
241 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
242 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
245 Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > ortho_;
248 Teuchos::RCP<Teuchos::ParameterList> params_;
255 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
258 static const MagnitudeType convtol_default_;
259 static const int maxIters_default_;
260 static const bool assertPositiveDefiniteness_default_;
261 static const bool showMaxResNormOnly_default_;
262 static const int verbosity_default_;
263 static const int outputStyle_default_;
264 static const int outputFreq_default_;
265 static const int defQuorum_default_;
266 static const std::string resScale_default_;
267 static const std::string label_default_;
268 static const Teuchos::RCP<std::ostream> outputStream_default_;
271 MagnitudeType convtol_;
272 int maxIters_, numIters_;
273 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
274 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
275 std::string resScale_;
279 Teuchos::RCP<Teuchos::Time> timerSolve_;
291 template<
class ScalarType,
class MV,
class OP>
294 template<
class ScalarType,
class MV,
class OP>
297 template<
class ScalarType,
class MV,
class OP>
300 template<
class ScalarType,
class MV,
class OP>
303 template<
class ScalarType,
class MV,
class OP>
306 template<
class ScalarType,
class MV,
class OP>
309 template<
class ScalarType,
class MV,
class OP>
312 template<
class ScalarType,
class MV,
class OP>
315 template<
class ScalarType,
class MV,
class OP>
318 template<
class ScalarType,
class MV,
class OP>
321 template<
class ScalarType,
class MV,
class OP>
326 template<
class ScalarType,
class MV,
class OP>
328 outputStream_(outputStream_default_),
329 convtol_(convtol_default_),
330 maxIters_(maxIters_default_),
332 verbosity_(verbosity_default_),
333 outputStyle_(outputStyle_default_),
334 outputFreq_(outputFreq_default_),
335 defQuorum_(defQuorum_default_),
336 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
337 showMaxResNormOnly_(showMaxResNormOnly_default_),
338 resScale_(resScale_default_),
339 label_(label_default_),
344 template<
class ScalarType,
class MV,
class OP>
347 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
349 outputStream_(outputStream_default_),
350 convtol_(convtol_default_),
351 maxIters_(maxIters_default_),
353 verbosity_(verbosity_default_),
354 outputStyle_(outputStyle_default_),
355 outputFreq_(outputFreq_default_),
356 defQuorum_(defQuorum_default_),
357 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
358 showMaxResNormOnly_(showMaxResNormOnly_default_),
359 resScale_(resScale_default_),
360 label_(label_default_),
363 TEUCHOS_TEST_FOR_EXCEPTION(
364 problem_.is_null (), std::invalid_argument,
365 "Belos::PseudoBlockStochasticCGSolMgr two-argument constructor: " 366 "'problem' is null. You must supply a non-null Belos::LinearProblem " 367 "instance when calling this constructor.");
369 if (! pl.is_null ()) {
375 template<
class ScalarType,
class MV,
class OP>
378 using Teuchos::ParameterList;
379 using Teuchos::parameterList;
385 if (params_.is_null()) {
386 params_ = parameterList (*defaultParams);
388 params->validateParameters (*defaultParams);
392 if (params->isParameter(
"Maximum Iterations")) {
393 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
396 params_->set(
"Maximum Iterations", maxIters_);
397 if (maxIterTest_!=Teuchos::null)
398 maxIterTest_->setMaxIters( maxIters_ );
402 if (params->isParameter(
"Assert Positive Definiteness")) {
403 assertPositiveDefiniteness_ = params->get(
"Assert Positive Definiteness",assertPositiveDefiniteness_default_);
406 params_->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_);
410 if (params->isParameter(
"Timer Label")) {
411 std::string tempLabel = params->get(
"Timer Label", label_default_);
414 if (tempLabel != label_) {
416 params_->set(
"Timer Label", label_);
417 std::string solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
418 #ifdef BELOS_TEUCHOS_TIME_MONITOR 419 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
421 if (ortho_ != Teuchos::null) {
422 ortho_->setLabel( label_ );
428 if (params->isParameter(
"Verbosity")) {
429 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
430 verbosity_ = params->get(
"Verbosity", verbosity_default_);
432 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
436 params_->set(
"Verbosity", verbosity_);
437 if (printer_ != Teuchos::null)
438 printer_->setVerbosity(verbosity_);
442 if (params->isParameter(
"Output Style")) {
443 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
444 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
446 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
450 params_->set(
"Output Style", outputStyle_);
451 outputTest_ = Teuchos::null;
455 if (params->isParameter(
"Output Stream")) {
456 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
459 params_->set(
"Output Stream", outputStream_);
460 if (printer_ != Teuchos::null)
461 printer_->setOStream( outputStream_ );
466 if (params->isParameter(
"Output Frequency")) {
467 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
471 params_->set(
"Output Frequency", outputFreq_);
472 if (outputTest_ != Teuchos::null)
473 outputTest_->setOutputFrequency( outputFreq_ );
477 if (printer_ == Teuchos::null) {
486 if (params->isParameter(
"Convergence Tolerance")) {
487 convtol_ = params->get(
"Convergence Tolerance",convtol_default_);
490 params_->set(
"Convergence Tolerance", convtol_);
491 if (convTest_ != Teuchos::null)
492 convTest_->setTolerance( convtol_ );
495 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
496 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
499 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
500 if (convTest_ != Teuchos::null)
501 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
505 bool newResTest =
false;
510 std::string tempResScale = resScale_;
511 bool implicitResidualScalingName =
false;
512 if (params->isParameter (
"Residual Scaling")) {
513 tempResScale = params->get<std::string> (
"Residual Scaling");
515 else if (params->isParameter (
"Implicit Residual Scaling")) {
516 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
517 implicitResidualScalingName =
true;
521 if (resScale_ != tempResScale) {
523 resScale_ = tempResScale;
527 if (implicitResidualScalingName) {
528 params_->set (
"Implicit Residual Scaling", resScale_);
531 params_->set (
"Residual Scaling", resScale_);
534 if (! convTest_.is_null()) {
538 catch (std::exception& e) {
547 if (params->isParameter(
"Deflation Quorum")) {
548 defQuorum_ = params->get(
"Deflation Quorum", defQuorum_);
549 params_->set(
"Deflation Quorum", defQuorum_);
550 if (convTest_ != Teuchos::null)
551 convTest_->setQuorum( defQuorum_ );
557 if (maxIterTest_ == Teuchos::null)
561 if (convTest_ == Teuchos::null || newResTest) {
562 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
566 if (sTest_ == Teuchos::null || newResTest)
567 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
569 if (outputTest_ == Teuchos::null || newResTest) {
577 std::string solverDesc =
" Pseudo Block CG ";
578 outputTest_->setSolverDesc( solverDesc );
583 if (timerSolve_ == Teuchos::null) {
584 std::string solveLabel = label_ +
": PseudoBlockStochasticCGSolMgr total solve time";
585 #ifdef BELOS_TEUCHOS_TIME_MONITOR 586 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
595 template<
class ScalarType,
class MV,
class OP>
596 Teuchos::RCP<const Teuchos::ParameterList>
599 using Teuchos::ParameterList;
600 using Teuchos::parameterList;
603 if (validParams_.is_null()) {
605 RCP<ParameterList> pl = parameterList ();
606 pl->set(
"Convergence Tolerance", convtol_default_,
607 "The relative residual tolerance that needs to be achieved by the\n" 608 "iterative solver in order for the linera system to be declared converged.");
609 pl->set(
"Maximum Iterations", maxIters_default_,
610 "The maximum number of block iterations allowed for each\n" 611 "set of RHS solved.");
612 pl->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_default_,
613 "Whether or not to assert that the linear operator\n" 614 "and the preconditioner are indeed positive definite.");
615 pl->set(
"Verbosity", verbosity_default_,
616 "What type(s) of solver information should be outputted\n" 617 "to the output stream.");
618 pl->set(
"Output Style", outputStyle_default_,
619 "What style is used for the solver information outputted\n" 620 "to the output stream.");
621 pl->set(
"Output Frequency", outputFreq_default_,
622 "How often convergence information should be outputted\n" 623 "to the output stream.");
624 pl->set(
"Deflation Quorum", defQuorum_default_,
625 "The number of linear systems that need to converge before\n" 626 "they are deflated. This number should be <= block size.");
627 pl->set(
"Output Stream", outputStream_default_,
628 "A reference-counted pointer to the output stream where all\n" 629 "solver output is sent.");
630 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
631 "When convergence information is printed, only show the maximum\n" 632 "relative residual norm when the block size is greater than one.");
633 pl->set(
"Implicit Residual Scaling", resScale_default_,
634 "The type of scaling used in the residual convergence test.");
640 pl->set(
"Residual Scaling", resScale_default_,
641 "The type of scaling used in the residual convergence test. This " 642 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
643 pl->set(
"Timer Label", label_default_,
644 "The string to use as a prefix for the timer labels.");
653 template<
class ScalarType,
class MV,
class OP>
661 Teuchos::BLAS<int,ScalarType> blas;
664 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
669 int numCurrRHS = numRHS2Solve;
671 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
672 for (
int i=0; i<numRHS2Solve; ++i) {
673 currIdx[i] = startPtr+i;
678 problem_->setLSIndex( currIdx );
682 Teuchos::ParameterList plist;
684 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
687 outputTest_->reset();
690 bool isConverged =
true;
695 Teuchos::RCP<PseudoBlockStochasticCGIter<ScalarType,MV,OP> > block_cg_iter
700 #ifdef BELOS_TEUCHOS_TIME_MONITOR 701 Teuchos::TimeMonitor slvtimer(*timerSolve_);
704 while ( numRHS2Solve > 0 ) {
707 std::vector<int> convRHSIdx;
708 std::vector<int> currRHSIdx( currIdx );
709 currRHSIdx.resize(numCurrRHS);
712 block_cg_iter->resetNumIters();
715 outputTest_->resetNumCalls();
718 Teuchos::RCP<MV> R_0 =
MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
723 block_cg_iter->initializeCG(newState);
729 block_cg_iter->iterate();
736 if ( convTest_->getStatus() ==
Passed ) {
743 if (convIdx.size() == currRHSIdx.size())
747 problem_->setCurrLS();
751 std::vector<int> unconvIdx(currRHSIdx.size());
752 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
754 for (
unsigned int j=0; j<convIdx.size(); ++j) {
755 if (currRHSIdx[i] == convIdx[j]) {
761 currIdx2[have] = currIdx2[i];
762 currRHSIdx[have++] = currRHSIdx[i];
765 currRHSIdx.resize(have);
766 currIdx2.resize(have);
769 problem_->setLSIndex( currRHSIdx );
772 std::vector<MagnitudeType> norms;
773 R_0 =
MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
774 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
779 block_cg_iter->initializeCG(defstate);
787 else if ( maxIterTest_->getStatus() ==
Passed ) {
801 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
802 "Belos::PseudoBlockStochasticCGSolMgr::solve(): Invalid return from PseudoBlockStochasticCGIter::iterate().");
805 catch (
const std::exception &e) {
806 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockStochasticCGIter::iterate() at iteration " 807 << block_cg_iter->getNumIters() << std::endl
808 << e.what() << std::endl;
814 problem_->setCurrLS();
817 startPtr += numCurrRHS;
818 numRHS2Solve -= numCurrRHS;
820 if ( numRHS2Solve > 0 ) {
822 numCurrRHS = numRHS2Solve;
823 currIdx.resize( numCurrRHS );
824 currIdx2.resize( numCurrRHS );
825 for (
int i=0; i<numCurrRHS; ++i)
826 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
829 problem_->setLSIndex( currIdx );
832 currIdx.resize( numRHS2Solve );
840 Y_=block_cg_iter->getStochasticVector();
847 #ifdef BELOS_TEUCHOS_TIME_MONITOR 852 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
856 numIters_ = maxIterTest_->getNumIters();
865 template<
class ScalarType,
class MV,
class OP>
868 std::ostringstream oss;
869 oss <<
"Belos::PseudoBlockStochasticCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
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...
Class which manages the output and verbosity of the Belos solvers.
ScaleType
The type of scaling to use on the residual norm value.
int getNumIters() const
Get the iteration count for the most recent call to solve().
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
PseudoBlockStochasticCGSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to g...
A factory class for generating StatusTestOutput objects.
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.
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::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters for this object.
A Belos::StatusTest class for specifying a maximum number of iterations.
Belos concrete class for performing the stochastic pseudo-block CG iteration.
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. ...
PseudoBlockStochasticCGSolMgrOrthoFailure(const std::string &what_arg)
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters the solver manager should use to solve the linear problem.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
Teuchos::RCP< const MV > R
The current residual.
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 ...
virtual ~PseudoBlockStochasticCGSolMgr()
Destructor.
std::string description() const
Method to return description of the block CG solver manager.
PseudoBlockStochasticCGSolMgr()
Empty constructor for BlockStochasticCGSolMgr. This constructor takes no arguments and sets the defau...
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.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
This class implements the stochastic pseudo-block CG iteration, where the basic stochastic CG algorit...
The Belos::PseudoBlockStochasticCGSolMgr provides a powerful and fully-featured solver manager over t...
Teuchos::RCP< MV > getStochasticVector()
Get a copy of the final stochastic vector.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
A class for extending the status testing capabilities of Belos via logical combinations.
PseudoBlockStochasticCGSolMgrLinearProblemFailure(const std::string &what_arg)
ReturnType solve()
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
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...
PseudoBlockStochasticCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...
Structure to contain pointers to CGIteration state variables.