Debian Codegraph @

packages/muelu/test/unit_tests_kokkos/LWGraph_kokkos.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 <Teuchos_DefaultComm.hpp>
12
13 #include "MueLu_TestHelpers_kokkos.hpp"
14 #include "MueLu_Version.hpp"
15
16 #include "MueLu_LWGraph_kokkos.hpp"
17 #include "MueLu_Level.hpp"
18 #include "MueLu_AmalgamationInfo.hpp"
19 #include "MueLu_AmalgamationFactory.hpp"
20 #include "MueLu_CoalesceDropFactory_kokkos.hpp"
21
22 namespace MueLuTests {
23
24 // Little utility to generate a LWGraph_kokkos.
25 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
26 Teuchos::RCP<MueLu::LWGraph_kokkos<LocalOrdinal, GlobalOrdinal, Node> >
27 gimmeLWGraph(const Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& A,
28 Teuchos::RCP<MueLu::AmalgamationInfo<LocalOrdinal, GlobalOrdinal, Node> >& amalgInfo) {
29 #include "MueLu_UseShortNames.hpp"
30
31 Level level;
32
33 TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level);
34 level.Set("A", A);
35
36 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
37 RCP<CoalesceDropFactory_kokkos> dropFact = rcp(new CoalesceDropFactory_kokkos());
38 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
39
40 level.Request("Graph", dropFact.get());
41 level.Request("UnAmalgamationInfo", amalgFact.get());
42
43 level.Request(*dropFact);
44 dropFact->Build(level);
45
46 auto graph = level.Get<RCP<LWGraph_kokkos> >("Graph", dropFact.get());
47 amalgInfo = level.Get<RCP<AmalgamationInfo> >("UnAmalgamationInfo", amalgFact.get());
48 level.Release("UnAmalgamationInfo", amalgFact.get());
49 level.Release("Graph", dropFact.get());
50 return graph;
51 }
52
53 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(LWGraph_kokkos, CreateLWGraph, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
54 #include "MueLu_UseShortNames.hpp"
55 MUELU_TESTING_SET_OSTREAM;
56 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
57 out << "version: " << MueLu::Version() << std::endl;
58
59 if (TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) {
60 out << "skipping test for linAlgebra==UseEpetra" << std::endl;
61 return;
62 }
63
64 RCP<Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(16);
65
66 RCP<AmalgamationInfo> amalgInfo;
67 RCP<LWGraph_kokkos> graph = gimmeLWGraph(A, amalgInfo);
68
69 const int numRanks = graph->GetComm()->getSize();
70
71 TEST_EQUALITY(graph != Teuchos::null, true);
72 if (numRanks == 1) {
73 TEST_EQUALITY(graph->GetNodeNumVertices() == 16, true);
74 } else if (numRanks == 4) {
75 TEST_EQUALITY(graph->GetNodeNumVertices() == 4, true);
76 }
77 } // CreateLWGraph
78
79 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(LWGraph_kokkos, LocalGraphData, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
80 #include "MueLu_UseShortNames.hpp"
81 MUELU_TESTING_SET_OSTREAM;
82 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
83 out << "version: " << MueLu::Version() << std::endl;
84
85 if (TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) {
86 out << "skipping test for linAlgebra==UseEpetra" << std::endl;
87 return;
88 }
89
90 RCP<Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(16);
91
92 RCP<AmalgamationInfo> amalgInfo;
93 RCP<LWGraph_kokkos> graph = gimmeLWGraph(A, amalgInfo);
94
95 // const int numRanks = graph->GetComm()->getSize();
96 // const int myRank = graph->GetComm()->getRank();
97
98 using row_map_type = typename LWGraph_kokkos::local_graph_type::row_map_type;
99 using entries_type = typename LWGraph_kokkos::local_graph_type::entries_type;
100
101 row_map_type rowPtrs = graph->getRowPtrs();
102 entries_type entries = graph->getEntries();
103 typename row_map_type::HostMirror rowPtrsHost = Kokkos::create_mirror_view(rowPtrs);
104 typename entries_type::HostMirror entriesHost = Kokkos::create_mirror_view(entries);
105 Kokkos::deep_copy(rowPtrsHost, rowPtrs);
106 Kokkos::deep_copy(entriesHost, entries);
107 rowPtrs = row_map_type();
108 entries = entries_type();
109
110 // Check that the graph is not null
111 TEST_EQUALITY(graph != Teuchos::null, true);
112
113 {
114 // Since this is a scalar problem and no entries are dropped
115 // we can simply compare the value from LWGraph with those in A
116 auto numrows = A->getLocalNumRows(), nument = A->getLocalNumEntries();
117 int result = 0;
118 auto lclLWGraph = *graph;
119 Kokkos::parallel_reduce(
120 "MueLu:TentativePF:Build:compute_agg_sizes", Kokkos::RangePolicy<typename NO::execution_space, size_t>(0, 1),
121 KOKKOS_LAMBDA(const LO i, int& incorrect) {
122 if (lclLWGraph.GetNodeNumVertices() != numrows)
123 incorrect++;
124 if (lclLWGraph.GetNodeNumEdges() != nument)
125 incorrect++;
126 },
127 result);
128 TEST_EQUALITY(result, 0);
129 }
130
131 graph.reset();
132 Array<LO> indices(3);
133 Array<SC> values(3);
134 size_t numEntries;
135 bool chk_rowPtr = true;
136 bool chk_colInd = true;
137 for (size_t rowIdx = 0; rowIdx < A->getLocalNumRows(); ++rowIdx) {
138 A->getLocalRowCopy(rowIdx, indices(), values(), numEntries);
139 if (rowPtrsHost(rowIdx + 1) - rowPtrsHost(rowIdx) != numEntries) {
140 chk_rowPtr = false;
141 }
142 for (size_t entryIdx = 0; entryIdx < numEntries; ++entryIdx) {
143 if (entriesHost(rowPtrsHost(rowIdx) + entryIdx) != indices[entryIdx]) {
144 chk_colInd = false;
145 }
146 }
147 }
148 TEST_EQUALITY(chk_rowPtr, true);
149 TEST_EQUALITY(chk_colInd, true);
150 } // LocalGraphData
151
152 #define MUELU_ETI_GROUP(SC, LO, GO, NO) \
153 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(LWGraph_kokkos, CreateLWGraph, SC, LO, GO, NO) \
154 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(LWGraph_kokkos, LocalGraphData, SC, LO, GO, NO)
155
156 #include <MueLu_ETI_4arg.hpp>
157
158 } // namespace MueLuTests