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 );
166 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
204 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
251 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> >
problem_;
258 Teuchos::RCP<StatusTest<ScalarType,MV,OP> >
sTest_;
260 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
convTest_;
264 Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> >
ortho_;
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>
332 template<
class ScalarType,
class MV,
class OP>
336 template<
class ScalarType,
class MV,
class OP>
338 outputStream_(outputStream_default_),
339 convtol_(convtol_default_),
340 maxIters_(maxIters_default_),
342 verbosity_(verbosity_default_),
343 outputStyle_(outputStyle_default_),
344 outputFreq_(outputFreq_default_),
345 defQuorum_(defQuorum_default_),
346 showMaxResNormOnly_(showMaxResNormOnly_default_),
347 resScale_(resScale_default_),
348 label_(label_default_),
353 template<
class ScalarType,
class MV,
class OP>
356 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
358 outputStream_(outputStream_default_),
359 convtol_(convtol_default_),
360 maxIters_(maxIters_default_),
362 verbosity_(verbosity_default_),
363 outputStyle_(outputStyle_default_),
364 outputFreq_(outputFreq_default_),
365 defQuorum_(defQuorum_default_),
366 showMaxResNormOnly_(showMaxResNormOnly_default_),
367 resScale_(resScale_default_),
368 label_(label_default_),
371 TEUCHOS_TEST_FOR_EXCEPTION(
372 problem_.is_null (), std::invalid_argument,
373 "Belos::BiCGStabSolMgr two-argument constructor: " 374 "'problem' is null. You must supply a non-null Belos::LinearProblem " 375 "instance when calling this constructor.");
377 if (! pl.is_null ()) {
383 template<
class ScalarType,
class MV,
class OP>
386 using Teuchos::ParameterList;
387 using Teuchos::parameterList;
390 RCP<const ParameterList> defaultParams = getValidParameters();
393 if (params_.is_null()) {
394 params_ = parameterList (*defaultParams);
396 params->validateParameters (*defaultParams);
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(
"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_ +
": BiCGStabSolMgr 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)
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 BiCGStab ";
578 outputTest_->setSolverDesc( solverDesc );
583 if (timerSolve_ == Teuchos::null) {
584 std::string solveLabel = label_ +
": BiCGStabSolMgr 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(
"Verbosity", verbosity_default_,
613 "What type(s) of solver information should be outputted\n" 614 "to the output stream.");
615 pl->set(
"Output Style", outputStyle_default_,
616 "What style is used for the solver information outputted\n" 617 "to the output stream.");
618 pl->set(
"Output Frequency", outputFreq_default_,
619 "How often convergence information should be outputted\n" 620 "to the output stream.");
621 pl->set(
"Deflation Quorum", defQuorum_default_,
622 "The number of linear systems that need to converge before\n" 623 "they are deflated. This number should be <= block size.");
624 pl->set(
"Output Stream", outputStream_default_,
625 "A reference-counted pointer to the output stream where all\n" 626 "solver output is sent.");
627 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
628 "When convergence information is printed, only show the maximum\n" 629 "relative residual norm when the block size is greater than one.");
630 pl->set(
"Implicit Residual Scaling", resScale_default_,
631 "The type of scaling used in the residual convergence test.");
637 pl->set(
"Residual Scaling", resScale_default_,
638 "The type of scaling used in the residual convergence test. This " 639 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
640 pl->set(
"Timer Label", label_default_,
641 "The string to use as a prefix for the timer labels.");
649 template<
class ScalarType,
class MV,
class OP>
656 setParameters (params_);
659 Teuchos::BLAS<int,ScalarType> blas;
661 TEUCHOS_TEST_FOR_EXCEPTION
663 "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. " 664 "You must call setProblem() on the LinearProblem before you may solve it.");
665 TEUCHOS_TEST_FOR_EXCEPTION
666 (problem_->isLeftPrec (), std::logic_error,
"Belos::BiCGStabSolMgr::solve: " 667 "The left-preconditioned case has not yet been implemented. Please use " 668 "right preconditioning for now. If you need to use left preconditioning, " 669 "please contact the Belos developers. Left preconditioning is more " 670 "interesting in BiCGStab because whether it works depends on the initial " 671 "guess (e.g., an initial guess of all zeros might NOT work).");
675 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
676 int numCurrRHS = numRHS2Solve;
678 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
679 for (
int i=0; i<numRHS2Solve; ++i) {
680 currIdx[i] = startPtr+i;
685 problem_->setLSIndex( currIdx );
689 Teuchos::ParameterList plist;
692 outputTest_->reset();
695 bool isConverged =
true;
700 Teuchos::RCP<BiCGStabIter<ScalarType,MV,OP> > block_cg_iter
705 #ifdef BELOS_TEUCHOS_TIME_MONITOR 706 Teuchos::TimeMonitor slvtimer(*timerSolve_);
710 while ( numRHS2Solve > 0 ) {
712 std::vector<int> convRHSIdx;
713 std::vector<int> currRHSIdx( currIdx );
714 currRHSIdx.resize(numCurrRHS);
717 block_cg_iter->resetNumIters();
720 outputTest_->resetNumCalls();
723 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
728 block_cg_iter->initializeBiCGStab(newState);
735 block_cg_iter->iterate();
742 if ( convTest_->getStatus() ==
Passed ) {
749 if (convIdx.size() == currRHSIdx.size())
753 problem_->setCurrLS();
757 std::vector<int> unconvIdx(currRHSIdx.size());
758 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
760 for (
unsigned int j=0; j<convIdx.size(); ++j) {
761 if (currRHSIdx[i] == convIdx[j]) {
767 currIdx2[have] = currIdx2[i];
768 currRHSIdx[have++] = currRHSIdx[i];
771 currRHSIdx.resize(have);
772 currIdx2.resize(have);
775 problem_->setLSIndex( currRHSIdx );
778 std::vector<MagnitudeType> norms;
779 R_0 = MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
780 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
785 block_cg_iter->initializeBiCGStab(defstate);
793 else if ( maxIterTest_->getStatus() ==
Passed ) {
807 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
808 "Belos::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
811 catch (
const std::exception &e) {
812 printer_->stream(
Errors) <<
"Error! Caught std::exception in BiCGStabIter::iterate() at iteration " 813 << block_cg_iter->getNumIters() << std::endl
814 << e.what() << std::endl;
820 problem_->setCurrLS();
823 startPtr += numCurrRHS;
824 numRHS2Solve -= numCurrRHS;
826 if ( numRHS2Solve > 0 ) {
828 numCurrRHS = numRHS2Solve;
829 currIdx.resize( numCurrRHS );
830 currIdx2.resize( numCurrRHS );
831 for (
int i=0; i<numCurrRHS; ++i)
832 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
835 problem_->setLSIndex( currIdx );
838 currIdx.resize( numRHS2Solve );
850 #ifdef BELOS_TEUCHOS_TIME_MONITOR 855 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
859 numIters_ = maxIterTest_->getNumIters();
864 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
865 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
875 template<
class ScalarType,
class MV,
class OP>
878 std::ostringstream oss;
879 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...
Teuchos::ScalarTraits< ScalarType >::magnitudeType MagnitudeType
Teuchos::RCP< const Teuchos::ParameterList > validParams_
List of valid parameters and their default values.
Class which manages the output and verbosity of the Belos solvers.
Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > problem_
Teuchos::ScalarTraits< MagnitudeType > MT
Teuchos::RCP< const MV > R
The current residual.
ScaleType
The type of scaling to use on the residual norm value.
static const int maxIters_default_
OperatorTraits< ScalarType, MV, OP > OPT
static const MagnitudeType convtol_default_
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > sTest_
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.
static const int defQuorum_default_
static const int outputFreq_default_
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.
static const Teuchos::RCP< std::ostream > outputStream_default_
static const std::string label_default_
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.
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...
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > outputTest_
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. ...
static const bool showMaxResNormOnly_default_
BiCGStabSolMgr()
Empty constructor for BiCGStabSolMgr. This constructor takes no arguments and sets the default values...
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > convTest_
Teuchos::RCP< StatusTestMaxIters< ScalarType, MV, OP > > maxIterTest_
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.
Teuchos::RCP< std::ostream > outputStream_
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...
ReturnType
Whether the Belos solve converged for all linear systems.
MultiVecTraits< ScalarType, MV > MVT
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 ...
Teuchos::RCP< Teuchos::Time > timerSolve_
static const int verbosity_default_
Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP > > ortho_
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
static const std::string resScale_default_
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::ScalarTraits< ScalarType > SCT
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< OutputManager< ScalarType > > printer_
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...
Teuchos::RCP< Teuchos::ParameterList > params_
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
MagnitudeType achievedTol_
static const int outputStyle_default_
BiCGStabSolMgrOrthoFailure(const std::string &what_arg)
Belos concrete class for performing the pseudo-block BiCGStab iteration.