Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_localRowOffsets_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_LOCALROWOFFSETS_DEF_HPP
41 #define TPETRA_DETAILS_LOCALROWOFFSETS_DEF_HPP
42 
46 
47 #include "TpetraCore_config.h"
48 #include "Tpetra_CrsGraph.hpp"
49 #include "Tpetra_RowGraph.hpp"
52 #include "Kokkos_Core.hpp"
53 
54 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
55 // This file can be deleted when deprecated code is removed
56 
57 namespace Tpetra {
58 namespace Details {
59 namespace Impl {
60 
61 template <class LO, class GO, class NT>
62 std::pair<typename LocalRowOffsetsResult<NT>::offsets_type, size_t>
63 localRowCounts (const RowGraph<LO, GO, NT>& G)
64 {
65  using result_type = LocalRowOffsetsResult<NT>;
66  using offsets_type = typename result_type::offsets_type;
67  using offset_type = typename result_type::offset_type;
68 
69  const LO lclNumRows (G.getLocalNumRows ());
70  offsets_type entPerRow;
71  if (lclNumRows != 0) {
72  using Kokkos::view_alloc;
73  using Kokkos::WithoutInitializing;
74  entPerRow =
75  offsets_type (view_alloc ("entPerRow", WithoutInitializing),
76  lclNumRows);
77  }
78  using host = Kokkos::DefaultHostExecutionSpace;
79  auto entPerRow_h = Kokkos::create_mirror_view (host (), entPerRow);
80 
81  // Don't trust G.getLocalMaxNumRowEntries() unless G is fillComplete.
82  // Even then, I would rather this method didn't exist (since it adds
83  // state and imposes overhead on fillComplete), and it's easy to
84  // compute ourselves here.
85  size_t maxNumEnt = 0;
86  for (LO i = 0; i < lclNumRows; ++i) {
87  const size_t lclNumEnt = G.getNumEntriesInLocalRow (i);
88  entPerRow_h[i] = offset_type (lclNumEnt);
89  maxNumEnt = maxNumEnt < lclNumEnt ? lclNumEnt : maxNumEnt;
90  }
91  // DEEP_COPY REVIEW - HOSTMIRROR-TO-DEVICE
92  using execution_space = typename NT::execution_space;
93  Kokkos::deep_copy (execution_space(), entPerRow, entPerRow_h);
94  return {entPerRow, maxNumEnt};
95 }
96 
97 template <class LO, class GO, class NT>
98 LocalRowOffsetsResult<NT>
99 localRowOffsetsFromRowGraph (const RowGraph<LO, GO, NT>& G)
100 {
101  using result_type = LocalRowOffsetsResult<NT>;
102  using offsets_type = typename result_type::offsets_type;
103  using offset_type = typename result_type::offset_type;
104 
105  offsets_type entPerRow;
106  size_t maxNumEnt = 0;
107  {
108  auto result = localRowCounts (G);
109  entPerRow = result.first;
110  maxNumEnt = result.second;
111  }
112 
113  const LO lclNumRows (G.getLocalNumRows ());
114  offsets_type ptr;
115  offset_type nnz = 0;
116  if (lclNumRows != 0) {
117  using Kokkos::view_alloc;
118  using Kokkos::WithoutInitializing;
119  ptr = offsets_type (view_alloc ("ptr", WithoutInitializing),
120  lclNumRows + 1);
122  nnz = computeOffsetsFromCounts (ptr, entPerRow);
123  }
124  return {ptr, nnz, maxNumEnt};
125 }
126 
127 template <class LO, class GO, class NT>
128 LocalRowOffsetsResult<NT>
129 localRowOffsetsFromFillCompleteCrsGraph (const CrsGraph<LO, GO, NT>& G)
130 {
131  using Kokkos::view_alloc;
132  using Kokkos::WithoutInitializing;
133  using result_type = LocalRowOffsetsResult<NT>;
134  using offsets_type = typename result_type::offsets_type;
135  using offset_type = typename result_type::offset_type;
136 
137  auto G_lcl = G.getLocalGraphDevice ();
138  offsets_type ptr (view_alloc ("ptr", WithoutInitializing),
139  G_lcl.row_map.extent (0));
140  // DEEP_COPY REVIEW - NOT TESTED
141  Kokkos::deep_copy (ptr, G_lcl.row_map);
142 
143  const offset_type nnz = G.getLocalNumEntries ();
144  const size_t maxNumEnt = G.getLocalMaxNumRowEntries ();
145  return {ptr, nnz, maxNumEnt};
146 }
147 
148 } // namespace Impl
149 
150 template <class LO, class GO, class NT>
151 LocalRowOffsetsResult<NT>
152 localRowOffsets (const RowGraph<LO, GO, NT>& G)
153 {
154  if (G.isFillComplete ()) {
155  using crs_graph_type = CrsGraph<LO, GO, NT>;
156  const crs_graph_type* G_crs =
157  dynamic_cast<const crs_graph_type*> (&G);
158  if (G_crs != nullptr) {
159  return Impl::localRowOffsetsFromFillCompleteCrsGraph (*G_crs);
160  }
161  }
162  return Impl::localRowOffsetsFromRowGraph (G);
163 }
164 
165 } // namespace Details
166 } // namespace Tpetra
167 
168 //
169 // Explicit instantiation macros
170 //
171 // Must be expanded from within the Tpetra namespace!
172 //
173 
174 #define TPETRA_DETAILS_LOCALROWOFFSETS_INSTANT(LO, GO, NT) \
175 namespace Details { \
176 namespace Impl { \
177  \
178 template std::pair<LocalRowOffsetsResult<NT>::offsets_type, size_t> \
179 localRowCounts (const RowGraph<LO, GO, NT>& G); \
180  \
181 template LocalRowOffsetsResult<NT> \
182 localRowOffsetsFromRowGraph (const RowGraph<LO, GO, NT>& G); \
183  \
184 template LocalRowOffsetsResult<NT> \
185 localRowOffsetsFromFillCompleteCrsGraph (const CrsGraph<LO, GO, NT>& G); \
186  \
187 } \
188  \
189 template LocalRowOffsetsResult<NT> \
190 localRowOffsets (const RowGraph<LO, GO, NT>& A); \
191 }
192 
193 #endif // TPETRA_ENABLE_DEPRECATED_CODE
194 
195 #endif // TPETRA_DETAILS_LOCALROWOFFSETS_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.
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...
OffsetsViewType::non_const_value_type computeOffsetsFromCounts(const ExecutionSpace &execSpace, const OffsetsViewType &ptr, const CountsViewType &counts)
Compute offsets from counts.
Declaration and definition of Tpetra::Details::getEntryOnHost.