1 #ifndef TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
2 #define TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
4 #include "Tpetra_CrsMatrix.hpp"
5 #include "Tpetra_Map.hpp"
6 #include "Tpetra_RowMatrix.hpp"
7 #include "Tpetra_Details_localDeepCopyRowMatrix.hpp"
8 #include "Teuchos_Array.hpp"
9 #include "Teuchos_ArrayView.hpp"
16 template<
class SC,
class LO,
class GO,
class NT>
17 typename CrsMatrix<SC, LO, GO, NT>::local_matrix_type
18 localDeepCopyFillCompleteCrsMatrix (
const CrsMatrix<SC, LO, GO, NT>& A)
20 using Kokkos::view_alloc;
21 using Kokkos::WithoutInitializing;
22 using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
23 using local_matrix_type =
24 typename crs_matrix_type::local_matrix_type;
25 local_matrix_type A_lcl = A.getLocalMatrix ();
27 using local_graph_type =
typename crs_matrix_type::local_graph_type;
28 using inds_type =
typename local_graph_type::entries_type;
29 inds_type ind (view_alloc (
"ind", WithoutInitializing),
30 A_lcl.graph.entries.extent (0));
34 typename local_graph_type::row_map_type::non_const_type;
35 offsets_type ptr (view_alloc (
"ptr", WithoutInitializing),
36 A_lcl.graph.row_map.extent (0));
39 using values_type =
typename local_matrix_type::values_type;
40 values_type val (view_alloc (
"val", WithoutInitializing),
41 A_lcl.values.extent (0));
44 local_graph_type lclGraph (ind, ptr);
45 const size_t numCols = A.getColMap ()->getNodeNumElements ();
46 return local_matrix_type (A.getObjectLabel (), numCols, val, lclGraph);
51 template<
class SC,
class LO,
class GO,
class NT>
52 CrsMatrix<SC, LO, GO, NT>
53 createDeepCopy (
const RowMatrix<SC, LO, GO, NT>& A)
55 using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
56 const crs_matrix_type* A_crs =
57 dynamic_cast<const crs_matrix_type*
> (&A);
59 if (A_crs !=
nullptr && A_crs->isFillComplete ()) {
60 auto A_lcl = localDeepCopyFillCompleteCrsMatrix (*A_crs);
61 auto G = A_crs->getCrsGraph ();
62 return crs_matrix_type (A_lcl, A_crs->getRowMap (),
64 A_crs->getDomainMap (),
65 A_crs->getRangeMap (),
69 else if (A.isGloballyIndexed ()) {
70 const LO lclNumRows (A.getNodeNumRows ());
72 std::unique_ptr<size_t[]> entPerRow (
new size_t [lclNumRows]);
74 for (LO i = 0; i < lclNumRows; ++i) {
75 const size_t lclNumEnt = A.getNumEntriesInLocalRow (i);
76 entPerRow[i] = lclNumEnt;
77 maxNumEnt = maxNumEnt < lclNumEnt ? lclNumEnt : maxNumEnt;
80 Teuchos::ArrayView<const size_t> entPerRow_av
81 (entPerRow.get (), lclNumRows);
83 const bool hasColMap =
84 A.hasColMap () && ! A.getColMap ().is_null ();
86 crs_matrix_type A_copy = hasColMap ?
87 crs_matrix_type (A.getRowMap (), A.getColMap (),
89 crs_matrix_type (A.getRowMap (), entPerRow_av);
91 const bool hasViews = A.supportsRowViews ();
93 Teuchos::Array<GO> inputIndsBuf;
94 Teuchos::Array<SC> inputValsBuf;
96 inputIndsBuf.resize (maxNumEnt);
97 inputValsBuf.resize (maxNumEnt);
100 const auto& rowMap = * (A.getRowMap ());
101 for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
102 const GO gblRow = rowMap.getGlobalElement (lclRow);
103 Teuchos::ArrayView<const GO> inputInds_av;
104 Teuchos::ArrayView<const SC> inputVals_av;
107 A.getGlobalRowView (gblRow, inputInds_av, inputVals_av);
108 numEnt =
static_cast<size_t> (inputInds_av.size ());
111 const size_t lclNumEnt = A.getNumEntriesInLocalRow (lclRow);
112 TEUCHOS_ASSERT(lclNumEnt <= maxNumEnt);
113 A.getGlobalRowCopy (gblRow, inputIndsBuf (),
114 inputValsBuf (), numEnt);
115 inputInds_av = inputIndsBuf.view (0, numEnt);
116 inputVals_av = inputValsBuf.view (0, numEnt);
118 A_copy.insertGlobalValues (gblRow, inputInds_av, inputVals_av);
121 if (A.isFillComplete ()) {
122 A_copy.fillComplete (A.getDomainMap (), A.getRangeMap ());
130 Teuchos::RCP<const Export<LO, GO, NT>> exp;
131 Teuchos::RCP<const Import<LO, GO, NT>> imp;
132 auto G = A.getGraph ();
133 if (! G.is_null ()) {
134 imp = G->getImporter ();
135 exp = G->getExporter ();
137 if (! imp.is_null () || ! exp.is_null ()) {
138 return crs_matrix_type (A_lcl, A.getRowMap (),
145 return crs_matrix_type (A_lcl, A.getRowMap (),
161 #define TPETRA_CREATEDEEPCOPY_CRSMATRIX_INSTANT(SC, LO, GO, NT) \
162 template CrsMatrix< SC , LO , GO , NT > \
163 createDeepCopy (const RowMatrix<SC, LO, GO, NT>& );
165 #endif // TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
KokkosSparse::CrsMatrix< typename Kokkos::ArithTraits< SC >::val_type, LO, typename NT::execution_space, void > localDeepCopyLocallyIndexedRowMatrix(const RowMatrix< SC, LO, GO, NT > &A, const char label[])
Deep copy of A's local sparse matrix.