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"
40#include "TpetraExt_MatrixMatrix_OpenMP.hpp"
41#include "TpetraExt_MatrixMatrix_Cuda.hpp"
42#include "TpetraExt_MatrixMatrix_HIP.hpp"
43#include "TpetraExt_MatrixMatrix_SYCL.hpp"
47namespace TripleMatrixMultiply {
55template <
class Scalar,
68 const std::string& label,
69 const Teuchos::RCP<Teuchos::ParameterList>&
params) {
83#ifdef HAVE_TPETRA_MMM_TIMINGS
84 std::string
prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
85 using Teuchos::TimeMonitor;
89 const std::string
prefix =
"TpetraExt::TripleMatrixMultiply::MultiplyRAP(): ";
119 const bool newFlag = !
Ac.getGraph()->isLocallyIndexed() && !
Ac.getGraph()->isGloballyIndexed();
121 using Teuchos::ParameterList;
157 prefix <<
"ERROR, inner dimensions of op(R) and op(A) "
158 "must match for matrix-matrix product. op(R) is "
162 prefix <<
"ERROR, inner dimensions of op(A) and op(P) "
163 "must match for matrix-matrix product. op(A) is "
171 prefix <<
"ERROR, dimensions of result Ac must "
172 "match dimensions of op(R) * op(A) * op(P). Ac has "
173 <<
Ac.getGlobalNumRows()
174 <<
" rows, should have at least " <<
Rleft << std::endl);
198#ifdef HAVE_TPETRA_MMM_TIMINGS
229#ifdef HAVE_TPETRA_MMM_TIMINGS
274#ifdef HAVE_TPETRA_MMM_TIMINGS
295 labelList.set(
"compute global constants",
params->get(
"compute global constants",
true));
300 "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.");
303 export_type
exporter = export_type(*
Pprime->getGraph()->getImporter());
310#ifdef HAVE_TPETRA_MMM_STATISTICS
320template <
class Scalar,
324void mult_R_A_P_newmatrix(
325 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
326 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
327 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
328 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
329 const std::string& label,
330 const Teuchos::RCP<Teuchos::ParameterList>& params) {
331 using Teuchos::Array;
332 using Teuchos::ArrayRCP;
333 using Teuchos::ArrayView;
338 typedef LocalOrdinal LO;
339 typedef GlobalOrdinal GO;
342 typedef Import<LO, GO, NO> import_type;
343 typedef Map<LO, GO, NO> map_type;
346 typedef typename map_type::local_map_type local_map_type;
348 typedef typename KCRS::StaticCrsGraphType graph_t;
349 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
350 typedef typename NO::execution_space execution_space;
351 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
352 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
354#ifdef HAVE_TPETRA_MMM_TIMINGS
355 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
356 using Teuchos::TimeMonitor;
357 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
359 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
362 RCP<const import_type> Cimport;
363 RCP<const map_type> Ccolmap;
364 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
365 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
366 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
367 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
368 local_map_type Irowmap_local;
369 if (!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
370 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
371 local_map_type Icolmap_local;
372 if (!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
379 lo_view_t Pcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Pcol2Ccol"), Pview.colMap->getLocalNumElements()), Icol2Ccol;
381 if (Pview.importMatrix.is_null()) {
384 Ccolmap = Pview.colMap;
385 const LO colMapSize =
static_cast<LO
>(Pview.colMap->getLocalNumElements());
387 Kokkos::parallel_for(
388 "Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_fill",
389 Kokkos::RangePolicy<execution_space, LO>(0, colMapSize),
390 KOKKOS_LAMBDA(
const LO i) {
402 if (!Pimport.is_null() && !Iimport.is_null()) {
403 Cimport = Pimport->setUnion(*Iimport);
404 }
else if (!Pimport.is_null() && Iimport.is_null()) {
405 Cimport = Pimport->setUnion();
406 }
else if (Pimport.is_null() && !Iimport.is_null()) {
407 Cimport = Iimport->setUnion();
409 throw std::runtime_error(
"TpetraExt::RAP status of matrix importers is nonsensical");
411 Ccolmap = Cimport->getTargetMap();
416 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
417 std::runtime_error,
"Tpetra::RAP: Import setUnion messed with the DomainMap in an unfortunate way");
424 Kokkos::resize(Icol2Ccol, Pview.importMatrix->getColMap()->getLocalNumElements());
425 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
426 Kokkos::parallel_for(
427 "Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_getGlobalElement", range_type(0, Pview.origMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
428 Pcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
430 Kokkos::parallel_for(
431 "Tpetra::mult_R_A_P_newmatrix::Icol2Ccol_getGlobalElement", range_type(0, Pview.importMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
432 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
440 Ac.replaceColMap(Ccolmap);
458 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"), Aview.colMap->getLocalNumElements());
459 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"), Aview.colMap->getLocalNumElements());
460 Kokkos::parallel_for(
461 "Tpetra::mult_R_A_P_newmatrix::construct_tables", range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex() + 1), KOKKOS_LAMBDA(
const LO i) {
462 GO aidx = Acolmap_local.getGlobalElement(i);
463 LO P_LID = Prowmap_local.getLocalElement(aidx);
464 if (P_LID != LO_INVALID) {
465 targetMapToOrigRow(i) = P_LID;
466 targetMapToImportRow(i) = LO_INVALID;
468 LO I_LID = Irowmap_local.getLocalElement(aidx);
469 targetMapToOrigRow(i) = LO_INVALID;
470 targetMapToImportRow(i) = I_LID;
476 KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, lo_view_t>::
477 mult_R_A_P_newmatrix_kernel_wrapper(Rview, Aview, Pview,
478 targetMapToOrigRow, targetMapToImportRow, Pcol2Ccol, Icol2Ccol,
479 Ac, Cimport, label, params);
483template <
class Scalar,
487void mult_R_A_P_reuse(
488 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
489 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
490 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
491 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
492 const std::string& label,
493 const Teuchos::RCP<Teuchos::ParameterList>& params) {
494 using Teuchos::Array;
495 using Teuchos::ArrayRCP;
496 using Teuchos::ArrayView;
501 typedef LocalOrdinal LO;
502 typedef GlobalOrdinal GO;
505 typedef Import<LO, GO, NO> import_type;
506 typedef Map<LO, GO, NO> map_type;
509 typedef typename map_type::local_map_type local_map_type;
511 typedef typename KCRS::StaticCrsGraphType graph_t;
512 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
513 typedef typename NO::execution_space execution_space;
514 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
515 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
517#ifdef HAVE_TPETRA_MMM_TIMINGS
518 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
519 using Teuchos::TimeMonitor;
520 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
522 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
525 RCP<const import_type> Cimport = Ac.getGraph()->getImporter();
526 RCP<const map_type> Ccolmap = Ac.getColMap();
527 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
528 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
529 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
530 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
531 local_map_type Irowmap_local;
532 if (!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
533 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
534 local_map_type Icolmap_local;
535 if (!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
536 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
539 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Bcol2Ccol"), Pview.colMap->getLocalNumElements()), Icol2Ccol;
543 Kokkos::parallel_for(
544 range_type(0, Pview.origMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
545 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
548 if (!Pview.importMatrix.is_null()) {
549 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
550 std::runtime_error,
"Tpetra::MMM: Import setUnion messed with the DomainMap in an unfortunate way");
552 Kokkos::resize(Icol2Ccol, Pview.importMatrix->getColMap()->getLocalNumElements());
553 Kokkos::parallel_for(
554 range_type(0, Pview.importMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
555 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
561 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"), Aview.colMap->getLocalNumElements());
562 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"), Aview.colMap->getLocalNumElements());
563 Kokkos::parallel_for(
564 range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex() + 1), KOKKOS_LAMBDA(
const LO i) {
565 GO aidx = Acolmap_local.getGlobalElement(i);
566 LO B_LID = Prowmap_local.getLocalElement(aidx);
567 if (B_LID != LO_INVALID) {
568 targetMapToOrigRow(i) = B_LID;
569 targetMapToImportRow(i) = LO_INVALID;
571 LO I_LID = Irowmap_local.getLocalElement(aidx);
572 targetMapToOrigRow(i) = LO_INVALID;
573 targetMapToImportRow(i) = I_LID;
579 KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, lo_view_t>::
580 mult_R_A_P_reuse_kernel_wrapper(Rview, Aview, Pview,
581 targetMapToOrigRow, targetMapToImportRow, Bcol2Ccol, Icol2Ccol,
582 Ac, Cimport, label, params);
586template <
class Scalar,
590void mult_PT_A_P_newmatrix(
591 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
592 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
593 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
594 const std::string& label,
595 const Teuchos::RCP<Teuchos::ParameterList>& params) {
596 using Teuchos::Array;
597 using Teuchos::ArrayRCP;
598 using Teuchos::ArrayView;
603 typedef LocalOrdinal LO;
604 typedef GlobalOrdinal GO;
607 typedef Import<LO, GO, NO> import_type;
608 typedef Map<LO, GO, NO> map_type;
611 typedef typename map_type::local_map_type local_map_type;
613 typedef typename KCRS::StaticCrsGraphType graph_t;
614 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
615 typedef typename NO::execution_space execution_space;
616 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
617 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
619#ifdef HAVE_TPETRA_MMM_TIMINGS
620 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
621 using Teuchos::TimeMonitor;
622 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP M5 Cmap")))));
624 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
627 RCP<const import_type> Cimport;
628 RCP<const map_type> Ccolmap;
629 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
630 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
631 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
632 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
633 local_map_type Irowmap_local;
634 if (!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
635 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
636 local_map_type Icolmap_local;
637 if (!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
644 lo_view_t Pcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Pcol2Ccol"), Pview.colMap->getLocalNumElements()), Icol2Ccol;
646 if (Pview.importMatrix.is_null()) {
649 Ccolmap = Pview.colMap;
650 const LO colMapSize =
static_cast<LO
>(Pview.colMap->getLocalNumElements());
652 Kokkos::parallel_for(
653 "Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_fill",
654 Kokkos::RangePolicy<execution_space, LO>(0, colMapSize),
655 KOKKOS_LAMBDA(
const LO i) {
667 if (!Pimport.is_null() && !Iimport.is_null()) {
668 Cimport = Pimport->setUnion(*Iimport);
669 }
else if (!Pimport.is_null() && Iimport.is_null()) {
670 Cimport = Pimport->setUnion();
671 }
else if (Pimport.is_null() && !Iimport.is_null()) {
672 Cimport = Iimport->setUnion();
674 throw std::runtime_error(
"TpetraExt::RAP status of matrix importers is nonsensical");
676 Ccolmap = Cimport->getTargetMap();
681 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
682 std::runtime_error,
"Tpetra::RAP: Import setUnion messed with the DomainMap in an unfortunate way");
689 Kokkos::resize(Icol2Ccol, Pview.importMatrix->getColMap()->getLocalNumElements());
690 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
691 Kokkos::parallel_for(
692 "Tpetra::mult_R_A_P_newmatrix::Pcol2Ccol_getGlobalElement", range_type(0, Pview.origMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
693 Pcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
695 Kokkos::parallel_for(
696 "Tpetra::mult_R_A_P_newmatrix::Icol2Ccol_getGlobalElement", range_type(0, Pview.importMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
697 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
705 Ac.replaceColMap(Ccolmap);
723 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"), Aview.colMap->getLocalNumElements());
724 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"), Aview.colMap->getLocalNumElements());
726 Kokkos::parallel_for(
727 "Tpetra::mult_R_A_P_newmatrix::construct_tables", range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex() + 1), KOKKOS_LAMBDA(
const LO i) {
728 GO aidx = Acolmap_local.getGlobalElement(i);
729 LO P_LID = Prowmap_local.getLocalElement(aidx);
730 if (P_LID != LO_INVALID) {
731 targetMapToOrigRow(i) = P_LID;
732 targetMapToImportRow(i) = LO_INVALID;
734 LO I_LID = Irowmap_local.getLocalElement(aidx);
735 targetMapToOrigRow(i) = LO_INVALID;
736 targetMapToImportRow(i) = I_LID;
742 KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, lo_view_t>::
743 mult_PT_A_P_newmatrix_kernel_wrapper(Aview, Pview,
744 targetMapToOrigRow, targetMapToImportRow, Pcol2Ccol, Icol2Ccol,
745 Ac, Cimport, label, params);
749template <
class Scalar,
753void mult_PT_A_P_reuse(
754 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
755 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
756 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
757 const std::string& label,
758 const Teuchos::RCP<Teuchos::ParameterList>& params) {
759 using Teuchos::Array;
760 using Teuchos::ArrayRCP;
761 using Teuchos::ArrayView;
766 typedef LocalOrdinal LO;
767 typedef GlobalOrdinal GO;
770 typedef Import<LO, GO, NO> import_type;
771 typedef Map<LO, GO, NO> map_type;
774 typedef typename map_type::local_map_type local_map_type;
776 typedef typename KCRS::StaticCrsGraphType graph_t;
777 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
778 typedef typename NO::execution_space execution_space;
779 typedef Kokkos::RangePolicy<execution_space, size_t> range_type;
780 typedef Kokkos::View<LO*, typename lno_view_t::array_layout, typename lno_view_t::device_type> lo_view_t;
782#ifdef HAVE_TPETRA_MMM_TIMINGS
783 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
784 using Teuchos::TimeMonitor;
785 RCP<TimeMonitor> MM = rcp(
new TimeMonitor(*(TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP M5 Cmap")))));
787 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
790 RCP<const import_type> Cimport = Ac.getGraph()->getImporter();
791 RCP<const map_type> Ccolmap = Ac.getColMap();
792 RCP<const import_type> Pimport = Pview.origMatrix->getGraph()->getImporter();
793 RCP<const import_type> Iimport = Pview.importMatrix.is_null() ? Teuchos::null : Pview.importMatrix->getGraph()->getImporter();
794 local_map_type Acolmap_local = Aview.colMap->getLocalMap();
795 local_map_type Prowmap_local = Pview.origMatrix->getRowMap()->getLocalMap();
796 local_map_type Irowmap_local;
797 if (!Pview.importMatrix.is_null()) Irowmap_local = Pview.importMatrix->getRowMap()->getLocalMap();
798 local_map_type Pcolmap_local = Pview.origMatrix->getColMap()->getLocalMap();
799 local_map_type Icolmap_local;
800 if (!Pview.importMatrix.is_null()) Icolmap_local = Pview.importMatrix->getColMap()->getLocalMap();
801 local_map_type Ccolmap_local = Ccolmap->getLocalMap();
804 lo_view_t Bcol2Ccol(Kokkos::ViewAllocateWithoutInitializing(
"Bcol2Ccol"), Pview.colMap->getLocalNumElements()), Icol2Ccol;
808 Kokkos::parallel_for(
809 range_type(0, Pview.origMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
810 Bcol2Ccol(i) = Ccolmap_local.getLocalElement(Pcolmap_local.getGlobalElement(i));
813 if (!Pview.importMatrix.is_null()) {
814 TEUCHOS_TEST_FOR_EXCEPTION(!Cimport->getSourceMap()->isSameAs(*Pview.origMatrix->getDomainMap()),
815 std::runtime_error,
"Tpetra::MMM: Import setUnion messed with the DomainMap in an unfortunate way");
817 Kokkos::resize(Icol2Ccol, Pview.importMatrix->getColMap()->getLocalNumElements());
818 Kokkos::parallel_for(
819 range_type(0, Pview.importMatrix->getColMap()->getLocalNumElements()), KOKKOS_LAMBDA(
const LO i) {
820 Icol2Ccol(i) = Ccolmap_local.getLocalElement(Icolmap_local.getGlobalElement(i));
826 lo_view_t targetMapToOrigRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToOrigRow"), Aview.colMap->getLocalNumElements());
827 lo_view_t targetMapToImportRow(Kokkos::ViewAllocateWithoutInitializing(
"targetMapToImportRow"), Aview.colMap->getLocalNumElements());
828 Kokkos::parallel_for(
829 range_type(Aview.colMap->getMinLocalIndex(), Aview.colMap->getMaxLocalIndex() + 1), KOKKOS_LAMBDA(
const LO i) {
830 GO aidx = Acolmap_local.getGlobalElement(i);
831 LO B_LID = Prowmap_local.getLocalElement(aidx);
832 if (B_LID != LO_INVALID) {
833 targetMapToOrigRow(i) = B_LID;
834 targetMapToImportRow(i) = LO_INVALID;
836 LO I_LID = Irowmap_local.getLocalElement(aidx);
837 targetMapToOrigRow(i) = LO_INVALID;
838 targetMapToImportRow(i) = I_LID;
844 KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, lo_view_t>::
845 mult_PT_A_P_reuse_kernel_wrapper(Aview, Pview,
846 targetMapToOrigRow, targetMapToImportRow, Bcol2Ccol, Icol2Ccol,
847 Ac, Cimport, label, params);
853template <
class Scalar,
857 class LocalOrdinalViewType>
858void KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalOrdinalViewType>::mult_R_A_P_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
859 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
860 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
861 const LocalOrdinalViewType& Acol2Prow_dev,
862 const LocalOrdinalViewType& Acol2PIrow_dev,
863 const LocalOrdinalViewType& Pcol2Accol_dev,
864 const LocalOrdinalViewType& PIcol2Accol_dev,
865 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
866 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Node> > Acimport,
867 const std::string& label,
868 const Teuchos::RCP<Teuchos::ParameterList>& params) {
869#ifdef HAVE_TPETRA_MMM_TIMINGS
870 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
871 using Teuchos::TimeMonitor;
872 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix SerialCore"))));
875 using Teuchos::Array;
876 using Teuchos::ArrayRCP;
877 using Teuchos::ArrayView;
883 typedef typename KCRS::StaticCrsGraphType graph_t;
884 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
885 typedef typename graph_t::row_map_type::non_const_type lno_view_t;
886 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
887 typedef typename KCRS::values_type::non_const_type scalar_view_t;
890 typedef LocalOrdinal LO;
891 typedef GlobalOrdinal GO;
893 typedef Map<LO, GO, NO> map_type;
894 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
895 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
896 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
899 RCP<const map_type> Accolmap = Ac.getColMap();
900 size_t m = Rview.origMatrix->getLocalNumRows();
901 size_t n = Accolmap->getLocalNumElements();
902 size_t p_max_nnz_per_row = Pview.origMatrix->getLocalMaxNumRowEntries();
905 auto Acol2Prow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
907 auto Acol2PIrow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
909 auto Pcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
911 auto PIcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
915 const auto Amat = Aview.origMatrix->getLocalMatrixHost();
916 const auto Pmat = Pview.origMatrix->getLocalMatrixHost();
917 const auto Rmat = Rview.origMatrix->getLocalMatrixHost();
919 auto Arowptr = Amat.graph.row_map;
920 auto Prowptr = Pmat.graph.row_map;
921 auto Rrowptr = Rmat.graph.row_map;
922 const auto Acolind = Amat.graph.entries;
923 const auto Pcolind = Pmat.graph.entries;
924 const auto Rcolind = Rmat.graph.entries;
925 const auto Avals = Amat.values;
926 const auto Pvals = Pmat.values;
927 const auto Rvals = Rmat.values;
929 typename c_lno_view_t::host_mirror_type::const_type Irowptr;
930 typename lno_nnz_view_t::host_mirror_type Icolind;
931 typename scalar_view_t::host_mirror_type Ivals;
932 if (!Pview.importMatrix.is_null()) {
933 auto lclP = Pview.importMatrix->getLocalMatrixHost();
934 Irowptr = lclP.graph.row_map;
935 Icolind = lclP.graph.entries;
937 p_max_nnz_per_row = std::max(p_max_nnz_per_row, Pview.importMatrix->getLocalMaxNumRowEntries());
940#ifdef HAVE_TPETRA_MMM_TIMINGS
941 RCP<TimeMonitor> MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix SerialCore - Compare"))));
951 size_t CSR_alloc = std::max(C_estimate_nnz(*Aview.origMatrix, *Pview.origMatrix), n);
952 typename lno_view_t::host_mirror_type Crowptr(Kokkos::ViewAllocateWithoutInitializing(
"Crowptr"), m + 1);
953 typename lno_nnz_view_t::host_mirror_type Ccolind(Kokkos::ViewAllocateWithoutInitializing(
"Ccolind"), CSR_alloc);
954 typename scalar_view_t::host_mirror_type Cvals(Kokkos::ViewAllocateWithoutInitializing(
"Cvals"), CSR_alloc);
964 const size_t INVALID = Teuchos::OrdinalTraits<size_t>::invalid();
965 Array<size_t> ac_status(n, ST_INVALID);
975 size_t nnz = 0, nnz_old = 0;
976 for (
size_t i = 0; i < m; i++) {
982 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i + 1]; kk++) {
984 const SC Rik = Rvals[kk];
988 for (
size_t ll = Arowptr[k]; ll < Arowptr[k + 1]; ll++) {
990 const SC Akl = Avals[ll];
994 if (Acol2Prow[l] != LO_INVALID) {
1001 size_t Pl = Teuchos::as<size_t>(Acol2Prow[l]);
1004 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl + 1]; jj++) {
1006 LO Acj = Pcol2Accol[j];
1009 if (ac_status[Acj] == INVALID || ac_status[Acj] < nnz_old) {
1010#ifdef HAVE_TPETRA_DEBUG
1012 TEUCHOS_TEST_FOR_EXCEPTION(nnz >= Teuchos::as<size_t>(Ccolind.size()),
1014 label <<
" ERROR, not enough memory allocated for matrix product. Allocated: " << Ccolind.extent(0) << std::endl);
1017 ac_status[Acj] = nnz;
1019 Cvals[nnz] = Rik * Akl * Plj;
1022 Cvals[ac_status[Acj]] += Rik * Akl * Plj;
1032 size_t Il = Teuchos::as<size_t>(Acol2PIrow[l]);
1033 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il + 1]; jj++) {
1035 LO Acj = PIcol2Accol[j];
1038 if (ac_status[Acj] == INVALID || ac_status[Acj] < nnz_old) {
1039#ifdef HAVE_TPETRA_DEBUG
1041 TEUCHOS_TEST_FOR_EXCEPTION(nnz >= Teuchos::as<size_t>(Ccolind.size()),
1043 label <<
" ERROR, not enough memory allocated for matrix product. Allocated: " << Ccolind.extent(0) << std::endl);
1046 ac_status[Acj] = nnz;
1048 Cvals[nnz] = Rik * Akl * Plj;
1051 Cvals[ac_status[Acj]] += Rik * Akl * Plj;
1058 if (nnz + n > CSR_alloc) {
1060 Kokkos::resize(Ccolind, CSR_alloc);
1061 Kokkos::resize(Cvals, CSR_alloc);
1069 Kokkos::resize(Ccolind, nnz);
1070 Kokkos::resize(Cvals, nnz);
1072#ifdef HAVE_TPETRA_MMM_TIMINGS
1074 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix Final Sort"))));
1076 auto Crowptr_dev = Kokkos::create_mirror_view_and_copy(
1077 typename KCRS::device_type(), Crowptr);
1078 auto Ccolind_dev = Kokkos::create_mirror_view_and_copy(
1079 typename KCRS::device_type(), Ccolind);
1080 auto Cvals_dev = Kokkos::create_mirror_view_and_copy(
1081 typename KCRS::device_type(), Cvals);
1084 if (params.is_null() || params->get(
"sort entries",
true))
1085 Import_Util::sortCrsEntries(Crowptr_dev, Ccolind_dev, Cvals_dev,
1086 ::KokkosSparse::SortAlgorithm::DEFAULT);
1087 Ac.setAllValues(Crowptr_dev, Ccolind_dev, Cvals_dev);
1089#ifdef HAVE_TPETRA_MMM_TIMINGS
1091 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Newmatrix ESFC"))));
1102 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
1103 labelList->set(
"Timer Label", label);
1104 if (!params.is_null()) labelList->set(
"compute global constants", params->get(
"compute global constants",
true));
1105 RCP<const Export<LO, GO, NO> > dummyExport;
1106 Ac.expertStaticFillComplete(Pview.origMatrix->getDomainMap(),
1107 Rview.origMatrix->getRangeMap(),
1116template <
class Scalar,
1118 class GlobalOrdinal,
1120 class LocalOrdinalViewType>
1121void KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalOrdinalViewType>::mult_R_A_P_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Rview,
1122 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1123 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1124 const LocalOrdinalViewType& Acol2Prow_dev,
1125 const LocalOrdinalViewType& Acol2PIrow_dev,
1126 const LocalOrdinalViewType& Pcol2Accol_dev,
1127 const LocalOrdinalViewType& PIcol2Accol_dev,
1128 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1129 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Node> > Acimport,
1130 const std::string& label,
1131 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1132#ifdef HAVE_TPETRA_MMM_TIMINGS
1133 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1134 using Teuchos::TimeMonitor;
1135 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse SerialCore"))));
1138 using Teuchos::Array;
1139 using Teuchos::ArrayRCP;
1140 using Teuchos::ArrayView;
1145 typedef typename Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::local_matrix_host_type KCRS;
1146 typedef typename KCRS::StaticCrsGraphType graph_t;
1147 typedef typename graph_t::row_map_type::const_type c_lno_view_t;
1148 typedef typename graph_t::entries_type::non_const_type lno_nnz_view_t;
1149 typedef typename KCRS::values_type::non_const_type scalar_view_t;
1152 typedef LocalOrdinal LO;
1153 typedef GlobalOrdinal GO;
1155 typedef Map<LO, GO, NO> map_type;
1156 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1157 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1158 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
1161 RCP<const map_type> Accolmap = Ac.getColMap();
1162 size_t m = Rview.origMatrix->getLocalNumRows();
1163 size_t n = Accolmap->getLocalNumElements();
1164 size_t p_max_nnz_per_row = Pview.origMatrix->getLocalMaxNumRowEntries();
1167 auto Acol2Prow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1169 auto Acol2PIrow = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1171 auto Pcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1173 auto PIcol2Accol = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
1177 const KCRS Amat = Aview.origMatrix->getLocalMatrixHost();
1178 const KCRS Pmat = Pview.origMatrix->getLocalMatrixHost();
1179 const KCRS Rmat = Rview.origMatrix->getLocalMatrixHost();
1180 const KCRS Cmat = Ac.getLocalMatrixHost();
1182 c_lno_view_t Arowptr = Amat.graph.row_map, Prowptr = Pmat.graph.row_map, Rrowptr = Rmat.graph.row_map, Crowptr = Cmat.graph.row_map;
1183 const lno_nnz_view_t Acolind = Amat.graph.entries, Pcolind = Pmat.graph.entries, Rcolind = Rmat.graph.entries, Ccolind = Cmat.graph.entries;
1184 const scalar_view_t Avals = Amat.values, Pvals = Pmat.values, Rvals = Rmat.values;
1185 scalar_view_t Cvals = Cmat.values;
1187 c_lno_view_t Irowptr;
1188 lno_nnz_view_t Icolind;
1189 scalar_view_t Ivals;
1190 if (!Pview.importMatrix.is_null()) {
1191 auto lclP = Pview.importMatrix->getLocalMatrixHost();
1192 Irowptr = lclP.graph.row_map;
1193 Icolind = lclP.graph.entries;
1194 Ivals = lclP.values;
1195 p_max_nnz_per_row = std::max(p_max_nnz_per_row, Pview.importMatrix->getLocalMaxNumRowEntries());
1198#ifdef HAVE_TPETRA_MMM_TIMINGS
1199 RCP<TimeMonitor> MM2 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse SerialCore - Compare"))));
1210 Array<size_t> ac_status(n, ST_INVALID);
1224 size_t OLD_ip = 0, CSR_ip = 0;
1225 for (
size_t i = 0; i < m; i++) {
1228 OLD_ip = Crowptr[i];
1229 CSR_ip = Crowptr[i + 1];
1230 for (
size_t k = OLD_ip; k < CSR_ip; k++) {
1231 ac_status[Ccolind[k]] = k;
1238 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i + 1]; kk++) {
1240 const SC Rik = Rvals[kk];
1244 for (
size_t ll = Arowptr[k]; ll < Arowptr[k + 1]; ll++) {
1246 const SC Akl = Avals[ll];
1250 if (Acol2Prow[l] != LO_INVALID) {
1257 size_t Pl = Teuchos::as<size_t>(Acol2Prow[l]);
1260 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl + 1]; jj++) {
1262 LO Cij = Pcol2Accol[j];
1265 TEUCHOS_TEST_FOR_EXCEPTION(ac_status[Cij] < OLD_ip || ac_status[Cij] >= CSR_ip,
1266 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph "
1267 <<
"(c_status = " << ac_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
1269 Cvals[ac_status[Cij]] += Rik * Akl * Plj;
1278 size_t Il = Teuchos::as<size_t>(Acol2PIrow[l]);
1279 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il + 1]; jj++) {
1281 LO Cij = PIcol2Accol[j];
1284 TEUCHOS_TEST_FOR_EXCEPTION(ac_status[Cij] < OLD_ip || ac_status[Cij] >= CSR_ip,
1285 std::runtime_error,
"Trying to insert a new entry (" << i <<
"," << Cij <<
") into a static graph "
1286 <<
"(c_status = " << ac_status[Cij] <<
" of [" << OLD_ip <<
"," << CSR_ip <<
"))");
1288 Cvals[ac_status[Cij]] += Rik * Akl * Plj;
1295#ifdef HAVE_TPETRA_MMM_TIMINGS
1296 auto MM3 = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"RAP Reuse ESFC"))));
1299 Ac.fillComplete(Ac.getDomainMap(), Ac.getRangeMap());
1305template <
class Scalar,
1307 class GlobalOrdinal,
1309 class LocalOrdinalViewType>
1310void KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalOrdinalViewType>::mult_PT_A_P_newmatrix_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1311 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1312 const LocalOrdinalViewType& Acol2Prow,
1313 const LocalOrdinalViewType& Acol2PIrow,
1314 const LocalOrdinalViewType& Pcol2Accol,
1315 const LocalOrdinalViewType& PIcol2Accol,
1316 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1317 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Node> > Acimport,
1318 const std::string& label,
1319 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1320#ifdef HAVE_TPETRA_MMM_TIMINGS
1321 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1322 using Teuchos::TimeMonitor;
1323 Teuchos::TimeMonitor MM(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose")));
1327 typedef RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node> transposer_type;
1328 transposer_type transposer(Pview.origMatrix, label + std::string(
"XP: "));
1330 using Teuchos::ParameterList;
1332 RCP<ParameterList> transposeParams(
new ParameterList);
1333 transposeParams->set(
"sort",
false);
1335 if (!params.is_null()) {
1336 transposeParams->set(
"compute global constants",
1337 params->get(
"compute global constants: temporaries",
1340 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1341 transposer.createTransposeLocal(transposeParams);
1342 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Rview;
1343 Rview.origMatrix = Ptrans;
1345 mult_R_A_P_newmatrix_kernel_wrapper(Rview, Aview, Pview, Acol2Prow, Acol2PIrow, Pcol2Accol, PIcol2Accol, Ac, Acimport, label, params);
1351template <
class Scalar,
1353 class GlobalOrdinal,
1355 class LocalOrdinalViewType>
1356void KernelWrappers3<Scalar, LocalOrdinal, GlobalOrdinal, Node, LocalOrdinalViewType>::mult_PT_A_P_reuse_kernel_wrapper(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1357 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1358 const LocalOrdinalViewType& Acol2Prow,
1359 const LocalOrdinalViewType& Acol2PIrow,
1360 const LocalOrdinalViewType& Pcol2Accol,
1361 const LocalOrdinalViewType& PIcol2Accol,
1362 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1363 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Node> > Acimport,
1364 const std::string& label,
1365 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1366#ifdef HAVE_TPETRA_MMM_TIMINGS
1367 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1368 using Teuchos::TimeMonitor;
1369 Teuchos::TimeMonitor MM(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose")));
1373 typedef RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node> transposer_type;
1374 transposer_type transposer(Pview.origMatrix, label + std::string(
"XP: "));
1376 using Teuchos::ParameterList;
1378 RCP<ParameterList> transposeParams(
new ParameterList);
1379 transposeParams->set(
"sort",
false);
1381 if (!params.is_null()) {
1382 transposeParams->set(
"compute global constants",
1383 params->get(
"compute global constants: temporaries",
1386 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1387 transposer.createTransposeLocal(transposeParams);
1388 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node> Rview;
1389 Rview.origMatrix = Ptrans;
1391 mult_R_A_P_reuse_kernel_wrapper(Rview, Aview, Pview, Acol2Prow, Acol2PIrow, Pcol2Accol, PIcol2Accol, Ac, Acimport, label, params);
1399template <
class Scalar,
1401 class GlobalOrdinal,
1403void KernelWrappers3MMM<Scalar, LocalOrdinal, GlobalOrdinal, Node>::mult_PT_A_P_newmatrix_kernel_wrapper_2pass(CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Aview,
1404 CrsMatrixStruct<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Pview,
1405 const Teuchos::Array<LocalOrdinal>& Acol2PRow,
1406 const Teuchos::Array<LocalOrdinal>& Acol2PRowImport,
1407 const Teuchos::Array<LocalOrdinal>& Pcol2Accol,
1408 const Teuchos::Array<LocalOrdinal>& PIcol2Accol,
1409 CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Ac,
1410 Teuchos::RCP<
const Import<LocalOrdinal, GlobalOrdinal, Node> > Acimport,
1411 const std::string& label,
1412 const Teuchos::RCP<Teuchos::ParameterList>& params) {
1413#ifdef HAVE_TPETRA_MMM_TIMINGS
1414 std::string prefix_mmm = std::string(
"TpetraExt ") + label + std::string(
": ");
1415 using Teuchos::TimeMonitor;
1416 Teuchos::RCP<Teuchos::TimeMonitor> MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix SerialCore"))));
1419 using Teuchos::Array;
1420 using Teuchos::ArrayRCP;
1421 using Teuchos::ArrayView;
1426 typedef LocalOrdinal LO;
1427 typedef GlobalOrdinal GO;
1429 typedef RowMatrixTransposer<SC, LO, GO, NO> transposer_type;
1430 const LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1431 const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero();
1436 size_t n = Ac.getRowMap()->getLocalNumElements();
1437 LO maxAccol = Ac.getColMap()->getMaxLocalIndex();
1440 ArrayRCP<size_t> Acrowptr_RCP;
1441 ArrayRCP<LO> Accolind_RCP;
1442 ArrayRCP<SC> Acvals_RCP;
1449 auto Arowptr = Aview.origMatrix->getLocalRowPtrsHost();
1450 auto Acolind = Aview.origMatrix->getLocalIndicesHost();
1451 auto Avals = Aview.origMatrix->getLocalValuesHost(
1452 Tpetra::Access::ReadOnly);
1453 auto Prowptr = Pview.origMatrix->getLocalRowPtrsHost();
1454 auto Pcolind = Pview.origMatrix->getLocalIndicesHost();
1455 auto Pvals = Pview.origMatrix->getLocalValuesHost(
1456 Tpetra::Access::ReadOnly);
1457 decltype(Prowptr) Irowptr;
1458 decltype(Pcolind) Icolind;
1459 decltype(Pvals) Ivals;
1461 if (!Pview.importMatrix.is_null()) {
1462 Irowptr = Pview.importMatrix->getLocalRowPtrsHost();
1463 Icolind = Pview.importMatrix->getLocalIndicesHost();
1464 Ivals = Pview.importMatrix->getLocalValuesHost(
1465 Tpetra::Access::ReadOnly);
1473 ArrayView<size_t> Acrowptr;
1474 ArrayView<LO> Accolind;
1475 ArrayView<SC> Acvals;
1486#ifdef HAVE_TPETRA_MMM_TIMINGS
1487 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP local transpose"))));
1494 transposer_type transposer(Pview.origMatrix, label + std::string(
"XP: "));
1496 using Teuchos::ParameterList;
1497 RCP<ParameterList> transposeParams(
new ParameterList);
1498 transposeParams->set(
"sort",
false);
1499 if (!params.is_null()) {
1500 transposeParams->set(
"compute global constants",
1501 params->get(
"compute global constants: temporaries",
1504 RCP<CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Ptrans =
1505 transposer.createTransposeLocal(transposeParams);
1507 auto Rrowptr = Ptrans->getLocalRowPtrsHost();
1508 auto Rcolind = Ptrans->getLocalIndicesHost();
1509 auto Rvals = Ptrans->getLocalValuesHost(Tpetra::Access::ReadOnly);
1514#ifdef HAVE_TPETRA_MMM_TIMINGS
1515 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP graph"))));
1518 const size_t ST_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
1519 Array<size_t> ac_status(maxAccol + 1, ST_INVALID);
1521 size_t nnz_alloc = std::max(Ac_estimate_nnz(*Aview.origMatrix, *Pview.origMatrix), n);
1522 size_t nnzPerRowA = 100;
1523 if (Aview.origMatrix->getLocalNumEntries() > 0)
1524 nnzPerRowA = Aview.origMatrix->getLocalNumEntries() / Aview.origMatrix->getLocalNumRows();
1525 Acrowptr_RCP.resize(n + 1);
1526 Acrowptr = Acrowptr_RCP();
1527 Accolind_RCP.resize(nnz_alloc);
1528 Accolind = Accolind_RCP();
1530 size_t nnz = 0, nnz_old = 0;
1531 for (
size_t i = 0; i < n; i++) {
1537 for (
size_t kk = Rrowptr[i]; kk < Rrowptr[i + 1]; kk++) {
1540 for (
size_t ll = Arowptr[k]; ll < Arowptr[k + 1]; ll++) {
1543 if (Acol2PRow[l] != LO_INVALID) {
1550 size_t Pl = Teuchos::as<size_t>(Acol2PRow[l]);
1553 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl + 1]; jj++) {
1555 LO Acj = Pcol2Accol[j];
1557 if (ac_status[Acj] == ST_INVALID || ac_status[Acj] < nnz_old) {
1559 ac_status[Acj] = nnz;
1560 Accolind[nnz] = Acj;
1571 size_t Il = Teuchos::as<size_t>(Acol2PRowImport[l]);
1572 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il + 1]; jj++) {
1574 LO Acj = PIcol2Accol[j];
1576 if (ac_status[Acj] == ST_INVALID || ac_status[Acj] < nnz_old) {
1578 ac_status[Acj] = nnz;
1579 Accolind[nnz] = Acj;
1589 if (nnz + std::max(5 * nnzPerRowA, n) > nnz_alloc) {
1591 nnz_alloc = std::max(nnz_alloc, nnz + std::max(5 * nnzPerRowA, n));
1592 Accolind_RCP.resize(nnz_alloc);
1593 Accolind = Accolind_RCP();
1594 Acvals_RCP.resize(nnz_alloc);
1595 Acvals = Acvals_RCP();
1602 Accolind_RCP.resize(nnz);
1603 Accolind = Accolind_RCP();
1606 Acvals_RCP.resize(nnz, SC_ZERO);
1607 Acvals = Acvals_RCP();
1613#ifdef HAVE_TPETRA_MMM_TIMINGS
1614 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix Fill Matrix"))));
1617 for (
size_t k = 0; k < n; k++) {
1618 for (
size_t ii = Prowptr[k]; ii < Prowptr[k + 1]; ii++) {
1620 const SC Pki = Pvals[ii];
1621 for (
size_t ll = Arowptr[k]; ll < Arowptr[k + 1]; ll++) {
1623 const SC Akl = Avals[ll];
1626 if (Acol2PRow[l] != LO_INVALID) {
1633 size_t Pl = Teuchos::as<size_t>(Acol2PRow[l]);
1634 for (
size_t jj = Prowptr[Pl]; jj < Prowptr[Pl + 1]; jj++) {
1636 LO Acj = Pcol2Accol[j];
1638 for (pp = Acrowptr[i]; pp < Acrowptr[i + 1]; pp++)
1639 if (Accolind[pp] == Acj)
1643 Acvals[pp] += Pki * Akl * Pvals[jj];
1652 size_t Il = Teuchos::as<size_t>(Acol2PRowImport[l]);
1653 for (
size_t jj = Irowptr[Il]; jj < Irowptr[Il + 1]; jj++) {
1655 LO Acj = PIcol2Accol[j];
1657 for (pp = Acrowptr[i]; pp < Acrowptr[i + 1]; pp++)
1658 if (Accolind[pp] == Acj)
1662 Acvals[pp] += Pki * Akl * Ivals[jj];
1669#ifdef HAVE_TPETRA_MMM_TIMINGS
1670 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP sort"))));
1677 Import_Util::sortCrsEntries(Acrowptr_RCP(), Accolind_RCP(), Acvals_RCP(),
1678 ::KokkosSparse::SortAlgorithm::SHELL);
1681 Ac.setAllValues(Acrowptr_RCP, Accolind_RCP, Acvals_RCP);
1683#ifdef HAVE_TPETRA_MMM_TIMINGS
1684 MM = rcp(
new TimeMonitor(*TimeMonitor::getNewTimer(prefix_mmm + std::string(
"PTAP Newmatrix ESFC"))));
1695 RCP<Teuchos::ParameterList> labelList = rcp(
new Teuchos::ParameterList);
1696 labelList->set(
"Timer Label", label);
1698 if (!params.is_null()) labelList->set(
"compute global constants", params->get(
"compute global constants",
true));
1699 RCP<const Export<LO, GO, NO> > dummyExport;
1700 Ac.expertStaticFillComplete(Pview.origMatrix->getDomainMap(),
1701 Pview.origMatrix->getDomainMap(),
1703 dummyExport, labelList);
1715#define TPETRA_TRIPLEMATRIXMULTIPLY_INSTANT(SCALAR, LO, GO, NODE) \
1717 template void TripleMatrixMultiply::MultiplyRAP( \
1718 const CrsMatrix<SCALAR, LO, GO, NODE>& R, \
1720 const CrsMatrix<SCALAR, LO, GO, NODE>& A, \
1722 const CrsMatrix<SCALAR, LO, GO, NODE>& P, \
1724 CrsMatrix<SCALAR, LO, GO, NODE>& Ac, \
1725 bool call_FillComplete_on_result, \
1726 const std::string& label, \
1727 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.
KokkosSparse::CrsMatrix< impl_scalar_type, local_ordinal_type, device_type, void, typename local_graph_device_type::size_type > local_matrix_device_type
The specialization of Kokkos::CrsMatrix that represents the part of the sparse matrix on each MPI pro...
Struct that holds views of the contents of a CrsMatrix.
static int TAFC_OptimizationCoreCount()
MPI process count above which Tpetra::CrsMatrix::transferAndFillComplete will attempt to do advanced ...
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.