Debian Codegraph @

packages/muelu/test/scaling/TwoMatrixMMKernelDriver.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 <algorithm> // shuffle
12 #include <vector> // vector
13 #include <random>
14
15 #include <Xpetra_MultiVectorFactory.hpp>
16 #include <Xpetra_IO.hpp>
17
18 // Teuchos
19 #include <Teuchos_StandardCatchMacros.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 // MueLu
27 #include "MueLu.hpp"
28 #include "MueLu_TestHelpers.hpp"
29
30 #if defined(HAVE_TPETRA_INST_OPENMP)
31 #include "Xpetra_TpetraVector.hpp"
32 #include "TpetraExt_MatrixMatrix_def.hpp"
33 #endif
34
35 // =========================================================================
36 // Support Routines
37 // =========================================================================
38 template <class View1, class View2>
39 inline void copy_view_n(const int n, const View1 x1, View2 x2) {
40 Kokkos::parallel_for(
41 n, KOKKOS_LAMBDA(const size_t i) {
42 x2[i] = x1[i];
43 });
44 }
45
46 template <class View1, class View2>
47 inline void copy_view(const View1 x1, View2 x2) {
48 copy_view_n(x1.extent(0), x1, x2);
49 }
50
51 template <class V1, class V2>
52 void print_crs_graph(std::string name, const V1 rowptr, const V2 colind) {
53 printf("%s rowptr[%d] = ", name.c_str(), rowptr.extent(0));
54 for (size_t i = 0; i < rowptr.extent(0); i++)
55 printf(" %d", (int)rowptr[i]);
56 printf("\n%s colind[%d] = ", name.c_str(), colind.extent(0));
57 for (size_t i = 0; i < colind.extent(0); i++)
58 printf(" %d", (int)colind[i]);
59 printf("\n");
60 }
61
62 // =========================================================================
63 // MKL Testing
64 // =========================================================================
65 #ifdef HAVE_MUELU_MKL
66 #include "mkl.h"
67
68 std::string mkl_error(sparse_status_t code) {
69 switch (code) {
70 case SPARSE_STATUS_SUCCESS:
71 return std::string("Success");
72 case SPARSE_STATUS_NOT_INITIALIZED:
73 return std::string("Empty handle or matrix array");
74 case SPARSE_STATUS_ALLOC_FAILED:
75 return std::string("Memory allocation failed");
76 case SPARSE_STATUS_INVALID_VALUE:
77 return std::string("Input contains an invalid value");
78 case SPARSE_STATUS_EXECUTION_FAILED:
79 return std::string("Execution failed");
80 case SPARSE_STATUS_INTERNAL_ERROR:
81 return std::string("Internal error");
82 case SPARSE_STATUS_NOT_SUPPORTED:
83 return std::string("Operation not supported");
84 };
85 }
86
87 // mkl_sparse_spmm
88 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
89 void MM2_MKL(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &A, const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &B1, Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &B2, Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &C, Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > &Ccolmap, std::string algorithm_name) {
90 #include <MueLu_UseShortNames.hpp>
91 using Teuchos::RCP;
92 using Teuchos::rcp;
93 using Teuchos::TimeMonitor;
94 // NOTE: MKL uses int as its columnn index type and the either double or float for its Scalar type
95
96 Xpetra::UnderlyingLib lib = A.getRowMap()->lib();
97 RCP<TimeMonitor> tm;
98 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type;
99 typedef typename crs_matrix_type::local_matrix_device_type KCRS;
100 typedef typename KCRS::StaticCrsGraphType graph_t;
101 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
102 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
103 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
104 typedef typename graph_t::entries_type::const_type c_lno_nnz_view_t;
105 typedef typename KCRS::values_type::non_const_type scalar_view_t;
106 typedef Tpetra::Map<LO, GO, NO> map_type;
107 typedef typename map_type::local_map_type local_map_type;
108
109 typedef typename Kokkos::View<MKL_INT *, typename lno_nnz_view_t::array_layout, typename lno_nnz_view_t::device_type> mkl_int_type;
110
111 RCP<const crs_matrix_type> Au = toTpetra(rcp(&A, false));
112 RCP<const crs_matrix_type> B1u = toTpetra(rcp(&B1, false));
113 RCP<const crs_matrix_type> B2u = toTpetra(rcp(&B2, false));
114 RCP<const crs_matrix_type> Cu = toTpetra(rcp(&C, false));
115 RCP<crs_matrix_type> Cnc = Teuchos::rcp_const_cast<crs_matrix_type>(Cu);
116
117 const KCRS &Amat = Au->getLocalMatrixDevice();
118 const KCRS &B1mat = B1u->getLocalMatrixDevice();
119 const KCRS &B2mat = B2u->getLocalMatrixDevice();
120
121 if (A.getLocalNumRows() != C.getLocalNumRows()) throw std::runtime_error("C is not sized correctly");
122
123 c_lno_view_t Arowptr = Amat.graph.row_map, B1rowptr = B1mat.graph.row_map, B2rowptr = B2mat.graph.row_map;
124 lno_view_t Crowptr("Crowptr", C.getLocalNumRows() + 1);
125 c_lno_nnz_view_t Acolind = Amat.graph.entries, B1colind = B1mat.graph.entries, B2colind = B2mat.graph.entries;
126 lno_nnz_view_t Ccolind;
127 const scalar_view_t Avals = Amat.values, B1vals = B1mat.values, B2vals = B2mat.values;
128 scalar_view_t Cvals;
129 RCP<const Tpetra::Map<LO, GO, Node> > Ccolmap_t = Xpetra::toTpetra(Ccolmap);
130 local_map_type Bcolmap_local = B1u->getColMap()->getLocalMap();
131 local_map_type Icolmap_local = B2u->getColMap()->getLocalMap();
132 local_map_type Ccolmap_local = Ccolmap_t->getLocalMap();
133
134 sparse_matrix_t AMKL;
135 sparse_matrix_t B1MKL;
136 sparse_matrix_t B2MKL;
137 sparse_matrix_t CMKL;
138 sparse_matrix_t Temp1MKL, Temp2MKL;
139
140 sparse_status_t result;
141 // **********************************
142 // Copy in the matrix data for MKL
143 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MM2 MKL " + algorithm_name + ": Matrix CopyIn")));
144
145 mkl_int_type ArowptrMKL("Arowptr", Arowptr.extent(0));
146 mkl_int_type AcolindMKL("Acolind", Acolind.extent(0));
147 mkl_int_type B1colindMKL("B1colind", B1colind.extent(0));
148 mkl_int_type B2colindMKL("B2colind", B2colind.extent(0));
149
150 // Generate new rowptrs that have all the rows for both B and I (as MKL requires)
151 size_t Nb = B1rowptr.extent(0) - 1, Ni = B2rowptr.extent(0) - 1;
152 mkl_int_type B1rowptrMKL("B1rowptr", Nb + Ni + 1);
153 mkl_int_type B2rowptrMKL("B2rowptr", Nb + Ni + 1);
154 Kokkos::parallel_for(
155 Nb + 1, KOKKOS_LAMBDA(const size_t i) {
156 B1rowptrMKL[i] = B1rowptr[i];
157 if (i != Nb) B2rowptrMKL[i] = 0;
158 });
159 Kokkos::parallel_for(
160 Ni + 1, KOKKOS_LAMBDA(const size_t i) {
161 if (i != 0) B1rowptrMKL[Nb + i] = B1rowptr[Nb - 1];
162 B2rowptrMKL[Nb + i] = B2rowptr[i];
163 });
164
165 // Reindex the column indices into C's column map
166 Kokkos::parallel_for(
167 B1colind.extent(0), KOKKOS_LAMBDA(const size_t i) {
168 B1colindMKL[i] = Ccolmap_local.getLocalElement(Bcolmap_local.getGlobalElement(B1colind(i)));
169 });
170
171 Kokkos::parallel_for(
172 B2colind.extent(0), KOKKOS_LAMBDA(const size_t i) {
173 B2colindMKL[i] = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(B2colind(i)));
174 });
175
176 // Copy in A
177 copy_view(Arowptr, ArowptrMKL);
178 copy_view(Acolind, AcolindMKL);
179
180 if (std::is_same<Scalar, double>::value) {
181 mkl_sparse_d_create_csr(&AMKL, SPARSE_INDEX_BASE_ZERO, Au->getLocalNumRows(), Au->getLocalNumCols(), ArowptrMKL.data(), ArowptrMKL.data() + 1, AcolindMKL.data(), (double *)Avals.data());
182 mkl_sparse_d_create_csr(&B1MKL, SPARSE_INDEX_BASE_ZERO, Nb + Ni, Ccolmap->getLocalNumElements(), B1rowptrMKL.data(), B1rowptrMKL.data() + 1, B1colindMKL.data(), (double *)B1vals.data());
183 mkl_sparse_d_create_csr(&B2MKL, SPARSE_INDEX_BASE_ZERO, Nb + Ni, Ccolmap->getLocalNumElements(), B2rowptrMKL.data(), B2rowptrMKL.data() + 1, B2colindMKL.data(), (double *)B2vals.data());
184 } else
185 throw std::runtime_error("MKL Type Mismatch");
186 tm = Teuchos::null;
187 Au->getComm()->barrier();
188
189 tm = Teuchos::null;
190 Au->getComm()->barrier();
191
192 if (algorithm_name == "MULT_ADD") {
193 // **********************************
194 // Multiply #1 (A*B1)
195 {
196 TimeMonitor tm1(*TimeMonitor::getNewTimer("MM2 MKL MULT_ADD: Multiply 1"));
197 result = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE, AMKL, B1MKL, &Temp1MKL);
198 typename KCRS::execution_space().fence();
199 if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Multiply 1 failed: " + mkl_error(result));
200 }
201
202 // **********************************
203 // Multiply #2 (A*B2)
204 {
205 TimeMonitor tm2(*TimeMonitor::getNewTimer("MM2 MKL MULT_ADD: Multiply 2"));
206 result = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE, AMKL, B1MKL, &Temp2MKL);
207 typename KCRS::execution_space().fence();
208 if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Multiply 2 failed: " + mkl_error(result));
209 }
210
211 // **********************************
212 // Add (A*B1) + (A*B2)
213 {
214 TimeMonitor tm3(*TimeMonitor::getNewTimer("MM2 MKL MULT_ADD: Add"));
215 result = mkl_sparse_d_add(SPARSE_OPERATION_NON_TRANSPOSE, Temp1MKL, 1.0, Temp2MKL, &CMKL);
216 typename KCRS::execution_space().fence();
217 if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Add failed: " + mkl_error(result));
218 }
219 } else if (algorithm_name == "ADD_MULT") {
220 // **********************************
221 // Add B1 + B2
222 {
223 TimeMonitor tm4(*TimeMonitor::getNewTimer("MM2 MKL ADD_MULT: Add"));
224 result = mkl_sparse_d_add(SPARSE_OPERATION_NON_TRANSPOSE, B1MKL, 1.0, B2MKL, &Temp1MKL);
225 typename KCRS::execution_space().fence();
226 if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Add failed: " + mkl_error(result));
227 }
228
229 // **********************************
230 // Multiply A*(B1+B2)
231 {
232 TimeMonitor tm5(*TimeMonitor::getNewTimer("MM2 MKL ADD_MULT: Multiply"));
233 result = mkl_sparse_spmm(SPARSE_OPERATION_NON_TRANSPOSE, AMKL, Temp1MKL, &CMKL);
234 typename KCRS::execution_space().fence();
235 if (result != SPARSE_STATUS_SUCCESS) throw std::runtime_error("MKL Multiply failed: " + mkl_error(result));
236 }
237 } else
238 throw std::runtime_error("Invalid MKL algorithm");
239
240 // **********************************
241 // Copy out the data for MKL
242 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MM2 MKL " + algorithm_name + ": Copy Out")));
243 sparse_index_base_t c_indexing;
244 MKL_INT c_rows, c_cols, *rows_start, *rows_end, *columns;
245 double *values;
246 mkl_sparse_d_export_csr(CMKL, &c_indexing, &c_rows, &c_cols, &rows_start, &rows_end, &columns, &values);
247 size_t cnnz = rows_end[c_rows - 1];
248 Kokkos::resize(Ccolind, cnnz);
249 Kokkos::resize(Cvals, cnnz);
250 if (c_rows != A.getLocalNumRows() || c_rows + 1 != Crowptr.extent(0)) throw std::runtime_error("C row size mismatch");
251 copy_view_n(c_rows, rows_start, Crowptr);
252 Crowptr(c_rows) = rows_end[c_rows - 1];
253 copy_view_n(cnnz, columns, Ccolind);
254 copy_view_n(cnnz, values, Cvals);
255
256 Cnc->replaceColMap(Ccolmap_t);
257 Cnc->setAllValues(Crowptr,
258 Ccolind,
259 Cvals);
260
261 mkl_sparse_destroy(AMKL);
262 mkl_sparse_destroy(B1MKL);
263 mkl_sparse_destroy(B2MKL);
264 mkl_sparse_destroy(CMKL);
265 mkl_sparse_destroy(Temp1MKL);
266 if (algorithm_name == "MULT_ADD") mkl_sparse_destroy(Temp2MKL);
267
268 tm = Teuchos::null;
269 Au->getComm()->barrier();
270 }
271
272 #endif
273
274 // =========================================================================
275 // Tpetra Kernel Testing
276 // =========================================================================
277 #include "Tpetra_Import_Util2.hpp"
278 #include "TpetraExt_MatrixMatrix.hpp"
279 #include "TpetraExt_MatrixMatrix_ExtraKernels_decl.hpp"
280 #include "TpetraExt_MatrixMatrix_ExtraKernels_def.hpp"
281
282 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
283 void MM2_Wrapper(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &A, const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &B1, Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &B2, Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> &C, Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > &Ccolmap, std::string algorithm_name, int team_work_size) {
284 #include <MueLu_UseShortNames.hpp>
285 using Teuchos::RCP;
286 using Teuchos::rcp;
287 using Teuchos::TimeMonitor;
288
289 Xpetra::UnderlyingLib lib = A.getRowMap()->lib();
290 RCP<TimeMonitor> tm;
291
292 std::string name = algorithm_name;
293
294 if (lib == Xpetra::UseTpetra) {
295 #if defined(HAVE_TPETRA_INST_OPENMP)
296 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> crs_matrix_type;
297 typedef Tpetra::Import<LocalOrdinal, GlobalOrdinal, Node> import_type;
298 typedef typename crs_matrix_type::local_matrix_device_type KCRS;
299 typedef typename KCRS::device_type device_t;
300 typedef typename KCRS::StaticCrsGraphType graph_t;
301 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
302 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
303 typedef typename KCRS::values_type::non_const_type scalar_view_t;
304 typedef Kokkos::View<LO *, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
305 typedef Tpetra::Map<LO, GO, NO> map_type;
306 typedef typename map_type::local_map_type local_map_type;
307 typedef typename Node::execution_space execution_space;
308 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
309 LocalOrdinal LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
310 RCP<const import_type> Cimport;
311 RCP<const crs_matrix_type> Au = toTpetra(rcpFromRef(A));
312 RCP<const crs_matrix_type> B1u = toTpetra(rcp(&B1, false));
313 RCP<crs_matrix_type> B2u = Teuchos::rcp_const_cast<crs_matrix_type>(toTpetra(rcp(&B2, false)));
314 RCP<const crs_matrix_type> Cu = toTpetra(rcp(&C, false));
315 RCP<crs_matrix_type> Cnc = Teuchos::rcp_const_cast<crs_matrix_type>(Cu);
316
317 // **********************************
318 // Copy in the data for Kernel Wrapper
319 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MM2 " + name + ": CopyIn")));
320
321 Tpetra::CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Aview, Bview;
322 Aview.origMatrix = Au;
323 Aview.origRowMap = Au->getRowMap();
324 Aview.rowMap = Au->getRowMap();
325 Aview.colMap = Au->getColMap();
326 Aview.domainMap = Au->getDomainMap();
327 Aview.importColMap = Teuchos::null;
328
329 Bview.origMatrix = B1u;
330 Bview.origRowMap = B1u->getRowMap();
331 Bview.rowMap = B1u->getRowMap();
332 Bview.colMap = B1u->getColMap();
333 Bview.domainMap = B1u->getDomainMap();
334 Bview.importColMap = Teuchos::null;
335
336 // A bunch of this stuff isn't getting set "right", but we fake it for testing purposes here...
337 Bview.importMatrix = B2u;
338 Bview.importColMap = B2u->getColMap();
339
340 RCP<const Tpetra::Map<LO, GO, Node> > Ccolmap_t = Xpetra::toTpetra(Ccolmap);
341 Cnc->replaceColMap(Ccolmap_t);
342
343 local_map_type Browmap_local = Bview.origMatrix->getRowMap()->getLocalMap();
344 local_map_type Irowmap_local = Bview.importMatrix->getRowMap()->getLocalMap();
345
346 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
347 local_map_type Bcolmap_local = Bview.origMatrix->getColMap()->getLocalMap();
348 local_map_type Icolmap_local = Bview.importMatrix->getColMap()->getLocalMap();
349 local_map_type Ccolmap_local = Ccolmap_t->getLocalMap();
350
351 // Bcol2Ccol
352 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing("Bcol2Ccol"), Bview.origMatrix->getColMap()->getLocalNumElements());
353 lo_view_t Icol2Ccol(Kokkos::ViewAllocateWithoutInitializing("Icol2Ccol"), Bview.importMatrix->getColMap()->getLocalNumElements());
354 Kokkos::parallel_for(
355 "Tpetra::mult_A_B_newmatrix::Bcol2Ccol_getGlobalElement", range_type(0, Bview.origMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(const LO i) {
356 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Bcolmap_local.getGlobalElement(i));
357 });
358 Kokkos::parallel_for(
359 "Tpetra::mult_A_B_newmatrix::Icol2Ccol_getGlobalElement", range_type(0, Bview.importMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(const LO i) {
360 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
361 });
362
363 // Acol2Brow
364 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing("targetMapToOrigRow"), Aview.colMap->getLocalNumElements());
365 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing("targetMapToImportRow"), Aview.colMap->getLocalNumElements());
366 Kokkos::parallel_for(
367 "Tpetra::mult_A_B_newmatrix::construct_tables", range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex() + 1), KOKKOS_LAMBDA(const LO i) {
368 GO aidx = Acolmap_local.getGlobalElement(i);
369 LO B_LID = Browmap_local.getLocalElement(aidx);
370 if (B_LID != LO_INVALID) {
371 targetMapToOrigRow(i) = B_LID;
372 targetMapToImportRow(i) = LO_INVALID;
373 } else {
374 LO I_LID = Irowmap_local.getLocalElement(aidx);
375 targetMapToOrigRow(i) = LO_INVALID;
376 targetMapToImportRow(i) = I_LID;
377 }
378 });
379 tm = Teuchos::null;
380 Au->getComm()->barrier();
381
382 // **********************************
383 // Multiply
384 Teuchos::RCP<Teuchos::ParameterList> params = Teuchos::rcp(new Teuchos::ParameterList);
385 params->set("openmp: algorithm", algorithm_name);
386 params->set("openmp: team work size", team_work_size);
387
388 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer(std::string("MM2 ") + name + ": Kernel")));
389 Tpetra::MMdetails::KernelWrappers<SC, LO, GO, Node, lno_nnz_view_t>::mult_A_B_newmatrix_kernel_wrapper(Aview, Bview, targetMapToOrigRow, targetMapToImportRow, Bcol2Ccol, Icol2Ccol, *Cnc, Cimport, name, params);
390
391 tm = Teuchos::null;
392 Au->getComm()->barrier();
393
394 #endif
395 }
396 }
397
398 // =========================================================================
399 // =========================================================================
400 // =========================================================================
401 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
402 int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int argc, char *argv[]) {
403 #include <MueLu_UseShortNames.hpp>
404
405 using std::endl;
406 using Teuchos::RCP; // reference count pointers
407 using Teuchos::rcp;
408 using Teuchos::TimeMonitor;
409
410 bool success = false;
411 bool verbose = true;
412 try {
413 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
414
415 // =========================================================================
416 // Convenient definitions
417 // =========================================================================
418 // SC zero = Teuchos::ScalarTraits<SC>::zero(), one = Teuchos::ScalarTraits<SC>::one();
419
420 // Instead of checking each time for rank, create a rank 0 stream
421 RCP<Teuchos::FancyOStream> pOut = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
422 Teuchos::FancyOStream &out = *pOut;
423 out.setOutputToRootOnly(0);
424
425 // =========================================================================
426 // Parameters initialization
427 // =========================================================================
428 // GO nx = 50, ny = 50, nz = 50;
429 // Galeri::Xpetra::Parameters<GO> matrixParameters(clp, nx, ny, nz, "Laplace3D"); // manage parameters of the test case
430 Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra
431
432 bool printTimings = true;
433 clp.setOption("timings", "notimings", &printTimings, "print timings to screen");
434 int nrepeat = 100;
435 clp.setOption("nrepeat", &nrepeat, "repeat the experiment N times");
436 // the kernels
437 bool do_mkl_ma = true;
438 bool do_mkl_am = true;
439 bool do_kk_mem = true;
440 bool do_kk_dense = true;
441 bool do_kk_default = true;
442 bool do_ltg = true;
443 double percent_rows_to_remove = 10.0;
444 clp.setOption("remove-rows", &percent_rows_to_remove, "% of rows to remove from B to make B1 & B2");
445
446 #ifndef HAVE_MUELU_MKL
447 do_mkl_ma = false;
448 do_mkl_am = false;
449 #endif
450 clp.setOption("mkl_ma", "nomkl_ma", &do_mkl_ma, "Evaluate MKL Mult-Add");
451 clp.setOption("mkl_am", "nomkl_am", &do_mkl_am, "Evaluate MKL Add-Mult");
452 clp.setOption("kk_mem", "nokk_mem", &do_kk_mem, "Evaluate KK Mem");
453 clp.setOption("kk_dense", "nokk_dense", &do_kk_dense, "Evaluate KK Dense");
454 clp.setOption("kk_default", "nokk_default", &do_kk_default, "Evaluate KK Default");
455 clp.setOption("ltg", "noltg", &do_ltg, "Evaluate LTG");
456 int kk_team_work_size = 16;
457
458 std::string matrixFileNameA = "A.mm";
459 clp.setOption("matrixfileA", &matrixFileNameA, "matrix market file containing matrix");
460 std::string matrixFileNameB = "B.mm";
461 clp.setOption("matrixfileB", &matrixFileNameB, "matrix market file containing matrix");
462
463 switch (clp.parse(argc, argv)) {
464 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
465 case Teuchos::CommandLineProcessor::PARSE_ERROR:
466 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
467 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
468 }
469
470 if (percent_rows_to_remove < 0.0) percent_rows_to_remove = 0.0;
471 if (percent_rows_to_remove > 100.0) percent_rows_to_remove = 100.0;
472
473 #ifndef HAVE_MUELU_MKL
474 if (do_mkl_ma || do_mkl_am) {
475 out << "MKL was requested, but this kernel is not available. Disabling..." << endl;
476 do_mkl_ma = false;
477 do_mkl_am = false;
478 }
479 #endif
480
481 // simple hack to randomize order of experiments
482 enum class Experiments { MKL_MULT_ADD = 0,
483 MKL_ADD_MULT,
484 KK_MEM,
485 KK_DENSE,
486 KK_DEFAULT,
487 LTG };
488 std::vector<Experiments> my_experiments;
489 // add the experiments we will run
490
491 #ifdef HAVE_MUELU_MKL
492 if (do_mkl_ma) my_experiments.push_back(Experiments::MKL_MULT_ADD); // MKL Mult-Add
493 if (do_mkl_am) my_experiments.push_back(Experiments::MKL_ADD_MULT); // MKL Add-Mult
494 #endif
495
496 // assume these are available
497 if (do_kk_mem) my_experiments.push_back(Experiments::KK_MEM); // KK Mem
498 if (do_kk_dense) my_experiments.push_back(Experiments::KK_DENSE); // KK Dense
499 if (do_kk_default) my_experiments.push_back(Experiments::KK_DEFAULT); // KK Default
500 if (do_ltg) my_experiments.push_back(Experiments::LTG); // LTG
501
502 out << "========================================================" << endl
503 << xpetraParameters
504 // << matrixParameters
505 << "========================================================" << endl
506 << "Template Types:" << endl
507 << " Scalar: " << Teuchos::demangleName(typeid(SC).name()) << endl
508 << " LocalOrdinal: " << Teuchos::demangleName(typeid(LO).name()) << endl
509 << " GlobalOrdinal: " << Teuchos::demangleName(typeid(GO).name()) << endl
510 << " Node: " << Teuchos::demangleName(typeid(NO).name()) << endl
511 << "Sizes:" << endl
512 << " Scalar: " << sizeof(SC) << endl
513 << " LocalOrdinal: " << sizeof(LO) << endl
514 << " GlobalOrdinal: " << sizeof(GO) << endl
515 << "========================================================" << endl
516 << "Matrix: " << Teuchos::demangleName(typeid(Matrix).name()) << endl
517 << "Vector: " << Teuchos::demangleName(typeid(Vector).name()) << endl
518 << "Hierarchy: " << Teuchos::demangleName(typeid(Hierarchy).name()) << endl
519 << "========================================================" << endl;
520
521 #if defined(HAVE_TPETRA_INST_OPENMP)
522 out << "Tpetra::KokkosCompat::KokkosOpenMPWrapperNode::execution_space().concurrency() = " << Tpetra::KokkosCompat::KokkosOpenMPWrapperNode::execution_space().concurrency() << endl
523 << "========================================================" << endl;
524 #endif
525
526 // At the moment, this test only runs on one MPI rank
527 if (comm->getSize() != 1) exit(1);
528
529 // =========================================================================
530 // Problem construction
531 // =========================================================================
532 RCP<TimeMonitor> globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MatrixRead: S - Global Time")));
533
534 comm->barrier();
535
536 RCP<Matrix> A = Xpetra::IO<SC, LO, GO, Node>::Read(std::string(matrixFileNameA), lib, comm);
537 RCP<Matrix> B = Xpetra::IO<SC, LO, GO, Node>::Read(std::string(matrixFileNameB), lib, comm);
538 RCP<Matrix> B1, B2, C;
539
540 RCP<const Map> Browmap = B->getRowMap();
541 RCP<const Map> Bcolmap = B->getColMap();
542
543 // Do some surgery to generate B1 and B2
544 const GO GO_INVALID = Teuchos::OrdinalTraits<GO>::invalid();
545 size_t brows = Browmap->getLocalNumElements();
546 size_t b2rows = std::max((size_t)0, std::min((size_t)(brows * percent_rows_to_remove / 100.0), brows));
547 size_t b1rows = brows - b2rows;
548 {
549 Teuchos::Array<GO> b1list(b1rows), b2list(b2rows);
550 for (size_t i = 0, i1 = 0, i2 = 0; i < brows; i++) {
551 if (i < b1rows) {
552 b1list[i1] = Browmap->getGlobalElement(i);
553 i1++;
554 } else {
555 b2list[i2] = Browmap->getGlobalElement(i);
556 i2++;
557 }
558 }
559
560 RCP<const Map> B1rowmap = Xpetra::MapFactory<LO, GO, Node>::Build(lib, GO_INVALID, b1list(), 0, comm);
561 RCP<const Map> B2rowmap = Xpetra::MapFactory<LO, GO, Node>::Build(lib, GO_INVALID, b2list(), 0, comm);
562
563 RCP<const Import> B1import = Xpetra::ImportFactory<LO, GO, Node>::Build(Browmap, B1rowmap);
564 RCP<const Import> B2import = Xpetra::ImportFactory<LO, GO, Node>::Build(Browmap, B2rowmap);
565 RCP<const Map> dummy;
566 B1 = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(B, *B1import, dummy, B1rowmap);
567 B2 = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(B, *B2import, dummy, B2rowmap);
568 }
569
570 globalTimeMonitor = Teuchos::null;
571 comm->barrier();
572
573 out << "Matrix Read complete." << endl
574 << "Matrix A:" << endl
575 << *A
576 << "========================================================" << endl
577 << "Matrix B1: rows " << b1rows << endl
578 << *B1
579 << "========================================================" << endl
580 << "Matrix B2: rows " << b2rows << endl
581 << *B2
582 << "========================================================" << endl;
583
584 // random source
585 std::random_device rd;
586 std::mt19937 random_source(rd());
587
588 // no need for a barrier, because the randomization process uses a collective.
589 if (!my_experiments.empty()) {
590 for (int i = 0; i < nrepeat; i++) {
591 // randomize the experiments
592 if (comm->getRank() == 0) {
593 std::shuffle(my_experiments.begin(), my_experiments.end(), random_source);
594 }
595
596 // Broadcast this ordering to the other processes
597 comm->broadcast(0,
598 static_cast<int>(sizeof(Experiments::LTG) * my_experiments.size()),
599 reinterpret_cast<char *>(my_experiments.data()));
600
601 // loop over the randomized experiments
602 for (const auto &experiment_id : my_experiments) {
603 switch (experiment_id) {
604 // MKL MULT_ADD
605 case Experiments::MKL_MULT_ADD:
606 #ifdef HAVE_MUELU_MKL
607 C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0);
608 {
609 TimeMonitor t(*TimeMonitor::getNewTimer("MM2 MKL MULT_ADD: Total"));
610 MM2_MKL(*A, *B1, *B2, *C, Bcolmap, std::string("MULT_ADD"));
611 }
612 #endif
613 break;
614 // MKL ADD_MULT
615 case Experiments::MKL_ADD_MULT:
616 #ifdef HAVE_MUELU_MKL
617 C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0);
618 {
619 TimeMonitor t(*TimeMonitor::getNewTimer("MM2 MKL ADD_MULT: Total"));
620 MM2_MKL(*A, *B1, *B2, *C, Bcolmap, std::string("ADD_MULT"));
621 }
622 #endif
623 break;
624 // KK Algorithms (KK Memory)
625 case Experiments::KK_MEM:
626 C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0);
627 {
628 TimeMonitor t(*TimeMonitor::getNewTimer("MM2 SPGEMM_KK_MEMORY: Total"));
629 MM2_Wrapper(*A, *B1, *B2, *C, Bcolmap, std::string("SPGEMM_KK_MEMORY"), kk_team_work_size);
630 }
631 break;
632 // KK Algorithms (KK Dense)
633 case Experiments::KK_DENSE:
634 C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0);
635 {
636 TimeMonitor t(*TimeMonitor::getNewTimer("MM2 SPGEMM_KK_DENSE: Total"));
637 MM2_Wrapper(*A, *B1, *B2, *C, Bcolmap, std::string("SPGEMM_KK_DENSE"), kk_team_work_size);
638 }
639 break;
640 // KK Algorithms (KK Default)
641 case Experiments::KK_DEFAULT:
642 C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0);
643 {
644 TimeMonitor t(*TimeMonitor::getNewTimer("MM2 SPGEMM_KK: Total"));
645 MM2_Wrapper(*A, *B1, *B2, *C, Bcolmap, std::string("SPGEMM_KK"), kk_team_work_size);
646 }
647 break;
648 // LTG
649 case Experiments::LTG:
650 C = Xpetra::MatrixFactory<SC, LO, GO, Node>::Build(A->getRowMap(), 0);
651 {
652 TimeMonitor t(*TimeMonitor::getNewTimer("MM2 LTG: Total"));
653 MM2_Wrapper(*A, *B1, *B2, *C, Bcolmap, std::string("LTG"), kk_team_work_size);
654 }
655 break;
656
657 default:
658 std::cerr << "Unknown experiment ID encountered: " << (int)experiment_id << std::endl;
659 }
660 comm->barrier();
661 } // end random exp loop
662 } // end repeat
663 } // end ! my_experiments.empty()
664
665 if (printTimings) {
666 TimeMonitor::summarize(A->getRowMap()->getComm().ptr(), std::cout, false, true, false, Teuchos::Union, "", true);
667 }
668
669 success = true;
670 }
671 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
672
673 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
674 } // main
675
676 //- -- --------------------------------------------------------
677 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_
678 #include "MueLu_Test_ETI.hpp"
679
680 int main(int argc, char *argv[]) {
681 return Automatic_Test_ETI(argc, argv);
682 }