MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_TekoSmoother_decl.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MueLu: A package for multigrid based preconditioning
4//
5// Copyright 2012 NTESS and the MueLu contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef MUELU_TEKOSMOOTHER_DECL_HPP_
11#define MUELU_TEKOSMOOTHER_DECL_HPP_
12
13#ifdef HAVE_MUELU_TEKO
14
15#include "Teko_Utilities.hpp"
16
17#include "Teko_InverseLibrary.hpp"
18#include "Teko_InverseFactory.hpp"
19
20#include "MueLu_ConfigDefs.hpp"
21
22#include <Teuchos_ParameterList.hpp>
23
24#include <Xpetra_MapExtractor_fwd.hpp>
25
27#include "MueLu_SmootherPrototype.hpp"
29#include "MueLu_Monitor.hpp"
30
31namespace MueLu {
32
42template <class Scalar = SmootherPrototype<>::scalar_type,
46class TekoSmoother : public SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
47 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
48
49#undef MUELU_TEKOSMOOTHER_SHORT
51
52 public:
54
55
59 : type_("Teko smoother") {
60 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "MueLu::TekoSmoother: Teko can only be used with SC=double. For more information refer to the doxygen documentation of TekoSmoother.");
61 };
62
63 TekoSmoother(const Teuchos::ParameterList &paramList)
64 : type_("Teko smoother") {
65 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "MueLu::TekoSmoother: Teko can only be used with SC=double. For more information refer to the doxygen documentation of TekoSmoother.");
66 }
67
69 virtual ~TekoSmoother() {}
71
73
74 RCP<const ParameterList> GetValidParameterList() const {
75 RCP<ParameterList> validParamList = rcp(new ParameterList());
76 return validParamList;
77 }
78
79 void DeclareInput(Level &currentLevel) const {}
80
81 void SetTekoParameters(RCP<ParameterList> tekoParams){};
83
85
86
89 void Setup(Level &currentLevel) {}
90
97 void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero = false) const {}
99
100 RCP<SmootherPrototype> Copy() const { return Teuchos::null; }
101
103
104
106 std::string description() const {
107 std::ostringstream out;
109 out << "{type = " << type_ << "}";
110 return out.str();
111 }
112
114 // using MueLu::Describable::describe; // overloading, not hiding
115 void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
117
118 if (verbLevel & Parameters0)
119 out0 << "Prec. type: " << type_ << std::endl;
120
121 if (verbLevel & Debug)
122 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
123 }
124
126 size_t getNodeSmootherComplexity() const;
127
129
130 private:
132 std::string type_;
133}; // class TekoSmoother
134
149template <class GlobalOrdinal,
150 class Node>
151class TekoSmoother<double, int, GlobalOrdinal, Node> : public SmootherPrototype<double, int, GlobalOrdinal, Node> {
152 typedef int LocalOrdinal;
153 typedef double Scalar;
154 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
155
156#undef MUELU_TEKOSMOOTHER_SHORT
158
159 public:
161
162
166 : type_("Teko smoother")
167 , A_(Teuchos::null)
168 , bA_(Teuchos::null)
169 , bThyOp_(Teuchos::null)
170 , tekoParams_(Teuchos::null)
171 , inverseOp_(Teuchos::null){};
172
175 TekoSmoother(const Teuchos::ParameterList &paramList)
176 : type_("Teko smoother")
177 , A_(Teuchos::null)
178 , bA_(Teuchos::null)
179 , bThyOp_(Teuchos::null)
180 , tekoParams_(Teuchos::null)
181 , inverseOp_(Teuchos::null) {
182 this->SetParameter("Inverse Type", paramList.getEntry("Inverse Type"));
183 const auto &tekoSettings = paramList.sublist("Inverse Factory Library");
184 tekoParams_ = Teuchos::make_rcp<Teuchos::ParameterList>(tekoSettings);
185 };
186
188 virtual ~TekoSmoother() {}
190
192
193 RCP<const ParameterList> GetValidParameterList() const {
194 RCP<ParameterList> validParamList = rcp(new ParameterList());
195
196 validParamList->set<RCP<const FactoryBase>>("A", null, "Generating factory of the matrix A");
197 validParamList->set<std::string>("Inverse Type", "", "Name of parameter list within 'Teko parameters' containing the Teko smoother parameters.");
198
199 return validParamList;
200 }
201
202 void DeclareInput(Level &currentLevel) const {
203 this->Input(currentLevel, "A");
204 }
205
206 void SetTekoParameters(RCP<ParameterList> tekoParams) { tekoParams_ = tekoParams; };
208
210
211
214 void Setup(Level &currentLevel) {
215 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
216
217 FactoryMonitor m(*this, "Setup TekoSmoother", currentLevel);
218 if (this->IsSetup() == true)
219 this->GetOStream(Warnings0) << "MueLu::TekoSmoother::Setup(): Setup() has already been called";
220
221 // extract blocked operator A from current level
222 A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A"); // A needed for extracting map extractors
223 bA_ = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
224 TEUCHOS_TEST_FOR_EXCEPTION(bA_.is_null(), Exceptions::BadCast,
225 "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
226
227 bThyOp_ = bA_->getThyraOperator();
228 TEUCHOS_TEST_FOR_EXCEPTION(bThyOp_.is_null(), Exceptions::BadCast,
229 "MueLu::TekoSmoother::Build: Could not extract thyra operator from BlockedCrsMatrix.");
230
231 Teuchos::RCP<const Thyra::LinearOpBase<Scalar>> thyOp = Teuchos::rcp_dynamic_cast<const Thyra::LinearOpBase<Scalar>>(bThyOp_);
232 TEUCHOS_TEST_FOR_EXCEPTION(thyOp.is_null(), Exceptions::BadCast,
233 "MueLu::TekoSmoother::Build: Downcast of Thyra::BlockedLinearOpBase to Teko::LinearOp failed.");
234
235 // parameter list contains TekoSmoother parameters but does not handle the Teko parameters itself!
236 const ParameterList &pL = Factory::GetParameterList();
237 std::string smootherType = pL.get<std::string>("Inverse Type");
238 TEUCHOS_TEST_FOR_EXCEPTION(smootherType.empty(), Exceptions::RuntimeError,
239 "MueLu::TekoSmoother::Build: You must provide a 'Smoother Type' name that is defined in the 'Teko parameters' sublist.");
240 type_ = smootherType;
241
242 TEUCHOS_TEST_FOR_EXCEPTION(tekoParams_.is_null(), Exceptions::BadCast,
243 "MueLu::TekoSmoother::Build: No Teko parameters have been set.");
244
245 Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*tekoParams_);
246 Teuchos::RCP<Teko::InverseFactory> inverse = invLib->getInverseFactory(smootherType);
247
248 inverseOp_ = Teko::buildInverse(*inverse, thyOp);
249 TEUCHOS_TEST_FOR_EXCEPTION(inverseOp_.is_null(), Exceptions::BadCast,
250 "MueLu::TekoSmoother::Build: Failed to build Teko inverse operator. Probably a problem with the Teko parameters.");
251
252 this->IsSetup(true);
253 }
254
261 void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero = false) const {
262 TEUCHOS_TEST_FOR_EXCEPTION(!this->IsSetup(), Exceptions::RuntimeError,
263 "MueLu::TekoSmoother::Apply(): Setup() has not been called");
264
265 auto comm = X.getMap()->getComm();
266 auto rgMapExtractor = bA_->getRangeMapExtractor();
267 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
268
269 using STS = Teuchos::ScalarTraits<Scalar>;
270
271 auto createProductMultiVector = [](const Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> &space,
272 int numVecs,
273 const char *castErrorMsg) {
274 auto mv = Thyra::createMembers(space, numVecs);
275 auto prodMv = Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar>>(mv);
276 TEUCHOS_TEST_FOR_EXCEPTION(prodMv.is_null(), Exceptions::BadCast, castErrorMsg);
277 return std::make_pair(mv, prodMv);
278 };
279
280 auto solveWithThyra = [&](const Teuchos::RCP<const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> &rhs,
281 bool initializeFromX,
282 const typename STS::magnitudeType alphaX) {
283 auto [thyB, thyProdB] = createProductMultiVector(
284 Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar>>(bThyOp_->productRange()),
285 Teuchos::as<int>(rhs->getNumVectors()),
286 "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
287
288 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(rhs, rgMapExtractor, thyProdB);
289
290 auto [thyX, thyProdX] = createProductMultiVector(
291 Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar>>(bThyOp_->productDomain()),
292 Teuchos::as<int>(X.getNumVectors()),
293 "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
294
295 if (initializeFromX) {
296 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
297 }
298
299 inverseOp_->apply(
300 Thyra::NOTRANS,
301 *thyB,
302 thyX.ptr(),
303 STS::one(),
304 STS::zero());
305
306 auto XX = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyX, comm);
307 X.update(STS::one(), *XX, alphaX);
308 };
309
310 if (InitialGuessIsZero) {
311 solveWithThyra(Teuchos::rcpFromRef(B), /*initializeFromX=*/true, STS::zero());
312 return;
313 }
314
315 auto residual = Utilities::Residual(*A_, X, B);
316 solveWithThyra(residual, /*initializeFromX=*/false, STS::one());
317 }
319
320 RCP<SmootherPrototype> Copy() const { return Teuchos::rcp(new MueLu::TekoSmoother<double, int, GlobalOrdinal, Node>(*this)); }
321
323
324
326 std::string description() const {
327 std::ostringstream out;
329 out << "{type = " << type_ << "}";
330 return out.str();
331 }
332
334 // using MueLu::Describable::describe; // overloading, not hiding
335 void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
337
338 if (verbLevel & Parameters0)
339 out0 << "Prec. type: " << type_ << std::endl;
340
341 if (verbLevel & Debug)
342 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
343 }
344
347 size_t cplx = 0;
348 return cplx;
349 }
350
352
353 private:
355 std::string type_;
356
358 RCP<Matrix> A_; // < ! internal blocked operator "A" generated by AFact_
359 RCP<BlockedCrsMatrix> bA_;
360 RCP<const Thyra::BlockedLinearOpBase<Scalar>> bThyOp_;
361
363 RCP<ParameterList> tekoParams_; // < ! parameter list containing Teko parameters. These parameters are not administrated by the factory and not validated.
364
365 Teko::LinearOp inverseOp_; // < ! Teko inverse operator
366}; // class TekoSmoother (specialization on SC=double)
367} // namespace MueLu
368
369#define MUELU_TEKOSMOOTHER_SHORT
370
371#endif // HAVE_MUELU_TEKO
372
373#endif /* MUELU_TEKOSMOOTHER_DECL_HPP_ */
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultGlobalOrdinal GlobalOrdinal
MueLu::DefaultNode Node
virtual std::string description() const
Return a simple one-line description of this object.
Exception indicating invalid cast attempted.
Exception throws to report errors in the internal logical of the program.
void Input(Level &level, const std::string &varName) const
Timer to be used in factories. Similar to Monitor but with additional timers.
Class that holds all level-specific information.
virtual const Teuchos::ParameterList & GetParameterList() const =0
void SetParameter(const std::string &name, const ParameterEntry &entry)
Set a parameter directly as a ParameterEntry.
Base class for smoother prototypes.
bool IsSetup() const
Get the state of a smoother prototype.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
RCP< const Thyra::BlockedLinearOpBase< Scalar > > bThyOp_
std::string description() const
Return a simple one-line description of this object.
RCP< const ParameterList > GetValidParameterList() const
Input.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
TekoSmoother(const Teuchos::ParameterList &paramList)
Constructor.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Interface to block smoothers in Teko package.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
std::string description() const
Return a simple one-line description of this object.
virtual ~TekoSmoother()
Destructor.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
RCP< const ParameterList > GetValidParameterList() const
Input.
std::string type_
smoother type
void Setup(Level &currentLevel)
Setup routine.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
TekoSmoother(const Teuchos::ParameterList &paramList)
void SetTekoParameters(RCP< ParameterList > tekoParams)
RCP< SmootherPrototype > Copy() const
void DeclareInput(Level &currentLevel) const
Input.
static RCP< MultiVector > Residual(const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const MultiVector &X, const MultiVector &RHS)
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Namespace for MueLu classes and methods.
@ Warnings0
Important warning messages (one line)
@ Debug
Print additional debugging information.
@ Parameters0
Print class parameters.