42 #ifndef BELOS_PSEUDO_BLOCK_TFQMR_SOLMGR_HPP 43 #define BELOS_PSEUDO_BLOCK_TFQMR_SOLMGR_HPP 61 #ifdef BELOS_TEUCHOS_TIME_MONITOR 62 #include "Teuchos_TimeMonitor.hpp" 93 template<
class ScalarType,
class MV,
class OP>
99 typedef Teuchos::ScalarTraits<ScalarType> SCT;
100 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
101 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
132 const Teuchos::RCP<Teuchos::ParameterList> &pl );
138 Teuchos::RCP<SolverManager<ScalarType, MV, OP> >
clone ()
const override {
163 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
164 return Teuchos::tuple(timerSolve_);
198 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList> ¶ms )
override;
244 bool checkStatusTest();
247 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
250 Teuchos::RCP<OutputManager<ScalarType> > printer_;
251 Teuchos::RCP<std::ostream> outputStream_;
254 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
255 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
256 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
257 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_, impConvTest_;
258 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
261 Teuchos::RCP<Teuchos::ParameterList> params_;
264 static constexpr
int maxIters_default_ = 1000;
265 static constexpr
bool expResTest_default_ =
false;
268 static constexpr
int outputFreq_default_ = -1;
269 static constexpr
int defQuorum_default_ = 1;
270 static constexpr
const char * impResScale_default_ =
"Norm of Preconditioned Initial Residual";
271 static constexpr
const char * expResScale_default_ =
"Norm of Initial Residual";
272 static constexpr
const char * label_default_ =
"Belos";
274 #if defined(_WIN32) && defined(__clang__) 275 static constexpr std::ostream * outputStream_default_ =
276 __builtin_constant_p(reinterpret_cast<const std::ostream*>(&std::cout));
278 static constexpr std::ostream * outputStream_default_ = &std::cout;
282 MagnitudeType convtol_, impTolScale_, achievedTol_;
283 int maxIters_, numIters_;
284 int verbosity_, outputStyle_, outputFreq_, defQuorum_;
286 std::string impResScale_, expResScale_;
290 Teuchos::RCP<Teuchos::Time> timerSolve_;
293 bool isSet_, isSTSet_;
298 template<
class ScalarType,
class MV,
class OP>
300 outputStream_(Teuchos::rcp(outputStream_default_,false)),
303 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
304 maxIters_(maxIters_default_),
306 verbosity_(verbosity_default_),
307 outputStyle_(outputStyle_default_),
308 outputFreq_(outputFreq_default_),
309 defQuorum_(defQuorum_default_),
310 expResTest_(expResTest_default_),
311 impResScale_(impResScale_default_),
312 expResScale_(expResScale_default_),
313 label_(label_default_),
320 template<
class ScalarType,
class MV,
class OP>
323 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
325 outputStream_(Teuchos::rcp(outputStream_default_,false)),
328 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
329 maxIters_(maxIters_default_),
331 verbosity_(verbosity_default_),
332 outputStyle_(outputStyle_default_),
333 outputFreq_(outputFreq_default_),
334 defQuorum_(defQuorum_default_),
335 expResTest_(expResTest_default_),
336 impResScale_(impResScale_default_),
337 expResScale_(expResScale_default_),
338 label_(label_default_),
342 TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument,
"Problem not given to solver manager.");
345 if ( !is_null(pl) ) {
350 template<
class ScalarType,
class MV,
class OP>
354 if (params_ == Teuchos::null) {
355 params_ = Teuchos::rcp(
new Teuchos::ParameterList(*getValidParameters()) );
358 params->validateParameters(*getValidParameters());
362 if (params->isParameter(
"Maximum Iterations")) {
363 maxIters_ = params->get(
"Maximum Iterations",maxIters_default_);
366 params_->set(
"Maximum Iterations", maxIters_);
367 if (maxIterTest_!=Teuchos::null)
368 maxIterTest_->setMaxIters( maxIters_ );
372 if (params->isParameter(
"Timer Label")) {
373 std::string tempLabel = params->get(
"Timer Label", label_default_);
376 if (tempLabel != label_) {
378 params_->set(
"Timer Label", label_);
379 std::string solveLabel = label_ +
": PseudoBlockTFQMRSolMgr total solve time";
380 #ifdef BELOS_TEUCHOS_TIME_MONITOR 381 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
387 if (params->isParameter(
"Verbosity")) {
388 if (Teuchos::isParameterType<int>(*params,
"Verbosity")) {
389 verbosity_ = params->get(
"Verbosity", verbosity_default_);
391 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,
"Verbosity");
395 params_->set(
"Verbosity", verbosity_);
396 if (printer_ != Teuchos::null)
397 printer_->setVerbosity(verbosity_);
401 if (params->isParameter(
"Output Style")) {
402 if (Teuchos::isParameterType<int>(*params,
"Output Style")) {
403 outputStyle_ = params->get(
"Output Style", outputStyle_default_);
405 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,
"Output Style");
409 params_->set(
"Output Style", outputStyle_);
414 if (params->isParameter(
"Output Stream")) {
415 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,
"Output Stream");
418 params_->set(
"Output Stream", outputStream_);
419 if (printer_ != Teuchos::null)
420 printer_->setOStream( outputStream_ );
425 if (params->isParameter(
"Output Frequency")) {
426 outputFreq_ = params->get(
"Output Frequency", outputFreq_default_);
430 params_->set(
"Output Frequency", outputFreq_);
431 if (outputTest_ != Teuchos::null)
432 outputTest_->setOutputFrequency( outputFreq_ );
436 if (printer_ == Teuchos::null) {
441 if (params->isParameter(
"Convergence Tolerance")) {
442 if (params->isType<MagnitudeType> (
"Convergence Tolerance")) {
443 convtol_ = params->get (
"Convergence Tolerance",
451 params_->set(
"Convergence Tolerance", convtol_);
455 if (params->isParameter(
"Implicit Tolerance Scale Factor")) {
456 if (params->isType<MagnitudeType> (
"Implicit Tolerance Scale Factor")) {
457 impTolScale_ = params->get (
"Implicit Tolerance Scale Factor",
462 impTolScale_ = params->get (
"Implicit Tolerance Scale Factor",
467 params_->set(
"Implicit Tolerance Scale Factor", impTolScale_);
471 if (params->isParameter(
"Implicit Residual Scaling")) {
472 std::string tempImpResScale = Teuchos::getParameter<std::string>( *params,
"Implicit Residual Scaling" );
475 if (impResScale_ != tempImpResScale) {
476 impResScale_ = tempImpResScale;
479 params_->set(
"Implicit Residual Scaling", impResScale_);
484 if (params->isParameter(
"Explicit Residual Scaling")) {
485 std::string tempExpResScale = Teuchos::getParameter<std::string>( *params,
"Explicit Residual Scaling" );
488 if (expResScale_ != tempExpResScale) {
489 expResScale_ = tempExpResScale;
492 params_->set(
"Explicit Residual Scaling", expResScale_);
497 if (params->isParameter(
"Explicit Residual Test")) {
498 expResTest_ = Teuchos::getParameter<bool>( *params,
"Explicit Residual Test" );
501 params_->set(
"Explicit Residual Test", expResTest_);
502 if (expConvTest_ == Teuchos::null) {
508 if (params->isParameter(
"Deflation Quorum")) {
509 defQuorum_ = params->get(
"Deflation Quorum", defQuorum_);
510 params_->set (
"Deflation Quorum", defQuorum_);
511 if (! impConvTest_.is_null ()) {
512 impConvTest_->setQuorum (defQuorum_);
514 if (! expConvTest_.is_null ()) {
515 expConvTest_->setQuorum (defQuorum_);
520 if (timerSolve_ == Teuchos::null) {
521 std::string solveLabel = label_ +
": PseudoBlockTFQMRSolMgr total solve time";
522 #ifdef BELOS_TEUCHOS_TIME_MONITOR 523 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
533 template<
class ScalarType,
class MV,
class OP>
545 Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
546 Teuchos::rcp(
new StatusTestGenResNorm_t( impTolScale_*convtol_, defQuorum_ ) );
548 impConvTest_ = tmpImpConvTest;
551 Teuchos::RCP<StatusTestGenResNorm_t> tmpExpConvTest =
552 Teuchos::rcp(
new StatusTestGenResNorm_t( convtol_, defQuorum_ ) );
553 tmpExpConvTest->defineResForm( StatusTestGenResNorm_t::Explicit,
Belos::TwoNorm );
555 expConvTest_ = tmpExpConvTest;
558 convTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
563 Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
564 Teuchos::rcp(
new StatusTestGenResNorm_t( convtol_, defQuorum_ ) );
566 impConvTest_ = tmpImpConvTest;
569 expConvTest_ = impConvTest_;
570 convTest_ = impConvTest_;
572 sTest_ = Teuchos::rcp(
new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
576 StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
580 std::string solverDesc =
" Pseudo Block TFQMR ";
581 outputTest_->setSolverDesc( solverDesc );
591 template<
class ScalarType,
class MV,
class OP>
592 Teuchos::RCP<const Teuchos::ParameterList>
595 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
598 if(is_null(validPL)) {
599 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
604 "The relative residual tolerance that needs to be achieved by the\n" 605 "iterative solver in order for the linear system to be declared converged.");
607 "The scale factor used by the implicit residual test when explicit residual\n" 608 "testing is used. May enable faster convergence when TFQMR bound is too loose.");
609 pl->set(
"Maximum Iterations", static_cast<int>(maxIters_default_),
610 "The maximum number of block iterations allowed for each\n" 611 "set of RHS solved.");
612 pl->set(
"Verbosity", static_cast<int>(verbosity_default_),
613 "What type(s) of solver information should be outputted\n" 614 "to the output stream.");
615 pl->set(
"Output Style", static_cast<int>(outputStyle_default_),
616 "What style is used for the solver information outputted\n" 617 "to the output stream.");
618 pl->set(
"Output Frequency", static_cast<int>(outputFreq_default_),
619 "How often convergence information should be outputted\n" 620 "to the output stream.");
621 pl->set(
"Deflation Quorum", static_cast<int>(defQuorum_default_),
622 "The number of linear systems that need to converge before they are deflated.");
623 pl->set(
"Output Stream", Teuchos::rcp(outputStream_default_,
false),
624 "A reference-counted pointer to the output stream where all\n" 625 "solver output is sent.");
626 pl->set(
"Explicit Residual Test", static_cast<bool>(expResTest_default_),
627 "Whether the explicitly computed residual should be used in the convergence test.");
628 pl->set(
"Implicit Residual Scaling", static_cast<const char *>(impResScale_default_),
629 "The type of scaling used in the implicit residual convergence test.");
630 pl->set(
"Explicit Residual Scaling", static_cast<const char *>(expResScale_default_),
631 "The type of scaling used in the explicit residual convergence test.");
632 pl->set(
"Timer Label", static_cast<const char *>(label_default_),
633 "The string to use as a prefix for the timer labels.");
641 template<
class ScalarType,
class MV,
class OP>
648 setParameters(Teuchos::parameterList(*getValidParameters()));
652 "Belos::PseudoBlockTFQMRSolMgr::solve(): Linear problem is not a valid object.");
655 "Belos::PseudoBlockTFQMRSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
659 "Belos::PseudoBlockTFQMRSolMgr::solve(): Linear problem and requested status tests are incompatible.");
664 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
665 int numCurrRHS = numRHS2Solve;
667 std::vector<int> currIdx( numRHS2Solve );
668 for (
int i=0; i<numRHS2Solve; ++i) {
669 currIdx[i] = startPtr+i;
673 problem_->setLSIndex( currIdx );
677 Teuchos::ParameterList plist;
680 outputTest_->reset();
683 bool isConverged =
true;
688 Teuchos::RCP<PseudoBlockTFQMRIter<ScalarType,MV,OP> > block_tfqmr_iter =
693 #ifdef BELOS_TEUCHOS_TIME_MONITOR 694 Teuchos::TimeMonitor slvtimer(*timerSolve_);
697 while ( numRHS2Solve > 0 ) {
700 std::vector<int> convRHSIdx;
701 std::vector<int> currRHSIdx( currIdx );
702 currRHSIdx.resize(numCurrRHS);
705 block_tfqmr_iter->resetNumIters();
708 outputTest_->resetNumCalls();
711 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())), currIdx );
716 block_tfqmr_iter->initializeTFQMR(newstate);
722 block_tfqmr_iter->iterate();
729 if ( convTest_->getStatus() ==
Passed ) {
732 std::vector<int> convIdx = expConvTest_->convIndices();
736 if (convIdx.size() == currRHSIdx.size())
740 problem_->updateSolution( block_tfqmr_iter->getCurrentUpdate(), true );
743 problem_->setCurrLS();
747 std::vector<int> unconvIdx( currRHSIdx.size() );
748 for (
unsigned int i=0; i<currRHSIdx.size(); ++i) {
750 for (
unsigned int j=0; j<convIdx.size(); ++j) {
751 if (currRHSIdx[i] == convIdx[j]) {
758 currRHSIdx[have++] = currRHSIdx[i];
761 unconvIdx.resize(have);
762 currRHSIdx.resize(have);
765 problem_->setLSIndex( currRHSIdx );
775 defstate.
Rtilde = MVT::CloneView( *currentState.
Rtilde, unconvIdx);
776 defstate.
U = MVT::CloneView( *currentState.
U, unconvIdx );
777 defstate.
AU = MVT::CloneView( *currentState.
AU, unconvIdx );
778 defstate.
V = MVT::CloneView( *currentState.
V, unconvIdx );
779 defstate.
W = MVT::CloneView( *currentState.
W, unconvIdx );
780 defstate.
D = MVT::CloneView( *currentState.
D, unconvIdx );
783 for (std::vector<int>::iterator uIter = unconvIdx.begin(); uIter != unconvIdx.end(); uIter++)
785 defstate.
alpha.push_back( currentState.
alpha[ *uIter ] );
786 defstate.
eta.push_back( currentState.
eta[ *uIter ] );
787 defstate.
rho.push_back( currentState.
rho[ *uIter ] );
788 defstate.
tau.push_back( currentState.
tau[ *uIter ] );
789 defstate.
theta.push_back( currentState.
theta[ *uIter ] );
792 block_tfqmr_iter->initializeTFQMR(defstate);
799 else if ( maxIterTest_->getStatus() ==
Passed ) {
813 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
814 "Belos::PseudoBlockTFQMRSolMgr::solve(): Invalid return from PseudoBlockTFQMRIter::iterate().");
817 catch (
const std::exception &e) {
818 printer_->stream(
Errors) <<
"Error! Caught std::exception in PseudoBlockTFQMRIter::iterate() at iteration " 819 << block_tfqmr_iter->getNumIters() << std::endl
820 << e.what() << std::endl;
826 problem_->updateSolution( block_tfqmr_iter->getCurrentUpdate(), true );
829 problem_->setCurrLS();
832 startPtr += numCurrRHS;
833 numRHS2Solve -= numCurrRHS;
834 if ( numRHS2Solve > 0 ) {
835 numCurrRHS = numRHS2Solve;
836 currIdx.resize( numCurrRHS );
837 for (
int i=0; i<numCurrRHS; ++i)
838 { currIdx[i] = startPtr+i; }
841 if (defQuorum_ > numCurrRHS) {
842 if (impConvTest_ != Teuchos::null)
843 impConvTest_->setQuorum( numCurrRHS );
844 if (expConvTest_ != Teuchos::null)
845 expConvTest_->setQuorum( numCurrRHS );
849 problem_->setLSIndex( currIdx );
852 currIdx.resize( numRHS2Solve );
863 #ifdef BELOS_TEUCHOS_TIME_MONITOR 868 Teuchos::TimeMonitor::summarize( printer_->stream(
TimingDetails) );
872 numIters_ = maxIterTest_->getNumIters();
885 const std::vector<MagnitudeType>* pTestValues = NULL;
887 pTestValues = expConvTest_->getTestValue();
888 if (pTestValues == NULL || pTestValues->size() < 1) {
889 pTestValues = impConvTest_->getTestValue();
894 pTestValues = impConvTest_->getTestValue();
896 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
897 "Belos::PseudoBlockTFQMRSolMgr::solve(): The implicit convergence test's " 898 "getTestValue() method returned NULL. Please report this bug to the " 899 "Belos developers.");
900 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
901 "Belos::TMQMRSolMgr::solve(): The implicit convergence test's " 902 "getTestValue() method returned a vector of length zero. Please report " 903 "this bug to the Belos developers.");
908 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
918 template<
class ScalarType,
class MV,
class OP>
921 std::ostringstream oss;
922 oss <<
"Belos::PseudoBlockTFQMRSolMgr<...,"<<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.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Class which manages the output and verbosity of the Belos solvers.
Teuchos::RCP< const MV > Rtilde
This class implements the preconditioned transpose-free QMR algorithm for solving non-Hermitian linea...
Structure to contain pointers to PseudoBlockTFQMRIter state variables.
Belos concrete class for generating iterations with the preconditioned tranpose-free QMR (TFQMR) meth...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
An implementation of StatusTestResNorm using a family of residual norms.
Teuchos::RCP< const MV > AU
PseudoBlockTFQMRSolMgrLinearProblemFailure(const std::string &what_arg)
static const double convTol
Default convergence tolerance.
Belos::StatusTest class for specifying a maximum number of iterations.
std::vector< ScalarType > alpha
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
A Belos::StatusTest class for specifying a maximum number of iterations.
PseudoBlockTFQMRSolMgr()
Empty constructor for PseudoBlockTFQMRSolMgr. This constructor takes no arguments and sets the defaul...
PseudoBlockTFQMRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
ResetType
How to reset the solver.
Pure virtual base class which describes the basic interface for a solver manager. ...
bool isLOADetected() const override
Whether loss of accuracy was detected during the last solve() invocation.
static const double impTolScale
"Implicit Tolerance Scale Factor"
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
std::string description() const override
Method to return description of the pseudo-block TFQMR solver manager.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
A linear system to solve, and its associated information.
std::vector< MagnitudeType > theta
Class which describes the linear problem to be solved by the iterative solver.
The Belos::PseudoBlockTFQMRSolMgr provides a powerful and fully-featured solver manager over the pseu...
Teuchos::RCP< const MV > D
ReturnType
Whether the Belos solve converged for all linear systems.
std::vector< ScalarType > eta
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const MV > U
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
std::vector< MagnitudeType > tau
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
Teuchos::RCP< const MV > W
The current residual basis.
A class for extending the status testing capabilities of Belos via logical combinations.
virtual ~PseudoBlockTFQMRSolMgr()
Destructor.
std::vector< ScalarType > rho
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
Default parameters common to most Belos solvers.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< const MV > V
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms) override
Set the parameters the solver manager should use to solve the linear problem.