Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_Details_Hash.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_DETAILS_HASH_HPP
11 #define TPETRA_DETAILS_HASH_HPP
12 
13 #include "Tpetra_ConfigDefs.hpp"
14 #ifdef TPETRA_USE_MURMUR_HASH
15 #include <Kokkos_Functional.hpp> // hash function used by Kokkos::UnorderedMap
16 #endif // TPETRA_USE_MURMUR_HASH
17 #include <type_traits> // make_signed
18 
19 namespace Tpetra {
20 namespace Details {
21 
22 namespace Impl {
23 
25 int getRecommendedSizeInt(const int size);
26 
27 } // namespace Impl
28 
37 template <class KeyType,
38  class DeviceType,
39  class OffsetType = typename std::make_signed<typename Kokkos::View<KeyType*, DeviceType>::size_type>::type,
40  class ResultType = int>
41 struct Hash {
45  typedef KeyType argument_type;
46 
50  typedef ResultType result_type;
51 
53  typedef OffsetType offset_type;
54 
61  static KOKKOS_INLINE_FUNCTION result_type
62  hashFunc(const argument_type& /*key*/, const offset_type& /*size*/) {
63  static_assert(!std::is_same<result_type, int>::value,
64  "Not yet implemented for ResultType != int");
65  }
66 
78  static result_type getRecommendedSize(const offset_type /*size*/) {
79  static_assert(!std::is_same<result_type, int>::value,
80  "Not yet implemented for ResultType != int");
81  }
82 };
83 
97 template <class KeyType, class DeviceType, class OffsetType>
98 struct Hash<KeyType, DeviceType, OffsetType, int> {
102  typedef KeyType argument_type;
103 
107  typedef int result_type;
108 
110  typedef OffsetType offset_type;
111 
118  static KOKKOS_INLINE_FUNCTION result_type
119  hashFunc(const argument_type& key, const offset_type& size) {
120 #ifdef TPETRA_USE_MURMUR_HASH
121  Kokkos::pod_hash<argument_type> hash;
122  const uint32_t k = hash(key);
123  return static_cast<result_type>(k % size);
124 #else
125  // We are using Epetra's hash function by default, as we have
126  // observed that it is much faster than the Murmur hash
127  // function. However, this is not a good hash function for general
128  // sets of keys. For our typical use case, this is good. Use
129  // Murmur hash if the maps are sparse.
130  const unsigned int seed = (2654435761U);
131  const int intkey = (int)((key & 0x000000007fffffffLL) +
132  ((key & 0x7fffffff80000000LL) >> 31));
133  return static_cast<result_type>((seed ^ intkey) % static_cast<int>(size));
134 #endif
135  }
136 
144  return Impl::getRecommendedSizeInt(static_cast<int>(size));
145  }
146 };
147 
148 } // namespace Details
149 } // namespace Tpetra
150 
151 #endif // TPETRA_DETAILS_HASH_HPP
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &, const offset_type &)
The hash function.
static result_type getRecommendedSize(const offset_type size)
Number of &quot;buckets&quot; that the constructor of FixedHashTable should allocate.
ResultType result_type
Type of the return value of the hash function.
KeyType argument_type
Type of the hash function&#39;s input.
OffsetType offset_type
Type of offsets into the hash table&#39;s array of (key,value) pairs.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &key, const offset_type &size)
The hash function.
int result_type
Type of the return value of the hash function.
The hash function for FixedHashTable.
KeyType argument_type
Type of the hash function&#39;s input.
OffsetType offset_type
Type of offsets into the hash table&#39;s array of (key,value) pairs.
static result_type getRecommendedSize(const offset_type)
Number of &quot;buckets&quot; that the constructor of FixedHashTable should allocate.