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 namespace Tpetra {
57 namespace Details {
58 
59 template <class SC, class LO, class GO, class NT>
60 KokkosSparse::CrsMatrix<
61  typename Kokkos::ArithTraits<SC>::val_type,
62  LO,
63  typename NT::execution_space,
64  void>
67  const char label[])
68 {
69  TEUCHOS_TEST_FOR_EXCEPTION
70  (A.isGloballyIndexed (), std::logic_error,
71  "Tpetra::Details::localDeepCopyLocallyIndexedRowMatrix: "
72  "Input RowMatrix is globally indexed.");
73 
74  using lro_result_type = LocalRowOffsetsResult<NT>;
75  using offsets_type = typename lro_result_type::offsets_type;
76  using offset_type = typename lro_result_type::offset_type;
77  offsets_type ptr;
78  offset_type nnz = 0;
79  size_t maxNumEnt = 0;
80  {
81  const auto& G = * (A.getGraph ());
82  const lro_result_type result = localRowOffsets (G);
83  ptr = result.ptr;
84  nnz = result.nnz;
85  maxNumEnt = result.maxNumEnt;
86  }
87 
88  using Kokkos::view_alloc;
89  using Kokkos::WithoutInitializing;
90  using IST = typename Kokkos::ArithTraits<SC>::val_type;
91  using local_matrix_type = KokkosSparse::CrsMatrix<
92  IST, LO, typename NT::execution_space, void>;
93  using local_graph_type =
94  typename local_matrix_type::staticcrsgraph_type;
95  using inds_type = typename local_graph_type::entries_type;
96  inds_type ind (view_alloc ("ind", WithoutInitializing), nnz);
97  auto ind_h = Kokkos::create_mirror_view (ind);
98 
99  using values_type = typename local_matrix_type::values_type;
100  values_type val (view_alloc ("val", WithoutInitializing), nnz);
101  auto val_h = Kokkos::create_mirror_view (val);
102 
103  const bool hasViews = A.supportsRowViews ();
104 
105  Teuchos::Array<LO> inputIndsBuf;
106  Teuchos::Array<SC> inputValsBuf;
107  if (! hasViews) {
108  inputIndsBuf.resize (maxNumEnt);
109  inputValsBuf.resize (maxNumEnt);
110  }
111 
112  const LO lclNumRows (A.getNodeNumRows ());
113  offset_type curPos = 0;
114  for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) {
115  Teuchos::ArrayView<const LO> inputInds_av;
116  Teuchos::ArrayView<const SC> inputVals_av;
117  size_t numEnt = 0;
118  if (hasViews) {
119  A.getLocalRowView (lclRow, inputInds_av,
120  inputVals_av);
121  numEnt = static_cast<size_t> (inputInds_av.size ());
122  }
123  else {
124  A.getLocalRowCopy (lclRow, inputIndsBuf (),
125  inputValsBuf (), numEnt);
126  inputInds_av = inputIndsBuf.view (0, numEnt);
127  inputVals_av = inputValsBuf.view (0, numEnt);
128  }
129  const IST* inVals =
130  reinterpret_cast<const IST*> (inputVals_av.getRawPtr ());
131  const LO* inInds = inputInds_av.getRawPtr ();
132  std::copy (inInds, inInds + numEnt, ind_h.data () + curPos);
133  std::copy (inVals, inVals + numEnt, val_h.data () + curPos);
134  curPos += offset_type (numEnt);
135  }
136  Kokkos::deep_copy (ind, ind_h);
137  Kokkos::deep_copy (val, val_h);
138 
139  local_graph_type lclGraph (ind, ptr);
140  const size_t numCols = A.getColMap ()->getNodeNumElements ();
141  return local_matrix_type (label, numCols, val, lclGraph);
142 }
143 
144 } // namespace Details
145 } // namespace Tpetra
146 
147 //
148 // Explicit instantiation macros
149 //
150 // Must be expanded from within the Tpetra namespace!
151 //
152 #define TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_INSTANT(SC, LO, GO, NT) \
153 namespace Details { \
154  template KokkosSparse::CrsMatrix< \
155  Kokkos::ArithTraits<SC>::val_type, \
156  LO, NT::execution_space, void> \
157  localDeepCopyLocallyIndexedRowMatrix<SC, LO, GO, NT> \
158  (const RowMatrix<SC, LO, GO, NT>& A, \
159  const char label[]); \
160 }
161 
162 #endif // TPETRA_DETAILS_LOCALDEEPCOPYROWMATRIX_DEF_HPP
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const =0
The Map that describes the distribution of columns over processes.
virtual size_t getNodeNumRows() const =0
The number of rows owned by the calling process.
virtual void getLocalRowCopy(LocalOrdinal LocalRow, const Teuchos::ArrayView< LocalOrdinal > &Indices, const Teuchos::ArrayView< Scalar > &Values, size_t &NumEntries) const =0
Get a copy of the given local row&#39;s entries.
virtual void getLocalRowView(LocalOrdinal LocalRow, Teuchos::ArrayView< const LocalOrdinal > &indices, Teuchos::ArrayView< const Scalar > &values) const =0
Get a constant, nonpersisting, locally indexed view of the given row of the matrix.
virtual Teuchos::RCP< const RowGraph< LocalOrdinal, GlobalOrdinal, Node > > getGraph() const =0
The RowGraph associated with this matrix.
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.
LocalRowOffsetsResult< NT > localRowOffsets(const RowGraph< LO, GO, NT > &G)
Get local row offsets (&quot;ptr&quot;, in compressed sparse row terms) for the given graph.
virtual bool supportsRowViews() const =0
Whether this object implements getLocalRowView() and getGlobalRowView().
A read-only, row-oriented interface to a sparse matrix.
virtual bool isGloballyIndexed() const =0
Whether matrix indices are globally indexed.
Result returned by localRowOffsets (see below).