10 #ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
11 #define TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
13 #include "Tpetra_Details_Hash.hpp"
18 #include "Teuchos_Describable.hpp"
19 #include "Teuchos_FancyOStream.hpp"
20 #include "Teuchos_VerbosityLevel.hpp"
22 #include "Kokkos_Core.hpp"
23 #include "Kokkos_ArithTraits.hpp"
53 template<
class KeyType,
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;
72 typedef typename Kokkos::View<
const offset_type*, Kokkos::LayoutLeft,
73 device_type> ptr_type;
80 typedef typename Kokkos::View<const Kokkos::pair<KeyType, ValueType>*,
81 Kokkos::LayoutLeft, device_type> val_type;
89 KOKKOS_INLINE_FUNCTION
bool hasContiguousValues ()
const {
90 return contiguousValues_;
97 typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type>
keys_type;
136 const ValueType startingValue);
150 const ValueType startingValue);
171 const KeyType firstContigKey,
172 const KeyType lastContigKey,
173 const ValueType startingValue);
191 const KeyType firstContigKey,
192 const KeyType lastContigKey,
193 const ValueType startingValue);
204 const Teuchos::ArrayView<const ValueType>& vals);
206 template<
class K,
class V,
class D>
214 template<
class InDeviceType>
216 typename std::enable_if<! std::is_same<DeviceType, InDeviceType>::value,
int>::type* = NULL)
218 using Kokkos::ViewAllocateWithoutInitializing;
219 typedef typename ptr_type::non_const_type nonconst_ptr_type;
220 typedef typename val_type::non_const_type nonconst_val_type;
232 nonconst_ptr_type ptr (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::ptr"),
233 src.ptr_.extent (0));
235 nonconst_val_type val (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::val"),
236 src.val_.extent (0));
244 this->minKey_ = src.minKey_;
245 this->maxKey_ = src.maxKey_;
246 this->minVal_ = src.minVal_;
247 this->maxVal_ = src.maxVal_;
248 this->firstContigKey_ = src.firstContigKey_;
249 this->lastContigKey_ = src.lastContigKey_;
250 this->contiguousValues_ = src.contiguousValues_;
251 this->checkedForDuplicateKeys_ = src.checkedForDuplicateKeys_;
252 this->hasDuplicateKeys_ = src.hasDuplicateKeys_;
256 KOKKOS_INLINE_FUNCTION ValueType
get (
const KeyType& key)
const {
257 const offset_type size = this->getSize ();
261 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
266 if (this->hasContiguousValues () &&
267 key >= firstContigKey_ && key <= lastContigKey_) {
268 return static_cast<ValueType
> (key - firstContigKey_) + this->
minVal ();
274 const offset_type
start = ptr_[hashVal];
275 const offset_type end = ptr_[hashVal+1];
276 for (offset_type k = start; k < end; ++k) {
277 if (val_[k].first == key) {
278 return val_[k].second;
284 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
290 KOKKOS_INLINE_FUNCTION offset_type
numPairs ()
const {
296 if (this->hasContiguousValues ()) {
297 return val_.extent (0) +
static_cast<offset_type
> (lastContigKey_ - firstContigKey_);
300 return val_.extent (0);
312 KOKKOS_INLINE_FUNCTION KeyType
minKey ()
const {
324 KOKKOS_INLINE_FUNCTION KeyType
maxKey ()
const {
335 KOKKOS_INLINE_FUNCTION ValueType
minVal ()
const {
346 KOKKOS_INLINE_FUNCTION ValueType
maxVal ()
const {
375 describe (Teuchos::FancyOStream &out,
376 const Teuchos::EVerbosityLevel verbLevel=
377 Teuchos::Describable::verbLevel_default)
const;
391 KeyType minKey_ = ::Kokkos::ArithTraits<KeyType>::max();
398 KeyType maxKey_ = ::Kokkos::ArithTraits<KeyType>::max();
404 ValueType minVal_ = ::Kokkos::ArithTraits<ValueType>::max();
410 ValueType maxVal_ = ::Kokkos::ArithTraits<ValueType>::is_integer ?
411 ::Kokkos::ArithTraits<ValueType>::min() :
412 -::Kokkos::ArithTraits<ValueType>::max();
420 KeyType firstContigKey_ = ::Kokkos::ArithTraits<KeyType>::max();
428 KeyType lastContigKey_ = ::Kokkos::ArithTraits<KeyType>::max();
436 bool contiguousValues_ =
true;
444 bool checkedForDuplicateKeys_ =
true;
449 bool hasDuplicateKeys_ =
false;
455 bool checkForDuplicateKeys ()
const;
458 KOKKOS_INLINE_FUNCTION offset_type getSize ()
const {
459 return ptr_.extent (0) == 0 ?
460 static_cast<offset_type
> (0) :
461 static_cast<offset_type> (ptr_.extent (0) - 1);
464 typedef Kokkos::View<
const KeyType*,
465 typename ptr_type::HostMirror::array_layout,
466 typename ptr_type::HostMirror::execution_space,
467 Kokkos::MemoryUnmanaged> host_input_keys_type;
469 typedef Kokkos::View<
const ValueType*,
470 typename ptr_type::HostMirror::array_layout,
471 typename ptr_type::HostMirror::execution_space,
472 Kokkos::MemoryUnmanaged> host_input_vals_type;
482 const ValueType startingValue,
485 KeyType firstContigKey,
486 KeyType lastContigKey,
487 const bool computeInitContigKeys);
496 init (
const host_input_keys_type& keys,
497 const host_input_vals_type& vals,
505 #endif // TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
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...
Import KokkosSparse::OrdinalTraits, a traits class for "invalid" (flag) values of integer types...
KOKKOS_DEFAULTED_FUNCTION FixedHashTable()=default
Default constructor; makes an empty table.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &, const offset_type &)
The hash function.
KOKKOS_INLINE_FUNCTION ValueType minVal() const
The minimum value in the table.
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
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.
ResultType result_type
Type of the return value of the hash function.
Declare and define Tpetra::Details::copyOffsets, an implementation detail of Tpetra (in particular...
KOKKOS_INLINE_FUNCTION KeyType minKey() const
The minimum key in the table.
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.
std::string description() const
Implementation of Teuchos::Describable interface.
KOKKOS_INLINE_FUNCTION offset_type numPairs() const
Number of (key, value) pairs in the table.
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, but a different DeviceType.
void start()
Start the deep_copy counter.
KOKKOS_INLINE_FUNCTION KeyType maxKey() const
The maximum key in the table.
OffsetType offset_type
Type of offsets into the hash table's array of (key,value) pairs.
bool hasDuplicateKeys()
Whether the table has any duplicate keys.
KOKKOS_INLINE_FUNCTION ValueType maxVal() const
The maximum value 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.