41 #ifndef TPETRA_TRIPLEMATRIXMULTIPLY_DEF_HPP
42 #define TPETRA_TRIPLEMATRIXMULTIPLY_DEF_HPP
45 #include "TpetraExt_MatrixMatrix_ExtraKernels_decl.hpp"
46 #include "Teuchos_VerboseObject.hpp"
47 #include "Teuchos_Array.hpp"
49 #include "Tpetra_ConfigDefs.hpp"
50 #include "Tpetra_CrsMatrix.hpp"
52 #include "Tpetra_RowMatrixTransposer.hpp"
53 #include "Tpetra_ConfigDefs.hpp"
54 #include "Tpetra_Map.hpp"
55 #include "Tpetra_Export.hpp"
60 #include "Teuchos_FancyOStream.hpp"
74 #include "TpetraExt_MatrixMatrix_OpenMP.hpp"
75 #include "TpetraExt_MatrixMatrix_Cuda.hpp"
76 #include "TpetraExt_MatrixMatrix_HIP.hpp"
80 namespace TripleMatrixMultiply{
88 template <
class Scalar,
100 bool call_FillComplete_on_result,
101 const std::string& label,
102 const Teuchos::RCP<Teuchos::ParameterList>& params)
107 typedef LocalOrdinal LO;
108 typedef GlobalOrdinal GO;
117 #ifdef HAVE_TPETRA_MMM_TIMINGS
118 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
119 using Teuchos::TimeMonitor;
120 RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP All Setup"))));
123 const std::string prefix =
"TpetraExt::TripleMatrixMultiply::MultiplyRAP(): ";
128 TEUCHOS_TEST_FOR_EXCEPTION(!R.
isFillComplete(), std::runtime_error, prefix <<
"Matrix R is not fill complete.");
129 TEUCHOS_TEST_FOR_EXCEPTION(!A.
isFillComplete(), std::runtime_error, prefix <<
"Matrix A is not fill complete.");
130 TEUCHOS_TEST_FOR_EXCEPTION(!P.
isFillComplete(), std::runtime_error, prefix <<
"Matrix P is not fill complete.");
135 RCP<const crs_matrix_type> Rprime =
null;
139 RCP<const crs_matrix_type> Aprime =
null;
143 RCP<const crs_matrix_type> Pprime =
null;
153 const bool newFlag = !Ac.
getGraph()->isLocallyIndexed() && !Ac.
getGraph()->isGloballyIndexed();
155 using Teuchos::ParameterList;
156 RCP<ParameterList> transposeParams (
new ParameterList);
157 transposeParams->set (
"sort",
false);
159 if (transposeR && &R != &P) {
160 transposer_type transposer(rcpFromRef (R));
161 Rprime = transposer.createTranspose (transposeParams);
163 Rprime = rcpFromRef(R);
167 transposer_type transposer(rcpFromRef (A));
168 Aprime = transposer.createTranspose (transposeParams);
170 Aprime = rcpFromRef(A);
174 transposer_type transposer(rcpFromRef (P));
175 Pprime = transposer.createTranspose (transposeParams);
177 Pprime = rcpFromRef(P);
190 TEUCHOS_TEST_FOR_EXCEPTION(Rright != Aleft, std::runtime_error,
191 prefix <<
"ERROR, inner dimensions of op(R) and op(A) "
192 "must match for matrix-matrix product. op(R) is "
193 << Rleft <<
"x" << Rright <<
", op(A) is "<< Aleft <<
"x" << Aright);
195 TEUCHOS_TEST_FOR_EXCEPTION(Aright != Pleft, std::runtime_error,
196 prefix <<
"ERROR, inner dimensions of op(A) and op(P) "
197 "must match for matrix-matrix product. op(A) is "
198 << Aleft <<
"x" << Aright <<
", op(P) is "<< Pleft <<
"x" << Pright);
204 TEUCHOS_TEST_FOR_EXCEPTION(Rleft > Ac.
getGlobalNumRows(), std::runtime_error,
205 prefix <<
"ERROR, dimensions of result Ac must "
206 "match dimensions of op(R) * op(A) * op(P). Ac has " << Ac.
getGlobalNumRows()
207 <<
" rows, should have at least " << Rleft << std::endl);
219 int numProcs = P.
getComm()->getSize();
223 crs_matrix_struct_type Rview;
224 crs_matrix_struct_type Aview;
225 crs_matrix_struct_type Pview;
227 RCP<const map_type> targetMap_R = Rprime->getRowMap();
228 RCP<const map_type> targetMap_A = Aprime->getRowMap();
229 RCP<const map_type> targetMap_P = Pprime->getRowMap();
231 #ifdef HAVE_TPETRA_MMM_TIMINGS
232 MM = Teuchos::null; MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP All I&X"))));
238 RCP<const import_type> dummyImporter;
240 if (!(transposeR && &R == &P))
241 MMdetails::import_and_extract_views(*Rprime, targetMap_R, Rview, dummyImporter,
true, label, params);
243 MMdetails::import_and_extract_views(*Aprime, targetMap_A, Aview, dummyImporter,
true, label, params);
248 targetMap_P = Aprime->getColMap();
251 MMdetails::import_and_extract_views(*Pprime, targetMap_P, Pview, Aprime->getGraph()->getImporter(),
false, label, params);
254 RCP<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Actemp;
256 bool needs_final_export = !Pprime->getGraph()->getImporter().is_null();
257 if (needs_final_export)
260 Actemp = rcp(&Ac,
false);
262 #ifdef HAVE_TPETRA_MMM_TIMINGS
263 MM = Teuchos::null; MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP All Multiply"))));
267 if (call_FillComplete_on_result && newFlag) {
268 if (transposeR && &R == &P)
269 MMdetails::mult_PT_A_P_newmatrix(Aview, Pview, *Actemp, label, params);
271 MMdetails::mult_R_A_P_newmatrix(Rview, Aview, Pview, *Actemp, label, params);
272 }
else if (call_FillComplete_on_result) {
273 if (transposeR && &R == &P)
274 MMdetails::mult_PT_A_P_reuse(Aview, Pview, *Actemp, label, params);
276 MMdetails::mult_R_A_P_reuse(Rview, Aview, Pview, *Actemp, label, params);
299 if (transposeR && &R == &P)
300 MMdetails::mult_PT_A_P_newmatrix(Aview, Pview, *Actemp, label, params);
302 MMdetails::mult_R_A_P_newmatrix(Rview, Aview, Pview, *Actemp, label, params);
305 if (needs_final_export) {
306 #ifdef HAVE_TPETRA_MMM_TIMINGS
307 MM = Teuchos::null; MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP exportAndFillComplete"))));
309 Teuchos::ParameterList labelList;
310 labelList.set(
"Timer Label", label);
311 Teuchos::ParameterList& labelList_subList = labelList.sublist(
"matrixmatrix: kernel params",
false);
313 RCP<crs_matrix_type> Acprime = rcpFromRef(Ac);
315 bool overrideAllreduce =
false;
317 if(!params.is_null()) {
318 Teuchos::ParameterList& params_sublist = params->sublist(
"matrixmatrix: kernel params",
false);
320 mm_optimization_core_count = params_sublist.get(
"MM_TAFC_OptimizationCoreCount",mm_optimization_core_count);
321 int mm_optimization_core_count2 = params->get(
"MM_TAFC_OptimizationCoreCount",mm_optimization_core_count);
322 if(mm_optimization_core_count2<mm_optimization_core_count) mm_optimization_core_count=mm_optimization_core_count2;
323 isMM = params_sublist.get(
"isMatrixMatrix_TransferAndFillComplete",
false);
324 overrideAllreduce = params_sublist.get(
"MM_TAFC_OverrideAllreduceCheck",
false);
326 labelList.set(
"compute global constants",params->get(
"compute global constants",
true));
328 labelList_subList.set(
"MM_TAFC_OptimizationCoreCount",mm_optimization_core_count,
"Core Count above which the optimized neighbor discovery is used");
330 labelList_subList.set(
"isMatrixMatrix_TransferAndFillComplete",isMM,
331 "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.");
332 labelList_subList.set(
"MM_TAFC_OverrideAllreduceCheck",overrideAllreduce);
334 export_type exporter = export_type(*Pprime->getGraph()->getImporter());
335 Actemp->exportAndFillComplete(Acprime,
337 Acprime->getDomainMap(),
338 Acprime->getRangeMap(),
339 rcp(&labelList,
false));
342 #ifdef HAVE_TPETRA_MMM_STATISTICS
343 printMultiplicationStatistics(Actemp->getGraph()->getExporter(), label+std::string(
" RAP MMM"));
354 template<
class Scalar,
358 void mult_R_A_P_newmatrix(
359 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
360 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
361 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
362 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
363 const std::string& label,
364 const Teuchos::RCP<Teuchos::ParameterList>& params)
366 using Teuchos::Array;
367 using Teuchos::ArrayRCP;
368 using Teuchos::ArrayView;
373 typedef LocalOrdinal LO;
374 typedef GlobalOrdinal GO;
377 typedef Import<LO,GO,NO> import_type;
378 typedef Map<LO,GO,NO> map_type;
381 typedef typename map_type::local_map_type local_map_type;
383 typedef typename KCRS::StaticCrsGraphType graph_t;
384 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
385 typedef typename NO::execution_space execution_space;
386 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
387 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
389 #ifdef HAVE_TPETRA_MMM_TIMINGS
390 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
391 using Teuchos::TimeMonitor;
392 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
394 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
397 RCP<const import_type> Cimport;
398 RCP<const map_type> Ccolmap;
399 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
400 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
401 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
402 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
403 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
404 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
405 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
413 lo_view_t Pcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Pcol2Ccol"),Pview.colMap->getNodeNumElements()), Icol2Ccol;
415 if (Pview.importMatrix.is_null()) {
418 Ccolmap = Pview.colMap;
419 const LO colMapSize =
static_cast<LO
>(Pview.colMap->getNodeNumElements());
421 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_fill",
422 Kokkos::RangePolicy<execution_space, LO>(0, colMapSize),
423 KOKKOS_LAMBDA(
const LO i) {
436 if (!Pimport.is_null() && !Iimport.is_null()) {
437 Cimport = Pimport->setUnion(*Iimport);
439 else if (!Pimport.is_null() && Iimport.is_null()) {
440 Cimport = Pimport->setUnion();
442 else if (Pimport.is_null() && !Iimport.is_null()) {
443 Cimport = Iimport->setUnion();
446 throw std::runtime_error(
"TpetraExt::RAP status of matrix importers is nonsensical");
448 Ccolmap = Cimport->getTargetMap();
453 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
454 std::runtime_error,
"Tpetra::RAP: Import setUnion messed with the DomainMap in an unfortunate way");
461 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getNodeNumElements());
462 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
463 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_getGlobalElement",range_type(0,Pview.origMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
464 Pcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
466 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Icol2Ccol_getGlobalElement",range_type(0,Pview.importMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
467 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
476 Ac.replaceColMap(Ccolmap);
494 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getNodeNumElements());
495 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getNodeNumElements());
496 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) {
497 GO aidx = Acolmap_local.getGlobalElement(i);
498 LO P_LID = Prowmap_local.getLocalElement(aidx);
499 if (P_LID != LO_INVALID) {
500 targetMapToOrigRow(i) = P_LID;
501 targetMapToImportRow(i) = LO_INVALID;
503 LO I_LID = Irowmap_local.getLocalElement(aidx);
504 targetMapToOrigRow(i) = LO_INVALID;
505 targetMapToImportRow(i) = I_LID;
511 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
512 mult_R_A_P_newmatrix_kernel_wrapper(Rview, Aview, Pview,
513 targetMapToOrigRow,targetMapToImportRow, Pcol2Ccol, Icol2Ccol,
514 Ac, Cimport, label, params);
519 template<
class Scalar,
523 void mult_R_A_P_reuse(
524 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
525 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
526 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
527 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
528 const std::string& label,
529 const Teuchos::RCP<Teuchos::ParameterList>& params)
531 using Teuchos::Array;
532 using Teuchos::ArrayRCP;
533 using Teuchos::ArrayView;
538 typedef LocalOrdinal LO;
539 typedef GlobalOrdinal GO;
542 typedef Import<LO,GO,NO> import_type;
543 typedef Map<LO,GO,NO> map_type;
546 typedef typename map_type::local_map_type local_map_type;
548 typedef typename KCRS::StaticCrsGraphType graph_t;
549 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
550 typedef typename NO::execution_space execution_space;
551 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
552 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
554 #ifdef HAVE_TPETRA_MMM_TIMINGS
555 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
556 using Teuchos::TimeMonitor;
557 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
559 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
562 RCP<const import_type> Cimport = Ac.getGraph()->getImporter();
563 RCP<const map_type> Ccolmap = Ac.getColMap();
564 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
565 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
566 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
567 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
568 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
569 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
570 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
571 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
574 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Bcol2Ccol"),Pview.colMap->getNodeNumElements()), Icol2Ccol;
578 Kokkos::parallel_for(range_type(0,Pview.origMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
579 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
582 if (!Pview.importMatrix.is_null()) {
583 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
584 std::runtime_error,
"Tpetra::MMM: Import setUnion messed with the DomainMap in an unfortunate way");
586 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getNodeNumElements());
587 Kokkos::parallel_for(range_type(0,Pview.importMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
588 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
594 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getNodeNumElements());
595 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getNodeNumElements());
596 Kokkos::parallel_for(range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(
const LO i) {
597 GO aidx = Acolmap_local.getGlobalElement(i);
598 LO B_LID = Prowmap_local.getLocalElement(aidx);
599 if (B_LID != LO_INVALID) {
600 targetMapToOrigRow(i) = B_LID;
601 targetMapToImportRow(i) = LO_INVALID;
603 LO I_LID = Irowmap_local.getLocalElement(aidx);
604 targetMapToOrigRow(i) = LO_INVALID;
605 targetMapToImportRow(i) = I_LID;
612 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
613 mult_R_A_P_reuse_kernel_wrapper(Rview, Aview, Pview,
614 targetMapToOrigRow,targetMapToImportRow, Bcol2Ccol, Icol2Ccol,
615 Ac, Cimport, label, params);
620 template<
class Scalar,
624 void mult_PT_A_P_newmatrix(
625 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
626 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
627 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
628 const std::string& label,
629 const Teuchos::RCP<Teuchos::ParameterList>& params)
631 using Teuchos::Array;
632 using Teuchos::ArrayRCP;
633 using Teuchos::ArrayView;
638 typedef LocalOrdinal LO;
639 typedef GlobalOrdinal GO;
642 typedef Import<LO,GO,NO> import_type;
643 typedef Map<LO,GO,NO> map_type;
646 typedef typename map_type::local_map_type local_map_type;
648 typedef typename KCRS::StaticCrsGraphType graph_t;
649 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
650 typedef typename NO::execution_space execution_space;
651 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
652 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
654 #ifdef HAVE_TPETRA_MMM_TIMINGS
655 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
656 using Teuchos::TimeMonitor;
657 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP M5 Cmap")))));
659 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
662 RCP<const import_type> Cimport;
663 RCP<const map_type> Ccolmap;
664 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
665 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
666 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
667 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
668 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
669 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
670 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
678 lo_view_t Pcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Pcol2Ccol"),Pview.colMap->getNodeNumElements()), Icol2Ccol;
680 if (Pview.importMatrix.is_null()) {
683 Ccolmap = Pview.colMap;
684 const LO colMapSize =
static_cast<LO
>(Pview.colMap->getNodeNumElements());
686 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_fill",
687 Kokkos::RangePolicy<execution_space, LO>(0, colMapSize),
688 KOKKOS_LAMBDA(
const LO i) {
701 if (!Pimport.is_null() && !Iimport.is_null()) {
702 Cimport = Pimport->setUnion(*Iimport);
704 else if (!Pimport.is_null() && Iimport.is_null()) {
705 Cimport = Pimport->setUnion();
707 else if (Pimport.is_null() && !Iimport.is_null()) {
708 Cimport = Iimport->setUnion();
711 throw std::runtime_error(
"TpetraExt::RAP status of matrix importers is nonsensical");
713 Ccolmap = Cimport->getTargetMap();
718 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
719 std::runtime_error,
"Tpetra::RAP: Import setUnion messed with the DomainMap in an unfortunate way");
726 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getNodeNumElements());
727 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
728 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_getGlobalElement",range_type(0,Pview.origMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
729 Pcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
731 Kokkos::parallel_for(
"Tpetra::mult_R_A_P_newmatrix::Icol2Ccol_getGlobalElement",range_type(0,Pview.importMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
732 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
741 Ac.replaceColMap(Ccolmap);
759 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getNodeNumElements());
760 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getNodeNumElements());
762 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) {
763 GO aidx = Acolmap_local.getGlobalElement(i);
764 LO P_LID = Prowmap_local.getLocalElement(aidx);
765 if (P_LID != LO_INVALID) {
766 targetMapToOrigRow(i) = P_LID;
767 targetMapToImportRow(i) = LO_INVALID;
769 LO I_LID = Irowmap_local.getLocalElement(aidx);
770 targetMapToOrigRow(i) = LO_INVALID;
771 targetMapToImportRow(i) = I_LID;
777 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
778 mult_PT_A_P_newmatrix_kernel_wrapper(Aview, Pview,
779 targetMapToOrigRow,targetMapToImportRow, Pcol2Ccol, Icol2Ccol,
780 Ac, Cimport, label, params);
784 template<
class Scalar,
788 void mult_PT_A_P_reuse(
789 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
790 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
791 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
792 const std::string& label,
793 const Teuchos::RCP<Teuchos::ParameterList>& params)
795 using Teuchos::Array;
796 using Teuchos::ArrayRCP;
797 using Teuchos::ArrayView;
802 typedef LocalOrdinal LO;
803 typedef GlobalOrdinal GO;
806 typedef Import<LO,GO,NO> import_type;
807 typedef Map<LO,GO,NO> map_type;
810 typedef typename map_type::local_map_type local_map_type;
812 typedef typename KCRS::StaticCrsGraphType graph_t;
813 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
814 typedef typename NO::execution_space execution_space;
815 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
816 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
818 #ifdef HAVE_TPETRA_MMM_TIMINGS
819 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
820 using Teuchos::TimeMonitor;
821 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
823 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
826 RCP<const import_type> Cimport = Ac.getGraph()->getImporter();
827 RCP<const map_type> Ccolmap = Ac.getColMap();
828 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
829 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
830 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
831 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
832 local_map_type Irowmap_local;
if(!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
833 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
834 local_map_type Icolmap_local;
if(!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
835 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
838 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Bcol2Ccol"),Pview.colMap->getNodeNumElements()), Icol2Ccol;
842 Kokkos::parallel_for(range_type(0,Pview.origMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
843 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
846 if (!Pview.importMatrix.is_null()) {
847 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
848 std::runtime_error,
"Tpetra::MMM: Import setUnion messed with the DomainMap in an unfortunate way");
850 Kokkos::resize(Icol2Ccol,Pview.importMatrix->getColMap()->getNodeNumElements());
851 Kokkos::parallel_for(range_type(0,Pview.importMatrix->getColMap()->getNodeNumElements()),KOKKOS_LAMBDA(
const LO i) {
852 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
858 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"),Aview.colMap->getNodeNumElements());
859 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"),Aview.colMap->getNodeNumElements());
860 Kokkos::parallel_for(range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex()+1),KOKKOS_LAMBDA(
const LO i) {
861 GO aidx = Acolmap_local.getGlobalElement(i);
862 LO B_LID = Prowmap_local.getLocalElement(aidx);
863 if (B_LID != LO_INVALID) {
864 targetMapToOrigRow(i) = B_LID;
865 targetMapToImportRow(i) = LO_INVALID;
867 LO I_LID = Irowmap_local.getLocalElement(aidx);
868 targetMapToOrigRow(i) = LO_INVALID;
869 targetMapToImportRow(i) = I_LID;
876 KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,lo_view_t>::
877 mult_PT_A_P_reuse_kernel_wrapper(Aview, Pview,
878 targetMapToOrigRow,targetMapToImportRow, Bcol2Ccol, Icol2Ccol,
879 Ac, Cimport, label, params);
886 template<
class Scalar,
890 class LocalOrdinalViewType>
891 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_R_A_P_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
892 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
893 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
894 const LocalOrdinalViewType & Acol2Prow_dev,
895 const LocalOrdinalViewType & Acol2PIrow_dev,
896 const LocalOrdinalViewType & Pcol2Accol_dev,
897 const LocalOrdinalViewType & PIcol2Accol_dev,
898 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
899 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
900 const std::string& label,
901 const Teuchos::RCP<Teuchos::ParameterList>& params) {
902 #ifdef HAVE_TPETRA_MMM_TIMINGS
903 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
904 using Teuchos::TimeMonitor;
905 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix SerialCore"))));
908 using Teuchos::Array;
909 using Teuchos::ArrayRCP;
910 using Teuchos::ArrayView;
916 typedef typename KCRS::StaticCrsGraphType graph_t;
917 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
918 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
919 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
920 typedef typename KCRS::values_type::non_const_type scalar_view_t;
923 typedef LocalOrdinal LO;
924 typedef GlobalOrdinal GO;
926 typedef Map<LO,GO,NO> map_type;
927 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
928 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
929 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
932 RCP<const map_type> Accolmap = Ac.getColMap();
933 size_t m = Rview.origMatrix->getNodeNumRows();
934 size_t n = Accolmap->getNodeNumElements();
935 size_t p_max_nnz_per_row = Pview.origMatrix->getNodeMaxNumRowEntries();
938 auto Acol2Prow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
940 auto Acol2PIrow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
942 auto Pcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
944 auto PIcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
948 const auto Amat = Aview.origMatrix->getLocalMatrixHost();
949 const auto Pmat = Pview.origMatrix->getLocalMatrixHost();
950 const auto Rmat = Rview.origMatrix->getLocalMatrixHost();
952 auto Arowptr = Amat.graph.row_map;
953 auto Prowptr = Pmat.graph.row_map;
954 auto Rrowptr = Rmat.graph.row_map;
955 const auto Acolind = Amat.graph.entries;
956 const auto Pcolind = Pmat.graph.entries;
957 const auto Rcolind = Rmat.graph.entries;
958 const auto Avals = Amat.values;
959 const auto Pvals = Pmat.values;
960 const auto Rvals = Rmat.values;
962 typename c_lno_view_t::HostMirror::const_type Irowptr;
963 typename lno_nnz_view_t::HostMirror Icolind;
964 typename scalar_view_t::HostMirror Ivals;
965 if(!Pview.importMatrix.is_null()) {
966 auto lclP = Pview.importMatrix->getLocalMatrixHost();
967 Irowptr = lclP.graph.row_map;
968 Icolind = lclP.graph.entries;
970 p_max_nnz_per_row = std::max(p_max_nnz_per_row,Pview.importMatrix->getNodeMaxNumRowEntries());
973 #ifdef HAVE_TPETRA_MMM_TIMINGS
974 RCP<TimeMonitor> MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix SerialCore - Compare"))));
984 size_t CSR_alloc = std::max(C_estimate_nnz(*Aview.origMatrix, *Pview.origMatrix), n);
985 typename lno_view_t::HostMirror Crowptr(Kokkos::ViewAllocateWithoutInitializing(
"Crowptr"),m+1);
986 typename lno_nnz_view_t::HostMirror Ccolind(Kokkos::ViewAllocateWithoutInitializing(
"Ccolind"),CSR_alloc);
987 typename scalar_view_t::HostMirror Cvals(Kokkos::ViewAllocateWithoutInitializing(
"Cvals"),CSR_alloc);
997 const size_t INVALID = Teuchos::OrdinalTraits<size_t>::invalid();
998 Array<size_t> ac_status(n, ST_INVALID);
1008 size_t nnz = 0, nnz_old = 0;
1009 for (
size_t i = 0; i < m; i++) {
1015 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i+1]; kk++) {
1017 const SC Rik = Rvals[kk];
1021 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1023 const SC Akl = Avals[ll];
1028 if (Acol2Prow[l] != LO_INVALID) {
1035 size_t Pl = Teuchos::as<size_t>(Acol2Prow[l]);
1038 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1040 LO Acj = Pcol2Accol[j];
1043 if (ac_status[Acj] == INVALID || ac_status[Acj] < nnz_old) {
1044 #ifdef HAVE_TPETRA_DEBUG
1046 TEUCHOS_TEST_FOR_EXCEPTION(nnz >= Teuchos::as<size_t>(Ccolind.size()),
1048 label <<
" ERROR, not enough memory allocated for matrix product. Allocated: " << Ccolind.extent(0) << std::endl);
1051 ac_status[Acj] = nnz;
1053 Cvals[nnz] = Rik*Akl*Plj;
1056 Cvals[ac_status[Acj]] += Rik*Akl*Plj;
1066 size_t Il = Teuchos::as<size_t>(Acol2PIrow[l]);
1067 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1069 LO Acj = PIcol2Accol[j];
1072 if (ac_status[Acj] == INVALID || ac_status[Acj] < nnz_old) {
1073 #ifdef HAVE_TPETRA_DEBUG
1075 TEUCHOS_TEST_FOR_EXCEPTION(nnz >= Teuchos::as<size_t>(Ccolind.size()),
1077 label <<
" ERROR, not enough memory allocated for matrix product. Allocated: " << Ccolind.extent(0) << std::endl);
1080 ac_status[Acj] = nnz;
1082 Cvals[nnz] = Rik*Akl*Plj;
1085 Cvals[ac_status[Acj]] += Rik*Akl*Plj;
1092 if (nnz + n > CSR_alloc) {
1094 Kokkos::resize(Ccolind,CSR_alloc);
1095 Kokkos::resize(Cvals,CSR_alloc);
1103 Kokkos::resize(Ccolind,nnz);
1104 Kokkos::resize(Cvals,nnz);
1106 #ifdef HAVE_TPETRA_MMM_TIMINGS
1107 MM = Teuchos::null; MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix Final Sort"))));
1109 auto Crowptr_dev = Kokkos::create_mirror_view_and_copy(
1110 typename KCRS::device_type(), Crowptr);
1111 auto Ccolind_dev = Kokkos::create_mirror_view_and_copy(
1112 typename KCRS::device_type(), Ccolind);
1113 auto Cvals_dev = Kokkos::create_mirror_view_and_copy(
1114 typename KCRS::device_type(), Cvals);
1117 if (params.is_null() || params->get(
"sort entries",
true))
1118 Import_Util::sortCrsEntries(Crowptr_dev, Ccolind_dev, Cvals_dev);
1119 Ac.setAllValues(Crowptr_dev, Ccolind_dev, Cvals_dev);
1121 #ifdef HAVE_TPETRA_MMM_TIMINGS
1122 MM = Teuchos::null; MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix ESFC"))));
1133 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
1134 labelList->set(
"Timer Label",label);
1135 if(!params.is_null()) labelList->set(
"compute global constants",params->get(
"compute global constants",
true));
1136 RCP<const Export<LO,GO,NO> > dummyExport;
1137 Ac.expertStaticFillComplete(Pview.origMatrix->getDomainMap(),
1138 Rview.origMatrix->getRangeMap(),
1148 template<
class Scalar,
1150 class GlobalOrdinal,
1152 class LocalOrdinalViewType>
1153 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_R_A_P_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
1154 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1155 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1156 const LocalOrdinalViewType & Acol2Prow_dev,
1157 const LocalOrdinalViewType & Acol2PIrow_dev,
1158 const LocalOrdinalViewType & Pcol2Accol_dev,
1159 const LocalOrdinalViewType & PIcol2Accol_dev,
1160 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1161 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1162 const std::string& label,
1163 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1164 #ifdef HAVE_TPETRA_MMM_TIMINGS
1165 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1166 using Teuchos::TimeMonitor;
1167 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse SerialCore"))));
1170 using Teuchos::Array;
1171 using Teuchos::ArrayRCP;
1172 using Teuchos::ArrayView;
1177 typedef typename Tpetra::CrsMatrix<Scalar,LocalOrdinal,GlobalOrdinal,Node>::local_matrix_host_type KCRS;
1178 typedef typename KCRS::StaticCrsGraphType graph_t;
1179 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
1180 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
1181 typedef typename KCRS::values_type::non_const_type scalar_view_t;
1184 typedef LocalOrdinal LO;
1185 typedef GlobalOrdinal GO;
1187 typedef Map<LO,GO,NO> map_type;
1188 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1189 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1190 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
1193 RCP<const map_type> Accolmap = Ac.getColMap();
1194 size_t m = Rview.origMatrix->getNodeNumRows();
1195 size_t n = Accolmap->getNodeNumElements();
1196 size_t p_max_nnz_per_row = Pview.origMatrix->getNodeMaxNumRowEntries();
1199 auto Acol2Prow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1201 auto Acol2PIrow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1203 auto Pcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1205 auto PIcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1209 const KCRS & Amat = Aview.origMatrix->getLocalMatrixHost();
1210 const KCRS & Pmat = Pview.origMatrix->getLocalMatrixHost();
1211 const KCRS & Rmat = Rview.origMatrix->getLocalMatrixHost();
1212 const KCRS & Cmat = Ac.getLocalMatrixHost();
1214 c_lno_view_t Arowptr = Amat.graph.row_map, Prowptr = Pmat.graph.row_map, Rrowptr = Rmat.graph.row_map, Crowptr = Cmat.graph.row_map;
1215 const lno_nnz_view_t Acolind = Amat.graph.entries, Pcolind = Pmat.graph.entries , Rcolind = Rmat.graph.entries, Ccolind = Cmat.graph.entries;
1216 const scalar_view_t Avals = Amat.values, Pvals = Pmat.values, Rvals = Rmat.values;
1217 scalar_view_t Cvals = Cmat.values;
1219 c_lno_view_t Irowptr;
1220 lno_nnz_view_t Icolind;
1221 scalar_view_t Ivals;
1222 if(!Pview.importMatrix.is_null()) {
1223 auto lclP = Pview.importMatrix->getLocalMatrixHost();
1224 Irowptr = lclP.graph.row_map;
1225 Icolind = lclP.graph.entries;
1226 Ivals = lclP.values;
1227 p_max_nnz_per_row = std::max(p_max_nnz_per_row,Pview.importMatrix->getNodeMaxNumRowEntries());
1230 #ifdef HAVE_TPETRA_MMM_TIMINGS
1231 RCP<TimeMonitor> MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse SerialCore - Compare"))));
1242 Array<size_t> ac_status(n, ST_INVALID);
1256 size_t OLD_ip = 0, CSR_ip = 0;
1257 for (
size_t i = 0; i < m; i++) {
1260 OLD_ip = Crowptr[i];
1261 CSR_ip = Crowptr[i+1];
1262 for (
size_t k = OLD_ip; k < CSR_ip; k++) {
1263 ac_status[Ccolind[k]] = k;
1270 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i+1]; kk++) {
1272 const SC Rik = Rvals[kk];
1276 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1278 const SC Akl = Avals[ll];
1283 if (Acol2Prow[l] != LO_INVALID) {
1290 size_t Pl = Teuchos::as<size_t>(Acol2Prow[l]);
1293 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1295 LO Cij = Pcol2Accol[j];
1298 TEUCHOS_TEST_FOR_EXCEPTION(ac_status[Cij] < OLD_ip || ac_status[Cij] >= CSR_ip,
1299 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph " <<
1300 "(c_status = " << ac_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
1302 Cvals[ac_status[Cij]] += Rik*Akl*Plj;
1311 size_t Il = Teuchos::as<size_t>(Acol2PIrow[l]);
1312 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1314 LO Cij = PIcol2Accol[j];
1317 TEUCHOS_TEST_FOR_EXCEPTION(ac_status[Cij] < OLD_ip || ac_status[Cij] >= CSR_ip,
1318 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph " <<
1319 "(c_status = " << ac_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
1321 Cvals[ac_status[Cij]] += Rik*Akl*Plj;
1328 #ifdef HAVE_TPETRA_MMM_TIMINGS
1329 auto MM3 = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse ESFC"))));
1332 Ac.fillComplete(Ac.getDomainMap(), Ac.getRangeMap());
1340 template<
class Scalar,
1342 class GlobalOrdinal,
1344 class LocalOrdinalViewType>
1345 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_PT_A_P_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1346 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1347 const LocalOrdinalViewType & Acol2Prow,
1348 const LocalOrdinalViewType & Acol2PIrow,
1349 const LocalOrdinalViewType & Pcol2Accol,
1350 const LocalOrdinalViewType & PIcol2Accol,
1351 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1352 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1353 const std::string& label,
1354 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1355 #ifdef HAVE_TPETRA_MMM_TIMINGS
1356 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1357 using Teuchos::TimeMonitor;
1358 Teuchos::TimeMonitor MM(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose")));
1362 typedef RowMatrixTransposer<Scalar,LocalOrdinal,GlobalOrdinal, Node> transposer_type;
1363 transposer_type transposer (Pview.origMatrix,label+std::string(
"XP: "));
1365 using Teuchos::ParameterList;
1367 RCP<ParameterList> transposeParams (
new ParameterList);
1368 transposeParams->set (
"sort",
false);
1370 if (! params.is_null ()) {
1371 transposeParams->set (
"compute global constants",
1372 params->get (
"compute global constants: temporaries",
1375 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1376 transposer.createTransposeLocal (transposeParams);
1377 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Rview;
1378 Rview.origMatrix = Ptrans;
1380 mult_R_A_P_newmatrix_kernel_wrapper(Rview,Aview,Pview,Acol2Prow,Acol2PIrow,Pcol2Accol,PIcol2Accol,Ac,Acimport,label,params);
1386 template<
class Scalar,
1388 class GlobalOrdinal,
1390 class LocalOrdinalViewType>
1391 void KernelWrappers3<Scalar,LocalOrdinal,GlobalOrdinal,Node,LocalOrdinalViewType>::mult_PT_A_P_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1392 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1393 const LocalOrdinalViewType & Acol2Prow,
1394 const LocalOrdinalViewType & Acol2PIrow,
1395 const LocalOrdinalViewType & Pcol2Accol,
1396 const LocalOrdinalViewType & PIcol2Accol,
1397 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1398 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1399 const std::string& label,
1400 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1401 #ifdef HAVE_TPETRA_MMM_TIMINGS
1402 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1403 using Teuchos::TimeMonitor;
1404 Teuchos::TimeMonitor MM(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose")));
1408 typedef RowMatrixTransposer<Scalar,LocalOrdinal,GlobalOrdinal, Node> transposer_type;
1409 transposer_type transposer (Pview.origMatrix,label+std::string(
"XP: "));
1411 using Teuchos::ParameterList;
1413 RCP<ParameterList> transposeParams (
new ParameterList);
1414 transposeParams->set (
"sort",
false);
1416 if (! params.is_null ()) {
1417 transposeParams->set (
"compute global constants",
1418 params->get (
"compute global constants: temporaries",
1421 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1422 transposer.createTransposeLocal (transposeParams);
1423 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Rview;
1424 Rview.origMatrix = Ptrans;
1426 mult_R_A_P_reuse_kernel_wrapper(Rview,Aview,Pview,Acol2Prow,Acol2PIrow,Pcol2Accol,PIcol2Accol,Ac,Acimport,label,params);
1434 template<
class Scalar,
1436 class GlobalOrdinal,
1438 void KernelWrappers3MMM<Scalar,LocalOrdinal,GlobalOrdinal,Node>::mult_PT_A_P_newmatrix_kernel_wrapper_2pass(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1439 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1440 const Teuchos::Array<LocalOrdinal> & Acol2PRow,
1441 const Teuchos::Array<LocalOrdinal> & Acol2PRowImport,
1442 const Teuchos::Array<LocalOrdinal> & Pcol2Accol,
1443 const Teuchos::Array<LocalOrdinal> & PIcol2Accol,
1444 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1445 Teuchos::RCP<
const Import<LocalOrdinal,GlobalOrdinal,Node> > Acimport,
1446 const std::string& label,
1447 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1448 #ifdef HAVE_TPETRA_MMM_TIMINGS
1449 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1450 using Teuchos::TimeMonitor;
1451 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix SerialCore"))));
1454 using Teuchos::Array;
1455 using Teuchos::ArrayRCP;
1456 using Teuchos::ArrayView;
1461 typedef LocalOrdinal LO;
1462 typedef GlobalOrdinal GO;
1464 typedef RowMatrixTransposer<SC,LO,GO,NO> transposer_type;
1465 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1466 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
1471 size_t n = Ac.getRowMap()->getNodeNumElements();
1472 LO maxAccol = Ac.getColMap()->getMaxLocalIndex();
1475 ArrayRCP<const size_t> Arowptr_RCP, Prowptr_RCP, Irowptr_RCP;
1476 ArrayRCP<size_t> Acrowptr_RCP;
1477 ArrayRCP<const LO> Acolind_RCP, Pcolind_RCP, Icolind_RCP;
1478 ArrayRCP<LO> Accolind_RCP;
1479 ArrayRCP<const Scalar> Avals_RCP, Pvals_RCP, Ivals_RCP;
1480 ArrayRCP<SC> Acvals_RCP;
1487 Aview.origMatrix->getAllValues(Arowptr_RCP, Acolind_RCP, Avals_RCP);
1488 Pview.origMatrix->getAllValues(Prowptr_RCP, Pcolind_RCP, Pvals_RCP);
1490 if (!Pview.importMatrix.is_null())
1491 Pview.importMatrix->getAllValues(Irowptr_RCP, Icolind_RCP, Ivals_RCP);
1498 ArrayView<const size_t> Arowptr, Prowptr, Irowptr;
1499 ArrayView<const LO> Acolind, Pcolind, Icolind;
1500 ArrayView<const SC> Avals, Pvals, Ivals;
1501 ArrayView<size_t> Acrowptr;
1502 ArrayView<LO> Accolind;
1503 ArrayView<SC> Acvals;
1504 Arowptr = Arowptr_RCP(); Acolind = Acolind_RCP(); Avals = Avals_RCP();
1505 Prowptr = Prowptr_RCP(); Pcolind = Pcolind_RCP(); Pvals = Pvals_RCP();
1506 if (!Pview.importMatrix.is_null()) {
1507 Irowptr = Irowptr_RCP(); Icolind = Icolind_RCP(); Ivals = Ivals_RCP();
1519 #ifdef HAVE_TPETRA_MMM_TIMINGS
1520 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose"))));
1527 ArrayRCP<const size_t> Rrowptr_RCP;
1528 ArrayRCP<const LO> Rcolind_RCP;
1529 ArrayRCP<const Scalar> Rvals_RCP;
1530 ArrayView<const size_t> Rrowptr;
1531 ArrayView<const LO> Rcolind;
1532 ArrayView<const SC> Rvals;
1534 transposer_type transposer (Pview.origMatrix,label+std::string(
"XP: "));
1536 using Teuchos::ParameterList;
1537 RCP<ParameterList> transposeParams (
new ParameterList);
1538 transposeParams->set (
"sort",
false);
1539 if (! params.is_null ()) {
1540 transposeParams->set (
"compute global constants",
1541 params->get (
"compute global constants: temporaries",
1544 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1545 transposer.createTransposeLocal (transposeParams);
1547 Ptrans->getAllValues(Rrowptr_RCP, Rcolind_RCP, Rvals_RCP);
1548 Rrowptr = Rrowptr_RCP();
1549 Rcolind = Rcolind_RCP();
1550 Rvals = Rvals_RCP();
1555 #ifdef HAVE_TPETRA_MMM_TIMINGS
1556 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP graph"))));
1559 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1560 Array<size_t> ac_status(maxAccol + 1, ST_INVALID);
1562 size_t nnz_alloc = std::max(Ac_estimate_nnz(*Aview.origMatrix, *Pview.origMatrix), n);
1563 size_t nnzPerRowA = 100;
1564 if (Aview.origMatrix->getNodeNumEntries() > 0)
1565 nnzPerRowA = Aview.origMatrix->getNodeNumEntries()/Aview.origMatrix->getNodeNumRows();
1566 Acrowptr_RCP.resize(n+1);
1567 Acrowptr = Acrowptr_RCP();
1568 Accolind_RCP.resize(nnz_alloc);
1569 Accolind = Accolind_RCP();
1571 size_t nnz = 0, nnz_old = 0;
1572 for (
size_t i = 0; i < n; i++) {
1578 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i+1]; kk++) {
1581 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1584 if (Acol2PRow[l] != LO_INVALID) {
1591 size_t Pl = Teuchos::as<size_t>(Acol2PRow[l]);
1594 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1596 LO Acj = Pcol2Accol[j];
1598 if (ac_status[Acj] == ST_INVALID || ac_status[Acj] < nnz_old) {
1600 ac_status[Acj] = nnz;
1601 Accolind[nnz] = Acj;
1612 size_t Il = Teuchos::as<size_t>(Acol2PRowImport[l]);
1613 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1615 LO Acj = PIcol2Accol[j];
1617 if (ac_status[Acj] == ST_INVALID || ac_status[Acj] < nnz_old){
1619 ac_status[Acj] = nnz;
1620 Accolind[nnz] = Acj;
1630 if (nnz + std::max(5*nnzPerRowA, n) > nnz_alloc) {
1632 nnz_alloc = std::max(nnz_alloc, nnz + std::max(5*nnzPerRowA, n));
1633 Accolind_RCP.resize(nnz_alloc); Accolind = Accolind_RCP();
1634 Acvals_RCP.resize(nnz_alloc); Acvals = Acvals_RCP();
1641 Accolind_RCP.resize(nnz);
1642 Accolind = Accolind_RCP();
1645 Acvals_RCP.resize(nnz, SC_ZERO);
1646 Acvals = Acvals_RCP();
1653 #ifdef HAVE_TPETRA_MMM_TIMINGS
1654 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix Fill Matrix"))));
1658 for (
size_t k = 0; k < n; k++) {
1659 for (
size_t ii = Prowptr[k]; ii < Prowptr[k+1]; ii++) {
1661 const SC Pki = Pvals[ii];
1662 for (
size_t ll = Arowptr[k]; ll < Arowptr[k+1]; ll++) {
1664 const SC Akl = Avals[ll];
1667 if (Acol2PRow[l] != LO_INVALID) {
1674 size_t Pl = Teuchos::as<size_t>(Acol2PRow[l]);
1675 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl+1]; jj++) {
1677 LO Acj = Pcol2Accol[j];
1679 for (pp = Acrowptr[i]; pp < Acrowptr[i+1]; pp++)
1680 if (Accolind[pp] == Acj)
1684 Acvals[pp] += Pki*Akl*Pvals[jj];
1693 size_t Il = Teuchos::as<size_t>(Acol2PRowImport[l]);
1694 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il+1]; jj++) {
1696 LO Acj = PIcol2Accol[j];
1698 for (pp = Acrowptr[i]; pp < Acrowptr[i+1]; pp++)
1699 if (Accolind[pp] == Acj)
1703 Acvals[pp] += Pki*Akl*Ivals[jj];
1711 #ifdef HAVE_TPETRA_MMM_TIMINGS
1712 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP sort"))));
1719 Import_Util::sortCrsEntries(Acrowptr_RCP(), Accolind_RCP(), Acvals_RCP());
1722 Ac.setAllValues(Acrowptr_RCP, Accolind_RCP, Acvals_RCP);
1724 #ifdef HAVE_TPETRA_MMM_TIMINGS
1725 MM = rcp(
new TimeMonitor (*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix ESFC"))));
1736 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
1737 labelList->set(
"Timer Label",label);
1739 if(!params.is_null()) labelList->set(
"compute global constants",params->get(
"compute global constants",
true));
1740 RCP<const Export<LO,GO,NO> > dummyExport;
1741 Ac.expertStaticFillComplete(Pview.origMatrix->getDomainMap(),
1742 Pview.origMatrix->getDomainMap(),
1744 dummyExport, labelList);
1758 #define TPETRA_TRIPLEMATRIXMULTIPLY_INSTANT(SCALAR,LO,GO,NODE) \
1761 void TripleMatrixMultiply::MultiplyRAP( \
1762 const CrsMatrix< SCALAR , LO , GO , NODE >& R, \
1764 const CrsMatrix< SCALAR , LO , GO , NODE >& A, \
1766 const CrsMatrix< SCALAR , LO , GO , NODE >& P, \
1768 CrsMatrix< SCALAR , LO , GO , NODE >& Ac, \
1769 bool call_FillComplete_on_result, \
1770 const std::string & label, \
1771 const Teuchos::RCP<Teuchos::ParameterList>& params); \
Utility functions for packing and unpacking sparse matrix entries.
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.
Sparse matrix that presents a row-oriented interface that lets users read or modify entries.
Teuchos::RCP< const map_type > getDomainMap() const override
The domain Map of this matrix.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const override
The communicator over which the matrix is distributed.
bool isFillActive() const
Whether the matrix is not fill complete.
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...
global_size_t getGlobalNumRows() const override
Number of global elements in the row map of this matrix.
Teuchos::RCP< const RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const override
This matrix's graph, as a RowGraph.
bool isFillComplete() const override
Whether the matrix is fill complete.
static int TAFC_OptimizationCoreCount()
MPI process count above which Tpetra::CrsMatrix::transferAndFillComplete will attempt to do advanced ...
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
A parallel distribution of indices over processes.
Construct and (optionally) redistribute the explicitly stored transpose of a CrsMatrix.
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.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
size_t global_size_t
Global size_t object.