Debian Codegraph @

packages/muelu/research/caglusa/auxiliaryOperators.hpp

raw
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 AUXILIARYOPERATORS_HPP
11 #define AUXILIARYOPERATORS_HPP
12
13 #include <Xpetra_IO.hpp>
14 #include <MueLu_IOhelpers.hpp>
15
16 #include <Xpetra_Matrix.hpp>
17 #include <Xpetra_MatrixUtils.hpp>
18 #include <Xpetra_HierarchicalOperator_decl.hpp>
19 #include <Xpetra_HierarchicalOperator_def.hpp>
20
21 #include <MueLu.hpp>
22 #include <MueLu_Level.hpp>
23 #include <MueLu_MasterList.hpp>
24 #include <MueLu_HierarchyManager.hpp>
25 #include <MueLu_ParameterListInterpreter.hpp>
26 #include <MueLu_RAPFactory.hpp>
27 #include <MueLu_AmalgamationFactory.hpp>
28 #include <MueLu_CoalesceDropFactory_kokkos.hpp>
29 #include <MueLu_ThresholdAFilterFactory.hpp>
30
31 namespace MueLu {
32
33 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
34 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
35 buildDistanceLaplacian(RCP<const Xpetra::CrsGraph<LocalOrdinal, GlobalOrdinal, Node> >& graph,
36 RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node> >& coords) {
37 const Scalar ONE = Teuchos::ScalarTraits<Scalar>::one();
38 const Scalar ZERO = Teuchos::ScalarTraits<Scalar>::zero();
39 const LocalOrdinal INV = Teuchos::OrdinalTraits<LocalOrdinal>::invalid();
40 auto distLapl = Xpetra::MatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(graph);
41
42 auto rowMap = graph->getRowMap();
43 auto colMap = graph->getColMap();
44 auto ghosted_coords = Xpetra::MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(colMap, coords->getNumVectors());
45 ghosted_coords->doImport(*coords, *graph->getImporter(), Xpetra::INSERT);
46
47 {
48 auto lcl_coords = coords->getHostLocalView(Xpetra::Access::ReadOnly);
49 auto lcl_ghosted_coords = ghosted_coords->getHostLocalView(Xpetra::Access::ReadOnly);
50 auto lcl_distLapl = distLapl->getLocalMatrixHost();
51
52 // TODO: parallel_for
53 for (LocalOrdinal rlid = 0; rlid < lcl_distLapl.numRows(); ++rlid) {
54 auto row = lcl_distLapl.row(rlid);
55 Scalar diag = ZERO;
56 LocalOrdinal diagIndex = INV;
57 for (LocalOrdinal k = 0; k < row.length; ++k) {
58 LocalOrdinal clid = row.colidx(k);
59 if (rowMap->getGlobalElement(rlid) == colMap->getGlobalElement(clid)) {
60 diagIndex = k;
61 } else {
62 Scalar dist = ZERO;
63 for (size_t j = 0; j < lcl_coords.extent(1); j++) {
64 auto s = lcl_coords(rlid, j) - lcl_ghosted_coords(clid, j);
65 dist += s * s;
66 }
67 row.value(k) = ONE / std::sqrt(dist);
68 diag -= row.value(k);
69 }
70 }
71 TEUCHOS_ASSERT(diagIndex != INV);
72 row.value(diagIndex) = diag;
73 }
74 }
75 distLapl->fillComplete();
76 return distLapl;
77 }
78
79 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
80 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
81 constructAuxiliaryOperator(RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> > op,
82 Teuchos::ParameterList& problemParams) {
83 using IO = Xpetra::IO<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
84 using IOhelpers = MueLu::IOhelpers<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
85
86 RCP<Xpetra::HierarchicalOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> > hop = rcp_dynamic_cast<Xpetra::HierarchicalOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(op);
87
88 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > auxOp;
89
90 const std::string auxOpStr = problemParams.get<std::string>("auxiliary operator");
91
92 if ((auxOpStr == "near") || (auxOpStr == "distanceLaplacian")) {
93 if (hop.is_null())
94 auxOp = rcp_dynamic_cast<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(op, true);
95 else
96 auxOp = hop->nearFieldMatrix();
97 #ifdef MUELU_HIERARCHICAL_DEBUG
98 // CoalesceDropFactory_kokkos assumes fitted row and column maps
99 Xpetra::MatrixUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::checkLocalRowMapMatchesColMap(*auxOp);
100 #endif
101 // coordinates
102 auto coords = Xpetra::IO<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>::ReadMultiVector(problemParams.get<std::string>("coordinates"), op->getRangeMap());
103
104 {
105 // apply dropping to auxOp
106 Level fineLevel;
107 fineLevel.SetFactoryManager(Teuchos::null);
108 fineLevel.SetLevelID(0);
109 fineLevel.Set("A", auxOp);
110 fineLevel.Set("Coordinates", coords);
111 fineLevel.Set("DofsPerNode", 1);
112 fineLevel.setlib(auxOp->getDomainMap()->lib());
113 auto amalgFact = rcp(new AmalgamationFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>());
114 auto dropFact = rcp(new CoalesceDropFactory_kokkos<Scalar, LocalOrdinal, GlobalOrdinal, Node>());
115 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
116
117 double dropTol = problemParams.get<double>("drop tolerance");
118 // double dropTol = 0.1; // 1D
119 // double dropTol = 0.03; // 2D
120 std::string dropScheme = "classical";
121 dropFact->SetParameter("aggregation: drop tol", Teuchos::ParameterEntry(dropTol));
122 dropFact->SetParameter("aggregation: drop scheme", Teuchos::ParameterEntry(dropScheme));
123
124 fineLevel.Request("A", dropFact.get());
125 fineLevel.Get("A", auxOp, dropFact.get());
126 }
127
128 {
129 // filter out small entries in auxOp
130 Level fineLevel;
131 fineLevel.SetFactoryManager(Teuchos::null);
132 fineLevel.SetLevelID(0);
133 fineLevel.Set("A", auxOp);
134 auto filterFact = rcp(new ThresholdAFilterFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>("A", 1.0e-8, true, -1));
135 fineLevel.Request("A", filterFact.get());
136 filterFact->Build(fineLevel);
137 auxOp = fineLevel.Get<RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >("A", filterFact.get());
138 }
139
140 if (auxOpStr == "distanceLaplacian") {
141 // build distance Laplacian using graph of auxOp and coordinates
142 auto graph = auxOp->getCrsGraph();
143 auxOp = buildDistanceLaplacian<Scalar, LocalOrdinal, GlobalOrdinal, Node>(graph, coords);
144 }
145
146 } else {
147 const bool readBinary = problemParams.get<bool>("read binary", false);
148 const bool readLocal = problemParams.get<bool>("read local", false);
149
150 // colmap of auxiliary operator
151 auto aux_colmap = IO::ReadMap(problemParams.get<std::string>("aux colmap"), op->getRangeMap()->lib(), op->getRangeMap()->getComm(), readBinary);
152
153 auxOp = IOhelpers::Read(auxOpStr, op->getRangeMap(), aux_colmap, op->getRangeMap(), op->getRangeMap(), true, readBinary, readLocal);
154 }
155
156 return auxOp;
157 }
158
159 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
160 RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
161 constructHierarchyFromAuxiliary(RCP<Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> > op,
162 RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> > auxH,
163 Teuchos::ParameterList& params,
164 Teuchos::FancyOStream& out) {
165 using op_type = Xpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
166 using hop_type = Xpetra::HierarchicalOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
167 using mat_type = Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
168
169 params.set("coarse: max size", 1);
170 params.set("max levels", auxH->GetNumLevels());
171
172 const bool implicitTranspose = params.get("transpose: use implicit", MueLu::MasterList::getDefault<bool>("transpose: use implicit"));
173
174 auto hop = rcp_dynamic_cast<hop_type>(op);
175 if (!hop.is_null())
176 op->describe(out, Teuchos::VERB_EXTREME);
177
178 RCP<Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> > H = rcp(new Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>());
179 RCP<Level> lvl = H->GetLevel(0);
180 auto op_mat = Teuchos::rcp_dynamic_cast<mat_type>(op);
181 if (!op_mat.is_null())
182 lvl->Set("A", op_mat);
183 else {
184 lvl->Set("A", Teuchos::rcp_dynamic_cast<op_type>(op, true));
185 }
186 // lvl->Set("Coordinates", coords);
187 for (int lvlNo = 1; lvlNo < auxH->GetNumLevels(); lvlNo++) {
188 RCP<Level> fineLvl = H->GetLevel(lvlNo - 1);
189 H->AddNewLevel();
190 lvl = H->GetLevel(lvlNo);
191 RCP<Level> auxLvl = auxH->GetLevel(lvlNo);
192 // auto mgr = auxLvl->GetFactoryManager();
193 // auxLvl->print(std::cout, MueLu::Debug);
194
195 RCP<mat_type> P = auxLvl->Get<RCP<mat_type> >("P");
196 lvl->Set("P", P);
197 params.sublist("level " + std::to_string(lvlNo)).set("P", P);
198
199 if (!implicitTranspose) {
200 TEUCHOS_ASSERT(auxLvl->IsAvailable("R"));
201 RCP<mat_type> R = auxLvl->Get<RCP<mat_type> >("R");
202 lvl->Set("R", R);
203 params.sublist("level " + std::to_string(lvlNo)).set("R", R);
204 }
205
206 if (fineLvl->IsType<RCP<op_type> >("A")) {
207 RCP<op_type> fineAOp = fineLvl->Get<RCP<op_type> >("A");
208 auto fineA = rcp_dynamic_cast<hop_type>(fineAOp);
209 auto coarseA = fineA->restrict(P);
210
211 #ifdef MUELU_HIERARCHICAL_DEBUG
212 {
213 // Test that the Galerkin product worked
214 using MagnitudeType = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
215 const Scalar one = Teuchos::ScalarTraits<Scalar>::one();
216 const MagnitudeType tol = 10000 * Teuchos::ScalarTraits<MagnitudeType>::eps();
217 auto testLHS = MultiVectorFactory::Build(coarseA->getDomainMap(), 1);
218 auto testRHS_HOp_coarse = MultiVectorFactory::Build(coarseA->getRangeMap(), 1);
219 auto testRHS_HOp_fine = MultiVectorFactory::Build(coarseA->getRangeMap(), 1);
220 auto temp1 = MultiVectorFactory::Build(fineA->getDomainMap(), 1);
221 auto temp2 = MultiVectorFactory::Build(fineA->getRangeMap(), 1);
222 testLHS->putScalar(one);
223 coarseA->apply(*testLHS, *testRHS_HOp_coarse);
224 P->apply(*testLHS, *temp1);
225 fineA->apply(*temp1, *temp2);
226 P->apply(*temp2, *testRHS_HOp_fine, Teuchos::TRANS);
227 testRHS_HOp_fine->update(one, *testRHS_HOp_coarse, -one);
228 auto norm = testRHS_HOp_fine->getVector(0)->norm2();
229 out << "|P^T*op_fine*P*1 - op_H_coarse*1| = " << norm << std::endl;
230 TEUCHOS_ASSERT(norm < tol);
231 }
232 #endif
233
234 if ((lvlNo + 1 == auxH->GetNumLevels()) || !coarseA->hasFarField() || coarseA->denserThanDenseMatrix()) {
235 // coarseA->describe(out, Teuchos::VERB_EXTREME);
236
237 auto matA = coarseA->toMatrix();
238
239 #ifdef MUELU_HIERARCHICAL_DEBUG
240 {
241 // test that the conversion to Crs format worked
242 using MagnitudeType = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
243 const Scalar one = Teuchos::ScalarTraits<Scalar>::one();
244 const MagnitudeType tol = 10000 * Teuchos::ScalarTraits<MagnitudeType>::eps();
245 auto testLHS = MultiVectorFactory::Build(coarseA->getDomainMap(), 1);
246 auto testRHS_HOp = MultiVectorFactory::Build(coarseA->getRangeMap(), 1);
247 auto testRHS_dense = MultiVectorFactory::Build(coarseA->getRangeMap(), 1);
248 testLHS->putScalar(one);
249 coarseA->apply(*testLHS, *testRHS_HOp);
250 matA->apply(*testLHS, *testRHS_dense);
251 testRHS_dense->update(one, *testRHS_HOp, -one);
252 auto norm = testRHS_dense->getVector(0)->norm2();
253 out << "|op_dense*1 - op_H*1| = " << norm << std::endl;
254 TEUCHOS_ASSERT(norm < tol);
255 }
256 #endif
257
258 using std::endl;
259 using std::setw;
260 const size_t numRows = matA->getRowMap()->getGlobalNumElements();
261 const size_t nnz = matA->getGlobalNumEntries();
262 const double nnzPerRow = Teuchos::as<double>(nnz) / numRows;
263 std::ostringstream oss;
264 oss << std::left;
265 // oss << setw(9) << "rows" << setw(12) << "nnz" << setw(14) << "nnz/row" << setw(12) << endl;
266 oss << setw(9) << numRows << setw(12) << nnz << setw(14) << nnzPerRow << endl;
267 out << oss.str();
268
269 lvl->Set("A", matA);
270 } else {
271 coarseA->describe(out, Teuchos::VERB_EXTREME, /*printHeader=*/false);
272 lvl->Set("A", rcp_dynamic_cast<op_type>(coarseA));
273 }
274 } else {
275 // classical RAP
276 RCP<mat_type> fineAmat = fineLvl->Get<RCP<mat_type> >("A");
277 // auto fineAmat = rcp_dynamic_cast<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(fineAOp, true);
278 Level fineLevel, coarseLevel;
279 fineLevel.SetFactoryManager(Teuchos::null);
280 coarseLevel.SetFactoryManager(Teuchos::null);
281 coarseLevel.SetPreviousLevel(rcpFromRef(fineLevel));
282 fineLevel.SetLevelID(0);
283 coarseLevel.SetLevelID(1);
284 fineLevel.Set("A", fineAmat);
285 coarseLevel.Set("P", P);
286 RCP<RAPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> > rapFact = rcp(new RAPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>());
287 Teuchos::ParameterList rapList = *(rapFact->GetValidParameterList());
288 rapList.set("transpose: use implicit", true);
289 rapFact->SetParameterList(rapList);
290 coarseLevel.Request("A", rapFact.get());
291 RCP<mat_type> matA = coarseLevel.Get<RCP<mat_type> >("A", rapFact.get());
292
293 using std::endl;
294 using std::setw;
295 const size_t numRows = matA->getRowMap()->getGlobalNumElements();
296 const size_t nnz = matA->getGlobalNumEntries();
297 const double nnzPerRow = Teuchos::as<double>(nnz) / numRows;
298 std::ostringstream oss;
299 oss << std::left;
300 oss << setw(9) << "rows" << setw(12) << "nnz" << setw(14) << "nnz/row" << setw(12) << endl;
301 oss << setw(9) << numRows << setw(12) << nnz << setw(14) << nnzPerRow << endl;
302 out << oss.str();
303
304 lvl->Set("A", matA);
305 }
306 }
307
308 RCP<HierarchyManager<Scalar, LocalOrdinal, GlobalOrdinal, Node> > mueLuFactory = rcp(new ParameterListInterpreter<Scalar, LocalOrdinal, GlobalOrdinal, Node>(params, op->getDomainMap()->getComm()));
309 H->setlib(op->getDomainMap()->lib());
310 H->SetProcRankVerbose(op->getDomainMap()->getComm()->getRank());
311 mueLuFactory->SetupHierarchy(*H);
312 H->IsPreconditioner(true);
313
314 return H;
315 }
316
317 } // namespace MueLu
318
319 #endif