10 #ifndef TPETRA_TRIPLEMATRIXMULTIPLY_DEF_HPP
11 #define TPETRA_TRIPLEMATRIXMULTIPLY_DEF_HPP
14 #include "TpetraExt_MatrixMatrix_ExtraKernels_decl.hpp"
15 #include "Teuchos_VerboseObject.hpp"
16 #include "Teuchos_Array.hpp"
18 #include "Tpetra_ConfigDefs.hpp"
19 #include "Tpetra_CrsMatrix.hpp"
21 #include "Tpetra_RowMatrixTransposer.hpp"
22 #include "Tpetra_ConfigDefs.hpp"
23 #include "Tpetra_Map.hpp"
24 #include "Tpetra_Export.hpp"
29 #include "Teuchos_FancyOStream.hpp"
43 #include "TpetraExt_MatrixMatrix_OpenMP.hpp"
44 #include "TpetraExt_MatrixMatrix_Cuda.hpp"
45 #include "TpetraExt_MatrixMatrix_HIP.hpp"
46 #include "TpetraExt_MatrixMatrix_SYCL.hpp"
50 namespace TripleMatrixMultiply{
58 template <
class Scalar,
70 bool call_FillComplete_on_result,
71 const std::string& label,
72 const Teuchos::RCP<Teuchos::ParameterList>& params)
77 typedef LocalOrdinal LO;
78 typedef GlobalOrdinal GO;
87 #ifdef HAVE_TPETRA_MMM_TIMINGS
88 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
89 using Teuchos::TimeMonitor;
90 RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP All Setup"))));
93 const std::string prefix =
"TpetraExt::TripleMatrixMultiply::MultiplyRAP(): ";
98 TEUCHOS_TEST_FOR_EXCEPTION(!R.
isFillComplete(), std::runtime_error, prefix <<
"Matrix R is not fill complete.");
99 TEUCHOS_TEST_FOR_EXCEPTION(!A.
isFillComplete(), std::runtime_error, prefix <<
"Matrix A is not fill complete.");
100 TEUCHOS_TEST_FOR_EXCEPTION(!P.
isFillComplete(), std::runtime_error, prefix <<
"Matrix P is not fill complete.");
105 RCP<const crs_matrix_type> Rprime = null;
109 RCP<const crs_matrix_type> Aprime = null;
113 RCP<const crs_matrix_type> Pprime = null;
123 const bool newFlag = !Ac.
getGraph()->isLocallyIndexed() && !Ac.
getGraph()->isGloballyIndexed();
125 using Teuchos::ParameterList;
126 RCP<ParameterList> transposeParams (
new ParameterList);
127 transposeParams->set (
"sort",
false);
129 if (transposeR && &R != &P) {
130 transposer_type transposer(rcpFromRef (R));
131 Rprime = transposer.createTranspose (transposeParams);
133 Rprime = rcpFromRef(R);
137 transposer_type transposer(rcpFromRef (A));
138 Aprime = transposer.createTranspose (transposeParams);
140 Aprime = rcpFromRef(A);
144 transposer_type transposer(rcpFromRef (P));
145 Pprime = transposer.createTranspose (transposeParams);
147 Pprime = rcpFromRef(P);
160 TEUCHOS_TEST_FOR_EXCEPTION(Rright != Aleft, std::runtime_error,
161 prefix <<
"ERROR, inner dimensions of op(R) and op(A) "
162 "must match for matrix-matrix product. op(R) is "
163 << Rleft <<
"x" << Rright <<
", op(A) is "<< Aleft <<
"x" << Aright);
165 TEUCHOS_TEST_FOR_EXCEPTION(Aright != Pleft, std::runtime_error,
166 prefix <<
"ERROR, inner dimensions of op(A) and op(P) "
167 "must match for matrix-matrix product. op(A) is "
168 << Aleft <<
"x" << Aright <<
", op(P) is "<< Pleft <<
"x" << Pright);
174 TEUCHOS_TEST_FOR_EXCEPTION(Rleft > Ac.
getGlobalNumRows(), std::runtime_error,
175 prefix <<
"ERROR, dimensions of result Ac must "
176 "match dimensions of op(R) * op(A) * op(P). Ac has " << Ac.
getGlobalNumRows()
177 <<
" rows, should have at least " << Rleft << std::endl);
189 int numProcs = P.
getComm()->getSize();
193 crs_matrix_struct_type Rview;
194 crs_matrix_struct_type Aview;
195 crs_matrix_struct_type Pview;
197 RCP<const map_type> targetMap_R = Rprime->getRowMap();
198 RCP<const map_type> targetMap_A = Aprime->getRowMap();
199 RCP<const map_type> targetMap_P = Pprime->getRowMap();
201 #ifdef HAVE_TPETRA_MMM_TIMINGS
202 MM = Teuchos::null; MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP All I&X"))));
208 RCP<const import_type> dummyImporter;
210 if (!(transposeR && &R == &P))
211 MMdetails::import_and_extract_views(*Rprime, targetMap_R, Rview, dummyImporter,
true, label, params);
213 MMdetails::import_and_extract_views(*Aprime, targetMap_A, Aview, dummyImporter,
true, label, params);
218 targetMap_P = Aprime->getColMap();
221 MMdetails::import_and_extract_views(*Pprime, targetMap_P, Pview, Aprime->getGraph()->getImporter(),
false, label, params);
224 RCP<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Actemp;
226 bool needs_final_export = !Pprime->getGraph()->getImporter().is_null();
227 if (needs_final_export)
230 Actemp = rcp(&Ac,
false);
232 #ifdef HAVE_TPETRA_MMM_TIMINGS
233 MM = Teuchos::null; MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP All Multiply"))));
237 if (call_FillComplete_on_result && newFlag) {
238 if (transposeR && &R == &P)
239 MMdetails::mult_PT_A_P_newmatrix(Aview, Pview, *Actemp, label, params);
241 MMdetails::mult_R_A_P_newmatrix(Rview, Aview, Pview, *Actemp, label, params);
242 }
else if (call_FillComplete_on_result) {
243 if (transposeR && &R == &P)
244 MMdetails::mult_PT_A_P_reuse(Aview, Pview, *Actemp, label, params);
246 MMdetails::mult_R_A_P_reuse(Rview, Aview, Pview, *Actemp, label, params);
269 if (transposeR && &R == &P)
270 MMdetails::mult_PT_A_P_newmatrix(Aview, Pview, *Actemp, label, params);
272 MMdetails::mult_R_A_P_newmatrix(Rview, Aview, Pview, *Actemp, label, params);
275 if (needs_final_export) {
276 #ifdef HAVE_TPETRA_MMM_TIMINGS
277 MM = Teuchos::null; MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP exportAndFillComplete"))));
279 Teuchos::ParameterList labelList;
280 labelList.set(
"Timer Label", label);
281 Teuchos::ParameterList& labelList_subList = labelList.sublist(
"matrixmatrix: kernel params",
false);
283 RCP<crs_matrix_type> Acprime = rcpFromRef(Ac);
285 bool overrideAllreduce =
false;
287 if(!params.is_null()) {
288 Teuchos::ParameterList& params_sublist = params->sublist(
"matrixmatrix: kernel params",
false);
290 mm_optimization_core_count = params_sublist.get(
"MM_TAFC_OptimizationCoreCount",mm_optimization_core_count);
291 int mm_optimization_core_count2 = params->get(
"MM_TAFC_OptimizationCoreCount",mm_optimization_core_count);
292 if(mm_optimization_core_count2<mm_optimization_core_count) mm_optimization_core_count=mm_optimization_core_count2;
293 isMM = params_sublist.get(
"isMatrixMatrix_TransferAndFillComplete",
false);
294 overrideAllreduce = params_sublist.get(
"MM_TAFC_OverrideAllreduceCheck",
false);
296 labelList.set(
"compute global constants",params->get(
"compute global constants",
true));
298 labelList_subList.set(
"MM_TAFC_OptimizationCoreCount",mm_optimization_core_count,
"Core Count above which the optimized neighbor discovery is used");
300 labelList_subList.set(
"isMatrixMatrix_TransferAndFillComplete",isMM,
301 "This parameter should be set to true only for MatrixMatrix operations: the optimization in Epetra that was ported to Tpetra does _not_ take into account the possibility that for any given source PID, a particular GID may not exist on the target PID: i.e. a transfer operation. A fix for this general case is in development.");
302 labelList_subList.set(
"MM_TAFC_OverrideAllreduceCheck",overrideAllreduce);
304 export_type exporter = export_type(*Pprime->getGraph()->getImporter());
305 Actemp->exportAndFillComplete(Acprime,
307 Acprime->getDomainMap(),
308 Acprime->getRangeMap(),
309 rcp(&labelList,
false));
312 #ifdef HAVE_TPETRA_MMM_STATISTICS
313 printMultiplicationStatistics(Actemp->getGraph()->getExporter(), label+std::string(
" RAP MMM"));
324 template<
class Scalar,
328 void mult_R_A_P_newmatrix(
329 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
330 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
331 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
332 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
333 const std::string& label,
334 const Teuchos::RCP<Teuchos::ParameterList>& params)
336 using Teuchos::Array;
337 using Teuchos::ArrayRCP;
338 using Teuchos::ArrayView;
343 typedef LocalOrdinal LO;
344 typedef GlobalOrdinal GO;
347 typedef Import<LO,GO,NO> import_type;
348 typedef Map<LO,GO,NO> map_type;
351 typedef typename map_type::local_map_type local_map_type;
353 typedef typename KCRS::StaticCrsGraphType graph_t;
354 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
355 typedef typename NO::execution_space execution_space;
356 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
357 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
359 #ifdef HAVE_TPETRA_MMM_TIMINGS
360 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
361 using Teuchos::TimeMonitor;
362 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
364 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
367 RCP<const import_type> Cimport;
368 RCP<const map_type> Ccolmap;
369 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
370 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
371 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
372 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
373 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
374 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
375 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
383 lo_view_t Pcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Pcol2Ccol"),Pview.colMap->getLocalNumElements()), Icol2Ccol;
385 if (Pview.importMatrix.is_null()) {
388 Ccolmap = Pview.colMap;
389 const LO colMapSize =
static_cast<LO
>(Pview.colMap->getLocalNumElements());
391 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_fill",
392 Kokkos::RangePolicy<execution_space, LO>(0, colMapSize),
393 KOKKOS_LAMBDA(
const LO i) {
406 if (!Pimport.is_null() && !Iimport.is_null()) {
407 Cimport = Pimport->setUnion(*Iimport);
409 else if (!Pimport.is_null() && Iimport.is_null()) {
410 Cimport = Pimport->setUnion();
412 else if (Pimport.is_null() && !Iimport.is_null()) {
413 Cimport = Iimport->setUnion();
416 throw std::runtime_error(
"TpetraExt::RAP status of matrix importers is nonsensical");
418 Ccolmap = Cimport->getTargetMap();
423 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
424 std::runtime_error,
"Tpetra::RAP: Import setUnion messed with the DomainMap in an unfortunate way");
431 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getLocalNumElements());
432 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
433 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_getGlobalElement",range_type(0,Pview.origMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
434 Pcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
436 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Icol2Ccol_getGlobalElement",range_type(0,Pview.importMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
437 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
446 Ac.replaceColMap(Ccolmap);
464 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getLocalNumElements());
465 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getLocalNumElements());
466 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::construct_tables",range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(
const LO i) {
467 GO aidx = Acolmap_local.getGlobalElement(i);
468 LO P_LID = Prowmap_local.getLocalElement(aidx);
469 if (P_LID != LO_INVALID) {
470 targetMapToOrigRow(i) = P_LID;
471 targetMapToImportRow(i) = LO_INVALID;
473 LO I_LID = Irowmap_local.getLocalElement(aidx);
474 targetMapToOrigRow(i) = LO_INVALID;
475 targetMapToImportRow(i) = I_LID;
481 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
482 mult_R_A_P_newmatrix_kernel_wrapper(Rview, Aview, Pview,
483 targetMapToOrigRow,targetMapToImportRow, Pcol2Ccol, Icol2Ccol,
484 Ac, Cimport, label, params);
489 template<
class Scalar,
493 void mult_R_A_P_reuse(
494 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
495 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
496 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
497 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
498 const std::string& label,
499 const Teuchos::RCP<Teuchos::ParameterList>& params)
501 using Teuchos::Array;
502 using Teuchos::ArrayRCP;
503 using Teuchos::ArrayView;
508 typedef LocalOrdinal LO;
509 typedef GlobalOrdinal GO;
512 typedef Import<LO,GO,NO> import_type;
513 typedef Map<LO,GO,NO> map_type;
516 typedef typename map_type::local_map_type local_map_type;
518 typedef typename KCRS::StaticCrsGraphType graph_t;
519 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
520 typedef typename NO::execution_space execution_space;
521 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
522 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
524 #ifdef HAVE_TPETRA_MMM_TIMINGS
525 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
526 using Teuchos::TimeMonitor;
527 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
529 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
532 RCP<const import_type> Cimport = Ac.getGraph()->getImporter();
533 RCP<const map_type> Ccolmap = Ac.getColMap();
534 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
535 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
536 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
537 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
538 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
539 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
540 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
541 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
544 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Bcol2Ccol"),Pview.colMap->getLocalNumElements()), Icol2Ccol;
548 Kokkos::parallel_for(range_type(0,Pview.origMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
549 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
552 if (!Pview.importMatrix.is_null()) {
553 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
554 std::runtime_error,
"Tpetra::MMM: Import setUnion messed with the DomainMap in an unfortunate way");
556 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getLocalNumElements());
557 Kokkos::parallel_for(range_type(0,Pview.importMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
558 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
564 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getLocalNumElements());
565 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getLocalNumElements());
566 Kokkos::parallel_for(range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(
const LO i) {
567 GO aidx = Acolmap_local.getGlobalElement(i);
568 LO B_LID = Prowmap_local.getLocalElement(aidx);
569 if (B_LID != LO_INVALID) {
570 targetMapToOrigRow(i) = B_LID;
571 targetMapToImportRow(i) = LO_INVALID;
573 LO I_LID = Irowmap_local.getLocalElement(aidx);
574 targetMapToOrigRow(i) = LO_INVALID;
575 targetMapToImportRow(i) = I_LID;
582 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
583 mult_R_A_P_reuse_kernel_wrapper(Rview, Aview, Pview,
584 targetMapToOrigRow,targetMapToImportRow, Bcol2Ccol, Icol2Ccol,
585 Ac, Cimport, label, params);
590 template<
class Scalar,
594 void mult_PT_A_P_newmatrix(
595 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
596 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
597 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
598 const std::string& label,
599 const Teuchos::RCP<Teuchos::ParameterList>& params)
601 using Teuchos::Array;
602 using Teuchos::ArrayRCP;
603 using Teuchos::ArrayView;
608 typedef LocalOrdinal LO;
609 typedef GlobalOrdinal GO;
612 typedef Import<LO,GO,NO> import_type;
613 typedef Map<LO,GO,NO> map_type;
616 typedef typename map_type::local_map_type local_map_type;
618 typedef typename KCRS::StaticCrsGraphType graph_t;
619 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
620 typedef typename NO::execution_space execution_space;
621 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
622 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
624 #ifdef HAVE_TPETRA_MMM_TIMINGS
625 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
626 using Teuchos::TimeMonitor;
627 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP M5 Cmap")))));
629 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
632 RCP<const import_type> Cimport;
633 RCP<const map_type> Ccolmap;
634 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
635 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
636 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
637 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
638 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
639 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
640 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
648 lo_view_t Pcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Pcol2Ccol"),Pview.colMap->getLocalNumElements()), Icol2Ccol;
650 if (Pview.importMatrix.is_null()) {
653 Ccolmap = Pview.colMap;
654 const LO colMapSize =
static_cast<LO
>(Pview.colMap->getLocalNumElements());
656 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_fill",
657 Kokkos::RangePolicy<execution_space, LO>(0, colMapSize),
658 KOKKOS_LAMBDA(
const LO i) {
671 if (!Pimport.is_null() && !Iimport.is_null()) {
672 Cimport = Pimport->setUnion(*Iimport);
674 else if (!Pimport.is_null() && Iimport.is_null()) {
675 Cimport = Pimport->setUnion();
677 else if (Pimport.is_null() && !Iimport.is_null()) {
678 Cimport = Iimport->setUnion();
681 throw std::runtime_error(
"TpetraExt::RAP status of matrix importers is nonsensical");
683 Ccolmap = Cimport->getTargetMap();
688 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
689 std::runtime_error,
"Tpetra::RAP: Import setUnion messed with the DomainMap in an unfortunate way");
696 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getLocalNumElements());
697 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
698 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_getGlobalElement",range_type(0,Pview.origMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
699 Pcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
701 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Icol2Ccol_getGlobalElement",range_type(0,Pview.importMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
702 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
711 Ac.replaceColMap(Ccolmap);
729 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getLocalNumElements());
730 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getLocalNumElements());
732 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::construct_tables",range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(
const LO i) {
733 GO aidx = Acolmap_local.getGlobalElement(i);
734 LO P_LID = Prowmap_local.getLocalElement(aidx);
735 if (P_LID != LO_INVALID) {
736 targetMapToOrigRow(i) = P_LID;
737 targetMapToImportRow(i) = LO_INVALID;
739 LO I_LID = Irowmap_local.getLocalElement(aidx);
740 targetMapToOrigRow(i) = LO_INVALID;
741 targetMapToImportRow(i) = I_LID;
747 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
748 mult_PT_A_P_newmatrix_kernel_wrapper(Aview, Pview,
749 targetMapToOrigRow,targetMapToImportRow, Pcol2Ccol, Icol2Ccol,
750 Ac, Cimport, label, params);
754 template<
class Scalar,
758 void mult_PT_A_P_reuse(
759 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
760 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
761 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
762 const std::string& label,
763 const Teuchos::RCP<Teuchos::ParameterList>& params)
765 using Teuchos::Array;
766 using Teuchos::ArrayRCP;
767 using Teuchos::ArrayView;
772 typedef LocalOrdinal LO;
773 typedef GlobalOrdinal GO;
776 typedef Import<LO,GO,NO> import_type;
777 typedef Map<LO,GO,NO> map_type;
780 typedef typename map_type::local_map_type local_map_type;
782 typedef typename KCRS::StaticCrsGraphType graph_t;
783 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
784 typedef typename NO::execution_space execution_space;
785 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
786 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
788 #ifdef HAVE_TPETRA_MMM_TIMINGS
789 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
790 using Teuchos::TimeMonitor;
791 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
793 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
796 RCP<const import_type> Cimport = Ac.getGraph()->getImporter();
797 RCP<const map_type> Ccolmap = Ac.getColMap();
798 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
799 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
800 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
801 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
802 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
803 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
804 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
805 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
808 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Bcol2Ccol"),Pview.colMap->getLocalNumElements()), Icol2Ccol;
812 Kokkos::parallel_for(range_type(0,Pview.origMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
813 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
816 if (!Pview.importMatrix.is_null()) {
817 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
818 std::runtime_error,
"Tpetra::MMM: Import setUnion messed with the DomainMap in an unfortunate way");
820 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getLocalNumElements());
821 Kokkos::parallel_for(range_type(0,Pview.importMatrix->getColMap()->getLocalNumElements()),KOKKOS_LAMBDA(
const LO i) {
822 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
828 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getLocalNumElements());
829 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getLocalNumElements());
830 Kokkos::parallel_for(range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(
const LO i) {
831 GO aidx = Acolmap_local.getGlobalElement(i);
832 LO B_LID = Prowmap_local.getLocalElement(aidx);
833 if (B_LID != LO_INVALID) {
834 targetMapToOrigRow(i) = B_LID;
835 targetMapToImportRow(i) = LO_INVALID;
837 LO I_LID = Irowmap_local.getLocalElement(aidx);
838 targetMapToOrigRow(i) = LO_INVALID;
839 targetMapToImportRow(i) = I_LID;
846 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
847 mult_PT_A_P_reuse_kernel_wrapper(Aview, Pview,
848 targetMapToOrigRow,targetMapToImportRow, Bcol2Ccol, Icol2Ccol,
849 Ac, Cimport, label, params);
856 template<
class Scalar,
860 class LocalOrdinalViewType>
861 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_R_A_P_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
862 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
863 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
864 const LocalOrdinalViewType & Acol2Prow_dev,
865 const LocalOrdinalViewType & Acol2PIrow_dev,
866 const LocalOrdinalViewType & Pcol2Accol_dev,
867 const LocalOrdinalViewType & PIcol2Accol_dev,
868 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
869 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
870 const std::string& label,
871 const Teuchos::RCP<Teuchos::ParameterList>& params) {
872 #ifdef HAVE_TPETRA_MMM_TIMINGS
873 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
874 using Teuchos::TimeMonitor;
875 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix SerialCore"))));
878 using Teuchos::Array;
879 using Teuchos::ArrayRCP;
880 using Teuchos::ArrayView;
886 typedef typename KCRS::StaticCrsGraphType graph_t;
887 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
888 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
889 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
890 typedef typename KCRS::values_type::non_const_type scalar_view_t;
893 typedef LocalOrdinal LO;
894 typedef GlobalOrdinal GO;
896 typedef Map<LO,GO,NO> map_type;
897 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
898 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
899 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
902 RCP<const map_type> Accolmap = Ac.getColMap();
903 size_t m = Rview.origMatrix->getLocalNumRows();
904 size_t n = Accolmap->getLocalNumElements();
905 size_t p_max_nnz_per_row = Pview.origMatrix->getLocalMaxNumRowEntries();
908 auto Acol2Prow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
910 auto Acol2PIrow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
912 auto Pcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
914 auto PIcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
918 const auto Amat = Aview.origMatrix->getLocalMatrixHost();
919 const auto Pmat = Pview.origMatrix->getLocalMatrixHost();
920 const auto Rmat = Rview.origMatrix->getLocalMatrixHost();
922 auto Arowptr = Amat.graph.row_map;
923 auto Prowptr = Pmat.graph.row_map;
924 auto Rrowptr = Rmat.graph.row_map;
925 const auto Acolind = Amat.graph.entries;
926 const auto Pcolind = Pmat.graph.entries;
927 const auto Rcolind = Rmat.graph.entries;
928 const auto Avals = Amat.values;
929 const auto Pvals = Pmat.values;
930 const auto Rvals = Rmat.values;
932 typename c_lno_view_t::HostMirror::const_type Irowptr;
933 typename lno_nnz_view_t::HostMirror Icolind;
934 typename scalar_view_t::HostMirror Ivals;
935 if(!Pview.importMatrix.is_null()) {
936 auto lclP = Pview.importMatrix->getLocalMatrixHost();
937 Irowptr = lclP.graph.row_map;
938 Icolind = lclP.graph.entries;
940 p_max_nnz_per_row = std::max(p_max_nnz_per_row,Pview.importMatrix->getLocalMaxNumRowEntries());
943 #ifdef HAVE_TPETRA_MMM_TIMINGS
944 RCP<TimeMonitor> MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix SerialCore - Compare"))));
954 size_t CSR_alloc = std::max(C_estimate_nnz(*Aview.origMatrix, *Pview.origMatrix), n);
955 typename lno_view_t::HostMirror Crowptr(Kokkos::ViewAllocateWithoutInitializing(
"Crowptr"),m+1);
956 typename lno_nnz_view_t::HostMirror Ccolind(Kokkos::ViewAllocateWithoutInitializing(
"Ccolind"),CSR_alloc);
957 typename scalar_view_t::HostMirror Cvals(Kokkos::ViewAllocateWithoutInitializing(
"Cvals"),CSR_alloc);
967 const size_t INVALID = Teuchos::OrdinalTraits<size_t>::invalid();
968 Array<size_t> ac_status(n, ST_INVALID);
978 size_t nnz = 0, nnz_old = 0;
979 for (
size_t i = 0; i < m; i++) {
985 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i+1]; kk++) {
987 const SC Rik = Rvals[kk];
991 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
993 const SC Akl = Avals[ll];
998 if (Acol2Prow[l] != LO_INVALID) {
1005 size_t Pl = Teuchos::as<size_t>(Acol2Prow[l]);
1008 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1010 LO Acj = Pcol2Accol[j];
1013 if (ac_status[Acj] == INVALID || ac_status[Acj] < nnz_old) {
1014 #ifdef HAVE_TPETRA_DEBUG
1016 TEUCHOS_TEST_FOR_EXCEPTION(nnz >= Teuchos::as<size_t>(Ccolind.size()),
1018 label <<
" ERROR, not enough memory allocated for matrix product. Allocated: " << Ccolind.extent(0) << std::endl);
1021 ac_status[Acj] = nnz;
1023 Cvals[nnz] = Rik*Akl*Plj;
1026 Cvals[ac_status[Acj]] += Rik*Akl*Plj;
1036 size_t Il = Teuchos::as<size_t>(Acol2PIrow[l]);
1037 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1039 LO Acj = PIcol2Accol[j];
1042 if (ac_status[Acj] == INVALID || ac_status[Acj] < nnz_old) {
1043 #ifdef HAVE_TPETRA_DEBUG
1045 TEUCHOS_TEST_FOR_EXCEPTION(nnz >= Teuchos::as<size_t>(Ccolind.size()),
1047 label <<
" ERROR, not enough memory allocated for matrix product. Allocated: " << Ccolind.extent(0) << std::endl);
1050 ac_status[Acj] = nnz;
1052 Cvals[nnz] = Rik*Akl*Plj;
1055 Cvals[ac_status[Acj]] += Rik*Akl*Plj;
1062 if (nnz + n > CSR_alloc) {
1064 Kokkos::resize(Ccolind,CSR_alloc);
1065 Kokkos::resize(Cvals,CSR_alloc);
1073 Kokkos::resize(Ccolind,nnz);
1074 Kokkos::resize(Cvals,nnz);
1076 #ifdef HAVE_TPETRA_MMM_TIMINGS
1077 MM = Teuchos::null; MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix Final Sort"))));
1079 auto Crowptr_dev = Kokkos::create_mirror_view_and_copy(
1080 typename KCRS::device_type(), Crowptr);
1081 auto Ccolind_dev = Kokkos::create_mirror_view_and_copy(
1082 typename KCRS::device_type(), Ccolind);
1083 auto Cvals_dev = Kokkos::create_mirror_view_and_copy(
1084 typename KCRS::device_type(), Cvals);
1087 if (params.is_null() || params->get(
"sort entries",
true))
1088 Import_Util::sortCrsEntries(Crowptr_dev, Ccolind_dev, Cvals_dev);
1089 Ac.setAllValues(Crowptr_dev, Ccolind_dev, Cvals_dev);
1091 #ifdef HAVE_TPETRA_MMM_TIMINGS
1092 MM = Teuchos::null; MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix ESFC"))));
1103 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
1104 labelList->set(
"Timer Label",label);
1105 if(!params.is_null()) labelList->set(
"compute global constants",params->get(
"compute global constants",
true));
1106 RCP<const Export<LO,GO,NO> > dummyExport;
1107 Ac.expertStaticFillComplete(Pview.origMatrix->getDomainMap(),
1108 Rview.origMatrix->getRangeMap(),
1118 template<
class Scalar,
1120 class GlobalOrdinal,
1122 class LocalOrdinalViewType>
1123 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_R_A_P_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
1124 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1125 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1126 const LocalOrdinalViewType & Acol2Prow_dev,
1127 const LocalOrdinalViewType & Acol2PIrow_dev,
1128 const LocalOrdinalViewType & Pcol2Accol_dev,
1129 const LocalOrdinalViewType & PIcol2Accol_dev,
1130 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1131 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1132 const std::string& label,
1133 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1134 #ifdef HAVE_TPETRA_MMM_TIMINGS
1135 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1136 using Teuchos::TimeMonitor;
1137 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse SerialCore"))));
1140 using Teuchos::Array;
1141 using Teuchos::ArrayRCP;
1142 using Teuchos::ArrayView;
1147 typedef typename Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>::local_matrix_host_type KCRS;
1148 typedef typename KCRS::StaticCrsGraphType graph_t;
1149 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
1150 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
1151 typedef typename KCRS::values_type::non_const_type scalar_view_t;
1154 typedef LocalOrdinal LO;
1155 typedef GlobalOrdinal GO;
1157 typedef Map<LO,GO,NO> map_type;
1158 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1159 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1160 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
1163 RCP<const map_type> Accolmap = Ac.getColMap();
1164 size_t m = Rview.origMatrix->getLocalNumRows();
1165 size_t n = Accolmap->getLocalNumElements();
1166 size_t p_max_nnz_per_row = Pview.origMatrix->getLocalMaxNumRowEntries();
1169 auto Acol2Prow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1171 auto Acol2PIrow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1173 auto Pcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1175 auto PIcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1179 const KCRS & Amat = Aview.origMatrix->getLocalMatrixHost();
1180 const KCRS & Pmat = Pview.origMatrix->getLocalMatrixHost();
1181 const KCRS & Rmat = Rview.origMatrix->getLocalMatrixHost();
1182 const KCRS & Cmat = Ac.getLocalMatrixHost();
1184 c_lno_view_t Arowptr = Amat.graph.row_map, Prowptr = Pmat.graph.row_map, Rrowptr = Rmat.graph.row_map, Crowptr = Cmat.graph.row_map;
1185 const lno_nnz_view_t Acolind = Amat.graph.entries, Pcolind = Pmat.graph.entries , Rcolind = Rmat.graph.entries, Ccolind = Cmat.graph.entries;
1186 const scalar_view_t Avals = Amat.values, Pvals = Pmat.values, Rvals = Rmat.values;
1187 scalar_view_t Cvals = Cmat.values;
1189 c_lno_view_t Irowptr;
1190 lno_nnz_view_t Icolind;
1191 scalar_view_t Ivals;
1192 if(!Pview.importMatrix.is_null()) {
1193 auto lclP = Pview.importMatrix->getLocalMatrixHost();
1194 Irowptr = lclP.graph.row_map;
1195 Icolind = lclP.graph.entries;
1196 Ivals = lclP.values;
1197 p_max_nnz_per_row = std::max(p_max_nnz_per_row,Pview.importMatrix->getLocalMaxNumRowEntries());
1200 #ifdef HAVE_TPETRA_MMM_TIMINGS
1201 RCP<TimeMonitor> MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse SerialCore - Compare"))));
1212 Array<size_t> ac_status(n, ST_INVALID);
1226 size_t OLD_ip = 0, CSR_ip = 0;
1227 for (
size_t i = 0; i < m; i++) {
1230 OLD_ip = Crowptr[i];
1231 CSR_ip = Crowptr[i+1];
1232 for (
size_t k = OLD_ip; k < CSR_ip; k++) {
1233 ac_status[Ccolind[k]] = k;
1240 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i+1]; kk++) {
1242 const SC Rik = Rvals[kk];
1246 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1248 const SC Akl = Avals[ll];
1253 if (Acol2Prow[l] != LO_INVALID) {
1260 size_t Pl = Teuchos::as<size_t>(Acol2Prow[l]);
1263 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1265 LO Cij = Pcol2Accol[j];
1268 TEUCHOS_TEST_FOR_EXCEPTION(ac_status[Cij] < OLD_ip || ac_status[Cij] >= CSR_ip,
1269 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph " <<
1270 "(c_status = " << ac_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
1272 Cvals[ac_status[Cij]] += Rik*Akl*Plj;
1281 size_t Il = Teuchos::as<size_t>(Acol2PIrow[l]);
1282 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1284 LO Cij = PIcol2Accol[j];
1287 TEUCHOS_TEST_FOR_EXCEPTION(ac_status[Cij] < OLD_ip || ac_status[Cij] >= CSR_ip,
1288 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph " <<
1289 "(c_status = " << ac_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
1291 Cvals[ac_status[Cij]] += Rik*Akl*Plj;
1298 #ifdef HAVE_TPETRA_MMM_TIMINGS
1299 auto MM3 = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse ESFC"))));
1302 Ac.fillComplete(Ac.getDomainMap(), Ac.getRangeMap());
1310 template<
class Scalar,
1312 class GlobalOrdinal,
1314 class LocalOrdinalViewType>
1315 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_PT_A_P_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1316 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1317 const LocalOrdinalViewType & Acol2Prow,
1318 const LocalOrdinalViewType & Acol2PIrow,
1319 const LocalOrdinalViewType & Pcol2Accol,
1320 const LocalOrdinalViewType & PIcol2Accol,
1321 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1322 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1323 const std::string& label,
1324 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1325 #ifdef HAVE_TPETRA_MMM_TIMINGS
1326 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1327 using Teuchos::TimeMonitor;
1328 Teuchos::TimeMonitor MM(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose")));
1332 typedef RowMatrixTransposer<Scalar,LocalOrdinal,GlobalOrdinal, Node> transposer_type;
1333 transposer_type transposer (Pview.origMatrix,label+std::string(
"XP: "));
1335 using Teuchos::ParameterList;
1337 RCP<ParameterList> transposeParams (
new ParameterList);
1338 transposeParams->set (
"sort",
false);
1340 if (! params.is_null ()) {
1341 transposeParams->set (
"compute global constants",
1342 params->get (
"compute global constants: temporaries",
1345 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1346 transposer.createTransposeLocal (transposeParams);
1347 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Rview;
1348 Rview.origMatrix = Ptrans;
1350 mult_R_A_P_newmatrix_kernel_wrapper(Rview,Aview,Pview,Acol2Prow,Acol2PIrow,Pcol2Accol,PIcol2Accol,Ac,Acimport,label,params);
1356 template<
class Scalar,
1358 class GlobalOrdinal,
1360 class LocalOrdinalViewType>
1361 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_PT_A_P_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1362 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1363 const LocalOrdinalViewType & Acol2Prow,
1364 const LocalOrdinalViewType & Acol2PIrow,
1365 const LocalOrdinalViewType & Pcol2Accol,
1366 const LocalOrdinalViewType & PIcol2Accol,
1367 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1368 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1369 const std::string& label,
1370 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1371 #ifdef HAVE_TPETRA_MMM_TIMINGS
1372 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1373 using Teuchos::TimeMonitor;
1374 Teuchos::TimeMonitor MM(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose")));
1378 typedef RowMatrixTransposer<Scalar,LocalOrdinal,GlobalOrdinal, Node> transposer_type;
1379 transposer_type transposer (Pview.origMatrix,label+std::string(
"XP: "));
1381 using Teuchos::ParameterList;
1383 RCP<ParameterList> transposeParams (
new ParameterList);
1384 transposeParams->set (
"sort",
false);
1386 if (! params.is_null ()) {
1387 transposeParams->set (
"compute global constants",
1388 params->get (
"compute global constants: temporaries",
1391 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1392 transposer.createTransposeLocal (transposeParams);
1393 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Rview;
1394 Rview.origMatrix = Ptrans;
1396 mult_R_A_P_reuse_kernel_wrapper(Rview,Aview,Pview,Acol2Prow,Acol2PIrow,Pcol2Accol,PIcol2Accol,Ac,Acimport,label,params);
1404 template<
class Scalar,
1406 class GlobalOrdinal,
1408 void KernelWrappers3MMM<Scalar,LocalOrdinal,GlobalOrdinal,Node>::mult_PT_A_P_newmatrix_kernel_wrapper_2pass(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1409 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1410 const Teuchos::Array<LocalOrdinal> & Acol2PRow,
1411 const Teuchos::Array<LocalOrdinal> & Acol2PRowImport,
1412 const Teuchos::Array<LocalOrdinal> & Pcol2Accol,
1413 const Teuchos::Array<LocalOrdinal> & PIcol2Accol,
1414 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1415 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1416 const std::string& label,
1417 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1418 #ifdef HAVE_TPETRA_MMM_TIMINGS
1419 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1420 using Teuchos::TimeMonitor;
1421 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix SerialCore"))));
1424 using Teuchos::Array;
1425 using Teuchos::ArrayRCP;
1426 using Teuchos::ArrayView;
1431 typedef LocalOrdinal LO;
1432 typedef GlobalOrdinal GO;
1434 typedef RowMatrixTransposer<SC,LO,GO,NO> transposer_type;
1435 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1436 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
1441 size_t n = Ac.getRowMap()->getLocalNumElements();
1442 LO maxAccol = Ac.getColMap()->getMaxLocalIndex();
1445 ArrayRCP<size_t> Acrowptr_RCP;
1446 ArrayRCP<LO> Accolind_RCP;
1447 ArrayRCP<SC> Acvals_RCP;
1454 auto Arowptr = Aview.origMatrix->getLocalRowPtrsHost();
1455 auto Acolind = Aview.origMatrix->getLocalIndicesHost();
1456 auto Avals = Aview.origMatrix->getLocalValuesHost(
1457 Tpetra::Access::ReadOnly);
1458 auto Prowptr = Pview.origMatrix->getLocalRowPtrsHost();
1459 auto Pcolind = Pview.origMatrix->getLocalIndicesHost();
1460 auto Pvals = Pview.origMatrix->getLocalValuesHost(
1461 Tpetra::Access::ReadOnly);
1462 decltype(Prowptr) Irowptr;
1463 decltype(Pcolind) Icolind;
1464 decltype(Pvals) Ivals;
1466 if (!Pview.importMatrix.is_null()) {
1467 Irowptr = Pview.importMatrix->getLocalRowPtrsHost();
1468 Icolind = Pview.importMatrix->getLocalIndicesHost();
1469 Ivals = Pview.importMatrix->getLocalValuesHost(
1470 Tpetra::Access::ReadOnly);
1478 ArrayView<size_t> Acrowptr;
1479 ArrayView<LO> Accolind;
1480 ArrayView<SC> Acvals;
1491 #ifdef HAVE_TPETRA_MMM_TIMINGS
1492 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose"))));
1499 transposer_type transposer (Pview.origMatrix,label+std::string(
"XP: "));
1501 using Teuchos::ParameterList;
1502 RCP<ParameterList> transposeParams (
new ParameterList);
1503 transposeParams->set (
"sort",
false);
1504 if (! params.is_null ()) {
1505 transposeParams->set (
"compute global constants",
1506 params->get (
"compute global constants: temporaries",
1509 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1510 transposer.createTransposeLocal (transposeParams);
1512 auto Rrowptr = Ptrans->getLocalRowPtrsHost();
1513 auto Rcolind = Ptrans->getLocalIndicesHost();
1514 auto Rvals = Ptrans->getLocalValuesHost(Tpetra::Access::ReadOnly);
1519 #ifdef HAVE_TPETRA_MMM_TIMINGS
1520 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP graph"))));
1523 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1524 Array<size_t> ac_status(maxAccol + 1, ST_INVALID);
1526 size_t nnz_alloc = std::max(Ac_estimate_nnz(*Aview.origMatrix, *Pview.origMatrix), n);
1527 size_t nnzPerRowA = 100;
1528 if (Aview.origMatrix->getLocalNumEntries() > 0)
1529 nnzPerRowA = Aview.origMatrix->getLocalNumEntries()/Aview.origMatrix->getLocalNumRows();
1530 Acrowptr_RCP.resize(n+1);
1531 Acrowptr = Acrowptr_RCP();
1532 Accolind_RCP.resize(nnz_alloc);
1533 Accolind = Accolind_RCP();
1535 size_t nnz = 0, nnz_old = 0;
1536 for (
size_t i = 0; i < n; i++) {
1542 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i+1]; kk++) {
1545 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1548 if (Acol2PRow[l] != LO_INVALID) {
1555 size_t Pl = Teuchos::as<size_t>(Acol2PRow[l]);
1558 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1560 LO Acj = Pcol2Accol[j];
1562 if (ac_status[Acj] == ST_INVALID || ac_status[Acj] < nnz_old) {
1564 ac_status[Acj] = nnz;
1565 Accolind[nnz] = Acj;
1576 size_t Il = Teuchos::as<size_t>(Acol2PRowImport[l]);
1577 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1579 LO Acj = PIcol2Accol[j];
1581 if (ac_status[Acj] == ST_INVALID || ac_status[Acj] < nnz_old){
1583 ac_status[Acj] = nnz;
1584 Accolind[nnz] = Acj;
1594 if (nnz + std::max(5*nnzPerRowA, n) > nnz_alloc) {
1596 nnz_alloc = std::max(nnz_alloc, nnz + std::max(5*nnzPerRowA, n));
1597 Accolind_RCP.resize(nnz_alloc); Accolind = Accolind_RCP();
1598 Acvals_RCP.resize(nnz_alloc); Acvals = Acvals_RCP();
1605 Accolind_RCP.resize(nnz);
1606 Accolind = Accolind_RCP();
1609 Acvals_RCP.resize(nnz, SC_ZERO);
1610 Acvals = Acvals_RCP();
1617 #ifdef HAVE_TPETRA_MMM_TIMINGS
1618 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix Fill Matrix"))));
1622 for (
size_t k = 0; k < n; k++) {
1623 for (
size_t ii = Prowptr[k]; ii < Prowptr[k+1]; ii++) {
1625 const SC Pki = Pvals[ii];
1626 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1628 const SC Akl = Avals[ll];
1631 if (Acol2PRow[l] != LO_INVALID) {
1638 size_t Pl = Teuchos::as<size_t>(Acol2PRow[l]);
1639 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1641 LO Acj = Pcol2Accol[j];
1643 for (pp = Acrowptr[i]; pp < Acrowptr[i+1]; pp++)
1644 if (Accolind[pp] == Acj)
1648 Acvals[pp] += Pki*Akl*Pvals[jj];
1657 size_t Il = Teuchos::as<size_t>(Acol2PRowImport[l]);
1658 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1660 LO Acj = PIcol2Accol[j];
1662 for (pp = Acrowptr[i]; pp < Acrowptr[i+1]; pp++)
1663 if (Accolind[pp] == Acj)
1667 Acvals[pp] += Pki*Akl*Ivals[jj];
1675 #ifdef HAVE_TPETRA_MMM_TIMINGS
1676 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP sort"))));
1683 Import_Util::sortCrsEntries(Acrowptr_RCP(), Accolind_RCP(), Acvals_RCP());
1686 Ac.setAllValues(Acrowptr_RCP, Accolind_RCP, Acvals_RCP);
1688 #ifdef HAVE_TPETRA_MMM_TIMINGS
1689 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix ESFC"))));
1700 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
1701 labelList->set(
"Timer Label",label);
1703 if(!params.is_null()) labelList->set(
"compute global constants",params->get(
"compute global constants",
true));
1704 RCP<const Export<LO,GO,NO> > dummyExport;
1705 Ac.expertStaticFillComplete(Pview.origMatrix->getDomainMap(),
1706 Pview.origMatrix->getDomainMap(),
1708 dummyExport, labelList);
1722 #define TPETRA_TRIPLEMATRIXMULTIPLY_INSTANT(SCALAR,LO,GO,NODE) \
1725 void TripleMatrixMultiply::MultiplyRAP( \
1726 const CrsMatrix< SCALAR , LO , GO , NODE >& R, \
1728 const CrsMatrix< SCALAR , LO , GO , NODE >& A, \
1730 const CrsMatrix< SCALAR , LO , GO , NODE >& P, \
1732 CrsMatrix< SCALAR , LO , GO , NODE >& Ac, \
1733 bool call_FillComplete_on_result, \
1734 const std::string & label, \
1735 const Teuchos::RCP<Teuchos::ParameterList>& params); \
1739 #endif // TPETRA_TRIPLEMATRIXMULTIPLY_DEF_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
static int TAFC_OptimizationCoreCount()
MPI process count above which Tpetra::CrsMatrix::transferAndFillComplete will attempt to do advanced ...
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
global_size_t getGlobalNumRows() const override
Number of global elements in the row map of this matrix.
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...
bool isFillActive() const
Whether the matrix is not fill complete.
size_t global_size_t
Global size_t object.
bool isFillComplete() const override
Whether the matrix is fill complete.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const override
The communicator over which the matrix is distributed.
Construct and (optionally) redistribute the explicitly stored transpose of a CrsMatrix.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Teuchos::RCP< const RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const override
This matrix's graph, as a RowGraph.
Utility functions for packing and unpacking sparse matrix entries.
void MultiplyRAP(const CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &R, bool transposeR, const CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool transposeA, const CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &P, bool transposeP, CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Ac, bool call_FillComplete_on_result=true, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Sparse matrix-matrix multiply.
A parallel distribution of indices over processes.
Teuchos::RCP< const map_type > getDomainMap() const override
The domain Map of this matrix.
Internal functions and macros designed for use with Tpetra::Import and Tpetra::Export objects...
Stand-alone utility functions and macros.
Struct that holds views of the contents of a CrsMatrix.