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  {
121 #ifdef TPETRA_USE_MURMUR_HASH
122  Kokkos::pod_hash<argument_type> hash;
123  const uint32_t k = hash (key);
124  return static_cast<result_type> (k % size);
125 #else
126  // We are using Epetra's hash function by default, as we have
127  // observed that it is much faster than the Murmur hash
128  // function. However, this is not a good hash function for general
129  // sets of keys. For our typical use case, this is good. Use
130  // Murmur hash if the maps are sparse.
131  const unsigned int seed = (2654435761U);
132  const int intkey = (int) ((key & 0x000000007fffffffLL) +
133  ((key & 0x7fffffff80000000LL) >> 31));
134  return static_cast<result_type> ((seed ^ intkey) % static_cast<int> (size));
135 #endif
136  }
137 
145  {
146  return Impl::getRecommendedSizeInt (static_cast<int> (size));
147  }
148 };
149 
150 } // namespace Details
151 } // namespace Tpetra
152 
153 #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.