MueLu  Version of the Day
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 
60 #include <Teuchos_ParameterList.hpp>
61 
62 #include <Xpetra_MapExtractor_fwd.hpp>
63 
65 #include "MueLu_SmootherPrototype.hpp"
69 #include "MueLu_Monitor.hpp"
70 
71 namespace MueLu {
72 
82  template <class Scalar = SmootherPrototype<>::scalar_type,
83  class LocalOrdinal = typename SmootherPrototype<Scalar>::local_ordinal_type,
86  class TekoSmoother : public SmootherPrototype<Scalar,LocalOrdinal,GlobalOrdinal,Node>
87  {
88  typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
89 
90 #undef MUELU_TEKOSMOOTHER_SHORT
91 #include "MueLu_UseShortNames.hpp"
92 
93  public:
94 
96 
97 
100  TekoSmoother() : type_("Teko smoother") {
101  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.");
102  };
103 
105  virtual ~TekoSmoother() { }
107 
109 
110  RCP<const ParameterList> GetValidParameterList() const {
111  RCP<ParameterList> validParamList = rcp(new ParameterList());
112  return validParamList;
113  }
114 
115  void DeclareInput(Level &currentLevel) const { }
116 
117  void SetTekoParameters(RCP<ParameterList> tekoParams) { };
119 
121 
122 
125  void Setup(Level &currentLevel) { }
126 
133  void Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero = false) const { }
135 
136  RCP<SmootherPrototype> Copy() const { return Teuchos::null; }
137 
139 
140 
142  std::string description() const {
143  std::ostringstream out;
145  out << "{type = " << type_ << "}";
146  return out.str();
147  }
148 
150  //using MueLu::Describable::describe; // overloading, not hiding
151  void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
153 
154  if (verbLevel & Parameters0)
155  out0 << "Prec. type: " << type_ << std::endl;
156 
157  if (verbLevel & Debug)
158  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
159  }
160 
162  size_t getNodeSmootherComplexity() const;
163 
165 
166  private:
167 
169  std::string type_;
170  }; // class TekoSmoother
171 
172 
188  template <class GlobalOrdinal,
189  class Node>
190  class TekoSmoother<double,int,GlobalOrdinal,Node> : public SmootherPrototype<double,int,GlobalOrdinal,Node>
191  {
192  typedef int LocalOrdinal;
193  typedef double Scalar;
194  typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractorClass;
195 
196 #undef MUELU_TEKOSMOOTHER_SHORT
197 #include "MueLu_UseShortNames.hpp"
198 
199  public:
200 
202 
203 
206  TekoSmoother() : type_("Teko smoother"), A_(Teuchos::null), bA_(Teuchos::null), bThyOp_(Teuchos::null), tekoParams_(Teuchos::null), inverseOp_(Teuchos::null) { };
207 
209  virtual ~TekoSmoother() { }
211 
213 
214  RCP<const ParameterList> GetValidParameterList() const {
215  RCP<ParameterList> validParamList = rcp(new ParameterList());
216 
217  validParamList->set< RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A");
218  validParamList->set< std::string > ("Inverse Type", "", "Name of parameter list within 'Teko parameters' containing the Teko smoother parameters.");
219 
220  return validParamList;
221  }
222 
223  void DeclareInput(Level &currentLevel) const {
224  this->Input(currentLevel, "A");
225  }
226 
227  void SetTekoParameters(RCP<ParameterList> tekoParams) { tekoParams_ = tekoParams; };
229 
231 
232 
235  void Setup(Level &currentLevel) {
236  RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
237 
238  FactoryMonitor m(*this, "Setup TekoSmoother", currentLevel);
239  if (this->IsSetup() == true)
240  this->GetOStream(Warnings0) << "MueLu::TekoSmoother::Setup(): Setup() has already been called";
241 
242  // extract blocked operator A from current level
243  A_ = Factory::Get< RCP<Matrix> >(currentLevel, "A"); // A needed for extracting map extractors
244  bA_ = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(A_);
245  TEUCHOS_TEST_FOR_EXCEPTION(bA_.is_null(), Exceptions::BadCast,
246  "MueLu::TekoSmoother::Build: input matrix A is not of type BlockedCrsMatrix.");
247 
248  bThyOp_ = bA_->getThyraOperator();
249  TEUCHOS_TEST_FOR_EXCEPTION(bThyOp_.is_null(), Exceptions::BadCast,
250  "MueLu::TekoSmoother::Build: Could not extract thyra operator from BlockedCrsMatrix.");
251 
252  Teuchos::RCP<const Thyra::LinearOpBase<Scalar> > thyOp = Teuchos::rcp_dynamic_cast<const Thyra::LinearOpBase<Scalar> >(bThyOp_);
253  TEUCHOS_TEST_FOR_EXCEPTION(thyOp.is_null(), Exceptions::BadCast,
254  "MueLu::TekoSmoother::Build: Downcast of Thyra::BlockedLinearOpBase to Teko::LinearOp failed.");
255 
256  // parameter list contains TekoSmoother parameters but does not handle the Teko parameters itself!
257  const ParameterList& pL = Factory::GetParameterList();
258  std::string smootherType = pL.get<std::string>("Inverse Type");
259  TEUCHOS_TEST_FOR_EXCEPTION(smootherType.empty(), Exceptions::RuntimeError,
260  "MueLu::TekoSmoother::Build: You must provide a 'Smoother Type' name that is defined in the 'Teko parameters' sublist.");
261  type_ = smootherType;
262 
263  TEUCHOS_TEST_FOR_EXCEPTION(tekoParams_.is_null(), Exceptions::BadCast,
264  "MueLu::TekoSmoother::Build: No Teko parameters have been set.");
265 
266  Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*tekoParams_);
267  Teuchos::RCP<Teko::InverseFactory> inverse = invLib->getInverseFactory(smootherType);
268 
269  inverseOp_ = Teko::buildInverse(*inverse, thyOp);
270  TEUCHOS_TEST_FOR_EXCEPTION(inverseOp_.is_null(), Exceptions::BadCast,
271  "MueLu::TekoSmoother::Build: Failed to build Teko inverse operator. Probably a problem with the Teko parameters.");
272 
273  this->IsSetup(true);
274  }
275 
282  void Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero = false) const {
283  TEUCHOS_TEST_FOR_EXCEPTION(this->IsSetup() == false, Exceptions::RuntimeError,
284  "MueLu::TekoSmoother::Apply(): Setup() has not been called");
285 
286  Teuchos::RCP<const Teuchos::Comm<int> > comm = X.getMap()->getComm();
287 
288  Teuchos::RCP<const MapExtractor> rgMapExtractor = bA_->getRangeMapExtractor();
289  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
290 
291  // copy initial solution vector X to Ptr<Thyra::MultiVectorBase> YY
292 
293  // create a Thyra RHS vector
294  Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyB = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productRange()),Teuchos::as<int>(B.getNumVectors()));
295  Teuchos::RCP<Thyra::ProductMultiVectorBase<Scalar> > thyProdB =
296  Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyB);
297  TEUCHOS_TEST_FOR_EXCEPTION(thyProdB.is_null(), Exceptions::BadCast,
298  "MueLu::TekoSmoother::Apply: Failed to cast range space to product range space.");
299 
300  // copy RHS vector B to Thyra::MultiVectorBase thyProdB
301  Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::updateThyra(Teuchos::rcpFromRef(B), rgMapExtractor, thyProdB);
302 
303  // create a Thyra SOL vector
304  Teuchos::RCP<Thyra::MultiVectorBase<Scalar> > thyX = Thyra::createMembers(Teuchos::rcp_dynamic_cast<const Thyra::VectorSpaceBase<Scalar> >(bThyOp_->productDomain()),Teuchos::as<int>(X.getNumVectors()));
305  Teuchos::RCP<Thyra::ProductMultiVectorBase<Scalar> > thyProdX =
306  Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(thyX);
307  TEUCHOS_TEST_FOR_EXCEPTION(thyProdX.is_null(), Exceptions::BadCast,
308  "MueLu::TekoSmoother::Apply: Failed to cast domain space to product domain space.");
309 
310  // copy RHS vector X to Thyra::MultiVectorBase thyProdX
311  Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::updateThyra(Teuchos::rcpFromRef(X), rgMapExtractor, thyProdX);
312 
313  inverseOp_->apply(
314  Thyra::NOTRANS,
315  *thyB, //const MultiVectorBase<Scalar> &X,
316  thyX.ptr(), //const Ptr<MultiVectorBase<Scalar> > &Y,
317  1.0,
318  0.0);
319 
320  // copy back content of Ptr<Thyra::MultiVectorBase> thyX into X
321  Teuchos::RCP<Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > XX =
322  Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toXpetra(thyX, comm);
323 
324  X.update(Teuchos::ScalarTraits<Scalar>::one(), *XX, Teuchos::ScalarTraits<Scalar>::zero());
325  }
327 
328  RCP<SmootherPrototype> Copy() const { return Teuchos::rcp (new MueLu::TekoSmoother<double,int,GlobalOrdinal,Node> (*this)); }
329 
331 
332 
334  std::string description() const {
335  std::ostringstream out;
337  out << "{type = " << type_ << "}";
338  return out.str();
339  }
340 
342  //using MueLu::Describable::describe; // overloading, not hiding
343  void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel = Default) const {
345 
346  if (verbLevel & Parameters0)
347  out0 << "Prec. type: " << type_ << std::endl;
348 
349  if (verbLevel & Debug)
350  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
351  }
352 
354  size_t getNodeSmootherComplexity() const {size_t cplx=0; return cplx;}
355 
357 
358  private:
359 
361  std::string type_;
362 
364  RCP<Matrix> A_; // < ! internal blocked operator "A" generated by AFact_
365  RCP<BlockedCrsMatrix> bA_;
366  RCP<const Thyra::BlockedLinearOpBase<Scalar> > bThyOp_;
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)
Exception indicating invalid cast attempted.
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
RCP< SmootherPrototype > Copy() const
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
bool IsSetup() const
Get the state of a smoother prototype.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
RCP< const ParameterList > GetValidParameterList() const
Input.
Timer to be used in factories. Similar to Monitor but with additional timers.
virtual ~TekoSmoother()
Destructor.
Base class for smoother prototypes.
Print additional debugging information.
Namespace for MueLu classes and methods.
std::string description() const
Return a simple one-line description of this object.
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply the Teko smoother.
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)
std::string description() const
Return a simple one-line description of this object.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
std::string type_
smoother type
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
void DeclareInput(Level &currentLevel) const
Input.
virtual const Teuchos::ParameterList & GetParameterList() const
Print class parameters.
RCP< const ParameterList > GetValidParameterList() const
Input.
void Setup(Level &currentLevel)
Setup routine.
void Input(Level &level, const std::string &varName) const
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=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
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
virtual std::string description() const
Return a simple one-line description of this object.
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
int inverse(const Epetra_SerialDenseMatrix &, Epetra_SerialDenseMatrix &)
Interface to block smoothers in Teko package.