Debian Codegraph @

packages/muelu/test/unit_tests_kokkos/NotayAggregationFactory.cpp

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 #include "Teuchos_UnitTestHarness.hpp"
11 #include "MueLu_TestHelpers_kokkos.hpp"
12 #include "MueLu_Version.hpp"
13
14 #include "MueLu_AmalgamationFactory.hpp"
15 #include "MueLu_CoalesceDropFactory.hpp"
16 #include "MueLu_Aggregates.hpp"
17 #include "MueLu_NotayAggregationFactory.hpp"
18 #include "MueLu_Types.hpp"
19
20 namespace MueLuTests {
21
22 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(NotayAggregation, InitialAggregation1D, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
23 #include "MueLu_UseShortNames.hpp"
24 MUELU_TESTING_SET_OSTREAM;
25 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
26
27 // using TST = Teuchos::ScalarTraits<SC>;
28 // using magnitude_type = typename TST::magnitudeType;
29 // using TMT = Teuchos::ScalarTraits<magnitude_type>;
30 // using real_type = typename TST::coordinateType;
31 // using RealValuedMultiVector = Xpetra::MultiVector<real_type,LO,GO,NO>;
32 using test_factory = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>;
33
34 out << "version: " << MueLu::Version() << std::endl;
35 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
36 int rank = comm->getRank();
37 int numproc = comm->getSize();
38 RCP<const Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(16 * comm->getSize());
39 const int numRows = static_cast<int>(A->getLocalNumRows());
40 RCP<Aggregates> aggregates = rcp(new Aggregates(A->getMap()));
41 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
42 std::vector<unsigned> aggStat(numRows, MueLu::READY);
43 LO numUnaggregatedNodes = numRows, numDirichletNodes = 0;
44
45 Array<LO> orderingVector(numRows);
46 for (LO i = 0; i < numRows; i++) {
47 orderingVector[i] = i;
48 }
49
50 Teuchos::ParameterList params;
51 params.set("aggregation: pairwise: tie threshold", 1e-6);
52 NAF->BuildInitialAggregates(params, A, orderingVector(),
53 Teuchos::ScalarTraits<SC>::magnitude(10.0),
54 *aggregates, aggStat, numUnaggregatedNodes, numDirichletNodes);
55
56 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
57 std::cout << "p=" << rank << " | aggregate sizes="
58 << sizes.view(0, sizes.size()) << std::endl;
59
60 #if 0
61 auto v2a = aggregates->GetVertex2AggId()->getData(0);
62 printf("[%d] Aggregates: ",rank);
63 for(int i=0; i<(int)v2a.size(); i++)
64 printf("%d(%d) ",i,v2a[i]);
65 printf("\n");
66 #endif
67
68 // TEST_EQUALITY(numUnaggregatedNodes, 0);
69 if (numproc == 1) {
70 TEST_EQUALITY(aggregates->GetNumAggregates(), 7);
71 } else {
72 TEST_EQUALITY(aggregates->GetNumAggregates(), 8);
73 }
74
75 // On proc 0 gets picked as a Dirichlet (so it does not get aggregated) and the last aggregate is a singleton
76 // All the other ranks wind up with strict pairs
77 int expected;
78 for (int i = 0; i < (int)sizes.size(); i++) {
79 if (numproc == 1)
80 expected = 2;
81 else
82 expected = ((rank == 0 || rank == 3) && i == (int)sizes.size() - 1) ? 1 : 2;
83 TEST_EQUALITY(sizes[i], expected);
84 }
85
86 } // InitialAggregation1D
87
88 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(NotayAggregation, InitialAggregation2D, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
89 #include "MueLu_UseShortNames.hpp"
90 MUELU_TESTING_SET_OSTREAM;
91 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
92
93 // using TST = Teuchos::ScalarTraits<SC>;
94 // using magnitude_type = typename TST::magnitudeType;
95 // using TMT = Teuchos::ScalarTraits<magnitude_type>;
96 // using real_type = typename TST::coordinateType;
97 // using RealValuedMultiVector = Xpetra::MultiVector<real_type,LO,GO,NO>;
98 using test_factory = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>;
99
100 out << "version: " << MueLu::Version() << std::endl;
101 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
102 int rank = comm->getRank();
103 int numproc = comm->getSize();
104 Xpetra::UnderlyingLib lib = TestHelpers_kokkos::Parameters::getLib();
105
106 // Do a 2D Star2D Matrix with up/down as a "weak connection"
107 Teuchos::ParameterList mp;
108 mp.set("matrixType", "Star2D");
109 const int nx = 3;
110 mp.set("nx", (GO)nx);
111 mp.set("ny", (GO)nx * comm->getSize());
112 mp.set("a", 2.2);
113 mp.set("b", -0.1);
114 mp.set("c", -0.1);
115 mp.set("d", -1.0);
116 mp.set("e", -1.0);
117 mp.set("z1", 0.0);
118 mp.set("z2", 0.0);
119 mp.set("z3", 0.0);
120 mp.set("z4", 0.0);
121 RCP<const Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::BuildMatrix(mp, lib);
122 const int numRows = static_cast<int>(A->getLocalNumRows());
123
124 RCP<Aggregates> aggregates = rcp(new Aggregates(A->getMap()));
125 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
126 std::vector<unsigned> aggStat(numRows, MueLu::READY);
127 LO numUnaggregatedNodes = numRows, numDirichletNodes = 0;
128
129 Array<LO> orderingVector(numRows);
130 for (LO i = 0; i < numRows; i++) {
131 orderingVector[i] = i;
132 }
133
134 Teuchos::ParameterList params;
135 params.set("aggregation: pairwise: tie threshold", 1e-6);
136 NAF->BuildInitialAggregates(params, A, orderingVector(),
137 Teuchos::ScalarTraits<SC>::magnitude(4.1),
138 *aggregates, aggStat, numUnaggregatedNodes, numDirichletNodes);
139
140 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
141
142 TEST_EQUALITY(numUnaggregatedNodes, 0);
143
144 // For this problem, the four corners will be detected as "Dirichlet" and ignored, this will
145 // generate some number of singletons
146 Teuchos::Array<int> expected;
147 if (numproc == 1) {
148 expected = Teuchos::Array<int>({2, 1, 1, 1});
149 } else if (rank == 0 || rank == numproc - 1) {
150 expected = Teuchos::Array<int>({2, 2, 2, 1});
151 } else {
152 expected = Teuchos::Array<int>({2, 2, 2, 1, 1, 1});
153 }
154
155 TEST_EQUALITY(sizes.size(), expected.size());
156
157 for (int i = 0; i < static_cast<int>(sizes.size()); i++) {
158 TEST_EQUALITY(sizes[i], expected[i]);
159 }
160
161 #if 0
162 auto v2a = aggregates->GetVertex2AggId()->getData(0);
163 printf("[%d] Aggregates: ",rank);
164 for(int i=0; i<(int)v2a.size(); i++)
165 printf("%d(%d) ",i,v2a[i]);
166 printf("\n");
167 #endif
168
169 } // InitialAggregation2D
170
171 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(NotayAggregation, IntermediateProlongator2D, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
172 #include "MueLu_UseShortNames.hpp"
173 MUELU_TESTING_SET_OSTREAM;
174 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
175
176 // using TST = Teuchos::ScalarTraits<SC>;
177 // using magnitude_type = typename TST::magnitudeType;
178 // using TMT = Teuchos::ScalarTraits<magnitude_type>;
179 // using real_type = typename TST::coordinateType;
180 // using RealValuedMultiVector = Xpetra::MultiVector<real_type,LO,GO,NO>;
181 using test_factory = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>;
182
183 out << "version: " << MueLu::Version() << std::endl;
184 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
185 Xpetra::UnderlyingLib lib = TestHelpers_kokkos::Parameters::getLib();
186
187 // Do a 2D Star2D Matrix with up/down as a "weak connection"
188 Teuchos::ParameterList mp;
189 mp.set("matrixType", "Star2D");
190 const int nx = 3;
191 mp.set("nx", (GO)nx);
192 mp.set("ny", (GO)nx * comm->getSize());
193 mp.set("a", 2.2);
194 mp.set("b", -0.1);
195 mp.set("c", -0.1);
196 mp.set("d", -1.0);
197 mp.set("e", -1.0);
198 mp.set("z1", 0.0);
199 mp.set("z2", 0.0);
200 mp.set("z3", 0.0);
201 mp.set("z4", 0.0);
202 RCP<const Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::BuildMatrix(mp, lib);
203 const int numRows = static_cast<int>(A->getLocalNumRows());
204
205 RCP<Aggregates> aggregates = rcp(new Aggregates(A->getMap()));
206 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
207 std::vector<unsigned> aggStat(numRows, MueLu::READY);
208 LO numUnaggregatedNodes = numRows, numDirichletNodes = 0;
209 typename Matrix::local_matrix_type intermediateP;
210
211 Array<LO> orderingVector(numRows);
212 for (LO i = 0; i < numRows; i++) {
213 orderingVector[i] = i;
214 }
215
216 Teuchos::ParameterList params;
217 params.set("aggregation: pairwise: tie threshold", 1e-6);
218 NAF->BuildInitialAggregates(params, A, orderingVector(),
219 Teuchos::ScalarTraits<SC>::magnitude(4.1),
220 *aggregates, aggStat, numUnaggregatedNodes, numDirichletNodes);
221
222 out << "numDirichletNodes=" << numDirichletNodes << std::endl;
223
224 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
225 auto v2a = aggregates->GetVertex2AggId()->getData(0);
226
227 TEST_EQUALITY(numUnaggregatedNodes, 0);
228
229 NAF->BuildIntermediateProlongator(A->getLocalNumRows(), numDirichletNodes,
230 aggregates->GetNumAggregates(),
231 v2a.view(0, A->getLocalNumRows()),
232 intermediateP);
233
234 } // IntermediateProlongator2D
235
236 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(NotayAggregation, CoarseLocalMatrix2D, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
237 #include "MueLu_UseShortNames.hpp"
238 MUELU_TESTING_SET_OSTREAM;
239 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
240
241 using test_factory = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>;
242
243 out << "version: " << MueLu::Version() << std::endl;
244 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
245 Xpetra::UnderlyingLib lib = TestHelpers_kokkos::Parameters::getLib();
246
247 // Do a 2D Star2D Matrix with up/down as a "weak connection"
248 Teuchos::ParameterList mp;
249 mp.set("matrixType", "Star2D");
250 const GO nx = 3;
251 mp.set("nx", nx);
252 mp.set("ny", nx * comm->getSize());
253 mp.set("a", 2.2);
254 mp.set("b", -0.1);
255 mp.set("c", -0.1);
256 mp.set("d", -1.0);
257 mp.set("e", -1.0);
258 mp.set("z1", 0.0);
259 mp.set("z2", 0.0);
260 mp.set("z3", 0.0);
261 mp.set("z4", 0.0);
262 RCP<const Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::BuildMatrix(mp, lib);
263 const int numRows = static_cast<int>(A->getLocalNumRows());
264
265 RCP<Aggregates> aggregates = rcp(new Aggregates(A->getMap()));
266 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
267 std::vector<unsigned> aggStat(numRows, MueLu::READY);
268 LO numUnaggregatedNodes = numRows, numDirichletNodes = 0;
269 typename Matrix::local_matrix_type intermediateP;
270
271 Array<LO> orderingVector(numRows);
272 for (LO i = 0; i < numRows; i++) {
273 orderingVector[i] = i;
274 }
275
276 Teuchos::ParameterList params;
277 params.set("aggregation: pairwise: tie threshold", 1e-6);
278 NAF->BuildInitialAggregates(params, A, orderingVector(),
279 Teuchos::ScalarTraits<SC>::magnitude(4.1),
280 *aggregates, aggStat, numUnaggregatedNodes, numDirichletNodes);
281
282 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
283 auto v2a = aggregates->GetVertex2AggId()->getData(0);
284
285 TEST_EQUALITY(numUnaggregatedNodes, 0);
286
287 typename Matrix::local_matrix_type coarseA = A->getLocalMatrixDevice();
288 NAF->BuildOnRankLocalMatrix(A->getLocalMatrixDevice(), coarseA);
289 NAF->BuildIntermediateProlongator(A->getLocalNumRows(), numDirichletNodes,
290 aggregates->GetNumAggregates(),
291 v2a.view(0, A->getLocalNumRows()),
292 intermediateP);
293 NAF->BuildCoarseLocalMatrix(intermediateP, coarseA);
294
295 } // CoarseLocalMatrix2D
296
297 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(NotayAggregation, BuildNotayAggregates2D, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
298 #include "MueLu_UseShortNames.hpp"
299 MUELU_TESTING_SET_OSTREAM;
300 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
301
302 using test_factory = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>;
303
304 out << "version: " << MueLu::Version() << std::endl;
305 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
306 Xpetra::UnderlyingLib lib = TestHelpers_kokkos::Parameters::getLib();
307
308 // Do a 2D Star2D Matrix with up/down as a "weak connection"
309 Teuchos::ParameterList mp;
310 mp.set("matrixType", "Star2D");
311 const int nx = 9;
312 mp.set("nx", (GO)nx);
313 mp.set("ny", (GO)nx * comm->getSize());
314 mp.set("a", 2.2);
315 mp.set("b", -0.1);
316 mp.set("c", -0.1);
317 mp.set("d", -1.0);
318 mp.set("e", -1.0);
319 mp.set("z1", 0.0);
320 mp.set("z2", 0.0);
321 mp.set("z3", 0.0);
322 mp.set("z4", 0.0);
323 RCP<Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::BuildMatrix(mp, lib);
324
325 // Use default ordering
326 {
327 MueLu::Level currentLevel;
328 test_factory::createSingleLevelHierarchy(currentLevel);
329 currentLevel.SetFactoryManager(Teuchos::null);
330 currentLevel.Request("A");
331 currentLevel.Set("A", A);
332
333 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
334 RCP<CoalesceDropFactory> dropFact = rcp(new CoalesceDropFactory());
335 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
336
337 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
338 NAF->SetFactory("Graph", dropFact);
339 NAF->SetFactory("DofsPerNode", dropFact);
340 NAF->SetParameter("aggregation: pairwise: size", Teuchos::ParameterEntry(4));
341 NAF->SetParameter("aggregation: Dirichlet threshold", Teuchos::ParameterEntry(4.1));
342
343 currentLevel.Request("Aggregates", NAF.get());
344 currentLevel.Request(*NAF);
345 NAF->Build(currentLevel);
346 out << "Pairwise aggregation factory is done" << std::endl;
347 RCP<Aggregates> aggregates = currentLevel.Get<RCP<Aggregates> >("Aggregates", NAF.get());
348 out << "Recovered aggregates from muelu level" << std::endl;
349 currentLevel.Release("Aggregates", NAF.get());
350 std::cout << "Testing pairwise aggregates" << std::endl;
351
352 auto v2a = aggregates->GetVertex2AggId()->getData(0);
353 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
354
355 TEST_EQUALITY(aggregates->AggregatesCrossProcessors(), false);
356 }
357
358 // Use random ordering
359 {
360 MueLu::Level currentLevel;
361 test_factory::createSingleLevelHierarchy(currentLevel);
362 currentLevel.SetFactoryManager(Teuchos::null);
363 currentLevel.Request("A");
364 currentLevel.Set("A", A);
365
366 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
367 RCP<CoalesceDropFactory> dropFact = rcp(new CoalesceDropFactory());
368 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
369
370 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
371 NAF->SetFactory("Graph", dropFact);
372 NAF->SetFactory("DofsPerNode", dropFact);
373 NAF->SetParameter("aggregation: pairwise: size", Teuchos::ParameterEntry(4));
374 NAF->SetParameter("aggregation: Dirichlet threshold", Teuchos::ParameterEntry(4.1));
375 NAF->SetParameter("aggregation: ordering", Teuchos::ParameterEntry(std::string("random")));
376
377 currentLevel.Request("Aggregates", NAF.get());
378 currentLevel.Request(*NAF);
379 NAF->Build(currentLevel);
380 out << "Pairwise aggregation factory is done" << std::endl;
381 RCP<Aggregates> aggregates = currentLevel.Get<RCP<Aggregates> >("Aggregates", NAF.get());
382 out << "Recovered aggregates from muelu level" << std::endl;
383 currentLevel.Release("Aggregates", NAF.get());
384 std::cout << "Testing pairwise aggregates" << std::endl;
385
386 auto v2a = aggregates->GetVertex2AggId()->getData(0);
387 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
388
389 TEST_EQUALITY(aggregates->AggregatesCrossProcessors(), false);
390 }
391
392 // Use Cuthill-McKee if we have it (requires Kokkos refactor)
393 {
394 MueLu::Level currentLevel;
395 test_factory::createSingleLevelHierarchy(currentLevel);
396 currentLevel.SetFactoryManager(Teuchos::null);
397 currentLevel.Request("A");
398 currentLevel.Set("A", A);
399
400 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
401 RCP<CoalesceDropFactory> dropFact = rcp(new CoalesceDropFactory());
402 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
403
404 RCP<NotayAggregationFactory> NAF = rcp(new NotayAggregationFactory());
405 NAF->SetFactory("Graph", dropFact);
406 NAF->SetFactory("DofsPerNode", dropFact);
407 NAF->SetParameter("aggregation: pairwise: size", Teuchos::ParameterEntry(4));
408 NAF->SetParameter("aggregation: Dirichlet threshold", Teuchos::ParameterEntry(4.1));
409 NAF->SetParameter("aggregation: ordering", Teuchos::ParameterEntry(std::string("cuthill-mckee")));
410
411 currentLevel.Request("Aggregates", NAF.get());
412 currentLevel.Request(*NAF);
413 NAF->Build(currentLevel);
414 out << "Pairwise aggregation factory is done" << std::endl;
415 RCP<Aggregates> aggregates = currentLevel.Get<RCP<Aggregates> >("Aggregates", NAF.get());
416 out << "Recovered aggregates from muelu level" << std::endl;
417 currentLevel.Release("Aggregates", NAF.get());
418 std::cout << "Testing pairwise aggregates" << std::endl;
419
420 auto v2a = aggregates->GetVertex2AggId()->getData(0);
421 Teuchos::ArrayRCP<LO> sizes = aggregates->ComputeAggregateSizesArrayRCP();
422
423 TEST_EQUALITY(aggregates->AggregatesCrossProcessors(), false);
424 }
425
426 } // BuildNotayAggregates2D
427
428 #define MUELU_ETI_GROUP(Scalar, LO, GO, Node) \
429 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(NotayAggregation, InitialAggregation1D, Scalar, LO, GO, Node) \
430 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(NotayAggregation, InitialAggregation2D, Scalar, LO, GO, Node) \
431 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(NotayAggregation, IntermediateProlongator2D, Scalar, LO, GO, Node) \
432 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(NotayAggregation, CoarseLocalMatrix2D, Scalar, LO, GO, Node) \
433 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(NotayAggregation, BuildNotayAggregates2D, Scalar, LO, GO, Node)
434
435 #include <MueLu_ETI_4arg.hpp>
436
437 } // namespace MueLuTests