Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_createDeepCopy_CrsMatrix_def.hpp
1 #ifndef TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
2 #define TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
3 
4 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
5 
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"
12 #include <memory>
13 
14 
15 namespace Tpetra {
16 
17 namespace { // (anonymous)
18 
19 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
20 template<class SC, class LO, class GO, class NT>
21 // This function is deprecated, but users don't call it directly; it is a
22 // helper from createDeepCopy. createDeepCopy is also deprecated.
23 // We silence TPETRA_DEPRECATED warnings here to prevent noise from
24 // compilation of createDeepCopy.
25 // TPETRA_DEPRECATED
26 typename CrsMatrix<SC, LO, GO, NT>::local_matrix_type
27 localDeepCopyFillCompleteCrsMatrix (const CrsMatrix<SC, LO, GO, NT>& A)
28 {
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 ();
36 
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));
41 
42  // DEEP_COPY REVIEW - DEVICE-TO-DEVICE
43  Kokkos::deep_copy (execution_space(), ind, A_lcl.graph.entries);
44 
45  using offsets_type =
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));
49 
50  // DEEP_COPY REVIEW - DEVICE-TO-DEVICE
51  Kokkos::deep_copy (execution_space(), ptr, A_lcl.graph.row_map);
52 
53  using values_type = typename local_matrix_type::values_type;
54  values_type val (view_alloc ("val", WithoutInitializing),
55  A_lcl.values.extent (0));
56 
57  // DEEP_COPY REVIEW - DEVICE-TO-DEVICE
58  Kokkos::deep_copy (execution_space(), val, A_lcl.values);
59  local_graph_device_type lclGraph (ind, ptr);
60  const size_t numCols = A.getColMap ()->getLocalNumElements ();
61 
62  return local_matrix_type (A.getObjectLabel (), numCols, val, lclGraph);
63 }
64 
65 } // namespace // (anonymous)
66 
67 template<class SC, class LO, class GO, class NT>
68 TPETRA_DEPRECATED
69 CrsMatrix<SC, LO, GO, NT>
70 createDeepCopy (const RowMatrix<SC, LO, GO, NT>& A)
71 {
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);
75 
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 (),
80  A_crs->getColMap (),
81  A_crs->getDomainMap (),
82  A_crs->getRangeMap (),
83  G->getImporter (),
84  G->getExporter ());
85  }
86  else if (A.isGloballyIndexed ()) {
87  const LO lclNumRows (A.getLocalNumRows ());
88 
89  std::unique_ptr<size_t[]> entPerRow (new size_t [lclNumRows]);
90  size_t maxNumEnt = 0;
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;
95  }
96 
97  Teuchos::ArrayView<const size_t> entPerRow_av
98  (entPerRow.get (), lclNumRows);
99 
100  const bool hasColMap =
101  A.hasColMap () && ! A.getColMap ().is_null ();
102 
103  crs_matrix_type A_copy = hasColMap ?
104  crs_matrix_type (A.getRowMap (), A.getColMap (),
105  entPerRow_av) :
106  crs_matrix_type (A.getRowMap (), entPerRow_av);
107 
108  const bool hasViews = A.supportsRowViews ();
109 
110  typename crs_matrix_type::nonconst_global_inds_host_view_type inputIndsBuf;
111  typename crs_matrix_type::nonconst_values_host_view_type inputValsBuf;
112  if (! hasViews) {
113  Kokkos::resize(inputIndsBuf,maxNumEnt);
114  Kokkos::resize(inputValsBuf,maxNumEnt);
115  }
116 
117  const auto& rowMap = * (A.getRowMap ());
118  for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
119  const GO gblRow = rowMap.getGlobalElement (lclRow);
120  if (hasViews) {
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);
124  // BAD BAD BAD
125  // we want a better way than reinterpret casting back and forth between scalar_type and
126  // impl_scalar_type everywhere
127  A_copy.insertGlobalValues (gblRow, inputInds.extent(0),
128  reinterpret_cast<const typename crs_matrix_type::scalar_type*>(inputVals.data()),
129  inputInds.data());
130  }
131  else {
132  const size_t lclNumEnt = A.getNumEntriesInLocalRow (lclRow);
133  TEUCHOS_ASSERT(lclNumEnt <= maxNumEnt);
134  size_t numEnt = 0;
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());
139 
140  }
141  }
142 
143  if (A.isFillComplete ()) {
144  A_copy.fillComplete (A.getDomainMap (), A.getRangeMap ());
145  }
146  return A_copy;
147  }
148  else { // locally indexed or empty
149  using Details::localDeepCopyLocallyIndexedRowMatrix;
150  auto A_lcl = localDeepCopyLocallyIndexedRowMatrix (A, "A");
151 
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 ();
158  }
159  if (! imp.is_null () || ! exp.is_null ()) {
160  return crs_matrix_type (A_lcl, A.getRowMap (),
161  A.getColMap (),
162  A.getDomainMap (),
163  A.getRangeMap (),
164  imp, exp);
165  }
166  else {
167  return crs_matrix_type (A_lcl, A.getRowMap (),
168  A.getColMap (),
169  A.getDomainMap (),
170  A.getRangeMap ());
171  }
172  }
173 }
174 #endif
175 
176 } // namespace Tpetra
177 
178 //
179 // Explicit instantiation macro
180 //
181 // Must be expanded from within the Tpetra namespace!
182 //
183 
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>& );
187 
188 #endif // TPETRA_ENABLE_DEPRECATED_CODE
189 
190 #endif // TPETRA_CREATEDEEPCOPY_CRSMATRIX_DEF_HPP
191 
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.