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 // Need to check if Muelu option is inconsistent with user data provided
109 bool Minv_Supplied = false, M_Supplied = false, MinvA_Supplied = false;
110 if (inParamList.isSublist("user data")) {
111 const Teuchos::ParameterList& userList = inParamList.sublist("user data");
112 if (userList.isParameter("M")) M_Supplied = true;
113 if (userList.isParameter("Minv")) Minv_Supplied = true;
114 if (userList.isParameter("MinvA")) Minv_Supplied = true;
115 }
116 if (inParamList.isParameter("aggregation: strength-of-connection: matrix") && (inParamList.get<std::string>("aggregation: strength-of-connection: matrix") == "MinvA")) {
117 if (inParamList.isSublist("project auxiliary matrices")) {
118 auto projectList = inParamList.sublist("project auxiliary matrices");
119 TEUCHOS_TEST_FOR_EXCEPTION(projectList.isParameter("M") && !M_Supplied, Exceptions::Incompatible, "MueLu_CreateXpetraPreconditioner: Must supply M as it is listed in the project auxiliary matrices sublist");
120 TEUCHOS_TEST_FOR_EXCEPTION(projectList.isParameter("Minv") && (projectList.get("Minv", "") == "NoFactory") && !Minv_Supplied, Exceptions::Incompatible,
121 "MueLu_CreateXpetraPreconditioner: Must supply Minv as NoFactory is listed as supplier of Minv in the project auxiliary matrices sublist");
122 TEUCHOS_TEST_FOR_EXCEPTION(projectList.isParameter("MinvA") && (projectList.get("MinvA", "") == "NoFactory") && !MinvA_Supplied, Exceptions::Incompatible,
123 "MueLu_CreateXpetraPreconditioner: Must supply MinvA as NoFactory is listed as supplier of MinvA in the project auxiliary matrices sublist");
124 } else { // default behavior if sublist("project auxiliary matrices") not user-supplied requires "M" to be user-supplied.
125 TEUCHOS_TEST_FOR_EXCEPTION(!M_Supplied, Exceptions::Incompatible, "MueLu_CreateXpetraPreconditioner: Must supply M when 'aggregation: strength-of-connection: matrix'= MinvA and sublist('project auxiliary matrices') not supplied.");
126 }
127 }
128
129 mueLuFactory = rcp(new ParameterListInterpreter(paramList, op->getDomainMap()->getComm()));
130
131 // Create Hierarchy
132 RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
133 H->setlib(op->getDomainMap()->lib());
134
135 // Set fine level operator
136 H->GetLevel(0)->Set("A", op);
137 H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
138
139 // Stick the non-serializible data on the hierarchy.
140 HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory, *H, nonSerialList);
141
142 mueLuFactory->SetupHierarchy(*H);
143
144 tm->stop();
145 tm->incrementNumCalls();
146
147 if (H->GetVerbLevel() & Statistics0) {
148 const bool alwaysWriteLocal = true;
149 const bool writeGlobalStats = true;
150 const bool writeZeroTimers = false;
151 const bool ignoreZeroTimers = true;
152 const std::string filter = timerName;
153 Teuchos::TimeMonitor::summarize(op->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
154 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
155 }
156
157 tm->reset();
158
159 return H;
160}
161
170template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
171Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
172CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op,
173 const std::string& xmlFileName) {
174 Teuchos::ParameterList paramList;
175 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<Teuchos::ParameterList>(&paramList), *op->getDomainMap()->getComm());
176 return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
177}
178
186template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
187Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
188CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op) {
189 Teuchos::ParameterList paramList;
190 return CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(op, paramList);
191}
192
200template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
201void ReuseXpetraPreconditioner(const Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A,
203 std::string label = H->GetLevel(0)->getObjectLabel();
204
205 std::string timerName;
206 if (label != "")
207 timerName = "MueLu setup time (" + label + ")";
208 else
209 timerName = "MueLu setup time";
210 RCP<Teuchos::Time> tm = Teuchos::TimeMonitor::getNewTimer(timerName);
211 tm->start();
212
213 typedef Scalar SC;
214 typedef LocalOrdinal LO;
215 typedef GlobalOrdinal GO;
216 typedef Node NO;
217
218 typedef Xpetra::Matrix<SC, LO, GO, NO> Matrix;
219 typedef Xpetra::Operator<SC, LO, GO, NO> Operator;
220
221 TEUCHOS_TEST_FOR_EXCEPTION(!H->GetNumLevels(), Exceptions::RuntimeError,
222 "MueLu::ReuseXpetraPreconditioner: Hierarchy has no levels in it");
223 TEUCHOS_TEST_FOR_EXCEPTION(!H->GetLevel(0)->IsAvailable("A"), Exceptions::RuntimeError,
224 "MueLu::ReuseXpetraPreconditioner: Hierarchy has no fine level operator");
225 RCP<Level> level0 = H->GetLevel(0);
226
227 RCP<Operator> O0 = level0->Get<RCP<Operator>>("A");
228 RCP<Matrix> A0 = Teuchos::rcp_dynamic_cast<Matrix>(O0);
229
230 if (!A0.is_null()) {
231 // If a user provided a "number of equations" argument in a parameter list
232 // during the initial setup, we must honor that settings and reuse it for
233 // all consequent setups.
234 A->SetFixedBlockSize(A0->GetFixedBlockSize());
235 }
236 level0->Set("A", A);
237
238 H->SetupRe();
239
240 tm->stop();
241 tm->incrementNumCalls();
242
243 if (H->GetVerbLevel() & Statistics0) {
244 const bool alwaysWriteLocal = true;
245 const bool writeGlobalStats = true;
246 const bool writeZeroTimers = false;
247 const bool ignoreZeroTimers = true;
248 const std::string filter = timerName;
249 Teuchos::TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
250 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
251 }
252
253 tm->reset();
254}
255
256} // namespace MueLu
257
258#define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
259
260#endif /* PACKAGES_MUELU_ADAPTERS_XPETRA_MUELU_CREATEXPETRAPRECONDITIONER_HPP_ */
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
MueLu::DefaultNode Node
Exception throws to report incompatible objects (like maps).
Exception throws to report errors in the internal logical of the program.
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
static void AddNonSerializableDataToHierarchy(HierarchyManager &HM, Hierarchy &H, const ParameterList &nonSerialList)
Add non-serializable data to Hierarchy.
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,...