Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
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
19namespace Tpetra {
20namespace Details {
21
22namespace Impl {
23
25int getRecommendedSizeInt(const int size);
26
27} // namespace Impl
28
37template <class KeyType,
38 class DeviceType,
39 class OffsetType = typename std::make_signed<typename Kokkos::View<KeyType*, DeviceType>::size_type>::type,
40 class ResultType = int>
41struct Hash {
46
51
54
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
79 static_assert(!std::is_same<result_type, int>::value,
80 "Not yet implemented for ResultType != int");
81 }
82};
83
97template <class KeyType, class DeviceType, class OffsetType>
98struct Hash<KeyType, DeviceType, OffsetType, int> {
103
107 typedef int result_type;
108
111
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
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
OffsetType offset_type
Type of offsets into the hash table's array of (key,value) pairs.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &key, const offset_type &size)
The hash function.
static result_type getRecommendedSize(const offset_type size)
Number of "buckets" that the constructor of FixedHashTable should allocate.
int result_type
Type of the return value of the hash function.
The hash function for FixedHashTable.
ResultType result_type
Type of the return value of the hash function.
KeyType argument_type
Type of the hash function's input.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &, const offset_type &)
The hash function.
OffsetType offset_type
Type of offsets into the hash table's array of (key,value) pairs.
static result_type getRecommendedSize(const offset_type)
Number of "buckets" that the constructor of FixedHashTable should allocate.