42 #ifndef BELOS_BICGSTAB_SOLMGR_HPP 43 #define BELOS_BICGSTAB_SOLMGR_HPP 61 #ifdef BELOS_TEUCHOS_TIME_MONITOR 62 #include "Teuchos_TimeMonitor.hpp" 96 template<
class ScalarType,
class MV,
class OP>
102 typedef Teuchos::ScalarTraits<ScalarType> SCT;
103 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
104 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
128 const Teuchos::RCP<Teuchos::ParameterList> &pl );
134 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
159 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
160 return Teuchos::tuple(timerSolve_);
197 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
244 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
247 Teuchos::RCP<OutputManager<ScalarType> > printer_;
248 Teuchos::RCP<std::ostream> outputStream_;
251 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
252 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
253 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
254 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
257 Teuchos::RCP<Teuchos::ParameterList> params_;
264 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
267 static constexpr
int maxIters_default_ = 1000;
268 static constexpr
bool showMaxResNormOnly_default_ =
false;
271 static constexpr
int outputFreq_default_ = -1;
272 static constexpr
int defQuorum_default_ = 1;
273 static constexpr
const char * resScale_default_ =
"Norm of Initial Residual";
274 static constexpr
const char * label_default_ =
"Belos";
276 #if defined(_WIN32) && defined(__clang__) 277 static constexpr std::ostream * outputStream_default_ =
278 __builtin_constant_p(reinterpret_cast<const std::ostream*>(&std::cout));
280 static constexpr std::ostream * outputStream_default_ = &std::cout;
284 MagnitudeType convtol_,achievedTol_;
285 int maxIters_, numIters_;
286 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
287 bool showMaxResNormOnly_;
288 std::string resScale_;
292 Teuchos::RCP<Teuchos::Time> timerSolve_;
299 template<
class ScalarType,
class MV,
class OP>
301 outputStream_(Teuchos::rcp(outputStream_default_,false)),
303 maxIters_(maxIters_default_),
305 verbosity_(verbosity_default_),
306 outputStyle_(outputStyle_default_),
307 outputFreq_(outputFreq_default_),
308 defQuorum_(defQuorum_default_),
309 showMaxResNormOnly_(showMaxResNormOnly_default_),
310 resScale_(resScale_default_),
311 label_(label_default_),
316 template<
class ScalarType,
class MV,
class OP>
319 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
321 outputStream_(Teuchos::rcp(outputStream_default_,false)),
323 maxIters_(maxIters_default_),
325 verbosity_(verbosity_default_),
326 outputStyle_(outputStyle_default_),
327 outputFreq_(outputFreq_default_),
328 defQuorum_(defQuorum_default_),
329 showMaxResNormOnly_(showMaxResNormOnly_default_),
330 resScale_(resScale_default_),
331 label_(label_default_),
334 TEUCHOS_TEST_FOR_EXCEPTION(
335 problem_.is_null (), std::invalid_argument,
336 "Belos::BiCGStabSolMgr two-argument constructor: " 337 "'problem' is null. You must supply a non-null Belos::LinearProblem " 338 "instance when calling this constructor.");
340 if (! pl.is_null ()) {
346 template<
class ScalarType,
class MV,
class OP>
349 using Teuchos::ParameterList;
350 using Teuchos::parameterList;
353 RCP<const ParameterList> defaultParams = getValidParameters();
356 if (params_.is_null()) {
357 params_ = parameterList (*defaultParams);
359 params->validateParameters (*defaultParams);
363 if (params->isParameter(
"Maximum Iterations")) {
364 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
367 params_->set(
"Maximum Iterations", maxIters_);
368 if (maxIterTest_!=Teuchos::null)
369 maxIterTest_->setMaxIters( maxIters_ );
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_ +
": BiCGStabSolMgr 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)
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_ );
471 bool newResTest =
false;
476 std::string tempResScale = resScale_;
477 bool implicitResidualScalingName =
false;
478 if (params->isParameter (
"Residual Scaling")) {
479 tempResScale = params->get<std::string> (
"Residual Scaling");
481 else if (params->isParameter (
"Implicit Residual Scaling")) {
482 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
483 implicitResidualScalingName =
true;
487 if (resScale_ != tempResScale) {
489 resScale_ = tempResScale;
493 if (implicitResidualScalingName) {
494 params_->set (
"Implicit Residual Scaling", resScale_);
497 params_->set (
"Residual Scaling", resScale_);
500 if (! convTest_.is_null()) {
504 catch (std::exception& e) {
513 if (params->isParameter(
"Deflation Quorum")) {
514 defQuorum_ = params->get(
"Deflation Quorum", defQuorum_);
515 params_->set(
"Deflation Quorum", defQuorum_);
516 if (convTest_ != Teuchos::null)
517 convTest_->setQuorum( defQuorum_ );
523 if (maxIterTest_ == Teuchos::null)
527 if (convTest_ == Teuchos::null || newResTest) {
528 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
532 if (sTest_ == Teuchos::null || newResTest)
533 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
535 if (outputTest_ == Teuchos::null || newResTest) {
543 std::string solverDesc =
" Pseudo Block BiCGStab ";
544 outputTest_->setSolverDesc( solverDesc );
549 if (timerSolve_ == Teuchos::null) {
550 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
551 #ifdef BELOS_TEUCHOS_TIME_MONITOR 552 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
561 template<
class ScalarType,
class MV,
class OP>
562 Teuchos::RCP<const Teuchos::ParameterList>
565 using Teuchos::ParameterList;
566 using Teuchos::parameterList;
569 if (validParams_.is_null()) {
571 RCP<ParameterList> pl = parameterList ();
576 "The relative residual tolerance that needs to be achieved by the\n" 577 "iterative solver in order for the linera system to be declared converged.");
578 pl->set(
"Maximum Iterations", static_cast<int>(maxIters_default_),
579 "The maximum number of block iterations allowed for each\n" 580 "set of RHS solved.");
581 pl->set(
"Verbosity", static_cast<int>(verbosity_default_),
582 "What type(s) of solver information should be outputted\n" 583 "to the output stream.");
584 pl->set(
"Output Style", static_cast<int>(outputStyle_default_),
585 "What style is used for the solver information outputted\n" 586 "to the output stream.");
587 pl->set(
"Output Frequency", static_cast<int>(outputFreq_default_),
588 "How often convergence information should be outputted\n" 589 "to the output stream.");
590 pl->set(
"Deflation Quorum", static_cast<int>(defQuorum_default_),
591 "The number of linear systems that need to converge before\n" 592 "they are deflated. This number should be <= block size.");
593 pl->set(
"Output Stream", Teuchos::rcp(outputStream_default_,
false),
594 "A reference-counted pointer to the output stream where all\n" 595 "solver output is sent.");
596 pl->set(
"Show Maximum Residual Norm Only", static_cast<bool>(showMaxResNormOnly_default_),
597 "When convergence information is printed, only show the maximum\n" 598 "relative residual norm when the block size is greater than one.");
599 pl->set(
"Implicit Residual Scaling", static_cast<const char *>(resScale_default_),
600 "The type of scaling used in the residual convergence test.");
606 pl->set(
"Residual Scaling", static_cast<const char *>(resScale_default_),
607 "The type of scaling used in the residual convergence test. This " 608 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
609 pl->set(
"Timer Label", static_cast<const char *>(label_default_),
610 "The string to use as a prefix for the timer labels.");
617 template<
class ScalarType,
class MV,
class OP>
624 setParameters (params_);
627 TEUCHOS_TEST_FOR_EXCEPTION
629 "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. " 630 "You must call setProblem() on the LinearProblem before you may solve it.");
631 TEUCHOS_TEST_FOR_EXCEPTION
632 (problem_->isLeftPrec (), std::logic_error,
"Belos::BiCGStabSolMgr::solve: " 633 "The left-preconditioned case has not yet been implemented. Please use " 634 "right preconditioning for now. If you need to use left preconditioning, " 635 "please contact the Belos developers. Left preconditioning is more " 636 "interesting in BiCGStab because whether it works depends on the initial " 637 "guess (e.g., an initial guess of all zeros might NOT work).");
641 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
642 int numCurrRHS = numRHS2Solve;
644 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
645 for (
int i=0; i<numRHS2Solve; ++i) {
646 currIdx[i] = startPtr+i;
651 problem_->setLSIndex( currIdx );
655 Teuchos::ParameterList plist;
658 outputTest_->reset();
661 bool isConverged =
true;
666 Teuchos::RCP<BiCGStabIter<ScalarType,MV,OP> > bicgstab_iter
671 #ifdef BELOS_TEUCHOS_TIME_MONITOR 672 Teuchos::TimeMonitor slvtimer(*timerSolve_);
676 while ( numRHS2Solve > 0 ) {
678 std::vector<int> convRHSIdx;
679 std::vector<int> currRHSIdx( currIdx );
680 currRHSIdx.resize(numCurrRHS);
683 bicgstab_iter->resetNumIters();
686 outputTest_->resetNumCalls();
689 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
694 bicgstab_iter->initializeBiCGStab(newState);
701 bicgstab_iter->iterate();
708 if ( convTest_->getStatus() ==
Passed ) {
715 if (convIdx.size() == currRHSIdx.size())
719 problem_->setCurrLS();
723 std::vector<int> unconvIdx(currRHSIdx.size());
724 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
726 for (
unsigned int j=0; j<convIdx.size(); ++j) {
727 if (currRHSIdx[i] == convIdx[j]) {
733 currIdx2[have] = currIdx2[i];
734 currRHSIdx[have++] = currRHSIdx[i];
737 currRHSIdx.resize(have);
738 currIdx2.resize(have);
741 problem_->setLSIndex( currRHSIdx );
744 std::vector<MagnitudeType> norms;
745 R_0 = MVT::CloneCopy( *(bicgstab_iter->getNativeResiduals(&norms)),currIdx2 );
746 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
751 bicgstab_iter->initializeBiCGStab(defstate);
759 else if ( maxIterTest_->getStatus() ==
Passed ) {
772 else if ( bicgstab_iter->breakdownDetected() ) {
776 "Belos::BiCGStabSolMgr::solve(): Warning! Solver has experienced a breakdown!" << std::endl;
788 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
789 "Belos::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
792 catch (
const std::exception &e) {
793 printer_->stream(
Errors) <<
"Error! Caught std::exception in BiCGStabIter::iterate() at iteration " 794 << bicgstab_iter->getNumIters() << std::endl
795 << e.what() << std::endl;
801 problem_->setCurrLS();
804 startPtr += numCurrRHS;
805 numRHS2Solve -= numCurrRHS;
807 if ( numRHS2Solve > 0 ) {
809 numCurrRHS = numRHS2Solve;
810 currIdx.resize( numCurrRHS );
811 currIdx2.resize( numCurrRHS );
812 for (
int i=0; i<numCurrRHS; ++i)
813 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
816 problem_->setLSIndex( currIdx );
819 currIdx.resize( numRHS2Solve );
831 #ifdef BELOS_TEUCHOS_TIME_MONITOR 836 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
840 numIters_ = maxIterTest_->getNumIters();
845 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
846 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
856 template<
class ScalarType,
class MV,
class OP>
859 std::ostringstream oss;
860 oss <<
"Belos::BiCGStabSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
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...
This class implements the pseudo-block BiCGStab iteration, where the basic BiCGStab algorithm is perf...
Class which manages the output and verbosity of the Belos solvers.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.
Teuchos::RCP< const MV > R
The current residual.
ScaleType
The type of scaling to use on the residual norm value.
A factory class for generating StatusTestOutput objects.
Structure to contain pointers to BiCGStabIteration state variables.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
An implementation of StatusTestResNorm using a family of residual norms.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
static const double convTol
Default convergence tolerance.
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.
BiCGStabSolMgrLinearProblemFailure(const std::string &what_arg)
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. ...
BiCGStabSolMgr()
Empty constructor for BiCGStabSolMgr. This constructor takes no arguments and sets the default values...
virtual ~BiCGStabSolMgr()
Destructor.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
BiCGStabSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
ReturnType solve() override
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.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
The Belos::BiCGStabSolMgr provides a powerful and fully-featured solver manager over the pseudo-block...
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.
std::string description() const override
Method to return description of the block BiCGStab solver manager.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
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.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Default parameters common to most Belos solvers.
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve...
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Belos concrete class for performing the pseudo-block BiCGStab iteration.