-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable hash computation from variable length keys #327
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5e52320
Enable hash computation from variable length keys
sleeepyjack e2cef98
Merge remote-tracking branch 'upstream/dev' into dynamic-hash
sleeepyjack acd719e
Revert murmurhash3 changes
sleeepyjack 270a94a
Remove useless attributes
sleeepyjack a73e3ff
Add unit test for dynamic vs. static key sizes
sleeepyjack 4a95fa8
Enable dynamic key length for mumurhash3
sleeepyjack b15d9aa
Fix implicit fallthrough warning
sleeepyjack fd2ed9c
Use reinterpret_cast instead of C-style cast
sleeepyjack 3aedeb0
Use memcpy instead of reinterpret_cast
sleeepyjack 669467d
Remove sync tag from hash bench for better accuracy
sleeepyjack 418b419
XXHash cleanups
sleeepyjack afddf1f
MurmurHash3 cleanups
sleeepyjack fc95dcb
Fix typo in comment
sleeepyjack f3b435c
Fix load_chunk
sleeepyjack 5940a96
Merge remote-tracking branch 'upstream/dev' into dynamic-hash
sleeepyjack 74a3739
Use compute_hash function instead of operator
sleeepyjack 78c1527
Merge remote-tracking branch 'upstream/dev' into dynamic-hash
sleeepyjack b821589
Use reinterpret_cast instead of memcpy /sadface
sleeepyjack d2a2538
Merge remote-tracking branch 'upstream/dev' into dynamic-hash
sleeepyjack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,11 @@ | |
|
||
#pragma once | ||
|
||
#include <cuco/detail/hash_functions/utils.cuh> | ||
#include <cuco/extent.cuh> | ||
|
||
#include <cstdint> | ||
#include <type_traits> | ||
|
||
namespace cuco::detail { | ||
|
||
|
@@ -31,15 +35,15 @@ template <typename Key> | |
struct MurmurHash3_fmix32 { | ||
static_assert(sizeof(Key) == 4, "Key type must be 4 bytes in size."); | ||
|
||
using argument_type = Key; ///< The type of the values taken as argument | ||
using result_type = uint32_t; ///< The type of the hash values produced | ||
using argument_type = Key; ///< The type of the values taken as argument | ||
using result_type = std::uint32_t; ///< The type of the hash values produced | ||
|
||
/** | ||
* @brief Constructs a MurmurHash3_fmix32 hash function with the given `seed`. | ||
* | ||
* @param seed A custom number to randomize the resulting hash value | ||
*/ | ||
__host__ __device__ constexpr MurmurHash3_fmix32(uint32_t seed = 0) : seed_{seed} {} | ||
__host__ __device__ constexpr MurmurHash3_fmix32(std::uint32_t seed = 0) : seed_{seed} {} | ||
|
||
/** | ||
* @brief Returns a hash value for its argument, as a value of type `result_type`. | ||
|
@@ -49,7 +53,7 @@ struct MurmurHash3_fmix32 { | |
*/ | ||
constexpr result_type __host__ __device__ operator()(Key const& key) const noexcept | ||
{ | ||
uint32_t h = static_cast<uint32_t>(key) ^ seed_; | ||
std::uint32_t h = static_cast<std::uint32_t>(key) ^ seed_; | ||
h ^= h >> 16; | ||
h *= 0x85ebca6b; | ||
h ^= h >> 13; | ||
|
@@ -59,7 +63,7 @@ struct MurmurHash3_fmix32 { | |
} | ||
|
||
private: | ||
uint32_t seed_; | ||
std::uint32_t seed_; | ||
}; | ||
|
||
/** | ||
|
@@ -73,15 +77,15 @@ template <typename Key> | |
struct MurmurHash3_fmix64 { | ||
static_assert(sizeof(Key) == 8, "Key type must be 8 bytes in size."); | ||
|
||
using argument_type = Key; ///< The type of the values taken as argument | ||
using result_type = uint64_t; ///< The type of the hash values produced | ||
using argument_type = Key; ///< The type of the values taken as argument | ||
using result_type = std::uint64_t; ///< The type of the hash values produced | ||
|
||
/** | ||
* @brief Constructs a MurmurHash3_fmix64 hash function with the given `seed`. | ||
* | ||
* @param seed A custom number to randomize the resulting hash value | ||
*/ | ||
__host__ __device__ constexpr MurmurHash3_fmix64(uint64_t seed = 0) : seed_{seed} {} | ||
__host__ __device__ constexpr MurmurHash3_fmix64(std::uint64_t seed = 0) : seed_{seed} {} | ||
|
||
/** | ||
* @brief Returns a hash value for its argument, as a value of type `result_type`. | ||
|
@@ -91,7 +95,7 @@ struct MurmurHash3_fmix64 { | |
*/ | ||
constexpr result_type __host__ __device__ operator()(Key const& key) const noexcept | ||
{ | ||
uint64_t h = static_cast<uint64_t>(key) ^ seed_; | ||
std::uint64_t h = static_cast<std::uint64_t>(key) ^ seed_; | ||
h ^= h >> 33; | ||
h *= 0xff51afd7ed558ccd; | ||
h ^= h >> 33; | ||
|
@@ -101,7 +105,7 @@ struct MurmurHash3_fmix64 { | |
} | ||
|
||
private: | ||
uint64_t seed_; | ||
std::uint64_t seed_; | ||
}; | ||
|
||
/** | ||
|
@@ -121,15 +125,15 @@ struct MurmurHash3_fmix64 { | |
*/ | ||
template <typename Key> | ||
struct MurmurHash3_32 { | ||
using argument_type = Key; ///< The type of the values taken as argument | ||
using result_type = uint32_t; ///< The type of the hash values produced | ||
using argument_type = Key; ///< The type of the values taken as argument | ||
using result_type = std::uint32_t; ///< The type of the hash values produced | ||
|
||
/** | ||
* @brief Constructs a MurmurHash3_32 hash function with the given `seed`. | ||
* | ||
* @param seed A custom number to randomize the resulting hash value | ||
*/ | ||
__host__ __device__ constexpr MurmurHash3_32(uint32_t seed = 0) : fmix32_{0}, seed_{seed} {} | ||
__host__ __device__ constexpr MurmurHash3_32(std::uint32_t seed = 0) : fmix32_{0}, seed_{seed} {} | ||
|
||
/** | ||
* @brief Returns a hash value for its argument, as a value of type `result_type`. | ||
|
@@ -139,18 +143,31 @@ struct MurmurHash3_32 { | |
*/ | ||
constexpr result_type __host__ __device__ operator()(Key const& key) const noexcept | ||
{ | ||
constexpr int len = sizeof(argument_type); | ||
const uint8_t* const data = (const uint8_t*)&key; | ||
constexpr int nblocks = len / 4; | ||
return (*this)(key, cuco::experimental::extent<std::size_t, sizeof(Key)>{}); | ||
} | ||
|
||
/** | ||
* @brief Returns a hash value for its argument, as a value of type `result_type`. | ||
* | ||
* @tparam Extent The extent type | ||
* | ||
* @param key The input argument to hash | ||
* @param size The extent of the key in bytes | ||
* @return A resulting hash value for `key` | ||
*/ | ||
template <typename Extent> | ||
constexpr result_type __host__ __device__ operator()(Key const& key, Extent size) const noexcept | ||
{ | ||
auto const data = reinterpret_cast<std::uint8_t const*>(&key); | ||
auto const nblocks = size / 4; | ||
|
||
uint32_t h1 = seed_; | ||
constexpr uint32_t c1 = 0xcc9e2d51; | ||
constexpr uint32_t c2 = 0x1b873593; | ||
std::uint32_t h1 = seed_; | ||
constexpr std::uint32_t c1 = 0xcc9e2d51; | ||
constexpr std::uint32_t c2 = 0x1b873593; | ||
//---------- | ||
// body | ||
const uint32_t* const blocks = (const uint32_t*)(data + nblocks * 4); | ||
for (int i = -nblocks; i; i++) { | ||
uint32_t k1 = blocks[i]; // getblock32(blocks,i); | ||
for (std::remove_const_t<decltype(nblocks)> i = 0; size >= 4 && i < nblocks; i++) { | ||
std::uint32_t k1 = load_chunk<std::uint32_t>(data, i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PointKernel Here's the essential change I had to make to eliminate the compiler warnings. |
||
k1 *= c1; | ||
k1 = rotl32(k1, 15); | ||
k1 *= c2; | ||
|
@@ -160,33 +177,31 @@ struct MurmurHash3_32 { | |
} | ||
//---------- | ||
// tail | ||
const uint8_t* tail = (const uint8_t*)(data + nblocks * 4); | ||
uint32_t k1 = 0; | ||
switch (len & 3) { | ||
case 3: k1 ^= tail[2] << 16; | ||
case 2: k1 ^= tail[1] << 8; | ||
std::uint32_t k1 = 0; | ||
switch (size & 3) { | ||
case 3: k1 ^= data[nblocks * 4 + 2] << 16; [[fallthrough]]; | ||
case 2: k1 ^= data[nblocks * 4 + 1] << 8; [[fallthrough]]; | ||
case 1: | ||
k1 ^= tail[0]; | ||
k1 ^= data[nblocks * 4 + 0]; | ||
k1 *= c1; | ||
k1 = rotl32(k1, 15); | ||
k1 *= c2; | ||
h1 ^= k1; | ||
}; | ||
//---------- | ||
// finalization | ||
h1 ^= len; | ||
h1 ^= size; | ||
h1 = fmix32_(h1); | ||
return h1; | ||
} | ||
|
||
private: | ||
constexpr __host__ __device__ uint32_t rotl32(uint32_t x, int8_t r) const noexcept | ||
constexpr __host__ __device__ std::uint32_t rotl32(std::uint32_t x, std::int8_t r) const noexcept | ||
{ | ||
return (x << r) | (x >> (32 - r)); | ||
} | ||
|
||
MurmurHash3_fmix32<uint32_t> fmix32_; | ||
uint32_t seed_; | ||
MurmurHash3_fmix32<std::uint32_t> fmix32_; | ||
std::uint32_t seed_; | ||
}; | ||
|
||
} // namespace cuco::detail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace cuco::detail { | ||
|
||
template <typename T, typename U, typename Extent> | ||
constexpr __host__ __device__ T load_chunk(U const* const data, Extent index) noexcept | ||
{ | ||
T chunk; | ||
memcpy(&chunk, __builtin_assume_aligned(data + index * sizeof(T), sizeof(T)), sizeof(T)); | ||
return chunk; | ||
} | ||
|
||
}; // namespace cuco::detail |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline. We would provide a member like
instead of overloading
()
operator.Per
std::hash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically we still meet the standard requirements/interface. This is just another overload of the operator. The standard does not prohibit additional functionality.