// @HEADER // ***************************************************************************** // Stratimikos: Thyra-based strategies for linear solvers // // Copyright 2006 NTESS and the Stratimikos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include "Thyra_MLPreconditionerFactory.hpp" #include "Thyra_EpetraOperatorViewExtractorStd.hpp" #include "Thyra_EpetraLinearOp.hpp" #include "Thyra_DefaultPreconditioner.hpp" #include "ml_MultiLevelPreconditioner.h" #include "ml_MultiLevelOperator.h" #include "ml_ValidateParameters.h" #include "ml_RefMaxwell.h" #include "ml_GradDiv.h" #include "Epetra_RowMatrix.h" #include "Teuchos_StandardParameterEntryValidators.hpp" #include "Teuchos_dyn_cast.hpp" #include "Teuchos_TimeMonitor.hpp" #include "Teuchos_implicit_cast.hpp" #include "Teuchos_ValidatorXMLConverterDB.hpp" #include "Teuchos_StaticSetupMacro.hpp" #include "Teuchos_iostream_helpers.hpp" namespace { enum EMLProblemType { ML_PROBTYPE_NONE, ML_PROBTYPE_SMOOTHED_AGGREGATION, ML_PROBTYPE_NONSYMMETRIC_SMOOTHED_AGGREGATION, ML_PROBTYPE_DOMAIN_DECOMPOSITION, ML_PROBTYPE_DOMAIN_DECOMPOSITION_ML, ML_PROBTYPE_MAXWELL, ML_PROBTYPE_REFMAXWELL, ML_PROBTYPE_GRADDIV }; const std::string BaseMethodDefaults_valueNames_none = "none"; const Teuchos::Array BaseMethodDefaults_valueNames = Teuchos::tuple( BaseMethodDefaults_valueNames_none, "SA", "NSSA", "DD", "DD-ML", "maxwell", "refmaxwell", "graddiv" ); TEUCHOS_ENUM_INPUT_STREAM_OPERATOR(EMLProblemType) TEUCHOS_STATIC_SETUP() { TEUCHOS_ADD_STRINGTOINTEGRALVALIDATOR_CONVERTER(EMLProblemType); } const std::string BaseMethodDefaults_name = "Base Method Defaults"; const std::string BaseMethodDefaults_default = "SA"; Teuchos::RCP< Teuchos::StringToIntegralParameterEntryValidator > BaseMethodDefaults_validator; const std::string ReuseFineLevelSmoother_name = "Reuse Fine Level Smoother"; const bool ReuseFineLevelSmoother_default = false; const std::string MLSettings_name = "ML Settings"; } // namespace namespace Thyra { using Teuchos::RCP; using Teuchos::ParameterList; // Constructors/initializers/accessors MLPreconditionerFactory::MLPreconditionerFactory() :epetraFwdOpViewExtractor_(Teuchos::rcp(new EpetraOperatorViewExtractorStd())) {} // Overridden from PreconditionerFactoryBase bool MLPreconditionerFactory::isCompatible( const LinearOpSourceBase &fwdOpSrc ) const { using Teuchos::outArg; Teuchos::RCP epetraFwdOp; EOpTransp epetraFwdOpTransp; EApplyEpetraOpAs epetraFwdOpApplyAs; EAdjointEpetraOp epetraFwdOpAdjointSupport; double epetraFwdOpScalar; Teuchos::RCP > fwdOp = fwdOpSrc.getOp(); epetraFwdOpViewExtractor_->getEpetraOpView( fwdOp, outArg(epetraFwdOp),outArg(epetraFwdOpTransp), outArg(epetraFwdOpApplyAs), outArg(epetraFwdOpAdjointSupport), outArg(epetraFwdOpScalar) ); if( !dynamic_cast(&*epetraFwdOp) ) return false; return true; } bool MLPreconditionerFactory::applySupportsConj(EConj /* conj */) const { return true; } bool MLPreconditionerFactory::applyTransposeSupportsConj(EConj /* conj */) const { return false; // See comment below } RCP > MLPreconditionerFactory::createPrec() const { return Teuchos::rcp(new DefaultPreconditioner()); } void MLPreconditionerFactory::initializePrec( const Teuchos::RCP > &fwdOpSrc, PreconditionerBase *prec, const ESupportSolveUse /* supportSolveUse */ ) const { using Teuchos::outArg; using Teuchos::OSTab; using Teuchos::dyn_cast; using Teuchos::RCP; using Teuchos::null; using Teuchos::rcp; using Teuchos::rcp_dynamic_cast; using Teuchos::rcp_const_cast; using Teuchos::set_extra_data; using Teuchos::get_optional_extra_data; using Teuchos::implicit_cast; Teuchos::Time totalTimer(""), timer(""); totalTimer.start(true); const RCP out = this->getOStream(); const Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel(); Teuchos::OSTab tab(out); if(out.get() && implicit_cast(verbLevel) > implicit_cast(Teuchos::VERB_LOW)) *out << "\nEntering Thyra::MLPreconditionerFactory::initializePrec(...) ...\n"; // Get the problem type const EMLProblemType problemType = BaseMethodDefaults_validator->getIntegralValue(*paramList_,BaseMethodDefaults_name,BaseMethodDefaults_default); Teuchos::RCP > fwdOp = fwdOpSrc->getOp(); #ifdef _DEBUG TEUCHOS_TEST_FOR_EXCEPT(fwdOp.get()==NULL); TEUCHOS_TEST_FOR_EXCEPT(prec==NULL); #endif // // Unwrap and get the forward Epetra_Operator object // Teuchos::RCP epetraFwdOp; EOpTransp epetraFwdOpTransp; EApplyEpetraOpAs epetraFwdOpApplyAs; EAdjointEpetraOp epetraFwdOpAdjointSupport; double epetraFwdOpScalar; epetraFwdOpViewExtractor_->getEpetraOpView( fwdOp,outArg(epetraFwdOp),outArg(epetraFwdOpTransp),outArg(epetraFwdOpApplyAs), outArg(epetraFwdOpAdjointSupport),outArg(epetraFwdOpScalar) ); // Validate what we get is what we need RCP epetraFwdRowMat = rcp_dynamic_cast(epetraFwdOp,true); TEUCHOS_TEST_FOR_EXCEPTION( epetraFwdOpApplyAs != EPETRA_OP_APPLY_APPLY, std::logic_error ,"Error, incorrect apply mode for an Epetra_RowMatrix" ); RCP epetraFwdCrsMat = rcp_dynamic_cast(epetraFwdRowMat); // // Get the concrete preconditioner object // DefaultPreconditioner *defaultPrec = &Teuchos::dyn_cast >(*prec); // // Get the EpetraLinearOp object that is used to implement the preconditoner linear op // RCP epetra_precOp = rcp_dynamic_cast(defaultPrec->getNonconstUnspecifiedPrecOp(),true); // // Get the embedded ML_Epetra Preconditioner object if it exists // Teuchos::RCP ml_precOp; Teuchos::RCP rm_precOp; Teuchos::RCP gd_precOp; if(epetra_precOp.get()) { if(problemType == ML_PROBTYPE_REFMAXWELL) rm_precOp = rcp_dynamic_cast(epetra_precOp->epetra_op(),true); else if(problemType == ML_PROBTYPE_GRADDIV) gd_precOp = rcp_dynamic_cast(epetra_precOp->epetra_op(),true); else ml_precOp = rcp_dynamic_cast(epetra_precOp->epetra_op(),true); } // // Get the attached forward operator if it exists and make sure that it matches // if(ml_precOp!=Teuchos::null) { // Get the forward operator and make sure that it matches what is // already being used! const Epetra_RowMatrix & rm = ml_precOp->RowMatrix(); TEUCHOS_TEST_FOR_EXCEPTION( &rm!=&*epetraFwdRowMat, std::logic_error ,"ML requires Epetra_RowMatrix to be the same for each initialization of the preconditioner" ); } // NOTE: No such check exists for RefMaxwell // // Perform initialization if needed // const bool startingOver = (ml_precOp.get() == NULL && rm_precOp.get() == NULL); if(startingOver) { if(out.get() && implicit_cast(verbLevel) >= implicit_cast(Teuchos::VERB_LOW)) *out << "\nCreating the initial ML_Epetra::MultiLevelPreconditioner object...\n"; timer.start(true); // Create the initial preconditioner: DO NOT compute it yet if(problemType==ML_PROBTYPE_REFMAXWELL) rm_precOp = rcp(new ML_Epetra::RefMaxwellPreconditioner(*epetraFwdCrsMat, paramList_->sublist(MLSettings_name),false)); else if(problemType==ML_PROBTYPE_GRADDIV) gd_precOp = rcp(new ML_Epetra::GradDivPreconditioner(*epetraFwdCrsMat, paramList_->sublist(MLSettings_name),false)); else ml_precOp = rcp(new ML_Epetra::MultiLevelPreconditioner(*epetraFwdRowMat, paramList_->sublist(MLSettings_name),false)); timer.stop(); if(out.get() && implicit_cast(verbLevel) >= implicit_cast(Teuchos::VERB_LOW)) OSTab(out).o() <<"> Creation time = "<SetParameterList(paramList_->sublist(MLSettings_name))); } else if (problemType==ML_PROBTYPE_GRADDIV) { TEUCHOS_TEST_FOR_EXCEPT(0!=gd_precOp->SetParameterList(paramList_->sublist(MLSettings_name))); } else { TEUCHOS_TEST_FOR_EXCEPT(0!=ml_precOp->SetParameterList(paramList_->sublist(MLSettings_name))); } } } // // Attach the epetraFwdOp to the ml_precOp to guarantee that it will not go away // if (problemType==ML_PROBTYPE_REFMAXWELL) set_extra_data(epetraFwdOp, "IFPF::epetraFwdOp", Teuchos::inOutArg(rm_precOp), Teuchos::POST_DESTROY, false); else if (problemType==ML_PROBTYPE_GRADDIV) set_extra_data(epetraFwdOp, "IFPF::epetraFwdOp", Teuchos::inOutArg(gd_precOp), Teuchos::POST_DESTROY, false); else set_extra_data(epetraFwdOp, "IFPF::epetraFwdOp", Teuchos::inOutArg(ml_precOp), Teuchos::POST_DESTROY, false); // // Update the factorization // if(out.get() && implicit_cast(verbLevel) >= implicit_cast(Teuchos::VERB_LOW)) *out << "\nComputing the preconditioner ...\n"; timer.start(true); if (problemType==ML_PROBTYPE_REFMAXWELL) { if (startingOver) { TEUCHOS_TEST_FOR_EXCEPT(0!=rm_precOp->ComputePreconditioner()); } else { TEUCHOS_TEST_FOR_EXCEPT(0!=rm_precOp->ReComputePreconditioner()); } } else if (problemType==ML_PROBTYPE_GRADDIV) { if (startingOver) { TEUCHOS_TEST_FOR_EXCEPT(0!=gd_precOp->ComputePreconditioner()); } else { TEUCHOS_TEST_FOR_EXCEPT(0!=gd_precOp->ReComputePreconditioner()); } } else { if (startingOver) { TEUCHOS_TEST_FOR_EXCEPT(0!=ml_precOp->ComputePreconditioner()); } else { TEUCHOS_TEST_FOR_EXCEPT(0!=ml_precOp->ReComputePreconditioner(paramList_->get(ReuseFineLevelSmoother_name))); } } timer.stop(); if(out.get() && implicit_cast(verbLevel) >= implicit_cast(Teuchos::VERB_LOW)) OSTab(out).o() <<"=> Setup time = "<initialize(rm_precOp,epetraFwdOpTransp,EPETRA_OP_APPLY_APPLY_INVERSE,EPETRA_OP_ADJOINT_UNSUPPORTED); else if (problemType==ML_PROBTYPE_GRADDIV) epetra_precOp->initialize(gd_precOp,epetraFwdOpTransp,EPETRA_OP_APPLY_APPLY_INVERSE,EPETRA_OP_ADJOINT_UNSUPPORTED); else epetra_precOp->initialize(ml_precOp,epetraFwdOpTransp,EPETRA_OP_APPLY_APPLY_INVERSE,EPETRA_OP_ADJOINT_UNSUPPORTED); // // Initialize the preconditioner // defaultPrec->initializeUnspecified( Teuchos::rcp_implicit_cast >(epetra_precOp) ); totalTimer.stop(); if(out.get() && implicit_cast(verbLevel) >= implicit_cast(Teuchos::VERB_LOW)) *out << "\nTotal time in MLPreconditionerFactory = "<(verbLevel) > implicit_cast(Teuchos::VERB_LOW)) *out << "\nLeaving Thyra::MLPreconditionerFactory::initializePrec(...) ...\n"; } void MLPreconditionerFactory::uninitializePrec( PreconditionerBase * /* prec */, Teuchos::RCP > * /* fwdOp */, ESupportSolveUse * /* supportSolveUse */ ) const { TEUCHOS_TEST_FOR_EXCEPT(true); } // Overridden from ParameterListAcceptor void MLPreconditionerFactory::setParameterList( Teuchos::RCP const& paramList ) { TEUCHOS_TEST_FOR_EXCEPT(paramList.get()==NULL); // Do not recurse paramList->validateParameters(*this->getValidParameters(),0); paramList_ = paramList; // set default for reuse of fine level smoother if(!paramList_->isType(ReuseFineLevelSmoother_name)) paramList_->set(ReuseFineLevelSmoother_name,ReuseFineLevelSmoother_default); const EMLProblemType defaultType = BaseMethodDefaults_validator->getIntegralValue( *paramList_,BaseMethodDefaults_name,BaseMethodDefaults_default ); if( ML_PROBTYPE_NONE != defaultType ) { const std::string defaultTypeStr = BaseMethodDefaults_valueNames[defaultType]; // ML will do validation on its own. We don't need to duplicate that here. Teuchos::ParameterList defaultParams; if(defaultType == ML_PROBTYPE_REFMAXWELL) { ML_Epetra::SetDefaultsRefMaxwell(defaultParams); } else if(defaultType == ML_PROBTYPE_GRADDIV) { ML_Epetra::SetDefaultsGradDiv(defaultParams); } else { TEUCHOS_TEST_FOR_EXCEPTION(0!=ML_Epetra::SetDefaults(defaultTypeStr,defaultParams) ,Teuchos::Exceptions::InvalidParameterValue ,"Error, the ML problem type \"" << defaultTypeStr << "\' is not recognized by ML!" ); } // Note, the only way the above exception message could be generated is if // a default problem type was removed from ML_Epetra::SetDefaults(...). // When a new problem type is added to this function, it must be added to // our enum EMLProblemType along with associated objects ... In other // words, this adapter must be maintained as ML is maintained. An // alternative design would be to just pass in whatever string to this // function. This would improve maintainability but it would not generate // very good error messages when a bad string was passed in. Currently, // the error message attached to the exception will contain the list of // valid problem types. paramList_->sublist(MLSettings_name).setParametersNotAlreadySet( defaultParams); } } RCP MLPreconditionerFactory::getNonconstParameterList() { return paramList_; } RCP MLPreconditionerFactory::unsetParameterList() { Teuchos::RCP _paramList = paramList_; paramList_ = Teuchos::null; return _paramList; } RCP MLPreconditionerFactory::getParameterList() const { return paramList_; } RCP MLPreconditionerFactory::getValidParameters() const { // NOTE: We're only going to use this function to genrate valid *Stratimikos* parameters. // Since ML's parameters can be validated internally, we'll handle those separarely. using Teuchos::rcp; using Teuchos::tuple; using Teuchos::implicit_cast; using Teuchos::rcp_implicit_cast; typedef Teuchos::ParameterEntryValidator PEV; static RCP validPL; if(is_null(validPL)) { RCP pl = rcp(new ParameterList()); BaseMethodDefaults_validator = rcp( new Teuchos::StringToIntegralParameterEntryValidator( BaseMethodDefaults_valueNames, tuple( "Do not set any default parameters", "Set default parameters for a smoothed aggregation method", "Set default parameters for a nonsymmetric smoothed aggregation method", "Set default parameters for a domain decomposition method", "Set default parameters for a domain decomposition method special to ML", "Set default parameters for a Maxwell-type of preconditioner", "Set default parameters for a RefMaxwell-type preconditioner", "Set default parameters for a Grad-Div-type preconditioner" ), tuple( ML_PROBTYPE_NONE, ML_PROBTYPE_SMOOTHED_AGGREGATION, ML_PROBTYPE_NONSYMMETRIC_SMOOTHED_AGGREGATION, ML_PROBTYPE_DOMAIN_DECOMPOSITION, ML_PROBTYPE_DOMAIN_DECOMPOSITION_ML, ML_PROBTYPE_MAXWELL, ML_PROBTYPE_REFMAXWELL, ML_PROBTYPE_GRADDIV ), BaseMethodDefaults_name ) ); pl->set(BaseMethodDefaults_name,BaseMethodDefaults_default, "Select the default method type which also sets parameter defaults\n" "in the sublist \"" + MLSettings_name + "\"!", rcp_implicit_cast(BaseMethodDefaults_validator) ); pl->set(ReuseFineLevelSmoother_name,ReuseFineLevelSmoother_default, "Enables/disables the reuse of the fine level smoother."); ParameterList mlpl; pl->set(MLSettings_name,mlpl, "Sampling of the parameters directly accepted by ML\n" "This list of parameters is generated by combining all of\n" "the parameters set for all of the default problem types supported\n" "by ML. Therefore, do not assume these parameters are at values that\n" "are reasonable to ML. This list is just to give a sense of some of\n" "the parameters that ML accepts. Consult ML documentation on how to\n" "set these parameters. Also, you can print the parameter list after\n" "it is used and see what defaults where set for each default problem\n" "type. Warning! the parameters in this sublist are currently *not*\n" "being validated by ML!" ); validPL = pl; } return validPL; } // Public functions overridden from Teuchos::Describable std::string MLPreconditionerFactory::description() const { std::ostringstream oss; oss << "Thyra::MLPreconditionerFactory"; return oss.str(); } } // namespace Thyra