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