Tpetra parallel linear algebra  Version of the Day
Tpetra_Details_PackTraits.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 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_DETAILS_PACKTRAITS_HPP
43 #define TPETRA_DETAILS_PACKTRAITS_HPP
44 
50 
51 #include "Tpetra_ConfigDefs.hpp"
52 #include "Kokkos_Core.hpp"
53 
54 namespace Tpetra {
55 namespace Details {
56 
62 template<class T, class D>
63 struct PackTraits {
65  typedef T value_type;
66 
80  static const bool compileTimeSize = true;
81 
83  typedef Kokkos::View<const char*, D, Kokkos::MemoryUnmanaged> input_buffer_type;
84 
86  typedef Kokkos::View<char*, D, Kokkos::MemoryUnmanaged> output_buffer_type;
87 
89  typedef Kokkos::View<const value_type*, D, Kokkos::MemoryUnmanaged> input_array_type;
90 
92  typedef Kokkos::View<value_type*, D, Kokkos::MemoryUnmanaged> output_array_type;
93 
108  static size_t numValuesPerScalar (const value_type& /* x */) {
109  // If your type T is something like Stokhos::UQ::PCE<S>, you must
110  // reimplement this function.
111  return static_cast<size_t> (1);
112  }
113 
138  static Kokkos::View<value_type*, D>
140  const size_t numEnt,
141  const std::string& label = "")
142  {
143  typedef Kokkos::View<value_type*, D> view_type;
144  typedef typename view_type::size_type size_type;
145 
146  // This exploits the fact that Kokkos::View's constructor ignores
147  // size arguments beyond what the View's type specifies. For
148  // value_type = Stokhos::UQ::PCE<S>, numValuesPerScalar returns
149  // something other than 1, and the constructor will actually use
150  // that value.
151  const size_type numVals = numValuesPerScalar (x);
152  return view_type (label, static_cast<size_type> (numEnt), numVals);
153  }
154 
168  static size_t
170  const input_array_type& inBuf,
171  const size_t numEnt)
172  {
173 #ifdef HAVE_TPETRA_DEBUG
174  TEUCHOS_TEST_FOR_EXCEPTION(
175  static_cast<size_t> (inBuf.dimension_0 ()) < numEnt,
176  std::invalid_argument, "PackTraits::packArray: inBuf.dimension_0() = "
177  << inBuf.dimension_0 () << " < numEnt = " << numEnt << ".");
178 #endif // HAVE_TPETRA_DEBUG
179 
180  if (numEnt == 0) {
181  return 0;
182  }
183  else {
184  // NOTE (mfh 02 Feb 2015) This assumes that all instances of T
185  // require the same number of bytes. To generalize this, we
186  // would need to sum up the counts for all entries of inBuf.
187  // That of course would suggest that we would need to memcpy
188  // each entry separately.
189  //
190  // We can't just default construct an instance of T, because if
191  // T's size is run-time dependent, a default-constructed T might
192  // not have the right size. However, we require that all
193  // entries of the input array have the correct size.
194  const size_t numBytes = numEnt * packValueCount (inBuf(0));
195 #ifdef HAVE_TPETRA_DEBUG
196  TEUCHOS_TEST_FOR_EXCEPTION(
197  static_cast<size_t> (outBuf.dimension_0 ()) < numBytes,
198  std::invalid_argument, "PackTraits::packArray: outBuf.dimension_0() = "
199  << outBuf.dimension_0 () << " < numBytes = " << numBytes << ".");
200 #endif // HAVE_TPETRA_DEBUG
201 
202  // FIXME (mfh 02,05 Feb 2015) This may assume UVM. On the other
203  // hand, reinterpret_cast may break aliasing and/or alignment
204  // rules.
205  memcpy (outBuf.ptr_on_device (), inBuf.ptr_on_device (), numBytes);
206  return numBytes;
207  }
208  }
209 
224  static size_t
226  const input_buffer_type& inBuf,
227  const size_t numEnt)
228  {
229 #ifdef HAVE_TPETRA_DEBUG
230  TEUCHOS_TEST_FOR_EXCEPTION(
231  static_cast<size_t> (outBuf.size ()) < numEnt, std::invalid_argument,
232  "PackTraits::unpackArray: outBuf.size() = " << outBuf.size ()
233  << " < numEnt = " << numEnt << ".");
234 #endif // HAVE_TPETRA_DEBUG
235 
236  if (numEnt == 0) {
237  return static_cast<size_t> (0);
238  }
239  else {
240  // NOTE (mfh 02 Feb 2015) This assumes that all instances of T
241  // require the same number of bytes. To generalize this, we
242  // would need to sum up the counts for all entries of inBuf.
243  // That of course would suggest that we would need to memcpy
244  // each entry separately.
245  //
246  // We can't just default construct an instance of T, because if
247  // T's size is run-time dependent, a default-constructed T might
248  // not have the right size. However, we require that all
249  // entries of the input array have the correct size.
250  const T& val = packValueCount (outBuf(0));
251  const size_t numBytes = numEnt * packValueCount (val);
252 #ifdef HAVE_TPETRA_DEBUG
253  TEUCHOS_TEST_FOR_EXCEPTION(
254  static_cast<size_t> (inBuf.dimension_0 ()) < numBytes,
255  std::invalid_argument, "PackTraits::unpackArray: inBuf.dimension_0() = "
256  << inBuf.dimension_0 () << " < numBytes = " << numBytes << ".");
257 #endif // HAVE_TPETRA_DEBUG
258 
259  // FIXME (mfh 02,05 Feb 2015) This may assume UVM. On the other
260  // hand, reinterpret_cast may break aliasing and/or alignment
261  // rules.
262  memcpy (outBuf.ptr_on_device (), inBuf.ptr_on_device (), numBytes);
263  return numBytes;
264  }
265  }
266 
290  static size_t
291  packValueCount (const T& /* inVal */)
292  {
293  return sizeof (T);
294  }
295 
305  static size_t
307  const T& inVal)
308  {
309  // It's actually OK for packValueCount to return an upper bound
310  // (e.g., padding for alignment). The memcpy call below will copy
311  // any included padding as well as the actual data.
312  const size_t numBytes = packValueCount (inVal);
313 
314  // FIXME (mfh 02,05 Feb 2015) This may assume UVM. On the other
315  // hand, reinterpret_cast may break aliasing and/or alignment
316  // rules.
317  memcpy (outBuf.ptr_on_device (), &inVal, numBytes);
318  return numBytes;
319  }
320 
333  static size_t
334  unpackValue (T& outVal, const input_buffer_type& inBuf)
335  {
336  // It's actually OK for packValueCount to return an upper bound
337  // (e.g., padding for alignment). The memcpy call below will copy
338  // any included padding as well as the actual data.
339  const size_t numBytes = packValueCount (outVal);
340 
341  // FIXME (mfh 02,05 Feb 2015) This may assume UVM. On the other
342  // hand, reinterpret_cast may break aliasing and/or alignment
343  // rules.
344  memcpy (&outVal, inBuf.ptr_on_device (), numBytes);
345  return numBytes;
346  }
347 }; // struct PackTraits
348 
349 } // namespace Details
350 } // namespace Tpetra
351 
352 #endif // TPETRA_DETAILS_PACKTRAITS_HPP
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Traits class for packing / unpacking data of type T, using Kokkos data structures that live in the gi...
static size_t packValue(const output_buffer_type &outBuf, const T &inVal)
Pack the given value of type value_type into the given output buffer of bytes (char).
Kokkos::View< char *, D, Kokkos::MemoryUnmanaged > output_buffer_type
The type of an output buffer of bytes.
Implementation details of Tpetra.
static size_t packArray(const output_buffer_type &outBuf, const input_array_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...
Kokkos::View< const value_type *, D, Kokkos::MemoryUnmanaged > input_array_type
The type of an input array of value_type.
Kokkos::View< const char *, D, Kokkos::MemoryUnmanaged > input_buffer_type
The type of an input buffer of bytes.
static size_t unpackArray(const output_array_type &outBuf, const input_buffer_type &inBuf, const size_t numEnt)
Unpack numEnt value_type entries from the given input buffer of bytes, to the given output buffer of ...
static size_t packValueCount(const T &)
Number of bytes required to pack or unpack the given value of type value_type.
Kokkos::View< value_type *, D, Kokkos::MemoryUnmanaged > output_array_type
The type of an output array of value_type.
static size_t unpackValue(T &outVal, const input_buffer_type &inBuf)
Unpack the given value from the given output buffer.
static const bool compileTimeSize
Whether the number of bytes required to pack one instance of value_type is fixed at compile time...
T value_type
The type of data to pack or unpack.
static size_t numValuesPerScalar(const value_type &)
Given an instance of value_type allocated with the right size, return the "number of values" that mak...
static Kokkos::View< value_type *, D > allocateArray(const value_type &x, const size_t numEnt, const std::string &label="")
Given an instance of value_type allocated with the right size, allocate and return a one-dimensional ...