MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_MultiPhys_def.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 MUELU_MULTIPHYS_DEF_HPP
11#define MUELU_MULTIPHYS_DEF_HPP
12
13#include <sstream>
14#include "MueLu_ConfigDefs.hpp"
15
16#include "Xpetra_Map.hpp"
17#include "Xpetra_CrsMatrixUtils.hpp"
18#include "Xpetra_MatrixUtils.hpp"
19
21#include "MueLu_SaPFactory.hpp"
22#include "MueLu_AggregationExportFactory.hpp"
23#include "MueLu_Utilities.hpp"
24#include "MueLu_Level.hpp"
25#include "MueLu_Hierarchy.hpp"
26#include "MueLu_RAPFactory.hpp"
27#include "MueLu_Monitor.hpp"
28#include "MueLu_PerfUtils.hpp"
29#include "MueLu_ParameterListInterpreter.hpp"
31#include <MueLu_HierarchyUtils.hpp>
35
36#ifdef HAVE_MUELU_CUDA
37#include "cuda_profiler_api.h"
38#endif
39
40namespace MueLu {
41
42template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
43const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> MultiPhys<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getDomainMap() const {
44 return AmatMultiphysics_->getDomainMap();
45}
46
47template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
48const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> MultiPhys<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getRangeMap() const {
49 return AmatMultiphysics_->getRangeMap();
50}
51
52template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
54 // this operator only makes sense for the Combo when using TransP for R
55
56 list.set("multigrid algorithm", "combine");
57 list.set("combine: numBlks", nBlks_);
58
59 // Make sure verbosity gets passed to the sublists
60 std::string verbosity = list.get("verbosity", "high");
62
64 for (int i = 0; i < nBlks_; i++) {
65 std::string listName = "subblockList" + Teuchos::toString(i);
66 if (list.isSublist(listName)) {
67 arrayOfParamLists_[i] = Teuchos::rcpFromRef(list.sublist(listName));
68 } else
69 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "Must provide sublist " + listName);
70
71 arrayOfParamLists_[i]->set("verbosity", arrayOfParamLists_[i]->get("verbosity", verbosity));
72 arrayOfParamLists_[i]->set("smoother: pre or post", "none");
73 arrayOfParamLists_[i]->set("smoother: type", "none");
74 }
75
76 // Are we using Kokkos?
77 useKokkos_ = !Node::is_serial;
78 useKokkos_ = list.get("use kokkos refactor", useKokkos_);
79
80 paramListMultiphysics_ = Teuchos::rcpFromRef(list);
81}
82
83template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
85 /*
86
87 Create a set of AMG hierarchies whose interpolation matrices are used to build on combined
88 AMG hierarchy for a multiphysics problem
89
90 */
91
92 //#ifdef HAVE_MUELU_CUDA
93 // if (paramListMultiphysics_.get<bool>("multiphysics: cuda profile setup", false)) cudaProfilerStart();
94 //#endif
95
96 std::string timerLabel;
97 if (reuse)
98 timerLabel = "MueLu MultiPhys: compute (reuse)";
99 else
100 timerLabel = "MueLu MultiPhys: compute";
101 RCP<Teuchos::TimeMonitor> tmCompute = getTimer(timerLabel);
102
104 // Generate the (iii,iii) Hierarchy
105
106 for (int iii = 0; iii < nBlks_; iii++) {
107 if (arrayOfCoords_ != Teuchos::null) {
108 arrayOfParamLists_[iii]->sublist("user data").set("Coordinates", arrayOfCoords_[iii]);
109 }
110
111 bool wantToRepartition = false;
112 if (paramListMultiphysics_->isParameter("repartition: enable"))
113 wantToRepartition = paramListMultiphysics_->get<bool>("repartition: enable");
114
115 arrayOfParamLists_[iii]->set("repartition: enable", wantToRepartition);
116 arrayOfParamLists_[iii]->set("repartition: rebalance P and R", true);
117 arrayOfParamLists_[iii]->set("repartition: explicit via new copy rebalance P and R", true);
118
119 if (paramListMultiphysics_->isParameter("repartition: use subcommunicators"))
120 arrayOfParamLists_[iii]->set("repartition: use subcommunicators", paramListMultiphysics_->isParameter("repartition: use subcommunicators"));
121 else
122 arrayOfParamLists_[iii]->set("repartition: use subcommunicators", true);
123 }
124 // repartitioning should only happen when createing the individual P's , not
125 // when combiing them
126
127 paramListMultiphysics_->set<bool>("repartition: enable", false);
128
129 LO maxLevels = 9999;
130 for (int i = 0; i < nBlks_; i++) {
131 std::string operatorLabel = "MultiPhys (" + Teuchos::toString(i) + "," + Teuchos::toString(i) + ")";
132 arrayOfAuxMatrices_[i]->setObjectLabel(operatorLabel);
134 LO tempNlevels = arrayOfHierarchies_[i]->GetGlobalNumLevels();
135 if (tempNlevels < maxLevels) maxLevels = tempNlevels;
136 }
137
138 hierarchyMultiphysics_ = rcp(new Hierarchy("Combo"));
139 for (LO i = 0; i < maxLevels; i++) {
140 hierarchyMultiphysics_->AddNewLevel();
141 }
142 for (int i = 0; i < nBlks_; i++) {
143 std::string subblkName = "Psubblock" + Teuchos::toString(i);
145 }
146 paramListMultiphysics_->set("coarse: max size", 1);
147 paramListMultiphysics_->set("max levels", maxLevels);
148
149 AmatMultiphysics_->setObjectLabel("A: block " + Teuchos::toString(nBlks_) + " x " + Teuchos::toString(nBlks_) + "multiphysics matrix");
150
151 // Rip off non-serializable data before validation
152 Teuchos::ParameterList nonSerialListMultiphysics, processedListMultiphysics;
153 MueLu::ExtractNonSerializableData(*paramListMultiphysics_, processedListMultiphysics, nonSerialListMultiphysics);
154
155 // Rip off the subblock List stuff as we don't need it any more and I think it messes up validator
156
157 Teuchos::ParameterList stripped;
158 for (ParameterList::ConstIterator inListEntry = processedListMultiphysics.begin(); inListEntry != processedListMultiphysics.end(); inListEntry++) {
159 const std::string& levelName = inListEntry->first;
160 if (levelName.find("subblockList") != 0) stripped.setEntry(inListEntry->first, inListEntry->second);
161 }
162
163 RCP<HierarchyManager<SC, LO, GO, NO>> mueLuFactory = rcp(new ParameterListInterpreter<SC, LO, GO, NO>(stripped, AmatMultiphysics_->getDomainMap()->getComm()));
164 hierarchyMultiphysics_->setlib(AmatMultiphysics_->getDomainMap()->lib());
165 hierarchyMultiphysics_->SetProcRankVerbose(AmatMultiphysics_->getDomainMap()->getComm()->getRank());
166
167 // We don't need nullspace or coordinates, since we don't use them when just combining prolongators that have been already created
168 hierarchyMultiphysics_->GetLevel(0)->Set("A", AmatMultiphysics_);
169
170 // Stick the non-serializible data on the hierarchy.
171 // Not sure that we need this, since we don't use it in building the multiphysics hierarchy
173 mueLuFactory->SetupHierarchy(*hierarchyMultiphysics_);
174
176}
177
178template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
179Teuchos::RCP<Teuchos::TimeMonitor> MultiPhys<Scalar, LocalOrdinal, GlobalOrdinal, Node>::getTimer(std::string name, RCP<const Teuchos::Comm<int>> comm) const {
180 if (IsPrint(Timings)) {
181 if (!syncTimers_)
182 return Teuchos::rcp(new Teuchos::TimeMonitor(*Teuchos::TimeMonitor::getNewTimer(name)));
183 else {
184 if (comm.is_null())
185 return Teuchos::rcp(new Teuchos::SyncTimeMonitor(*Teuchos::TimeMonitor::getNewTimer(name), AmatMultiphysics_->getRowMap()->getComm().ptr()));
186 else
187 return Teuchos::rcp(new Teuchos::SyncTimeMonitor(*Teuchos::TimeMonitor::getNewTimer(name), comm.ptr()));
188 }
189 } else
190 return Teuchos::null;
191}
192
193template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
194void MultiPhys<Scalar, LocalOrdinal, GlobalOrdinal, Node>::resetMatrix(RCP<Matrix> AmatMultiphysics_new, bool ComputePrec) {
195 bool reuse = !AmatMultiphysics_.is_null();
196 AmatMultiphysics_ = AmatMultiphysics_new;
197 if (ComputePrec) compute(reuse);
198}
199
200template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
201void MultiPhys<Scalar, LocalOrdinal, GlobalOrdinal, Node>::applyInverse(const MultiVector& RHS, MultiVector& X) const {
202 hierarchyMultiphysics_->Iterate(RHS, X, 1, true);
203}
204
205template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
206void MultiPhys<Scalar, LocalOrdinal, GlobalOrdinal, Node>::apply(const MultiVector& RHS, MultiVector& X,
207 Teuchos::ETransp /* mode */,
208 Scalar /* alpha */,
209 Scalar /* beta */) const {
210 RCP<Teuchos::TimeMonitor> tm = getTimer("MueLu MultiPhys: solve");
211 hierarchyMultiphysics_->Iterate(RHS, X, 1, true);
212}
213
214template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
218
219template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
221 initialize(const Teuchos::RCP<Matrix>& AmatMultiPhysics,
222 const Teuchos::ArrayRCP<RCP<Matrix>> arrayOfAuxMatrices,
223 const Teuchos::ArrayRCP<Teuchos::RCP<MultiVector>> arrayOfNullspaces,
224 const Teuchos::ArrayRCP<Teuchos::RCP<RealValuedMultiVector>> arrayOfCoords,
225 const int nBlks,
226 Teuchos::ParameterList& List) {
228 for (int i = 0; i < nBlks_; i++) arrayOfHierarchies_[i] = Teuchos::null;
229
230 // Default settings
231 useKokkos_ = false;
232 enable_reuse_ = false;
233 syncTimers_ = false;
234
235 // set parameters
236 setParameters(List);
237
238} // initialize
239
240template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
242 describe(Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel /* verbLevel */) const {
243 std::ostringstream oss;
244
245 RCP<const Teuchos::Comm<int>> comm = AmatMultiphysics_->getDomainMap()->getComm();
246
247 oss << "\n--------------------------------------------------------------------------------\n"
248 << "--- MultiPhysics Summary ---\n"
249 "--------------------------------------------------------------------------------"
250 << std::endl;
251 oss << std::endl;
252
253 GlobalOrdinal numRows;
254 GlobalOrdinal nnz;
255
256 AmatMultiphysics_->getRowMap()->getComm()->barrier();
257
258 for (int i = 0; i < nBlks_; i++) {
259 numRows = arrayOfAuxMatrices_[i]->getGlobalNumRows();
260 nnz = arrayOfAuxMatrices_[i]->getGlobalNumEntries();
261 Xpetra::global_size_t tt = numRows;
262 int rowspacer = 3;
263 while (tt != 0) {
264 tt /= 10;
265 rowspacer++;
266 }
267 tt = nnz;
268 int nnzspacer = 2;
269 while (tt != 0) {
270 tt /= 10;
271 nnzspacer++;
272 }
273
274 oss << "block " << std::setw(rowspacer) << " rows " << std::setw(nnzspacer) << " nnz " << std::setw(9) << " nnz/row" << std::endl;
275 oss << "(" << Teuchos::toString(i) << ", " << Teuchos::toString(i) << ")" << std::setw(rowspacer) << numRows << std::setw(nnzspacer) << nnz << std::setw(9) << as<double>(nnz) / numRows << std::endl;
276 }
277 oss << std::endl;
278
279 out << oss.str();
280
281 for (int i = 0; i < nBlks_; i++) {
282 arrayOfHierarchies_[i]->describe(out, GetVerbLevel());
283 }
284
285} // describe
286
287} // namespace MueLu
288
289#define MUELU_MULTIPHYS_SHORT
290#endif // ifdef MUELU_MULTIPHYS_DEF_HPP
Various adapters that will create a MueLu preconditioner that is an Xpetra::Matrix.
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
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.
static void CopyBetweenHierarchies(Hierarchy &fromHierarchy, Hierarchy &toHierarchy, const std::string fromLabel, const std::string toLabel, const std::string dataType)
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
Teuchos::RCP< Hierarchy > hierarchyMultiphysics_
const Teuchos::RCP< const Map > getRangeMap() const
Returns the Xpetra::Map object associated with the range of this operator.
Teuchos::RCP< Teuchos::TimeMonitor > getTimer(std::string name, RCP< const Teuchos::Comm< int > > comm=Teuchos::null) const
get a (synced) timer
Teuchos::ArrayRCP< Teuchos::RCP< Matrix > > arrayOfAuxMatrices_
void compute(bool reuse=false)
Setup the preconditioner.
void applyInverse(const MultiVector &RHS, MultiVector &X) const
apply standard MultiPhys cycle
void setParameters(Teuchos::ParameterList &list)
Set parameters.
void resetMatrix(Teuchos::RCP< Matrix > SM_Matrix_new, bool ComputePrec=true)
Reset system matrix.
Teuchos::RCP< Matrix > AmatMultiphysics_
Hierarchies: used to define P for (0,0)-block, .... (nBlks_-1,nBlks_-1) block.
const Teuchos::RCP< const Map > getDomainMap() const
Returns the Xpetra::Map object associated with the domain of this operator.
Teuchos::ArrayRCP< Teuchos::RCP< Hierarchy > > arrayOfHierarchies_
bool hasTransposeApply() const
Indicates whether this operator supports applying the adjoint operator.
Teuchos::RCP< Teuchos::ParameterList > paramListMultiphysics_
Teuchos::ArrayRCP< Teuchos::RCP< RealValuedMultiVector > > arrayOfCoords_
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_HIGH) const
void initialize(const Teuchos::RCP< Matrix > &AmatMultiPhysics, const Teuchos::ArrayRCP< RCP< Matrix > > arrayOfAuxMatrices, const Teuchos::ArrayRCP< Teuchos::RCP< MultiVector > > arrayOfNullspaces, const Teuchos::ArrayRCP< Teuchos::RCP< RealValuedMultiVector > > arrayOfCoords, const int nBlks, Teuchos::ParameterList &List)
void apply(const MultiVector &X, MultiVector &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Teuchos::ArrayRCP< Teuchos::RCP< Teuchos::ParameterList > > arrayOfParamLists_
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
VerbLevel GetVerbLevel() const
Get the verbosity level.
bool IsPrint(MsgType type, int thisProcRankOnly=-1) const
Find out whether we need to print out information for a specific message type.
static void SetDefaultVerbLevel(const VerbLevel defaultVerbLevel)
Set the default (global) verbosity level.
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.
@ Runtime0
One-liner description of what is happening.
@ Timings
Print all timing information.
MsgType toVerbLevel(const std::string &verbLevelStr)
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,...