209 using host_mirror_space =
210 typename ViewTraits<Key, Device, void, void>::host_mirror_space;
218 using key_type =
typename std::remove_const<declared_key_type>::type;
219 using const_key_type =
typename std::add_const<key_type>::type;
222 using declared_value_type = Value;
223 using value_type =
typename std::remove_const<declared_value_type>::type;
224 using const_value_type =
typename std::add_const<value_type>::type;
226 using device_type = Device;
227 using execution_space =
typename Device::execution_space;
244 static const bool is_set = std::is_same<void, value_type>::value;
245 static const bool has_const_key =
246 std::is_same<const_key_type, declared_key_type>::value;
247 static const bool has_const_value =
248 is_set || std::is_same<const_value_type, declared_value_type>::value;
250 static const bool is_insertable_map =
251 !has_const_key && (is_set || !has_const_value);
252 static const bool is_modifiable_map = has_const_key && !has_const_value;
253 static const bool is_const_map = has_const_key && has_const_value;
260 using histogram_type = Impl::UnorderedMapHistogram<const_map_type>;
267 using impl_value_type = std::conditional_t<is_set, int, declared_value_type>;
269 using key_type_view = std::conditional_t<
273 using value_type_view = std::conditional_t<
274 is_insertable_map || is_modifiable_map,
278 using size_type_view = std::conditional_t<
283 std::conditional_t<is_insertable_map, Bitset<execution_space>,
286 enum { modified_idx = 0, erasable_idx = 1, failed_insert_idx = 2 };
287 enum { num_scalars = 3 };
302 : m_bounded_insert(
true),
304 m_equal_to(equal_to),
313 m_keys(
"UnorderedMap keys",
capacity() + 1),
314 m_values(
"UnorderedMap values", (is_set ? 1 :
capacity() + 1)),
315 m_scalars(
"UnorderedMap scalars") {
316 if (!is_insertable_map) {
317 throw std::runtime_error(
318 "Cannot construct a non-insertable (i.e. const key_type) "
322 Kokkos::deep_copy(m_hash_lists, invalid_index);
323 Kokkos::deep_copy(m_next_index, invalid_index);
326 void reset_failed_insert_flag() { reset_flag(failed_insert_idx); }
328 histogram_type get_histogram() {
return histogram_type(*
this); }
332 m_bounded_insert =
true;
336 m_available_indexes.clear();
338 Kokkos::deep_copy(m_hash_lists, invalid_index);
339 Kokkos::deep_copy(m_next_index, invalid_index);
341 const key_type
tmp = key_type();
342 Kokkos::deep_copy(m_keys,
tmp);
345 const impl_value_type
tmp = impl_value_type();
346 Kokkos::deep_copy(m_values,
tmp);
348 { Kokkos::deep_copy(m_scalars, 0); }
351 KOKKOS_INLINE_FUNCTION
constexpr bool is_allocated()
const {
352 return (m_keys.is_allocated() && m_values.is_allocated() &&
353 m_scalars.is_allocated());
372 if (!is_insertable_map)
return false;
381 tmp.m_bounded_insert =
false;
382 Impl::UnorderedMapRehash<insertable_map_type>
f(
tmp, *
this);
385 tmp.m_bounded_insert = bounded_insert;
402 m_size = m_available_indexes.count();
403 reset_flag(modified_idx);
415 bool erasable()
const {
416 return is_insertable_map ? get_flag(erasable_idx) :
false;
420 bool result = !erasable();
421 if (is_insertable_map && result) {
422 execution_space().fence();
423 set_flag(erasable_idx);
424 execution_space().fence();
430 bool result = erasable();
431 if (is_insertable_map && result) {
432 execution_space().fence();
433 Impl::UnorderedMapErase<declared_map_type> f(*
this);
435 execution_space().fence();
436 reset_flag(erasable_idx);
445 KOKKOS_FORCEINLINE_FUNCTION
458 KOKKOS_INLINE_FUNCTION
472 KOKKOS_INLINE_FUNCTION
474 impl_value_type
const &
v = impl_value_type())
const {
477 if (!is_insertable_map ||
capacity() == 0
u ||
478 m_scalars((
int)erasable_idx)) {
482 if (!m_scalars((
int)modified_idx)) {
483 m_scalars((
int)modified_idx) =
true;
505 : m_available_indexes.max_hint();
518 KOKKOS_NONTEMPORAL_PREFETCH_LOAD(
519 &m_keys[
curr != invalid_index ?
curr : 0]);
523 while (
curr != invalid_index &&
525 result.increment_list_position();
529 KOKKOS_NONTEMPORAL_PREFETCH_LOAD(
530 &m_keys[
curr != invalid_index ?
curr : 0]);
535 if (
curr != invalid_index) {
540 if (!m_available_indexes.reset(
new_index)) {
541 KOKKOS_IMPL_DO_NOT_USE_PRINTF(
"Unable to free existing\n");
564 }
else if (m_available_indexes.set(
index_hint)) {
567 KOKKOS_NONTEMPORAL_PREFETCH_STORE(&m_keys[
new_index]);
571 KOKKOS_NONTEMPORAL_PREFETCH_STORE(&m_values[
new_index]);
599 KOKKOS_INLINE_FUNCTION
600 bool erase(key_type
const &
k)
const {
603 if (is_insertable_map && 0
u <
capacity() && m_scalars((
int)erasable_idx)) {
604 if (!m_scalars((
int)modified_idx)) {
605 m_scalars((
int)modified_idx) =
true;
608 size_type index =
find(k);
609 if (valid_at(index)) {
610 m_available_indexes.reset(index);
625 KOKKOS_INLINE_FUNCTION
628 ? m_hash_lists(m_hasher(
k) % m_hash_lists.extent(0))
631 KOKKOS_NONTEMPORAL_PREFETCH_LOAD(&m_keys[
curr != invalid_index ?
curr : 0]);
632 while (
curr != invalid_index && !m_equal_to(m_keys[
curr],
k)) {
633 KOKKOS_NONTEMPORAL_PREFETCH_LOAD(
634 &m_keys[
curr != invalid_index ?
curr : 0]);
645 KOKKOS_INLINE_FUNCTION
656 KOKKOS_FORCEINLINE_FUNCTION
657 std::conditional_t<(is_set || has_const_value), impl_value_type,
669 KOKKOS_FORCEINLINE_FUNCTION
674 KOKKOS_FORCEINLINE_FUNCTION
675 bool valid_at(size_type
i)
const {
return m_available_indexes.test(
i); }
677 template <
typename SKey,
typename SValue>
679 UnorderedMap<SKey, SValue, Device, Hasher, EqualTo>
const &src,
680 typename std::enable_if<
681 Impl::UnorderedMapCanAssign<declared_key_type, declared_value_type,
682 SKey, SValue>::value,
684 : m_bounded_insert(src.m_bounded_insert),
685 m_hasher(src.m_hasher),
686 m_equal_to(src.m_equal_to),
688 m_available_indexes(src.m_available_indexes),
689 m_hash_lists(src.m_hash_lists),
690 m_next_index(src.m_next_index),
692 m_values(src.m_values),
693 m_scalars(src.m_scalars) {}
695 template <
typename SKey,
typename SValue>
696 typename std::enable_if<
697 Impl::UnorderedMapCanAssign<declared_key_type, declared_value_type, SKey,
699 declared_map_type &>::type
700 operator=(UnorderedMap<SKey, SValue, Device, Hasher, EqualTo>
const &src) {
701 m_bounded_insert = src.m_bounded_insert;
702 m_hasher = src.m_hasher;
703 m_equal_to = src.m_equal_to;
705 m_available_indexes = src.m_available_indexes;
706 m_hash_lists = src.m_hash_lists;
707 m_next_index = src.m_next_index;
709 m_values = src.m_values;
710 m_scalars = src.m_scalars;
714 template <
typename SKey,
typename SValue,
typename SDevice>
715 typename std::enable_if<
716 std::is_same<typename std::remove_const<SKey>::type, key_type>::value &&
717 std::is_same<typename std::remove_const<SValue>::type,
718 value_type>::value>::type
720 UnorderedMap<SKey, SValue, SDevice, Hasher, EqualTo>
const &src) {
721 if (m_hash_lists.data() != src.m_hash_lists.data()) {
722 insertable_map_type tmp;
724 tmp.m_bounded_insert = src.m_bounded_insert;
725 tmp.m_hasher = src.m_hasher;
726 tmp.m_equal_to = src.m_equal_to;
727 tmp.m_size = src.
size();
728 tmp.m_available_indexes = bitset_type(src.capacity());
729 tmp.m_hash_lists = size_type_view(
730 view_alloc(WithoutInitializing,
"UnorderedMap hash list"),
731 src.m_hash_lists.extent(0));
732 tmp.m_next_index = size_type_view(
733 view_alloc(WithoutInitializing,
"UnorderedMap next index"),
734 src.m_next_index.extent(0));
736 key_type_view(view_alloc(WithoutInitializing,
"UnorderedMap keys"),
737 src.m_keys.extent(0));
738 tmp.m_values = value_type_view(
739 view_alloc(WithoutInitializing,
"UnorderedMap values"),
740 src.m_values.extent(0));
741 tmp.m_scalars = scalars_view(
"UnorderedMap scalars");
743 Kokkos::deep_copy(tmp.m_available_indexes, src.m_available_indexes);
745 using raw_deep_copy =
746 Kokkos::Impl::DeepCopy<
typename device_type::memory_space,
747 typename SDevice::memory_space>;
749 raw_deep_copy(tmp.m_hash_lists.data(), src.m_hash_lists.data(),
750 sizeof(size_type) * src.m_hash_lists.extent(0));
751 raw_deep_copy(tmp.m_next_index.data(), src.m_next_index.data(),
752 sizeof(size_type) * src.m_next_index.extent(0));
753 raw_deep_copy(tmp.m_keys.data(), src.m_keys.data(),
754 sizeof(key_type) * src.m_keys.extent(0));
756 raw_deep_copy(tmp.m_values.data(), src.m_values.data(),
757 sizeof(impl_value_type) * src.m_values.extent(0));
759 raw_deep_copy(tmp.m_scalars.data(), src.m_scalars.data(),
760 sizeof(
int) * num_scalars);
768 bool modified()
const {
return get_flag(modified_idx); }
770 void set_flag(
int flag)
const {
771 using raw_deep_copy =
772 Kokkos::Impl::DeepCopy<
typename device_type::memory_space,
774 const int true_ =
true;
775 raw_deep_copy(m_scalars.data() + flag, &true_,
sizeof(
int));
778 void reset_flag(
int flag)
const {
779 using raw_deep_copy =
780 Kokkos::Impl::DeepCopy<
typename device_type::memory_space,
782 const int false_ =
false;
783 raw_deep_copy(m_scalars.data() + flag, &false_,
sizeof(
int));
786 bool get_flag(
int flag)
const {
787 using raw_deep_copy =
789 typename device_type::memory_space>;
791 raw_deep_copy(&result, m_scalars.data() + flag,
sizeof(
int));
795 static uint32_t calculate_capacity(uint32_t capacity_hint) {
798 ? ((
static_cast<uint32_t
>(7ull * capacity_hint / 6u) + 127u) /
805 bool m_bounded_insert;
806 hasher_type m_hasher;
807 equal_to_type m_equal_to;
808 mutable size_type m_size;
809 bitset_type m_available_indexes;
810 size_type_view m_hash_lists;
811 size_type_view m_next_index;
812 key_type_view m_keys;
813 value_type_view m_values;
814 scalars_view m_scalars;
816 template <
typename KKey,
typename VValue,
typename DDevice,
typename HHash,
818 friend class UnorderedMap;
820 template <
typename UMap>
821 friend struct Impl::UnorderedMapErase;
823 template <
typename UMap>
824 friend struct Impl::UnorderedMapHistogram;
826 template <
typename UMap>
827 friend struct Impl::UnorderedMapPrint;