378 dst.success_ =
false;
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;
443 const offsets_view_type&
ptr) :
446 size_ (ptr_.extent (0) == 0 ?
462 dst = dst + src > 0?1:0;
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];
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) {
498 pairs_view_type pairs_;
499 offsets_view_type ptr_;
509template<
class KeyType,
class ValueType,
class DeviceType>
516 checkedForDuplicateKeys_ (
false)
525template<
class KeyType,
class ValueType,
class DeviceType>
532 checkedForDuplicateKeys_ (
false)
542 using Kokkos::ViewAllocateWithoutInitializing;
553template<
class KeyType,
class ValueType,
class DeviceType>
561 checkedForDuplicateKeys_ (
false)
570 using Kokkos::ViewAllocateWithoutInitializing;
590 ::Kokkos::ArithTraits<KeyType>::min () :
591 -::Kokkos::ArithTraits<KeyType>::max ();
597template<
class KeyType,
class ValueType,
class DeviceType>
609 checkedForDuplicateKeys_ (
false)
625 ::Kokkos::ArithTraits<KeyType>::min () :
626 -::Kokkos::ArithTraits<KeyType>::max ();
631template<
class KeyType,
class ValueType,
class DeviceType>
643 checkedForDuplicateKeys_ (
false)
652 using Kokkos::ViewAllocateWithoutInitializing;
672 ::Kokkos::ArithTraits<KeyType>::min () :
673 -::Kokkos::ArithTraits<KeyType>::max ();
678template<
class KeyType,
class ValueType,
class DeviceType>
686 checkedForDuplicateKeys_ (
false)
702 ::Kokkos::ArithTraits<KeyType>::min () :
703 -::Kokkos::ArithTraits<KeyType>::max ();
708template<
class KeyType,
class ValueType,
class DeviceType>
711 const Teuchos::ArrayView<const ValueType>&
vals) :
712 contiguousValues_ (
false),
713 checkedForDuplicateKeys_ (
false)
736 ::Kokkos::ArithTraits<KeyType>::min () :
737 -::Kokkos::ArithTraits<KeyType>::max ();
741template<
class KeyType,
class ValueType,
class DeviceType>
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 ();
765 "number of keys " <<
keys.extent (0) <<
" does not fit in "
767 "max value is " <<
theMaxVal <<
". This means that it is not possible to "
768 "use this constructor.");
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.");
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 "
784 FHT::worthBuildingFixedHashTableInParallel<execution_space> ();
809 auto keys_h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
811 firstContigKey_ =
keys_h[0];
815 lastContigKey_ = firstContigKey_ + 1;
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) {
878 FHT::CountBuckets<counts_type, keys_type> functor (counts, theKeys, size);
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);
942 using ::Tpetra::Details::computeOffsetsFromCounts;
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>
1053FixedHashTable<KeyType, ValueType, DeviceType>::
1054init (
const host_input_keys_type& keys,
1055 const host_input_vals_type& vals,
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);
1129 FHT::FillPairsResult<KeyType> result (initMinKey, initMaxKey);
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;
1204 typedef Kokkos::RangePolicy<execution_space, offset_type> range_type;
1205 Kokkos::parallel_reduce (
"Tpetra::Details::FixedHashTable::CheckForDuplicateKeys", range_type (0,
size),
functor,
hasDupKeys);
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>
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;
1250 out << this->description() <<
endl;
1253 out <<
"FixedHashTable:" <<
endl;
1261 out <<
"Template parameters:" <<
endl;
1268 const offset_type
tableSize = this->getSize ();
1269 const offset_type
numKeys = val_.extent (0);
1271 out <<
"Table parameters:" <<
endl;
1279 out <<
"Contents: ";
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]) {