type_dispatcher.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
9 #include <cudf/types.hpp>
10 #include <cudf/utilities/error.hpp>
14 
15 #include <string>
16 
22 namespace CUDF_EXPORT cudf {
43 template <typename T>
45 {
46  return type_id::EMPTY;
47 };
48 
53 struct type_to_name_impl {
59  template <typename T>
60  inline std::string operator()()
61  {
62  return "void";
63  }
64 };
65 
66 template <cudf::type_id t>
67 struct id_to_type_impl {
68  using type = void;
69 };
79 template <cudf::type_id Id>
80 using id_to_type = typename id_to_type_impl<Id>::type;
81 
96 // clang-format off
97 template <typename T>
99  std::conditional_t<std::is_same_v<numeric::decimal32, T>, int32_t,
100  std::conditional_t<std::is_same_v<numeric::decimal64, T>, int64_t,
101  std::conditional_t<std::is_same_v<numeric::decimal128, T>, __int128_t, T>>>;
102 // clang-format on
103 
119 template <typename T>
120 constexpr inline type_id type_to_id()
121 {
122  return base_type_to_id<std::remove_cv_t<T>>();
123 }
124 
132 #ifndef CUDF_TYPE_MAPPING
133 #define CUDF_TYPE_MAPPING(Type, Id) \
134  template <> \
135  constexpr inline type_id base_type_to_id<Type>() \
136  { \
137  return Id; \
138  } \
139  template <> \
140  inline std::string type_to_name_impl::operator()<Type>() \
141  { \
142  return CUDF_STRINGIFY(Type); \
143  } \
144  template <> \
145  struct id_to_type_impl<Id> { \
146  using type = Type; \
147  };
148 #endif
149 
150 // Defines all of the mappings between C++ types and their corresponding `cudf::type_id` values.
151 CUDF_TYPE_MAPPING(int8_t, type_id::INT8)
152 CUDF_TYPE_MAPPING(int16_t, type_id::INT16)
153 CUDF_TYPE_MAPPING(int32_t, type_id::INT32)
154 CUDF_TYPE_MAPPING(int64_t, type_id::INT64)
155 CUDF_TYPE_MAPPING(uint8_t, type_id::UINT8)
156 CUDF_TYPE_MAPPING(uint16_t, type_id::UINT16)
157 CUDF_TYPE_MAPPING(uint32_t, type_id::UINT32)
158 CUDF_TYPE_MAPPING(uint64_t, type_id::UINT64)
159 CUDF_TYPE_MAPPING(float, type_id::FLOAT32)
160 CUDF_TYPE_MAPPING(double, type_id::FLOAT64)
161 CUDF_TYPE_MAPPING(bool, type_id::BOOL8)
162 CUDF_TYPE_MAPPING(cudf::timestamp_D, type_id::TIMESTAMP_DAYS)
163 CUDF_TYPE_MAPPING(cudf::timestamp_s, type_id::TIMESTAMP_SECONDS)
164 CUDF_TYPE_MAPPING(cudf::timestamp_ms, type_id::TIMESTAMP_MILLISECONDS)
165 CUDF_TYPE_MAPPING(cudf::timestamp_us, type_id::TIMESTAMP_MICROSECONDS)
166 CUDF_TYPE_MAPPING(cudf::timestamp_ns, type_id::TIMESTAMP_NANOSECONDS)
167 CUDF_TYPE_MAPPING(cudf::duration_D, type_id::DURATION_DAYS)
168 CUDF_TYPE_MAPPING(cudf::duration_s, type_id::DURATION_SECONDS)
169 CUDF_TYPE_MAPPING(cudf::duration_ms, type_id::DURATION_MILLISECONDS)
170 CUDF_TYPE_MAPPING(cudf::duration_us, type_id::DURATION_MICROSECONDS)
171 CUDF_TYPE_MAPPING(cudf::duration_ns, type_id::DURATION_NANOSECONDS)
172 CUDF_TYPE_MAPPING(cudf::dictionary32, type_id::DICTIONARY32)
173 CUDF_TYPE_MAPPING(cudf::string_view, type_id::STRING)
174 CUDF_TYPE_MAPPING(cudf::list_view, type_id::LIST)
175 CUDF_TYPE_MAPPING(numeric::decimal32, type_id::DECIMAL32)
176 CUDF_TYPE_MAPPING(numeric::decimal64, type_id::DECIMAL64)
177 CUDF_TYPE_MAPPING(numeric::decimal128, type_id::DECIMAL128)
178 CUDF_TYPE_MAPPING(cudf::struct_view, type_id::STRUCT)
179 
180 
188 template <> // CUDF_TYPE_MAPPING(char,INT8) causes duplicate id_to_type_impl definition
189 constexpr inline type_id base_type_to_id<char>()
190 {
191  return type_id::INT8;
192 }
193 
203 template <typename T>
205 {
206  return (id == type_id::DECIMAL32 && std::is_same_v<T, int32_t>) ||
207  (id == type_id::DECIMAL64 && std::is_same_v<T, int64_t>) ||
208  (id == type_id::DECIMAL128 && std::is_same_v<T, __int128_t>) || id == type_to_id<T>();
209 }
210 
219 template <cudf::type_id Id>
222 };
223 
224 template <typename T>
225 struct type_to_scalar_type_impl {
226  using ScalarType = cudf::scalar;
227 };
228 
235 #ifndef MAP_NUMERIC_SCALAR
236 #define MAP_NUMERIC_SCALAR(Type) \
237  template <> \
238  struct type_to_scalar_type_impl<Type> { \
239  using ScalarType = cudf::numeric_scalar<Type>; \
240  using ScalarDeviceType = cudf::numeric_scalar_device_view<Type>; \
241  };
242 #endif
243 
244 MAP_NUMERIC_SCALAR(int8_t)
245 MAP_NUMERIC_SCALAR(int16_t)
246 MAP_NUMERIC_SCALAR(int32_t)
247 MAP_NUMERIC_SCALAR(int64_t)
248 MAP_NUMERIC_SCALAR(__int128_t)
249 MAP_NUMERIC_SCALAR(uint8_t)
250 MAP_NUMERIC_SCALAR(uint16_t)
251 MAP_NUMERIC_SCALAR(uint32_t)
252 MAP_NUMERIC_SCALAR(uint64_t)
253 MAP_NUMERIC_SCALAR(float)
254 MAP_NUMERIC_SCALAR(double)
255 MAP_NUMERIC_SCALAR(bool)
256 
257 template <>
258 struct type_to_scalar_type_impl<std::string> {
259  using ScalarType = cudf::string_scalar;
260  using ScalarDeviceType = cudf::string_scalar_device_view;
261 };
262 
263 template <>
264 struct type_to_scalar_type_impl<cudf::string_view> {
265  using ScalarType = cudf::string_scalar;
266  using ScalarDeviceType = cudf::string_scalar_device_view;
267 };
268 
269 template <>
270 struct type_to_scalar_type_impl<numeric::decimal32> {
273 };
274 
275 template <>
276 struct type_to_scalar_type_impl<numeric::decimal64> {
279 };
280 
281 template <>
282 struct type_to_scalar_type_impl<numeric::decimal128> {
285 };
286 
287 template <> // TODO: this is a temporary solution for make_pair_iterator
288 struct type_to_scalar_type_impl<cudf::dictionary32> {
289  using ScalarType = cudf::numeric_scalar<int32_t>;
290  using ScalarDeviceType = cudf::numeric_scalar_device_view<int32_t>;
291 };
292 
293 template <> // TODO: this is to get compilation working. list scalars will be implemented at a
294  // later time.
295 struct type_to_scalar_type_impl<cudf::list_view> {
296  using ScalarType = cudf::list_scalar;
297  // using ScalarDeviceType = cudf::list_scalar_device_view;
298 };
299 
300 template <> // TODO: Ditto, likewise.
301 struct type_to_scalar_type_impl<cudf::struct_view> {
302  using ScalarType = cudf::struct_scalar;
303  // using ScalarDeviceType = cudf::struct_scalar_device_view; // CALEB: TODO!
304 };
305 
312 #ifndef MAP_TIMESTAMP_SCALAR
313 #define MAP_TIMESTAMP_SCALAR(Type) \
314  template <> \
315  struct type_to_scalar_type_impl<Type> { \
316  using ScalarType = cudf::timestamp_scalar<Type>; \
317  using ScalarDeviceType = cudf::timestamp_scalar_device_view<Type>; \
318  };
319 #endif
320 
326 
327 
333 #ifndef MAP_DURATION_SCALAR
334 #define MAP_DURATION_SCALAR(Type) \
335  template <> \
336  struct type_to_scalar_type_impl<Type> { \
337  using ScalarType = cudf::duration_scalar<Type>; \
338  using ScalarDeviceType = cudf::duration_scalar_device_view<Type>; \
339  };
340 #endif
341 
347 
348 
353 template <typename T>
355 
361 template <typename T>
362 using scalar_device_type_t = typename type_to_scalar_type_impl<T>::ScalarDeviceType;
363 
456 // This pragma disables a compiler warning that complains about the valid usage
457 // of calling a __host__ functor from this function which is __host__ __device__
458 #ifdef __CUDACC__
459 #pragma nv_exec_check_disable
460 #endif
461 template <template <cudf::type_id> typename IdTypeMap = id_to_type_impl,
462  typename Functor,
463  typename... Ts>
464 CUDF_HOST_DEVICE __forceinline__ constexpr decltype(auto) type_dispatcher(cudf::data_type dtype,
465  Functor f,
466  Ts&&... args)
467 {
468  switch (dtype.id()) {
469  case type_id::INT8:
470  return f.template operator()<typename IdTypeMap<type_id::INT8>::type>(
471  std::forward<Ts>(args)...);
472  case type_id::INT16:
473  return f.template operator()<typename IdTypeMap<type_id::INT16>::type>(
474  std::forward<Ts>(args)...);
475  case type_id::INT32:
476  return f.template operator()<typename IdTypeMap<type_id::INT32>::type>(
477  std::forward<Ts>(args)...);
478  case type_id::INT64:
479  return f.template operator()<typename IdTypeMap<type_id::INT64>::type>(
480  std::forward<Ts>(args)...);
481  case type_id::UINT8:
482  return f.template operator()<typename IdTypeMap<type_id::UINT8>::type>(
483  std::forward<Ts>(args)...);
484  case type_id::UINT16:
485  return f.template operator()<typename IdTypeMap<type_id::UINT16>::type>(
486  std::forward<Ts>(args)...);
487  case type_id::UINT32:
488  return f.template operator()<typename IdTypeMap<type_id::UINT32>::type>(
489  std::forward<Ts>(args)...);
490  case type_id::UINT64:
491  return f.template operator()<typename IdTypeMap<type_id::UINT64>::type>(
492  std::forward<Ts>(args)...);
493  case type_id::FLOAT32:
494  return f.template operator()<typename IdTypeMap<type_id::FLOAT32>::type>(
495  std::forward<Ts>(args)...);
496  case type_id::FLOAT64:
497  return f.template operator()<typename IdTypeMap<type_id::FLOAT64>::type>(
498  std::forward<Ts>(args)...);
499  case type_id::BOOL8:
500  return f.template operator()<typename IdTypeMap<type_id::BOOL8>::type>(
501  std::forward<Ts>(args)...);
502  case type_id::TIMESTAMP_DAYS:
503  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_DAYS>::type>(
504  std::forward<Ts>(args)...);
505  case type_id::TIMESTAMP_SECONDS:
506  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_SECONDS>::type>(
507  std::forward<Ts>(args)...);
508  case type_id::TIMESTAMP_MILLISECONDS:
509  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_MILLISECONDS>::type>(
510  std::forward<Ts>(args)...);
511  case type_id::TIMESTAMP_MICROSECONDS:
512  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_MICROSECONDS>::type>(
513  std::forward<Ts>(args)...);
514  case type_id::TIMESTAMP_NANOSECONDS:
515  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_NANOSECONDS>::type>(
516  std::forward<Ts>(args)...);
517  case type_id::DURATION_DAYS:
518  return f.template operator()<typename IdTypeMap<type_id::DURATION_DAYS>::type>(
519  std::forward<Ts>(args)...);
520  case type_id::DURATION_SECONDS:
521  return f.template operator()<typename IdTypeMap<type_id::DURATION_SECONDS>::type>(
522  std::forward<Ts>(args)...);
523  case type_id::DURATION_MILLISECONDS:
524  return f.template operator()<typename IdTypeMap<type_id::DURATION_MILLISECONDS>::type>(
525  std::forward<Ts>(args)...);
526  case type_id::DURATION_MICROSECONDS:
527  return f.template operator()<typename IdTypeMap<type_id::DURATION_MICROSECONDS>::type>(
528  std::forward<Ts>(args)...);
529  case type_id::DURATION_NANOSECONDS:
530  return f.template operator()<typename IdTypeMap<type_id::DURATION_NANOSECONDS>::type>(
531  std::forward<Ts>(args)...);
532  case type_id::DICTIONARY32:
533  return f.template operator()<typename IdTypeMap<type_id::DICTIONARY32>::type>(
534  std::forward<Ts>(args)...);
535  case type_id::STRING:
536  return f.template operator()<typename IdTypeMap<type_id::STRING>::type>(
537  std::forward<Ts>(args)...);
538  case type_id::LIST:
539  return f.template operator()<typename IdTypeMap<type_id::LIST>::type>(
540  std::forward<Ts>(args)...);
541  case type_id::DECIMAL32:
542  return f.template operator()<typename IdTypeMap<type_id::DECIMAL32>::type>(
543  std::forward<Ts>(args)...);
544  case type_id::DECIMAL64:
545  return f.template operator()<typename IdTypeMap<type_id::DECIMAL64>::type>(
546  std::forward<Ts>(args)...);
547  case type_id::DECIMAL128:
548  return f.template operator()<typename IdTypeMap<type_id::DECIMAL128>::type>(
549  std::forward<Ts>(args)...);
550  case type_id::STRUCT:
551  return f.template operator()<typename IdTypeMap<type_id::STRUCT>::type>(
552  std::forward<Ts>(args)...);
553  default: {
554 #ifndef __CUDA_ARCH__
555  CUDF_FAIL("Invalid type_id.");
556 #else
557  CUDF_UNREACHABLE("Invalid type_id.");
558 #endif
559  }
560  }
561 }
562 
563 // @cond
564 namespace detail {
565 template <typename T1>
566 struct double_type_dispatcher_second_type {
567 #ifdef __CUDACC__
568 #pragma nv_exec_check_disable
569 #endif
570  template <typename T2, typename F, typename... Ts>
571  CUDF_HOST_DEVICE __forceinline__ decltype(auto) operator()(F&& f, Ts&&... args) const
572  {
573  return f.template operator()<T1, T2>(std::forward<Ts>(args)...);
574  }
575 };
576 
577 template <template <cudf::type_id> typename IdTypeMap>
578 struct double_type_dispatcher_first_type {
579 #ifdef __CUDACC__
580 #pragma nv_exec_check_disable
581 #endif
582  template <typename T1, typename F, typename... Ts>
583  CUDF_HOST_DEVICE __forceinline__ decltype(auto) operator()(cudf::data_type type2,
584  F&& f,
585  Ts&&... args) const
586  {
587  return type_dispatcher<IdTypeMap>(type2,
588  detail::double_type_dispatcher_second_type<T1>{},
589  std::forward<F>(f),
590  std::forward<Ts>(args)...);
591  }
592 };
593 } // namespace detail
594 // @endcond
595 
611 #ifdef __CUDACC__
612 #pragma nv_exec_check_disable
613 #endif
614 template <template <cudf::type_id> typename IdTypeMap = id_to_type_impl, typename F, typename... Ts>
615 CUDF_HOST_DEVICE __forceinline__ constexpr decltype(auto) double_type_dispatcher(
616  cudf::data_type type1, cudf::data_type type2, F&& f, Ts&&... args)
617 {
618  return type_dispatcher<IdTypeMap>(type1,
619  detail::double_type_dispatcher_first_type<IdTypeMap>{},
620  type2,
621  std::forward<F>(f),
622  std::forward<Ts>(args)...);
623 }
624 
634 std::string type_to_name(data_type type);
635  // end of group
637 } // namespace CUDF_EXPORT cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:286
A type of scalar_device_view that stores a pointer to a fixed_point value.
An owning class to represent a fixed_point number in device memory.
Definition: scalar.hpp:289
An owning class to represent a list value in device memory.
Definition: scalar.hpp:706
A non-owning, immutable view of device data that represents a list of elements of arbitrary type (inc...
Definition: list_view.hpp:20
A type of scalar_device_view that stores a pointer to a numerical value.
An owning class to represent a numerical value in device memory.
Definition: scalar.hpp:229
An owning class to represent a singular value.
Definition: scalar.hpp:41
A type of scalar_device_view that stores a pointer to a string value.
An owning class to represent a string in device memory.
Definition: scalar.hpp:411
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:35
An owning class to represent a struct value in device memory.
Definition: scalar.hpp:772
A non-owning, immutable view of device data that represents a struct with fields of arbitrary types (...
Definition: struct_view.hpp:19
A type for representing a number with a fixed amount of precision.
Concrete type definition for dictionary columns.
Concrete type definitions for int32_t and int64_t durations in varying resolutions.
Exception types and error-checking macros used throughout libcudf.
Class definition for fixed point data type.
dictionary_wrapper< int32_t > dictionary32
32-bit integer indexed dictionary wrapper
Definition: dictionary.hpp:207
fixed_point< int32_t, Radix::BASE_10 > decimal32
32-bit decimal fixed point
fixed_point< int64_t, Radix::BASE_10 > decimal64
64-bit decimal fixed point
fixed_point< __int128_t, Radix::BASE_10 > decimal128
128-bit decimal fixed point
detail::timestamp< cudf::duration_ms > timestamp_ms
Type alias representing a cudf::duration_ms (int64_t) since the unix epoch.
Definition: timestamps.hpp:55
detail::timestamp< cudf::duration_s > timestamp_s
Type alias representing a cudf::duration_s (int64_t) since the unix epoch.
Definition: timestamps.hpp:51
detail::timestamp< cudf::duration_D > timestamp_D
Type alias representing a cudf::duration_D (int32_t) since the unix epoch.
Definition: timestamps.hpp:39
cuda::std::chrono::duration< int64_t, cuda::std::chrono::nanoseconds::period > duration_ns
Type alias representing an int64_t duration of nanoseconds.
Definition: durations.hpp:51
cuda::std::chrono::duration< int32_t, cuda::std::chrono::days::period > duration_D
Type alias representing an int32_t duration of days.
Definition: durations.hpp:27
cuda::std::chrono::duration< int64_t, cuda::std::chrono::milliseconds::period > duration_ms
Type alias representing an int64_t duration of milliseconds.
Definition: durations.hpp:43
cuda::std::chrono::duration< int64_t, cuda::std::chrono::microseconds::period > duration_us
Type alias representing an int64_t duration of microseconds.
Definition: durations.hpp:47
detail::timestamp< cudf::duration_us > timestamp_us
Type alias representing a cudf::duration_us (int64_t) since the unix epoch.
Definition: timestamps.hpp:59
detail::timestamp< cudf::duration_ns > timestamp_ns
Type alias representing a cudf::duration_ns (int64_t) since the unix epoch.
Definition: timestamps.hpp:63
cuda::std::chrono::duration< int64_t, cuda::std::chrono::seconds::period > duration_s
Type alias representing an int64_t duration of seconds.
Definition: durations.hpp:39
#define MAP_TIMESTAMP_SCALAR(Type)
Macro used to define scalar type and scalar device type for cudf::timestamp_scalar template class for...
constexpr CUDF_HOST_DEVICE type_id base_type_to_id()
Maps a C++ type to its corresponding cudf::type_id
std::string type_to_name(data_type type)
Return a name for a given type.
#define MAP_DURATION_SCALAR(Type)
Macro used to define scalar type and scalar device type for cudf::duration_scalar template class for ...
CUDF_HOST_DEVICE constexpr decltype(auto) __forceinline__ type_dispatcher(cudf::data_type dtype, Functor f, Ts &&... args)
Invokes an operator() template with the type instantiation based on the specified cudf::data_type's i...
std::conditional_t< std::is_same_v< numeric::decimal32, T >, int32_t, std::conditional_t< std::is_same_v< numeric::decimal64, T >, int64_t, std::conditional_t< std::is_same_v< numeric::decimal128, T >, __int128_t, T > >> device_storage_type_t
"Returns" the corresponding type that is stored on the device when using cudf::column
typename type_to_scalar_type_impl< T >::ScalarDeviceType scalar_device_type_t
Maps a C++ type to the scalar device type required to hold its value.
constexpr type_id base_type_to_id< char >()
Specialization to map 'char' type to type_id::INT8.
#define CUDF_TYPE_MAPPING(Type, Id)
Macro used to define a mapping between a concrete C++ type and a cudf::type_id enum.
typename type_to_scalar_type_impl< T >::ScalarType scalar_type_t
Maps a C++ type to the scalar type required to hold its value.
constexpr type_id type_to_id()
Maps a C++ type to its corresponding cudf::type_id
CUDF_HOST_DEVICE constexpr decltype(auto) __forceinline__ double_type_dispatcher(cudf::data_type type1, cudf::data_type type2, F &&f, Ts &&... args)
Dispatches two type template parameters to a callable.
typename id_to_type_impl< Id >::type id_to_type
Maps a cudf::type_id to its corresponding concrete C++ type.
constexpr bool type_id_matches_device_storage_type(type_id id)
Checks if fixed_point-like types have template type T matching the column's stored type id.
#define MAP_NUMERIC_SCALAR(Type)
Macro used to define scalar type and scalar device type for cudf::numeric_scalar template class for n...
#define CUDF_FAIL(...)
Indicates that an erroneous code path has been taken.
Definition: error.hpp:223
type_id
Identifies a column's logical element type.
Definition: types.hpp:192
cuDF interfaces
Definition: host_udf.hpp:26
fixed_point and supporting types
Definition: fixed_point.hpp:30
A strongly typed wrapper for indices in a DICTIONARY type column.
Definition: dictionary.hpp:39
Use this specialization on type_dispatcher whenever you only need to operate on the underlying stored...
device_storage_type_t< id_to_type< Id > > type
The underlying type.
Concrete type definitions for int32_t and int64_t timestamps in varying resolutions as durations sinc...
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:21