44 #ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP 45 #define TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP 47 #include <Tpetra_Details_Hash.hpp> 49 #include <Teuchos_Describable.hpp> 50 #include <Kokkos_Core.hpp> 68 template<
class T1,
class T2,
69 const bool T1_is_signed = std::is_signed<T1>::value,
70 const bool T2_is_signed = std::is_signed<T2>::value>
71 struct OutputCanFitInput {
72 static const bool value =
sizeof (T1) >
sizeof (T2) ||
73 (
sizeof (T1) ==
sizeof (T2) &&
74 (std::is_unsigned<T1>::value || (std::is_signed<T1>::value && std::is_signed<T2>::value)));
82 template<
class OutputViewType,
class InputViewType,
83 const bool outputCanFitInput = OutputCanFitInput<
typename OutputViewType::non_const_value_type,
84 typename InputViewType::non_const_value_type>::value>
88 template<
class OutputViewType,
class InputViewType>
89 class CopyOffsets<OutputViewType, InputViewType, false> {
91 typedef typename OutputViewType::execution_space execution_space;
92 typedef typename OutputViewType::size_type size_type;
93 typedef bool value_type;
95 typedef typename InputViewType::non_const_value_type input_value_type;
96 typedef typename OutputViewType::non_const_value_type output_value_type;
98 CopyOffsets (
const OutputViewType& dst,
const InputViewType& src) :
107 minDstVal_ (static_cast<input_value_type> (std::numeric_limits<output_value_type>::min ())),
108 maxDstVal_ (static_cast<input_value_type> (std::numeric_limits<output_value_type>::max ()))
111 KOKKOS_INLINE_FUNCTION
void 112 operator () (
const size_type& i, value_type& noOverflow)
const {
113 const input_value_type src_i = src_(i);
114 if (src_i < minDstVal_ || src_i > maxDstVal_) {
117 dst_(i) =
static_cast<output_value_type
> (src_i);
120 KOKKOS_INLINE_FUNCTION
void init (value_type& noOverflow)
const {
124 KOKKOS_INLINE_FUNCTION
void 125 join (
volatile value_type& result,
126 const volatile value_type& current)
const {
127 result = result && current;
133 input_value_type minDstVal_;
134 input_value_type maxDstVal_;
138 template<
class OutputViewType,
class InputViewType>
139 class CopyOffsets<OutputViewType, InputViewType, true> {
141 typedef typename OutputViewType::execution_space execution_space;
142 typedef typename OutputViewType::size_type size_type;
143 typedef bool value_type;
145 CopyOffsets (
const OutputViewType& dst,
const InputViewType& src) :
150 KOKKOS_INLINE_FUNCTION
void 151 operator () (
const size_type& i, value_type& )
const {
156 KOKKOS_INLINE_FUNCTION
void init (value_type& noOverflow)
const {
160 KOKKOS_INLINE_FUNCTION
void 161 join (
volatile value_type& result,
162 const volatile value_type& current)
const {
163 result = result && current;
171 template<
class OutputViewType,
class InputViewType>
172 void copyOffsets (
const OutputViewType& dst,
const InputViewType& src) {
173 TEUCHOS_TEST_FOR_EXCEPTION
174 (dst.dimension_0 () != src.dimension_0 (), std::invalid_argument,
175 "copyOffsets: dst.dimension_0() = " << dst.dimension_0 ()
176 <<
" != src.dimension_0() = " << src.dimension_0 () <<
".");
177 typedef CopyOffsets<OutputViewType, InputViewType> functor_type;
178 bool noOverflow =
false;
179 Kokkos::parallel_reduce (dst.dimension_0 (), functor_type (dst, src), noOverflow);
180 TEUCHOS_TEST_FOR_EXCEPTION
181 (! noOverflow, std::runtime_error,
"copyOffsets: One or more values in " 182 "src were too big (in the sense of integer overflow) to fit in dst.");
212 template<
class KeyType,
217 typedef typename DeviceType::execution_space execution_space;
218 typedef typename DeviceType::memory_space memory_space;
219 typedef Kokkos::Device<execution_space, memory_space> device_type;
231 typedef typename Kokkos::View<
const offset_type*, Kokkos::LayoutLeft,
232 device_type> ptr_type;
239 typedef typename Kokkos::View<const Kokkos::pair<KeyType, ValueType>*,
240 Kokkos::LayoutLeft, device_type> val_type;
248 KOKKOS_INLINE_FUNCTION
bool hasContiguousValues ()
const {
249 return contiguousValues_;
256 typedef Kokkos::View<const KeyType*, Kokkos::LayoutLeft, device_type>
keys_type;
283 const bool keepKeys =
false);
299 const ValueType startingValue);
316 const ValueType startingValue,
317 const bool keepKeys =
false);
337 const KeyType firstContigKey,
338 const KeyType lastContigKey,
339 const ValueType startingValue,
340 const bool keepKeys =
false);
354 const Teuchos::ArrayView<const ValueType>& vals);
356 template<
class K,
class V,
class D>
364 template<
class InDeviceType>
366 typename std::enable_if<! std::is_same<DeviceType, InDeviceType>::value,
int>::type* = NULL)
368 using Kokkos::ViewAllocateWithoutInitializing;
369 typedef typename ptr_type::non_const_type nonconst_ptr_type;
370 typedef typename val_type::non_const_type nonconst_val_type;
380 nonconst_ptr_type ptr (ViewAllocateWithoutInitializing (
"ptr"),
381 src.ptr_.dimension_0 ());
382 copyOffsets (ptr, src.ptr_);
383 nonconst_val_type val (ViewAllocateWithoutInitializing (
"val"),
384 src.val_.dimension_0 ());
391 this->minKey_ = src.minKey_;
392 this->maxKey_ = src.maxKey_;
393 this->minVal_ = src.minVal_;
394 this->maxVal_ = src.maxVal_;
395 this->firstContigKey_ = src.firstContigKey_;
396 this->lastContigKey_ = src.lastContigKey_;
397 this->contiguousValues_ = src.contiguousValues_;
398 this->checkedForDuplicateKeys_ = src.checkedForDuplicateKeys_;
399 this->hasDuplicateKeys_ = src.hasDuplicateKeys_;
401 #if defined(HAVE_TPETRA_DEBUG) 403 #endif // defined(HAVE_TPETRA_DEBUG) 407 KOKKOS_INLINE_FUNCTION ValueType
get (
const KeyType& key)
const {
408 const offset_type size = this->getSize ();
417 if (this->hasContiguousValues () &&
418 key >= firstContigKey_ && key <= lastContigKey_) {
419 return static_cast<ValueType
> (key - firstContigKey_) + this->minVal ();
423 hash_type::hashFunc (key, size);
425 const offset_type start = ptr_[hashVal];
426 const offset_type end = ptr_[hashVal+1];
427 for (offset_type k = start; k < end; ++k) {
428 if (val_[k].first == key) {
429 return val_[k].second;
443 bool hasKeys ()
const;
451 KOKKOS_INLINE_FUNCTION KeyType
getKey (
const ValueType& val)
const {
455 if (val < this->minVal () || val > this->maxVal ()) {
459 const ValueType index = val - this->minVal ();
467 KOKKOS_INLINE_FUNCTION offset_type
numPairs ()
const {
473 if (this->hasContiguousValues ()) {
474 return val_.dimension_0 () +
static_cast<offset_type
> (lastContigKey_ - firstContigKey_);
477 return val_.dimension_0 ();
489 KOKKOS_INLINE_FUNCTION KeyType
minKey ()
const {
501 KOKKOS_INLINE_FUNCTION KeyType
maxKey ()
const {
512 KOKKOS_INLINE_FUNCTION ValueType
minVal ()
const {
523 KOKKOS_INLINE_FUNCTION ValueType
maxVal ()
const {
539 bool hasDuplicateKeys ();
543 std::string description ()
const;
548 describe (Teuchos::FancyOStream &out,
549 const Teuchos::EVerbosityLevel verbLevel=
550 Teuchos::Describable::verbLevel_default)
const;
599 KeyType firstContigKey_;
607 KeyType lastContigKey_;
615 bool contiguousValues_;
622 bool checkedForDuplicateKeys_;
627 bool hasDuplicateKeys_;
633 bool checkForDuplicateKeys ()
const;
636 KOKKOS_INLINE_FUNCTION offset_type getSize ()
const {
637 return ptr_.dimension_0 () == 0 ?
638 static_cast<offset_type
> (0) :
639 static_cast<offset_type> (ptr_.dimension_0 () - 1);
645 typedef Kokkos::View<
const KeyType*,
646 typename ptr_type::HostMirror::array_layout,
647 typename ptr_type::HostMirror::execution_space,
648 Kokkos::MemoryUnmanaged> host_input_keys_type;
650 typedef Kokkos::View<
const ValueType*,
651 typename ptr_type::HostMirror::array_layout,
652 typename ptr_type::HostMirror::execution_space,
653 Kokkos::MemoryUnmanaged> host_input_vals_type;
662 init (
const keys_type& keys,
663 const ValueType startingValue,
666 KeyType firstContigKey,
667 KeyType lastContigKey,
668 const bool computeInitContigKeys);
677 init (
const host_input_keys_type& keys,
678 const host_input_vals_type& vals,
686 #endif // TPETRA_DETAILS_FIXEDHASHTABLE_DECL_HPP Namespace Tpetra contains the class and methods constituting the Tpetra library.
Traits class for "invalid" (flag) values of integer types that Tpetra uses as local ordinals or globa...
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.
ResultType result_type
Type of the return value of the hash function.
void deep_copy(const LittleBlock< ST2, LO > &dst, const LittleBlock< ST1, LO > &src, typename std::enable_if< std::is_convertible< ST1, ST2 >::value &&!std::is_const< ST2 >::value, int >::type *=NULL)
Copy the LittleBlock src into the LittleBlock dst.
Implementation details of Tpetra.
KOKKOS_INLINE_FUNCTION KeyType minKey() const
The minimum key in the table.
Traits class for "invalid" (flag) values of integer types that Tpetra uses as local ordinals or globa...
The hash function for FixedHashTable.
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.
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.
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.