MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_CreateXpetraPreconditioner.hpp
Go to the documentation of this file.
1 /*
2  * MueLu_CreateXpetraPreconditioner.hpp
3  *
4  * Created on: Feb 5, 2016
5  * Author: tawiesn
6  */
7 
8 #ifndef PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_
9 #define PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_
10 
13 
15 #include <Xpetra_CrsMatrix.hpp>
16 #include <Xpetra_MultiVector.hpp>
17 
18 #include <MueLu.hpp>
19 
20 #include <MueLu_Exceptions.hpp>
21 #include <MueLu_Hierarchy.hpp>
22 #include <MueLu_MasterList.hpp>
23 #include <MueLu_MLParameterListInterpreter.hpp>
24 #include <MueLu_ParameterListInterpreter.hpp>
25 #include <MueLu_Utilities.hpp>
26 #include <MueLu_HierarchyUtils.hpp>
27 
28 #include <stdlib.h>
29 
30 namespace MueLu {
31 
40  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
43  const Teuchos::ParameterList& inParamList) {
44  using SC = Scalar;
45  using LO = LocalOrdinal;
46  using GO = GlobalOrdinal;
47  using NO = Node;
48 
54 
55  bool hasParamList = inParamList.numParams();
56 
57  RCP<HierarchyManager> mueLuFactory;
58 
59  // Rip off non-serializable data before validation
60  Teuchos::ParameterList nonSerialList,paramList;
61  MueLu::ExtractNonSerializableData(inParamList, paramList, nonSerialList);
62 
63  std::string label;
64  if (hasParamList && paramList.isParameter("hierarchy label")) {
65  label = paramList.get<std::string>("hierarchy label");
66  paramList.remove("hierarchy label");
67  } else
68  label = op->getObjectLabel();
69 
70  std::string timerName;
71  if (label != "")
72  timerName = "MueLu setup time (" + label + ")";
73  else
74  timerName = "MueLu setup time";
76  tm->start();
77 
78  std::string syntaxStr = "parameterlist: syntax";
79  if (hasParamList && paramList.isParameter(syntaxStr) && paramList.get<std::string>(syntaxStr) == "ml") {
80  paramList.remove(syntaxStr);
81  mueLuFactory = rcp(new MLParameterListInterpreter(paramList));
82  } else {
83  mueLuFactory = rcp(new ParameterListInterpreter(paramList,op->getDomainMap()->getComm()));
84  }
85 
86  // Create Hierarchy
87  RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
88  H->setlib(op->getDomainMap()->lib());
89 
90  // Stick the non-serializible data on the hierarchy.
91  HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory,*H, nonSerialList);
92 
93  // Set fine level operator
94  H->GetLevel(0)->Set("A", op);
95  H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
96 
97  mueLuFactory->SetupHierarchy(*H);
98 
99  tm->stop();
100  tm->incrementNumCalls();
101 
102  if (H->GetVerbLevel() & Statistics0) {
103  const bool alwaysWriteLocal = true;
104  const bool writeGlobalStats = true;
105  const bool writeZeroTimers = false;
106  const bool ignoreZeroTimers = true;
107  const std::string filter = timerName;
108  Teuchos::TimeMonitor::summarize(op->getRowMap()->getComm().ptr(), std::cout, alwaysWriteLocal, writeGlobalStats,
109  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
110  }
111 
112  tm->reset();
113 
114  return H;
115  }
116 
125  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
128  const std::string& xmlFileName) {
129  Teuchos::ParameterList paramList;
130  Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<Teuchos::ParameterList>(&paramList), *op->getDomainMap()->getComm());
131  return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
132  }
133 
141  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
144  Teuchos::ParameterList paramList;
145  return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
146  }
147 
155  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
158  std::string label = H->GetLevel(0)->getObjectLabel();
159 
160  std::string timerName;
161  if (label != "")
162  timerName = "MueLu setup time (" + label + ")";
163  else
164  timerName = "MueLu setup time";
166  tm->start();
167 
168  typedef Scalar SC;
169  typedef LocalOrdinal LO;
170  typedef GlobalOrdinal GO;
171  typedef Node NO;
172 
173  typedef Xpetra::Matrix<SC,LO,GO,NO> Matrix;
174  typedef Xpetra::Operator<SC,LO,GO,NO> Operator;
175 
177  "MueLu::ReuseXpetraPreconditioner: Hierarchy has no levels in it");
178  TEUCHOS_TEST_FOR_EXCEPTION(!H->GetLevel(0)->IsAvailable("A"), Exceptions::RuntimeError,
179  "MueLu::ReuseXpetraPreconditioner: Hierarchy has no fine level operator");
180  RCP<Level> level0 = H->GetLevel(0);
181 
182  RCP<Operator> O0 = level0->Get<RCP<Operator> >("A");
183  RCP<Matrix> A0 = Teuchos::rcp_dynamic_cast<Matrix>(O0);
184 
185  if (!A0.is_null()) {
186  // If a user provided a "number of equations" argument in a parameter list
187  // during the initial setup, we must honor that settings and reuse it for
188  // all consequent setups.
189  A->SetFixedBlockSize(A0->GetFixedBlockSize());
190  }
191  level0->Set("A", A);
192 
193  H->SetupRe();
194 
195  tm->stop();
196  tm->incrementNumCalls();
197 
198  if (H->GetVerbLevel() & Statistics0) {
199  const bool alwaysWriteLocal = true;
200  const bool writeGlobalStats = true;
201  const bool writeZeroTimers = false;
202  const bool ignoreZeroTimers = true;
203  const std::string filter = timerName;
204  Teuchos::TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), std::cout, alwaysWriteLocal, writeGlobalStats,
205  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
206  }
207 
208  tm->reset();
209  }
210 
211 
212 #ifdef HAVE_MUELU_DEPRECATED_CODE
213 
224  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
225  MUELU_DEPRECATED
228  const Teuchos::ParameterList& inParamList,
229  Teuchos::RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node> > coords,
231 
232  Teuchos::ParameterList userParamList = inParamList.sublist("user data");
233  if (Teuchos::nonnull(coords)) {
234  userParamList.set<RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node> > >("Coordinates", coords);
235  }
236  if(Teuchos::nonnull(nullspace)) {
237  userParamList.set<RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >("Nullspace", nullspace);
238  }
239 
240  return CreateXpetraPreconditioner(op, inParamList);
241 
242  }
243 
253  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
254  MUELU_DEPRECATED
257  const Teuchos::ParameterList& inParamList,
258  Teuchos::RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node> > coords) {
259 
260  Teuchos::ParameterList mueluParams = inParamList;
261  Teuchos::ParameterList& userParamList = mueluParams.sublist("user data");
262  if (Teuchos::nonnull(coords)) {
263  userParamList.set<RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node> > >("Coordinates", coords);
264  }
265  return CreateXpetraPreconditioner(op, mueluParams);
266  }
267 
268 #endif // HAVE_MUELU_DEPRECATED_CODE
269 
270 } //namespace
271 
272 #define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
273 
274 #endif /* PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_ */
RCP< Level > & GetLevel(const int levelID=0)
Retrieve a certain level from hierarchy.
static void AddNonSerializableDataToHierarchy(HierarchyManager &HM, Hierarchy &H, const ParameterList &nonSerialList)
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
bool nonnull(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
LocalOrdinal LO
Ordinal numParams() const
static RCP< Time > getNewTimer(const std::string &name)
bool isParameter(const std::string &name) const
Print statistics that do not involve significant additional computation.
bool remove(std::string const &name, bool throwIfNotExists=true)
void start(bool reset=false)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
double stop()
void ReuseXpetraPreconditioner(const Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A, Teuchos::RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node >> &H)
Helper function to reuse an existing MueLu preconditioner.
VerbLevel GetVerbLevel() const
Get the verbosity level.
Teuchos::RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > CreateXpetraPreconditioner(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > op, const Teuchos::ParameterList &inParamList)
Helper function to create a MueLu preconditioner that can be used by Xpetra.Given an Xpetra::Matrix...
Class that accepts ML-style parameters and builds a MueLu preconditioner. This interpreter uses the s...
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
Scalar SC
Node NO
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Exception throws to report errors in the internal logical of the program.
void setlib(Xpetra::UnderlyingLib inlib)
void incrementNumCalls()
virtual RCP< Hierarchy > CreateHierarchy() const
Create an empty Hierarchy object.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
long ExtractNonSerializableData(const Teuchos::ParameterList &inList, Teuchos::ParameterList &serialList, Teuchos::ParameterList &nonSerialList)
virtual void SetupHierarchy(Hierarchy &H) const
Setup Hierarchy object.
bool is_null() const