MueLu  Version of the Day
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 {
41  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
44  const Teuchos::ParameterList& inParamList,
52 
53  std::string timerName = "MueLu setup time";
55  tm->start();
56 
57  bool hasParamList = inParamList.numParams();
58 
59  RCP<HierarchyManager> mueLuFactory;
60 
61  // Rip off non-serializable data before validation
62  Teuchos::ParameterList nonSerialList,paramList;
63  MueLu::ExtractNonSerializableData(inParamList, paramList, nonSerialList);
64 
65  std::string syntaxStr = "parameterlist: syntax";
66  if (hasParamList && paramList.isParameter(syntaxStr) && paramList.get<std::string>(syntaxStr) == "ml") {
67  paramList.remove(syntaxStr);
68  mueLuFactory = rcp(new MLParameterListInterpreter(paramList));
69  } else {
70  mueLuFactory = rcp(new ParameterListInterpreter(paramList,op->getDomainMap()->getComm()));
71  }
72 
73  // Create Hierarchy
74  std::string label;
75  if (hasParamList && paramList.isParameter("hierarchy label")) {
76  label = paramList.get<std::string>("hierarchy label");
77  paramList.remove("hierarchy label");
78  } else
79  label = op->getObjectLabel();
80  RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
81  H->setlib(op->getDomainMap()->lib());
82 
83  // Stick the non-serializible data on the hierarchy.
84  HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory,*H, nonSerialList);
85 
86  // Set fine level operator
87  H->GetLevel(0)->Set("A", op);
88 
89  // Set coordinates if available
90  if (coords != Teuchos::null)
91  H->GetLevel(0)->Set("Coordinates", coords);
92 
93  // Set nullspace if available
94  if (nullspace != Teuchos::null)
95  H->GetLevel(0)->Set("Nullspace", nullspace);
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 
117  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
120  const Teuchos::ParameterList& inParamList,
121  const Teuchos::ParameterList& dummy) {
127 
128  std::string timerName = "MueLu setup time";
130  tm->start();
131 
132  bool hasParamList = inParamList.numParams();
133 
134  RCP<HierarchyManager> mueLuFactory;
135 
136  // Rip off non-serializable data before validation
137  Teuchos::ParameterList nonSerialList,paramList;
138  MueLu::ExtractNonSerializableData(inParamList, paramList, nonSerialList);
139 
140  std::string syntaxStr = "parameterlist: syntax";
141  if (hasParamList && paramList.isParameter(syntaxStr) && paramList.get<std::string>(syntaxStr) == "ml") {
142  paramList.remove(syntaxStr);
143  mueLuFactory = rcp(new MLParameterListInterpreter(paramList));
144  } else {
145  mueLuFactory = rcp(new ParameterListInterpreter(paramList,op->getDomainMap()->getComm()));
146  }
147 
148  // Create Hierarchy
149  RCP<Hierarchy> H = mueLuFactory->CreateHierarchy();
150  H->setlib(op->getDomainMap()->lib());
151 
152  // Stick the non-serializible data on the hierarchy.
153  HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory,*H, nonSerialList);
154 
155  // Set fine level operator
156  H->GetLevel(0)->Set("A", op);
157 
158  mueLuFactory->SetupHierarchy(*H);
159 
160  tm->stop();
161  tm->incrementNumCalls();
162 
163  if (H->GetVerbLevel() & Statistics0) {
164  const bool alwaysWriteLocal = true;
165  const bool writeGlobalStats = true;
166  const bool writeZeroTimers = false;
167  const bool ignoreZeroTimers = true;
168  const std::string filter = timerName;
169  Teuchos::TimeMonitor::summarize(op->getRowMap()->getComm().ptr(), std::cout, alwaysWriteLocal, writeGlobalStats,
170  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
171  }
172 
173  tm->reset();
174 
175  return H;
176  }
177 
185  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
188  std::string timerName = "MueLu setup time";
190  tm->start();
191 
192  typedef Scalar SC;
193  typedef LocalOrdinal LO;
194  typedef GlobalOrdinal GO;
195  typedef Node NO;
196 
197  typedef Xpetra::Matrix<SC,LO,GO,NO> Matrix;
198  typedef Xpetra::Operator<SC,LO,GO,NO> Operator;
199 
201  "MueLu::ReuseXpetraPreconditioner: Hierarchy has no levels in it");
202  TEUCHOS_TEST_FOR_EXCEPTION(!H->GetLevel(0)->IsAvailable("A"), Exceptions::RuntimeError,
203  "MueLu::ReuseXpetraPreconditioner: Hierarchy has no fine level operator");
204  RCP<Level> level0 = H->GetLevel(0);
205 
206  RCP<Operator> O0 = level0->Get<RCP<Operator> >("A");
207  RCP<Matrix> A0 = Teuchos::rcp_dynamic_cast<Matrix>(O0);
208 
209  if (!A0.is_null()) {
210  // If a user provided a "number of equations" argument in a parameter list
211  // during the initial setup, we must honor that settings and reuse it for
212  // all consequent setups.
213  A->SetFixedBlockSize(A0->GetFixedBlockSize());
214  }
215  level0->Set("A", A);
216 
217  H->SetupRe();
218 
219  tm->stop();
220  tm->incrementNumCalls();
221 
222  if (H->GetVerbLevel() & Statistics0) {
223  const bool alwaysWriteLocal = true;
224  const bool writeGlobalStats = true;
225  const bool writeZeroTimers = false;
226  const bool ignoreZeroTimers = true;
227  const std::string filter = timerName;
228  Teuchos::TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), std::cout, alwaysWriteLocal, writeGlobalStats,
229  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
230  }
231 
232  tm->reset();
233  }
234 
235 } //namespace
236 
237 #define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
238 
239 #endif /* PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_ */
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.
RCP< Level > & GetLevel(const int levelID=0)
Retrieve a certain level from hierarchy.
VerbLevel GetVerbLevel() const
Get the verbosity level.
static void AddNonSerializableDataToHierarchy(HierarchyManager &HM, Hierarchy &H, const ParameterList &nonSerialList)
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > CreateXpetraPreconditioner(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > op, const Teuchos::ParameterList &inParamList, Teuchos::RCP< Xpetra::MultiVector< double, LocalOrdinal, GlobalOrdinal, Node > > coords=Teuchos::null, Teuchos::RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > nullspace=Teuchos::null)
Helper function to create a MueLu preconditioner that can be used by Xpetra.Given an Xpetra::Matrix...
Ordinal numParams() const
LocalOrdinal LO
Namespace for MueLu classes and methods.
bool is_null() const
virtual void SetupHierarchy(Hierarchy &H) const
Setup Hierarchy object.
static RCP< Time > getNewTimer(const std::string &name)
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()
Class that accepts ML-style parameters and builds a MueLu preconditioner. This interpreter uses the s...
virtual RCP< Hierarchy > CreateHierarchy() const
Create an empty Hierarchy object.
Scalar SC
bool isParameter(const std::string &name) const
Node NO
Exception throws to report errors in the internal logical of the program.
void setlib(Xpetra::UnderlyingLib inlib)
void incrementNumCalls()
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
long ExtractNonSerializableData(const Teuchos::ParameterList &inList, Teuchos::ParameterList &serialList, Teuchos::ParameterList &nonSerialList)