Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_FixedHashTable_decl.hpp
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_FIXEDHASHTABLE_DECL_HPP
11#define TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
12
13#include "Tpetra_Details_Hash.hpp"
17
18#include "Teuchos_Describable.hpp"
19#include "Teuchos_FancyOStream.hpp"
20#include "Teuchos_VerbosityLevel.hpp"
21
22#include "Kokkos_Core.hpp"
23#include "KokkosKernels_ArithTraits.hpp"
24
25namespace Tpetra {
26namespace Details {
27
53template <class KeyType,
54 class ValueType,
55 class DeviceType>
56class FixedHashTable {
57 private:
58 typedef typename DeviceType::execution_space execution_space;
59 typedef typename DeviceType::memory_space memory_space;
60 typedef Kokkos::Device<execution_space, memory_space> device_type;
61
62 typedef Hash<KeyType, device_type> hash_type;
63 typedef typename hash_type::offset_type offset_type;
64
72 typedef typename Kokkos::View<const offset_type*, Kokkos::LayoutLeft,
73 device_type>
74 ptr_type;
81 typedef typename Kokkos::View<const Kokkos::pair<KeyType, ValueType>*,
82 Kokkos::LayoutLeft, device_type>
83 val_type;
84
91 KOKKOS_INLINE_FUNCTION bool hasContiguousValues() const {
92 return contiguousValues_;
93 }
94
95 public:
99 typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type> keys_type;
100
102 KOKKOS_DEFAULTED_FUNCTION FixedHashTable() = default;
103
114
122 FixedHashTable(const Teuchos::ArrayView<const KeyType>& keys);
123
138 const ValueType startingValue);
139
151 FixedHashTable(const Teuchos::ArrayView<const KeyType>& keys,
152 const ValueType startingValue);
153
173 const KeyType firstContigKey,
174 const KeyType lastContigKey,
175 const ValueType startingValue);
176
192 FixedHashTable(const Teuchos::ArrayView<const KeyType>& keys,
193 const KeyType firstContigKey,
194 const KeyType lastContigKey,
195 const ValueType startingValue);
196
205 FixedHashTable(const Teuchos::ArrayView<const KeyType>& keys,
206 const Teuchos::ArrayView<const ValueType>& vals);
207
208 template <class K, class V, class D>
209 friend class FixedHashTable;
210
216 template <class InDeviceType>
217 FixedHashTable(const FixedHashTable<KeyType, ValueType, InDeviceType>& src,
218 typename std::enable_if<!std::is_same<DeviceType, InDeviceType>::value, int>::type* = NULL) {
219 using Kokkos::ViewAllocateWithoutInitializing;
220 typedef typename ptr_type::non_const_type nonconst_ptr_type;
221 typedef typename val_type::non_const_type nonconst_val_type;
222
223 Tpetra::Details::ProfilingRegion pr("Tpetra::Details::FixedHashTable::ctor(InDeviceType)");
224
225 // FIXME (mfh 28 May 2015) The code below _always_ copies. This
226 // shouldn't be necessary if the input and output memory spaces
227 // are the same. However, it is always correct.
228
229 // Different Devices may have different offset_type, because
230 // offset_type comes from the memory space's size_type typedef.
231 // That's why we use a specialized deep copy function here instead
232 // of Kokkos::deep_copy.
233 nonconst_ptr_type ptr(ViewAllocateWithoutInitializing("Tpetra::FixedHashTable::ptr"),
234 src.ptr_.extent(0));
235 ::Tpetra::Details::copyOffsets(ptr, src.ptr_);
236 nonconst_val_type val(ViewAllocateWithoutInitializing("Tpetra::FixedHashTable::val"),
237 src.val_.extent(0));
238 // val and src.val_ have the same entry types, unlike (possibly)
239 // ptr and src.ptr_. Thus, we can use Kokkos::deep_copy here.
240 // DEEP_COPY REVIEW - DEVICE-TO-DEVICE
241 Kokkos::deep_copy(execution_space(), val, src.val_);
242
243 this->ptr_ = ptr;
244 this->val_ = val;
245 this->minKey_ = src.minKey_;
246 this->maxKey_ = src.maxKey_;
247 this->minVal_ = src.minVal_;
248 this->maxVal_ = src.maxVal_;
249 this->firstContigKey_ = src.firstContigKey_;
250 this->lastContigKey_ = src.lastContigKey_;
251 this->contiguousValues_ = src.contiguousValues_;
252 this->checkedForDuplicateKeys_ = src.checkedForDuplicateKeys_;
253 this->hasDuplicateKeys_ = src.hasDuplicateKeys_;
254 }
255
257 KOKKOS_INLINE_FUNCTION ValueType get(const KeyType& key) const {
258 const offset_type size = this->getSize();
259 if (size == 0) {
260 // Don't use Teuchos::OrdinalTraits or std::numeric_limits here,
261 // because neither have the right device function markings.
262 return Tpetra::Details::OrdinalTraits<ValueType>::invalid();
263 }
264
265 // If this object assumes contiguous values, then it doesn't store
266 // the initial sequence of >= 1 contiguous keys in the table.
267 if (this->hasContiguousValues() &&
268 key >= firstContigKey_ && key <= lastContigKey_) {
269 return static_cast<ValueType>(key - firstContigKey_) + this->minVal();
270 }
271
272 const typename hash_type::result_type hashVal =
273 hash_type::hashFunc(key, size);
274
275 const offset_type start = ptr_[hashVal];
276 const offset_type end = ptr_[hashVal + 1];
277 for (offset_type k = start; k < end; ++k) {
278 if (val_[k].first == key) {
279 return val_[k].second;
280 }
281 }
282
283 // Don't use Teuchos::OrdinalTraits or std::numeric_limits here,
284 // because neither have the right device function markings.
285 return Tpetra::Details::OrdinalTraits<ValueType>::invalid();
286 }
287
291 KOKKOS_INLINE_FUNCTION offset_type numPairs() const {
292 // NOTE (mfh 26 May 2015) Using val_.extent(0) only works
293 // because the table stores pairs with duplicate keys separately.
294 // If the table didn't do that, we would have to keep a separate
295 // numPairs_ field (remembering the size of the input array of
296 // keys).
297 if (this->hasContiguousValues()) {
298 return val_.extent(0) + static_cast<offset_type>(lastContigKey_ - firstContigKey_);
299 } else {
300 return val_.extent(0);
301 }
302 }
303
312 KOKKOS_INLINE_FUNCTION KeyType minKey() const {
313 return minKey_;
314 }
315
324 KOKKOS_INLINE_FUNCTION KeyType maxKey() const {
325 return maxKey_;
326 }
327
335 KOKKOS_INLINE_FUNCTION ValueType minVal() const {
336 return minVal_;
337 }
338
346 KOKKOS_INLINE_FUNCTION ValueType maxVal() const {
347 return maxVal_;
348 }
349
363
369
370
371 std::string description() const;
372
374 void
375 describe(Teuchos::FancyOStream& out,
376 const Teuchos::EVerbosityLevel verbLevel =
377 Teuchos::Describable::verbLevel_default) const;
379
380 private:
382 ptr_type ptr_;
384 val_type val_;
385
391 KeyType minKey_ = ::KokkosKernels::ArithTraits<KeyType>::max();
392
398 KeyType maxKey_ = ::KokkosKernels::ArithTraits<KeyType>::max();
399
404 ValueType minVal_ = ::KokkosKernels::ArithTraits<ValueType>::max();
405
410 ValueType maxVal_ = ::KokkosKernels::ArithTraits<ValueType>::is_integer ? ::KokkosKernels::ArithTraits<ValueType>::min() : -::KokkosKernels::ArithTraits<ValueType>::max();
411
418 KeyType firstContigKey_ = ::KokkosKernels::ArithTraits<KeyType>::max();
419
426 KeyType lastContigKey_ = ::KokkosKernels::ArithTraits<KeyType>::max();
427
434 bool contiguousValues_ = true;
435
442 bool checkedForDuplicateKeys_ = true;
443
447 bool hasDuplicateKeys_ = false;
448
453 bool checkForDuplicateKeys() const;
454
456 KOKKOS_INLINE_FUNCTION offset_type getSize() const {
457 return ptr_.extent(0) == 0 ? static_cast<offset_type>(0) : static_cast<offset_type>(ptr_.extent(0) - 1);
458 }
459
460 typedef Kokkos::View<const KeyType*,
461 typename ptr_type::host_mirror_type::array_layout,
462 typename ptr_type::host_mirror_type::execution_space,
463 Kokkos::MemoryUnmanaged>
464 host_input_keys_type;
465
466 typedef Kokkos::View<const ValueType*,
467 typename ptr_type::host_mirror_type::array_layout,
468 typename ptr_type::host_mirror_type::execution_space,
469 Kokkos::MemoryUnmanaged>
470 host_input_vals_type;
471
478 void
479 init(const keys_type& keys,
480 const ValueType startingValue,
481 KeyType initMinKey,
482 KeyType initMaxKey,
483 KeyType firstContigKey,
484 KeyType lastContigKey,
485 const bool computeInitContigKeys);
486
493 void
494 init(const host_input_keys_type& keys,
495 const host_input_vals_type& vals,
496 KeyType initMinKey,
497 KeyType initMaxKey);
498};
499
500} // namespace Details
501} // namespace Tpetra
502
503#endif // TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
Import KokkosSparse::OrdinalTraits, a traits class for "invalid" (flag) values of integer types,...
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
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.
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.
FixedHashTable(const Teuchos::ArrayView< const KeyType > &keys, const ValueType startingValue)
Constructor for arbitrary keys and contiguous values starting with startingValue.
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.
FixedHashTable(const Teuchos::ArrayView< const KeyType > &keys, const Teuchos::ArrayView< const ValueType > &vals)
Constructor for arbitrary keys and arbitrary values.
KOKKOS_DEFAULTED_FUNCTION FixedHashTable()=default
Default constructor; makes an empty table.
FixedHashTable(const Teuchos::ArrayView< const KeyType > &keys)
Constructor for arbitrary keys and contiguous values starting with zero.
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.
FixedHashTable(const keys_type &keys, const ValueType startingValue)
Constructor for arbitrary keys and contiguous values starting with startingValue.
FixedHashTable(const Teuchos::ArrayView< const KeyType > &keys, const KeyType firstContigKey, const KeyType lastContigKey, const ValueType startingValue)
Constructor for arbitrary keys and contiguous values starting with startingValue, that takes an initi...
FixedHashTable(const keys_type &keys, const KeyType firstContigKey, const KeyType lastContigKey, const ValueType startingValue)
Constructor for arbitrary keys and contiguous values starting with startingValue, that takes an initi...
FixedHashTable(const keys_type &keys)
Constructor for arbitrary keys and contiguous values starting with zero.
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,...
Nonmember function that computes a residual Computes R = B - A * X.
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.
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 &, const offset_type &)
typename std::make_signed< typename Kokkos::View< KeyType *, device_type >::size_type >::type offset_type