Skip to content
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

Fix hash to size conversion #362

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions include/cuco/detail/probing_scheme_impl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ __host__ __device__ constexpr auto linear_probing<CGSize, Hash>::operator()(
{
using size_type = typename Extent::value_type;
return detail::probing_iterator<Extent>{
cuco::detail::sanitize_hash<size_type>(hash_(probe_key)) % upper_bound,
cuco::detail::hash_to_index<size_type>(hash_(probe_key)) % upper_bound,
1, // step size is 1
upper_bound};
}
Expand All @@ -114,7 +114,7 @@ __host__ __device__ constexpr auto linear_probing<CGSize, Hash>::operator()(
{
using size_type = typename Extent::value_type;
return detail::probing_iterator<Extent>{
cuco::detail::sanitize_hash<size_type>(hash_(probe_key) + g.thread_rank()) % upper_bound,
cuco::detail::hash_to_index<size_type>(hash_(probe_key), g.thread_rank()) % upper_bound,
cg_size,
upper_bound};
}
Expand All @@ -133,9 +133,9 @@ __host__ __device__ constexpr auto double_hashing<CGSize, Hash1, Hash2>::operato
{
using size_type = typename Extent::value_type;
return detail::probing_iterator<Extent>{
cuco::detail::sanitize_hash<size_type>(hash1_(probe_key)) % upper_bound,
cuco::detail::hash_to_index<size_type>(hash1_(probe_key)) % upper_bound,
max(size_type{1},
cuco::detail::sanitize_hash<size_type>(hash2_(probe_key)) %
cuco::detail::hash_to_index<size_type>(hash2_(probe_key)) %
upper_bound), // step size in range [1, prime - 1]
upper_bound};
}
Expand All @@ -149,8 +149,8 @@ __host__ __device__ constexpr auto double_hashing<CGSize, Hash1, Hash2>::operato
{
using size_type = typename Extent::value_type;
return detail::probing_iterator<Extent>{
cuco::detail::sanitize_hash<size_type>(hash1_(probe_key) + g.thread_rank()) % upper_bound,
static_cast<size_type>((cuco::detail::sanitize_hash<size_type>(hash2_(probe_key)) %
cuco::detail::hash_to_index<size_type>(hash1_(probe_key), g.thread_rank()) % upper_bound,
static_cast<size_type>((cuco::detail::hash_to_index<size_type>(hash2_(probe_key)) %
(upper_bound.value() / cg_size - 1) +
1) *
cg_size),
Expand Down
16 changes: 12 additions & 4 deletions include/cuco/detail/utils.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,23 @@ struct strong_type {
* @tparam SizeType The target type
* @tparam HashType The input type
*
* @param[in] hash The hash value
* @param[in] thread_rank Local thread offset during CG probing
*
* @return Converted hash value
*/
template <typename SizeType, typename HashType>
__host__ __device__ constexpr SizeType sanitize_hash(HashType hash) noexcept
__host__ __device__ constexpr SizeType hash_to_index(HashType hash,
uint32_t thread_rank = 0) noexcept
{
if constexpr (cuda::std::is_signed_v<SizeType>) {
return cuda::std::abs(static_cast<SizeType>(hash));
if constexpr (std::is_unsigned_v<SizeType>) {
constexpr auto max_size = static_cast<HashType>(cuda::std::numeric_limits<SizeType>::max());
return static_cast<SizeType>((hash + thread_rank) & max_size);
} else {
return static_cast<SizeType>(hash);
using larger_type =
cuda::std::conditional_t<sizeof(HashType) >= sizeof(SizeType), HashType, SizeType>;
constexpr auto max_size = static_cast<larger_type>(cuda::std::numeric_limits<SizeType>::max());
return static_cast<SizeType>((hash & max_size + thread_rank) & max_size);
}
}

Expand Down
Loading