MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
23 
25 
27 #include "MueLu_SmootherPrototype.hpp"
29 #include "MueLu_Monitor.hpp"
30 
31 namespace MueLu {
32 
42 template <class Scalar = SmootherPrototype<>::scalar_type,
43  class LocalOrdinal = typename SmootherPrototype<Scalar>::local_ordinal_type,
44  class GlobalOrdinal = typename SmootherPrototype<Scalar, LocalOrdinal>::global_ordinal_type,
45  class Node = typename SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
46 class TekoSmoother : public SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
48 
49 #undef MUELU_TEKOSMOOTHER_SHORT
50 #include "MueLu_UseShortNames.hpp"
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 
70  RCP<ParameterList> validParamList = rcp(new ParameterList());
71  return validParamList;
72  }
73 
74  void DeclareInput(Level &currentLevel) const {}
75 
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 
144 template <class GlobalOrdinal,
145  class Node>
146 class TekoSmoother<double, int, GlobalOrdinal, Node> : public SmootherPrototype<double, int, GlobalOrdinal, Node> {
147  typedef int LocalOrdinal;
148  typedef double Scalar;
150 
151 #undef MUELU_TEKOSMOOTHER_SHORT
152 #include "MueLu_UseShortNames.hpp"
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 
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_);
206  "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
207 
208  bThyOp_ = bA_->getThyraOperator();
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_);
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!
218  std::string smootherType = pL.get<std::string>("Inverse Type");
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 {
244  "MueLu::TekoSmoother::Apply(): Setup() has not been called");
245 
246  Teuchos::RCP<const Teuchos::Comm<int> > comm = X.getMap()->getComm();
247 
248  Teuchos::RCP<const MapExtractor> rgMapExtractor = bA_->getRangeMapExtractor();
249  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
250 
251  // copy initial solution vector X to Ptr<Thyra::MultiVectorBase> YY
252 
253  // create a Thyra RHS vector
254  Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyB = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productRange()), Teuchos::as<int>(B.getNumVectors()));
256  Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyB);
258  "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
259 
260  // copy RHS vector B to Thyra::MultiVectorBase thyProdB
261  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(B), rgMapExtractor, thyProdB);
262 
263  // create a Thyra SOL vector
264  Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyX = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productDomain()), Teuchos::as<int>(X.getNumVectors()));
266  Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyX);
268  "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
269 
270  // copy RHS vector X to Thyra::MultiVectorBase thyProdX
271  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
272 
273  inverseOp_->apply(
274  Thyra::NOTRANS,
275  *thyB, // const MultiVectorBase<Scalar> &X,
276  thyX.ptr(), // const Ptr<MultiVectorBase<Scalar> > &Y,
277  1.0,
278  0.0);
279 
280  // copy back content of Ptr<Thyra::MultiVectorBase> thyX into X
282  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyX, comm);
283 
285  }
287 
289 
291 
292 
294  std::string description() const {
295  std::ostringstream out;
297  out << "{type = " << type_ << "}";
298  return out.str();
299  }
300 
302  // using MueLu::Describable::describe; // overloading, not hiding
303  void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
305 
306  if (verbLevel & Parameters0)
307  out0 << "Prec. type: " << type_ << std::endl;
308 
309  if (verbLevel & Debug)
310  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
311  }
312 
314  size_t getNodeSmootherComplexity() const {
315  size_t cplx = 0;
316  return cplx;
317  }
318 
320 
321  private:
323  std::string type_;
324 
326  RCP<Matrix> A_; // < ! internal blocked operator "A" generated by AFact_
329 
331  RCP<ParameterList> tekoParams_; // < ! parameter list containing Teko parameters. These parameters are not administrated by the factory and not validated.
332 
333  Teko::LinearOp inverseOp_; // < ! Teko inverse operator
334 }; // class TekoSmoother (specialization on SC=double)
335 } // namespace MueLu
336 
337 #define MUELU_TEKOSMOOTHER_SHORT
338 
339 #endif // HAVE_MUELU_TEKO
340 
341 #endif /* MUELU_TEKOSMOOTHER_DECL_HPP_ */
Important warning messages (one line)
virtual const Teuchos::ParameterList & GetParameterList() const
std::string description() const
Return a simple one-line description of this object.
Exception indicating invalid cast attempted.
MueLu::DefaultLocalOrdinal LocalOrdinal
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
RCP< const ParameterList > GetValidParameterList() const
Input.
bool is_null(const std::shared_ptr< T > &p)
RCP< SmootherPrototype > Copy() const
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual ~TekoSmoother()
Destructor.
Base class for smoother prototypes.
Print additional debugging information.
std::string description() const
Return a simple one-line description of this object.
MueLu::DefaultNode Node
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
void SetTekoParameters(RCP< ParameterList > tekoParams)
RCP< const ParameterList > GetValidParameterList() const
Input.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
Ptr< T > ptr() const
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
bool IsSetup() const
Get the state of a smoother prototype.
std::string type_
smoother type
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Print class parameters.
void DeclareInput(Level &currentLevel) const
Input.
void Setup(Level &currentLevel)
Setup routine.
void Apply(MultiVector &X, const MultiVector &B, bool=false) const
Apply the Teko smoother.
RCP< const Thyra::BlockedLinearOpBase< Scalar > > bThyOp_
Exception throws to report errors in the internal logical of the program.
Xpetra::MapExtractor< Scalar, LocalOrdinal, GlobalOrdinal, Node > MapExtractorClass
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
void Input(Level &level, const std::string &varName) const
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
virtual std::string description() const
Return a simple one-line description of this object.
std::string toString(const T &t)
int inverse(const Epetra_SerialDenseMatrix &, Epetra_SerialDenseMatrix &)
Interface to block smoothers in Teko package.
bool is_null() const