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 {
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
97template<class KeyType, class DeviceType, class OffsetType>
98struct 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
Nonmember function that computes a residual Computes R = B - A * X.
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.
static KOKKOS_INLINE_FUNCTION result_type hashFunc(const argument_type &, const offset_type &)
The hash function.
typename std::make_signed< typename Kokkos::View< KeyType *, device_type >::size_type >::type offset_type
static result_type getRecommendedSize(const offset_type)
Number of "buckets" that the constructor of FixedHashTable should allocate.