type_dispatcher.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cudf/detail/utilities/assert.cuh>
21 #include <cudf/types.hpp>
22 #include <cudf/utilities/error.hpp>
26 
27 #include <string>
28 
34 namespace CUDF_EXPORT cudf {
56 template <typename T>
57 inline constexpr type_id type_to_id()
58 {
59  return type_id::EMPTY;
60 };
61 
66 struct type_to_name_impl {
72  template <typename T>
73  inline std::string operator()()
74  {
75  return "void";
76  }
77 };
78 
79 template <cudf::type_id t>
80 struct id_to_type_impl {
81  using type = void;
82 };
92 template <cudf::type_id Id>
93 using id_to_type = typename id_to_type_impl<Id>::type;
94 
109 // clang-format off
110 template <typename T>
112  std::conditional_t<std::is_same_v<numeric::decimal32, T>, int32_t,
113  std::conditional_t<std::is_same_v<numeric::decimal64, T>, int64_t,
114  std::conditional_t<std::is_same_v<numeric::decimal128, T>, __int128_t, T>>>;
115 // clang-format on
116 
126 template <typename T>
128 {
129  return (id == type_id::DECIMAL32 && std::is_same_v<T, int32_t>) ||
130  (id == type_id::DECIMAL64 && std::is_same_v<T, int64_t>) ||
131  (id == type_id::DECIMAL128 && std::is_same_v<T, __int128_t>) || id == type_to_id<T>();
132 }
133 
141 #ifndef CUDF_TYPE_MAPPING
142 #define CUDF_TYPE_MAPPING(Type, Id) \
143  template <> \
144  constexpr inline type_id type_to_id<Type>() \
145  { \
146  return Id; \
147  } \
148  template <> \
149  inline std::string type_to_name_impl::operator()<Type>() \
150  { \
151  return CUDF_STRINGIFY(Type); \
152  } \
153  template <> \
154  struct id_to_type_impl<Id> { \
155  using type = Type; \
156  };
157 #endif
158 
159 // Defines all of the mappings between C++ types and their corresponding `cudf::type_id` values.
160 CUDF_TYPE_MAPPING(int8_t, type_id::INT8)
161 CUDF_TYPE_MAPPING(int16_t, type_id::INT16)
162 CUDF_TYPE_MAPPING(int32_t, type_id::INT32)
163 CUDF_TYPE_MAPPING(int64_t, type_id::INT64)
164 CUDF_TYPE_MAPPING(uint8_t, type_id::UINT8)
165 CUDF_TYPE_MAPPING(uint16_t, type_id::UINT16)
166 CUDF_TYPE_MAPPING(uint32_t, type_id::UINT32)
167 CUDF_TYPE_MAPPING(uint64_t, type_id::UINT64)
168 CUDF_TYPE_MAPPING(float, type_id::FLOAT32)
169 CUDF_TYPE_MAPPING(double, type_id::FLOAT64)
170 CUDF_TYPE_MAPPING(bool, type_id::BOOL8)
171 CUDF_TYPE_MAPPING(cudf::timestamp_D, type_id::TIMESTAMP_DAYS)
172 CUDF_TYPE_MAPPING(cudf::timestamp_s, type_id::TIMESTAMP_SECONDS)
173 CUDF_TYPE_MAPPING(cudf::timestamp_ms, type_id::TIMESTAMP_MILLISECONDS)
174 CUDF_TYPE_MAPPING(cudf::timestamp_us, type_id::TIMESTAMP_MICROSECONDS)
175 CUDF_TYPE_MAPPING(cudf::timestamp_ns, type_id::TIMESTAMP_NANOSECONDS)
176 CUDF_TYPE_MAPPING(cudf::duration_D, type_id::DURATION_DAYS)
177 CUDF_TYPE_MAPPING(cudf::duration_s, type_id::DURATION_SECONDS)
178 CUDF_TYPE_MAPPING(cudf::duration_ms, type_id::DURATION_MILLISECONDS)
179 CUDF_TYPE_MAPPING(cudf::duration_us, type_id::DURATION_MICROSECONDS)
180 CUDF_TYPE_MAPPING(cudf::duration_ns, type_id::DURATION_NANOSECONDS)
181 CUDF_TYPE_MAPPING(cudf::dictionary32, type_id::DICTIONARY32)
182 CUDF_TYPE_MAPPING(cudf::string_view, type_id::STRING)
183 CUDF_TYPE_MAPPING(cudf::list_view, type_id::LIST)
184 CUDF_TYPE_MAPPING(numeric::decimal32, type_id::DECIMAL32)
185 CUDF_TYPE_MAPPING(numeric::decimal64, type_id::DECIMAL64)
186 CUDF_TYPE_MAPPING(numeric::decimal128, type_id::DECIMAL128)
187 CUDF_TYPE_MAPPING(cudf::struct_view, type_id::STRUCT)
188 
189 
197 template <> // CUDF_TYPE_MAPPING(char,INT8) causes duplicate id_to_type_impl definition
198 constexpr inline type_id type_to_id<char>()
199 {
200  return type_id::INT8;
201 }
202 
211 template <cudf::type_id Id>
214 };
215 
216 template <typename T>
217 struct type_to_scalar_type_impl {
218  using ScalarType = cudf::scalar;
219 };
220 
227 #ifndef MAP_NUMERIC_SCALAR
228 #define MAP_NUMERIC_SCALAR(Type) \
229  template <> \
230  struct type_to_scalar_type_impl<Type> { \
231  using ScalarType = cudf::numeric_scalar<Type>; \
232  using ScalarDeviceType = cudf::numeric_scalar_device_view<Type>; \
233  };
234 #endif
235 
236 MAP_NUMERIC_SCALAR(int8_t)
237 MAP_NUMERIC_SCALAR(int16_t)
238 MAP_NUMERIC_SCALAR(int32_t)
239 MAP_NUMERIC_SCALAR(int64_t)
240 MAP_NUMERIC_SCALAR(__int128_t)
241 MAP_NUMERIC_SCALAR(uint8_t)
242 MAP_NUMERIC_SCALAR(uint16_t)
243 MAP_NUMERIC_SCALAR(uint32_t)
244 MAP_NUMERIC_SCALAR(uint64_t)
245 MAP_NUMERIC_SCALAR(float)
246 MAP_NUMERIC_SCALAR(double)
247 MAP_NUMERIC_SCALAR(bool)
248 
249 template <>
250 struct type_to_scalar_type_impl<std::string> {
251  using ScalarType = cudf::string_scalar;
252  using ScalarDeviceType = cudf::string_scalar_device_view;
253 };
254 
255 template <>
256 struct type_to_scalar_type_impl<cudf::string_view> {
257  using ScalarType = cudf::string_scalar;
258  using ScalarDeviceType = cudf::string_scalar_device_view;
259 };
260 
261 template <>
262 struct type_to_scalar_type_impl<numeric::decimal32> {
265 };
266 
267 template <>
268 struct type_to_scalar_type_impl<numeric::decimal64> {
271 };
272 
273 template <>
274 struct type_to_scalar_type_impl<numeric::decimal128> {
277 };
278 
279 template <> // TODO: this is a temporary solution for make_pair_iterator
280 struct type_to_scalar_type_impl<cudf::dictionary32> {
281  using ScalarType = cudf::numeric_scalar<int32_t>;
282  using ScalarDeviceType = cudf::numeric_scalar_device_view<int32_t>;
283 };
284 
285 template <> // TODO: this is to get compilation working. list scalars will be implemented at a
286  // later time.
287 struct type_to_scalar_type_impl<cudf::list_view> {
288  using ScalarType = cudf::list_scalar;
289  // using ScalarDeviceType = cudf::list_scalar_device_view;
290 };
291 
292 template <> // TODO: Ditto, likewise.
293 struct type_to_scalar_type_impl<cudf::struct_view> {
294  using ScalarType = cudf::struct_scalar;
295  // using ScalarDeviceType = cudf::struct_scalar_device_view; // CALEB: TODO!
296 };
297 
304 #ifndef MAP_TIMESTAMP_SCALAR
305 #define MAP_TIMESTAMP_SCALAR(Type) \
306  template <> \
307  struct type_to_scalar_type_impl<Type> { \
308  using ScalarType = cudf::timestamp_scalar<Type>; \
309  using ScalarDeviceType = cudf::timestamp_scalar_device_view<Type>; \
310  };
311 #endif
312 
318 
319 
325 #ifndef MAP_DURATION_SCALAR
326 #define MAP_DURATION_SCALAR(Type) \
327  template <> \
328  struct type_to_scalar_type_impl<Type> { \
329  using ScalarType = cudf::duration_scalar<Type>; \
330  using ScalarDeviceType = cudf::duration_scalar_device_view<Type>; \
331  };
332 #endif
333 
339 
340 
345 template <typename T>
347 
353 template <typename T>
354 using scalar_device_type_t = typename type_to_scalar_type_impl<T>::ScalarDeviceType;
355 
448 // This pragma disables a compiler warning that complains about the valid usage
449 // of calling a __host__ functor from this function which is __host__ __device__
450 #ifdef __CUDACC__
451 #pragma nv_exec_check_disable
452 #endif
453 template <template <cudf::type_id> typename IdTypeMap = id_to_type_impl,
454  typename Functor,
455  typename... Ts>
456 CUDF_HOST_DEVICE __forceinline__ constexpr decltype(auto) type_dispatcher(cudf::data_type dtype,
457  Functor f,
458  Ts&&... args)
459 {
460  switch (dtype.id()) {
461  case type_id::INT8:
462  return f.template operator()<typename IdTypeMap<type_id::INT8>::type>(
463  std::forward<Ts>(args)...);
464  case type_id::INT16:
465  return f.template operator()<typename IdTypeMap<type_id::INT16>::type>(
466  std::forward<Ts>(args)...);
467  case type_id::INT32:
468  return f.template operator()<typename IdTypeMap<type_id::INT32>::type>(
469  std::forward<Ts>(args)...);
470  case type_id::INT64:
471  return f.template operator()<typename IdTypeMap<type_id::INT64>::type>(
472  std::forward<Ts>(args)...);
473  case type_id::UINT8:
474  return f.template operator()<typename IdTypeMap<type_id::UINT8>::type>(
475  std::forward<Ts>(args)...);
476  case type_id::UINT16:
477  return f.template operator()<typename IdTypeMap<type_id::UINT16>::type>(
478  std::forward<Ts>(args)...);
479  case type_id::UINT32:
480  return f.template operator()<typename IdTypeMap<type_id::UINT32>::type>(
481  std::forward<Ts>(args)...);
482  case type_id::UINT64:
483  return f.template operator()<typename IdTypeMap<type_id::UINT64>::type>(
484  std::forward<Ts>(args)...);
485  case type_id::FLOAT32:
486  return f.template operator()<typename IdTypeMap<type_id::FLOAT32>::type>(
487  std::forward<Ts>(args)...);
488  case type_id::FLOAT64:
489  return f.template operator()<typename IdTypeMap<type_id::FLOAT64>::type>(
490  std::forward<Ts>(args)...);
491  case type_id::BOOL8:
492  return f.template operator()<typename IdTypeMap<type_id::BOOL8>::type>(
493  std::forward<Ts>(args)...);
494  case type_id::TIMESTAMP_DAYS:
495  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_DAYS>::type>(
496  std::forward<Ts>(args)...);
497  case type_id::TIMESTAMP_SECONDS:
498  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_SECONDS>::type>(
499  std::forward<Ts>(args)...);
500  case type_id::TIMESTAMP_MILLISECONDS:
501  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_MILLISECONDS>::type>(
502  std::forward<Ts>(args)...);
503  case type_id::TIMESTAMP_MICROSECONDS:
504  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_MICROSECONDS>::type>(
505  std::forward<Ts>(args)...);
506  case type_id::TIMESTAMP_NANOSECONDS:
507  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_NANOSECONDS>::type>(
508  std::forward<Ts>(args)...);
509  case type_id::DURATION_DAYS:
510  return f.template operator()<typename IdTypeMap<type_id::DURATION_DAYS>::type>(
511  std::forward<Ts>(args)...);
512  case type_id::DURATION_SECONDS:
513  return f.template operator()<typename IdTypeMap<type_id::DURATION_SECONDS>::type>(
514  std::forward<Ts>(args)...);
515  case type_id::DURATION_MILLISECONDS:
516  return f.template operator()<typename IdTypeMap<type_id::DURATION_MILLISECONDS>::type>(
517  std::forward<Ts>(args)...);
518  case type_id::DURATION_MICROSECONDS:
519  return f.template operator()<typename IdTypeMap<type_id::DURATION_MICROSECONDS>::type>(
520  std::forward<Ts>(args)...);
521  case type_id::DURATION_NANOSECONDS:
522  return f.template operator()<typename IdTypeMap<type_id::DURATION_NANOSECONDS>::type>(
523  std::forward<Ts>(args)...);
524  case type_id::DICTIONARY32:
525  return f.template operator()<typename IdTypeMap<type_id::DICTIONARY32>::type>(
526  std::forward<Ts>(args)...);
527  case type_id::STRING:
528  return f.template operator()<typename IdTypeMap<type_id::STRING>::type>(
529  std::forward<Ts>(args)...);
530  case type_id::LIST:
531  return f.template operator()<typename IdTypeMap<type_id::LIST>::type>(
532  std::forward<Ts>(args)...);
533  case type_id::DECIMAL32:
534  return f.template operator()<typename IdTypeMap<type_id::DECIMAL32>::type>(
535  std::forward<Ts>(args)...);
536  case type_id::DECIMAL64:
537  return f.template operator()<typename IdTypeMap<type_id::DECIMAL64>::type>(
538  std::forward<Ts>(args)...);
539  case type_id::DECIMAL128:
540  return f.template operator()<typename IdTypeMap<type_id::DECIMAL128>::type>(
541  std::forward<Ts>(args)...);
542  case type_id::STRUCT:
543  return f.template operator()<typename IdTypeMap<type_id::STRUCT>::type>(
544  std::forward<Ts>(args)...);
545  default: {
546 #ifndef __CUDA_ARCH__
547  CUDF_FAIL("Invalid type_id.");
548 #else
549  CUDF_UNREACHABLE("Invalid type_id.");
550 #endif
551  }
552  }
553 }
554 
555 // @cond
556 namespace detail {
557 template <typename T1>
558 struct double_type_dispatcher_second_type {
559 #ifdef __CUDACC__
560 #pragma nv_exec_check_disable
561 #endif
562  template <typename T2, typename F, typename... Ts>
563  CUDF_HOST_DEVICE __forceinline__ decltype(auto) operator()(F&& f, Ts&&... args) const
564  {
565  return f.template operator()<T1, T2>(std::forward<Ts>(args)...);
566  }
567 };
568 
569 template <template <cudf::type_id> typename IdTypeMap>
570 struct double_type_dispatcher_first_type {
571 #ifdef __CUDACC__
572 #pragma nv_exec_check_disable
573 #endif
574  template <typename T1, typename F, typename... Ts>
575  CUDF_HOST_DEVICE __forceinline__ decltype(auto) operator()(cudf::data_type type2,
576  F&& f,
577  Ts&&... args) const
578  {
579  return type_dispatcher<IdTypeMap>(type2,
580  detail::double_type_dispatcher_second_type<T1>{},
581  std::forward<F>(f),
582  std::forward<Ts>(args)...);
583  }
584 };
585 } // namespace detail
586 // @endcond
587 
603 #ifdef __CUDACC__
604 #pragma nv_exec_check_disable
605 #endif
606 template <template <cudf::type_id> typename IdTypeMap = id_to_type_impl, typename F, typename... Ts>
607 CUDF_HOST_DEVICE __forceinline__ constexpr decltype(auto) double_type_dispatcher(
608  cudf::data_type type1, cudf::data_type type2, F&& f, Ts&&... args)
609 {
610  return type_dispatcher<IdTypeMap>(type1,
611  detail::double_type_dispatcher_first_type<IdTypeMap>{},
612  type2,
613  std::forward<F>(f),
614  std::forward<Ts>(args)...);
615 }
616 
626 std::string type_to_name(data_type type);
627  // end of group
629 } // namespace CUDF_EXPORT cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
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:303
An owning class to represent a list value in device memory.
Definition: scalar.hpp:730
A non-owning, immutable view of device data that represents a list of elements of arbitrary type (inc...
Definition: list_view.hpp:32
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:243
An owning class to represent a singular value.
Definition: scalar.hpp:48
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:430
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:44
An owning class to represent a struct value in device memory.
Definition: scalar.hpp:796
A non-owning, immutable view of device data that represents a struct with fields of arbitrary types (...
Definition: struct_view.hpp:30
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.
Class definition for fixed point data type.
dictionary_wrapper< int32_t > dictionary32
32-bit integer indexed dictionary wrapper
Definition: dictionary.hpp:217
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:67
detail::timestamp< cudf::duration_s > timestamp_s
Type alias representing a cudf::duration_s (int64_t) since the unix epoch.
Definition: timestamps.hpp:63
detail::timestamp< cudf::duration_D > timestamp_D
Type alias representing a cudf::duration_D (int32_t) since the unix epoch.
Definition: timestamps.hpp:51
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:59
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:35
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:51
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:55
detail::timestamp< cudf::duration_us > timestamp_us
Type alias representing a cudf::duration_us (int64_t) since the unix epoch.
Definition: timestamps.hpp:71
detail::timestamp< cudf::duration_ns > timestamp_ns
Type alias representing a cudf::duration_ns (int64_t) since the unix epoch.
Definition: timestamps.hpp:75
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:47
#define MAP_TIMESTAMP_SCALAR(Type)
Macro used to define scalar type and scalar device type for cudf::timestamp_scalar template class for...
constexpr type_id type_to_id< char >()
Specialization to map 'char' type to type_id::INT8.
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.
#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:217
type_id
Identifies a column's logical element type.
Definition: types.hpp:203
cuDF interfaces
Definition: aggregation.hpp:35
fixed_point and supporting types
Definition: fixed_point.hpp:33
A strongly typed wrapper for indices in a DICTIONARY type column.
Definition: dictionary.hpp:49
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:32