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 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 // Tobias Wiesner (tawiesn@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 
48 #ifndef MUELU_TEKOSMOOTHER_DECL_HPP_
49 #define MUELU_TEKOSMOOTHER_DECL_HPP_
50 
51 #ifdef HAVE_MUELU_TEKO
52 
53 #include "Teko_Utilities.hpp"
54 
55 #include "Teko_InverseLibrary.hpp"
56 #include "Teko_InverseFactory.hpp"
57 
58 #include "MueLu_ConfigDefs.hpp"
59 
61 
63 
65 #include "MueLu_SmootherPrototype.hpp"
67 #include "MueLu_Monitor.hpp"
68 
69 namespace MueLu {
70 
80 template <class Scalar = SmootherPrototype<>::scalar_type,
81  class LocalOrdinal = typename SmootherPrototype<Scalar>::local_ordinal_type,
82  class GlobalOrdinal = typename SmootherPrototype<Scalar, LocalOrdinal>::global_ordinal_type,
83  class Node = typename SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal>::node_type>
84 class TekoSmoother : public SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
86 
87 #undef MUELU_TEKOSMOOTHER_SHORT
88 #include "MueLu_UseShortNames.hpp"
89 
90  public:
92 
93 
97  : type_("Teko smoother") {
98  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.");
99  };
100 
102  virtual ~TekoSmoother() {}
104 
106 
108  RCP<ParameterList> validParamList = rcp(new ParameterList());
109  return validParamList;
110  }
111 
112  void DeclareInput(Level &currentLevel) const {}
113 
116 
118 
119 
122  void Setup(Level &currentLevel) {}
123 
130  void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero = false) const {}
132 
133  RCP<SmootherPrototype> Copy() const { return Teuchos::null; }
134 
136 
137 
139  std::string description() const {
140  std::ostringstream out;
142  out << "{type = " << type_ << "}";
143  return out.str();
144  }
145 
147  // using MueLu::Describable::describe; // overloading, not hiding
148  void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
150 
151  if (verbLevel & Parameters0)
152  out0 << "Prec. type: " << type_ << std::endl;
153 
154  if (verbLevel & Debug)
155  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
156  }
157 
159  size_t getNodeSmootherComplexity() const;
160 
162 
163  private:
165  std::string type_;
166 }; // class TekoSmoother
167 
182 template <class GlobalOrdinal,
183  class Node>
184 class TekoSmoother<double, int, GlobalOrdinal, Node> : public SmootherPrototype<double, int, GlobalOrdinal, Node> {
185  typedef int LocalOrdinal;
186  typedef double Scalar;
188 
189 #undef MUELU_TEKOSMOOTHER_SHORT
190 #include "MueLu_UseShortNames.hpp"
191 
192  public:
194 
195 
199  : type_("Teko smoother")
200  , A_(Teuchos::null)
201  , bA_(Teuchos::null)
202  , bThyOp_(Teuchos::null)
203  , tekoParams_(Teuchos::null)
204  , inverseOp_(Teuchos::null){};
205 
207  virtual ~TekoSmoother() {}
209 
211 
213  RCP<ParameterList> validParamList = rcp(new ParameterList());
214 
215  validParamList->set<RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
216  validParamList->set<std::string>("Inverse Type", "", "Name of parameter list within 'Teko parameters' containing the Teko smoother parameters.");
217 
218  return validParamList;
219  }
220 
221  void DeclareInput(Level &currentLevel) const {
222  this->Input(currentLevel, "A");
223  }
224 
225  void SetTekoParameters(RCP<ParameterList> tekoParams) { tekoParams_ = tekoParams; };
227 
229 
230 
233  void Setup(Level &currentLevel) {
234  RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
235 
236  FactoryMonitor m(*this, "Setup TekoSmoother", currentLevel);
237  if (this->IsSetup() == true)
238  this->GetOStream(Warnings0) << "MueLu::TekoSmoother::Setup(): Setup() has already been called";
239 
240  // extract blocked operator A from current level
241  A_ = Factory::Get<RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
242  bA_ = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
244  "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
245 
246  bThyOp_ = bA_->getThyraOperator();
248  "MueLu::TekoSmoother::Build: Could not extract thyra operator from BlockedCrsMatrix.");
249 
250  Teuchos::RCP<const Thyra::LinearOpBase<Scalar> > thyOp = Teuchos::rcp_dynamic_cast<const Thyra::LinearOpBase<Scalar> >(bThyOp_);
252  "MueLu::TekoSmoother::Build: Downcast of Thyra::BlockedLinearOpBase to Teko::LinearOp failed.");
253 
254  // parameter list contains TekoSmoother parameters but does not handle the Teko parameters itself!
256  std::string smootherType = pL.get<std::string>("Inverse Type");
258  "MueLu::TekoSmoother::Build: You must provide a 'Smoother Type' name that is defined in the 'Teko parameters' sublist.");
259  type_ = smootherType;
260 
261  TEUCHOS_TEST_FOR_EXCEPTION(tekoParams_.is_null(), Exceptions::BadCast,
262  "MueLu::TekoSmoother::Build: No Teko parameters have been set.");
263 
264  Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*tekoParams_);
265  Teuchos::RCP<Teko::InverseFactory> inverse = invLib->getInverseFactory(smootherType);
266 
267  inverseOp_ = Teko::buildInverse(*inverse, thyOp);
268  TEUCHOS_TEST_FOR_EXCEPTION(inverseOp_.is_null(), Exceptions::BadCast,
269  "MueLu::TekoSmoother::Build: Failed to build Teko inverse operator. Probably a problem with the Teko parameters.");
270 
271  this->IsSetup(true);
272  }
273 
280  void Apply(MultiVector &X, const MultiVector &B, bool /* InitialGuessIsZero */ = false) const {
282  "MueLu::TekoSmoother::Apply(): Setup() has not been called");
283 
284  Teuchos::RCP<const Teuchos::Comm<int> > comm = X.getMap()->getComm();
285 
286  Teuchos::RCP<const MapExtractor> rgMapExtractor = bA_->getRangeMapExtractor();
287  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
288 
289  // copy initial solution vector X to Ptr<Thyra::MultiVectorBase> YY
290 
291  // create a Thyra RHS vector
292  Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyB = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productRange()), Teuchos::as<int>(B.getNumVectors()));
294  Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyB);
296  "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
297 
298  // copy RHS vector B to Thyra::MultiVectorBase thyProdB
299  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(B), rgMapExtractor, thyProdB);
300 
301  // create a Thyra SOL vector
302  Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyX = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productDomain()), Teuchos::as<int>(X.getNumVectors()));
304  Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyX);
306  "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
307 
308  // copy RHS vector X to Thyra::MultiVectorBase thyProdX
309  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
310 
311  inverseOp_->apply(
312  Thyra::NOTRANS,
313  *thyB, // const MultiVectorBase<Scalar> &X,
314  thyX.ptr(), // const Ptr<MultiVectorBase<Scalar> > &Y,
315  1.0,
316  0.0);
317 
318  // copy back content of Ptr<Thyra::MultiVectorBase> thyX into X
320  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(thyX, comm);
321 
323  }
325 
327 
329 
330 
332  std::string description() const {
333  std::ostringstream out;
335  out << "{type = " << type_ << "}";
336  return out.str();
337  }
338 
340  // using MueLu::Describable::describe; // overloading, not hiding
341  void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
343 
344  if (verbLevel & Parameters0)
345  out0 << "Prec. type: " << type_ << std::endl;
346 
347  if (verbLevel & Debug)
348  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
349  }
350 
352  size_t getNodeSmootherComplexity() const {
353  size_t cplx = 0;
354  return cplx;
355  }
356 
358 
359  private:
361  std::string type_;
362 
364  RCP<Matrix> A_; // < ! internal blocked operator "A" generated by AFact_
367 
369  RCP<ParameterList> tekoParams_; // < ! parameter list containing Teko parameters. These parameters are not administrated by the factory and not validated.
370 
371  Teko::LinearOp inverseOp_; // < ! Teko inverse operator
372 }; // class TekoSmoother (specialization on SC=double)
373 } // namespace MueLu
374 
375 #define MUELU_TEKOSMOOTHER_SHORT
376 
377 #endif // HAVE_MUELU_TEKO
378 
379 #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:99
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.
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