42 #ifndef BELOS_BLOCK_CG_SOLMGR_HPP
43 #define BELOS_BLOCK_CG_SOLMGR_HPP
67 #ifdef BELOS_TEUCHOS_TIME_MONITOR
113 template<
class ScalarType,
class MV,
class OP>
191 return Teuchos::tuple(timerSolve_);
300 static const MagnitudeType convtol_default_;
301 static const MagnitudeType orthoKappa_default_;
302 static const int maxIters_default_;
303 static const bool adaptiveBlockSize_default_;
304 static const bool showMaxResNormOnly_default_;
305 static const int blockSize_default_;
306 static const int verbosity_default_;
307 static const int outputStyle_default_;
308 static const int outputFreq_default_;
309 static const std::string label_default_;
310 static const std::string orthoType_default_;
318 MagnitudeType convtol_;
321 MagnitudeType orthoKappa_;
328 MagnitudeType achievedTol_;
336 int blockSize_, verbosity_, outputStyle_, outputFreq_;
337 bool adaptiveBlockSize_, showMaxResNormOnly_;
338 std::string orthoType_;
352 template<
class ScalarType,
class MV,
class OP>
353 const typename BlockCGSolMgr<ScalarType,MV,OP>::MagnitudeType BlockCGSolMgr<ScalarType,MV,OP>::convtol_default_ = 1e-8;
355 template<
class ScalarType,
class MV,
class OP>
356 const typename BlockCGSolMgr<ScalarType,MV,OP>::MagnitudeType BlockCGSolMgr<ScalarType,MV,OP>::orthoKappa_default_ = -1.0;
358 template<
class ScalarType,
class MV,
class OP>
359 const int BlockCGSolMgr<ScalarType,MV,OP>::maxIters_default_ = 1000;
361 template<
class ScalarType,
class MV,
class OP>
362 const bool BlockCGSolMgr<ScalarType,MV,OP>::adaptiveBlockSize_default_ =
true;
364 template<
class ScalarType,
class MV,
class OP>
365 const bool BlockCGSolMgr<ScalarType,MV,OP>::showMaxResNormOnly_default_ =
false;
367 template<
class ScalarType,
class MV,
class OP>
368 const int BlockCGSolMgr<ScalarType,MV,OP>::blockSize_default_ = 1;
370 template<
class ScalarType,
class MV,
class OP>
371 const int BlockCGSolMgr<ScalarType,MV,OP>::verbosity_default_ =
Belos::Errors;
373 template<
class ScalarType,
class MV,
class OP>
374 const int BlockCGSolMgr<ScalarType,MV,OP>::outputStyle_default_ =
Belos::General;
376 template<
class ScalarType,
class MV,
class OP>
377 const int BlockCGSolMgr<ScalarType,MV,OP>::outputFreq_default_ = -1;
379 template<
class ScalarType,
class MV,
class OP>
380 const std::string BlockCGSolMgr<ScalarType,MV,OP>::label_default_ =
"Belos";
382 template<
class ScalarType,
class MV,
class OP>
383 const std::string BlockCGSolMgr<ScalarType,MV,OP>::orthoType_default_ =
"DGKS";
385 template<
class ScalarType,
class MV,
class OP>
390 template<
class ScalarType,
class MV,
class OP>
392 outputStream_(outputStream_default_),
393 convtol_(convtol_default_),
394 orthoKappa_(orthoKappa_default_),
395 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
396 maxIters_(maxIters_default_),
398 blockSize_(blockSize_default_),
399 verbosity_(verbosity_default_),
400 outputStyle_(outputStyle_default_),
401 outputFreq_(outputFreq_default_),
402 adaptiveBlockSize_(adaptiveBlockSize_default_),
403 showMaxResNormOnly_(showMaxResNormOnly_default_),
404 orthoType_(orthoType_default_),
405 label_(label_default_),
411 template<
class ScalarType,
class MV,
class OP>
416 outputStream_(outputStream_default_),
417 convtol_(convtol_default_),
418 orthoKappa_(orthoKappa_default_),
419 achievedTol_(Teuchos::ScalarTraits<MagnitudeType>::zero()),
420 maxIters_(maxIters_default_),
422 blockSize_(blockSize_default_),
423 verbosity_(verbosity_default_),
424 outputStyle_(outputStyle_default_),
425 outputFreq_(outputFreq_default_),
426 adaptiveBlockSize_(adaptiveBlockSize_default_),
427 showMaxResNormOnly_(showMaxResNormOnly_default_),
428 orthoType_(orthoType_default_),
429 label_(label_default_),
433 "BlockCGSolMgr's constructor requires a nonnull LinearProblem instance.");
443 template<
class ScalarType,
class MV,
class OP>
449 if (params_ == Teuchos::null) {
458 maxIters_ = params->
get(
"Maximum Iterations",maxIters_default_);
461 params_->set(
"Maximum Iterations", maxIters_);
462 if (maxIterTest_!=Teuchos::null)
463 maxIterTest_->setMaxIters( maxIters_ );
468 blockSize_ = params->
get(
"Block Size",blockSize_default_);
470 "Belos::BlockCGSolMgr: \"Block Size\" must be strictly positive.");
473 params_->set(
"Block Size", blockSize_);
478 adaptiveBlockSize_ = params->
get(
"Adaptive Block Size",adaptiveBlockSize_default_);
481 params_->set(
"Adaptive Block Size", adaptiveBlockSize_);
486 std::string tempLabel = params->
get(
"Timer Label", label_default_);
489 if (tempLabel != label_) {
491 params_->set(
"Timer Label", label_);
492 std::string solveLabel = label_ +
": BlockCGSolMgr total solve time";
493 #ifdef BELOS_TEUCHOS_TIME_MONITOR
496 if (ortho_ != Teuchos::null) {
497 ortho_->setLabel( label_ );
504 std::string tempOrthoType = params->
get(
"Orthogonalization",orthoType_default_);
506 std::invalid_argument,
507 "Belos::BlockCGSolMgr: \"Orthogonalization\" must be either \"DGKS\", \"ICGS\", or \"IMGS\".");
508 if (tempOrthoType != orthoType_) {
509 orthoType_ = tempOrthoType;
511 if (orthoType_==
"DGKS") {
512 if (orthoKappa_ <= 0) {
520 else if (orthoType_==
"ICGS") {
523 else if (orthoType_==
"IMGS") {
530 if (params->
isParameter(
"Orthogonalization Constant")) {
531 orthoKappa_ = params->
get(
"Orthogonalization Constant",orthoKappa_default_);
534 params_->set(
"Orthogonalization Constant",orthoKappa_);
535 if (orthoType_==
"DGKS") {
536 if (orthoKappa_ > 0 && ortho_ != Teuchos::null) {
544 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
545 verbosity_ = params->
get(
"Verbosity", verbosity_default_);
547 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
551 params_->set(
"Verbosity", verbosity_);
552 if (printer_ != Teuchos::null)
553 printer_->setVerbosity(verbosity_);
558 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
559 outputStyle_ = params->
get(
"Output Style", outputStyle_default_);
561 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
565 params_->set(
"Output Style", outputStyle_);
566 outputTest_ = Teuchos::null;
571 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
574 params_->set(
"Output Stream", outputStream_);
575 if (printer_ != Teuchos::null)
576 printer_->setOStream( outputStream_ );
582 outputFreq_ = params->
get(
"Output Frequency", outputFreq_default_);
586 params_->set(
"Output Frequency", outputFreq_);
587 if (outputTest_ != Teuchos::null)
588 outputTest_->setOutputFrequency( outputFreq_ );
592 if (printer_ == Teuchos::null) {
601 if (params->
isParameter(
"Convergence Tolerance")) {
602 convtol_ = params->
get(
"Convergence Tolerance",convtol_default_);
605 params_->set(
"Convergence Tolerance", convtol_);
606 if (convTest_ != Teuchos::null)
607 convTest_->setTolerance( convtol_ );
610 if (params->
isParameter(
"Show Maximum Residual Norm Only")) {
611 showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,
"Show Maximum Residual Norm Only");
614 params_->set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_);
615 if (convTest_ != Teuchos::null)
616 convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
622 if (maxIterTest_ == Teuchos::null)
626 if (convTest_ == Teuchos::null)
627 convTest_ =
Teuchos::rcp(
new StatusTestResNorm_t( convtol_, 1 ) );
629 if (sTest_ == Teuchos::null)
630 sTest_ =
Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
632 if (outputTest_ == Teuchos::null) {
640 std::string solverDesc =
" Block CG ";
641 outputTest_->setSolverDesc( solverDesc );
646 if (ortho_ == Teuchos::null) {
647 if (orthoType_==
"DGKS") {
648 if (orthoKappa_ <= 0) {
656 else if (orthoType_==
"ICGS") {
659 else if (orthoType_==
"IMGS") {
664 "Belos::BlockCGSolMgr(): Invalid orthogonalization type.");
669 if (timerSolve_ == Teuchos::null) {
670 std::string solveLabel = label_ +
": BlockCGSolMgr total solve time";
671 #ifdef BELOS_TEUCHOS_TIME_MONITOR
681 template<
class ScalarType,
class MV,
class OP>
690 pl->
set(
"Convergence Tolerance", convtol_default_,
691 "The relative residual tolerance that needs to be achieved by the\n"
692 "iterative solver in order for the linear system to be declared converged.");
693 pl->
set(
"Maximum Iterations", maxIters_default_,
694 "The maximum number of block iterations allowed for each\n"
695 "set of RHS solved.");
696 pl->
set(
"Block Size", blockSize_default_,
697 "The number of vectors in each block.");
698 pl->
set(
"Adaptive Block Size", adaptiveBlockSize_default_,
699 "Whether the solver manager should adapt to the block size\n"
700 "based on the number of RHS to solve.");
701 pl->
set(
"Verbosity", verbosity_default_,
702 "What type(s) of solver information should be outputted\n"
703 "to the output stream.");
704 pl->
set(
"Output Style", outputStyle_default_,
705 "What style is used for the solver information outputted\n"
706 "to the output stream.");
707 pl->
set(
"Output Frequency", outputFreq_default_,
708 "How often convergence information should be outputted\n"
709 "to the output stream.");
710 pl->
set(
"Output Stream", outputStream_default_,
711 "A reference-counted pointer to the output stream where all\n"
712 "solver output is sent.");
713 pl->
set(
"Show Maximum Residual Norm Only", showMaxResNormOnly_default_,
714 "When convergence information is printed, only show the maximum\n"
715 "relative residual norm when the block size is greater than one.");
716 pl->
set(
"Timer Label", label_default_,
717 "The string to use as a prefix for the timer labels.");
719 pl->
set(
"Orthogonalization", orthoType_default_,
720 "The type of orthogonalization to use: DGKS, ICGS, or IMGS.");
721 pl->
set(
"Orthogonalization Constant",orthoKappa_default_,
722 "The constant used by DGKS orthogonalization to determine\n"
723 "whether another step of classical Gram-Schmidt is necessary.");
731 template<
class ScalarType,
class MV,
class OP>
735 using Teuchos::rcp_const_cast;
736 using Teuchos::rcp_dynamic_cast;
743 setParameters(Teuchos::parameterList(*getValidParameters()));
751 "Belos::BlockCGSolMgr::solve(): Linear problem is not ready, setProblem() "
752 "has not been called.");
756 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
757 int numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
759 std::vector<int> currIdx, currIdx2;
764 if ( adaptiveBlockSize_ ) {
765 blockSize_ = numCurrRHS;
766 currIdx.resize( numCurrRHS );
767 currIdx2.resize( numCurrRHS );
768 for (
int i=0; i<numCurrRHS; ++i)
769 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
773 currIdx.resize( blockSize_ );
774 currIdx2.resize( blockSize_ );
775 for (
int i=0; i<numCurrRHS; ++i)
776 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
777 for (
int i=numCurrRHS; i<blockSize_; ++i)
778 { currIdx[i] = -1; currIdx2[i] = i; }
782 problem_->setLSIndex( currIdx );
787 plist.
set(
"Block Size",blockSize_);
790 outputTest_->reset();
794 bool isConverged =
true;
799 RCP<CGIteration<ScalarType,MV,OP> > block_cg_iter;
800 if (blockSize_ == 1) {
805 outputTest_, plist));
815 #ifdef BELOS_TEUCHOS_TIME_MONITOR
819 while ( numRHS2Solve > 0 ) {
822 std::vector<int> convRHSIdx;
823 std::vector<int> currRHSIdx( currIdx );
824 currRHSIdx.resize(numCurrRHS);
827 block_cg_iter->resetNumIters();
830 outputTest_->resetNumCalls();
833 RCP<MV> R_0 = MVT::CloneViewNonConst( *(rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
838 block_cg_iter->initializeCG(newstate);
844 block_cg_iter->iterate();
848 if (convTest_->getStatus() ==
Passed) {
853 std::vector<int> convIdx =
854 rcp_dynamic_cast<conv_test_type>(convTest_)->convIndices();
859 if (convIdx.size() == currRHSIdx.size())
864 problem_->setCurrLS();
869 std::vector<int> unconvIdx(currRHSIdx.size());
870 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
872 for (
unsigned int j=0; j<convIdx.size(); ++j) {
873 if (currRHSIdx[i] == convIdx[j]) {
879 currIdx2[have] = currIdx2[i];
880 currRHSIdx[have++] = currRHSIdx[i];
885 currRHSIdx.resize(have);
886 currIdx2.resize(have);
889 problem_->setLSIndex( currRHSIdx );
892 std::vector<MagnitudeType> norms;
893 R_0 = MVT::CloneCopy( *(block_cg_iter->getNativeResiduals(&norms)),currIdx2 );
894 for (
int i=0; i<have; ++i) { currIdx2[i] = i; }
897 block_cg_iter->setBlockSize( have );
902 block_cg_iter->initializeCG(defstate);
908 else if (maxIterTest_->getStatus() ==
Passed) {
918 "Belos::BlockCGSolMgr::solve(): Neither the convergence test nor "
919 "the maximum iteration count test passed. Please report this bug "
920 "to the Belos developers.");
923 catch (
const std::exception &e) {
924 std::ostream& err = printer_->stream (
Errors);
925 err <<
"Error! Caught std::exception in CGIteration::iterate() at "
926 <<
"iteration " << block_cg_iter->getNumIters() << std::endl
927 << e.what() << std::endl;
934 problem_->setCurrLS();
937 startPtr += numCurrRHS;
938 numRHS2Solve -= numCurrRHS;
939 if ( numRHS2Solve > 0 ) {
940 numCurrRHS = ( numRHS2Solve < blockSize_) ? numRHS2Solve : blockSize_;
942 if ( adaptiveBlockSize_ ) {
943 blockSize_ = numCurrRHS;
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 currIdx.resize( blockSize_ );
951 currIdx2.resize( blockSize_ );
952 for (
int i=0; i<numCurrRHS; ++i)
953 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
954 for (
int i=numCurrRHS; i<blockSize_; ++i)
955 { currIdx[i] = -1; currIdx2[i] = i; }
958 problem_->setLSIndex( currIdx );
961 block_cg_iter->setBlockSize( blockSize_ );
964 currIdx.resize( numRHS2Solve );
975 #ifdef BELOS_TEUCHOS_TIME_MONITOR
986 numIters_ = maxIterTest_->getNumIters();
992 const std::vector<MagnitudeType>* pTestValues =
993 rcp_dynamic_cast<conv_test_type>(convTest_)->getTestValue();
996 "Belos::BlockCGSolMgr::solve(): The convergence test's getTestValue() "
997 "method returned NULL. Please report this bug to the Belos developers.");
1000 "Belos::BlockCGSolMgr::solve(): The convergence test's getTestValue() "
1001 "method returned a vector of length zero. Please report this bug to the "
1002 "Belos developers.");
1007 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
1017 template<
class ScalarType,
class MV,
class OP>
1020 std::ostringstream oss;
1023 oss <<
"Ortho Type='"<<orthoType_<<
"\', Block Size=" << blockSize_;
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...
virtual ~BlockCGSolMgr()
Destructor.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the parameters the solver manager should use to solve the linear problem.
Class which manages the output and verbosity of the Belos solvers.
int getNumIters() const
Get the iteration count for the most recent call to solve().
bool is_null(const boost::shared_ptr< T > &p)
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters for this object.
This class implements the preconditioned Conjugate Gradient (CG) iteration.
Structure to contain pointers to CGIteration state variables.
T & get(const std::string &name, T def_value)
Belos concrete class for performing the conjugate-gradient (CG) iteration.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
bool isLOADetected() const
Return whether a loss of accuracy was detected by this solver during the most current solve...
Belos concrete class for performing the block conjugate-gradient (CG) iteration.
An implementation of StatusTestResNorm using a family of residual norms.
The Belos::BlockCGSolMgr provides a powerful and fully-featured solver manager over the CG and BlockC...
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
BlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
BlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i.e.
A factory class for generating StatusTestOutput objects.
Iterated Modified Gram-Schmidt (IMGS) implementation of the Belos::OrthoManager class.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
Traits class which defines basic operations on multivectors.
std::string description() const
Method to return description of the block CG solver manager.
Belos::StatusTest for logically combining several status tests.
bool isParameter(const std::string &name) const
Classical Gram-Schmidt (with DGKS correction) implementation of the Belos::OrthoManager class...
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
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.
Class which describes the linear problem to be solved by the iterative solver.
BlockCGSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to generate orthonor...
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 ...
void reset(const ResetType type)
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Iterated Classical Gram-Schmidt (ICGS) implementation of the Belos::OrthoManager class.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
An implementation of the Belos::MatOrthoManager that performs orthogonalization using multiple steps ...
BlockCGSolMgrOrthoFailure(const std::string &what_arg)
An implementation of the Belos::MatOrthoManager that performs orthogonalization using multiple steps ...
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.
ReturnType solve()
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
An implementation of the Belos::MatOrthoManager that performs orthogonalization using (potentially) m...
A class for extending the status testing capabilities of Belos via logical combinations.
BlockCGSolMgr()
Empty constructor for BlockCGSolMgr. This constructor takes no arguments and sets the default values ...
Class which defines basic traits for the operator type.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
Parent class to all Belos exceptions.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
Belos header file which uses auto-configuration information to include necessary C++ headers...
This class implements the block, preconditioned Conjugate Gradient (CG) iteration.