Debian Codegraph @

packages/muelu/test/perf_tests_kokkos/KokkosKernels.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 <iostream>
11 #include <Kokkos_Core.hpp>
12 #include <Kokkos_ArithTraits.hpp>
13 #include <KokkosSparse_CrsMatrix.hpp>
14 #include <Kokkos_StaticCrsGraph.hpp>
15 #include <Kokkos_Timer.hpp>
16
17 template <class scalar_type, class local_ordinal_type, class device_type>
18 class LWGraph_kokkos {
19 private:
20 typedef Kokkos::StaticCrsGraph<local_ordinal_type, Kokkos::LayoutLeft, device_type> local_graph_type;
21 typedef Kokkos::View<bool*, device_type> boundary_nodes_type;
22
23 public:
24 LWGraph_kokkos(const local_graph_type& graph)
25 : graph_(graph) {}
26 ~LWGraph_kokkos() {}
27
28 //! Set boolean array indicating which rows correspond to Dirichlet boundaries.
29 KOKKOS_INLINE_FUNCTION
30 void setBoundaryNodeMap(const boundary_nodes_type bndry) {
31 dirichletBoundaries_ = bndry;
32 }
33
34 private:
35 //! Underlying graph (with label)
36 const local_graph_type graph_;
37
38 //! Boolean array marking Dirichlet rows.
39 boundary_nodes_type dirichletBoundaries_;
40 };
41
42 template <class scalar_type, class local_ordinal_type, class device_type>
43 KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type>
44 kernel_construct(local_ordinal_type numRows) {
45 auto numCols = numRows;
46 auto nnz = 10 * numRows;
47
48 auto varianz_nel_row = 0.2 * nnz / numRows;
49 auto width_row = 0.01 * numRows;
50
51 auto elements_per_row = nnz / numRows;
52
53 auto rowPtr = new local_ordinal_type[numRows + 1];
54 rowPtr[0] = 0;
55 for (int row = 0; row < numRows; row++) {
56 int varianz = (1.0 * rand() / INT_MAX - 0.5) * varianz_nel_row;
57
58 rowPtr[row + 1] = rowPtr[row] + elements_per_row + varianz;
59 }
60 nnz = rowPtr[numRows];
61
62 auto colInd = new local_ordinal_type[nnz];
63 auto values = new scalar_type[nnz];
64 for (int row = 0; row < numRows; row++) {
65 for (int k = rowPtr[row]; k < rowPtr[row + 1]; k++) {
66 int pos = row + (1.0 * rand() / INT_MAX - 0.5) * width_row;
67
68 if (pos < 0) pos += numCols;
69 if (pos >= numCols) pos -= numCols;
70 colInd[k] = pos;
71 values[k] = 100.0 * rand() / INT_MAX - 50.0;
72 }
73 }
74
75 typedef KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type> local_matrix_type;
76
77 return local_matrix_type("A", numRows, numCols, nnz, values, rowPtr, colInd);
78 }
79
80 template <class scalar_type, class local_ordinal_type, class device_type>
81 void kernel_coalesce_drop_device(KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type> A) {
82 typedef KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type> local_matrix_type;
83 typedef Kokkos::ArithTraits<scalar_type> ATS;
84 typedef typename ATS::mag_type magnitude_type;
85 typedef Kokkos::View<bool*, device_type> boundary_nodes_type;
86
87 auto numRows = A.numRows();
88
89 scalar_type eps = 0.05;
90
91 typedef Kokkos::RangePolicy<typename local_matrix_type::execution_space> RangePolicy;
92
93 // Stage 0: detect Dirichlet rows
94 boundary_nodes_type boundaryNodes("boundaryNodes", numRows);
95
96 double tol = 0.0;
97 Kokkos::parallel_for(
98 "MueLu:Utils::DetectDirichletRows", RangePolicy(0, numRows),
99 KOKKOS_LAMBDA(const local_ordinal_type row) {
100 auto rowView = A.row(row);
101 auto length = rowView.length;
102
103 boundaryNodes(row) = true;
104 for (decltype(length) colID = 0; colID < length; colID++)
105 if ((rowView.colidx(colID) != row) && (ATS::magnitude(rowView.value(colID)) > tol)) {
106 boundaryNodes(row) = false;
107 break;
108 }
109 });
110
111 // Stage 1: calculate the number of remaining entries per row
112 typedef Kokkos::StaticCrsGraph<local_ordinal_type, Kokkos::LayoutLeft, device_type> local_graph_type;
113 typedef typename local_graph_type::row_map_type row_map_type;
114 typedef typename local_graph_type::entries_type entries_type;
115
116 typename entries_type::non_const_type diag("ghosted", numRows);
117 typename row_map_type::non_const_type rows("row_map", numRows + 1); // rows(0) = 0 automatically
118
119 local_ordinal_type realnnz = 0;
120
121 Kokkos::parallel_reduce(
122 "kernel_cd:stage1_reduce", RangePolicy(0, numRows),
123 KOKKOS_LAMBDA(const local_ordinal_type row, local_ordinal_type& nnz) {
124 auto rowView = A.row(row);
125 auto length = rowView.length;
126
127 local_ordinal_type rownnz = 0;
128 for (decltype(length) colID = 0; colID < length; colID++) {
129 auto col = rowView.colidx(colID);
130
131 // Avoid square root by using squared values
132 auto aiiajj = eps * eps * ATS::magnitude(diag(row)) * ATS::magnitude(diag(col)); // eps^2*|a_ii|*|a_jj|
133 auto aij2 = ATS::magnitude(rowView.value(colID)) * ATS::magnitude(rowView.value(colID)); // |a_ij|^2
134
135 if (aij2 > aiiajj || row == col)
136 rownnz++;
137 }
138 rows(row + 1) = rownnz;
139 nnz += rownnz;
140 },
141 realnnz);
142
143 // parallel_scan (exclusive)
144 Kokkos::parallel_scan(
145 "kernel_cd:stage1_scan", RangePolicy(0, numRows + 1),
146 KOKKOS_LAMBDA(const local_ordinal_type i, local_ordinal_type& upd, const bool& final) {
147 upd += rows(i);
148 if (final)
149 rows(i) = upd;
150 });
151
152 // Stage 2: fill in the column indices
153 typename boundary_nodes_type::non_const_type bndNodes("boundaryNodes", numRows);
154 typename entries_type::non_const_type cols("entries", realnnz);
155
156 local_ordinal_type numDropped = 0;
157
158 Kokkos::parallel_reduce(
159 "kernel_cd:stage2_reduce", RangePolicy(0, numRows),
160 KOKKOS_LAMBDA(const local_ordinal_type row, local_ordinal_type& dropped) {
161 auto rowView = A.row(row);
162 auto length = rowView.length;
163
164 local_ordinal_type rownnz = 0;
165 for (decltype(length) colID = 0; colID < length; colID++) {
166 auto col = rowView.colidx(colID);
167
168 // Avoid square root by using squared values
169 auto aiiajj = eps * eps * ATS::magnitude(diag(row)) * ATS::magnitude(diag(col)); // eps^2*|a_ii|*|a_jj|
170 auto aij2 = ATS::magnitude(rowView.value(colID)) * ATS::magnitude(rowView.value(colID)); // |a_ij|^2
171
172 if (aij2 > aiiajj || row == col) {
173 cols(rows(row) + rownnz) = col;
174 rownnz++;
175 } else {
176 dropped++;
177 }
178 if (rownnz == 1) {
179 // If the only element remaining after filtering is diagonal, mark node as boundary
180 // FIXME: this should really be replaced by the following
181 // if (indices.size() == 1 && indices[0] == row)
182 // boundaryNodes[row] = true;
183 // We do not do it this way now because there is no framework for distinguishing isolated
184 // and boundary nodes in the aggregation algorithms
185 bndNodes(row) = true;
186 }
187 }
188 },
189 numDropped);
190
191 boundaryNodes = bndNodes;
192
193 local_graph_type kokkosGraph(cols, rows);
194
195 LWGraph_kokkos<scalar_type, local_ordinal_type, device_type> graph(kokkosGraph);
196
197 graph.setBoundaryNodeMap(boundaryNodes);
198 }
199
200 template <class scalar_type, class local_ordinal_type, class device_type>
201 void kernel_coalesce_drop_serial(KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type> A) {
202 typedef KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type> local_matrix_type;
203 typedef Kokkos::ArithTraits<scalar_type> ATS;
204 typedef typename ATS::mag_type magnitude_type;
205 typedef Kokkos::View<bool*, device_type> boundary_nodes_type;
206
207 auto numRows = A.numRows();
208
209 scalar_type eps = 0.05;
210
211 typedef Kokkos::RangePolicy<typename local_matrix_type::execution_space> RangePolicy;
212
213 // Stage 0: detect Dirichlet rows
214 boundary_nodes_type boundaryNodes("boundaryNodes", numRows);
215 for (int row = 0; row < numRows; row++) {
216 auto rowView = A.row(row);
217 auto length = rowView.length;
218
219 boundaryNodes(row) = true;
220 for (decltype(length) colID = 0; colID < length; colID++)
221 if ((rowView.colidx(colID) != row) && (ATS::magnitude(rowView.value(colID)) > 1e-13)) {
222 boundaryNodes(row) = false;
223 break;
224 }
225 }
226
227 // Stage 1: calculate the number of remaining entries per row
228 typedef Kokkos::StaticCrsGraph<local_ordinal_type, Kokkos::LayoutLeft, device_type> local_graph_type;
229 typedef typename local_graph_type::row_map_type row_map_type;
230 typedef typename local_graph_type::entries_type entries_type;
231
232 typename entries_type::non_const_type diag("ghosted", numRows);
233 typename row_map_type::non_const_type rows("row_map", numRows + 1); // rows(0) = 0 automatically
234 typename entries_type::non_const_type cols("entries", 20 * numRows); // estimate
235
236 typename boundary_nodes_type::non_const_type bndNodes("boundaryNodes", numRows);
237 local_ordinal_type numDropped = 0;
238
239 local_ordinal_type realnnz = 0;
240 for (int row = 0; row < numRows; row++) {
241 auto rowView = A.row(row);
242 auto length = rowView.length;
243
244 local_ordinal_type rownnz = 0;
245 for (decltype(length) colID = 0; colID < length; colID++) {
246 local_ordinal_type col = rowView.colidx(colID);
247
248 // Avoid square root by using squared values
249 magnitude_type aiiajj = eps * eps * ATS::magnitude(diag(row)) * ATS::magnitude(diag(col)); // eps^2*|a_ii|*|a_jj|
250 magnitude_type aij2 = ATS::magnitude(rowView.value(colID)) * ATS::magnitude(rowView.value(colID)); // |a_ij|^2
251
252 if (aij2 > aiiajj || row == col) {
253 cols(rownnz++) = col;
254
255 } else {
256 numDropped++;
257 }
258
259 if (rownnz == 1) {
260 // If the only element remaining after filtering is diagonal, mark node as boundary
261 // FIXME: this should really be replaced by the following
262 // if (indices.size() == 1 && indices[0] == row)
263 // boundaryNodes[row] = true;
264 // We do not do it this way now because there is no framework for distinguishing isolated
265 // and boundary nodes in the aggregation algorithms
266 bndNodes(row) = true;
267 }
268 }
269 rows(row + 1) = rows(row) + rownnz;
270 realnnz += rownnz;
271 }
272 boundaryNodes = bndNodes;
273
274 local_graph_type kokkosGraph(cols, rows);
275
276 LWGraph_kokkos<scalar_type, local_ordinal_type, device_type> graph(kokkosGraph);
277
278 graph.setBoundaryNodeMap(boundaryNodes);
279 }
280
281 template <class scalar_type, class local_ordinal_type, class device_type>
282 int main_(int argc, char** argv) {
283 int n = 100000;
284 int loop = 10;
285
286 for (int i = 1; i < argc; i++) {
287 if (strcmp(argv[i], "-n") == 0) {
288 n = atoi(argv[++i]);
289 continue;
290 } else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--loop") == 0) {
291 loop = atoi(argv[++i]);
292 continue;
293 } else if (strcmp(argv[i], "--node") == 0) {
294 i++;
295 continue;
296 } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
297 std::cout << "./MueLu_KokkosKernels.exe [-n <matrix_size>] [-l <number_of_loops>]" << std::endl;
298 return 0;
299 } else {
300 throw std::runtime_error(std::string("Uknown option: ") + argv[i]);
301 }
302 }
303
304 typedef KokkosSparse::CrsMatrix<scalar_type, local_ordinal_type, device_type> local_matrix_type;
305 typedef typename device_type::execution_space execution_space;
306
307 local_matrix_type A = kernel_construct<scalar_type, local_ordinal_type, device_type>(n);
308
309 execution_space().fence();
310 Kokkos::Timer timer;
311
312 #ifdef KOKKOS_ENABLE_SERIAL
313 if (typeid(device_type) == typeid(Kokkos::Serial)) {
314 for (int i = 0; i < loop; i++)
315 kernel_coalesce_drop_serial(A);
316
317 } else {
318 #else
319 {
320 #endif
321 for (int i = 0; i < loop; i++)
322 kernel_coalesce_drop_device(A);
323 }
324
325 double kernel_time = timer.seconds();
326
327 execution_space().fence();
328
329 printf("kernel_coalesce_drop: %.2e (s)\n", kernel_time / loop);
330
331 return 0;
332 }
333
334 int main(int argc, char* argv[]) {
335 Kokkos::initialize(argc, argv);
336
337 srand(13721);
338
339 std::string node;
340 for (int i = 1; i < argc; i++) {
341 if (strncmp(argv[i], "--node=", 7) == 0) {
342 std::cout << "Use --node <node> instead of --node=<node>" << std::endl;
343 Kokkos::finalize();
344 return 0;
345
346 } else if (strncmp(argv[i], "--node", 6) == 0)
347 node = argv[++i];
348 }
349
350 std::cout << "node = " << (node == "" ? "default" : node) << std::endl;
351
352 if (node == "") {
353 return main_<double, int, Kokkos::DefaultExecutionSpace>(argc, argv);
354
355 } else if (node == "serial") {
356 #ifdef KOKKOS_ENABLE_SERIAL
357 return main_<double, int, Kokkos::Serial>(argc, argv);
358 #else
359 std::cout << "Error: Serial node type is disabled" << std::endl;
360 #endif
361
362 } else if (node == "openmp") {
363 #ifdef KOKKOS_ENABLE_OPENMP
364 return main_<double, int, Kokkos::OpenMP>(argc, argv);
365 #else
366 std::cout << "Error: OpenMP node type is disabled" << std::endl;
367 #endif
368
369 } else if (node == "cuda") {
370 #ifdef KOKKOS_ENABLE_CUDA
371 return main_<double, int, Kokkos::Cuda>(argc, argv);
372 #else
373 std::cout << "Error: CUDA node type is disabled" << std::endl;
374 #endif
375 } else if (node == "hip") {
376 #ifdef KOKKOS_ENABLE_HIP
377 return main_<double, int, Kokkos::HIP>(argc, argv);
378 #else
379 std::cout << "Error: HIP node type is disabled" << std::endl;
380 #endif
381 } else if (node == "sycl") {
382 #ifdef KOKKOS_ENABLE_SYCL
383 return main_<double, int, Kokkos::Experimental::SYCL>(argc, argv);
384 #else
385 std::cout << "Error: SYCL node type is disabled" << std::endl;
386 #endif
387 }
388
389 Kokkos::finalize();
390
391 return 0;
392 }