Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_PackTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_DETAILS_PACKTRAITS_HPP
11 #define TPETRA_DETAILS_PACKTRAITS_HPP
12 
18 
19 #include "Tpetra_ConfigDefs.hpp"
20 #include "Kokkos_Core.hpp"
21 
22 namespace Tpetra {
23 namespace Details {
24 
28 template <class T>
29 struct PackTraits {
31  using value_type = T;
32 
46  static const bool compileTimeSize = true;
47 
49  using input_buffer_type = Kokkos::View<const char*, Kokkos::AnonymousSpace>;
50 
52  using output_buffer_type = Kokkos::View<char*, Kokkos::AnonymousSpace>;
53 
55  using input_array_type = Kokkos::View<const value_type*, Kokkos::AnonymousSpace>;
56 
58  using output_array_type = Kokkos::View<value_type*, Kokkos::AnonymousSpace>;
59 
74  KOKKOS_INLINE_FUNCTION
75  static size_t
76  numValuesPerScalar(const value_type& /* x */) {
77  // If your type T is something like Stokhos::UQ::PCE<S>, you must
78  // reimplement this function.
79  return static_cast<size_t>(1);
80  }
81 
95  // packArray error code (success = 0)}
96  KOKKOS_INLINE_FUNCTION
97  static Kokkos::pair<int, size_t>
98  packArray(char outBuf[],
99  const value_type inBuf[],
100  const size_t numEnt) {
101  size_t numBytes = 0;
102  int errorCode = 0;
103  typedef Kokkos::pair<int, size_t> pair_type;
104 
105  if (numEnt == 0) {
106  return pair_type(errorCode, numBytes);
107  } else {
108  // NOTE (mfh 02 Feb 2015) This assumes that all instances of T
109  // require the same number of bytes. To generalize this, we
110  // would need to sum up the counts for all entries of inBuf.
111  // That of course would suggest that we would need to memcpy
112  // each entry separately.
113  //
114  // We can't just default construct an instance of T, because if
115  // T's size is run-time dependent, a default-constructed T might
116  // not have the right size. However, we require that all
117  // entries of the input array have the correct size.
118  numBytes = numEnt * packValueCount(inBuf[0]);
119 
120  // As of CUDA 6, it's totally fine to use memcpy in a CUDA device
121  // function. It does what one would expect.
122  memcpy(outBuf, inBuf, numBytes);
123  return pair_type(errorCode, numBytes);
124  }
125  }
126 
141  // unpackArray error code (success = 0)}
142  KOKKOS_INLINE_FUNCTION
143  static Kokkos::pair<int, size_t>
145  const char inBuf[],
146  const size_t numEnt) {
147  size_t numBytes = 0;
148  int errorCode = 0;
149  typedef Kokkos::pair<int, size_t> pair_type;
150 
151  if (numEnt == 0) {
152  return pair_type(errorCode, numBytes);
153  } else {
154  // NOTE (mfh 02 Feb 2015) This assumes that all instances of
155  // value_type require the same number of bytes. To generalize
156  // this, we would need to sum up the counts for all entries of
157  // inBuf. That of course would suggest that we would need to
158  // memcpy each entry separately.
159  //
160  // We can't just default construct an instance of value_type,
161  // because if value_type's size is run-time dependent, a
162  // default-constructed value_type might not have the right size.
163  // However, we require that all entries of the input array have
164  // the correct size.
165  const value_type& val = outBuf[0];
166  numBytes = numEnt * packValueCount(val);
167 
168  // As of CUDA 6, it's totally fine to use memcpy in a CUDA device
169  // function. It does what one would expect.
170  memcpy((void*)outBuf, inBuf, numBytes);
171  return pair_type(errorCode, numBytes);
172  }
173  }
174 
198  KOKKOS_INLINE_FUNCTION
199  static size_t
200  packValueCount(const T& /* inVal */) {
201  return sizeof(T);
202  }
203 
213  KOKKOS_INLINE_FUNCTION
214  static size_t
215  packValue(char outBuf[],
216  const T& inVal) {
217  // It's actually OK for packValueCount to return an upper bound
218  // (e.g., padding for alignment). The memcpy call below will copy
219  // any included padding as well as the actual data.
220  const size_t numBytes = packValueCount(inVal);
221 
222  // As of CUDA 6, it's totally fine to use memcpy in a CUDA device
223  // function. It does what one would expect.
224  memcpy(outBuf, &inVal, numBytes);
225  return numBytes;
226  }
227 
239  KOKKOS_INLINE_FUNCTION
240  static size_t
241  packValue(char outBuf[],
242  const size_t outBufIndex,
243  const T& inVal) {
244  // It's actually OK for packValueCount to return an upper bound
245  // (e.g., padding for alignment). The memcpy call below will copy
246  // any included padding as well as the actual data.
247  const size_t numBytes = packValueCount(inVal);
248  const size_t offset = outBufIndex * numBytes;
249 
250  // As of CUDA 6, it's totally fine to use memcpy in a CUDA device
251  // function. It does what one would expect.
252  memcpy(outBuf + offset, &inVal, numBytes);
253  return numBytes;
254  }
255 
268  KOKKOS_INLINE_FUNCTION
269  static size_t
270  unpackValue(T& outVal, const char inBuf[]) {
271  // It's actually OK for packValueCount to return an upper bound
272  // (e.g., padding for alignment). The memcpy call below will copy
273  // any included padding as well as the actual data.
274  const size_t numBytes = packValueCount(outVal);
275 
276  // As of CUDA 6, it's totally fine to use memcpy in a CUDA device
277  // function. It does what one would expect.
278  memcpy((void*)&outVal, inBuf, numBytes);
279  return numBytes;
280  }
281 }; // struct PackTraits
282 
283 } // namespace Details
284 } // namespace Tpetra
285 
286 #endif // TPETRA_DETAILS_PACKTRAITS_HPP
LO value_type
The type of data to pack or unpack.
Kokkos::View< char *, Kokkos::AnonymousSpace > output_buffer_type
The type of an output buffer of bytes.
static KOKKOS_INLINE_FUNCTION size_t numValuesPerScalar(const value_type &)
Given an instance of value_type allocated with the right size, return the &quot;number of values&quot; that mak...
static KOKKOS_INLINE_FUNCTION size_t unpackValue(T &outVal, const char inBuf[])
Unpack the given value from the given output buffer.
Traits class for packing / unpacking data of type T.
static KOKKOS_INLINE_FUNCTION Kokkos::pair< int, size_t > packArray(char outBuf[], const value_type inBuf[], const size_t numEnt)
Pack the first numEnt entries of the given input buffer of value_type, into the output buffer of byte...
static const bool compileTimeSize
Whether the number of bytes required to pack one instance of value_type is fixed at compile time...
static KOKKOS_INLINE_FUNCTION size_t packValue(char outBuf[], const size_t outBufIndex, const T &inVal)
Pack the given value of type value_type into the given output buffer of bytes (char).
static KOKKOS_INLINE_FUNCTION size_t packValue(char outBuf[], const T &inVal)
Pack the given value of type value_type into the given output buffer of bytes (char).
Kokkos::View< const char *, Kokkos::AnonymousSpace > input_buffer_type
The type of an input buffer of bytes.
Kokkos::View< const value_type *, Kokkos::AnonymousSpace > input_array_type
The type of an input array of value_type.
Kokkos::View< value_type *, Kokkos::AnonymousSpace > output_array_type
The type of an output array of value_type.
static KOKKOS_INLINE_FUNCTION size_t packValueCount(const T &)
Number of bytes required to pack or unpack the given value of type value_type.
static KOKKOS_INLINE_FUNCTION Kokkos::pair< int, size_t > unpackArray(value_type outBuf[], const char inBuf[], const size_t numEnt)
Unpack numEnt value_type entries from the given input buffer of bytes, to the given output buffer of ...