Tpetra parallel linear algebra Version of the Day
Tpetra_Details_FixedHashTable_decl.hpp
1/*
2// @HEADER
3// ***********************************************************************
4//
5// Tpetra: Templated Linear Algebra Services Package
6// Copyright (2008) Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39//
40// ************************************************************************
41// @HEADER
42*/
43
44#ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
45#define TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
46
47#include "Tpetra_Details_Hash.hpp"
50#include "Teuchos_Describable.hpp"
51#include "Teuchos_FancyOStream.hpp"
52#include "Teuchos_VerbosityLevel.hpp"
53#include "Kokkos_Core.hpp"
54
55namespace Tpetra {
56namespace Details {
57
83template<class KeyType,
84 class ValueType,
85 class DeviceType>
87private:
88 typedef typename DeviceType::execution_space execution_space;
89 typedef typename DeviceType::memory_space memory_space;
90 typedef Kokkos::Device<execution_space, memory_space> device_type;
91
93 typedef typename hash_type::offset_type offset_type;
94
102 typedef typename Kokkos::View<const offset_type*, Kokkos::LayoutLeft,
103 device_type> ptr_type;
110 typedef typename Kokkos::View<const Kokkos::pair<KeyType, ValueType>*,
111 Kokkos::LayoutLeft, device_type> val_type;
112
119 KOKKOS_INLINE_FUNCTION bool hasContiguousValues () const {
120 return contiguousValues_;
121 }
122
123public:
127 typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type> keys_type;
128
131
141 FixedHashTable (const keys_type& keys);
142
150 FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys);
151
165 FixedHashTable (const keys_type& keys,
166 const ValueType startingValue);
167
179 FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys,
180 const ValueType startingValue);
181
200 FixedHashTable (const keys_type& keys,
201 const KeyType firstContigKey,
202 const KeyType lastContigKey,
203 const ValueType startingValue);
204
220 FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys,
221 const KeyType firstContigKey,
222 const KeyType lastContigKey,
223 const ValueType startingValue);
224
233 FixedHashTable (const Teuchos::ArrayView<const KeyType>& keys,
234 const Teuchos::ArrayView<const ValueType>& vals);
235
236 template<class K, class V, class D>
237 friend class FixedHashTable;
238
244 template<class InDeviceType>
246 typename std::enable_if<! std::is_same<DeviceType, InDeviceType>::value, int>::type* = NULL)
247 {
248 using Kokkos::ViewAllocateWithoutInitializing;
249 typedef typename ptr_type::non_const_type nonconst_ptr_type;
250 typedef typename val_type::non_const_type nonconst_val_type;
251
252 // FIXME (mfh 28 May 2015) The code below _always_ copies. This
253 // shouldn't be necessary if the input and output memory spaces
254 // are the same. However, it is always correct.
255
256 // Different Devices may have different offset_type, because
257 // offset_type comes from the memory space's size_type typedef.
258 // That's why we use a specialized deep copy function here instead
259 // of Kokkos::deep_copy.
260 nonconst_ptr_type ptr (ViewAllocateWithoutInitializing ("Tpetra::FixedHashTable::ptr"),
261 src.ptr_.extent (0));
262 ::Tpetra::Details::copyOffsets (ptr, src.ptr_);
263 nonconst_val_type val (ViewAllocateWithoutInitializing ("Tpetra::FixedHashTable::val"),
264 src.val_.extent (0));
265 // val and src.val_ have the same entry types, unlike (possibly)
266 // ptr and src.ptr_. Thus, we can use Kokkos::deep_copy here.
267 Kokkos::deep_copy (val, src.val_);
268
269 this->ptr_ = ptr;
270 this->val_ = val;
271 this->minKey_ = src.minKey_;
272 this->maxKey_ = src.maxKey_;
273 this->minVal_ = src.minVal_;
274 this->maxVal_ = src.maxVal_;
275 this->firstContigKey_ = src.firstContigKey_;
276 this->lastContigKey_ = src.lastContigKey_;
277 this->contiguousValues_ = src.contiguousValues_;
278 this->checkedForDuplicateKeys_ = src.checkedForDuplicateKeys_;
279 this->hasDuplicateKeys_ = src.hasDuplicateKeys_;
280 }
281
283 KOKKOS_INLINE_FUNCTION ValueType get (const KeyType& key) const {
284 const offset_type size = this->getSize ();
285 if (size == 0) {
286 // Don't use Teuchos::OrdinalTraits or std::numeric_limits here,
287 // because neither have the right device function markings.
288 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
289 }
290
291 // If this object assumes contiguous values, then it doesn't store
292 // the initial sequence of >= 1 contiguous keys in the table.
293 if (this->hasContiguousValues () &&
294 key >= firstContigKey_ && key <= lastContigKey_) {
295 return static_cast<ValueType> (key - firstContigKey_) + this->minVal ();
296 }
297
298 const typename hash_type::result_type hashVal =
299 hash_type::hashFunc (key, size);
300
301 const offset_type start = ptr_[hashVal];
302 const offset_type end = ptr_[hashVal+1];
303 for (offset_type k = start; k < end; ++k) {
304 if (val_[k].first == key) {
305 return val_[k].second;
306 }
307 }
308
309 // Don't use Teuchos::OrdinalTraits or std::numeric_limits here,
310 // because neither have the right device function markings.
311 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
312 }
313
317 KOKKOS_INLINE_FUNCTION offset_type numPairs () const {
318 // NOTE (mfh 26 May 2015) Using val_.extent(0) only works
319 // because the table stores pairs with duplicate keys separately.
320 // If the table didn't do that, we would have to keep a separate
321 // numPairs_ field (remembering the size of the input array of
322 // keys).
323 if (this->hasContiguousValues ()) {
324 return val_.extent (0) + static_cast<offset_type> (lastContigKey_ - firstContigKey_);
325 }
326 else {
327 return val_.extent (0);
328 }
329 }
330
339 KOKKOS_INLINE_FUNCTION KeyType minKey () const {
340 return minKey_;
341 }
342
351 KOKKOS_INLINE_FUNCTION KeyType maxKey () const {
352 return maxKey_;
353 }
354
362 KOKKOS_INLINE_FUNCTION ValueType minVal () const {
363 return minVal_;
364 }
365
373 KOKKOS_INLINE_FUNCTION ValueType maxVal () const {
374 return maxVal_;
375 }
376
389 bool hasDuplicateKeys ();
390
396
397
398 std::string description () const;
399
401 void
402 describe (Teuchos::FancyOStream &out,
403 const Teuchos::EVerbosityLevel verbLevel=
404 Teuchos::Describable::verbLevel_default) const;
406
407private:
409 ptr_type ptr_;
411 val_type val_;
412
417 KeyType minKey_;
418
423 KeyType maxKey_;
424
429 ValueType minVal_;
430
435 ValueType maxVal_;
436
443 KeyType firstContigKey_;
444
451 KeyType lastContigKey_;
452
459 bool contiguousValues_;
460
466 bool checkedForDuplicateKeys_;
467
471 bool hasDuplicateKeys_;
472
477 bool checkForDuplicateKeys () const;
478
480 KOKKOS_INLINE_FUNCTION offset_type getSize () const {
481 return ptr_.extent (0) == 0 ?
482 static_cast<offset_type> (0) :
483 static_cast<offset_type> (ptr_.extent (0) - 1);
484 }
485
486 typedef Kokkos::View<const KeyType*,
487 typename ptr_type::HostMirror::array_layout,
488 typename ptr_type::HostMirror::execution_space,
489 Kokkos::MemoryUnmanaged> host_input_keys_type;
490
491 typedef Kokkos::View<const ValueType*,
492 typename ptr_type::HostMirror::array_layout,
493 typename ptr_type::HostMirror::execution_space,
494 Kokkos::MemoryUnmanaged> host_input_vals_type;
495
502 void
503 init (const keys_type& keys,
504 const ValueType startingValue,
505 KeyType initMinKey,
506 KeyType initMaxKey,
507 KeyType firstContigKey,
508 KeyType lastContigKey,
509 const bool computeInitContigKeys);
510
517 void
518 init (const host_input_keys_type& keys,
519 const host_input_vals_type& vals,
520 KeyType initMinKey,
521 KeyType initMaxKey);
522};
523
524} // Details namespace
525} // Tpetra namespace
526
527#endif // TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
Import KokkosSparse::OrdinalTraits, a traits class for "invalid" (flag) values of integer types,...
Declare and define Tpetra::Details::copyOffsets, an implementation detail of Tpetra (in particular,...
KOKKOS_INLINE_FUNCTION ValueType minVal() const
The minimum value in the table.
std::string description() const
Implementation of Teuchos::Describable interface.
bool hasDuplicateKeys()
Whether the table has any duplicate keys.
FixedHashTable(const FixedHashTable< KeyType, ValueType, InDeviceType > &src, typename std::enable_if<! std::is_same< DeviceType, InDeviceType >::value, int >::type *=NULL)
"Copy" constructor that takes a FixedHashTable with the same KeyType and ValueType,...
KOKKOS_INLINE_FUNCTION offset_type numPairs() const
Number of (key, value) pairs in the table.
Kokkos::View< const KeyType *, Kokkos::LayoutLeft, device_type > keys_type
Type of a 1-D Kokkos::View (array) used to store keys.
KOKKOS_INLINE_FUNCTION KeyType maxKey() const
The maximum key in the table.
KOKKOS_INLINE_FUNCTION ValueType maxVal() const
The maximum value in the table.
KOKKOS_INLINE_FUNCTION KeyType minKey() const
The minimum key in the table.
KOKKOS_INLINE_FUNCTION ValueType get(const KeyType &key) const
Get the value corresponding to the given key.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print this object with the given verbosity to the output stream.
Implementation details of Tpetra.
void copyOffsets(const OutputViewType &dst, const InputViewType &src)
Copy row offsets (in a sparse graph or matrix) from src to dst. The offsets may have different types.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
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.
The hash function for FixedHashTable.
ResultType result_type
Type of the return value of the hash function.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &key, const offset_type &size)
The hash function.
OffsetType offset_type
Type of offsets into the hash table's array of (key,value) pairs.