MueLu  Version of the Day
MueLu_DirectSolver_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_DIRECTSOLVER_DEF_HPP
47 #define MUELU_DIRECTSOLVER_DEF_HPP
48 
49 #include <Xpetra_Utils.hpp>
50 #include <Xpetra_Matrix.hpp>
51 
53 
54 #include "MueLu_Exceptions.hpp"
55 #include "MueLu_Level.hpp"
56 
57 #include "MueLu_Amesos2Smoother.hpp"
58 #include "MueLu_AmesosSmoother.hpp"
59 
60 namespace MueLu {
61 
62  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
63  DirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::DirectSolver(const std::string& type, const Teuchos::ParameterList& paramListIn)
64  : type_(type) {
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  ParameterList paramList = paramListIn;
76 
77  // We want DirectSolver to be able to work with both Epetra and Tpetra objects, therefore we try to construct both
78  // Amesos and Amesos2 solver prototypes. The construction really depends on configuration options.
79  triedEpetra_ = triedTpetra_ = false;
80 #if defined(HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_AMESOS2)
81  try {
82  sTpetra_ = rcp(new Amesos2Smoother(type_, paramList));
83  TEUCHOS_TEST_FOR_EXCEPTION(sTpetra_.is_null(), Exceptions::RuntimeError, "Unable to construct Amesos2 direct solver");
84  } catch (Exceptions::RuntimeError& e) {
85  errorTpetra_ = e.what();
86  }
87  triedTpetra_ = true;
88 #endif
89 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_AMESOS)
90  try {
91  // GetAmesosSmoother masks the template argument matching, and simply throws if template arguments are incompatible with Epetra
92  sEpetra_ = GetAmesosSmoother<SC,LO,GO,NO>(type_, paramList);
93  TEUCHOS_TEST_FOR_EXCEPTION(sEpetra_.is_null(), Exceptions::RuntimeError, "Unable to construct Amesos direct solver");
94  } catch (Exceptions::RuntimeError& e) {
95  // AmesosSmoother throws if Scalar != double, LocalOrdinal != int, GlobalOrdinal != int
96  errorEpetra_ = e.what();
97  }
98  triedEpetra_ = true;
99 #endif
100 
101  // Check if we were able to construct at least one solver. In many cases that's all we need, for instance if a user
102  // simply wants to use Tpetra only stack, never enables Amesos, and always runs Tpetra objects.
103  TEUCHOS_TEST_FOR_EXCEPTION(!triedEpetra_ && !triedTpetra_, Exceptions::RuntimeError, "Unable to construct any direct solver."
104  "Plase enable (TPETRA and AMESOS2) or (EPETRA and AMESOS)");
105 
106  TEUCHOS_TEST_FOR_EXCEPTION(sEpetra_.is_null() && sTpetra_.is_null(), Exceptions::RuntimeError,
107  "Could not enable any direct solver:\n"
108  << (triedEpetra_ ? "Epetra mode was disabled due to an error:\n" : "")
109  << (triedEpetra_ ? errorEpetra_ : "")
110  << (triedTpetra_ ? "Tpetra mode was disabled due to an error:\n" : "")
111  << (triedTpetra_ ? errorTpetra_ : ""));
112 
113  this->SetParameterList(paramList);
114  }
115 
116  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
117  void DirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetFactory(const std::string& varName, const RCP<const FactoryBase>& factory) {
118  // We need to propagate SetFactory to proper place
119  if (!sEpetra_.is_null()) sEpetra_->SetFactory(varName, factory);
120  if (!sTpetra_.is_null()) sTpetra_->SetFactory(varName, factory);
121  }
122 
123  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
125  // Decide whether we are running in Epetra or Tpetra mode
126  //
127  // Theoretically, we could make this decision in the constructor, and create only
128  // one of the smoothers. But we want to be able to reuse, so one can imagine a scenario
129  // where one first runs hierarchy with tpetra matrix, and then with epetra.
130  bool useTpetra = (currentLevel.lib() == Xpetra::UseTpetra);
131  s_ = (useTpetra ? sTpetra_ : sEpetra_);
132  if (s_.is_null()) {
133  if (useTpetra) {
134 #if not defined(HAVE_MUELU_AMESOS2)
135  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError,
136  "Error: running in Tpetra mode, but MueLu with Amesos2 was disabled during the configure stage.\n"
137  "Please make sure that:\n"
138  " - Amesos2 is enabled (Trilinos_ENABLE_Amesos2=ON),\n"
139  " - Amesos2 is available for MueLu to use (MueLu_ENABLE_Amesos2=ON)\n");
140 #else
141  if (triedTpetra_)
142  this->GetOStream(Errors) << "Tpetra mode was disabled due to an error:\n" << errorTpetra_ << std::endl;
143 #endif
144  }
145  if (!useTpetra) {
146 #if not defined(HAVE_MUELU_AMESOS)
147  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError,
148  "Error: running in Epetra mode, but MueLu with Amesos was disabled during the configure stage.\n"
149  "Please make sure that:\n"
150  " - Amesos is enabled (you can do that with Trilinos_ENABLE_Amesos=ON),\n"
151  " - Amesos is available for MueLu to use (MueLu_ENABLE_Amesos=ON)\n");
152 #else
153  if (triedEpetra_)
154  this->GetOStream(Errors) << "Epetra mode was disabled due to an error:\n" << errorEpetra_ << std::endl;
155 #endif
156  }
157  TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError,
158  "Direct solver for " << (useTpetra ? "Tpetra" : "Epetra") << " was not constructed");
159  }
160 
161  s_->DeclareInput(currentLevel);
162  }
163 
164  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
166  if (SmootherPrototype::IsSetup() == true)
167  this->GetOStream(Warnings0) << "MueLu::DirectSolver::Setup(): Setup() has already been called";
168 
169  int oldRank = s_->SetProcRankVerbose(this->GetProcRankVerbose());
170 
171  s_->Setup(currentLevel);
172 
173  s_->SetProcRankVerbose(oldRank);
174 
176 
177  this->SetParameterList(s_->GetParameterList());
178  }
179 
180  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
181  void DirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply(MultiVector& X, const MultiVector& B, bool InitialGuessIsZero) const {
182  TEUCHOS_TEST_FOR_EXCEPTION(SmootherPrototype::IsSetup() == false, Exceptions::RuntimeError, "MueLu::AmesosSmoother::Apply(): Setup() has not been called");
183 
184  s_->Apply(X, B, InitialGuessIsZero);
185  }
186 
187  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
188  RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> > DirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Copy() const {
189  RCP<DirectSolver> newSmoo = rcp(new DirectSolver(*this));
190 
191  // We need to be quite careful with Copy
192  // We still want DirectSolver to follow Prototype Pattern, so we need to hide the fact that we do have some state
193  if (!sEpetra_.is_null())
194  newSmoo->sEpetra_ = sEpetra_->Copy();
195  if (!sTpetra_.is_null())
196  newSmoo->sTpetra_ = sTpetra_->Copy();
197 
198  // Copy the default mode
199  newSmoo->s_ = (s_.get() == sTpetra_.get() ? newSmoo->sTpetra_ : newSmoo->sEpetra_);
200  newSmoo->SetParameterList(this->GetParameterList());
201 
202  return newSmoo;
203  }
204 
205  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
207  std::ostringstream out;
208  if (s_ != Teuchos::null) {
209  out << s_->description();
210  } else {
212  out << "{type = " << type_ << "}";
213  }
214  return out.str();
215  }
216 
217  template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
218  void DirectSolver<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print(Teuchos::FancyOStream& out, const VerbLevel verbLevel) const {
220 
221  if (verbLevel & Parameters0)
222  out0 << "Prec. type: " << type_ << std::endl;
223 
224  if (verbLevel & Parameters1) {
225  out0 << "Parameter list: " << std::endl;
226  Teuchos::OSTab tab3(out);
227  out << this->GetParameterList();
228  }
229 
230  if (verbLevel & Debug)
231  out0 << "IsSetup: " << Teuchos::toString(SmootherPrototype::IsSetup()) << std::endl;
232  }
233 
234 } // namespace MueLu
235 
236 #endif // MUELU_DIRECTSOLVER_DEF_HPP
Important warning messages (one line)
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
DirectSolver cannot be applied. Apply() always returns a RuntimeError exception.
void Setup(Level &currentLevel)
DirectSolver cannot be turned into a smoother using Setup(). Setup() always returns a RuntimeError ex...
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
void DeclareInput(Level &currentLevel) const
Input.
Print additional debugging information.
Namespace for MueLu classes and methods.
virtual const Teuchos::ParameterList & GetParameterList() const
std::string type_
amesos1/2-specific key phrase that denote smoother type
DirectSolver(const std::string &type="", const Teuchos::ParameterList &paramList=Teuchos::ParameterList())
Constructor Note: only parameters shared by Amesos and Amesos2 should be used for type and paramList ...
RCP< SmootherPrototype > Copy() const
When this prototype is cloned using Copy(), the clone is an Amesos or an Amesos2 smoother.
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
std::string description() const
Return a simple one-line description of this object.
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
Class that encapsulates Amesos2 direct solvers.
bool IsSetup() const
Get the state of a smoother prototype.
Xpetra::UnderlyingLib lib()
#define MUELU_DESCRIBE
Helper macro for implementing Describable::describe() for BaseClass objects.
RCP< SmootherPrototype > sTpetra_
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Print class parameters.
void SetFactory(const std::string &varName, const RCP< const FactoryBase > &factory)
Custom SetFactory.
RCP< SmootherPrototype > sEpetra_
Smoother.
RCP< SmootherPrototype > s_
Print class parameters (more parameters, more verbose)
Exception throws to report errors in the internal logical of the program.
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.