44 #ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
45 #define TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP
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"
83 template<
class KeyType,
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;
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;
119 KOKKOS_INLINE_FUNCTION
bool hasContiguousValues ()
const {
120 return contiguousValues_;
127 typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type>
keys_type;
154 const bool keepKeys =
false);
170 const ValueType startingValue);
187 const ValueType startingValue,
188 const bool keepKeys =
false);
212 const KeyType firstContigKey,
213 const KeyType lastContigKey,
214 const ValueType startingValue,
215 const bool keepKeys =
false);
236 const KeyType firstContigKey,
237 const KeyType lastContigKey,
238 const ValueType startingValue,
239 const bool keepKeys =
false);
253 const Teuchos::ArrayView<const ValueType>& vals);
255 template<
class K,
class V,
class D>
263 template<
class InDeviceType>
265 typename std::enable_if<! std::is_same<DeviceType, InDeviceType>::value,
int>::type* = NULL)
267 using Kokkos::ViewAllocateWithoutInitializing;
268 typedef typename ptr_type::non_const_type nonconst_ptr_type;
269 typedef typename val_type::non_const_type nonconst_val_type;
279 nonconst_ptr_type ptr (ViewAllocateWithoutInitializing (
"ptr"),
280 src.ptr_.extent (0));
282 nonconst_val_type val (ViewAllocateWithoutInitializing (
"val"),
283 src.val_.extent (0));
290 this->minKey_ = src.minKey_;
291 this->maxKey_ = src.maxKey_;
292 this->minVal_ = src.minVal_;
293 this->maxVal_ = src.maxVal_;
294 this->firstContigKey_ = src.firstContigKey_;
295 this->lastContigKey_ = src.lastContigKey_;
296 this->contiguousValues_ = src.contiguousValues_;
297 this->checkedForDuplicateKeys_ = src.checkedForDuplicateKeys_;
298 this->hasDuplicateKeys_ = src.hasDuplicateKeys_;
300 #if defined(HAVE_TPETRA_DEBUG)
302 #endif // defined(HAVE_TPETRA_DEBUG)
306 KOKKOS_INLINE_FUNCTION ValueType
get (
const KeyType& key)
const {
307 const offset_type size = this->getSize ();
311 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
316 if (this->hasContiguousValues () &&
317 key >= firstContigKey_ && key <= lastContigKey_) {
318 return static_cast<ValueType
> (key - firstContigKey_) + this->
minVal ();
324 const offset_type start = ptr_[hashVal];
325 const offset_type end = ptr_[hashVal+1];
326 for (offset_type k = start; k < end; ++k) {
327 if (val_[k].first == key) {
328 return val_[k].second;
334 return Tpetra::Details::OrdinalTraits<ValueType>::invalid ();
350 KOKKOS_INLINE_FUNCTION KeyType
getKey (
const ValueType& val)
const {
355 return Tpetra::Details::OrdinalTraits<KeyType>::invalid ();
358 const ValueType index = val - this->
minVal ();
366 KOKKOS_INLINE_FUNCTION offset_type
numPairs ()
const {
372 if (this->hasContiguousValues ()) {
373 return val_.extent (0) +
static_cast<offset_type
> (lastContigKey_ - firstContigKey_);
376 return val_.extent (0);
388 KOKKOS_INLINE_FUNCTION KeyType
minKey ()
const {
400 KOKKOS_INLINE_FUNCTION KeyType
maxKey ()
const {
411 KOKKOS_INLINE_FUNCTION ValueType
minVal ()
const {
422 KOKKOS_INLINE_FUNCTION ValueType
maxVal ()
const {
451 describe (Teuchos::FancyOStream &out,
452 const Teuchos::EVerbosityLevel verbLevel=
453 Teuchos::Describable::verbLevel_default)
const;
458 std::string objectLabel_;
505 KeyType firstContigKey_;
513 KeyType lastContigKey_;
521 bool contiguousValues_;
528 bool checkedForDuplicateKeys_;
533 bool hasDuplicateKeys_;
539 bool checkForDuplicateKeys ()
const;
542 KOKKOS_INLINE_FUNCTION offset_type getSize ()
const {
543 return ptr_.extent (0) == 0 ?
544 static_cast<offset_type
> (0) :
545 static_cast<offset_type> (ptr_.extent (0) - 1);
551 typedef Kokkos::View<
const KeyType*,
552 typename ptr_type::HostMirror::array_layout,
553 typename ptr_type::HostMirror::execution_space,
554 Kokkos::MemoryUnmanaged> host_input_keys_type;
556 typedef Kokkos::View<
const ValueType*,
557 typename ptr_type::HostMirror::array_layout,
558 typename ptr_type::HostMirror::execution_space,
559 Kokkos::MemoryUnmanaged> host_input_vals_type;
569 const ValueType startingValue,
572 KeyType firstContigKey,
573 KeyType lastContigKey,
574 const bool computeInitContigKeys);
583 init (
const host_input_keys_type& keys,
584 const host_input_vals_type& vals,
592 #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_INLINE_FUNCTION KeyType getKey(const ValueType &val) const
Get the key corresponding to the given value.
KOKKOS_INLINE_FUNCTION ValueType minVal() const
The minimum value in the table.
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...
bool hasKeys() const
Whether it is safe to call getKey().
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.
FixedHashTable()
Default constructor; makes an empty table.
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.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &key, const offset_type &size)
The hash function.
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.