MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
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  } catch (Exceptions::BadCast& e) {
91  errorTpetra_ = e.what();
92  }
93  triedTpetra_ = true;
94 #endif
95 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_IFPACK)
96  try {
97  // GetIfpackSmoother masks the template argument matching, and simply throws if template arguments are incompatible with Epetra
99  TEUCHOS_TEST_FOR_EXCEPTION(sEpetra_.is_null(), Exceptions::RuntimeError, "Unable to construct Ifpack smoother");
100  } catch (Exceptions::RuntimeError& e) {
101  // IfpackSmoother throws if Scalar != double, LocalOrdinal != int, GlobalOrdinal != int
102  errorEpetra_ = e.what();
103  }
104  triedEpetra_ = true;
105 #endif
106 
107  // Check if we were able to construct at least one smoother. In many cases that's all we need, for instance if a user
108  // simply wants to use Tpetra only stack, never enables Ifpack, and always runs Tpetra objects.
109  TEUCHOS_TEST_FOR_EXCEPTION(!triedEpetra_ && !triedTpetra_, Exceptions::RuntimeError, "Unable to construct any smoother."
110  "Please enable (TPETRA and IFPACK2) or (EPETRA and IFPACK)");
111 
113  "Could not construct any smoother:\n"
114  << (triedEpetra_ ? "=> Failed to build an Epetra smoother due to the following exception:\n" : "=> Epetra and/or Ifpack are not enabled.\n")
115  << (triedEpetra_ ? errorEpetra_ + "\n" : "")
116  << (triedTpetra_ ? "=> Failed to build a Tpetra smoother due to the following exception:\n" : "=> Tpetra and/or Ifpack2 are not enabled.\n")
117  << (triedTpetra_ ? errorTpetra_ + "\n" : ""));
118 
119  this->SetParameterList(paramList);
120  }
121 
122  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
124  // We need to propagate SetFactory to proper place
125  if (!sEpetra_.is_null()) sEpetra_->SetFactory(varName, factory);
126  if (!sTpetra_.is_null()) sTpetra_->SetFactory(varName, factory);
127  }
128 
129  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
131  // Decide whether we are running in Epetra or Tpetra mode
132  //
133  // Theoretically, we could make this decision in the constructor, and create only
134  // one of the smoothers. But we want to be able to reuse, so one can imagine a scenario
135  // where one first runs hierarchy with tpetra matrix, and then with epetra.
136  bool useTpetra = (currentLevel.lib() == Xpetra::UseTpetra);
137  s_ = (useTpetra ? sTpetra_ : sEpetra_);
138  if (s_.is_null()) {
139  if (useTpetra) {
140 #if not defined(HAVE_MUELU_IFPACK22)
142  "Error: running in Tpetra mode, but MueLu with Ifpack2 was disabled during the configure stage.\n"
143  "Please make sure that:\n"
144  " - Ifpack2 is enabled (Trilinos_ENABLE_Ifpack2=ON),\n"
145  " - Ifpack2 is available for MueLu to use (MueLu_ENABLE_Ifpack2=ON)\n");
146 #else
147  if (triedTpetra_)
148  this->GetOStream(Errors) << "Tpetra mode was disabled due to an error:\n" << errorTpetra_ << std::endl;
149 #endif
150  }
151  if (!useTpetra) {
152 #if not defined(HAVE_MUELU_IFPACK)
154  "Error: running in Epetra mode, but MueLu with Ifpack was disabled during the configure stage.\n"
155  "Please make sure that:\n"
156  " - Ifpack is enabled (you can do that with Trilinos_ENABLE_Ifpack=ON),\n"
157  " - Ifpack is available for MueLu to use (MueLu_ENABLE_Ifpack=ON)\n");
158 #else
159  if (triedEpetra_)
160  this->GetOStream(Errors) << "Epetra mode was disabled due to an error:\n" << errorEpetra_ << std::endl;
161 #endif
162  }
164  "Smoother for " << (useTpetra ? "Tpetra" : "Epetra") << " was not constructed");
165  }
166 
167  s_->DeclareInput(currentLevel);
168  }
169 
170  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
172  if (SmootherPrototype::IsSetup() == true)
173  this->GetOStream(Warnings0) << "MueLu::TrilinosSmoother::Setup(): Setup() has already been called" << std::endl;
174 
175  int oldRank = s_->SetProcRankVerbose(this->GetProcRankVerbose());
176 
177  s_->Setup(currentLevel);
178 
179  s_->SetProcRankVerbose(oldRank);
180 
182 
183  this->SetParameterList(s_->GetParameterList());
184  }
185 
186  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
187  void TrilinosSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
188  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::TrilinosSmoother::Apply(): Setup() has not been called");
189 
190  s_->Apply(X, B, InitialGuessIsZero);
191  }
192 
193  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
196  RCP<TrilinosSmoother> newSmoo = rcp(new TrilinosSmoother(type_, this->GetParameterList(), overlap_));
197 
198  // We need to be quite careful with Copy
199  // We still want TrilinosSmoother to follow Prototype Pattern, so we need to hide the fact that we do have some state
200  if (!sEpetra_.is_null())
201  newSmoo->sEpetra_ = sEpetra_->Copy();
202  if (!sTpetra_.is_null())
203  newSmoo->sTpetra_ = sTpetra_->Copy();
204 
205  // Copy the default mode
206  newSmoo->s_ = (s_.get() == sTpetra_.get() ? newSmoo->sTpetra_ : newSmoo->sEpetra_);
207  newSmoo->SetParameterList(this->GetParameterList());
208 
209  return newSmoo;
210  }
211 
212  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
214  if (type == "RELAXATION") { return "point relaxation stand-alone"; }
215  if (type == "CHEBYSHEV") { return "Chebyshev"; }
216  if (type == "ILUT") { return "ILUT"; }
217  if (type == "RILUK") { return "ILU"; }
218  if (type == "ILU") { return "ILU"; }
219  if (type == "Amesos") { return "Amesos"; }
220 
221  // special types
222 
223  // Note: for Ifpack there is no distinction between block and banded relaxation as there is no
224  // BandedContainer or TridiagonalContainer.
225  if (type == "LINESMOOTHING_BLOCKRELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
226  if (type == "LINESMOOTHING_BLOCK RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
227  if (type == "LINESMOOTHING_BLOCK_RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
228  if (type == "LINESMOOTHING_BANDEDRELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
229  if (type == "LINESMOOTHING_BANDED RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
230  if (type == "LINESMOOTHING_BANDED_RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
231  if (type == "LINESMOOTHING_TRIDIRELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
232  if (type == "LINESMOOTHING_TRIDI RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
233  if (type == "LINESMOOTHING_TRIDI_RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
234  if (type == "LINESMOOTHING_TRIDIAGONALRELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
235  if (type == "LINESMOOTHING_TRIDIAGONAL RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
236  if (type == "LINESMOOTHING_TRIDIAGONAL_RELAXATION") { return "LINESMOOTHING_BLOCKRELAXATION"; }
237  if(type == "BLOCK_RELAXATION" ||
238  type == "BLOCK RELAXATION" ||
239  type == "BLOCKRELAXATION" ||
240  // Banded
241  type == "BANDED_RELAXATION" ||
242  type == "BANDED RELAXATION" ||
243  type == "BANDEDRELAXATION" ||
244  // Tridiagonal
245  type == "TRIDI_RELAXATION" ||
246  type == "TRIDI RELAXATION" ||
247  type == "TRIDIRELAXATION" ||
248  type == "TRIDIAGONAL_RELAXATION" ||
249  type == "TRIDIAGONAL RELAXATION" ||
250  type == "TRIDIAGONALRELAXATION") {
251  return "block relaxation stand-alone";
252  }
253 
254  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "Cannot convert Ifpack2 preconditioner name to Ifpack: unknown type: \"" + type + "\"");
255  }
256 
257  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
259  Teuchos::ParameterList ifpack1List = ifpack2List;
260 
261  if (ifpack2List.isParameter("relaxation: type") && ifpack2List.get<std::string>("relaxation: type") == "Symmetric Gauss-Seidel")
262  ifpack1List.set("relaxation: type", "symmetric Gauss-Seidel");
263 
264  if (ifpack2List.isParameter("fact: iluk level-of-fill")) {
265  ifpack1List.remove("fact: iluk level-of-fill");
266  ifpack1List.set("fact: level-of-fill", ifpack2List.get<int>("fact: iluk level-of-fill"));
267  }
268 
269  return ifpack1List;
270  }
271 
272  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
274  std::ostringstream out;
275  if (s_ != Teuchos::null) {
276  out << s_->description();
277  } else {
279  out << "{type = " << type_ << "}";
280  }
281  return out.str();
282  }
283 
284  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
287 
288  if (verbLevel & Parameters0)
289  out0 << "Prec. type: " << type_ << std::endl;
290 
291  if (verbLevel & Parameters1) {
292  out0 << "PrecType: " << type_ << std::endl;
293  out0 << "Parameter list: " << std::endl;
294  Teuchos::OSTab tab2(out);
295  out << this->GetParameterList();
296  out0 << "Overlap: " << overlap_ << std::endl;
297  }
298 
299  if (verbLevel & Debug) {
300  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl
301  << "-" << std::endl
302  << "Epetra PrecType: " << Ifpack2ToIfpack1Type(type_) << std::endl
303  << "Epetra Parameter list: " << std::endl;
304  Teuchos::OSTab tab2(out);
305  out << Ifpack2ToIfpack1Param(this->GetParameterList());;
306  }
307  }
308 
309 } // namespace MueLu
310 
311 #endif // MUELU_TRILINOSSMOOTHER_DEF_HPP
Important warning messages (one line)
Exception indicating invalid cast attempted.
std::string description() const
Return a simple one-line description of this object.
T & get(const std::string &name, T def_value)
Class that encapsulates external library smoothers.
static std::string Ifpack2ToIfpack1Type(const std::string &type)
Convert an Ifpack2 preconditioner name to Ifpack.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Print additional debugging information.
LocalOrdinal LO
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.
bool isParameter(const std::string &name) const
bool remove(std::string const &name, bool throwIfNotExists=true)
virtual void SetParameterList(const Teuchos::ParameterList &paramList)
Set parameters from a parameter list and return with default values.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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.
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.
std::string toString(const T &t)
void SetFactory(const std::string &varName, const RCP< const FactoryBase > &factory)
Custom SetFactory.