Debian Codegraph @

packages/muelu/test/perf_tests_kokkos/Redirection.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 <cstdio>
11 #include <iomanip>
12 #include <iostream>
13 #include <unistd.h>
14
15 #include <Teuchos_XMLParameterListHelpers.hpp>
16 #include <Teuchos_StandardCatchMacros.hpp>
17
18 // Xpetra
19 #include <Xpetra_Matrix.hpp>
20
21 // Galeri
22 #include <Galeri_XpetraParameters.hpp>
23 #include <Galeri_XpetraProblemFactory.hpp>
24 #include <Galeri_XpetraUtils.hpp>
25 #include <Galeri_XpetraMaps.hpp>
26
27 #include <MueLu.hpp>
28
29 #include <MueLu_BaseClass.hpp>
30 #ifdef HAVE_MUELU_EXPLICIT_INSTANTIATION
31 #include <MueLu_ExplicitInstantiation.hpp>
32 #endif
33
34 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
35 int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int argc, char *argv[]) {
36 #include <MueLu_UseShortNames.hpp>
37 using Teuchos::ArrayView;
38 using Teuchos::ParameterList;
39 using Teuchos::RCP;
40 using Teuchos::rcp;
41 using Teuchos::TimeMonitor;
42
43 // =========================================================================
44 // MPI initialization using Teuchos
45 // =========================================================================
46 auto comm = Teuchos::DefaultComm<int>::getComm();
47
48 // =========================================================================
49 // Convenient definitions
50 // =========================================================================
51
52 // =========================================================================
53 // Parameters initialization
54 // =========================================================================
55 GO nx = 100, ny = 100, nz = 100;
56 Galeri::Xpetra::Parameters<GO> galeriParameters(clp, nx, ny, nz, "Laplace3D"); // manage parameters of the test case
57 Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra
58
59 int loops = 10;
60 clp.setOption("l", &loops, "Number of loops");
61
62 clp.recogniseAllOptions(true);
63 switch (clp.parse(argc, argv)) {
64 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
65 case Teuchos::CommandLineProcessor::PARSE_ERROR:
66 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
67 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
68 }
69
70 // Retrieve matrix parameters (they may have been changed on the command line)
71 // [for instance, if we changed matrix type from 2D to 3D we need to update nz]
72 ParameterList galeriList = galeriParameters.GetParameterList();
73
74 // =========================================================================
75 // Problem construction
76 // =========================================================================
77 std::string matrixType = galeriParameters.GetMatrixType();
78
79 RCP<const Map> map;
80 if (matrixType == "Laplace1D")
81 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian1D", comm, galeriList);
82 else if (matrixType == "Laplace2D" || matrixType == "Star2D" ||
83 matrixType == "BigStar2D" || matrixType == "Elasticity2D")
84 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian2D", comm, galeriList);
85 else if (matrixType == "Laplace3D" || matrixType == "Brick3D" || matrixType == "Elasticity3D")
86 map = Galeri::Xpetra::CreateMap<LO, GO, Node>(xpetraParameters.GetLib(), "Cartesian3D", comm, galeriList);
87
88 RCP<Galeri::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> > Pr =
89 Galeri::Xpetra::BuildProblem<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixType, map, galeriList);
90 RCP<Matrix> A = Pr->BuildMatrix();
91
92 LO numRows = A->getLocalNumRows();
93
94 RCP<TimeMonitor> tm;
95
96 // Loop 1
97 {
98 GO validation = 0;
99
100 ArrayView<const LocalOrdinal> indices;
101 ArrayView<const Scalar> vals;
102
103 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Loop #1: Xpetra")));
104 for (int i = 0; i < loops; i++) {
105 for (LocalOrdinal row = 0; row < numRows; row++) {
106 A->getLocalRowView(row, indices, vals);
107
108 validation += indices.size();
109 }
110 }
111 tm = Teuchos::null;
112
113 std::cout << "validation = " << validation << std::endl;
114 }
115
116 // Loop 2
117 {
118 GO validation = 0;
119
120 if (lib == Xpetra::UseTpetra) {
121 typedef Tpetra::CrsMatrix<SC, LO, GO, NO> tCrsMatrix;
122 RCP<const tCrsMatrix> tA = toTpetra(A);
123 TEUCHOS_TEST_FOR_EXCEPTION(tA.is_null(), MueLu::Exceptions::RuntimeError,
124 "A is not a Tpetra CrsMatrix");
125
126 typename Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_inds_host_view_type indices;
127 typename Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::values_host_view_type vals;
128
129 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Loop #2: Tpetra/Epetra")));
130 for (int i = 0; i < loops; i++) {
131 for (LocalOrdinal row = 0; row < numRows; row++) {
132 tA->getLocalRowView(row, indices, vals);
133
134 validation += indices.extent(0);
135 }
136 }
137 tm = Teuchos::null;
138 }
139 if (lib == Xpetra::UseEpetra) {
140 #ifdef HAVE_MUELU_EPETRA
141 typedef Epetra_CrsMatrix eCrsMatrix;
142 RCP<const eCrsMatrix> eA = Utilities::Op2EpetraCrs(A);
143 TEUCHOS_TEST_FOR_EXCEPTION(eA.is_null(), MueLu::Exceptions::RuntimeError,
144 "A is not a Epetra CrsMatrix");
145
146 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Loop #2: Tpetra/Epetra")));
147
148 for (int i = 0; i < loops; i++) {
149 for (LocalOrdinal row = 0; row < numRows; row++) {
150 int numEntries;
151 double *eValues;
152 int *eIndices;
153
154 eA->ExtractMyRowView(row, numEntries, eValues, eIndices);
155
156 validation += numEntries;
157 }
158 }
159 tm = Teuchos::null;
160 #else
161 TEUCHOS_TEST_FOR_EXCEPTION(true, MueLu::Exceptions::RuntimeError,
162 "Epetra is not available");
163 #endif
164 }
165 std::cout << "validation = " << validation << std::endl;
166 }
167
168 // Loop 3
169 {
170 auto localMatrix = A->getLocalMatrixHost();
171
172 GO validation = 0;
173
174 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Loop #3: Kokkos-serial")));
175 for (int i = 0; i < loops; i++) {
176 for (LocalOrdinal row = 0; row < numRows; row++) {
177 auto rowView = localMatrix.row(row);
178 auto length = rowView.length;
179
180 validation += length;
181 }
182 }
183 tm = Teuchos::null;
184
185 std::cout << "validation = " << validation << std::endl;
186 }
187
188 // Loop 4
189 {
190 auto localMatrix = A->getLocalMatrixDevice();
191
192 GO validation = 0;
193
194 typedef Kokkos::RangePolicy<typename NO::execution_space> RangePolicy;
195
196 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("Loop #4: Kokkos-parallel (node)")));
197 for (int i = 0; i < loops; i++)
198 Kokkos::parallel_reduce(
199 "Utils::DetectDirichletRows", RangePolicy(0, numRows),
200 KOKKOS_LAMBDA(const LO row, GO &r) {
201 auto rowView = localMatrix.row(row);
202 auto length = rowView.length;
203
204 r += length;
205 },
206 validation);
207 tm = Teuchos::null;
208
209 std::cout << "validation = " << validation << std::endl;
210 }
211
212 {
213 const bool alwaysWriteLocal = false;
214 const bool writeGlobalStats = true;
215 const bool writeZeroTimers = false;
216 const bool ignoreZeroTimers = true;
217 const std::string filter = "";
218 TimeMonitor::summarize(comm.ptr(), std::cout, alwaysWriteLocal, writeGlobalStats,
219 writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers);
220 }
221
222 return EXIT_SUCCESS;
223 }
224
225 //- -- --------------------------------------------------------
226 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_
227 #include "MueLu_Test_ETI.hpp"
228
229 int main(int argc, char *argv[]) {
230 return Automatic_Test_ETI(argc, argv);
231 }