Debian Codegraph @

packages/muelu/test/unit_tests_kokkos/Regression.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
12 #include <Xpetra_Map.hpp>
13 #include <Xpetra_Matrix.hpp>
14 // Galeri
15 #include <Galeri_XpetraParameters.hpp>
16 #include <Galeri_XpetraProblemFactory.hpp>
17 #include <Galeri_XpetraUtils.hpp>
18
19 #include <MueLu_config.hpp>
20 #include <MueLu_TestHelpers_kokkos.hpp>
21 #include <MueLu_Version.hpp>
22 #include <MueLu_Hierarchy.hpp>
23 #include <MueLu_CreateXpetraPreconditioner.hpp>
24 #include <MueLu_CoalesceDropFactory_kokkos.hpp>
25 #include <MueLu_UncoupledAggregationFactory.hpp>
26
27 #include <Tpetra_Details_KokkosCounter.hpp>
28
29 namespace MueLuTests {
30
31 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Regression, H2D, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
32 #include <MueLu_UseShortNames.hpp>
33 MUELU_TESTING_SET_OSTREAM;
34 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
35 out << "version: " << MueLu::Version() << std::endl;
36
37 typedef typename Teuchos::ScalarTraits<SC>::magnitudeType real_type;
38 typedef typename Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector;
39
40 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
41 if (comm->getSize() != 1) {
42 out << "Skipping test for more than 1 proc" << std::endl;
43 return;
44 }
45 Teuchos::RCP<Teuchos::StackedTimer> stacked_timer = rcp(new Teuchos::StackedTimer("H2D"));
46 Teuchos::TimeMonitor::setStackedTimer(stacked_timer);
47 int numRows = 399;
48 RCP<Matrix> A = MueLuTests::TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(numRows);
49 RCP<const Map> map = A->getMap();
50 RCP<RealValuedMultiVector> coordinates = Xpetra::MultiVectorFactory<real_type, LO, GO, NO>::Build(map, 1);
51 RCP<MultiVector> nullspace = MultiVectorFactory::Build(map, 1);
52 nullspace->putScalar(Teuchos::ScalarTraits<SC>::one());
53
54 Teuchos::ParameterList MueLuList;
55 MueLuList.set("verbosity", "high");
56 MueLuList.set("coarse: max size", numRows - 1); // make it so we want two levels
57 MueLuList.set("smoother: type", "RELAXATION");
58 MueLuList.sublist("smoother: params").set("relaxation: type", "Jacobi");
59 MueLuList.set("max levels", 2);
60 MueLuList.set("use kokkos refactor", true);
61
62 Teuchos::ParameterList userParamList = MueLuList.sublist("user data");
63 userParamList.set<RCP<RealValuedMultiVector> >("Coordinates", coordinates);
64 userParamList.set<RCP<MultiVector> >("Nullspace", nullspace);
65
66 Tpetra::Details::DeepCopyCounter::reset();
67 Tpetra::Details::DeepCopyCounter::start();
68
69 Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> > H =
70 MueLu::CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(A, MueLuList);
71
72 Tpetra::Details::DeepCopyCounter::stop();
73
74 // confirm that we did get a hierarchy with two levels -- a sanity check for this test
75 TEST_EQUALITY(2, H->GetGlobalNumLevels());
76
77 // When Kokkos Kernels uses TPLs, some Kokkos::deep_copy in the Kokkos Kernels native implementations are not called.
78 int kkNativeDeepCopies = 8;
79 #if defined(KOKKOSKERNELS_ENABLE_TPL_MKL)
80 if constexpr (Node::is_cpu)
81 kkNativeDeepCopies = 0;
82 #endif
83 #if defined(KOKKOSKERNELS_ENABLE_TPL_CUSPARSE)
84 if constexpr (std::is_same_v<typename Node::execution_space, Kokkos::Cuda>) {
85 // kokkos kernels only uses cuSparse for these versions
86 #if (CUDA_VERSION < 11000) || (CUDA_VERSION >= 11040)
87 kkNativeDeepCopies = 0;
88 #endif
89 }
90 #endif
91 #if defined(KOKKOSKERNELS_ENABLE_TPL_ROCSPARSE)
92 if constexpr (std::is_same_v<typename Node::execution_space, Kokkos::HIP>)
93 kkNativeDeepCopies = 0;
94 #endif
95
96 if (Node::is_cpu) {
97 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), 0);
98 }
99 #ifdef KOKKOS_HAS_SHARED_SPACE
100 else {
101 size_t targetNumDeepCopies = kkNativeDeepCopies + (std::is_same_v<typename Node::memory_space, Kokkos::SharedSpace> ? 17 : 32);
102 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), targetNumDeepCopies);
103 }
104 #else
105 else {
106 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), kkNativeDeepCopies + 32);
107 }
108 #endif // KOKKOS_HAS_SHARED_SPACE
109
110 auto X = Xpetra::MultiVectorFactory<SC, LO, GO, NO>::Build(map, 1);
111 auto B = Xpetra::MultiVectorFactory<SC, LO, GO, NO>::Build(map, 1);
112
113 Tpetra::Details::DeepCopyCounter::reset();
114 Tpetra::Details::DeepCopyCounter::start();
115
116 H->Iterate(*B, *X, 1, false);
117
118 Tpetra::Details::DeepCopyCounter::stop();
119
120 if (Node::is_cpu) {
121 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), 0);
122 } else {
123 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), 2);
124 }
125
126 stacked_timer->stopBaseTimer();
127 Teuchos::StackedTimer::OutputOptions options;
128 options.output_fraction = options.output_histogram = options.output_minmax = true;
129 stacked_timer->report(out, comm, options);
130
131 } // H2D
132
133 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Regression, Aggregation, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
134 #include <MueLu_UseShortNames.hpp>
135 MUELU_TESTING_SET_OSTREAM;
136 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
137 out << "version: " << MueLu::Version() << std::endl;
138 RCP<const Teuchos::Comm<int> > comm = TestHelpers_kokkos::Parameters::getDefaultComm();
139 Teuchos::RCP<Teuchos::StackedTimer> stacked_timer = rcp(new Teuchos::StackedTimer("H2D"));
140 Teuchos::TimeMonitor::setStackedTimer(stacked_timer);
141
142 // Make a Matrix with multiple degrees of freedom per node
143 GlobalOrdinal nx = 20, ny = 20;
144
145 // Describes the initial layout of matrix rows across processors.
146 Teuchos::ParameterList galeriList;
147 galeriList.set("nx", nx);
148 galeriList.set("ny", ny);
149
150 using test_factory = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>;
151 Teuchos::CommandLineProcessor clp(false);
152 Galeri::Xpetra::Parameters<GO> matrixParameters(clp, 8748); // manage parameters of the test case
153 Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra
154
155 RCP<Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build2DPoisson(nx, ny);
156
157 Tpetra::Details::DeepCopyCounter::reset();
158 Tpetra::Details::DeepCopyCounter::start();
159
160 Level aLevel;
161 TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(aLevel);
162 aLevel.Request("A");
163 aLevel.Set("A", A);
164
165 RCP<CoalesceDropFactory_kokkos> dropFact = rcp(new CoalesceDropFactory_kokkos());
166 RCP<UncoupledAggregationFactory> aggFact = rcp(new UncoupledAggregationFactory());
167 aggFact->SetFactory("Graph", dropFact);
168 aLevel.Request(*aggFact);
169 aggFact->Build(aLevel);
170
171 Tpetra::Details::DeepCopyCounter::stop();
172
173 if (Node::is_cpu) {
174 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), 0);
175 }
176 #ifdef KOKKOS_HAS_SHARED_SPACE
177 else {
178 size_t targetNumDeepCopies = std::is_same_v<typename Node::memory_space, Kokkos::SharedSpace> ? 11 : 17;
179 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), targetNumDeepCopies);
180 }
181 #else
182 else {
183 TEST_EQUALITY(Tpetra::Details::DeepCopyCounter::get_count_different_space(), 17);
184 }
185 #endif
186
187 stacked_timer->stopBaseTimer();
188 Teuchos::StackedTimer::OutputOptions options;
189 options.output_fraction = options.output_histogram = options.output_minmax = true;
190 stacked_timer->report(out, comm, options);
191
192 } // Aggregration
193
194 #define MUELU_ETI_GROUP(Scalar, LO, GO, Node) \
195 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Regression, H2D, Scalar, LO, GO, Node) \
196 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Regression, Aggregation, Scalar, LO, GO, Node)
197
198 #include <MueLu_ETI_4arg.hpp>
199
200 } // namespace MueLuTests