MueLu  Version of the Day
MueLu_TrilinosSmoother_def.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 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_TRILINOSSMOOTHER_DEF_HPP
47 #define MUELU_TRILINOSSMOOTHER_DEF_HPP
48 
49 #include <Xpetra_Map.hpp>
50 #include <Xpetra_Matrix.hpp>
51 
53 
54 #include "MueLu_Level.hpp"
55 #include "MueLu_IfpackSmoother.hpp"
56 #include "MueLu_Ifpack2Smoother.hpp"
57 #include "MueLu_Exceptions.hpp"
58 
59 namespace MueLu {
60 
61  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
62  TrilinosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TrilinosSmoother(const std::string& type, const Teuchos::ParameterList& paramListIn, const LO& overlap)
63  : type_(type), overlap_(overlap)
64  {
65  // The original idea behind all smoothers was to use prototype pattern. However, it does not fully work of the dependencies
66  // calculation. Particularly, we need to propagate DeclareInput to proper prototypes. Therefore, both TrilinosSmoother and
67  // DirectSolver do not follow the pattern exactly.
68  // The difference is that in order to propagate the calculation of dependencies, we need to call a DeclareInput on a
69  // constructed object (or objects, as we have two different code branches for Epetra and Tpetra). The only place where we
70  // could construct these objects is the constructor. Thus, we need to store RCPs, and both TrilinosSmoother and DirectSolver
71  // obtain a state: they contain RCP to smoother prototypes.
72  sEpetra_ = Teuchos::null;
73  sTpetra_ = Teuchos::null;
74 
75  TEUCHOS_TEST_FOR_EXCEPTION(overlap_ < 0, Exceptions::RuntimeError, "Overlap parameter is negative (" << overlap << ")");
76 
77  // paramList contains only parameters which are understood by Ifpack/Ifpack2
78  ParameterList paramList = paramListIn;
79 
80 
81  // We want TrilinosSmoother to be able to work with both Epetra and Tpetra objects, therefore we try to construct both
82  // Ifpack and Ifpack2 smoother prototypes. The construction really depends on configuration options.
83  triedEpetra_ = triedTpetra_ = false;
84 #if defined(HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_IFPACK2)
85  try {
86  sTpetra_ = rcp(new Ifpack2Smoother(type_, paramList, overlap_));
87  TEUCHOS_TEST_FOR_EXCEPTION(sTpetra_.is_null(), Exceptions::RuntimeError, "Unable to construct Ifpack2 smoother");
88  } catch (Exceptions::RuntimeError& e) {
89  errorTpetra_ = e.what();
90  }
91  triedTpetra_ = true;
92 #endif
93 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_IFPACK)
94  try {
95  // GetIfpackSmoother masks the template argument matching, and simply throws if template arguments are incompatible with Epetra
97  TEUCHOS_TEST_FOR_EXCEPTION(sEpetra_.is_null(), Exceptions::RuntimeError, "Unable to construct Ifpack smoother");
98  } catch (Exceptions::RuntimeError& e) {
99  // IfpackSmoother throws if Scalar != double, LocalOrdinal != int, GlobalOrdinal != int
100  errorEpetra_ = e.what();
101  }
102  triedEpetra_ = true;
103 #endif
104 
105  // Check if we were able to construct at least one smoother. In many cases that's all we need, for instance if a user
106  // simply wants to use Tpetra only stack, never enables Ifpack, and always runs Tpetra objects.
107  TEUCHOS_TEST_FOR_EXCEPTION(!triedEpetra_ && !triedTpetra_, Exceptions::RuntimeError, "Unable to construct any smoother."
108  "Plase enable (TPETRA and IFPACK2) or (EPETRA and IFPACK)");
109 
110  TEUCHOS_TEST_FOR_EXCEPTION(sEpetra_.is_null() && sTpetra_.is_null(), Exceptions::RuntimeError,
111  "Could not enable any smoother:\n"
112  << (triedEpetra_ ? "Epetra mode was disabled due to an error:\n" : "")
113  << (triedEpetra_ ? errorEpetra_ : "")
114  << (triedTpetra_ ? "Tpetra mode was disabled due to an error:\n" : "")
115  << (triedTpetra_ ? errorTpetra_ : ""));
116 
117  this->SetParameterList(paramList);
118  }
119 
120  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
121  void TrilinosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetFactory(const std::string& varName, const RCP<const FactoryBase>& factory) {
122  // We need to propagate SetFactory to proper place
123  if (!sEpetra_.is_null()) sEpetra_->SetFactory(varName, factory);
124  if (!sTpetra_.is_null()) sTpetra_->SetFactory(varName, factory);
125  }
126 
127  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
129  // Decide whether we are running in Epetra or Tpetra mode
130  //
131  // Theoretically, we could make this decision in the constructor, and create only
132  // one of the smoothers. But we want to be able to reuse, so one can imagine a scenario
133  // where one first runs hierarchy with tpetra matrix, and then with epetra.
134  bool useTpetra = (currentLevel.lib() == Xpetra::UseTpetra);
135  s_ = (useTpetra ? sTpetra_ : sEpetra_);
136  if (s_.is_null()) {
137  if (useTpetra) {
138 #if not defined(HAVE_MUELU_IFPACK22)
139  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError,
140  "Error: running in Tpetra mode, but MueLu with Ifpack2 was disabled during the configure stage.\n"
141  "Please make sure that:\n"
142  " - Ifpack2 is enabled (Trilinos_ENABLE_Ifpack2=ON),\n"
143  " - Ifpack2 is available for MueLu to use (MueLu_ENABLE_Ifpack2=ON)\n");
144 #else
145  if (triedTpetra_)
146  this->GetOStream(Errors) << "Tpetra mode was disabled due to an error:\n" << errorTpetra_ << std::endl;
147 #endif
148  }
149  if (!useTpetra) {
150 #if not defined(HAVE_MUELU_IFPACK)
151  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError,
152  "Error: running in Epetra mode, but MueLu with Ifpack was disabled during the configure stage.\n"
153  "Please make sure that:\n"
154  " - Ifpack is enabled (you can do that with Trilinos_ENABLE_Ifpack=ON),\n"
155  " - Ifpack is available for MueLu to use (MueLu_ENABLE_Ifpack=ON)\n");
156 #else
157  if (triedEpetra_)
158  this->GetOStream(Errors) << "Epetra mode was disabled due to an error:\n" << errorEpetra_ << std::endl;
159 #endif
160  }
161  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError,
162  "Smoother for " << (useTpetra ? "Tpetra" : "Epetra") << " was not constructed");
163  }
164 
165  s_->DeclareInput(currentLevel);
166  }
167 
168  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
170  if (SmootherPrototype::IsSetup() == true)
171  this->GetOStream(Warnings0) << "MueLu::TrilinosSmoother::Setup(): Setup() has already been called";
172 
173  int oldRank = s_->SetProcRankVerbose(this->GetProcRankVerbose());
174 
175  s_->Setup(currentLevel);
176 
177  s_->SetProcRankVerbose(oldRank);
178 
180 
181  this->SetParameterList(s_->GetParameterList());
182  }
183 
184  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
185  void TrilinosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
186  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::TrilinosSmoother::Apply(): Setup() has not been called");
187 
188  s_->Apply(X, B, InitialGuessIsZero);
189  }
190 
191  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
192  RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
194  RCP<TrilinosSmoother> newSmoo = rcp(new TrilinosSmoother(type_, this->GetParameterList(), overlap_));
195 
196  // We need to be quite careful with Copy
197  // We still want TrilinosSmoother to follow Prototype Pattern, so we need to hide the fact that we do have some state
198  if (!sEpetra_.is_null())
199  newSmoo->sEpetra_ = sEpetra_->Copy();
200  if (!sTpetra_.is_null())
201  newSmoo->sTpetra_ = sTpetra_->Copy();
202 
203  // Copy the default mode
204  newSmoo->s_ = (s_.get() == sTpetra_.get() ? newSmoo->sTpetra_ : newSmoo->sEpetra_);
205  newSmoo->SetParameterList(this->GetParameterList());
206 
207  return newSmoo;
208  }
209 
210  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
212  if (type == "RELAXATION") { return "point relaxation stand-alone"; }
213  if (type == "CHEBYSHEV") { return "Chebyshev"; }
214  if (type == "ILUT") { return "ILUT"; }
215  if (type == "RILUK") { return "ILU"; }
216  if (type == "ILU") { return "ILU"; }
217  if (type == "Amesos") { return "Amesos"; }
218 
219  // special types
220 
221  // Note: for Ifpack there is no distinction between block and banded relaxation as there is no BandedContainer.
222  if (type == "LINESMOOTHING_BLOCKRELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
223  if (type == "LINESMOOTHING_BLOCK RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
224  if (type == "LINESMOOTHING_BLOCK_RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
225  if (type == "LINESMOOTHING_BANDEDRELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
226  if (type == "LINESMOOTHING_BANDED RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
227  if (type == "LINESMOOTHING_BANDED_RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
228 
229  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "Cannot convert Ifpack2 preconditioner name to Ifpack: unknown type: " + type);
230  }
231 
232  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
233  Teuchos::ParameterList TrilinosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Ifpack2ToIfpack1Param(const Teuchos::ParameterList& ifpack2List) {
234  Teuchos::ParameterList ifpack1List = ifpack2List;
235 
236  if (ifpack2List.isParameter("relaxation: type") && ifpack2List.get<std::string>("relaxation: type") == "Symmetric Gauss-Seidel")
237  ifpack1List.set("relaxation: type", "symmetric Gauss-Seidel");
238 
239  if (ifpack2List.isParameter("fact: iluk level-of-fill")) {
240  ifpack1List.remove("fact: iluk level-of-fill");
241  ifpack1List.set("fact: level-of-fill", ifpack2List.get<int>("fact: iluk level-of-fill"));
242  }
243 
244  return ifpack1List;
245  }
246 
247  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
249  std::ostringstream out;
250  if (s_ != Teuchos::null) {
251  out << s_->description();
252  } else {
254  out << "{type = " << type_ << "}";
255  }
256  return out.str();
257  }
258 
259  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
260  void TrilinosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
262 
263  if (verbLevel & Parameters0)
264  out0 << "Prec. type: " << type_ << std::endl;
265 
266  if (verbLevel & Parameters1) {
267  out0 << "PrecType: " << type_ << std::endl;
268  out0 << "Parameter list: " << std::endl;
269  Teuchos::OSTab tab2(out);
270  out << this->GetParameterList();
271  out0 << "Overlap: " << overlap_ << std::endl;
272  }
273 
274  if (verbLevel & Debug) {
275  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
276  << "-" << std::endl
277  << "Epetra PrecType: " << Ifpack2ToIfpack1Type(type_) << std::endl
278  << "Epetra Parameter list: " << std::endl;
279  Teuchos::OSTab tab2(out);
280  out << Ifpack2ToIfpack1Param(this->GetParameterList());;
281  }
282  }
283 
284 } // namespace MueLu
285 
286 #endif // MUELU_TRILINOSSMOOTHER_DEF_HPP
Important warning messages (one line)
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
std::string description() const
Return a simple one-line description of this object.
static std::string Ifpack2ToIfpack1Type(const std::string &type)
Convert an Ifpack2 preconditioner name to Ifpack.
static Teuchos::ParameterList Ifpack2ToIfpack1Param(const Teuchos::ParameterList &ifpack2List)
Convert an Ifpack2 parameter list to Ifpack.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Print the object with some verbosity level to an FancyOStream object.
Print additional debugging information.
Namespace for MueLu classes and methods.
virtual const Teuchos::ParameterList & GetParameterList() const
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
TrilinosSmoother cannot be applied. Apply() always returns a RuntimeError exception.
void Setup(Level &currentLevel)
TrilinosSmoother cannot be turned into a smoother using Setup(). Setup() always returns a RuntimeErro...
friend class TrilinosSmoother
Friend declaration required for clone() functionality.
virtual void SetParameterList(const ParameterList &paramList)
Set parameters from a parameter list and return with default values.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
std::string type_
ifpack1/2-specific key phrase that denote smoother type
bool IsSetup() const
Get the state of a smoother prototype.
Xpetra::UnderlyingLib lib()
RCP< SmootherPrototype > Copy() const
When this prototype is cloned using Copy(), the clone is an Ifpack or an Ifpack2 smoother.
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Print class parameters.
void DeclareInput(Level &currentLevel) const
Input.
RCP< SmootherPrototype > sEpetra_
Smoother.
Print class parameters (more parameters, more verbose)
Exception throws to report errors in the internal logical of the program.
LO overlap_
overlap when using the smoother in additive Schwarz mode
Class that encapsulates Ifpack2 smoothers.
virtual std::string description() const
Return a simple one-line description of this object.
int GetProcRankVerbose() const
Get proc rank used for printing. Do not use this information for any other purpose.
void SetFactory(const std::string &varName, const RCP< const FactoryBase > &factory)
Custom SetFactory.