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 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
88 crs_matrix_type (A.getRowMap (), A.getColMap (),
90 Tpetra::StaticProfile) :
91 crs_matrix_type (A.getRowMap (), entPerRow_av,
92 Tpetra::StaticProfile);
93 #else // TPETRA_ENABLE_DEPRECATED_CODE
94 crs_matrix_type (A.getRowMap (), A.getColMap (),
96 crs_matrix_type (A.getRowMap (), entPerRow_av);
97 #endif // TPETRA_ENABLE_DEPRECATED_CODE
99 const bool hasViews = A.supportsRowViews ();
101 Teuchos::Array<GO> inputIndsBuf;
102 Teuchos::Array<SC> inputValsBuf;
104 inputIndsBuf.resize (maxNumEnt);
105 inputValsBuf.resize (maxNumEnt);
108 const auto& rowMap = * (A.getRowMap ());
109 for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
110 const GO gblRow = rowMap.getGlobalElement (lclRow);
111 Teuchos::ArrayView<const GO> inputInds_av;
112 Teuchos::ArrayView<const SC> inputVals_av;
115 A.getGlobalRowView (gblRow, inputInds_av, inputVals_av);
116 numEnt =
static_cast<size_t> (inputInds_av.size ());
119 const size_t lclNumEnt = A.getNumEntriesInLocalRow (lclRow);
120 TEUCHOS_ASSERT(lclNumEnt <= maxNumEnt);
121 A.getGlobalRowCopy (gblRow, inputIndsBuf (),
122 inputValsBuf (), numEnt);
123 inputInds_av = inputIndsBuf.view (0, numEnt);
124 inputVals_av = inputValsBuf.view (0, numEnt);
126 A_copy.insertGlobalValues (gblRow, inputInds_av, inputVals_av);
129 if (A.isFillComplete ()) {
130 A_copy.fillComplete (A.getDomainMap (), A.getRangeMap ());
138 Teuchos::RCP<const Export<LO, GO, NT>> exp;
139 Teuchos::RCP<const Import<LO, GO, NT>> imp;
140 auto G = A.getGraph ();
141 if (! G.is_null ()) {
142 imp = G->getImporter ();
143 exp = G->getExporter ();
145 if (! imp.is_null () || ! exp.is_null ()) {
146 return crs_matrix_type (A_lcl, A.getRowMap (),
153 return crs_matrix_type (A_lcl, A.getRowMap (),
169 #define TPETRA_CREATEDEEPCOPY_CRSMATRIX_INSTANT(SC, LO, GO, NT) \
170 template CrsMatrix< SC , LO , GO , NT > \
171 createDeepCopy (const RowMatrix<SC, LO, GO, NT>& );
173 #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.