10#ifndef TPETRA_MATRIXMATRIX_SYCL_DEF_HPP
11#define TPETRA_MATRIXMATRIX_SYCL_DEF_HPP
13#include "Tpetra_Details_IntRowPtrHelper.hpp"
15#ifdef HAVE_TPETRA_INST_SYCL
21template <
class Scalar,
24 class LocalOrdinalViewType>
25struct KernelWrappers<Scalar, LocalOrdinal, GlobalOrdinal,
Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType> {
26 static inline void mult_A_B_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
27 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
28 const LocalOrdinalViewType& Acol2Brow,
29 const LocalOrdinalViewType& Acol2Irow,
30 const LocalOrdinalViewType& Bcol2Ccol,
31 const LocalOrdinalViewType& Icol2Ccol,
32 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
33 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
34 const std::string& label = std::string(),
35 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null);
37 static inline void mult_A_B_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
38 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
39 const LocalOrdinalViewType& Acol2Brow,
40 const LocalOrdinalViewType& Acol2Irow,
41 const LocalOrdinalViewType& Bcol2Ccol,
42 const LocalOrdinalViewType& Icol2Ccol,
43 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
44 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
45 const std::string& label = std::string(),
46 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null);
50template <
class Scalar,
52 class GlobalOrdinal,
class LocalOrdinalViewType>
53struct KernelWrappers2<Scalar, LocalOrdinal, GlobalOrdinal,
Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType> {
54 static inline void jacobi_A_B_newmatrix_kernel_wrapper(
typename Teuchos::ScalarTraits<Scalar>::magnitudeType omega,
55 const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Dinv,
56 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
57 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
58 const LocalOrdinalViewType& Acol2Brow,
59 const LocalOrdinalViewType& Acol2Irow,
60 const LocalOrdinalViewType& Bcol2Ccol,
61 const LocalOrdinalViewType& Icol2Ccol,
62 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
63 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
64 const std::string& label = std::string(),
65 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null);
67 static inline void jacobi_A_B_reuse_kernel_wrapper(
typename Teuchos::ScalarTraits<Scalar>::magnitudeType omega,
68 const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Dinv,
69 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
70 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
71 const LocalOrdinalViewType& Acol2Brow,
72 const LocalOrdinalViewType& Acol2Irow,
73 const LocalOrdinalViewType& Bcol2Ccol,
74 const LocalOrdinalViewType& Icol2Ccol,
75 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
76 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
77 const std::string& label = std::string(),
78 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null);
80 static inline void jacobi_A_B_newmatrix_KokkosKernels(
typename Teuchos::ScalarTraits<Scalar>::magnitudeType omega,
81 const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Dinv,
82 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
83 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
84 const LocalOrdinalViewType& Acol2Brow,
85 const LocalOrdinalViewType& Acol2Irow,
86 const LocalOrdinalViewType& Bcol2Ccol,
87 const LocalOrdinalViewType& Icol2Ccol,
88 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
89 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
90 const std::string& label = std::string(),
91 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null);
96template <
class Scalar,
99 class LocalOrdinalViewType>
100void KernelWrappers<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType>::mult_A_B_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
101 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
102 const LocalOrdinalViewType& Acol2Brow,
103 const LocalOrdinalViewType& Acol2Irow,
104 const LocalOrdinalViewType& Bcol2Ccol,
105 const LocalOrdinalViewType& Icol2Ccol,
106 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
107 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
108 const std::string& label,
109 const Teuchos::RCP<Teuchos::ParameterList>& params) {
110#ifdef HAVE_TPETRA_MMM_TIMINGS
111 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
112 using Teuchos::TimeMonitor;
113 Teuchos::RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"MMM Newmatrix SYCLWrapper")))));
116 typedef Tpetra::KokkosCompat::KokkosSYCLWrapperNode Node;
117 std::string nodename(
"SYCL");
122 typedef typename KCRS::device_type device_t;
123 typedef typename KCRS::StaticCrsGraphType graph_t;
124 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
125 using int_view_t = Kokkos::View<int*, typename lno_view_t::array_layout, typename lno_view_t::memory_space, typename lno_view_t::memory_traits>;
126 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
127 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
128 typedef typename KCRS::values_type::non_const_type scalar_view_t;
132 int team_work_size = 16;
133 std::string myalg(
"SPGEMM_KK_MEMORY");
134 if (!params.is_null()) {
135 if (params->isParameter(
"sycl: algorithm"))
136 myalg = params->get(
"sycl: algorithm", myalg);
137 if (params->isParameter(
"sycl: team work size"))
138 team_work_size = params->get(
"sycl: team work size", team_work_size);
142 typedef KokkosKernels::Experimental::KokkosKernelsHandle<
143 typename lno_view_t::const_value_type,
typename lno_nnz_view_t::const_value_type,
typename scalar_view_t::const_value_type,
144 typename device_t::execution_space,
typename device_t::memory_space,
typename device_t::memory_space>
146 using IntKernelHandle = KokkosKernels::Experimental::KokkosKernelsHandle<
147 typename int_view_t::const_value_type,
typename lno_nnz_view_t::const_value_type,
typename scalar_view_t::const_value_type,
148 typename device_t::execution_space,
typename device_t::memory_space,
typename device_t::memory_space>;
151 const KCRS Amat = Aview.origMatrix->getLocalMatrixDevice();
152 const KCRS Bmat = Bview.origMatrix->getLocalMatrixDevice();
154 c_lno_view_t Arowptr = Amat.graph.row_map,
155 Browptr = Bmat.graph.row_map;
156 const lno_nnz_view_t Acolind = Amat.graph.entries,
157 Bcolind = Bmat.graph.entries;
158 const scalar_view_t Avals = Amat.values,
162 std::string alg = nodename + std::string(
" algorithm");
164 if (!params.is_null() && params->isParameter(alg)) myalg = params->get(alg, myalg);
165 KokkosSparse::SPGEMMAlgorithm alg_enum = KokkosSparse::StringToSPGEMMAlgorithm(myalg);
168 const KCRS Bmerged = Tpetra::MMdetails::merge_matrices(Aview, Bview, Acol2Brow, Acol2Irow, Bcol2Ccol, Icol2Ccol, C.getColMap()->getLocalNumElements());
170#ifdef HAVE_TPETRA_MMM_TIMINGS
172 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"MMM Newmatrix SYCLCore"))));
176 typename KernelHandle::nnz_lno_t AnumRows = Amat.numRows();
177 typename KernelHandle::nnz_lno_t BnumRows = Bmerged.numRows();
178 typename KernelHandle::nnz_lno_t BnumCols = Bmerged.numCols();
180 lno_view_t row_mapC(Kokkos::ViewAllocateWithoutInitializing(
"non_const_lno_row"), AnumRows + 1);
181 lno_nnz_view_t entriesC;
182 scalar_view_t valuesC;
187 const bool useIntRowptrs =
188 irph.shouldUseIntRowptrs() &&
189 Aview.
origMatrix->getApplyHelper()->shouldUseIntRowptrs();
193 kh.create_spgemm_handle(alg_enum);
194 kh.set_team_work_size(team_work_size);
196 int_view_t int_row_mapC(Kokkos::ViewAllocateWithoutInitializing(
"non_const_int_row"), AnumRows + 1);
198 auto Aint = Aview.origMatrix->getApplyHelper()->getIntRowptrMatrix(Amat);
199 auto Bint = irph.getIntRowptrMatrix(Bmerged);
200 KokkosSparse::spgemm_symbolic(&kh, AnumRows, BnumRows, BnumCols, Aint.graph.row_map, Aint.graph.entries,
false, Bint.graph.row_map, Bint.graph.entries,
false, int_row_mapC);
202 size_t c_nnz_size = kh.get_spgemm_handle()->get_c_nnz();
204 entriesC = lno_nnz_view_t(Kokkos::ViewAllocateWithoutInitializing(
"entriesC"), c_nnz_size);
205 valuesC = scalar_view_t(Kokkos::ViewAllocateWithoutInitializing(
"valuesC"), c_nnz_size);
207 KokkosSparse::spgemm_numeric(&kh, AnumRows, BnumRows, BnumCols, Aint.graph.row_map, Aint.graph.entries, Aint.values,
false, Bint.graph.row_map, Bint.graph.entries, Bint.values,
false, int_row_mapC, entriesC, valuesC);
209 Kokkos::parallel_for(
210 int_row_mapC.size(), KOKKOS_LAMBDA(
int i) { row_mapC(i) = int_row_mapC(i); });
211 kh.destroy_spgemm_handle();
215 kh.create_spgemm_handle(alg_enum);
216 kh.set_team_work_size(team_work_size);
218 KokkosSparse::spgemm_symbolic(&kh, AnumRows, BnumRows, BnumCols, Amat.graph.row_map, Amat.graph.entries,
false, Bmerged.graph.row_map, Bmerged.graph.entries,
false, row_mapC);
220 size_t c_nnz_size = kh.get_spgemm_handle()->get_c_nnz();
222 entriesC = lno_nnz_view_t(Kokkos::ViewAllocateWithoutInitializing(
"entriesC"), c_nnz_size);
223 valuesC = scalar_view_t(Kokkos::ViewAllocateWithoutInitializing(
"valuesC"), c_nnz_size);
226 KokkosSparse::spgemm_numeric(&kh, AnumRows, BnumRows, BnumCols, Amat.graph.row_map, Amat.graph.entries, Amat.values,
false, Bmerged.graph.row_map, Bmerged.graph.entries, Bmerged.values,
false, row_mapC, entriesC, valuesC);
227 kh.destroy_spgemm_handle();
230#ifdef HAVE_TPETRA_MMM_TIMINGS
232 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"MMM Newmatrix SYCLSort"))));
236 if (params.is_null() || params->get(
"sort entries",
true))
237 Import_Util::sortCrsEntries(row_mapC, entriesC, valuesC);
238 C.setAllValues(row_mapC, entriesC, valuesC);
240#ifdef HAVE_TPETRA_MMM_TIMINGS
242 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"MMM Newmatrix SYCLESFC"))));
246 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
247 labelList->set(
"Timer Label", label);
248 if (!params.is_null()) labelList->set(
"compute global constants", params->get(
"compute global constants",
true));
249 RCP<const Export<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > dummyExport;
250 C.expertStaticFillComplete(Bview.origMatrix->getDomainMap(), Aview.origMatrix->getRangeMap(), Cimport, dummyExport, labelList);
254template <
class Scalar,
257 class LocalOrdinalViewType>
258void KernelWrappers<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType>::mult_A_B_reuse_kernel_wrapper(
259 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
260 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
261 const LocalOrdinalViewType& targetMapToOrigRow_dev,
262 const LocalOrdinalViewType& targetMapToImportRow_dev,
263 const LocalOrdinalViewType& Bcol2Ccol_dev,
264 const LocalOrdinalViewType& Icol2Ccol_dev,
265 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
266 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
267 const std::string& label,
268 const Teuchos::RCP<Teuchos::ParameterList>& params) {
270 typedef Tpetra::KokkosCompat::KokkosSYCLWrapperNode Node;
274 bool throwOnInsert =
true;
275 if (!params.is_null() && params->isType<
bool>(
"MM Throw For Non-Existent Entries"))
276 throwOnInsert = params->get<
bool>(
"MM Throw For Non-Existent Entries");
278#ifdef HAVE_TPETRA_MMM_TIMINGS
279 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
280 using Teuchos::TimeMonitor;
281 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"MMM Reuse SerialCore"))));
282 Teuchos::RCP<Teuchos::TimeMonitor> MM2;
288 typedef typename Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_matrix_host_type KCRS;
289 typedef typename KCRS::StaticCrsGraphType graph_t;
290 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
291 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
292 typedef typename KCRS::values_type::non_const_type scalar_view_t;
295 typedef LocalOrdinal LO;
296 typedef GlobalOrdinal GO;
298 typedef Map<LO, GO, NO> map_type;
299 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
300 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
301 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
309 auto targetMapToOrigRow =
310 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
311 targetMapToOrigRow_dev);
312 auto targetMapToImportRow =
313 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
314 targetMapToImportRow_dev);
316 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
319 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
323 RCP<const map_type> Ccolmap = C.getColMap();
324 size_t m = Aview.origMatrix->getLocalNumRows();
325 size_t n = Ccolmap->getLocalNumElements();
328 const KCRS Amat = Aview.origMatrix->getLocalMatrixHost();
329 const KCRS Bmat = Bview.origMatrix->getLocalMatrixHost();
330 const KCRS Cmat = C.getLocalMatrixHost();
332 c_lno_view_t Arowptr = Amat.graph.row_map,
333 Browptr = Bmat.graph.row_map,
334 Crowptr = Cmat.graph.row_map;
335 const lno_nnz_view_t Acolind = Amat.graph.entries,
336 Bcolind = Bmat.graph.entries,
337 Ccolind = Cmat.graph.entries;
338 const scalar_view_t Avals = Amat.values, Bvals = Bmat.values;
339 scalar_view_t Cvals = Cmat.values;
341 c_lno_view_t Irowptr;
342 lno_nnz_view_t Icolind;
344 if (!Bview.importMatrix.is_null()) {
345 auto lclB = Bview.importMatrix->getLocalMatrixHost();
346 Irowptr = lclB.graph.row_map;
347 Icolind = lclB.graph.entries;
351#ifdef HAVE_TPETRA_MMM_TIMINGS
353 MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"MMM Newmatrix SerialCore - Compare"))));
365 std::vector<size_t> c_status(n, ST_INVALID);
368 size_t CSR_ip = 0, OLD_ip = 0;
369 for (
size_t i = 0; i < m; i++) {
373 CSR_ip = Crowptr[i + 1];
374 for (
size_t k = OLD_ip; k < CSR_ip; k++) {
375 c_status[Ccolind[k]] = k;
381 for (
size_t k = Arowptr[i]; k < Arowptr[i + 1]; k++) {
383 const SC Aval = Avals[k];
387 if (targetMapToOrigRow[Aik] != LO_INVALID) {
389 size_t Bk = Teuchos::as<size_t>(targetMapToOrigRow[Aik]);
391 for (
size_t j = Browptr[Bk]; j < Browptr[Bk + 1]; ++j) {
393 LO Cij = Bcol2Ccol[Bkj];
395 const bool badInsert = c_status[Cij] < OLD_ip || c_status[Cij] >= CSR_ip;
397 Cvals[c_status[Cij]] += Aval * Bvals[j];
398 else if (throwOnInsert)
399 TEUCHOS_TEST_FOR_EXCEPTION(badInsert,
400 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph "
401 <<
"(c_status = " << c_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
406 size_t Ik = Teuchos::as<size_t>(targetMapToImportRow[Aik]);
407 for (
size_t j = Irowptr[Ik]; j < Irowptr[Ik + 1]; ++j) {
409 LO Cij = Icol2Ccol[Ikj];
411 const bool badInsert = c_status[Cij] < OLD_ip || c_status[Cij] >= CSR_ip;
413 Cvals[c_status[Cij]] += Aval * Ivals[j];
414 else if (throwOnInsert)
415 TEUCHOS_TEST_FOR_EXCEPTION(badInsert,
416 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph "
417 <<
"(c_status = " << c_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
423 C.fillComplete(C.getDomainMap(), C.getRangeMap());
427template <
class Scalar,
430 class LocalOrdinalViewType>
431void KernelWrappers2<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType>::jacobi_A_B_newmatrix_kernel_wrapper(
typename Teuchos::ScalarTraits<Scalar>::magnitudeType omega,
432 const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Dinv,
433 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
434 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
435 const LocalOrdinalViewType& Acol2Brow,
436 const LocalOrdinalViewType& Acol2Irow,
437 const LocalOrdinalViewType& Bcol2Ccol,
438 const LocalOrdinalViewType& Icol2Ccol,
439 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
440 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
441 const std::string& label,
442 const Teuchos::RCP<Teuchos::ParameterList>& params) {
443#ifdef HAVE_TPETRA_MMM_TIMINGS
444 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
445 using Teuchos::TimeMonitor;
446 Teuchos::RCP<TimeMonitor> MM;
454 std::string myalg(
"KK");
455 if (!params.is_null()) {
456 if (params->isParameter(
"sycl: jacobi algorithm"))
457 myalg = params->get(
"sycl: jacobi algorithm", myalg);
460 if (myalg ==
"MSAK") {
461 ::Tpetra::MatrixMatrix::ExtraKernels::jacobi_A_B_newmatrix_MultiplyScaleAddKernel(omega, Dinv, Aview, Bview, Acol2Brow, Acol2Irow, Bcol2Ccol, Icol2Ccol, C, Cimport, label, params);
462 }
else if (myalg ==
"KK") {
463 jacobi_A_B_newmatrix_KokkosKernels(omega, Dinv, Aview, Bview, Acol2Brow, Acol2Irow, Bcol2Ccol, Icol2Ccol, C, Cimport, label, params);
465 throw std::runtime_error(
"Tpetra::MatrixMatrix::Jacobi newmatrix unknown kernel");
468#ifdef HAVE_TPETRA_MMM_TIMINGS
470 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"Jacobi Newmatrix SYCLESFC"))));
474 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
475 labelList->set(
"Timer Label", label);
476 if (!params.is_null()) labelList->set(
"compute global constants", params->get(
"compute global constants",
true));
479 if (!C.isFillComplete()) {
480 RCP<const Export<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > dummyExport;
481 C.expertStaticFillComplete(Bview.origMatrix->getDomainMap(), Aview.origMatrix->getRangeMap(), Cimport, dummyExport, labelList);
486template <
class Scalar,
489 class LocalOrdinalViewType>
490void KernelWrappers2<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType>::jacobi_A_B_reuse_kernel_wrapper(
typename Teuchos::ScalarTraits<Scalar>::magnitudeType omega,
491 const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Dinv,
492 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
493 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
494 const LocalOrdinalViewType& targetMapToOrigRow_dev,
495 const LocalOrdinalViewType& targetMapToImportRow_dev,
496 const LocalOrdinalViewType& Bcol2Ccol_dev,
497 const LocalOrdinalViewType& Icol2Ccol_dev,
498 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
499 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
500 const std::string& label,
501 const Teuchos::RCP<Teuchos::ParameterList>& params) {
503 typedef Tpetra::KokkosCompat::KokkosSYCLWrapperNode Node;
505#ifdef HAVE_TPETRA_MMM_TIMINGS
506 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
507 using Teuchos::TimeMonitor;
508 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"Jacobi Reuse SYCLCore"))));
509 Teuchos::RCP<Teuchos::TimeMonitor> MM2;
515 typedef typename Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_matrix_host_type KCRS;
516 typedef typename KCRS::StaticCrsGraphType graph_t;
517 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
518 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
519 typedef typename KCRS::values_type::non_const_type scalar_view_t;
520 typedef typename scalar_view_t::memory_space scalar_memory_space;
523 typedef LocalOrdinal LO;
524 typedef GlobalOrdinal GO;
526 typedef Map<LO, GO, NO> map_type;
527 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
528 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
529 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
537 auto targetMapToOrigRow =
538 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
539 targetMapToOrigRow_dev);
540 auto targetMapToImportRow =
541 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
542 targetMapToImportRow_dev);
544 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
547 Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
551 RCP<const map_type> Ccolmap = C.getColMap();
552 size_t m = Aview.origMatrix->getLocalNumRows();
553 size_t n = Ccolmap->getLocalNumElements();
556 const KCRS Amat = Aview.origMatrix->getLocalMatrixHost();
557 const KCRS Bmat = Bview.origMatrix->getLocalMatrixHost();
558 const KCRS Cmat = C.getLocalMatrixHost();
560 c_lno_view_t Arowptr = Amat.graph.row_map, Browptr = Bmat.graph.row_map, Crowptr = Cmat.graph.row_map;
561 const lno_nnz_view_t Acolind = Amat.graph.entries, Bcolind = Bmat.graph.entries, Ccolind = Cmat.graph.entries;
562 const scalar_view_t Avals = Amat.values, Bvals = Bmat.values;
563 scalar_view_t Cvals = Cmat.values;
565 c_lno_view_t Irowptr;
566 lno_nnz_view_t Icolind;
568 if (!Bview.importMatrix.is_null()) {
569 auto lclB = Bview.importMatrix->getLocalMatrixHost();
570 Irowptr = lclB.graph.row_map;
571 Icolind = lclB.graph.entries;
577 Dinv.template getLocalView<scalar_memory_space>(Access::ReadOnly);
579#ifdef HAVE_TPETRA_MMM_TIMINGS
581 MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"Jacobi Reuse SYCLCore - Compare"))));
589 std::vector<size_t> c_status(n, ST_INVALID);
592 size_t CSR_ip = 0, OLD_ip = 0;
593 for (
size_t i = 0; i < m; i++) {
597 CSR_ip = Crowptr[i + 1];
598 for (
size_t k = OLD_ip; k < CSR_ip; k++) {
599 c_status[Ccolind[k]] = k;
605 SC minusOmegaDval = -omega * Dvals(i, 0);
608 for (
size_t j = Browptr[i]; j < Browptr[i + 1]; j++) {
609 Scalar Bval = Bvals[j];
613 LO Cij = Bcol2Ccol[Bij];
615 TEUCHOS_TEST_FOR_EXCEPTION(c_status[Cij] < OLD_ip || c_status[Cij] >= CSR_ip,
616 std::runtime_error,
"Trying to insert a new entry into a static graph");
618 Cvals[c_status[Cij]] = Bvals[j];
622 for (
size_t k = Arowptr[i]; k < Arowptr[i + 1]; k++) {
624 const SC Aval = Avals[k];
628 if (targetMapToOrigRow[Aik] != LO_INVALID) {
630 size_t Bk = Teuchos::as<size_t>(targetMapToOrigRow[Aik]);
632 for (
size_t j = Browptr[Bk]; j < Browptr[Bk + 1]; ++j) {
634 LO Cij = Bcol2Ccol[Bkj];
636 TEUCHOS_TEST_FOR_EXCEPTION(c_status[Cij] < OLD_ip || c_status[Cij] >= CSR_ip,
637 std::runtime_error,
"Trying to insert a new entry into a static graph");
639 Cvals[c_status[Cij]] += minusOmegaDval * Aval * Bvals[j];
644 size_t Ik = Teuchos::as<size_t>(targetMapToImportRow[Aik]);
645 for (
size_t j = Irowptr[Ik]; j < Irowptr[Ik + 1]; ++j) {
647 LO Cij = Icol2Ccol[Ikj];
649 TEUCHOS_TEST_FOR_EXCEPTION(c_status[Cij] < OLD_ip || c_status[Cij] >= CSR_ip,
650 std::runtime_error,
"Trying to insert a new entry into a static graph");
652 Cvals[c_status[Cij]] += minusOmegaDval * Aval * Ivals[j];
658#ifdef HAVE_TPETRA_MMM_TIMINGS
661 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"Jacobi Reuse ESFC"))));
664 C.fillComplete(C.getDomainMap(), C.getRangeMap());
668template <
class Scalar,
671 class LocalOrdinalViewType>
672void KernelWrappers2<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode, LocalOrdinalViewType>::jacobi_A_B_newmatrix_KokkosKernels(
typename Teuchos::ScalarTraits<Scalar>::magnitudeType omega,
673 const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Dinv,
674 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Aview,
675 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& Bview,
676 const LocalOrdinalViewType& Acol2Brow,
677 const LocalOrdinalViewType& Acol2Irow,
678 const LocalOrdinalViewType& Bcol2Ccol,
679 const LocalOrdinalViewType& Icol2Ccol,
680 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode>& C,
681 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > Cimport,
682 const std::string& label,
683 const Teuchos::RCP<Teuchos::ParameterList>& params) {
684#ifdef HAVE_TPETRA_MMM_TIMINGS
685 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
686 using Teuchos::TimeMonitor;
687 Teuchos::RCP<TimeMonitor> MM;
693 auto rowMap = Aview.origMatrix->getRowMap();
695 Aview.origMatrix->getLocalDiagCopy(diags);
696 size_t diagLength = rowMap->getLocalNumElements();
697 Teuchos::Array<Scalar> diagonal(diagLength);
698 diags.get1dCopy(diagonal());
700 for (
size_t i = 0; i < diagLength; ++i) {
701 TEUCHOS_TEST_FOR_EXCEPTION(diagonal[i] == Teuchos::ScalarTraits<Scalar>::zero(),
703 "Matrix A has a zero/missing diagonal: " << diagonal[i] << std::endl
704 <<
"KokkosKernels Jacobi-fused SpGEMM requires nonzero diagonal entries in A" << std::endl);
709 using device_t =
typename Tpetra::KokkosCompat::KokkosSYCLWrapperNode::device_type;
711 using graph_t =
typename matrix_t::StaticCrsGraphType;
712 using lno_view_t =
typename graph_t::row_map_type::non_const_type;
713 using int_view_t = Kokkos::View<
int*,
714 typename lno_view_t::array_layout,
715 typename lno_view_t::memory_space,
716 typename lno_view_t::memory_traits>;
717 using c_lno_view_t =
typename graph_t::row_map_type::const_type;
718 using lno_nnz_view_t =
typename graph_t::entries_type::non_const_type;
719 using scalar_view_t =
typename matrix_t::values_type::non_const_type;
722 using handle_t =
typename KokkosKernels::Experimental::KokkosKernelsHandle<
723 typename lno_view_t::const_value_type,
typename lno_nnz_view_t::const_value_type,
typename scalar_view_t::const_value_type,
724 typename device_t::execution_space,
typename device_t::memory_space,
typename device_t::memory_space>;
725 using int_handle_t =
typename KokkosKernels::Experimental::KokkosKernelsHandle<
726 typename int_view_t::const_value_type,
typename lno_nnz_view_t::const_value_type,
typename scalar_view_t::const_value_type,
727 typename device_t::execution_space,
typename device_t::memory_space,
typename device_t::memory_space>;
730 const matrix_t Bmerged = Tpetra::MMdetails::merge_matrices(Aview, Bview, Acol2Brow, Acol2Irow, Bcol2Ccol, Icol2Ccol, C.getColMap()->getLocalNumElements());
733 const matrix_t Amat = Aview.origMatrix->getLocalMatrixDevice();
734 const matrix_t Bmat = Bview.origMatrix->getLocalMatrixDevice();
736 typename handle_t::nnz_lno_t AnumRows = Amat.numRows();
737 typename handle_t::nnz_lno_t BnumRows = Bmerged.numRows();
738 typename handle_t::nnz_lno_t BnumCols = Bmerged.numCols();
741 lno_view_t row_mapC(Kokkos::ViewAllocateWithoutInitializing(
"row_mapC"), AnumRows + 1);
742 lno_nnz_view_t entriesC;
743 scalar_view_t valuesC;
746 int team_work_size = 16;
747 std::string myalg(
"SPGEMM_KK_MEMORY");
748 if (!params.is_null()) {
749 if (params->isParameter(
"sycl: algorithm"))
750 myalg = params->get(
"sycl: algorithm", myalg);
751 if (params->isParameter(
"sycl: team work size"))
752 team_work_size = params->get(
"sycl: team work size", team_work_size);
756 std::string nodename(
"SYCL");
757 std::string alg = nodename + std::string(
" algorithm");
758 if (!params.is_null() && params->isParameter(alg)) myalg = params->get(alg, myalg);
759 KokkosSparse::SPGEMMAlgorithm alg_enum = KokkosSparse::StringToSPGEMMAlgorithm(myalg);
763 const bool useIntRowptrs =
764 irph.shouldUseIntRowptrs() &&
765 Aview.
origMatrix->getApplyHelper()->shouldUseIntRowptrs();
767 const Scalar jacobiOmega = omega * Teuchos::ScalarTraits<Scalar>::one();
771 kh.create_spgemm_handle(alg_enum);
772 kh.set_team_work_size(team_work_size);
774 int_view_t int_row_mapC(Kokkos::ViewAllocateWithoutInitializing(
"int_row_mapC"), AnumRows + 1);
776 auto Aint = Aview.origMatrix->getApplyHelper()->getIntRowptrMatrix(Amat);
777 auto Bint = irph.getIntRowptrMatrix(Bmerged);
779 KokkosSparse::spgemm_symbolic(&kh, AnumRows, BnumRows, BnumCols,
780 Aint.graph.row_map, Aint.graph.entries,
false,
781 Bint.graph.row_map, Bint.graph.entries,
false,
784 size_t c_nnz_size = kh.get_spgemm_handle()->get_c_nnz();
786 entriesC = lno_nnz_view_t(Kokkos::ViewAllocateWithoutInitializing(
"entriesC"), c_nnz_size);
787 valuesC = scalar_view_t(Kokkos::ViewAllocateWithoutInitializing(
"valuesC"), c_nnz_size);
792 KokkosSparse::Experimental::spgemm_jacobi(&kh, AnumRows, BnumRows, BnumCols,
793 Aint.graph.row_map, Aint.graph.entries, Amat.values,
false,
794 Bint.graph.row_map, Bint.graph.entries, Bint.values,
false,
795 int_row_mapC, entriesC, valuesC,
796 jacobiOmega, Dinv.getLocalViewDevice(Access::ReadOnly));
798 Kokkos::parallel_for(
799 int_row_mapC.size(), KOKKOS_LAMBDA(
int i) { row_mapC(i) = int_row_mapC(i); });
800 kh.destroy_spgemm_handle();
803 kh.create_spgemm_handle(alg_enum);
804 kh.set_team_work_size(team_work_size);
806 KokkosSparse::spgemm_symbolic(&kh, AnumRows, BnumRows, BnumCols,
807 Amat.graph.row_map, Amat.graph.entries,
false,
808 Bmerged.graph.row_map, Bmerged.graph.entries,
false,
811 size_t c_nnz_size = kh.get_spgemm_handle()->get_c_nnz();
813 entriesC = lno_nnz_view_t(Kokkos::ViewAllocateWithoutInitializing(
"entriesC"), c_nnz_size);
814 valuesC = scalar_view_t(Kokkos::ViewAllocateWithoutInitializing(
"valuesC"), c_nnz_size);
816 KokkosSparse::Experimental::spgemm_jacobi(&kh, AnumRows, BnumRows, BnumCols,
817 Amat.graph.row_map, Amat.graph.entries, Amat.values,
false,
818 Bmerged.graph.row_map, Bmerged.graph.entries, Bmerged.values,
false,
819 row_mapC, entriesC, valuesC,
820 jacobiOmega, Dinv.getLocalViewDevice(Access::ReadOnly));
821 kh.destroy_spgemm_handle();
824#ifdef HAVE_TPETRA_MMM_TIMINGS
826 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"Jacobi Newmatrix SYCLSort"))));
830 if (params.is_null() || params->get(
"sort entries",
true))
831 Import_Util::sortCrsEntries(row_mapC, entriesC, valuesC);
832 C.setAllValues(row_mapC, entriesC, valuesC);
834#ifdef HAVE_TPETRA_MMM_TIMINGS
836 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"Jacobi Newmatrix SYCLESFC"))));
840 Teuchos::RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
841 labelList->set(
"Timer Label", label);
842 if (!params.is_null()) labelList->set(
"compute global constants", params->get(
"compute global constants",
true));
843 Teuchos::RCP<const Export<LocalOrdinal, GlobalOrdinal, Tpetra::KokkosCompat::KokkosSYCLWrapperNode> > dummyExport;
844 C.expertStaticFillComplete(Bview.origMatrix->getDomainMap(), Aview.origMatrix->getRangeMap(), Cimport, dummyExport, labelList);
KokkosSparse::CrsMatrix< impl_scalar_type, local_ordinal_type, device_type, void, typename local_graph_device_type::size_type > local_matrix_device_type
The specialization of Kokkos::CrsMatrix that represents the part of the sparse matrix on each MPI pro...
Struct that holds views of the contents of a CrsMatrix.
Teuchos::RCP< const CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > origMatrix
The original matrix.
static bool debug()
Whether Tpetra is in debug mode.
Namespace Tpetra contains the class and methods constituting the Tpetra library.