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 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_
11 #define PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_
12 
15 
17 #include <Xpetra_CrsMatrix.hpp>
18 #include <Xpetra_MultiVector.hpp>
19 
20 #include <MueLu.hpp>
21 
22 #include <MueLu_Exceptions.hpp>
23 #include <MueLu_Hierarchy.hpp>
24 #include <MueLu_MasterList.hpp>
25 #include <MueLu_ParameterListInterpreter.hpp>
26 #include <MueLu_Utilities.hpp>
27 #include <MueLu_HierarchyUtils.hpp>
29 #include <stdlib.h>
30 
31 namespace MueLu {
32 
41 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
44  const Teuchos::ParameterList& inParamList) {
45  using SC = Scalar;
46  using LO = LocalOrdinal;
47  using GO = GlobalOrdinal;
48  using NO = Node;
49 
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  std::string paramXML = MueLu::ML2MueLuParameterTranslator::translate(paramList, "");
82  paramList = *Teuchos::getParametersFromXmlString(paramXML);
83  }
84  mueLuFactory = rcp(new ParameterListInterpreter(paramList, op->getDomainMap()->getComm()));
85 
86  // Create Hierarchy
87  RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
88  H->setlib(op->getDomainMap()->lib());
89 
90  // Set fine level operator
91  H->GetLevel(0)->Set("A", op);
92  H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
93 
94  // Stick the non-serializible data on the hierarchy.
95  HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory, *H, nonSerialList);
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>
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(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
205  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
206  }
207 
208  tm->reset();
209 }
210 
211 } // namespace MueLu
212 
213 #define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
214 
215 #endif /* PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_ */
MueLu::DefaultLocalOrdinal LocalOrdinal
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
RCP< Level > & GetLevel(const int levelID=0)
Retrieve a certain level from hierarchy.
static void AddNonSerializableDataToHierarchy(HierarchyManager &HM, Hierarchy &H, const ParameterList &nonSerialList)
Add non-serializable data to Hierarchy.
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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.
LocalOrdinal LO
Ordinal numParams() const
MueLu::DefaultNode Node
static RCP< Time > getNewTimer(const std::string &name)
virtual RCP< Hierarchy > CreateHierarchy() const
Create an empty Hierarchy object.
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...
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)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
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()
VerbLevel GetVerbLevel() const
Get the verbosity level.
virtual void SetupHierarchy(Hierarchy &H) const
Setup Hierarchy object.
int SetProcRankVerbose(int procRank) const
Set proc rank used for printing.
Scalar SC
Node NO
Exception throws to report errors in the internal logical of the program.
void setlib(Xpetra::UnderlyingLib inlib)
void incrementNumCalls()
static std::string translate(Teuchos::ParameterList &paramList, const std::string &defaultVals="")
: Translate ML parameters to MueLu parameter XML string
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...
bool is_null() const