1 #ifndef TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
2 #define TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
4 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
6 #include "Tpetra_CrsMatrix.hpp"
7 #include "Tpetra_Map.hpp"
8 #include "Tpetra_RowMatrix.hpp"
9 #include "Tpetra_Details_localDeepCopyRowMatrix.hpp"
10 #include "Teuchos_Array.hpp"
11 #include "Teuchos_ArrayView.hpp"
19 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
20 template<
class SC,
class LO,
class GO,
class NT>
26 typename CrsMatrix<SC, LO, GO, NT>::local_matrix_type
27 localDeepCopyFillCompleteCrsMatrix (
const CrsMatrix<SC, LO, GO, NT>& A)
29 using execution_space =
typename NT::execution_space;
30 using Kokkos::view_alloc;
31 using Kokkos::WithoutInitializing;
32 using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
33 using local_matrix_type =
34 typename crs_matrix_type::local_matrix_type;
35 local_matrix_type A_lcl = A.getLocalMatrixDevice ();
37 using local_graph_device_type =
typename crs_matrix_type::local_graph_device_type;
38 using inds_type =
typename local_graph_device_type::entries_type;
39 inds_type ind (view_alloc (
"ind", WithoutInitializing),
40 A_lcl.graph.entries.extent (0));
46 typename local_graph_device_type::row_map_type::non_const_type;
47 offsets_type ptr (view_alloc (
"ptr", WithoutInitializing),
48 A_lcl.graph.row_map.extent (0));
53 using values_type =
typename local_matrix_type::values_type;
54 values_type val (view_alloc (
"val", WithoutInitializing),
55 A_lcl.values.extent (0));
59 local_graph_device_type lclGraph (ind, ptr);
60 const size_t numCols = A.getColMap ()->getLocalNumElements ();
62 return local_matrix_type (A.getObjectLabel (), numCols, val, lclGraph);
67 template<
class SC,
class LO,
class GO,
class NT>
69 CrsMatrix<SC, LO, GO, NT>
70 createDeepCopy (
const RowMatrix<SC, LO, GO, NT>& A)
72 using crs_matrix_type = CrsMatrix<SC, LO, GO, NT>;
73 const crs_matrix_type* A_crs =
74 dynamic_cast<const crs_matrix_type*
> (&A);
76 if (A_crs !=
nullptr && A_crs->isFillComplete ()) {
77 auto A_lcl = localDeepCopyFillCompleteCrsMatrix (*A_crs);
78 auto G = A_crs->getCrsGraph ();
79 return crs_matrix_type (A_lcl, A_crs->getRowMap (),
81 A_crs->getDomainMap (),
82 A_crs->getRangeMap (),
86 else if (A.isGloballyIndexed ()) {
87 const LO lclNumRows (A.getLocalNumRows ());
89 std::unique_ptr<size_t[]> entPerRow (
new size_t [lclNumRows]);
91 for (LO i = 0; i < lclNumRows; ++i) {
92 const size_t lclNumEnt = A.getNumEntriesInLocalRow (i);
93 entPerRow[i] = lclNumEnt;
94 maxNumEnt = maxNumEnt < lclNumEnt ? lclNumEnt : maxNumEnt;
97 Teuchos::ArrayView<const size_t> entPerRow_av
98 (entPerRow.get (), lclNumRows);
100 const bool hasColMap =
101 A.hasColMap () && ! A.getColMap ().is_null ();
103 crs_matrix_type A_copy = hasColMap ?
104 crs_matrix_type (A.getRowMap (), A.getColMap (),
106 crs_matrix_type (A.getRowMap (), entPerRow_av);
108 const bool hasViews = A.supportsRowViews ();
110 typename crs_matrix_type::nonconst_global_inds_host_view_type inputIndsBuf;
111 typename crs_matrix_type::nonconst_values_host_view_type inputValsBuf;
113 Kokkos::resize(inputIndsBuf,maxNumEnt);
114 Kokkos::resize(inputValsBuf,maxNumEnt);
117 const auto& rowMap = * (A.getRowMap ());
118 for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
119 const GO gblRow = rowMap.getGlobalElement (lclRow);
121 typename crs_matrix_type::global_inds_host_view_type inputInds;
122 typename crs_matrix_type::values_host_view_type inputVals;
123 A.getGlobalRowView (gblRow, inputInds, inputVals);
127 A_copy.insertGlobalValues (gblRow, inputInds.extent(0),
128 reinterpret_cast<const typename crs_matrix_type::scalar_type*
>(inputVals.data()),
132 const size_t lclNumEnt = A.getNumEntriesInLocalRow (lclRow);
133 TEUCHOS_ASSERT(lclNumEnt <= maxNumEnt);
135 A.getGlobalRowCopy (gblRow, inputIndsBuf, inputValsBuf, numEnt);
136 A_copy.insertGlobalValues (gblRow, numEnt,
137 reinterpret_cast<const typename crs_matrix_type::scalar_type*>(inputValsBuf.data()),
138 inputIndsBuf.data());
143 if (A.isFillComplete ()) {
144 A_copy.fillComplete (A.getDomainMap (), A.getRangeMap ());
149 using Details::localDeepCopyLocallyIndexedRowMatrix;
150 auto A_lcl = localDeepCopyLocallyIndexedRowMatrix (A,
"A");
152 Teuchos::RCP<const Export<LO, GO, NT>> exp;
153 Teuchos::RCP<const Import<LO, GO, NT>> imp;
154 auto G = A.getGraph ();
155 if (! G.is_null ()) {
156 imp = G->getImporter ();
157 exp = G->getExporter ();
159 if (! imp.is_null () || ! exp.is_null ()) {
160 return crs_matrix_type (A_lcl, A.getRowMap (),
167 return crs_matrix_type (A_lcl, A.getRowMap (),
184 #define TPETRA_CREATEDEEPCOPY_CRSMATRIX_INSTANT(SC, LO, GO, NT) \
185 template CrsMatrix< SC , LO , GO , NT > \
186 createDeepCopy (const RowMatrix<SC, LO, GO, NT>& );
188 #endif // TPETRA_ENABLE_DEPRECATED_CODE
190 #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.