cuCollections icon indicating copy to clipboard operation
cuCollections copied to clipboard

Support any hash constructor in hash_test.cu::check_hash_result

Open esoha-nvidia opened this issue 1 year ago • 1 comments

          I see why you made this: Because we have hash functions that don't take a seed.  However, we'll eventually have the `SigBitHash` that takes not a seed but rather a list or a range or whatever.

I think that it would be better to not write a million of these functions. The right way to do it is with a variadic template. Also, you could make that a different PR. The code would look like this:

template <typename Hash, typename... HashConstructorArgs>
static __host__ __device__ bool check_hash_result(typename Hash::argument_type const& key,
                                                  typename Hash::result_type expected,
                                                  HashConstructorArgs&&... hash_constructor_args) noexcept
{
  Hash h(std::forward<HashConstructorArgs>(hash_constructor_args)...);
  return (h(key) == expected);
}

The seed is now the last argument so you need to find all occurrences of check_hash_result in this file and make the seed the last argument.

After doing this change, the function will now be compatible with all hash construction.


If you want to read more about std::forward, these are good:

  • https://stackoverflow.com/questions/16447951/how-to-forward-variable-number-of-arguments-to-another-function
  • https://stackoverflow.com/questions/7257144/when-to-use-stdforward-to-forward-arguments

The short of it is that std::forward written this way with a "universal reference" makes it so that this code works well when:

  • you pass in something that has a name, like check_hash_result(my_key, my_expected, my_seed). (my_seed has a "name", it's a left-value aka l-value aka something that can be on the left side of an assignment)
  • you pass in something nameless, like check_hash_result(my_key, my_expected, 12345). (12345 has no "name", it's a right-value aka r-value aka something that can not be on the left side of an assignment statement)

When used like this, the compiler will correctly optimize if the value of the seed or whatever is passed in there needs to be copied or moved.

Originally posted by @esoha-nvidia in https://github.com/NVIDIA/cuCollections/pull/514#discussion_r1653534402

esoha-nvidia avatar Jun 25 '24 21:06 esoha-nvidia

I like the idea. We'll incorporate it into the test.

sleeepyjack avatar Jun 25 '24 22:06 sleeepyjack