cub.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #pragma once
  2. #include <cstdint>
  3. #include <c10/core/ScalarType.h>
  4. #include <ATen/cuda/CUDAConfig.h>
  5. // NOTE: These templates are intentionally not defined in this header,
  6. // which aviods re-compiling them for each translation unit. If you get
  7. // a link error, you need to add an explicit instantiation for your
  8. // types in cub.cu
  9. namespace at {
  10. namespace cuda {
  11. namespace cub {
  12. inline int get_num_bits(uint64_t max_key) {
  13. int num_bits = 1;
  14. while (max_key > 1) {
  15. max_key >>= 1;
  16. num_bits++;
  17. }
  18. return num_bits;
  19. }
  20. namespace detail {
  21. // radix_sort_pairs doesn't interact with value_t other than to copy
  22. // the data, so we can save template instantiations by reinterpreting
  23. // it as an opaque type.
  24. template <int N> struct alignas(N) OpaqueType { char data[N]; };
  25. template<typename key_t, int value_size>
  26. void radix_sort_pairs_impl(
  27. const key_t *keys_in, key_t *keys_out,
  28. const OpaqueType<value_size> *values_in, OpaqueType<value_size> *values_out,
  29. int64_t n, bool descending, int64_t begin_bit, int64_t end_bit);
  30. } // namespace detail
  31. template<typename key_t, typename value_t>
  32. void radix_sort_pairs(
  33. const key_t *keys_in, key_t *keys_out,
  34. const value_t *values_in, value_t *values_out,
  35. int64_t n, bool descending=false, int64_t begin_bit=0, int64_t end_bit=sizeof(key_t)*8) {
  36. static_assert(std::is_trivially_copyable<value_t>::value ||
  37. AT_ROCM_ENABLED(), // ROCm incorrectly fails this check for vector types
  38. "radix_sort_pairs value type must be trivially copyable");
  39. // Make value type opaque, so all inputs of a certain size use the same template instantiation
  40. using opaque_t = detail::OpaqueType<sizeof(value_t)>;
  41. static_assert(sizeof(value_t) <= 8 && (sizeof(value_t) & (sizeof(value_t) - 1)) == 0,
  42. "This size of value_t is not instantiated. Please instantiate it in cub.cu"
  43. " and modify this check.");
  44. static_assert(sizeof(value_t) == alignof(value_t), "Expected value_t to be size-aligned");
  45. detail::radix_sort_pairs_impl(
  46. keys_in, keys_out,
  47. reinterpret_cast<const opaque_t*>(values_in),
  48. reinterpret_cast<opaque_t*>(values_out),
  49. n, descending, begin_bit, end_bit);
  50. }
  51. template<typename key_t>
  52. void radix_sort_keys(
  53. const key_t *keys_in, key_t *keys_out,
  54. int64_t n, bool descending=false, int64_t begin_bit=0, int64_t end_bit=sizeof(key_t)*8);
  55. // NOTE: Intermediate sums will be truncated to input_t precision
  56. template <typename input_t, typename output_t>
  57. void inclusive_sum_truncating(const input_t *input, output_t *output, int64_t n);
  58. template <typename scalar_t>
  59. void inclusive_sum(const scalar_t *input, scalar_t *output, int64_t n) {
  60. return inclusive_sum_truncating(input, output, n);
  61. }
  62. // NOTE: Sums are done is common_type<input_t, output_t>
  63. template <typename input_t, typename output_t>
  64. void exclusive_sum_in_common_type(const input_t *input, output_t *output, int64_t n);
  65. template <typename scalar_t>
  66. void exclusive_sum(const scalar_t *input, scalar_t *output, int64_t n) {
  67. return exclusive_sum_in_common_type(input, output, n);
  68. }
  69. void mask_exclusive_sum(const uint8_t *mask, int64_t *output_idx, int64_t n);
  70. inline void mask_exclusive_sum(const bool *mask, int64_t *output_idx, int64_t n) {
  71. return mask_exclusive_sum(
  72. reinterpret_cast<const uint8_t*>(mask), output_idx, n);
  73. }
  74. }}} // namespace at::cuda::cub