Tpetra parallel linear algebra  Version of the Day
Tpetra_Details_localDeepCopyRowMatrix_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // ************************************************************************
38 // @HEADER
39 
40 #ifndef TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_DEF_HPP
41 #define TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_DEF_HPP
42 
43 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
47 
48 #include "Tpetra_Map.hpp"
49 #include "Tpetra_RowMatrix.hpp"
50 #include "Tpetra_Details_localRowOffsets.hpp"
51 #include "Teuchos_TestForException.hpp"
52 #include "Teuchos_Array.hpp"
53 #include "Kokkos_Core.hpp"
54 #include <algorithm>
55 #include <stdexcept>
56 
57 namespace Tpetra {
58 namespace Details {
59 
60 template <class SC, class LO, class GO, class NT>
61 KokkosSparse::CrsMatrix<
62  typename Kokkos::ArithTraits<SC>::val_type,
63  LO,
64  typename NT::device_type,
65  void,
66  size_t>
67 TPETRA_DEPRECATED
68 localDeepCopyLocallyIndexedRowMatrix
69 (const RowMatrix<SC, LO, GO, NT>& A,
70  const char label[])
71 {
72  TEUCHOS_TEST_FOR_EXCEPTION
73  (A.isGloballyIndexed (), std::logic_error,
74  "Tpetra::Details::localDeepCopyLocallyIndexedRowMatrix: "
75  "Input RowMatrix is globally indexed.");
76 
77  using lro_result_type = LocalRowOffsetsResult<NT>;
78  using offsets_type = typename lro_result_type::offsets_type;
79  using offset_type = typename lro_result_type::offset_type;
80  offsets_type ptr;
81  offset_type nnz = 0;
82  size_t maxNumEnt = 0;
83  {
84  const auto& G = * (A.getGraph ());
85  const lro_result_type result = localRowOffsets (G);
86  ptr = result.ptr;
87  nnz = result.nnz;
88  maxNumEnt = result.maxNumEnt;
89  }
90 
91  using Kokkos::view_alloc;
92  using Kokkos::WithoutInitializing;
93  using IST = typename Kokkos::ArithTraits<SC>::val_type;
94  using local_matrix_device_type = KokkosSparse::CrsMatrix<
95  IST, LO, typename NT::device_type, void, size_t>;
96  using local_graph_device_type =
97  typename local_matrix_device_type::staticcrsgraph_type;
98  using inds_type = typename local_graph_device_type::entries_type;
99  inds_type ind (view_alloc ("ind", WithoutInitializing), nnz);
100  auto ind_h = Kokkos::create_mirror_view (ind);
101 
102  using values_type = typename local_matrix_device_type::values_type;
103  values_type val (view_alloc ("val", WithoutInitializing), nnz);
104  auto val_h = Kokkos::create_mirror_view (val);
105 
106  const bool hasViews = A.supportsRowViews ();
107  using row_matrix_type = RowMatrix<SC, LO, GO, NT>;
108  using h_lids_type = typename row_matrix_type::nonconst_local_inds_host_view_type;
109  using h_vals_type = typename row_matrix_type::nonconst_values_host_view_type;
110  using h_lids_type_const = typename row_matrix_type::local_inds_host_view_type;
111  using h_vals_type_const = typename row_matrix_type::values_host_view_type;
112 
113 
114  h_lids_type inputIndsBuf;
115  h_vals_type inputValsBuf;
116  if (! hasViews) {
117  Kokkos::resize(inputIndsBuf,maxNumEnt);
118  Kokkos::resize(inputValsBuf,maxNumEnt);
119  }
120 
121  const LO lclNumRows (A.getNodeNumRows ());
122  offset_type curPos = 0;
123  for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
124  h_lids_type_const inputInds_av;
125  h_vals_type_const inputVals_av;
126  size_t numEnt = 0;
127  if (hasViews) {
128  A.getLocalRowView (lclRow, inputInds_av,
129  inputVals_av);
130  numEnt = static_cast<size_t> (inputInds_av.size ());
131  }
132  else {
133  A.getLocalRowCopy (lclRow, inputIndsBuf,
134  inputValsBuf, numEnt);
135  inputInds_av = Kokkos::subview(inputIndsBuf,std::make_pair((size_t)0,numEnt));
136  inputVals_av = Kokkos::subview(inputValsBuf,std::make_pair((size_t)0,numEnt));
137  }
138  const IST* inVals =
139  reinterpret_cast<const IST*> (inputVals_av.data());
140  const LO* inInds = inputInds_av.data();
141  std::copy (inInds, inInds + numEnt, ind_h.data () + curPos);
142  std::copy (inVals, inVals + numEnt, val_h.data () + curPos);
143  curPos += offset_type (numEnt);
144  }
145  Kokkos::deep_copy (ind, ind_h);
146  Kokkos::deep_copy (val, val_h);
147 
148  local_graph_device_type lclGraph (ind, ptr);
149  const size_t numCols = A.getColMap ()->getNodeNumElements ();
150  return local_matrix_device_type (label, numCols, val, lclGraph);
151 }
152 
153 
154 } // namespace Details
155 } // namespace Tpetra
156 
157 
158 //
159 // Explicit instantiation macros
160 //
161 // Must be expanded from within the Tpetra namespace!
162 //
163 #define TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_INSTANT(SC, LO, GO, NT) \
164 namespace Details { \
165  template KokkosSparse::CrsMatrix< \
166  Kokkos::ArithTraits<SC>::val_type, \
167  LO, NT::device_type, void, size_t> \
168  localDeepCopyLocallyIndexedRowMatrix<SC, LO, GO, NT> \
169  (const RowMatrix<SC, LO, GO, NT>& A, \
170  const char label[]); \
171 }
172 
173 #endif // TPETRA_ENABLE_DEPRECATED_CODE
174 
175 #endif // TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_DEF_HPP
Implementation details of Tpetra.
LocalRowOffsetsResult< NT > localRowOffsets(const RowGraph< LO, GO, NT > &G)
Get local row offsets ("ptr", in compressed sparse row terms) for the given graph.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
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.