10 #ifndef THYRA_BELOS_TPETRA_PRECONDITIONERFACTORY_DEF_HPP
11 #define THYRA_BELOS_TPETRA_PRECONDITIONERFACTORY_DEF_HPP
15 #include "Thyra_DefaultPreconditioner.hpp"
19 #include "BelosTpetraOperator.hpp"
20 #include "BelosConfigDefs.hpp"
21 #include "BelosLinearProblem.hpp"
22 #include "BelosTpetraAdapter.hpp"
24 #include "Tpetra_MixedScalarMultiplyOp.hpp"
40 #if (!defined(HAVE_TPETRA_INST_DOUBLE) || (defined(HAVE_TPETRA_INST_DOUBLE) && defined(HAVE_TPETRA_INST_FLOAT))) && \
41 (!defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) || (defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) && defined(HAVE_TPETRA_INST_COMPLEX_FLOAT)))
42 # define THYRA_BELOS_PREC_ENABLE_HALF_PRECISION
50 template <
typename MatrixType>
58 template <
typename MatrixType>
60 const LinearOpSourceBase<scalar_type> &fwdOpSrc
63 typedef typename MatrixType::local_ordinal_type local_ordinal_type;
64 typedef typename MatrixType::global_ordinal_type global_ordinal_type;
65 typedef typename MatrixType::node_type node_type;
69 const auto tpetraFwdOp = TpetraExtractHelper::getConstTpetraOperator(fwdOp);
77 template <
typename MatrixType>
81 return Teuchos::rcp(
new DefaultPreconditioner<scalar_type>);
85 template <
typename MatrixType>
87 const Teuchos::RCP<
const LinearOpSourceBase<scalar_type> > &fwdOpSrc,
88 PreconditionerBase<scalar_type> *prec,
89 const ESupportSolveUse
102 totalTimer.start(
true);
108 *out <<
"\nEntering Thyra::BelosTpetraPreconditionerFactory::initializePrec(...) ...\n";
116 typedef typename MatrixType::local_ordinal_type local_ordinal_type;
117 typedef typename MatrixType::global_ordinal_type global_ordinal_type;
118 typedef typename MatrixType::node_type node_type;
120 typedef Tpetra::Operator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> TpetraLinOp;
122 const auto tpetraFwdOp = TpetraExtractHelper::getConstTpetraOperator(fwdOp);
126 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> TpetraMV;
127 typedef Belos::TpetraOperator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> BelosTpOp;
128 typedef Belos::LinearProblem<scalar_type, TpetraMV, TpetraLinOp> BelosTpLinProb;
130 #ifdef THYRA_BELOS_PREC_ENABLE_HALF_PRECISION
135 typedef Tpetra::Operator<half_scalar_type, local_ordinal_type, global_ordinal_type, node_type> TpetraLinOpHalf;
136 typedef Tpetra::MultiVector<half_scalar_type, local_ordinal_type, global_ordinal_type, node_type> TpetraMVHalf;
137 typedef Belos::TpetraOperator<half_scalar_type, local_ordinal_type, global_ordinal_type, node_type> BelosTpOpHalf;
138 typedef Belos::LinearProblem<half_scalar_type, TpetraMVHalf, TpetraLinOpHalf> BelosTpLinProbHalf;
144 Teuchos::ptr(
dynamic_cast<DefaultPreconditioner<scalar_type> *
>(prec));
149 if (paramList_.is_null ()) {
153 innerParamList = paramList_;
156 bool useHalfPrecision =
false;
158 useHalfPrecision = Teuchos::getParameter<bool>(*innerParamList,
"half precision");
160 const std::string solverType = Teuchos::getParameter<std::string>(*innerParamList,
"BelosPrec Solver Type");
164 std::string solverTypeUpper (solverType);
165 std::transform(solverTypeUpper.begin(), solverTypeUpper.end(),solverTypeUpper.begin(), ::toupper);
169 *out <<
"\nCreating a new BelosTpetra::Preconditioner object...\n";
173 if (useHalfPrecision) {
174 #ifdef THYRA_BELOS_PREC_ENABLE_HALF_PRECISION
176 Teuchos::OSTab(out).o() <<
"> Creating half precision preconditioner\n";
178 const RCP<const MatrixType> tpetraFwdMatrix = Teuchos::rcp_dynamic_cast<
const MatrixType>(tpetraFwdOp);
180 auto tpetraFwdMatrixHalf = tpetraFwdMatrix->template convert<half_scalar_type>();
183 belosLinProbHalf->setOperator(tpetraFwdMatrixHalf);
184 RCP<TpetraLinOpHalf> belosOpRCPHalf =
rcp(
new BelosTpOpHalf(belosLinProbHalf, packageParamList, solverType,
true));
185 RCP<TpetraLinOp> wrappedOp =
rcp(
new Tpetra::MixedScalarMultiplyOp<scalar_type,half_scalar_type,local_ordinal_type,global_ordinal_type,node_type>(belosOpRCPHalf));
194 belosLinProb->setOperator(tpetraFwdOp);
195 RCP<TpetraLinOp> belosOpRCP =
rcp(
new BelosTpOp(belosLinProb, packageParamList, solverType,
true));
198 defaultPrec->initializeUnspecified(thyraPrecOp);
202 *out <<
"\nTotal time in Thyra::BelosTpetraPreconditionerFactory::initializePrec(...) = " << totalTimer.totalElapsedTime() <<
" sec\n";
206 *out <<
"\nLeaving Thyra::BelosTpetraPreconditionerFactory::initializePrec(...) ...\n";
211 template <
typename MatrixType>
213 PreconditionerBase<scalar_type> *prec,
214 Teuchos::RCP<
const LinearOpSourceBase<scalar_type> > *fwdOp,
215 ESupportSolveUse *supportSolveUse
225 Teuchos::ptr(
dynamic_cast<DefaultPreconditioner<scalar_type> *
>(prec));
233 if (supportSolveUse) {
235 *supportSolveUse = Thyra::SUPPORT_SOLVE_UNSPECIFIED;
238 defaultPrec->uninitialize();
245 template <
typename MatrixType>
252 const auto validParamList = this->getValidParameters();
256 paramList_ = paramList;
257 Teuchos::readVerboseObjectSublist(paramList_.getRawPtr(),
this);
261 template <
typename MatrixType>
269 template <
typename MatrixType>
275 return savedParamList;
279 template <
typename MatrixType>
286 template <
typename MatrixType>
295 "BelosPrec Solver Type",
"GMRES",
296 "Name of Belos solver to be used as a preconditioner. (Use valid names for Belos::SolverFactory.)"
299 "half precision",
false,
300 "Whether a half-of-standard-precision Belos-solver-as-preconditioner should be built."
303 "BelosPrec Solver Params",
false,
304 "Belos solver settings that are passed onto the Belos solver itself."
306 Teuchos::setupVerboseObjectSublist(validParamList.
getRawPtr());
309 return validParamList;
315 template <
typename MatrixType>
318 return "Thyra::BelosTpetraPreconditionerFactory";
324 #endif // THYRA_BELOS_TPETRA_PRECONDITIONERFACTORY_DEF_HPP
std::string description() const
void uninitializePrec(PreconditionerBase< scalar_type > *prec, Teuchos::RCP< const LinearOpSourceBase< scalar_type > > *fwdOp, ESupportSolveUse *supportSolveUse) const
bool isCompatible(const LinearOpSourceBase< scalar_type > &fwdOp) const
bool nonnull(const std::shared_ptr< T > &p)
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void initializePrec(const Teuchos::RCP< const LinearOpSourceBase< scalar_type > > &fwdOp, PreconditionerBase< scalar_type > *prec, const ESupportSolveUse supportSolveUse) const
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
basic_OSTab< char > OSTab
RCP< LinearOpBase< Scalar > > createLinearOp(const RCP< Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &tpetraOperator, const RCP< const VectorSpaceBase< Scalar > > rangeSpace=Teuchos::null, const RCP< const VectorSpaceBase< Scalar > > domainSpace=Teuchos::null)
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > ¶mList)
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Teuchos::RCP< PreconditionerBase< scalar_type > > createPrec() const
BelosTpetraPreconditionerFactory()
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()