42 #ifndef BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP 43 #define BELOS_PSEUDO_BLOCK_CG_SOLMGR_HPP 61 #include "Teuchos_BLAS.hpp" 62 #include "Teuchos_LAPACK.hpp" 63 #ifdef BELOS_TEUCHOS_TIME_MONITOR 64 #include "Teuchos_TimeMonitor.hpp" 111 template<
class ScalarType,
class MV,
class OP,
112 const bool supportsScalarType =
116 Belos::Details::LapackSupportsScalar<ScalarType>::value>
118 static const bool scalarTypeIsSupported =
128 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
133 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
138 template<
class ScalarType,
class MV,
class OP>
145 typedef Teuchos::ScalarTraits<ScalarType> SCT;
146 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
147 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
177 const Teuchos::RCP<Teuchos::ParameterList> &pl );
192 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const;
203 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
204 return Teuchos::tuple(timerSolve_);
238 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> >
250 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
291 std::string description()
const;
296 void compute_condnum_tridiag_sym(Teuchos::ArrayView<MagnitudeType> diag,
297 Teuchos::ArrayView<MagnitudeType> offdiag,
298 ScalarType & lambda_min,
299 ScalarType & lambda_max,
300 ScalarType & ConditionNumber );
303 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
306 Teuchos::RCP<OutputManager<ScalarType> > printer_;
307 Teuchos::RCP<std::ostream> outputStream_;
310 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
311 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
312 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
313 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
316 Teuchos::RCP<Teuchos::ParameterList> params_;
323 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
326 static const MagnitudeType convtol_default_;
327 static const int maxIters_default_;
328 static const bool assertPositiveDefiniteness_default_;
329 static const bool showMaxResNormOnly_default_;
330 static const int verbosity_default_;
331 static const int outputStyle_default_;
332 static const int outputFreq_default_;
333 static const int defQuorum_default_;
334 static const std::string resScale_default_;
335 static const std::string label_default_;
336 static const Teuchos::RCP<std::ostream> outputStream_default_;
337 static const bool genCondEst_default_;
340 MagnitudeType convtol_,achievedTol_;
341 int maxIters_, numIters_;
342 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
343 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
344 std::string resScale_;
346 ScalarType condEstimate_;
350 Teuchos::RCP<Teuchos::Time> timerSolve_;
358 template<
class ScalarType,
class MV,
class OP>
361 template<
class ScalarType,
class MV,
class OP>
364 template<
class ScalarType,
class MV,
class OP>
367 template<
class ScalarType,
class MV,
class OP>
370 template<
class ScalarType,
class MV,
class OP>
373 template<
class ScalarType,
class MV,
class OP>
376 template<
class ScalarType,
class MV,
class OP>
379 template<
class ScalarType,
class MV,
class OP>
382 template<
class ScalarType,
class MV,
class OP>
385 template<
class ScalarType,
class MV,
class OP>
388 template<
class ScalarType,
class MV,
class OP>
391 template<
class ScalarType,
class MV,
class OP>
395 template<
class ScalarType,
class MV,
class OP>
397 outputStream_(outputStream_default_),
398 convtol_(convtol_default_),
399 maxIters_(maxIters_default_),
401 verbosity_(verbosity_default_),
402 outputStyle_(outputStyle_default_),
403 outputFreq_(outputFreq_default_),
404 defQuorum_(defQuorum_default_),
405 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
406 showMaxResNormOnly_(showMaxResNormOnly_default_),
407 resScale_(resScale_default_),
408 genCondEst_(genCondEst_default_),
409 condEstimate_(-Teuchos::ScalarTraits<ScalarType>::one()),
410 label_(label_default_),
415 template<
class ScalarType,
class MV,
class OP>
418 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
420 outputStream_(outputStream_default_),
421 convtol_(convtol_default_),
422 maxIters_(maxIters_default_),
424 verbosity_(verbosity_default_),
425 outputStyle_(outputStyle_default_),
426 outputFreq_(outputFreq_default_),
427 defQuorum_(defQuorum_default_),
428 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
429 showMaxResNormOnly_(showMaxResNormOnly_default_),
430 resScale_(resScale_default_),
431 genCondEst_(genCondEst_default_),
432 condEstimate_(-Teuchos::ScalarTraits<ScalarType>::one()),
433 label_(label_default_),
436 TEUCHOS_TEST_FOR_EXCEPTION(
437 problem_.is_null (), std::invalid_argument,
438 "Belos::PseudoBlockCGSolMgr two-argument constructor: " 439 "'problem' is null. You must supply a non-null Belos::LinearProblem " 440 "instance when calling this constructor.");
442 if (! pl.is_null ()) {
448 template<
class ScalarType,
class MV,
class OP>
453 using Teuchos::ParameterList;
454 using Teuchos::parameterList;
458 RCP<const ParameterList> defaultParams = this->getValidParameters ();
477 if (params_.is_null ()) {
479 params_ = parameterList (defaultParams->name ());
481 params->validateParameters (*defaultParams);
485 if (params->isParameter (
"Maximum Iterations")) {
486 maxIters_ = params->get (
"Maximum Iterations", maxIters_default_);
489 params_->set (
"Maximum Iterations", maxIters_);
490 if (! maxIterTest_.is_null ()) {
491 maxIterTest_->setMaxIters (maxIters_);
496 if (params->isParameter (
"Assert Positive Definiteness")) {
497 assertPositiveDefiniteness_ =
498 params->get (
"Assert Positive Definiteness",
499 assertPositiveDefiniteness_default_);
502 params_->set (
"Assert Positive Definiteness", assertPositiveDefiniteness_);
506 if (params->isParameter (
"Timer Label")) {
507 const std::string tempLabel = params->get (
"Timer Label", label_default_);
510 if (tempLabel != label_) {
512 params_->set (
"Timer Label", label_);
513 const std::string solveLabel =
514 label_ +
": PseudoBlockCGSolMgr total solve time";
515 #ifdef BELOS_TEUCHOS_TIME_MONITOR 516 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
522 if (params->isParameter (
"Verbosity")) {
523 if (Teuchos::isParameterType<int> (*params,
"Verbosity")) {
524 verbosity_ = params->get (
"Verbosity", verbosity_default_);
526 verbosity_ = (int) Teuchos::getParameter<Belos::MsgType> (*params,
"Verbosity");
530 params_->set (
"Verbosity", verbosity_);
531 if (! printer_.is_null ()) {
532 printer_->setVerbosity (verbosity_);
537 if (params->isParameter (
"Output Style")) {
538 if (Teuchos::isParameterType<int> (*params,
"Output Style")) {
539 outputStyle_ = params->get (
"Output Style", outputStyle_default_);
542 outputStyle_ = (int) Teuchos::getParameter<Belos::OutputType> (*params,
"Output Style");
547 params_->set (
"Output Style", outputStyle_);
548 outputTest_ = Teuchos::null;
552 if (params->isParameter (
"Output Stream")) {
553 outputStream_ = params->get<RCP<std::ostream> > (
"Output Stream");
556 params_->set (
"Output Stream", outputStream_);
557 if (! printer_.is_null ()) {
558 printer_->setOStream (outputStream_);
564 if (params->isParameter (
"Output Frequency")) {
565 outputFreq_ = params->get (
"Output Frequency", outputFreq_default_);
569 params_->set (
"Output Frequency", outputFreq_);
570 if (! outputTest_.is_null ()) {
571 outputTest_->setOutputFrequency (outputFreq_);
576 if (params->isParameter (
"Estimate Condition Number")) {
577 genCondEst_ = params->get (
"Estimate Condition Number", genCondEst_default_);
581 if (printer_.is_null ()) {
590 if (params->isParameter (
"Convergence Tolerance")) {
591 convtol_ = params->get (
"Convergence Tolerance", convtol_default_);
594 params_->set (
"Convergence Tolerance", convtol_);
595 if (! convTest_.is_null ()) {
600 if (params->isParameter (
"Show Maximum Residual Norm Only")) {
601 showMaxResNormOnly_ = params->get<
bool> (
"Show Maximum Residual Norm Only");
604 params_->set (
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
605 if (! convTest_.is_null ()) {
606 convTest_->setShowMaxResNormOnly (showMaxResNormOnly_);
611 bool newResTest =
false;
616 std::string tempResScale = resScale_;
617 bool implicitResidualScalingName =
false;
618 if (params->isParameter (
"Residual Scaling")) {
619 tempResScale = params->get<std::string> (
"Residual Scaling");
621 else if (params->isParameter (
"Implicit Residual Scaling")) {
622 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
623 implicitResidualScalingName =
true;
627 if (resScale_ != tempResScale) {
630 resScale_ = tempResScale;
634 if (implicitResidualScalingName) {
635 params_->set (
"Implicit Residual Scaling", resScale_);
638 params_->set (
"Residual Scaling", resScale_);
641 if (! convTest_.is_null ()) {
645 catch (std::exception& e) {
654 if (params->isParameter (
"Deflation Quorum")) {
655 defQuorum_ = params->get (
"Deflation Quorum", defQuorum_);
656 params_->set (
"Deflation Quorum", defQuorum_);
657 if (! convTest_.is_null ()) {
658 convTest_->setQuorum( defQuorum_ );
665 if (maxIterTest_.is_null ()) {
670 if (convTest_.is_null () || newResTest) {
671 convTest_ = rcp (
new StatusTestResNorm_t (convtol_, defQuorum_, showMaxResNormOnly_));
675 if (sTest_.is_null () || newResTest) {
676 sTest_ = rcp (
new StatusTestCombo_t (StatusTestCombo_t::OR, maxIterTest_, convTest_));
679 if (outputTest_.is_null () || newResTest) {
683 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
687 const std::string solverDesc =
" Pseudo Block CG ";
688 outputTest_->setSolverDesc (solverDesc);
692 if (timerSolve_.is_null ()) {
693 const std::string solveLabel =
694 label_ +
": PseudoBlockCGSolMgr total solve time";
695 #ifdef BELOS_TEUCHOS_TIME_MONITOR 696 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
705 template<
class ScalarType,
class MV,
class OP>
706 Teuchos::RCP<const Teuchos::ParameterList>
709 using Teuchos::ParameterList;
710 using Teuchos::parameterList;
713 if (validParams_.is_null()) {
715 RCP<ParameterList> pl = parameterList ();
716 pl->set(
"Convergence Tolerance", convtol_default_,
717 "The relative residual tolerance that needs to be achieved by the\n" 718 "iterative solver in order for the linera system to be declared converged.");
719 pl->set(
"Maximum Iterations", maxIters_default_,
720 "The maximum number of block iterations allowed for each\n" 721 "set of RHS solved.");
722 pl->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_default_,
723 "Whether or not to assert that the linear operator\n" 724 "and the preconditioner are indeed positive definite.");
725 pl->set(
"Verbosity", verbosity_default_,
726 "What type(s) of solver information should be outputted\n" 727 "to the output stream.");
728 pl->set(
"Output Style", outputStyle_default_,
729 "What style is used for the solver information outputted\n" 730 "to the output stream.");
731 pl->set(
"Output Frequency", outputFreq_default_,
732 "How often convergence information should be outputted\n" 733 "to the output stream.");
734 pl->set(
"Deflation Quorum", defQuorum_default_,
735 "The number of linear systems that need to converge before\n" 736 "they are deflated. This number should be <= block size.");
737 pl->set(
"Output Stream", outputStream_default_,
738 "A reference-counted pointer to the output stream where all\n" 739 "solver output is sent.");
740 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
741 "When convergence information is printed, only show the maximum\n" 742 "relative residual norm when the block size is greater than one.");
743 pl->set(
"Implicit Residual Scaling", resScale_default_,
744 "The type of scaling used in the residual convergence test.");
745 pl->set(
"Estimate Condition Number", genCondEst_default_,
746 "Whether or not to estimate the condition number of the preconditioned system.");
752 pl->set(
"Residual Scaling", resScale_default_,
753 "The type of scaling used in the residual convergence test. This " 754 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
755 pl->set(
"Timer Label", label_default_,
756 "The string to use as a prefix for the timer labels.");
764 template<
class ScalarType,
class MV,
class OP>
767 const char prefix[] =
"Belos::PseudoBlockCGSolMgr::solve: ";
772 if (!isSet_) { setParameters( params_ ); }
774 Teuchos::BLAS<int,ScalarType> blas;
776 TEUCHOS_TEST_FOR_EXCEPTION
778 prefix <<
"The linear problem to solve is not ready. You must call " 779 "setProblem() on the Belos::LinearProblem instance before telling the " 780 "Belos solver to solve it.");
784 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
785 int numCurrRHS = numRHS2Solve;
787 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
788 for (
int i=0; i<numRHS2Solve; ++i) {
789 currIdx[i] = startPtr+i;
794 problem_->setLSIndex( currIdx );
798 Teuchos::ParameterList plist;
800 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
801 if(genCondEst_) plist.set(
"Max Size For Condest",maxIters_);
804 outputTest_->reset();
807 bool isConverged =
true;
812 Teuchos::RCP<PseudoBlockCGIter<ScalarType,MV,OP> > block_cg_iter
817 #ifdef BELOS_TEUCHOS_TIME_MONITOR 818 Teuchos::TimeMonitor slvtimer(*timerSolve_);
821 bool first_time=
true;
822 while ( numRHS2Solve > 0 ) {
823 if(genCondEst_ && first_time) block_cg_iter->setDoCondEst(
true);
824 else block_cg_iter->setDoCondEst(
false);
827 std::vector<int> convRHSIdx;
828 std::vector<int> currRHSIdx( currIdx );
829 currRHSIdx.resize(numCurrRHS);
832 block_cg_iter->resetNumIters();
835 outputTest_->resetNumCalls();
838 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
843 block_cg_iter->initializeCG(newState);
850 block_cg_iter->iterate();
857 if ( convTest_->getStatus() ==
Passed ) {
864 if (convIdx.size() == currRHSIdx.size())
868 problem_->setCurrLS();
872 std::vector<int> unconvIdx(currRHSIdx.size());
873 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
875 for (
unsigned int j=0; j<convIdx.size(); ++j) {
876 if (currRHSIdx[i] == convIdx[j]) {
882 currIdx2[have] = currIdx2[i];
883 currRHSIdx[have++] = currRHSIdx[i];
886 currRHSIdx.resize(have);
887 currIdx2.resize(have);
890 problem_->setLSIndex( currRHSIdx );
893 std::vector<MagnitudeType> norms;
894 R_0 = MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
895 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
900 block_cg_iter->initializeCG(defstate);
908 else if ( maxIterTest_->getStatus() ==
Passed ) {
922 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
923 "Belos::PseudoBlockCGSolMgr::solve(): Invalid return from PseudoBlockCGIter::iterate().");
926 catch (
const std::exception &e) {
927 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockCGIter::iterate() at iteration " 928 << block_cg_iter->getNumIters() << std::endl
929 << e.what() << std::endl;
935 problem_->setCurrLS();
938 startPtr += numCurrRHS;
939 numRHS2Solve -= numCurrRHS;
941 if ( numRHS2Solve > 0 ) {
943 numCurrRHS = numRHS2Solve;
944 currIdx.resize( numCurrRHS );
945 currIdx2.resize( numCurrRHS );
946 for (
int i=0; i<numCurrRHS; ++i)
947 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
950 problem_->setLSIndex( currIdx );
953 currIdx.resize( numRHS2Solve );
965 #ifdef BELOS_TEUCHOS_TIME_MONITOR 970 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
974 numIters_ = maxIterTest_->getNumIters();
978 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
979 if (pTestValues != NULL && pTestValues->size () > 0) {
980 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
985 ScalarType l_min, l_max;
986 Teuchos::ArrayView<MagnitudeType> diag = block_cg_iter->getDiag();
987 Teuchos::ArrayView<MagnitudeType> offdiag = block_cg_iter->getOffDiag();
988 compute_condnum_tridiag_sym(diag,offdiag,l_min,l_max,condEstimate_);
998 template<
class ScalarType,
class MV,
class OP>
1001 std::ostringstream oss;
1002 oss <<
"Belos::PseudoBlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
1009 template<
class ScalarType,
class MV,
class OP>
1013 Teuchos::ArrayView<MagnitudeType> offdiag,
1014 ScalarType & lambda_min,
1015 ScalarType & lambda_max,
1016 ScalarType & ConditionNumber )
1018 typedef Teuchos::ScalarTraits<ScalarType> STS;
1027 ScalarType scalar_dummy;
1028 MagnitudeType mag_dummy;
1030 Teuchos::LAPACK<int,ScalarType> lapack;
1031 const int N = diag.size ();
1033 lambda_min = STS::one ();
1034 lambda_max = STS::one ();
1036 lapack.STEQR (char_N, N, diag.getRawPtr (), offdiag.getRawPtr (),
1037 &scalar_dummy, 1, &mag_dummy, &info);
1038 TEUCHOS_TEST_FOR_EXCEPTION
1039 (info < 0, std::logic_error,
"Belos::PseudoBlockCGSolMgr::" 1040 "compute_condnum_tridiag_sym: LAPACK's _STEQR failed with info = " 1041 << info <<
" < 0. This suggests there might be a bug in the way Belos " 1042 "is calling LAPACK. Please report this to the Belos developers.");
1043 lambda_min = Teuchos::as<ScalarType> (diag[0]);
1044 lambda_max = Teuchos::as<ScalarType> (diag[N-1]);
1051 if (STS::real (lambda_max) < STS::real (lambda_min)) {
1052 ConditionNumber = STS::one ();
1056 ConditionNumber = lambda_max / lambda_min;
void reset(const ResetType type)
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Teuchos::RCP< const MV > R
The current residual.
Collection of types and exceptions used within the Belos solvers.
ScalarType getConditionEstimate() const
Gets the estimated condition number.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
virtual ~PseudoBlockCGSolMgr()
Destructor.
Class which manages the output and verbosity of the Belos solvers.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
ScaleType
The type of scaling to use on the residual norm value.
PseudoBlockCGSolMgr(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< Teuchos::ParameterList > &pl)
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > getResidualStatusTest() const
A factory class for generating StatusTestOutput objects.
PseudoBlockCGSolMgrLinearProblemFailure 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...
An implementation of StatusTestResNorm using a family of residual norms.
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.
The Belos::PseudoBlockCGSolMgr provides a powerful and fully-featured solver manager over the pseudo-...
Belos::StatusTest for logically combining several status tests.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
This class implements the pseudo-block CG iteration, where the basic CG algorithm is performed on all...
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. ...
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
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.
PseudoBlockCGSolMgrOrthoFailure(const std::string &what_arg)
Type traits class that says whether Teuchos::LAPACK has a valid implementation for the given ScalarTy...
ReturnType
Whether the Belos solve converged for all linear systems.
Teuchos::RCP< StatusTestGenResNorm< ScalarType, MV, OP > > getResidualStatusTest() const
Return the residual status test.
int setTolerance(MagnitudeType tolerance)
Set the value of the tolerance.
virtual ~PseudoBlockCGSolMgr()
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.
PseudoBlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
A class for extending the status testing capabilities of Belos via logical combinations.
PseudoBlockCGSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to generate or...
Class which defines basic traits for the operator type.
Belos concrete class for performing the pseudo-block CG iteration.
Parent class to all Belos exceptions.
Base class for Belos::SolverManager subclasses which normally can only compile with ScalarType types ...
Belos header file which uses auto-configuration information to include necessary C++ headers...