42 #ifndef BELOS_BLOCK_CG_SOLMGR_HPP 43 #define BELOS_BLOCK_CG_SOLMGR_HPP 65 #include "Teuchos_BLAS.hpp" 66 #include "Teuchos_LAPACK.hpp" 67 #ifdef BELOS_TEUCHOS_TIME_MONITOR 68 # include "Teuchos_TimeMonitor.hpp" 113 template<
class ScalarType,
class MV,
class OP>
119 typedef Teuchos::ScalarTraits<ScalarType> SCT;
120 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
121 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
164 const Teuchos::RCP<Teuchos::ParameterList> &pl );
179 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters()
const;
190 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
191 return Teuchos::tuple(timerSolve_);
221 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms );
262 std::string description()
const;
269 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
272 Teuchos::RCP<OutputManager<ScalarType> > printer_;
274 Teuchos::RCP<std::ostream> outputStream_;
280 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
283 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
286 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > convTest_;
289 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
292 Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > ortho_;
295 Teuchos::RCP<Teuchos::ParameterList> params_;
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_;
311 static const Teuchos::RCP<std::ostream> outputStream_default_;
318 MagnitudeType convtol_;
321 MagnitudeType orthoKappa_;
328 MagnitudeType achievedTol_;
336 int blockSize_, verbosity_, outputStyle_, outputFreq_;
337 bool adaptiveBlockSize_, showMaxResNormOnly_;
338 std::string orthoType_;
344 Teuchos::RCP<Teuchos::Time> timerSolve_;
352 template<
class ScalarType,
class MV,
class OP>
355 template<
class ScalarType,
class MV,
class OP>
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>
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>
414 const Teuchos::RCP<Teuchos::ParameterList> &pl) :
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_),
432 TEUCHOS_TEST_FOR_EXCEPTION(problem_.is_null(), std::invalid_argument,
433 "BlockCGSolMgr's constructor requires a nonnull LinearProblem instance.");
438 if (! pl.is_null()) {
443 template<
class ScalarType,
class MV,
class OP>
449 if (params_ == Teuchos::null) {
457 if (params->isParameter(
"Maximum Iterations")) {
458 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
461 params_->set(
"Maximum Iterations", maxIters_);
462 if (maxIterTest_!=Teuchos::null)
463 maxIterTest_->setMaxIters( maxIters_ );
467 if (params->isParameter(
"Block Size")) {
468 blockSize_ = params->get(
"Block Size",blockSize_default_);
469 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
470 "Belos::BlockCGSolMgr: \"Block Size\" must be strictly positive.");
473 params_->set(
"Block Size", blockSize_);
477 if (params->isParameter(
"Adaptive Block Size")) {
478 adaptiveBlockSize_ = params->get(
"Adaptive Block Size",adaptiveBlockSize_default_);
481 params_->set(
"Adaptive Block Size", adaptiveBlockSize_);
485 if (params->isParameter(
"Timer Label")) {
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 494 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
496 if (ortho_ != Teuchos::null) {
497 ortho_->setLabel( label_ );
503 if (params->isParameter(
"Orthogonalization")) {
504 std::string tempOrthoType = params->get(
"Orthogonalization",orthoType_default_);
505 TEUCHOS_TEST_FOR_EXCEPTION( tempOrthoType !=
"DGKS" && tempOrthoType !=
"ICGS" && tempOrthoType !=
"IMGS",
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) {
543 if (params->isParameter(
"Verbosity")) {
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_);
557 if (params->isParameter(
"Output Style")) {
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;
570 if (params->isParameter(
"Output Stream")) {
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_ );
581 if (params->isParameter(
"Output Frequency")) {
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") {
663 TEUCHOS_TEST_FOR_EXCEPTION(orthoType_!=
"ICGS"&&orthoType_!=
"DGKS"&&orthoType_!=
"IMGS",std::logic_error,
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 672 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
681 template<
class ScalarType,
class MV,
class OP>
682 Teuchos::RCP<const Teuchos::ParameterList>
685 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
688 if(is_null(validPL)) {
689 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
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;
746 Teuchos::BLAS<int,ScalarType> blas;
747 Teuchos::LAPACK<int,ScalarType> lapack;
749 TEUCHOS_TEST_FOR_EXCEPTION( !problem_->isProblemSet(),
751 "Belos::BlockCGSolMgr::solve(): Linear problem is not ready, setProblem() " 752 "has not been called.");
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 );
786 Teuchos::ParameterList plist;
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 816 Teuchos::TimeMonitor slvtimer(*timerSolve_);
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();
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) {
917 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
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 981 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
986 numIters_ = maxIterTest_->getNumIters();
992 const std::vector<MagnitudeType>* pTestValues =
993 rcp_dynamic_cast<conv_test_type>(convTest_)->getTestValue();
995 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
996 "Belos::BlockCGSolMgr::solve(): The convergence test's getTestValue() " 997 "method returned NULL. Please report this bug to the Belos developers.");
999 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
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;
1021 oss <<
"Belos::BlockCGSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<
">";
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().
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.
Belos concrete class for performing the conjugate-gradient (CG) iteration.
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.
BlockCGSolMgrLinearProblemFailure(const std::string &what_arg)
BlockCGSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i.e.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
A factory class for generating StatusTestOutput objects.
Iterated Modified Gram-Schmidt (IMGS) implementation of the Belos::OrthoManager class.
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.
Classical Gram-Schmidt (with DGKS correction) implementation of the Belos::OrthoManager class...
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. ...
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...
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.
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.
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.