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
64 virtual ~TekoSmoother() {}
66
68
69 RCP<const ParameterList> GetValidParameterList() const {
70 RCP<ParameterList> validParamList = rcp(new ParameterList());
71 return validParamList;
72 }
73
74 void DeclareInput(Level &currentLevel) const {}
75
76 void SetTekoParameters(RCP<ParameterList> tekoParams){};
78
80
81
84 void Setup(Level &currentLevel) {}
85
92 void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero = false) const {}
94
95 RCP<SmootherPrototype> Copy() const { return Teuchos::null; }
96
98
99
101 std::string description() const {
102 std::ostringstream out;
104 out << "{type = " << type_ << "}";
105 return out.str();
106 }
107
109 // using MueLu::Describable::describe; // overloading, not hiding
110 void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
112
113 if (verbLevel & Parameters0)
114 out0 << "Prec. type: " << type_ << std::endl;
115
116 if (verbLevel & Debug)
117 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
118 }
119
121 size_t getNodeSmootherComplexity() const;
122
124
125 private:
127 std::string type_;
128}; // class TekoSmoother
129
144template <class GlobalOrdinal,
145 class Node>
146class TekoSmoother<double, int, GlobalOrdinal, Node> : public SmootherPrototype<double, int, GlobalOrdinal, Node> {
147 typedef int LocalOrdinal;
148 typedef double Scalar;
149 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
150
151#undef MUELU_TEKOSMOOTHER_SHORT
153
154 public:
156
157
161 : type_("Teko smoother")
162 , A_(Teuchos::null)
163 , bA_(Teuchos::null)
164 , bThyOp_(Teuchos::null)
165 , tekoParams_(Teuchos::null)
166 , inverseOp_(Teuchos::null){};
167
169 virtual ~TekoSmoother() {}
171
173
174 RCP<const ParameterList> GetValidParameterList() const {
175 RCP<ParameterList> validParamList = rcp(new ParameterList());
176
177 validParamList->set<RCP<const FactoryBase>>("A", null, "Generating factory of the matrix A");
178 validParamList->set<std::string>("Inverse Type", "", "Name of parameter list within 'Teko parameters' containing the Teko smoother parameters.");
179
180 return validParamList;
181 }
182
183 void DeclareInput(Level &currentLevel) const {
184 this->Input(currentLevel, "A");
185 }
186
187 void SetTekoParameters(RCP<ParameterList> tekoParams) { tekoParams_ = tekoParams; };
189
191
192
195 void Setup(Level &currentLevel) {
196 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
197
198 FactoryMonitor m(*this, "Setup TekoSmoother", currentLevel);
199 if (this->IsSetup() == true)
200 this->GetOStream(Warnings0) << "MueLu::TekoSmoother::Setup(): Setup() has already been called";
201
202 // extract blocked operator A from current level
203 A_ = Factory::Get<RCP<Matrix>>(currentLevel, "A"); // A needed for extracting map extractors
204 bA_ = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
205 TEUCHOS_TEST_FOR_EXCEPTION(bA_.is_null(), Exceptions::BadCast,
206 "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
207
208 bThyOp_ = bA_->getThyraOperator();
209 TEUCHOS_TEST_FOR_EXCEPTION(bThyOp_.is_null(), Exceptions::BadCast,
210 "MueLu::TekoSmoother::Build: Could not extract thyra operator from BlockedCrsMatrix.");
211
212 Teuchos::RCP<const Thyra::LinearOpBase<Scalar>> thyOp = Teuchos::rcp_dynamic_cast<const Thyra::LinearOpBase<Scalar>>(bThyOp_);
213 TEUCHOS_TEST_FOR_EXCEPTION(thyOp.is_null(), Exceptions::BadCast,
214 "MueLu::TekoSmoother::Build: Downcast of Thyra::BlockedLinearOpBase to Teko::LinearOp failed.");
215
216 // parameter list contains TekoSmoother parameters but does not handle the Teko parameters itself!
217 const ParameterList &pL = Factory::GetParameterList();
218 std::string smootherType = pL.get<std::string>("Inverse Type");
219 TEUCHOS_TEST_FOR_EXCEPTION(smootherType.empty(), Exceptions::RuntimeError,
220 "MueLu::TekoSmoother::Build: You must provide a 'Smoother Type' name that is defined in the 'Teko parameters' sublist.");
221 type_ = smootherType;
222
223 TEUCHOS_TEST_FOR_EXCEPTION(tekoParams_.is_null(), Exceptions::BadCast,
224 "MueLu::TekoSmoother::Build: No Teko parameters have been set.");
225
226 Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*tekoParams_);
227 Teuchos::RCP<Teko::InverseFactory> inverse = invLib->getInverseFactory(smootherType);
228
229 inverseOp_ = Teko::buildInverse(*inverse, thyOp);
230 TEUCHOS_TEST_FOR_EXCEPTION(inverseOp_.is_null(), Exceptions::BadCast,
231 "MueLu::TekoSmoother::Build: Failed to build Teko inverse operator. Probably a problem with the Teko parameters.");
232
233 this->IsSetup(true);
234 }
235
242 void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero = false) const {
243 TEUCHOS_TEST_FOR_EXCEPTION(!this->IsSetup(), Exceptions::RuntimeError,
244 "MueLu::TekoSmoother::Apply(): Setup() has not been called");
245
246 auto comm = X.getMap()->getComm();
247 auto rgMapExtractor = bA_->getRangeMapExtractor();
248 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
249
250 using STS = Teuchos::ScalarTraits<Scalar>;
251
252 auto createProductMultiVector = [](const Teuchos::RCP<const Thyra::VectorSpaceBase<Scalar>> &space,
253 int numVecs,
254 const char *castErrorMsg) {
255 auto mv = Thyra::createMembers(space, numVecs);
256 auto prodMv = Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar>>(mv);
257 TEUCHOS_TEST_FOR_EXCEPTION(prodMv.is_null(), Exceptions::BadCast, castErrorMsg);
258 return std::make_pair(mv, prodMv);
259 };
260
261 auto solveWithThyra = [&](const Teuchos::RCP<const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> &rhs,
262 bool initializeFromX,
263 const typename STS::magnitudeType alphaX) {
264 auto [thyB, thyProdB] = createProductMultiVector(
265 Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar>>(bThyOp_->productRange()),
266 Teuchos::as<int>(rhs->getNumVectors()),
267 "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
268
269 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(rhs, rgMapExtractor, thyProdB);
270
271 auto [thyX, thyProdX] = createProductMultiVector(
272 Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar>>(bThyOp_->productDomain()),
273 Teuchos::as<int>(X.getNumVectors()),
274 "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
275
276 if (initializeFromX) {
277 Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
278 }
279
280 inverseOp_->apply(
281 Thyra::NOTRANS,
282 *thyB,
283 thyX.ptr(),
284 STS::one(),
285 STS::zero());
286
287 auto XX = Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyX, comm);
288 X.update(STS::one(), *XX, alphaX);
289 };
290
291 if (InitialGuessIsZero) {
292 solveWithThyra(Teuchos::rcpFromRef(B), /*initializeFromX=*/true, STS::zero());
293 return;
294 }
295
296 auto residual = Utilities::Residual(*A_, X, B);
297 solveWithThyra(residual, /*initializeFromX=*/false, STS::one());
298 }
300
301 RCP<SmootherPrototype> Copy() const { return Teuchos::rcp(new MueLu::TekoSmoother<double, int, GlobalOrdinal, Node>(*this)); }
302
304
305
307 std::string description() const {
308 std::ostringstream out;
310 out << "{type = " << type_ << "}";
311 return out.str();
312 }
313
315 // using MueLu::Describable::describe; // overloading, not hiding
316 void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
318
319 if (verbLevel & Parameters0)
320 out0 << "Prec. type: " << type_ << std::endl;
321
322 if (verbLevel & Debug)
323 out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
324 }
325
328 size_t cplx = 0;
329 return cplx;
330 }
331
333
334 private:
336 std::string type_;
337
339 RCP<Matrix> A_; // < ! internal blocked operator "A" generated by AFact_
340 RCP<BlockedCrsMatrix> bA_;
341 RCP<const Thyra::BlockedLinearOpBase<Scalar>> bThyOp_;
342
344 RCP<ParameterList> tekoParams_; // < ! parameter list containing Teko parameters. These parameters are not administrated by the factory and not validated.
345
346 Teko::LinearOp inverseOp_; // < ! Teko inverse operator
347}; // class TekoSmoother (specialization on SC=double)
348} // namespace MueLu
349
350#define MUELU_TEKOSMOOTHER_SHORT
351
352#endif // HAVE_MUELU_TEKO
353
354#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
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.
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.
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.