44 #ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
45 #define TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
47 #include "Tpetra_Details_Hash.hpp"
52 #include "Teuchos_Describable.hpp"
53 #include "Teuchos_FancyOStream.hpp"
54 #include "Teuchos_VerbosityLevel.hpp"
56 #include "Kokkos_Core.hpp"
57 #include "Kokkos_ArithTraits.hpp"
87 template<
class KeyType,
92 typedef typename DeviceType::execution_space execution_space;
93 typedef typename DeviceType::memory_space memory_space;
94 typedef Kokkos::Device<execution_space, memory_space> device_type;
106 typedef typename Kokkos::View<
const offset_type*, Kokkos::LayoutLeft,
107 device_type> ptr_type;
114 typedef typename Kokkos::View<const Kokkos::pair<KeyType, ValueType>*,
115 Kokkos::LayoutLeft, device_type> val_type;
123 KOKKOS_INLINE_FUNCTION
bool hasContiguousValues ()
const {
124 return contiguousValues_;
131 typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type>
keys_type;
170 const ValueType startingValue);
184 const ValueType startingValue);
205 const KeyType firstContigKey,
206 const KeyType lastContigKey,
207 const ValueType startingValue);
225 const KeyType firstContigKey,
226 const KeyType lastContigKey,
227 const ValueType startingValue);
238 const Teuchos::ArrayView<const ValueType>& vals);
240 template<
class K,
class V,
class D>
248 template<
class InDeviceType>
250 typename std::enable_if<! std::is_same<DeviceType, InDeviceType>::value,
int>::type* = NULL)
252 using Kokkos::ViewAllocateWithoutInitializing;
253 typedef typename ptr_type::non_const_type nonconst_ptr_type;
254 typedef typename val_type::non_const_type nonconst_val_type;
266 nonconst_ptr_type ptr (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::ptr"),
267 src.ptr_.extent (0));
269 nonconst_val_type val (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::val"),
270 src.val_.extent (0));
278 this->minKey_ = src.minKey_;
279 this->maxKey_ = src.maxKey_;
280 this->minVal_ = src.minVal_;
281 this->maxVal_ = src.maxVal_;
282 this->firstContigKey_ = src.firstContigKey_;
283 this->lastContigKey_ = src.lastContigKey_;
284 this->contiguousValues_ = src.contiguousValues_;
285 this->checkedForDuplicateKeys_ = src.checkedForDuplicateKeys_;
286 this->hasDuplicateKeys_ = src.hasDuplicateKeys_;
290 KOKKOS_INLINE_FUNCTION ValueType
get (
const KeyType& key)
const {
291 const offset_type size = this->getSize ();
295 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
300 if (this->hasContiguousValues () &&
301 key >= firstContigKey_ && key <= lastContigKey_) {
302 return static_cast<ValueType
> (key - firstContigKey_) + this->
minVal ();
308 const offset_type
start = ptr_[hashVal];
309 const offset_type end = ptr_[hashVal+1];
310 for (offset_type k = start; k < end; ++k) {
311 if (val_[k].first == key) {
312 return val_[k].second;
318 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
324 KOKKOS_INLINE_FUNCTION offset_type
numPairs ()
const {
330 if (this->hasContiguousValues ()) {
331 return val_.extent (0) +
static_cast<offset_type
> (lastContigKey_ - firstContigKey_);
334 return val_.extent (0);
346 KOKKOS_INLINE_FUNCTION KeyType
minKey ()
const {
358 KOKKOS_INLINE_FUNCTION KeyType
maxKey ()
const {
369 KOKKOS_INLINE_FUNCTION ValueType
minVal ()
const {
380 KOKKOS_INLINE_FUNCTION ValueType
maxVal ()
const {
409 describe (Teuchos::FancyOStream &out,
410 const Teuchos::EVerbosityLevel verbLevel=
411 Teuchos::Describable::verbLevel_default)
const;
425 KeyType minKey_ = ::Kokkos::ArithTraits<KeyType>::max();
432 KeyType maxKey_ = ::Kokkos::ArithTraits<KeyType>::is_integer ?
433 ::Kokkos::ArithTraits<KeyType>::min() :
434 -::Kokkos::ArithTraits<KeyType>::max();
440 ValueType minVal_ = ::Kokkos::ArithTraits<ValueType>::max();
446 ValueType maxVal_ = ::Kokkos::ArithTraits<ValueType>::is_integer ?
447 ::Kokkos::ArithTraits<ValueType>::min() :
448 -::Kokkos::ArithTraits<ValueType>::max();
456 KeyType firstContigKey_ = ::Kokkos::ArithTraits<KeyType>::max();
464 KeyType lastContigKey_ = ::Kokkos::ArithTraits<KeyType>::is_integer ?
465 ::Kokkos::ArithTraits<KeyType>::min() :
466 -::Kokkos::ArithTraits<KeyType>::max();
474 bool contiguousValues_ =
true;
482 bool checkedForDuplicateKeys_ =
true;
487 bool hasDuplicateKeys_ =
false;
493 bool checkForDuplicateKeys ()
const;
496 KOKKOS_INLINE_FUNCTION offset_type getSize ()
const {
497 return ptr_.extent (0) == 0 ?
498 static_cast<offset_type
> (0) :
499 static_cast<offset_type> (ptr_.extent (0) - 1);
502 typedef Kokkos::View<
const KeyType*,
503 typename ptr_type::HostMirror::array_layout,
504 typename ptr_type::HostMirror::execution_space,
505 Kokkos::MemoryUnmanaged> host_input_keys_type;
507 typedef Kokkos::View<
const ValueType*,
508 typename ptr_type::HostMirror::array_layout,
509 typename ptr_type::HostMirror::execution_space,
510 Kokkos::MemoryUnmanaged> host_input_vals_type;
520 const ValueType startingValue,
523 KeyType firstContigKey,
524 KeyType lastContigKey,
525 const bool computeInitContigKeys);
534 init (
const host_input_keys_type& keys,
535 const host_input_vals_type& vals,
543 #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.