Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_localDeepCopyRowMatrix_def.hpp
Go to the documentation of this file.
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 
46 
47 #include "Tpetra_Map.hpp"
48 #include "Tpetra_RowMatrix.hpp"
49 #include "Tpetra_Details_localRowOffsets.hpp"
50 #include "Teuchos_TestForException.hpp"
51 #include "Teuchos_Array.hpp"
52 #include "Kokkos_Core.hpp"
53 #include <algorithm>
54 #include <stdexcept>
55 
56 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
57 // This file can be deleted when deprecated code is removed
58 
59 namespace Tpetra {
60 namespace Details {
61 
62 // This function is deprecated, but users don't call it directly; it is a
63 // helper from createDeepCopy. createDeepCopy is also deprecated.
64 // We silence TPETRA_DEPRECATED warnings here to prevent noise from
65 // compilation of createDeepCopy.
66 // TPETRA_DEPRECATED
67 template <class SC, class LO, class GO, class NT>
68 KokkosSparse::CrsMatrix<
69  typename Kokkos::ArithTraits<SC>::val_type,
70  LO,
71  typename NT::device_type,
72  void,
73  size_t>
74 localDeepCopyLocallyIndexedRowMatrix
75 (const RowMatrix<SC, LO, GO, NT>& A,
76  const char label[])
77 {
78  TEUCHOS_TEST_FOR_EXCEPTION
79  (A.isGloballyIndexed (), std::logic_error,
80  "Tpetra::Details::localDeepCopyLocallyIndexedRowMatrix: "
81  "Input RowMatrix is globally indexed.");
82 
83  using lro_result_type = LocalRowOffsetsResult<NT>;
84  using offsets_type = typename lro_result_type::offsets_type;
85  using offset_type = typename lro_result_type::offset_type;
86  offsets_type ptr;
87  offset_type nnz = 0;
88  size_t maxNumEnt = 0;
89  {
90  const auto& G = * (A.getGraph ());
91  const lro_result_type result = localRowOffsets (G);
92  ptr = result.ptr;
93  nnz = result.nnz;
94  maxNumEnt = result.maxNumEnt;
95  }
96 
97  using Kokkos::view_alloc;
98  using Kokkos::WithoutInitializing;
99  using IST = typename Kokkos::ArithTraits<SC>::val_type;
100  using local_matrix_device_type = KokkosSparse::CrsMatrix<
101  IST, LO, typename NT::device_type, void, size_t>;
102  using local_graph_device_type =
103  typename local_matrix_device_type::staticcrsgraph_type;
104  using inds_type = typename local_graph_device_type::entries_type;
105  inds_type ind (view_alloc ("ind", WithoutInitializing), nnz);
106  auto ind_h = Kokkos::create_mirror_view (ind);
107 
108  using values_type = typename local_matrix_device_type::values_type;
109  values_type val (view_alloc ("val", WithoutInitializing), nnz);
110  auto val_h = Kokkos::create_mirror_view (val);
111 
112  const bool hasViews = A.supportsRowViews ();
113  using row_matrix_type = RowMatrix<SC, LO, GO, NT>;
114  using h_lids_type = typename row_matrix_type::nonconst_local_inds_host_view_type;
115  using h_vals_type = typename row_matrix_type::nonconst_values_host_view_type;
116  using h_lids_type_const = typename row_matrix_type::local_inds_host_view_type;
117  using h_vals_type_const = typename row_matrix_type::values_host_view_type;
118 
119 
120  h_lids_type inputIndsBuf;
121  h_vals_type inputValsBuf;
122  if (! hasViews) {
123  Kokkos::resize(inputIndsBuf,maxNumEnt);
124  Kokkos::resize(inputValsBuf,maxNumEnt);
125  }
126 
127  const LO lclNumRows (A.getLocalNumRows ());
128  offset_type curPos = 0;
129  for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
130  h_lids_type_const inputInds_av;
131  h_vals_type_const inputVals_av;
132  size_t numEnt = 0;
133  if (hasViews) {
134  A.getLocalRowView (lclRow, inputInds_av,
135  inputVals_av);
136  numEnt = static_cast<size_t> (inputInds_av.size ());
137  }
138  else {
139  A.getLocalRowCopy (lclRow, inputIndsBuf,
140  inputValsBuf, numEnt);
141  inputInds_av = Kokkos::subview(inputIndsBuf,std::make_pair((size_t)0,numEnt));
142  inputVals_av = Kokkos::subview(inputValsBuf,std::make_pair((size_t)0,numEnt));
143  }
144  const IST* inVals =
145  reinterpret_cast<const IST*> (inputVals_av.data());
146  const LO* inInds = inputInds_av.data();
147  std::copy (inInds, inInds + numEnt, ind_h.data () + curPos);
148  std::copy (inVals, inVals + numEnt, val_h.data () + curPos);
149  curPos += offset_type (numEnt);
150  }
151 
152  using execution_space = typename inds_type::execution_space;
153  // DEEP_COPY REVIEW - HOSTMIRROR-TO-DEVICE
154  Kokkos::deep_copy (execution_space(), ind, ind_h);
155  // DEEP_COPY REVIEW - HOSTMIRROR-TO-DEVICE
156  Kokkos::deep_copy (execution_space(), val, val_h);
157 
158  local_graph_device_type lclGraph (ind, ptr);
159  const size_t numCols = A.getColMap ()->getLocalNumElements ();
160  return local_matrix_device_type (label, numCols, val, lclGraph);
161 }
162 
163 
164 } // namespace Details
165 } // namespace Tpetra
166 
167 
168 //
169 // Explicit instantiation macros
170 //
171 // Must be expanded from within the Tpetra namespace!
172 //
173 #define TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_INSTANT(SC, LO, GO, NT) \
174 namespace Details { \
175  template KokkosSparse::CrsMatrix< \
176  Kokkos::ArithTraits<SC>::val_type, \
177  LO, NT::device_type, void, size_t> \
178  localDeepCopyLocallyIndexedRowMatrix<SC, LO, GO, NT> \
179  (const RowMatrix<SC, LO, GO, NT>& A, \
180  const char label[]); \
181 }
182 
183 #endif // TPETRA_ENABLE_DEPRECATED_CODE
184 
185 #endif // TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_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.