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 // ***********************************************************************
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 
47 #ifndef PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_
48 #define PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_
49 
52 
54 #include <Xpetra_CrsMatrix.hpp>
55 #include <Xpetra_MultiVector.hpp>
56 
57 #include <MueLu.hpp>
58 
59 #include <MueLu_Exceptions.hpp>
60 #include <MueLu_Hierarchy.hpp>
61 #include <MueLu_MasterList.hpp>
62 #include <MueLu_ParameterListInterpreter.hpp>
63 #include <MueLu_Utilities.hpp>
64 #include <MueLu_HierarchyUtils.hpp>
66 #include <stdlib.h>
67 
68 namespace MueLu {
69 
78 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
81  const Teuchos::ParameterList& inParamList) {
82  using SC = Scalar;
83  using LO = LocalOrdinal;
84  using GO = GlobalOrdinal;
85  using NO = Node;
86 
91 
92  bool hasParamList = inParamList.numParams();
93 
94  RCP<HierarchyManager> mueLuFactory;
95 
96  // Rip off non-serializable data before validation
97  Teuchos::ParameterList nonSerialList, paramList;
98  MueLu::ExtractNonSerializableData(inParamList, paramList, nonSerialList);
99 
100  std::string label;
101  if (hasParamList && paramList.isParameter("hierarchy label")) {
102  label = paramList.get<std::string>("hierarchy label");
103  paramList.remove("hierarchy label");
104  } else
105  label = op->getObjectLabel();
106 
107  std::string timerName;
108  if (label != "")
109  timerName = "MueLu setup time (" + label + ")";
110  else
111  timerName = "MueLu setup time";
113  tm->start();
114 
115  std::string syntaxStr = "parameterlist: syntax";
116  if (hasParamList && paramList.isParameter(syntaxStr) && paramList.get<std::string>(syntaxStr) == "ml") {
117  paramList.remove(syntaxStr);
118  std::string paramXML = MueLu::ML2MueLuParameterTranslator::translate(paramList, "");
119  paramList = *Teuchos::getParametersFromXmlString(paramXML);
120  }
121  mueLuFactory = rcp(new ParameterListInterpreter(paramList, op->getDomainMap()->getComm()));
122 
123  // Create Hierarchy
124  RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
125  H->setlib(op->getDomainMap()->lib());
126 
127  // Set fine level operator
128  H->GetLevel(0)->Set("A", op);
129  H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
130 
131  // Stick the non-serializible data on the hierarchy.
132  HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory, *H, nonSerialList);
133 
134  mueLuFactory->SetupHierarchy(*H);
135 
136  tm->stop();
137  tm->incrementNumCalls();
138 
139  if (H->GetVerbLevel() & Statistics0) {
140  const bool alwaysWriteLocal = true;
141  const bool writeGlobalStats = true;
142  const bool writeZeroTimers = false;
143  const bool ignoreZeroTimers = true;
144  const std::string filter = timerName;
145  Teuchos::TimeMonitor::summarize(op->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
146  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
147  }
148 
149  tm->reset();
150 
151  return H;
152 }
153 
162 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
165  const std::string& xmlFileName) {
166  Teuchos::ParameterList paramList;
167  Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<Teuchos::ParameterList>(&paramList), *op->getDomainMap()->getComm());
168  return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
169 }
170 
178 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
181  Teuchos::ParameterList paramList;
182  return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
183 }
184 
192 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
195  std::string label = H->GetLevel(0)->getObjectLabel();
196 
197  std::string timerName;
198  if (label != "")
199  timerName = "MueLu setup time (" + label + ")";
200  else
201  timerName = "MueLu setup time";
203  tm->start();
204 
205  typedef Scalar SC;
206  typedef LocalOrdinal LO;
207  typedef GlobalOrdinal GO;
208  typedef Node NO;
209 
210  typedef Xpetra::Matrix<SC, LO, GO, NO> Matrix;
211  typedef Xpetra::Operator<SC, LO, GO, NO> Operator;
212 
214  "MueLu::ReuseXpetraPreconditioner: Hierarchy has no levels in it");
215  TEUCHOS_TEST_FOR_EXCEPTION(!H->GetLevel(0)->IsAvailable("A"), Exceptions::RuntimeError,
216  "MueLu::ReuseXpetraPreconditioner: Hierarchy has no fine level operator");
217  RCP<Level> level0 = H->GetLevel(0);
218 
219  RCP<Operator> O0 = level0->Get<RCP<Operator>>("A");
220  RCP<Matrix> A0 = Teuchos::rcp_dynamic_cast<Matrix>(O0);
221 
222  if (!A0.is_null()) {
223  // If a user provided a "number of equations" argument in a parameter list
224  // during the initial setup, we must honor that settings and reuse it for
225  // all consequent setups.
226  A->SetFixedBlockSize(A0->GetFixedBlockSize());
227  }
228  level0->Set("A", A);
229 
230  H->SetupRe();
231 
232  tm->stop();
233  tm->incrementNumCalls();
234 
235  if (H->GetVerbLevel() & Statistics0) {
236  const bool alwaysWriteLocal = true;
237  const bool writeGlobalStats = true;
238  const bool writeZeroTimers = false;
239  const bool ignoreZeroTimers = true;
240  const std::string filter = timerName;
241  Teuchos::TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
242  writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
243  }
244 
245  tm->reset();
246 }
247 
248 } // namespace MueLu
249 
250 #define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
251 
252 #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)
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.
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()
virtual RCP< Hierarchy > CreateHierarchy() const
Create an empty Hierarchy object.
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...
virtual void SetupHierarchy(Hierarchy &H) const
Setup Hierarchy object.
bool is_null() const