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 #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"
10 #include <memory>
11 
12 namespace Tpetra {
13 
14 namespace { // (anonymous)
15 
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)
19 {
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 ();
26 
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));
31  Kokkos::deep_copy (ind, A_lcl.graph.entries);
32 
33  using offsets_type =
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));
37  Kokkos::deep_copy (ptr, A_lcl.graph.row_map);
38 
39  using values_type = typename local_matrix_type::values_type;
40  values_type val (view_alloc ("val", WithoutInitializing),
41  A_lcl.values.extent (0));
42  Kokkos::deep_copy (val, A_lcl.values);
43 
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);
47 }
48 
49 } // namespace // (anonymous)
50 
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)
54 {
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);
58 
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 (),
63  A_crs->getColMap (),
64  A_crs->getDomainMap (),
65  A_crs->getRangeMap (),
66  G->getImporter (),
67  G->getExporter ());
68  }
69  else if (A.isGloballyIndexed ()) {
70  const LO lclNumRows (A.getNodeNumRows ());
71 
72  std::unique_ptr<size_t[]> entPerRow (new size_t [lclNumRows]);
73  size_t maxNumEnt = 0;
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;
78  }
79 
80  Teuchos::ArrayView<const size_t> entPerRow_av
81  (entPerRow.get (), lclNumRows);
82 
83  const bool hasColMap =
84  A.hasColMap () && ! A.getColMap ().is_null ();
85 
86  crs_matrix_type A_copy = hasColMap ?
87  crs_matrix_type (A.getRowMap (), A.getColMap (),
88  entPerRow_av) :
89  crs_matrix_type (A.getRowMap (), entPerRow_av);
90 
91  const bool hasViews = A.supportsRowViews ();
92 
93  Teuchos::Array<GO> inputIndsBuf;
94  Teuchos::Array<SC> inputValsBuf;
95  if (! hasViews) {
96  inputIndsBuf.resize (maxNumEnt);
97  inputValsBuf.resize (maxNumEnt);
98  }
99 
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;
105  size_t numEnt = 0;
106  if (hasViews) {
107  A.getGlobalRowView (gblRow, inputInds_av, inputVals_av);
108  numEnt = static_cast<size_t> (inputInds_av.size ());
109  }
110  else {
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);
117  }
118  A_copy.insertGlobalValues (gblRow, inputInds_av, inputVals_av);
119  }
120 
121  if (A.isFillComplete ()) {
122  A_copy.fillComplete (A.getDomainMap (), A.getRangeMap ());
123  }
124  return A_copy;
125  }
126  else { // locally indexed or empty
128  auto A_lcl = localDeepCopyLocallyIndexedRowMatrix (A, "A");
129 
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 ();
136  }
137  if (! imp.is_null () || ! exp.is_null ()) {
138  return crs_matrix_type (A_lcl, A.getRowMap (),
139  A.getColMap (),
140  A.getDomainMap (),
141  A.getRangeMap (),
142  imp, exp);
143  }
144  else {
145  return crs_matrix_type (A_lcl, A.getRowMap (),
146  A.getColMap (),
147  A.getDomainMap (),
148  A.getRangeMap ());
149  }
150  }
151 }
152 
153 } // namespace Tpetra
154 
155 //
156 // Explicit instantiation macro
157 //
158 // Must be expanded from within the Tpetra namespace!
159 //
160 
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>& );
164 
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&#39;s local sparse matrix.