376 const offset_type count = Kokkos::atomic_fetch_add (&counts_[hashVal], atomic_incr_type(-1));
381 const offset_type curPos = ptr_[hashVal+1] - count;
383 pairs_[curPos].first = key;
384 pairs_[curPos].second = theVal;
389 pairs_view_type pairs_;
390 counts_view_type counts_;
391 offsets_view_type ptr_;
392 keys_view_type keys_;
394 typename pair_type::second_type startingValue_;
396 key_type initMinKey_;
398 key_type initMaxKey_;
424template<
class OffsetsViewType,
426 class SizeType =
typename OffsetsViewType::size_type>
429 typedef typename OffsetsViewType::const_type offsets_view_type;
430 typedef typename PairsViewType::const_type pairs_view_type;
431 typedef typename offsets_view_type::execution_space execution_space;
432 typedef typename offsets_view_type::memory_space memory_space;
433 typedef SizeType size_type;
436 typedef int value_type;
443 const offsets_view_type& ptr) :
446 size_ (ptr_.extent (0) == 0 ?
452 KOKKOS_INLINE_FUNCTION
void init (value_type& dst)
const
458 KOKKOS_INLINE_FUNCTION
void
460 const value_type& src)
const
462 dst = dst + src > 0?1:0;
466 KOKKOS_INLINE_FUNCTION
void
469 typedef typename offsets_view_type::non_const_value_type offset_type;
470 typedef typename pairs_view_type::non_const_value_type pair_type;
471 typedef typename pair_type::first_type key_type;
477 const offset_type beg = ptr_[i];
478 const offset_type end = ptr_[i+1];
479 bool foundDuplicateKey =
false;
484 for (offset_type j = beg + 1; j < end; ++j) {
485 const key_type curKey = pairs_[j].first;
486 for (offset_type k = beg; k < j; ++k) {
487 if (pairs_[k].first == curKey) {
488 foundDuplicateKey =
true;
493 dst = (dst>0) || foundDuplicateKey?1:0;
498 pairs_view_type pairs_;
499 offsets_view_type ptr_;
509template<
class KeyType,
class ValueType,
class DeviceType>
513 maxVal_ (keys.size () == 0 ?
514 static_cast<ValueType> (0) :
515 static_cast<ValueType> (keys.size () - 1)),
516 checkedForDuplicateKeys_ (false)
518 const ValueType startingValue =
static_cast<ValueType
> (0);
519 const KeyType initMinKey = this->minKey_;
520 const KeyType initMaxKey = this->maxKey_;
521 this->init (keys, startingValue, initMinKey, initMaxKey,
522 initMinKey, initMinKey,
false);
525template<
class KeyType,
class ValueType,
class DeviceType>
529 maxVal_ (keys.size () == 0 ?
530 static_cast<ValueType> (0) :
531 static_cast<ValueType> (keys.size () - 1)),
532 checkedForDuplicateKeys_ (false)
534 typedef typename keys_type::non_const_type nonconst_keys_type;
539 const ValueType startingValue =
static_cast<ValueType
> (0);
540 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
542 using Kokkos::ViewAllocateWithoutInitializing;
543 nonconst_keys_type keys_d (ViewAllocateWithoutInitializing (
"FixedHashTable::keys"),
546 Kokkos::deep_copy (keys_d, keys_k);
547 const KeyType initMinKey = this->minKey_;
548 const KeyType initMaxKey = this->maxKey_;
549 this->init (keys_d, startingValue, initMinKey, initMaxKey,
550 initMinKey, initMinKey,
false);
553template<
class KeyType,
class ValueType,
class DeviceType>
556 const ValueType startingValue) :
557 minVal_ (startingValue),
558 maxVal_ (keys.size () == 0 ?
560 static_cast<ValueType> (startingValue + keys.size () - 1)),
561 checkedForDuplicateKeys_ (false)
563 typedef typename keys_type::non_const_type nonconst_keys_type;
568 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
570 using Kokkos::ViewAllocateWithoutInitializing;
571 nonconst_keys_type keys_d (ViewAllocateWithoutInitializing (
"FixedHashTable::keys"),
574 Kokkos::deep_copy (execution_space(), keys_d, keys_k);
576 const KeyType initMinKey = ::Kokkos::ArithTraits<KeyType>::max ();
589 const KeyType initMaxKey = ::Kokkos::ArithTraits<KeyType>::is_integer ?
590 ::Kokkos::ArithTraits<KeyType>::min () :
591 -::Kokkos::ArithTraits<KeyType>::max ();
592 this->init (keys_d, startingValue, initMinKey, initMaxKey,
593 initMinKey, initMinKey,
false);
597template<
class KeyType,
class ValueType,
class DeviceType>
600 const KeyType firstContigKey,
601 const KeyType lastContigKey,
602 const ValueType startingValue) :
603 minVal_ (startingValue),
604 maxVal_ (keys.size () == 0 ?
606 static_cast<ValueType> (startingValue + keys.size () - 1)),
607 firstContigKey_ (firstContigKey),
608 lastContigKey_ (lastContigKey),
609 checkedForDuplicateKeys_ (false)
611 const KeyType initMinKey = ::Kokkos::ArithTraits<KeyType>::max ();
624 const KeyType initMaxKey = ::Kokkos::ArithTraits<KeyType>::is_integer ?
625 ::Kokkos::ArithTraits<KeyType>::min () :
626 -::Kokkos::ArithTraits<KeyType>::max ();
627 this->init (keys, startingValue, initMinKey, initMaxKey,
628 firstContigKey, lastContigKey,
true);
631template<
class KeyType,
class ValueType,
class DeviceType>
634 const KeyType firstContigKey,
635 const KeyType lastContigKey,
636 const ValueType startingValue) :
637 minVal_ (startingValue),
638 maxVal_ (keys.size () == 0 ?
640 static_cast<ValueType> (startingValue + keys.size () - 1)),
641 firstContigKey_ (firstContigKey),
642 lastContigKey_ (lastContigKey),
643 checkedForDuplicateKeys_ (false)
645 typedef typename keys_type::non_const_type nonconst_keys_type;
650 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
652 using Kokkos::ViewAllocateWithoutInitializing;
653 nonconst_keys_type keys_d (ViewAllocateWithoutInitializing (
"FixedHashTable::keys"),
656 Kokkos::deep_copy (keys_d, keys_k);
658 const KeyType initMinKey = ::Kokkos::ArithTraits<KeyType>::max ();
671 const KeyType initMaxKey = ::Kokkos::ArithTraits<KeyType>::is_integer ?
672 ::Kokkos::ArithTraits<KeyType>::min () :
673 -::Kokkos::ArithTraits<KeyType>::max ();
674 this->init (keys_d, startingValue, initMinKey, initMaxKey,
675 firstContigKey, lastContigKey,
true);
678template<
class KeyType,
class ValueType,
class DeviceType>
681 const ValueType startingValue) :
682 minVal_ (startingValue),
683 maxVal_ (keys.size () == 0 ?
685 static_cast<ValueType> (startingValue + keys.size () - 1)),
686 checkedForDuplicateKeys_ (false)
688 const KeyType initMinKey = ::Kokkos::ArithTraits<KeyType>::max ();
701 const KeyType initMaxKey = ::Kokkos::ArithTraits<KeyType>::is_integer ?
702 ::Kokkos::ArithTraits<KeyType>::min () :
703 -::Kokkos::ArithTraits<KeyType>::max ();
704 this->init (keys, startingValue, initMinKey, initMaxKey,
705 initMinKey, initMinKey,
false);
708template<
class KeyType,
class ValueType,
class DeviceType>
711 const Teuchos::ArrayView<const ValueType>& vals) :
712 contiguousValues_ (false),
713 checkedForDuplicateKeys_ (false)
718 host_input_keys_type keys_k (keys.size () == 0 ? NULL : keys.getRawPtr (),
720 host_input_vals_type vals_k (vals.size () == 0 ? NULL : vals.getRawPtr (),
722 const KeyType initMinKey = ::Kokkos::ArithTraits<KeyType>::max ();
735 const KeyType initMaxKey = ::Kokkos::ArithTraits<KeyType>::is_integer ?
736 ::Kokkos::ArithTraits<KeyType>::min () :
737 -::Kokkos::ArithTraits<KeyType>::max ();
738 this->init (keys_k, vals_k, initMinKey, initMaxKey);
741template<
class KeyType,
class ValueType,
class DeviceType>
744init (
const keys_type& keys,
745 ValueType startingValue,
748 KeyType firstContigKey,
749 KeyType lastContigKey,
750 const bool computeInitContigKeys)
752 using Kokkos::subview;
753 using Kokkos::ViewAllocateWithoutInitializing;
754 using Teuchos::TypeNameTraits;
755 typedef typename std::decay<
decltype (keys.extent (0)) >::type size_type;
757 const char prefix[] =
"Tpetra::Details::FixedHashTable: ";
759 const offset_type numKeys =
static_cast<offset_type
> (keys.extent (0));
761 const offset_type theMaxVal = ::Kokkos::ArithTraits<offset_type>::max ();
762 const size_type maxValST =
static_cast<size_type
> (theMaxVal);
763 TEUCHOS_TEST_FOR_EXCEPTION
764 (keys.extent (0) > maxValST, std::invalid_argument, prefix <<
"The "
765 "number of keys " << keys.extent (0) <<
" does not fit in "
766 "offset_type = " << TypeNameTraits<offset_type>::name () <<
", whose "
767 "max value is " << theMaxVal <<
". This means that it is not possible to "
768 "use this constructor.");
770 TEUCHOS_TEST_FOR_EXCEPTION
771 (
static_cast<unsigned long long> (numKeys) >
772 static_cast<unsigned long long> (::Kokkos::ArithTraits<ValueType>::max ()),
773 std::invalid_argument,
"Tpetra::Details::FixedHashTable: The number of "
774 "keys " << numKeys <<
" is greater than the maximum representable "
775 "ValueType value " << ::Kokkos::ArithTraits<ValueType>::max () <<
". "
776 "This means that it is not possible to use this constructor.");
777 TEUCHOS_TEST_FOR_EXCEPTION
778 (numKeys >
static_cast<offset_type
> (INT_MAX), std::logic_error, prefix <<
779 "This class currently only works when the number of keys is <= INT_MAX = "
780 << INT_MAX <<
". If this is a problem for you, please talk to the Tpetra "
783 const bool buildInParallel =
784 FHT::worthBuildingFixedHashTableInParallel<execution_space> ();
795 if (computeInitContigKeys) {
809 auto keys_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
811 firstContigKey_ = keys_h[0];
815 lastContigKey_ = firstContigKey_ + 1;
821 for (offset_type k = 1; k < numKeys; ++k) {
822 if (lastContigKey_ != keys_h[k]) {
831 firstContigKey_ = firstContigKey;
832 lastContigKey_ = lastContigKey;
835 offset_type startIndex;
837 initMinKey = std::min (initMinKey, firstContigKey_);
838 initMaxKey = std::max (initMaxKey, lastContigKey_);
839 startIndex =
static_cast<offset_type
> (lastContigKey_ - firstContigKey_);
844 const offset_type theNumKeys = numKeys - startIndex;
845 const offset_type size = hash_type::getRecommendedSize (theNumKeys);
846#ifdef HAVE_TPETRA_DEBUG
847 TEUCHOS_TEST_FOR_EXCEPTION(
848 size == 0 && numKeys != 0, std::logic_error,
849 "Tpetra::Details::FixedHashTable constructor: "
850 "getRecommendedSize(" << numKeys <<
") returned zero, "
851 "even though the number of keys " << numKeys <<
" is nonzero. "
852 "Please report this bug to the Tpetra developers.");
855 subview (keys, std::pair<offset_type, offset_type> (startIndex, numKeys));
862 typedef typename ptr_type::non_const_type counts_type;
863 counts_type counts (
"Tpetra::FixedHashTable::counts", size);
870 typename keys_type::HostMirror theKeysHost;
877 if (buildInParallel) {
879 using range_type = Kokkos::RangePolicy<execution_space, offset_type>;
880 const char kernelLabel[] =
"Tpetra::Details::FixedHashTable CountBuckets";
882 using key_type =
typename keys_type::non_const_value_type;
883 Kokkos::pair<int, key_type> err;
884 Kokkos::parallel_reduce (kernelLabel, range_type (0, theNumKeys),
886 TEUCHOS_TEST_FOR_EXCEPTION
887 (err.first != 0, std::logic_error,
"Tpetra::Details::FixedHashTable "
888 "constructor: CountBuckets found a key " << err.second <<
" that "
889 "results in an out-of-bounds hash value.");
892 Kokkos::parallel_for (kernelLabel, range_type (0, theNumKeys), functor);
896 Kokkos::HostSpace hostMemSpace;
897 theKeysHost = Kokkos::create_mirror_view(theKeys);
899 Kokkos::deep_copy(execution_space(), theKeysHost, theKeys);
900 auto countsHost = Kokkos::create_mirror_view (hostMemSpace, counts);
902 for (offset_type k = 0; k < theNumKeys; ++k) {
903 using key_type =
typename keys_type::non_const_value_type;
904 const key_type key = theKeysHost[k];
906 using hash_value_type =
typename hash_type::result_type;
907 const hash_value_type hashVal = hash_type::hashFunc (key, size);
908 TEUCHOS_TEST_FOR_EXCEPTION
909 (hashVal < hash_value_type (0) ||
910 hashVal >= hash_value_type (countsHost.extent (0)),
911 std::logic_error,
"Tpetra::Details::FixedHashTable "
912 "constructor: Sequential CountBuckets found a key " << key
913 <<
" that results in an out-of-bounds hash value.");
915 ++countsHost[hashVal];
918 Kokkos::deep_copy (execution_space(), counts, countsHost);
924 execution_space().fence ();
927 typename ptr_type::non_const_type ptr (
"Tpetra::FixedHashTable::ptr", size+1);
943 if (buildInParallel) {
947 if (! buildInParallel || debug) {
948 Kokkos::HostSpace hostMemSpace;
949 auto counts_h = Kokkos::create_mirror_view_and_copy (hostMemSpace, counts);
950 auto ptr_h = Kokkos::create_mirror_view (hostMemSpace, ptr);
952#ifdef KOKKOS_ENABLE_SERIAL
953 Kokkos::Serial hostExecSpace;
955 Kokkos::DefaultHostExecutionSpace hostExecSpace;
960 Kokkos::deep_copy (execution_space(), ptr, ptr_h);
964 for (offset_type i = 0; i < size; ++i) {
965 if (ptr_h[i+1] != ptr_h[i] + counts_h[i]) {
969 TEUCHOS_TEST_FOR_EXCEPTION
970 (bad, std::logic_error,
"Tpetra::Details::FixedHashTable "
971 "constructor: computeOffsetsFromCounts gave an incorrect "
979 execution_space().fence ();
983 typedef typename val_type::non_const_type nonconst_val_type;
984 nonconst_val_type val (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::pairs"),
988 typedef FHT::FillPairs<
typename val_type::non_const_type, keys_type,
989 typename ptr_type::non_const_type> functor_type;
990 typename functor_type::value_type result (initMinKey, initMaxKey);
992 const ValueType newStartingValue = startingValue +
static_cast<ValueType
> (startIndex);
993 if (buildInParallel) {
994 functor_type functor (val, counts, ptr, theKeys, newStartingValue,
995 initMinKey, initMaxKey);
996 typedef Kokkos::RangePolicy<execution_space, offset_type> range_type;
997 Kokkos::parallel_reduce (
"Tpetra::Details::FixedHashTable::FillPairs", range_type (0, theNumKeys), functor, result);
1000 Kokkos::HostSpace hostMemSpace;
1001 auto counts_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, counts);
1002 auto ptr_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, ptr);
1003 auto val_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, val);
1004 for (offset_type k = 0; k < theNumKeys; ++k) {
1005 typedef typename hash_type::result_type hash_value_type;
1006 const KeyType key = theKeysHost[k];
1007 if (key > result.maxKey_) {
1008 result.maxKey_ = key;
1010 if (key < result.minKey_) {
1011 result.minKey_ = key;
1013 const ValueType theVal = newStartingValue +
static_cast<ValueType
> (k);
1014 const hash_value_type hashVal = hash_type::hashFunc (key, size);
1017 const offset_type count = counts_h[hashVal];
1018 --counts_h[hashVal];
1020 result.success_ =
false;
1024 const offset_type curPos = ptr_h[hashVal+1] - count;
1025 val_h[curPos].first = key;
1026 val_h[curPos].second = theVal;
1029 Kokkos::deep_copy(counts, counts_h);
1030 Kokkos::deep_copy(val, val_h);
1045 minKey_ = result.minKey_;
1046 maxKey_ = result.maxKey_;
1051template<
class KeyType,
class ValueType,
class DeviceType>
1054init (
const host_input_keys_type& keys,
1055 const host_input_vals_type& vals,
1059 Tpetra::Details::ProfilingRegion pr(
"Tpetra::Details::FixedHashTable::init(4-arg)");
1060 const offset_type numKeys =
static_cast<offset_type
> (keys.extent (0));
1061 TEUCHOS_TEST_FOR_EXCEPTION
1062 (
static_cast<unsigned long long> (numKeys) >
static_cast<unsigned long long> (::Kokkos::ArithTraits<ValueType>::max ()),
1063 std::invalid_argument,
"Tpetra::Details::FixedHashTable: The number of "
1064 "keys " << numKeys <<
" is greater than the maximum representable "
1065 "ValueType value " << ::Kokkos::ArithTraits<ValueType>::max () <<
".");
1066 TEUCHOS_TEST_FOR_EXCEPTION
1067 (numKeys >
static_cast<offset_type
> (INT_MAX), std::logic_error,
"Tpetra::"
1068 "Details::FixedHashTable: This class currently only works when the number "
1069 "of keys is <= INT_MAX = " << INT_MAX <<
". If this is a problem for you"
1070 ", please talk to the Tpetra developers.");
1077 const offset_type size = hash_type::getRecommendedSize (numKeys);
1078#ifdef HAVE_TPETRA_DEBUG
1079 TEUCHOS_TEST_FOR_EXCEPTION(
1080 size == 0 && numKeys != 0, std::logic_error,
1081 "Tpetra::Details::FixedHashTable constructor: "
1082 "getRecommendedSize(" << numKeys <<
") returned zero, "
1083 "even though the number of keys " << numKeys <<
" is nonzero. "
1084 "Please report this bug to the Tpetra developers.");
1093 Kokkos::HostSpace hostMemSpace;
1094 typename ptr_type::non_const_type ptr (
"Tpetra::FixedHashTable::ptr", size + 1);
1095 auto ptr_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, ptr);
1099 using Kokkos::ViewAllocateWithoutInitializing;
1100 typedef typename val_type::non_const_type nonconst_val_type;
1101 nonconst_val_type val (ViewAllocateWithoutInitializing (
"Tpetra::FixedHashTable::pairs"),
1103 auto val_h = Kokkos::create_mirror_view_and_copy(hostMemSpace, val);
1106 for (offset_type k = 0; k < numKeys; ++k) {
1107 const typename hash_type::result_type hashVal =
1108 hash_type::hashFunc (keys[k], size);
1120 for (offset_type i = 0; i < size; ++i) {
1121 ptr_h[i+1] += ptr_h[i];
1126 typename ptr_type::non_const_type::HostMirror curRowStart (
"Tpetra::FixedHashTable::curRowStart", size);
1130 for (offset_type k = 0; k < numKeys; ++k) {
1131 typedef typename hash_type::result_type hash_value_type;
1132 const KeyType key = keys[k];
1133 if (key > result.maxKey_) {
1134 result.maxKey_ = key;
1136 if (key < result.minKey_) {
1137 result.minKey_ = key;
1139 const ValueType theVal = vals[k];
1140 if (theVal > maxVal_) {
1143 if (theVal < minVal_) {
1146 const hash_value_type hashVal = hash_type::hashFunc (key, size);
1148 const offset_type offset = curRowStart[hashVal];
1149 const offset_type curPos = ptr_h[hashVal] + offset;
1150 if (curPos >= ptr_h[hashVal+1]) {
1151 result.success_ =
false;
1154 val_h[curPos].first = key;
1155 val_h[curPos].second = theVal;
1156 ++curRowStart[hashVal];
1160 TEUCHOS_TEST_FOR_EXCEPTION
1161 (! result.success_, std::logic_error,
"Tpetra::Details::FixedHashTable::"
1162 "init: Filling the hash table failed! Please report this bug to the "
1163 "Tpetra developers.");
1166 Kokkos::deep_copy(ptr, ptr_h);
1167 Kokkos::deep_copy(val, val_h);
1171 minKey_ = result.minKey_;
1172 maxKey_ = result.maxKey_;
1176template <
class KeyType,
class ValueType,
class DeviceType>
1181 if (! checkedForDuplicateKeys_) {
1182 hasDuplicateKeys_ = checkForDuplicateKeys ();
1183 checkedForDuplicateKeys_ =
true;
1185 return hasDuplicateKeys_;
1188template <
class KeyType,
class ValueType,
class DeviceType>
1193 const offset_type size = this->getSize ();
1197 if (size == 0 || this->numPairs () == 0) {
1201 typedef FHT::CheckForDuplicateKeys<ptr_type, val_type> functor_type;
1202 functor_type functor (val_, ptr_);
1204 typedef Kokkos::RangePolicy<execution_space, offset_type> range_type;
1205 Kokkos::parallel_reduce (
"Tpetra::Details::FixedHashTable::CheckForDuplicateKeys", range_type (0, size), functor, hasDupKeys);
1206 return hasDupKeys > 0;
1210template <
class KeyType,
class ValueType,
class DeviceType>
1215 std::ostringstream oss;
1216 oss <<
"FixedHashTable<"
1217 << Teuchos::TypeNameTraits<KeyType>::name () <<
","
1218 << Teuchos::TypeNameTraits<ValueType>::name () <<
">: "
1219 <<
"{ numKeys: " << val_.extent (0)
1220 <<
", tableSize: " << this->getSize () <<
" }";
1224template <
class KeyType,
class ValueType,
class DeviceType>
1227describe (Teuchos::FancyOStream& out,
1228 const Teuchos::EVerbosityLevel verbLevel)
const
1232 using Teuchos::OSTab;
1233 using Teuchos::rcpFromRef;
1234 using Teuchos::TypeNameTraits;
1235 using Teuchos::VERB_DEFAULT;
1236 using Teuchos::VERB_NONE;
1237 using Teuchos::VERB_LOW;
1238 using Teuchos::VERB_EXTREME;
1243 Teuchos::EVerbosityLevel vl = verbLevel;
1244 if (vl == VERB_DEFAULT) vl = VERB_LOW;
1246 if (vl == VERB_NONE) {
1249 else if (vl == VERB_LOW) {
1253 out <<
"FixedHashTable:" << endl;
1255 OSTab tab1 (rcpFromRef (out));
1261 out <<
"Template parameters:" << endl;
1263 OSTab tab2 (rcpFromRef (out));
1264 out <<
"KeyType: " << TypeNameTraits<KeyType>::name () << endl
1265 <<
"ValueType: " << TypeNameTraits<ValueType>::name () << endl;
1268 const offset_type tableSize = this->getSize ();
1269 const offset_type numKeys = val_.extent (0);
1271 out <<
"Table parameters:" << endl;
1273 OSTab tab2 (rcpFromRef (out));
1274 out <<
"numKeys: " << numKeys << endl
1275 <<
"tableSize: " << tableSize << endl;
1278 if (vl >= VERB_EXTREME) {
1279 out <<
"Contents: ";
1280 if (tableSize == 0 || numKeys == 0) {
1281 out <<
"[]" << endl;
1283 out <<
"[ " << endl;
1285 OSTab tab2 (rcpFromRef (out));
1286 for (offset_type i = 0; i < tableSize; ++i) {
1287 OSTab tab3 (rcpFromRef (out));
1289 for (offset_type k = ptr_[i]; k < ptr_[i+1]; ++k) {
1290 out <<
"(" << val_[k].first <<
"," << val_[k].second <<
")";
1291 if (k + 1 < ptr_[i+1]) {