44#ifndef TPETRA_DETAILS_FIXEDHASHTABLE_DEF_HPP
45#define TPETRA_DETAILS_FIXEDHASHTABLE_DEF_HPP
47#include "Tpetra_Details_FixedHashTable_decl.hpp"
49#ifdef TPETRA_USE_MURMUR_HASH
50# include "Kokkos_Functional.hpp"
52#include "Kokkos_ArithTraits.hpp"
53#include "Teuchos_TypeNameTraits.hpp"
78template<
class ExecSpace>
79bool worthBuildingFixedHashTableInParallel () {
80 return ExecSpace::concurrency() > 1;
101template<
class CountsViewType,
103 class SizeType =
typename KeysViewType::size_type>
106 typedef CountsViewType counts_view_type;
107 typedef KeysViewType keys_view_type;
108 typedef typename CountsViewType::execution_space execution_space;
109 typedef typename CountsViewType::memory_space memory_space;
110 typedef SizeType size_type;
111 typedef typename keys_view_type::non_const_value_type key_type;
127 const keys_view_type& keys,
128 const size_type size) :
138 KOKKOS_INLINE_FUNCTION
void
144 Kokkos::atomic_increment (&counts_[hashVal]);
147 using value_type = Kokkos::pair<int, key_type>;
152 KOKKOS_INLINE_FUNCTION
void
157 const key_type keyVal = keys_[i];
159 if (hashVal < hash_value_type (0) ||
160 hashVal >= hash_value_type (counts_.extent (0))) {
165 Kokkos::atomic_increment (&counts_[hashVal]);
170 KOKKOS_INLINE_FUNCTION
void init (value_type& dst)
const
173 dst.second = key_type (0);
176 KOKKOS_INLINE_FUNCTION
void
177 join (
volatile value_type& dst,
178 const volatile value_type& src)
const
180 const bool good = dst.first == 0 || src.first == 0;
181 dst.first = good ? 0 : dst.first;
187 counts_view_type counts_;
189 keys_view_type keys_;
204template<
class KeyType>
206 KOKKOS_INLINE_FUNCTION
208 minKey_ (::Kokkos::Details::ArithTraits<KeyType>::max ()),
221 maxKey_ (::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
222 ::Kokkos::Details::ArithTraits<KeyType>::min () :
223 -::Kokkos::Details::ArithTraits<KeyType>::max ()),
226 static_assert (std::is_arithmetic<KeyType>::value,
"FillPairsResult: "
227 "KeyType must be some kind of number type.");
230 KOKKOS_INLINE_FUNCTION
231 FillPairsResult (
const KeyType& initMinKey,
232 const KeyType& initMaxKey) :
237 static_assert (std::is_arithmetic<KeyType>::value,
"FillPairsResult: "
238 "KeyType must be some kind of number type.");
275template<
class PairsViewType,
277 class CountsViewType,
278 class SizeType =
typename KeysViewType::size_type>
281 typedef typename CountsViewType::non_const_type counts_view_type;
282 typedef typename counts_view_type::const_type offsets_view_type;
284 typedef PairsViewType pairs_view_type;
285 typedef typename KeysViewType::const_type keys_view_type;
286 typedef typename offsets_view_type::execution_space execution_space;
287 typedef typename offsets_view_type::memory_space memory_space;
288 typedef SizeType size_type;
290 typedef typename keys_view_type::non_const_value_type key_type;
291 typedef typename pairs_view_type::non_const_value_type pair_type;
315 const counts_view_type& counts,
316 const offsets_view_type& ptr,
317 const keys_view_type& keys,
318 const typename pair_type::second_type startingValue) :
323 size_ (counts.extent (0)),
324 startingValue_ (startingValue),
325 initMinKey_ (::Kokkos::
Details::ArithTraits<key_type>::max ()),
326 initMaxKey_ (::Kokkos::
Details::ArithTraits<key_type>::is_integer ?
327 ::Kokkos::
Details::ArithTraits<key_type>::min () :
328 -::Kokkos::
Details::ArithTraits<key_type>::max ())
350 const counts_view_type& counts,
351 const offsets_view_type& ptr,
352 const keys_view_type& keys,
353 const typename pair_type::second_type startingValue,
354 const key_type initMinKey,
355 const key_type initMaxKey) :
360 size_ (counts.extent (0)),
361 startingValue_ (startingValue),
362 initMinKey_ (initMinKey),
363 initMaxKey_ (initMaxKey)
374 KOKKOS_INLINE_FUNCTION
void
375 join (
volatile value_type& dst,
376 const volatile value_type& src)
const
378 if (src.maxKey_ > dst.maxKey_) {
379 dst.maxKey_ = src.maxKey_;
381 if (src.minKey_ < dst.minKey_) {
382 dst.minKey_ = src.minKey_;
384 dst.success_ = dst.success_ && src.success_;
391 KOKKOS_INLINE_FUNCTION
void
395 typedef typename offsets_view_type::non_const_value_type offset_type;
396 typedef typename pair_type::second_type val_type;
397 typedef typename std::remove_reference<
decltype( counts_[0] ) >::type atomic_incr_type;
399 const key_type key = keys_[i];
406 const val_type theVal = startingValue_ +
static_cast<val_type
> (i);
410 const offset_type count = Kokkos::atomic_fetch_add (&counts_[hashVal], atomic_incr_type(-1));
415 const offset_type curPos = ptr_[hashVal+1] - count;
417 pairs_[curPos].first = key;
418 pairs_[curPos].second = theVal;
423 pairs_view_type pairs_;
424 counts_view_type counts_;
425 offsets_view_type ptr_;
426 keys_view_type keys_;
428 typename pair_type::second_type startingValue_;
430 key_type initMinKey_;
432 key_type initMaxKey_;
458template<
class OffsetsViewType,
460 class SizeType =
typename OffsetsViewType::size_type>
463 typedef typename OffsetsViewType::const_type offsets_view_type;
464 typedef typename PairsViewType::const_type pairs_view_type;
465 typedef typename offsets_view_type::execution_space execution_space;
466 typedef typename offsets_view_type::memory_space memory_space;
467 typedef SizeType size_type;
470 typedef int value_type;
477 const offsets_view_type& ptr) :
480 size_ (ptr_.extent (0) == 0 ?
486 KOKKOS_INLINE_FUNCTION
void init (value_type& dst)
const
492 KOKKOS_INLINE_FUNCTION
void
493 join (
volatile value_type& dst,
494 const volatile value_type& src)
const
496 dst = dst + src > 0?1:0;
500 KOKKOS_INLINE_FUNCTION
void
503 typedef typename offsets_view_type::non_const_value_type offset_type;
504 typedef typename pairs_view_type::non_const_value_type pair_type;
505 typedef typename pair_type::first_type key_type;
511 const offset_type beg = ptr_[i];
512 const offset_type end = ptr_[i+1];
513 bool foundDuplicateKey =
false;
518 for (offset_type j = beg + 1; j < end; ++j) {
519 const key_type curKey = pairs_[j].first;
520 for (offset_type k = beg; k < j; ++k) {
521 if (pairs_[k].first == curKey) {
522 foundDuplicateKey =
true;
527 dst = (dst>0) || foundDuplicateKey?1:0;
532 pairs_view_type pairs_;
533 offsets_view_type ptr_;
543template<
class KeyType,
class ValueType,
class DeviceType>
546 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
547 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
548 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
549 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
550 minVal_ (::Kokkos::
Details::ArithTraits<ValueType>::max ()),
551 maxVal_ (::Kokkos::
Details::ArithTraits<ValueType>::is_integer ?
552 ::Kokkos::
Details::ArithTraits<ValueType>::min () :
553 -::Kokkos::
Details::ArithTraits<ValueType>::max ()),
554 firstContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
555 lastContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
556 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
557 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
558 contiguousValues_ (true),
559 checkedForDuplicateKeys_ (true),
560 hasDuplicateKeys_ (false)
564template<
class KeyType,
class ValueType,
class DeviceType>
567 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
568 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
569 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
570 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
572 maxVal_ (keys.size () == 0 ?
573 static_cast<ValueType> (0) :
574 static_cast<ValueType> (keys.size () - 1)),
575 firstContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
576 lastContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
577 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
578 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
579 contiguousValues_ (true),
580 checkedForDuplicateKeys_ (false),
581 hasDuplicateKeys_ (false)
583 const ValueType startingValue =
static_cast<ValueType
> (0);
584 const KeyType initMinKey = this->minKey_;
585 const KeyType initMaxKey = this->maxKey_;
586 this->init (keys, startingValue, initMinKey, initMaxKey,
587 initMinKey, initMinKey,
false);
590template<
class KeyType,
class ValueType,
class DeviceType>
593 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
594 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
595 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
596 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
598 maxVal_ (keys.size () == 0 ?
599 static_cast<ValueType> (0) :
600 static_cast<ValueType> (keys.size () - 1)),
601 firstContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
602 lastContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
603 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
604 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
605 contiguousValues_ (true),
606 checkedForDuplicateKeys_ (false),
607 hasDuplicateKeys_ (false)
609 typedef typename keys_type::non_const_type nonconst_keys_type;
614 const ValueType startingValue =
static_cast<ValueType
> (0);
615 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
617 using Kokkos::ViewAllocateWithoutInitializing;
618 nonconst_keys_type keys_d (ViewAllocateWithoutInitializing (
"FixedHashTable::keys"),
621 const KeyType initMinKey = this->minKey_;
622 const KeyType initMaxKey = this->maxKey_;
623 this->init (keys_d, startingValue, initMinKey, initMaxKey,
624 initMinKey, initMinKey,
false);
627template<
class KeyType,
class ValueType,
class DeviceType>
630 const ValueType startingValue) :
631 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
632 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
633 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
634 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
635 minVal_ (startingValue),
636 maxVal_ (keys.size () == 0 ?
638 static_cast<ValueType> (startingValue + keys.size () - 1)),
639 firstContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
640 lastContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
641 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
642 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
643 contiguousValues_ (true),
644 checkedForDuplicateKeys_ (false),
645 hasDuplicateKeys_ (false)
647 typedef typename keys_type::non_const_type nonconst_keys_type;
652 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
654 using Kokkos::ViewAllocateWithoutInitializing;
655 nonconst_keys_type keys_d (ViewAllocateWithoutInitializing (
"FixedHashTable::keys"),
659 const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
672 const KeyType initMaxKey = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
673 ::Kokkos::Details::ArithTraits<KeyType>::min () :
674 -::Kokkos::Details::ArithTraits<KeyType>::max ();
675 this->init (keys_d, startingValue, initMinKey, initMaxKey,
676 initMinKey, initMinKey,
false);
680template<
class KeyType,
class ValueType,
class DeviceType>
683 const KeyType firstContigKey,
684 const KeyType lastContigKey,
685 const ValueType startingValue) :
686 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
687 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
688 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
689 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
690 minVal_ (startingValue),
691 maxVal_ (keys.size () == 0 ?
693 static_cast<ValueType> (startingValue + keys.size () - 1)),
694 firstContigKey_ (firstContigKey),
695 lastContigKey_ (lastContigKey),
696 contiguousValues_ (true),
697 checkedForDuplicateKeys_ (false),
698 hasDuplicateKeys_ (false)
700 const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
713 const KeyType initMaxKey = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
714 ::Kokkos::Details::ArithTraits<KeyType>::min () :
715 -::Kokkos::Details::ArithTraits<KeyType>::max ();
716 this->init (keys, startingValue, initMinKey, initMaxKey,
717 firstContigKey, lastContigKey,
true);
720template<
class KeyType,
class ValueType,
class DeviceType>
723 const KeyType firstContigKey,
724 const KeyType lastContigKey,
725 const ValueType startingValue) :
726 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
727 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
728 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
729 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
730 minVal_ (startingValue),
731 maxVal_ (keys.size () == 0 ?
733 static_cast<ValueType> (startingValue + keys.size () - 1)),
734 firstContigKey_ (firstContigKey),
735 lastContigKey_ (lastContigKey),
736 contiguousValues_ (true),
737 checkedForDuplicateKeys_ (false),
738 hasDuplicateKeys_ (false)
740 typedef typename keys_type::non_const_type nonconst_keys_type;
745 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
747 using Kokkos::ViewAllocateWithoutInitializing;
748 nonconst_keys_type keys_d (ViewAllocateWithoutInitializing (
"FixedHashTable::keys"),
752 const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
765 const KeyType initMaxKey = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
766 ::Kokkos::Details::ArithTraits<KeyType>::min () :
767 -::Kokkos::Details::ArithTraits<KeyType>::max ();
768 this->init (keys_d, startingValue, initMinKey, initMaxKey,
769 firstContigKey, lastContigKey,
true);
772template<
class KeyType,
class ValueType,
class DeviceType>
775 const ValueType startingValue) :
776 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
777 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
778 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
779 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
780 minVal_ (startingValue),
781 maxVal_ (keys.size () == 0 ?
783 static_cast<ValueType> (startingValue + keys.size () - 1)),
784 firstContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
785 lastContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
786 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
787 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
788 contiguousValues_ (true),
789 checkedForDuplicateKeys_ (false),
790 hasDuplicateKeys_ (false)
792 const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
805 const KeyType initMaxKey = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
806 ::Kokkos::Details::ArithTraits<KeyType>::min () :
807 -::Kokkos::Details::ArithTraits<KeyType>::max ();
808 this->init (keys, startingValue, initMinKey, initMaxKey,
809 initMinKey, initMinKey,
false);
812template<
class KeyType,
class ValueType,
class DeviceType>
815 const Teuchos::ArrayView<const ValueType>& vals) :
816 minKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
817 maxKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
818 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
819 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
820 minVal_ (::Kokkos::
Details::ArithTraits<ValueType>::max ()),
821 maxVal_ (::Kokkos::
Details::ArithTraits<ValueType>::is_integer ?
822 ::Kokkos::
Details::ArithTraits<ValueType>::min () :
823 -::Kokkos::
Details::ArithTraits<ValueType>::max ()),
824 firstContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::max ()),
825 lastContigKey_ (::Kokkos::
Details::ArithTraits<KeyType>::is_integer ?
826 ::Kokkos::
Details::ArithTraits<KeyType>::min () :
827 -::Kokkos::
Details::ArithTraits<KeyType>::max ()),
828 contiguousValues_ (false),
829 checkedForDuplicateKeys_ (false),
830 hasDuplicateKeys_ (false)
835 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
837 host_input_vals_type vals_k (vals.size () == 0 ? NULL : vals.getRawPtr (),
839 const KeyType initMinKey = ::Kokkos::Details::ArithTraits<KeyType>::max ();
852 const KeyType initMaxKey = ::Kokkos::Details::ArithTraits<KeyType>::is_integer ?
853 ::Kokkos::Details::ArithTraits<KeyType>::min () :
854 -::Kokkos::Details::ArithTraits<KeyType>::max ();
855 this->init (keys_k, vals_k, initMinKey, initMaxKey);
858template<
class KeyType,
class ValueType,
class DeviceType>
861init (
const keys_type& keys,
862 ValueType startingValue,
865 KeyType firstContigKey,
866 KeyType lastContigKey,
867 const bool computeInitContigKeys)
869 using Kokkos::subview;
870 using Kokkos::ViewAllocateWithoutInitializing;
871 using Teuchos::TypeNameTraits;
872 typedef typename std::decay<
decltype (keys.extent (0)) >::type size_type;
873 const char prefix[] =
"Tpetra::Details::FixedHashTable: ";
875 const offset_type numKeys =
static_cast<offset_type
> (keys.extent (0));
877 const offset_type theMaxVal = ::Kokkos::Details::ArithTraits<offset_type>::max ();
878 const size_type maxValST =
static_cast<size_type
> (theMaxVal);
879 TEUCHOS_TEST_FOR_EXCEPTION
880 (keys.extent (0) > maxValST, std::invalid_argument, prefix <<
"The "
881 "number of keys " << keys.extent (0) <<
" does not fit in "
882 "offset_type = " << TypeNameTraits<offset_type>::name () <<
", whose "
883 "max value is " << theMaxVal <<
". This means that it is not possible to "
884 "use this constructor.");
886 TEUCHOS_TEST_FOR_EXCEPTION
887 (
static_cast<unsigned long long> (numKeys) >
888 static_cast<unsigned long long> (::Kokkos::Details::ArithTraits<ValueType>::max ()),
889 std::invalid_argument,
"Tpetra::Details::FixedHashTable: The number of "
890 "keys " << numKeys <<
" is greater than the maximum representable "
891 "ValueType value " << ::Kokkos::Details::ArithTraits<ValueType>::max () <<
". "
892 "This means that it is not possible to use this constructor.");
893 TEUCHOS_TEST_FOR_EXCEPTION
894 (numKeys >
static_cast<offset_type
> (INT_MAX), std::logic_error, prefix <<
895 "This class currently only works when the number of keys is <= INT_MAX = "
896 << INT_MAX <<
". If this is a problem for you, please talk to the Tpetra "
899 const bool buildInParallel =
900 FHT::worthBuildingFixedHashTableInParallel<execution_space> ();
911 if (computeInitContigKeys) {
925 auto keys_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
927 firstContigKey_ = keys_h[0];
931 lastContigKey_ = firstContigKey_ + 1;
937 for (offset_type k = 1; k < numKeys; ++k) {
938 if (lastContigKey_ != keys_h[k]) {
947 firstContigKey_ = firstContigKey;
948 lastContigKey_ = lastContigKey;
951 offset_type startIndex;
953 initMinKey = std::min (initMinKey, firstContigKey_);
954 initMaxKey = std::max (initMaxKey, lastContigKey_);
955 startIndex =
static_cast<offset_type
> (lastContigKey_ - firstContigKey_);
960 const offset_type theNumKeys = numKeys - startIndex;
961 const offset_type size = hash_type::getRecommendedSize (theNumKeys);
962#ifdef HAVE_TPETRA_DEBUG
963 TEUCHOS_TEST_FOR_EXCEPTION(
964 size == 0 && numKeys != 0, std::logic_error,
965 "Tpetra::Details::FixedHashTable constructor: "
966 "getRecommendedSize(" << numKeys <<
") returned zero, "
967 "even though the number of keys " << numKeys <<
" is nonzero. "
968 "Please report this bug to the Tpetra developers.");
971 subview (keys, std::pair<offset_type, offset_type> (startIndex, numKeys));
978 typedef typename ptr_type::non_const_type counts_type;
979 counts_type counts (
"Tpetra::FixedHashTable::counts", size);
986 typename keys_type::HostMirror theKeysHost;
993 if (buildInParallel) {
994 FHT::CountBuckets<counts_type, keys_type> functor (counts, theKeys, size);
995 using execution_space =
typename counts_type::execution_space;
996 using range_type = Kokkos::RangePolicy<execution_space, offset_type>;
997 const char kernelLabel[] =
"Tpetra::Details::FixedHashTable CountBuckets";
999 using key_type =
typename keys_type::non_const_value_type;
1000 Kokkos::pair<int, key_type> err;
1001 Kokkos::parallel_reduce (kernelLabel, range_type (0, theNumKeys),
1003 TEUCHOS_TEST_FOR_EXCEPTION
1004 (err.first != 0, std::logic_error,
"Tpetra::Details::FixedHashTable "
1005 "constructor: CountBuckets found a key " << err.second <<
" that "
1006 "results in an out-of-bounds hash value.");
1009 Kokkos::parallel_for (kernelLabel, range_type (0, theNumKeys), functor);
1013 Kokkos::HostSpace hostMemSpace;
1014 theKeysHost = Kokkos::create_mirror_view(theKeys);
1016 auto countsHost = Kokkos::create_mirror_view (hostMemSpace, counts);
1018 for (offset_type k = 0; k < theNumKeys; ++k) {
1019 using key_type =
typename keys_type::non_const_value_type;
1020 const key_type key = theKeysHost[k];
1022 using hash_value_type =
typename hash_type::result_type;
1023 const hash_value_type hashVal = hash_type::hashFunc (key, size);
1024 TEUCHOS_TEST_FOR_EXCEPTION
1025 (hashVal < hash_value_type (0) ||
1026 hashVal >= hash_value_type (countsHost.extent (0)),
1027 std::logic_error,
"Tpetra::Details::FixedHashTable "
1028 "constructor: Sequential CountBuckets found a key " << key
1029 <<
" that results in an out-of-bounds hash value.");
1031 ++countsHost[hashVal];
1039 execution_space().fence ();
1042 typename ptr_type::non_const_type ptr (
"Tpetra::FixedHashTable::ptr", size+1);
1058 if (buildInParallel) {
1062 if (! buildInParallel || debug) {
1063 Kokkos::HostSpace hostMemSpace;
1064 auto counts_h = Kokkos::create_mirror_view_and_copy (hostMemSpace, counts);
1065 auto ptr_h = Kokkos::create_mirror_view (hostMemSpace, ptr);
1067#ifdef KOKKOS_ENABLE_SERIAL
1068 Kokkos::Serial hostExecSpace;
1070 Kokkos::DefaultHostExecutionSpace hostExecSpace;
1078 for (offset_type i = 0; i < size; ++i) {
1079 if (ptr_h[i+1] != ptr_h[i] + counts_h[i]) {
1083 TEUCHOS_TEST_FOR_EXCEPTION
1084 (bad, std::logic_error,
"Tpetra::Details::FixedHashTable "
1085 "constructor: computeOffsetsFromCounts gave an incorrect "
1093 execution_space().fence ();
1097 typedef typename val_type::non_const_type nonconst_val_type;
1098 nonconst_val_type val (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::pairs"),
1102 typedef FHT::FillPairs<
typename val_type::non_const_type, keys_type,
1103 typename ptr_type::non_const_type> functor_type;
1104 typename functor_type::value_type result (initMinKey, initMaxKey);
1106 const ValueType newStartingValue = startingValue +
static_cast<ValueType
> (startIndex);
1107 if (buildInParallel) {
1108 functor_type functor (val, counts, ptr, theKeys, newStartingValue,
1109 initMinKey, initMaxKey);
1110 typedef Kokkos::RangePolicy<execution_space, offset_type> range_type;
1111 Kokkos::parallel_reduce (range_type (0, theNumKeys), functor, result);
1114 Kokkos::HostSpace hostMemSpace;
1115 auto counts_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, counts);
1116 auto ptr_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, ptr);
1117 auto val_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, val);
1118 for (offset_type k = 0; k < theNumKeys; ++k) {
1119 typedef typename hash_type::result_type hash_value_type;
1120 const KeyType key = theKeysHost[k];
1121 if (key > result.maxKey_) {
1122 result.maxKey_ = key;
1124 if (key < result.minKey_) {
1125 result.minKey_ = key;
1127 const ValueType theVal = newStartingValue +
static_cast<ValueType
> (k);
1128 const hash_value_type hashVal = hash_type::hashFunc (key, size);
1131 const offset_type count = counts_h[hashVal];
1132 --counts_h[hashVal];
1134 result.success_ =
false;
1138 const offset_type curPos = ptr_h[hashVal+1] - count;
1139 val_h[curPos].first = key;
1140 val_h[curPos].second = theVal;
1159 minKey_ = result.minKey_;
1160 maxKey_ = result.maxKey_;
1165template<
class KeyType,
class ValueType,
class DeviceType>
1167FixedHashTable<KeyType, ValueType, DeviceType>::
1168init (
const host_input_keys_type& keys,
1169 const host_input_vals_type& vals,
1173 const offset_type numKeys =
static_cast<offset_type
> (keys.extent (0));
1174 TEUCHOS_TEST_FOR_EXCEPTION
1175 (
static_cast<unsigned long long> (numKeys) >
static_cast<unsigned long long> (::Kokkos::Details::ArithTraits<ValueType>::max ()),
1176 std::invalid_argument,
"Tpetra::Details::FixedHashTable: The number of "
1177 "keys " << numKeys <<
" is greater than the maximum representable "
1178 "ValueType value " << ::Kokkos::Details::ArithTraits<ValueType>::max () <<
".");
1179 TEUCHOS_TEST_FOR_EXCEPTION
1180 (numKeys >
static_cast<offset_type
> (INT_MAX), std::logic_error,
"Tpetra::"
1181 "Details::FixedHashTable: This class currently only works when the number "
1182 "of keys is <= INT_MAX = " << INT_MAX <<
". If this is a problem for you"
1183 ", please talk to the Tpetra developers.");
1190 const offset_type size = hash_type::getRecommendedSize (numKeys);
1191#ifdef HAVE_TPETRA_DEBUG
1192 TEUCHOS_TEST_FOR_EXCEPTION(
1193 size == 0 && numKeys != 0, std::logic_error,
1194 "Tpetra::Details::FixedHashTable constructor: "
1195 "getRecommendedSize(" << numKeys <<
") returned zero, "
1196 "even though the number of keys " << numKeys <<
" is nonzero. "
1197 "Please report this bug to the Tpetra developers.");
1206 Kokkos::HostSpace hostMemSpace;
1207 typename ptr_type::non_const_type ptr (
"Tpetra::FixedHashTable::ptr", size + 1);
1208 auto ptr_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, ptr);
1212 using Kokkos::ViewAllocateWithoutInitializing;
1213 typedef typename val_type::non_const_type nonconst_val_type;
1214 nonconst_val_type val (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::pairs"),
1216 auto val_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, val);
1219 for (offset_type k = 0; k < numKeys; ++k) {
1220 const typename hash_type::result_type hashVal =
1221 hash_type::hashFunc (keys[k], size);
1233 for (offset_type i = 0; i < size; ++i) {
1234 ptr_h[i+1] += ptr_h[i];
1239 typename ptr_type::non_const_type::HostMirror curRowStart (
"Tpetra::FixedHashTable::curRowStart", size);
1242 FHT::FillPairsResult<KeyType> result (initMinKey, initMaxKey);
1243 for (offset_type k = 0; k < numKeys; ++k) {
1244 typedef typename hash_type::result_type hash_value_type;
1245 const KeyType key = keys[k];
1246 if (key > result.maxKey_) {
1247 result.maxKey_ = key;
1249 if (key < result.minKey_) {
1250 result.minKey_ = key;
1252 const ValueType theVal = vals[k];
1253 if (theVal > maxVal_) {
1256 if (theVal < minVal_) {
1259 const hash_value_type hashVal = hash_type::hashFunc (key, size);
1261 const offset_type offset = curRowStart[hashVal];
1262 const offset_type curPos = ptr_h[hashVal] + offset;
1263 if (curPos >= ptr_h[hashVal+1]) {
1264 result.success_ =
false;
1267 val_h[curPos].first = key;
1268 val_h[curPos].second = theVal;
1269 ++curRowStart[hashVal];
1273 TEUCHOS_TEST_FOR_EXCEPTION
1274 (! result.success_, std::logic_error,
"Tpetra::Details::FixedHashTable::"
1275 "init: Filling the hash table failed! Please report this bug to the "
1276 "Tpetra developers.");
1284 minKey_ = result.minKey_;
1285 maxKey_ = result.maxKey_;
1289template <
class KeyType,
class ValueType,
class DeviceType>
1294 if (! checkedForDuplicateKeys_) {
1295 hasDuplicateKeys_ = checkForDuplicateKeys ();
1296 checkedForDuplicateKeys_ =
true;
1298 return hasDuplicateKeys_;
1301template <
class KeyType,
class ValueType,
class DeviceType>
1306 const offset_type size = this->getSize ();
1310 if (size == 0 || this->numPairs () == 0) {
1314 typedef FHT::CheckForDuplicateKeys<ptr_type, val_type> functor_type;
1315 functor_type functor (val_, ptr_);
1317 typedef Kokkos::RangePolicy<execution_space, offset_type> range_type;
1318 Kokkos::parallel_reduce (range_type (0, size), functor, hasDupKeys);
1319 return hasDupKeys > 0;
1323template <
class KeyType,
class ValueType,
class DeviceType>
1328 std::ostringstream oss;
1329 oss <<
"FixedHashTable<"
1330 << Teuchos::TypeNameTraits<KeyType>::name () <<
","
1331 << Teuchos::TypeNameTraits<ValueType>::name () <<
">: "
1332 <<
"{ numKeys: " << val_.extent (0)
1333 <<
", tableSize: " << this->getSize () <<
" }";
1337template <
class KeyType,
class ValueType,
class DeviceType>
1340describe (Teuchos::FancyOStream& out,
1341 const Teuchos::EVerbosityLevel verbLevel)
const
1345 using Teuchos::OSTab;
1346 using Teuchos::rcpFromRef;
1347 using Teuchos::TypeNameTraits;
1348 using Teuchos::VERB_DEFAULT;
1349 using Teuchos::VERB_NONE;
1350 using Teuchos::VERB_LOW;
1351 using Teuchos::VERB_EXTREME;
1356 Teuchos::EVerbosityLevel vl = verbLevel;
1357 if (vl == VERB_DEFAULT) vl = VERB_LOW;
1359 if (vl == VERB_NONE) {
1362 else if (vl == VERB_LOW) {
1363 out << this->description() << endl;
1366 out <<
"FixedHashTable:" << endl;
1368 OSTab tab1 (rcpFromRef (out));
1374 out <<
"Template parameters:" << endl;
1376 OSTab tab2 (rcpFromRef (out));
1377 out <<
"KeyType: " << TypeNameTraits<KeyType>::name () << endl
1378 <<
"ValueType: " << TypeNameTraits<ValueType>::name () << endl;
1381 const offset_type tableSize = this->getSize ();
1382 const offset_type numKeys = val_.extent (0);
1384 out <<
"Table parameters:" << endl;
1386 OSTab tab2 (rcpFromRef (out));
1387 out <<
"numKeys: " << numKeys << endl
1388 <<
"tableSize: " << tableSize << endl;
1391 if (vl >= VERB_EXTREME) {
1392 out <<
"Contents: ";
1393 if (tableSize == 0 || numKeys == 0) {
1394 out <<
"[]" << endl;
1396 out <<
"[ " << endl;
1398 OSTab tab2 (rcpFromRef (out));
1399 for (offset_type i = 0; i < tableSize; ++i) {
1400 OSTab tab3 (rcpFromRef (out));
1402 for (offset_type k = ptr_[i]; k < ptr_[i+1]; ++k) {
1403 out <<
"(" << val_[k].first <<
"," << val_[k].second <<
")";
1404 if (k + 1 < ptr_[i+1]) {
1430#define TPETRA_DETAILS_FIXEDHASHTABLE_INSTANT_DEFAULTNODE(LO,GO) \
1431 template class FixedHashTable< GO , LO >;
1442#define TPETRA_DETAILS_FIXEDHASHTABLE_INSTANT(LO, GO, DEVICE) \
1443 template class FixedHashTable< GO , LO , DEVICE >;
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
Declare and define the functions Tpetra::Details::computeOffsetsFromCounts and Tpetra::computeOffsets...
static bool debug()
Whether Tpetra is in debug mode.
Functor for checking whether a FixedHashTable has one or more duplicate entries.
KOKKOS_INLINE_FUNCTION void init(value_type &dst) const
Set the initial value of the reduction result.
KOKKOS_INLINE_FUNCTION void operator()(const size_type &i, value_type &dst) const
Parallel loop body.
KOKKOS_INLINE_FUNCTION void join(volatile value_type &dst, const volatile value_type &src) const
Combine two intermediate reduction results.
CheckForDuplicateKeys(const pairs_view_type &pairs, const offsets_view_type &ptr)
Constructor.
Parallel for functor for counting "buckets" in the FixedHashTable.
KOKKOS_INLINE_FUNCTION void init(value_type &dst) const
Set the initial value of the reduction result.
KOKKOS_INLINE_FUNCTION void operator()(const size_type &i) const
Do this for every entry of keys_.
CountBuckets(const counts_view_type &counts, const keys_view_type &keys, const size_type size)
Constructor.
Parallel reduce functor for filling the FixedHashTable, and computing the min and max keys.
FillPairs(const pairs_view_type &pairs, const counts_view_type &counts, const offsets_view_type &ptr, const keys_view_type &keys, const typename pair_type::second_type startingValue, const key_type initMinKey, const key_type initMaxKey)
Constructor that takes initial min and max key values.
KOKKOS_INLINE_FUNCTION void init(value_type &dst) const
Set the initial value of the reduction result.
KOKKOS_INLINE_FUNCTION void operator()(const size_type &i, value_type &dst) const
Parallel loop body; do this for every entry of keys_.
FillPairs(const pairs_view_type &pairs, const counts_view_type &counts, const offsets_view_type &ptr, const keys_view_type &keys, const typename pair_type::second_type startingValue)
Constructor.
std::string description() const
Implementation of Teuchos::Describable interface.
bool hasDuplicateKeys()
Whether the table has any duplicate keys.
Kokkos::View< const KeyType *, Kokkos::LayoutLeft, device_type > keys_type
Type of a 1-D Kokkos::View (array) used to store keys.
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.
Implementation details of Tpetra.
OffsetsViewType::non_const_value_type computeOffsetsFromCounts(const ExecutionSpace &execSpace, const OffsetsViewType &ptr, const CountsViewType &counts)
Compute offsets from counts.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
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.
Reduction result for FillPairs functor below.
KeyType maxKey_
The current maximum key.
KeyType minKey_
The current minimum key.
bool success_
Whether fill succeeded (it can only fail on a bug)
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 &key, const offset_type &size)
The hash function.