MueLu Version of the Day
Loading...
Searching...
No Matches
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
16#include <Teuchos_XMLParameterListHelpers.hpp>
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
32
33namespace MueLu {
34
43template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
44Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
45CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op,
46 const Teuchos::ParameterList& inParamList) {
47 using SC = Scalar;
48 using LO = LocalOrdinal;
49 using GO = GlobalOrdinal;
50 using NO = Node;
51
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 label;
66 if (hasParamList && paramList.isParameter("hierarchy label")) {
67 label = paramList.get<std::string>("hierarchy label");
68 } else
69 label = op->getObjectLabel();
70
71 RCP<Teuchos::Time> tm;
72 std::string timerName;
73
74 // Do Kokkos tuning, if requested
75 if (paramList.isSublist("kokkos tuning: muelu parameter mapping") &&
76 paramList.sublist("kokkos tuning: muelu parameter mapping").isParameter("kokkos context id")) {
77 // Time tuning separately
78 if (label != "")
79 timerName = "MueLu tuning time (" + label + ")";
80 else
81 timerName = "MueLu tuning time";
82 tm = Teuchos::TimeMonitor::getNewTimer(timerName);
83 tm->start();
84
85 MueLu::KokkosTuningInterface KokkosTuner(op->getMap()->getComm());
86 KokkosTuner.SetParameterList(paramList);
87 KokkosTuner.SetMueLuParameters(paramList);
88 tm->stop();
89 tm->incrementNumCalls();
90 tm = Teuchos::null;
91 }
92
93 // Setup Timer
94 if (label != "")
95 timerName = "MueLu setup time (" + label + ")";
96 else
97 timerName = "MueLu setup time";
98
99 tm = Teuchos::TimeMonitor::getNewTimer(timerName);
100 tm->start();
101
102 std::string syntaxStr = "parameterlist: syntax";
103 if (hasParamList && paramList.isParameter(syntaxStr) && paramList.get<std::string>(syntaxStr) == "ml") {
104 paramList.remove(syntaxStr);
105 std::string paramXML = MueLu::ML2MueLuParameterTranslator::translate(paramList, "");
106 paramList = *Teuchos::getParametersFromXmlString(paramXML);
107 }
108 mueLuFactory = rcp(new ParameterListInterpreter(paramList, op->getDomainMap()->getComm()));
109
110 // Create Hierarchy
111 RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
112 H->setlib(op->getDomainMap()->lib());
113
114 // Set fine level operator
115 H->GetLevel(0)->Set("A", op);
116 H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
117
118 // Stick the non-serializible data on the hierarchy.
119 HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory, *H, nonSerialList);
120
121 mueLuFactory->SetupHierarchy(*H);
122
123 tm->stop();
124 tm->incrementNumCalls();
125
126 if (H->GetVerbLevel() & Statistics0) {
127 const bool alwaysWriteLocal = true;
128 const bool writeGlobalStats = true;
129 const bool writeZeroTimers = false;
130 const bool ignoreZeroTimers = true;
131 const std::string filter = timerName;
132 Teuchos::TimeMonitor::summarize(op->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
133 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
134 }
135
136 tm->reset();
137
138 return H;
139}
140
149template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
150Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
151CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op,
152 const std::string& xmlFileName) {
153 Teuchos::ParameterList paramList;
154 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<Teuchos::ParameterList>(&paramList), *op->getDomainMap()->getComm());
155 return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
156}
157
165template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
166Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
167CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op) {
168 Teuchos::ParameterList paramList;
169 return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
170}
171
179template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
180void ReuseXpetraPreconditioner(const Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A,
182 std::string label = H->GetLevel(0)->getObjectLabel();
183
184 std::string timerName;
185 if (label != "")
186 timerName = "MueLu setup time (" + label + ")";
187 else
188 timerName = "MueLu setup time";
189 RCP<Teuchos::Time> tm = Teuchos::TimeMonitor::getNewTimer(timerName);
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
200 TEUCHOS_TEST_FOR_EXCEPTION(!H->GetNumLevels(), Exceptions::RuntimeError,
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(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
229 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
230 }
231
232 tm->reset();
233}
234
235} // namespace MueLu
236
237#define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
238
239#endif /* PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_ */
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
MueLu::DefaultNode Node
Exception throws to report errors in the internal logical of the program.
static void AddNonSerializableDataToHierarchy(HierarchyManager &HM, Hierarchy &H, const ParameterList &nonSerialList)
Add non-serializable data to Hierarchy.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
void SetParameterList(Teuchos::ParameterList &inParams)
void SetMueLuParameters(size_t kokkos_context_id, Teuchos::ParameterList &mueluParams, bool overwrite=true) const
static std::string translate(Teuchos::ParameterList &paramList, const std::string &defaultVals="")
: Translate ML parameters to MueLu parameter XML string
Namespace for MueLu classes and methods.
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.
@ Statistics0
Print statistics that do not involve significant additional computation.
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.
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,...