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
31namespace MueLu {
32
40
41template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
42Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
43CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op,
44 const Teuchos::ParameterList& inParamList) {
45 using SC = Scalar;
46 using LO = LocalOrdinal;
47 using GO = GlobalOrdinal;
48 using NO = Node;
49
54
55 bool hasParamList = inParamList.numParams();
56
57 RCP<HierarchyManager> mueLuFactory;
58
59 // Rip off non-serializable data before validation
60 Teuchos::ParameterList nonSerialList, paramList;
61 MueLu::ExtractNonSerializableData(inParamList, paramList, nonSerialList);
62
63 std::string label;
64 if (hasParamList && paramList.isParameter("hierarchy label")) {
65 label = paramList.get<std::string>("hierarchy label");
66 paramList.remove("hierarchy label");
67 } else
68 label = op->getObjectLabel();
69
70 std::string timerName;
71 if (label != "")
72 timerName = "MueLu setup time (" + label + ")";
73 else
74 timerName = "MueLu setup time";
75 RCP<Teuchos::Time> tm = Teuchos::TimeMonitor::getNewTimer(timerName);
76 tm->start();
77
78 std::string syntaxStr = "parameterlist: syntax";
79 if (hasParamList && paramList.isParameter(syntaxStr) && paramList.get<std::string>(syntaxStr) == "ml") {
80 paramList.remove(syntaxStr);
81 std::string paramXML = MueLu::ML2MueLuParameterTranslator::translate(paramList, "");
82 paramList = *Teuchos::getParametersFromXmlString(paramXML);
83 }
84 mueLuFactory = rcp(new ParameterListInterpreter(paramList, op->getDomainMap()->getComm()));
85
86 // Create Hierarchy
87 RCP<Hierarchy> H = mueLuFactory->CreateHierarchy(label);
88 H->setlib(op->getDomainMap()->lib());
89
90 // Set fine level operator
91 H->GetLevel(0)->Set("A", op);
92 H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
93
94 // Stick the non-serializible data on the hierarchy.
95 HierarchyUtils::AddNonSerializableDataToHierarchy(*mueLuFactory, *H, nonSerialList);
96
97 mueLuFactory->SetupHierarchy(*H);
98
99 tm->stop();
100 tm->incrementNumCalls();
101
102 if (H->GetVerbLevel() & Statistics0) {
103 const bool alwaysWriteLocal = true;
104 const bool writeGlobalStats = true;
105 const bool writeZeroTimers = false;
106 const bool ignoreZeroTimers = true;
107 const std::string filter = timerName;
108 Teuchos::TimeMonitor::summarize(op->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
109 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
110 }
111
112 tm->reset();
113
114 return H;
115}
116
124
125template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
126Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
127CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op,
128 const std::string& xmlFileName) {
129 Teuchos::ParameterList paramList;
130 Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, Teuchos::Ptr<Teuchos::ParameterList>(&paramList), *op->getDomainMap()->getComm());
132}
133
140
141template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
142Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
143CreateXpetraPreconditioner(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> op) {
144 Teuchos::ParameterList paramList;
146}
147
155template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
156void ReuseXpetraPreconditioner(const Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A,
158 std::string label = H->GetLevel(0)->getObjectLabel();
159
160 std::string timerName;
161 if (label != "")
162 timerName = "MueLu setup time (" + label + ")";
163 else
164 timerName = "MueLu setup time";
165 RCP<Teuchos::Time> tm = Teuchos::TimeMonitor::getNewTimer(timerName);
166 tm->start();
167
168 typedef Scalar SC;
169 typedef LocalOrdinal LO;
170 typedef GlobalOrdinal GO;
171 typedef Node NO;
172
173 typedef Xpetra::Matrix<SC, LO, GO, NO> Matrix;
174 typedef Xpetra::Operator<SC, LO, GO, NO> Operator;
175
176 TEUCHOS_TEST_FOR_EXCEPTION(!H->GetNumLevels(), Exceptions::RuntimeError,
177 "MueLu::ReuseXpetraPreconditioner: Hierarchy has no levels in it");
178 TEUCHOS_TEST_FOR_EXCEPTION(!H->GetLevel(0)->IsAvailable("A"), Exceptions::RuntimeError,
179 "MueLu::ReuseXpetraPreconditioner: Hierarchy has no fine level operator");
180 RCP<Level> level0 = H->GetLevel(0);
181
182 RCP<Operator> O0 = level0->Get<RCP<Operator>>("A");
183 RCP<Matrix> A0 = Teuchos::rcp_dynamic_cast<Matrix>(O0);
184
185 if (!A0.is_null()) {
186 // If a user provided a "number of equations" argument in a parameter list
187 // during the initial setup, we must honor that settings and reuse it for
188 // all consequent setups.
189 A->SetFixedBlockSize(A0->GetFixedBlockSize());
190 }
191 level0->Set("A", A);
192
193 H->SetupRe();
194
195 tm->stop();
196 tm->incrementNumCalls();
197
198 if (H->GetVerbLevel() & Statistics0) {
199 const bool alwaysWriteLocal = true;
200 const bool writeGlobalStats = true;
201 const bool writeZeroTimers = false;
202 const bool ignoreZeroTimers = true;
203 const std::string filter = timerName;
204 Teuchos::TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), H->GetOStream(Statistics0), alwaysWriteLocal, writeGlobalStats,
205 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
206 }
207
208 tm->reset();
209}
210
211} // namespace MueLu
212
213#define XPETRA_CREATEXPETRAPRECONDITIONER_SHORT
214
215#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.
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,...