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