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) :
135 template<
class ScalarType,
class MV,
class OP>
142 typedef Teuchos::ScalarTraits<ScalarType> SCT;
143 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
144 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
168 const Teuchos::RCP<Teuchos::ParameterList> &pl );
183 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const;
194 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
195 return Teuchos::tuple(timerSolve_);
237 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
278 std::string description()
const;
283 void compute_condnum_tridiag_sym(Teuchos::ArrayView<MagnitudeType> diag,
284 Teuchos::ArrayView<MagnitudeType> offdiag,
285 ScalarType & lambda_min,
286 ScalarType & lambda_max,
287 ScalarType & ConditionNumber );
290 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
293 Teuchos::RCP<OutputManager<ScalarType> > printer_;
294 Teuchos::RCP<std::ostream> outputStream_;
297 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
298 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
299 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
300 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
303 Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > ortho_;
306 Teuchos::RCP<Teuchos::ParameterList> params_;
313 mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
316 static const MagnitudeType convtol_default_;
317 static const int maxIters_default_;
318 static const bool assertPositiveDefiniteness_default_;
319 static const bool showMaxResNormOnly_default_;
320 static const int verbosity_default_;
321 static const int outputStyle_default_;
322 static const int outputFreq_default_;
323 static const int defQuorum_default_;
324 static const std::string resScale_default_;
325 static const std::string label_default_;
326 static const Teuchos::RCP<std::ostream> outputStream_default_;
327 static const bool genCondEst_default_;
330 MagnitudeType convtol_,achievedTol_;
331 int maxIters_, numIters_;
332 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
333 bool assertPositiveDefiniteness_, showMaxResNormOnly_;
334 std::string resScale_;
336 ScalarType condEstimate_;
340 Teuchos::RCP<Teuchos::Time> timerSolve_;
348 template<
class ScalarType,
class MV,
class OP>
351 template<
class ScalarType,
class MV,
class OP>
354 template<
class ScalarType,
class MV,
class OP>
357 template<
class ScalarType,
class MV,
class OP>
360 template<
class ScalarType,
class MV,
class OP>
363 template<
class ScalarType,
class MV,
class OP>
366 template<
class ScalarType,
class MV,
class OP>
369 template<
class ScalarType,
class MV,
class OP>
372 template<
class ScalarType,
class MV,
class OP>
375 template<
class ScalarType,
class MV,
class OP>
378 template<
class ScalarType,
class MV,
class OP>
381 template<
class ScalarType,
class MV,
class OP>
385 template<
class ScalarType,
class MV,
class OP>
387 outputStream_(outputStream_default_),
388 convtol_(convtol_default_),
389 maxIters_(maxIters_default_),
391 verbosity_(verbosity_default_),
392 outputStyle_(outputStyle_default_),
393 outputFreq_(outputFreq_default_),
394 defQuorum_(defQuorum_default_),
395 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
396 showMaxResNormOnly_(showMaxResNormOnly_default_),
397 resScale_(resScale_default_),
398 genCondEst_(genCondEst_default_),
399 condEstimate_(-Teuchos::ScalarTraits<ScalarType>::one()),
400 label_(label_default_),
405 template<
class ScalarType,
class MV,
class OP>
408 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
410 outputStream_(outputStream_default_),
411 convtol_(convtol_default_),
412 maxIters_(maxIters_default_),
414 verbosity_(verbosity_default_),
415 outputStyle_(outputStyle_default_),
416 outputFreq_(outputFreq_default_),
417 defQuorum_(defQuorum_default_),
418 assertPositiveDefiniteness_(assertPositiveDefiniteness_default_),
419 showMaxResNormOnly_(showMaxResNormOnly_default_),
420 resScale_(resScale_default_),
421 genCondEst_(genCondEst_default_),
422 condEstimate_(-Teuchos::ScalarTraits<ScalarType>::one()),
423 label_(label_default_),
426 TEUCHOS_TEST_FOR_EXCEPTION(
427 problem_.is_null (), std::invalid_argument,
428 "Belos::PseudoBlockCGSolMgr two-argument constructor: " 429 "'problem' is null. You must supply a non-null Belos::LinearProblem " 430 "instance when calling this constructor.");
432 if (! pl.is_null ()) {
438 template<
class ScalarType,
class MV,
class OP>
443 using Teuchos::ParameterList;
444 using Teuchos::parameterList;
448 RCP<const ParameterList> defaultParams = this->getValidParameters ();
467 if (params_.is_null ()) {
469 params_ = parameterList (defaultParams->name ());
471 params->validateParameters (*defaultParams);
475 if (params->isParameter (
"Maximum Iterations")) {
476 maxIters_ = params->get (
"Maximum Iterations", maxIters_default_);
479 params_->set (
"Maximum Iterations", maxIters_);
480 if (! maxIterTest_.is_null ()) {
481 maxIterTest_->setMaxIters (maxIters_);
486 if (params->isParameter (
"Assert Positive Definiteness")) {
487 assertPositiveDefiniteness_ =
488 params->get (
"Assert Positive Definiteness",
489 assertPositiveDefiniteness_default_);
492 params_->set (
"Assert Positive Definiteness", assertPositiveDefiniteness_);
496 if (params->isParameter (
"Timer Label")) {
497 const std::string tempLabel = params->get (
"Timer Label", label_default_);
500 if (tempLabel != label_) {
502 params_->set (
"Timer Label", label_);
503 const std::string solveLabel =
504 label_ +
": PseudoBlockCGSolMgr total solve time";
505 #ifdef BELOS_TEUCHOS_TIME_MONITOR 506 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
508 if (! ortho_.is_null ()) {
509 ortho_->setLabel (label_);
515 if (params->isParameter (
"Verbosity")) {
516 if (Teuchos::isParameterType<int> (*params,
"Verbosity")) {
517 verbosity_ = params->get (
"Verbosity", verbosity_default_);
519 verbosity_ = (int) Teuchos::getParameter<Belos::MsgType> (*params,
"Verbosity");
523 params_->set (
"Verbosity", verbosity_);
524 if (! printer_.is_null ()) {
525 printer_->setVerbosity (verbosity_);
530 if (params->isParameter (
"Output Style")) {
531 if (Teuchos::isParameterType<int> (*params,
"Output Style")) {
532 outputStyle_ = params->get (
"Output Style", outputStyle_default_);
535 outputStyle_ = (int) Teuchos::getParameter<Belos::OutputType> (*params,
"Output Style");
540 params_->set (
"Output Style", outputStyle_);
541 outputTest_ = Teuchos::null;
545 if (params->isParameter (
"Output Stream")) {
546 outputStream_ = params->get<RCP<std::ostream> > (
"Output Stream");
549 params_->set (
"Output Stream", outputStream_);
550 if (! printer_.is_null ()) {
551 printer_->setOStream (outputStream_);
557 if (params->isParameter (
"Output Frequency")) {
558 outputFreq_ = params->get (
"Output Frequency", outputFreq_default_);
562 params_->set (
"Output Frequency", outputFreq_);
563 if (! outputTest_.is_null ()) {
564 outputTest_->setOutputFrequency (outputFreq_);
569 if (params->isParameter (
"Estimate Condition Number")) {
570 genCondEst_ = params->get (
"Estimate Condition Number", genCondEst_default_);
574 if (printer_.is_null ()) {
583 if (params->isParameter (
"Convergence Tolerance")) {
584 convtol_ = params->get (
"Convergence Tolerance", convtol_default_);
587 params_->set (
"Convergence Tolerance", convtol_);
588 if (! convTest_.is_null ()) {
593 if (params->isParameter (
"Show Maximum Residual Norm Only")) {
594 showMaxResNormOnly_ = params->get<
bool> (
"Show Maximum Residual Norm Only");
597 params_->set (
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
598 if (! convTest_.is_null ()) {
599 convTest_->setShowMaxResNormOnly (showMaxResNormOnly_);
604 bool newResTest =
false;
609 std::string tempResScale = resScale_;
610 bool implicitResidualScalingName =
false;
611 if (params->isParameter (
"Residual Scaling")) {
612 tempResScale = params->get<std::string> (
"Residual Scaling");
614 else if (params->isParameter (
"Implicit Residual Scaling")) {
615 tempResScale = params->get<std::string> (
"Implicit Residual Scaling");
616 implicitResidualScalingName =
true;
620 if (resScale_ != tempResScale) {
623 resScale_ = tempResScale;
627 if (implicitResidualScalingName) {
628 params_->set (
"Implicit Residual Scaling", resScale_);
631 params_->set (
"Residual Scaling", resScale_);
634 if (! convTest_.is_null ()) {
638 catch (std::exception& e) {
647 if (params->isParameter (
"Deflation Quorum")) {
648 defQuorum_ = params->get (
"Deflation Quorum", defQuorum_);
649 params_->set (
"Deflation Quorum", defQuorum_);
650 if (! convTest_.is_null ()) {
651 convTest_->setQuorum( defQuorum_ );
658 if (maxIterTest_.is_null ()) {
663 if (convTest_.is_null () || newResTest) {
664 convTest_ = rcp (
new StatusTestResNorm_t (convtol_, defQuorum_, showMaxResNormOnly_));
668 if (sTest_.is_null () || newResTest) {
669 sTest_ = rcp (
new StatusTestCombo_t (StatusTestCombo_t::OR, maxIterTest_, convTest_));
672 if (outputTest_.is_null () || newResTest) {
676 outputTest_ = stoFactory.
create (printer_, sTest_, outputFreq_,
680 const std::string solverDesc =
" Pseudo Block CG ";
681 outputTest_->setSolverDesc (solverDesc);
685 if (timerSolve_.is_null ()) {
686 const std::string solveLabel =
687 label_ +
": PseudoBlockCGSolMgr total solve time";
688 #ifdef BELOS_TEUCHOS_TIME_MONITOR 689 timerSolve_ = Teuchos::TimeMonitor::getNewCounter (solveLabel);
698 template<
class ScalarType,
class MV,
class OP>
699 Teuchos::RCP<const Teuchos::ParameterList>
702 using Teuchos::ParameterList;
703 using Teuchos::parameterList;
706 if (validParams_.is_null()) {
708 RCP<ParameterList> pl = parameterList ();
709 pl->set(
"Convergence Tolerance", convtol_default_,
710 "The relative residual tolerance that needs to be achieved by the\n" 711 "iterative solver in order for the linera system to be declared converged.");
712 pl->set(
"Maximum Iterations", maxIters_default_,
713 "The maximum number of block iterations allowed for each\n" 714 "set of RHS solved.");
715 pl->set(
"Assert Positive Definiteness", assertPositiveDefiniteness_default_,
716 "Whether or not to assert that the linear operator\n" 717 "and the preconditioner are indeed positive definite.");
718 pl->set(
"Verbosity", verbosity_default_,
719 "What type(s) of solver information should be outputted\n" 720 "to the output stream.");
721 pl->set(
"Output Style", outputStyle_default_,
722 "What style is used for the solver information outputted\n" 723 "to the output stream.");
724 pl->set(
"Output Frequency", outputFreq_default_,
725 "How often convergence information should be outputted\n" 726 "to the output stream.");
727 pl->set(
"Deflation Quorum", defQuorum_default_,
728 "The number of linear systems that need to converge before\n" 729 "they are deflated. This number should be <= block size.");
730 pl->set(
"Output Stream", outputStream_default_,
731 "A reference-counted pointer to the output stream where all\n" 732 "solver output is sent.");
733 pl->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
734 "When convergence information is printed, only show the maximum\n" 735 "relative residual norm when the block size is greater than one.");
736 pl->set(
"Implicit Residual Scaling", resScale_default_,
737 "The type of scaling used in the residual convergence test.");
738 pl->set(
"Estimate Condition Number", genCondEst_default_,
739 "Whether or not to estimate the condition number of the preconditioned system.");
745 pl->set(
"Residual Scaling", resScale_default_,
746 "The type of scaling used in the residual convergence test. This " 747 "name is deprecated; the new name is \"Implicit Residual Scaling\".");
748 pl->set(
"Timer Label", label_default_,
749 "The string to use as a prefix for the timer labels.");
758 template<
class ScalarType,
class MV,
class OP>
761 const char prefix[] =
"Belos::PseudoBlockCGSolMgr::solve: ";
766 if (!isSet_) { setParameters( params_ ); }
768 Teuchos::BLAS<int,ScalarType> blas;
770 TEUCHOS_TEST_FOR_EXCEPTION
772 prefix <<
"The linear problem to solve is not ready. You must call " 773 "setProblem() on the Belos::LinearProblem instance before telling the " 774 "Belos solver to solve it.");
778 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
779 int numCurrRHS = numRHS2Solve;
781 std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
782 for (
int i=0; i<numRHS2Solve; ++i) {
783 currIdx[i] = startPtr+i;
788 problem_->setLSIndex( currIdx );
792 Teuchos::ParameterList plist;
794 plist.set(
"Assert Positive Definiteness",assertPositiveDefiniteness_);
795 if(genCondEst_) plist.set(
"Max Size For Condest",maxIters_);
798 outputTest_->reset();
801 bool isConverged =
true;
806 Teuchos::RCP<PseudoBlockCGIter<ScalarType,MV,OP> > block_cg_iter
811 #ifdef BELOS_TEUCHOS_TIME_MONITOR 812 Teuchos::TimeMonitor slvtimer(*timerSolve_);
815 bool first_time=
true;
816 while ( numRHS2Solve > 0 ) {
817 if(genCondEst_ && first_time) block_cg_iter->setDoCondEst(
true);
818 else block_cg_iter->setDoCondEst(
false);
821 std::vector<int> convRHSIdx;
822 std::vector<int> currRHSIdx( currIdx );
823 currRHSIdx.resize(numCurrRHS);
826 block_cg_iter->resetNumIters();
829 outputTest_->resetNumCalls();
832 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
837 block_cg_iter->initializeCG(newState);
844 block_cg_iter->iterate();
851 if ( convTest_->getStatus() ==
Passed ) {
858 if (convIdx.size() == currRHSIdx.size())
862 problem_->setCurrLS();
866 std::vector<int> unconvIdx(currRHSIdx.size());
867 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
869 for (
unsigned int j=0; j<convIdx.size(); ++j) {
870 if (currRHSIdx[i] == convIdx[j]) {
876 currIdx2[have] = currIdx2[i];
877 currRHSIdx[have++] = currRHSIdx[i];
880 currRHSIdx.resize(have);
881 currIdx2.resize(have);
884 problem_->setLSIndex( currRHSIdx );
887 std::vector<MagnitudeType> norms;
888 R_0 = MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
889 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
894 block_cg_iter->initializeCG(defstate);
902 else if ( maxIterTest_->getStatus() ==
Passed ) {
916 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
917 "Belos::PseudoBlockCGSolMgr::solve(): Invalid return from PseudoBlockCGIter::iterate().");
920 catch (
const std::exception &e) {
921 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockCGIter::iterate() at iteration " 922 << block_cg_iter->getNumIters() << std::endl
923 << e.what() << std::endl;
929 problem_->setCurrLS();
932 startPtr += numCurrRHS;
933 numRHS2Solve -= numCurrRHS;
935 if ( numRHS2Solve > 0 ) {
937 numCurrRHS = numRHS2Solve;
938 currIdx.resize( numCurrRHS );
939 currIdx2.resize( numCurrRHS );
940 for (
int i=0; i<numCurrRHS; ++i)
941 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
944 problem_->setLSIndex( currIdx );
947 currIdx.resize( numRHS2Solve );
959 #ifdef BELOS_TEUCHOS_TIME_MONITOR 964 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
968 numIters_ = maxIterTest_->getNumIters();
972 const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
973 if (pTestValues != NULL && pTestValues->size () > 0) {
974 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
979 ScalarType l_min, l_max;
980 Teuchos::ArrayView<MagnitudeType> diag = block_cg_iter->getDiag();
981 Teuchos::ArrayView<MagnitudeType> offdiag = block_cg_iter->getOffDiag();
982 compute_condnum_tridiag_sym(diag,offdiag,l_min,l_max,condEstimate_);
992 template<
class ScalarType,
class MV,
class OP>
995 std::ostringstream oss;
996 oss <<
"Belos::PseudoBlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
1003 template<
class ScalarType,
class MV,
class OP>
1007 Teuchos::ArrayView<MagnitudeType> offdiag,
1008 ScalarType & lambda_min,
1009 ScalarType & lambda_max,
1010 ScalarType & ConditionNumber )
1012 typedef Teuchos::ScalarTraits<ScalarType> STS;
1021 ScalarType scalar_dummy;
1022 MagnitudeType mag_dummy;
1024 Teuchos::LAPACK<int,ScalarType> lapack;
1025 const int N = diag.size ();
1027 lambda_min = STS::one ();
1028 lambda_max = STS::one ();
1030 lapack.STEQR (char_N, N, diag.getRawPtr (), offdiag.getRawPtr (),
1031 &scalar_dummy, 1, &mag_dummy, &info);
1032 TEUCHOS_TEST_FOR_EXCEPTION
1033 (info < 0, std::logic_error,
"Belos::PseudoBlockCGSolMgr::" 1034 "compute_condnum_tridiag_sym: LAPACK's _STEQR failed with info = " 1035 << info <<
" < 0. This suggests there might be a bug in the way Belos " 1036 "is calling LAPACK. Please report this to the Belos developers.");
1037 lambda_min = Teuchos::as<ScalarType> (diag[0]);
1038 lambda_max = Teuchos::as<ScalarType> (diag[N-1]);
1045 if (STS::real (lambda_max) < STS::real (lambda_min)) {
1046 ConditionNumber = STS::one ();
1050 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.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
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< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
A factory class for generating StatusTestOutput objects.
PseudoBlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
An implementation of StatusTestResNorm using a family of residual norms.
ScalarType getConditionEstimate() const
Gets the estimated condition number.
int getNumIters() const
Get the iteration count for the most recent call to solve().
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.
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. ...
A linear system to solve, and its associated information.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
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.
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.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
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...
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...