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 
14 #include <Teuchos_XMLParameterListHelpers.hpp>
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>
41  Teuchos::RCP<MueLu::Hierarchy<Scalar,LocalOrdinal,GlobalOrdinal,Node> >
42  CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > op,
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";
75  RCP<Teuchos::Time> tm = Teuchos::TimeMonitor::getNewTimer(timerName);
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(), H->GetOStream(Statistics0), 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>
126  Teuchos::RCP<MueLu::Hierarchy<Scalar,LocalOrdinal,GlobalOrdinal,Node> >
127  CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > op,
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>
142  Teuchos::RCP<MueLu::Hierarchy<Scalar,LocalOrdinal,GlobalOrdinal,Node> >
143  CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > op) {
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>
156  void ReuseXpetraPreconditioner(const Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& A,
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";
165  RCP<Teuchos::Time> tm = Teuchos::TimeMonitor::getNewTimer(timerName);
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 
176  TEUCHOS_TEST_FOR_EXCEPTION(!H->GetNumLevels(), Exceptions::RuntimeError,
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(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
205  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
206  }
207 
208  tm->reset();
209  }
210 
211 
212 
213 } //namespace
214 
215 #define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
216 
217 #endif /* PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_ */
MueLu::DefaultLocalOrdinal LocalOrdinal
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.
static void AddNonSerializableDataToHierarchy(HierarchyManager &HM, Hierarchy &H, const ParameterList &nonSerialList)
Add non-serializable data to Hierarchy.
Namespace for MueLu classes and methods.
MueLu::DefaultNode Node
Print statistics that do not involve significant additional computation.
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that accepts ML-style parameters and builds a MueLu preconditioner. This interpreter uses the s...
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...
Exception throws to report errors in the internal logical of the program.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
long ExtractNonSerializableData(const Teuchos::ParameterList &inList, Teuchos::ParameterList &serialList, Teuchos::ParameterList &nonSerialList)
Extract non-serializable data from level-specific sublists and move it to a separate parameter list...