42 #ifndef BELOS_BICGSTAB_SOLMGR_HPP 43 #define BELOS_BICGSTAB_SOLMGR_HPP 61 #include "Teuchos_BLAS.hpp" 62 #include "Teuchos_LAPACK.hpp" 63 #ifdef BELOS_TEUCHOS_TIME_MONITOR 64 #include "Teuchos_TimeMonitor.hpp" 108 template<
class ScalarType,
class MV,
class OP>
114 typedef Teuchos::ScalarTraits<ScalarType> SCT;
115 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
116 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
140 const Teuchos::RCP<Teuchos::ParameterList> &pl );
155 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const;
166 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
167 return Teuchos::tuple(timerSolve_);
204 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
245 std::string description()
const;
251 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
254 Teuchos::RCP<OutputManager<ScalarType> > printer_;
255 Teuchos::RCP<std::ostream> outputStream_;
258 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
259 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
260 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
261 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
264 Teuchos::RCP<Teuchos::ParameterList> params_;
271 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
274 static const MagnitudeType convtol_default_;
275 static const int maxIters_default_;
276 static const bool showMaxResNormOnly_default_;
277 static const int verbosity_default_;
278 static const int outputStyle_default_;
279 static const int outputFreq_default_;
280 static const int defQuorum_default_;
281 static const std::string resScale_default_;
282 static const std::string label_default_;
283 static const Teuchos::RCP<std::ostream> outputStream_default_;
286 MagnitudeType convtol_,achievedTol_;
287 int maxIters_, numIters_;
288 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
289 bool showMaxResNormOnly_;
290 std::string resScale_;
294 Teuchos::RCP<Teuchos::Time> timerSolve_;
302 template<
class ScalarType,
class MV,
class OP>
305 template<
class ScalarType,
class MV,
class OP>
308 template<
class ScalarType,
class MV,
class OP>
311 template<
class ScalarType,
class MV,
class OP>
314 template<
class ScalarType,
class MV,
class OP>
317 template<
class ScalarType,
class MV,
class OP>
320 template<
class ScalarType,
class MV,
class OP>
323 template<
class ScalarType,
class MV,
class OP>
326 template<
class ScalarType,
class MV,
class OP>
329 template<
class ScalarType,
class MV,
class OP>
333 template<
class ScalarType,
class MV,
class OP>
335 outputStream_(outputStream_default_),
336 convtol_(convtol_default_),
337 maxIters_(maxIters_default_),
339 verbosity_(verbosity_default_),
340 outputStyle_(outputStyle_default_),
341 outputFreq_(outputFreq_default_),
342 defQuorum_(defQuorum_default_),
343 showMaxResNormOnly_(showMaxResNormOnly_default_),
344 resScale_(resScale_default_),
345 label_(label_default_),
350 template<
class ScalarType,
class MV,
class OP>
353 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
355 outputStream_(outputStream_default_),
356 convtol_(convtol_default_),
357 maxIters_(maxIters_default_),
359 verbosity_(verbosity_default_),
360 outputStyle_(outputStyle_default_),
361 outputFreq_(outputFreq_default_),
362 defQuorum_(defQuorum_default_),
363 showMaxResNormOnly_(showMaxResNormOnly_default_),
364 resScale_(resScale_default_),
365 label_(label_default_),
368 TEUCHOS_TEST_FOR_EXCEPTION(
369 problem_.is_null (), std::invalid_argument,
370 "Belos::BiCGStabSolMgr two-argument constructor: " 371 "'problem' is null. You must supply a non-null Belos::LinearProblem " 372 "instance when calling this constructor.");
374 if (! pl.is_null ()) {
380 template<
class ScalarType,
class MV,
class OP>
383 using Teuchos::ParameterList;
384 using Teuchos::parameterList;
390 if (params_.is_null()) {
391 params_ = parameterList (*defaultParams);
393 params->validateParameters (*defaultParams);
397 if (params->isParameter(
"Maximum Iterations")) {
398 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
401 params_->set(
"Maximum Iterations", maxIters_);
402 if (maxIterTest_!=Teuchos::null)
403 maxIterTest_->setMaxIters( maxIters_ );
407 if (params->isParameter(
"Timer Label")) {
408 std::string tempLabel = params->get(
"Timer Label", label_default_);
411 if (tempLabel != label_) {
413 params_->set(
"Timer Label", label_);
414 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
415 #ifdef BELOS_TEUCHOS_TIME_MONITOR 416 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
422 if (params->isParameter(
"Verbosity")) {
423 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
424 verbosity_ = params->get(
"Verbosity", verbosity_default_);
426 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
430 params_->set(
"Verbosity", verbosity_);
431 if (printer_ != Teuchos::null)
432 printer_->setVerbosity(verbosity_);
436 if (params->isParameter(
"Output Style")) {
437 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
438 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
440 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
444 params_->set(
"Output Style", outputStyle_);
445 outputTest_ = Teuchos::null;
449 if (params->isParameter(
"Output Stream")) {
450 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
453 params_->set(
"Output Stream", outputStream_);
454 if (printer_ != Teuchos::null)
455 printer_->setOStream( outputStream_ );
460 if (params->isParameter(
"Output Frequency")) {
461 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
465 params_->set(
"Output Frequency", outputFreq_);
466 if (outputTest_ != Teuchos::null)
467 outputTest_->setOutputFrequency( outputFreq_ );
471 if (printer_ == Teuchos::null) {
480 if (params->isParameter(
"Convergence Tolerance")) {
481 convtol_ = params->get(
"Convergence Tolerance",convtol_default_);
484 params_->set(
"Convergence Tolerance", convtol_);
485 if (convTest_ != Teuchos::null)
486 convTest_->setTolerance( convtol_ );
489 if (params->isParameter(
"Show Maximum Residual Norm Only")) {
490 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
493 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
494 if (convTest_ != Teuchos::null)
495 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
499 bool newResTest =
false;
504 std::string tempResScale = resScale_;
505 bool implicitResidualScalingName =
false;
506 if (params->isParameter (
"Residual Scaling")) {
507 tempResScale = params->get<std::string> (
"Residual Scaling");
509 else if (params->isParameter (
"Implicit Residual Scaling")) {
510 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
511 implicitResidualScalingName =
true;
515 if (resScale_ != tempResScale) {
517 resScale_ = tempResScale;
521 if (implicitResidualScalingName) {
522 params_->set (
"Implicit Residual Scaling", resScale_);
525 params_->set (
"Residual Scaling", resScale_);
528 if (! convTest_.is_null()) {
532 catch (std::exception& e) {
541 if (params->isParameter(
"Deflation Quorum")) {
542 defQuorum_ = params->get(
"Deflation Quorum", defQuorum_);
543 params_->set(
"Deflation Quorum", defQuorum_);
544 if (convTest_ != Teuchos::null)
545 convTest_->setQuorum( defQuorum_ );
551 if (maxIterTest_ == Teuchos::null)
555 if (convTest_ == Teuchos::null || newResTest) {
556 convTest_ = Teuchos::rcp(
new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
560 if (sTest_ == Teuchos::null || newResTest)
561 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
563 if (outputTest_ == Teuchos::null || newResTest) {
571 std::string solverDesc =
" Pseudo Block BiCGStab ";
572 outputTest_->setSolverDesc( solverDesc );
577 if (timerSolve_ == Teuchos::null) {
578 std::string solveLabel = label_ +
": BiCGStabSolMgr total solve time";
579 #ifdef BELOS_TEUCHOS_TIME_MONITOR 580 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
589 template<
class ScalarType,
class MV,
class OP>
590 Teuchos::RCP<const Teuchos::ParameterList>
593 using Teuchos::ParameterList;
594 using Teuchos::parameterList;
597 if (validParams_.is_null()) {
599 RCP<ParameterList> pl = parameterList ();
600 pl->set(
"Convergence Tolerance", convtol_default_,
601 "The relative residual tolerance that needs to be achieved by the\n" 602 "iterative solver in order for the linera system to be declared converged.");
603 pl->set(
"Maximum Iterations", maxIters_default_,
604 "The maximum number of block iterations allowed for each\n" 605 "set of RHS solved.");
606 pl->set(
"Verbosity", verbosity_default_,
607 "What type(s) of solver information should be outputted\n" 608 "to the output stream.");
609 pl->set(
"Output Style", outputStyle_default_,
610 "What style is used for the solver information outputted\n" 611 "to the output stream.");
612 pl->set(
"Output Frequency", outputFreq_default_,
613 "How often convergence information should be outputted\n" 614 "to the output stream.");
615 pl->set(
"Deflation Quorum", defQuorum_default_,
616 "The number of linear systems that need to converge before\n" 617 "they are deflated. This number should be <= block size.");
618 pl->set(
"Output Stream", outputStream_default_,
619 "A reference-counted pointer to the output stream where all\n" 620 "solver output is sent.");
621 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
622 "When convergence information is printed, only show the maximum\n" 623 "relative residual norm when the block size is greater than one.");
624 pl->set(
"Implicit Residual Scaling", resScale_default_,
625 "The type of scaling used in the residual convergence test.");
631 pl->set(
"Residual Scaling", resScale_default_,
632 "The type of scaling used in the residual convergence test. This " 633 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
634 pl->set(
"Timer Label", label_default_,
635 "The string to use as a prefix for the timer labels.");
642 template<
class ScalarType,
class MV,
class OP>
652 Teuchos::BLAS<int,ScalarType> blas;
654 TEUCHOS_TEST_FOR_EXCEPTION
656 "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. " 657 "You must call setProblem() on the LinearProblem before you may solve it.");
658 TEUCHOS_TEST_FOR_EXCEPTION
659 (problem_->isLeftPrec (), std::logic_error,
"Belos::BiCGStabSolMgr::solve: " 660 "The left-preconditioned case has not yet been implemented. Please use " 661 "right preconditioning for now. If you need to use left preconditioning, " 662 "please contact the Belos developers. Left preconditioning is more " 663 "interesting in BiCGStab because whether it works depends on the initial " 664 "guess (e.g., an initial guess of all zeros might NOT work).");
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;
685 outputTest_->reset();
688 bool isConverged =
true;
693 Teuchos::RCP<BiCGStabIter<ScalarType,MV,OP> > block_cg_iter
698 #ifdef BELOS_TEUCHOS_TIME_MONITOR 699 Teuchos::TimeMonitor slvtimer(*timerSolve_);
703 while ( numRHS2Solve > 0 ) {
705 std::vector<int> convRHSIdx;
706 std::vector<int> currRHSIdx( currIdx );
707 currRHSIdx.resize(numCurrRHS);
710 block_cg_iter->resetNumIters();
713 outputTest_->resetNumCalls();
716 Teuchos::RCP<MV> R_0 =
MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
721 block_cg_iter->initializeBiCGStab(newState);
728 block_cg_iter->iterate();
735 if ( convTest_->getStatus() ==
Passed ) {
742 if (convIdx.size() == currRHSIdx.size())
746 problem_->setCurrLS();
750 std::vector<int> unconvIdx(currRHSIdx.size());
751 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
753 for (
unsigned int j=0; j<convIdx.size(); ++j) {
754 if (currRHSIdx[i] == convIdx[j]) {
760 currIdx2[have] = currIdx2[i];
761 currRHSIdx[have++] = currRHSIdx[i];
764 currRHSIdx.resize(have);
765 currIdx2.resize(have);
768 problem_->setLSIndex( currRHSIdx );
771 std::vector<MagnitudeType> norms;
772 R_0 =
MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
773 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
778 block_cg_iter->initializeBiCGStab(defstate);
786 else if ( maxIterTest_->getStatus() ==
Passed ) {
800 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
801 "Belos::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
804 catch (
const std::exception &e) {
805 printer_->stream(
Errors) <<
"Error! Caught std::exception in BiCGStabIter::iterate() at iteration " 806 << block_cg_iter->getNumIters() << std::endl
807 << e.what() << std::endl;
813 problem_->setCurrLS();
816 startPtr += numCurrRHS;
817 numRHS2Solve -= numCurrRHS;
819 if ( numRHS2Solve > 0 ) {
821 numCurrRHS = numRHS2Solve;
822 currIdx.resize( numCurrRHS );
823 currIdx2.resize( numCurrRHS );
824 for (
int i=0; i<numCurrRHS; ++i)
825 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
828 problem_->setLSIndex( currIdx );
831 currIdx.resize( numRHS2Solve );
843 #ifdef BELOS_TEUCHOS_TIME_MONITOR 848 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
852 numIters_ = maxIterTest_->getNumIters();
857 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
858 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
868 template<
class ScalarType,
class MV,
class OP>
871 std::ostringstream oss;
872 oss <<
"Belos::BiCGStabSolMgr<...,"<<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.
BiCGStabSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to generate orthono...
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.
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.
An implementation of StatusTestResNorm using a family of residual norms.
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
std::string description() const
Method to return description of the block BiCGStab solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters for this object.
Belos::StatusTest class for specifying a maximum number of iterations.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters the solver manager should use to solve the linear problem.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
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.
BiCGStabSolMgrLinearProblemFailure(const std::string &what_arg)
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...
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. ...
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.
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.
BiCGStabSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
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.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
void reset(const ResetType type)
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
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.
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.
Belos header file which uses auto-configuration information to include necessary C++ headers...
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
BiCGStabSolMgrOrthoFailure(const std::string &what_arg)
Belos concrete class for performing the pseudo-block BiCGStab iteration.