libcudf  23.12.00
type_dispatcher.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2023, 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 {
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.
188 
189 
197 template <cudf::type_id Id>
200 };
201 
202 template <typename T>
203 struct type_to_scalar_type_impl {
204  using ScalarType = cudf::scalar;
205 };
206 
213 #ifndef MAP_NUMERIC_SCALAR
214 #define MAP_NUMERIC_SCALAR(Type) \
215  template <> \
216  struct type_to_scalar_type_impl<Type> { \
217  using ScalarType = cudf::numeric_scalar<Type>; \
218  using ScalarDeviceType = cudf::numeric_scalar_device_view<Type>; \
219  };
220 #endif
221 
222 MAP_NUMERIC_SCALAR(int8_t)
223 MAP_NUMERIC_SCALAR(int16_t)
224 MAP_NUMERIC_SCALAR(int32_t)
225 MAP_NUMERIC_SCALAR(int64_t)
226 MAP_NUMERIC_SCALAR(__int128_t)
227 MAP_NUMERIC_SCALAR(uint8_t)
228 MAP_NUMERIC_SCALAR(uint16_t)
229 MAP_NUMERIC_SCALAR(uint32_t)
230 MAP_NUMERIC_SCALAR(uint64_t)
231 MAP_NUMERIC_SCALAR(float)
232 MAP_NUMERIC_SCALAR(double)
233 MAP_NUMERIC_SCALAR(bool)
234 
235 template <>
236 struct type_to_scalar_type_impl<std::string> {
237  using ScalarType = cudf::string_scalar;
238  using ScalarDeviceType = cudf::string_scalar_device_view;
239 };
240 
241 template <>
242 struct type_to_scalar_type_impl<cudf::string_view> {
243  using ScalarType = cudf::string_scalar;
244  using ScalarDeviceType = cudf::string_scalar_device_view;
245 };
246 
247 template <>
248 struct type_to_scalar_type_impl<numeric::decimal32> {
251 };
252 
253 template <>
254 struct type_to_scalar_type_impl<numeric::decimal64> {
257 };
258 
259 template <>
260 struct type_to_scalar_type_impl<numeric::decimal128> {
263 };
264 
265 template <> // TODO: this is a temporary solution for make_pair_iterator
266 struct type_to_scalar_type_impl<cudf::dictionary32> {
267  using ScalarType = cudf::numeric_scalar<int32_t>;
268  using ScalarDeviceType = cudf::numeric_scalar_device_view<int32_t>;
269 };
270 
271 template <> // TODO: this is to get compilation working. list scalars will be implemented at a
272  // later time.
273 struct type_to_scalar_type_impl<cudf::list_view> {
274  using ScalarType = cudf::list_scalar;
275  // using ScalarDeviceType = cudf::list_scalar_device_view;
276 };
277 
278 template <> // TODO: Ditto, likewise.
279 struct type_to_scalar_type_impl<cudf::struct_view> {
280  using ScalarType = cudf::struct_scalar;
281  // using ScalarDeviceType = cudf::struct_scalar_device_view; // CALEB: TODO!
282 };
283 
290 #ifndef MAP_TIMESTAMP_SCALAR
291 #define MAP_TIMESTAMP_SCALAR(Type) \
292  template <> \
293  struct type_to_scalar_type_impl<Type> { \
294  using ScalarType = cudf::timestamp_scalar<Type>; \
295  using ScalarDeviceType = cudf::timestamp_scalar_device_view<Type>; \
296  };
297 #endif
298 
304 
305 
311 #ifndef MAP_DURATION_SCALAR
312 #define MAP_DURATION_SCALAR(Type) \
313  template <> \
314  struct type_to_scalar_type_impl<Type> { \
315  using ScalarType = cudf::duration_scalar<Type>; \
316  using ScalarDeviceType = cudf::duration_scalar_device_view<Type>; \
317  };
318 #endif
319 
325 
326 
331 template <typename T>
333 
339 template <typename T>
340 using scalar_device_type_t = typename type_to_scalar_type_impl<T>::ScalarDeviceType;
341 
434 // This pragma disables a compiler warning that complains about the valid usage
435 // of calling a __host__ functor from this function which is __host__ __device__
436 #ifdef __CUDACC__
437 #pragma nv_exec_check_disable
438 #endif
439 template <template <cudf::type_id> typename IdTypeMap = id_to_type_impl,
440  typename Functor,
441  typename... Ts>
442 CUDF_HOST_DEVICE __forceinline__ constexpr decltype(auto) type_dispatcher(cudf::data_type dtype,
443  Functor f,
444  Ts&&... args)
445 {
446  switch (dtype.id()) {
447  case type_id::INT8:
448  return f.template operator()<typename IdTypeMap<type_id::INT8>::type>(
449  std::forward<Ts>(args)...);
450  case type_id::INT16:
451  return f.template operator()<typename IdTypeMap<type_id::INT16>::type>(
452  std::forward<Ts>(args)...);
453  case type_id::INT32:
454  return f.template operator()<typename IdTypeMap<type_id::INT32>::type>(
455  std::forward<Ts>(args)...);
456  case type_id::INT64:
457  return f.template operator()<typename IdTypeMap<type_id::INT64>::type>(
458  std::forward<Ts>(args)...);
459  case type_id::UINT8:
460  return f.template operator()<typename IdTypeMap<type_id::UINT8>::type>(
461  std::forward<Ts>(args)...);
462  case type_id::UINT16:
463  return f.template operator()<typename IdTypeMap<type_id::UINT16>::type>(
464  std::forward<Ts>(args)...);
465  case type_id::UINT32:
466  return f.template operator()<typename IdTypeMap<type_id::UINT32>::type>(
467  std::forward<Ts>(args)...);
468  case type_id::UINT64:
469  return f.template operator()<typename IdTypeMap<type_id::UINT64>::type>(
470  std::forward<Ts>(args)...);
471  case type_id::FLOAT32:
472  return f.template operator()<typename IdTypeMap<type_id::FLOAT32>::type>(
473  std::forward<Ts>(args)...);
474  case type_id::FLOAT64:
475  return f.template operator()<typename IdTypeMap<type_id::FLOAT64>::type>(
476  std::forward<Ts>(args)...);
477  case type_id::BOOL8:
478  return f.template operator()<typename IdTypeMap<type_id::BOOL8>::type>(
479  std::forward<Ts>(args)...);
481  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_DAYS>::type>(
482  std::forward<Ts>(args)...);
484  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_SECONDS>::type>(
485  std::forward<Ts>(args)...);
487  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_MILLISECONDS>::type>(
488  std::forward<Ts>(args)...);
490  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_MICROSECONDS>::type>(
491  std::forward<Ts>(args)...);
493  return f.template operator()<typename IdTypeMap<type_id::TIMESTAMP_NANOSECONDS>::type>(
494  std::forward<Ts>(args)...);
496  return f.template operator()<typename IdTypeMap<type_id::DURATION_DAYS>::type>(
497  std::forward<Ts>(args)...);
499  return f.template operator()<typename IdTypeMap<type_id::DURATION_SECONDS>::type>(
500  std::forward<Ts>(args)...);
502  return f.template operator()<typename IdTypeMap<type_id::DURATION_MILLISECONDS>::type>(
503  std::forward<Ts>(args)...);
505  return f.template operator()<typename IdTypeMap<type_id::DURATION_MICROSECONDS>::type>(
506  std::forward<Ts>(args)...);
508  return f.template operator()<typename IdTypeMap<type_id::DURATION_NANOSECONDS>::type>(
509  std::forward<Ts>(args)...);
511  return f.template operator()<typename IdTypeMap<type_id::DICTIONARY32>::type>(
512  std::forward<Ts>(args)...);
513  case type_id::STRING:
514  return f.template operator()<typename IdTypeMap<type_id::STRING>::type>(
515  std::forward<Ts>(args)...);
516  case type_id::LIST:
517  return f.template operator()<typename IdTypeMap<type_id::LIST>::type>(
518  std::forward<Ts>(args)...);
519  case type_id::DECIMAL32:
520  return f.template operator()<typename IdTypeMap<type_id::DECIMAL32>::type>(
521  std::forward<Ts>(args)...);
522  case type_id::DECIMAL64:
523  return f.template operator()<typename IdTypeMap<type_id::DECIMAL64>::type>(
524  std::forward<Ts>(args)...);
525  case type_id::DECIMAL128:
526  return f.template operator()<typename IdTypeMap<type_id::DECIMAL128>::type>(
527  std::forward<Ts>(args)...);
528  case type_id::STRUCT:
529  return f.template operator()<typename IdTypeMap<type_id::STRUCT>::type>(
530  std::forward<Ts>(args)...);
531  default: {
532 #ifndef __CUDA_ARCH__
533  CUDF_FAIL("Invalid type_id.");
534 #else
535  CUDF_UNREACHABLE("Invalid type_id.");
536 #endif
537  }
538  }
539 }
540 
541 // @cond
542 namespace detail {
543 template <typename T1>
544 struct double_type_dispatcher_second_type {
545 #ifdef __CUDACC__
546 #pragma nv_exec_check_disable
547 #endif
548  template <typename T2, typename F, typename... Ts>
549  CUDF_HOST_DEVICE __forceinline__ decltype(auto) operator()(F&& f, Ts&&... args) const
550  {
551  return f.template operator()<T1, T2>(std::forward<Ts>(args)...);
552  }
553 };
554 
555 template <template <cudf::type_id> typename IdTypeMap>
556 struct double_type_dispatcher_first_type {
557 #ifdef __CUDACC__
558 #pragma nv_exec_check_disable
559 #endif
560  template <typename T1, typename F, typename... Ts>
561  CUDF_HOST_DEVICE __forceinline__ decltype(auto) operator()(cudf::data_type type2,
562  F&& f,
563  Ts&&... args) const
564  {
565  return type_dispatcher<IdTypeMap>(type2,
566  detail::double_type_dispatcher_second_type<T1>{},
567  std::forward<F>(f),
568  std::forward<Ts>(args)...);
569  }
570 };
571 } // namespace detail
572 // @endcond
573 
589 #ifdef __CUDACC__
590 #pragma nv_exec_check_disable
591 #endif
592 template <template <cudf::type_id> typename IdTypeMap = id_to_type_impl, typename F, typename... Ts>
593 CUDF_HOST_DEVICE __forceinline__ constexpr decltype(auto) double_type_dispatcher(
594  cudf::data_type type1, cudf::data_type type2, F&& f, Ts&&... args)
595 {
596  return type_dispatcher<IdTypeMap>(type1,
597  detail::double_type_dispatcher_first_type<IdTypeMap>{},
598  type2,
599  std::forward<F>(f),
600  std::forward<Ts>(args)...);
601 }
602 
612 std::string type_to_name(data_type type);
613  // end of group
615 } // namespace cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:227
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:727
A non-owning, immutable view of device data that represents a list of elements of arbitrary type (inc...
Definition: list_view.hpp:30
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:429
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:793
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:216
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:66
detail::timestamp< cudf::duration_s > timestamp_s
Type alias representing a cudf::duration_s (int64_t) since the unix epoch.
Definition: timestamps.hpp:62
detail::timestamp< cudf::duration_D > timestamp_D
Type alias representing a cudf::duration_D (int32_t) since the unix epoch.
Definition: timestamps.hpp:50
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:57
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:33
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:49
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:53
detail::timestamp< cudf::duration_us > timestamp_us
Type alias representing a cudf::duration_us (int64_t) since the unix epoch.
Definition: timestamps.hpp:70
detail::timestamp< cudf::duration_ns > timestamp_ns
Type alias representing a cudf::duration_ns (int64_t) since the unix epoch.
Definition: timestamps.hpp:74
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:45
#define MAP_TIMESTAMP_SCALAR(Type)
Macro used to define scalar type and scalar device type for cudf::timestamp_scalar template class for...
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:209
type_id
Identifies a column's logical element type.
Definition: types.hpp:187
@ BOOL8
Boolean using one byte per value, 0 == false, else true.
@ FLOAT64
8 byte floating point
@ UINT32
4 byte unsigned integer
@ LIST
List elements.
@ DURATION_MILLISECONDS
time interval of milliseconds in int64
@ UINT16
2 byte unsigned integer
@ INT64
8 byte signed integer
@ DECIMAL128
Fixed-point type with __int128_t.
@ INT16
2 byte signed integer
@ TIMESTAMP_MILLISECONDS
point in time in milliseconds since Unix Epoch in int64
@ DURATION_NANOSECONDS
time interval of nanoseconds in int64
@ STRING
String elements.
@ INT32
4 byte signed integer
@ DURATION_DAYS
time interval of days in int32
@ UINT64
8 byte unsigned integer
@ TIMESTAMP_MICROSECONDS
point in time in microseconds since Unix Epoch in int64
@ DURATION_SECONDS
time interval of seconds in int64
@ DURATION_MICROSECONDS
time interval of microseconds in int64
@ FLOAT32
4 byte floating point
@ STRUCT
Struct elements.
@ EMPTY
Always null with no underlying data.
@ TIMESTAMP_SECONDS
point in time in seconds since Unix Epoch in int64
@ TIMESTAMP_NANOSECONDS
point in time in nanoseconds since Unix Epoch in int64
@ TIMESTAMP_DAYS
point in time in days since Unix Epoch in int32
@ DECIMAL64
Fixed-point type with int64_t.
@ DECIMAL32
Fixed-point type with int32_t.
@ UINT8
1 byte unsigned integer
@ INT8
1 byte signed integer
@ DICTIONARY32
Dictionary type using int32 indices.
cuDF interfaces
Definition: aggregation.hpp:34
fixed_point and supporting types
Definition: fixed_point.hpp:32
A strongly typed wrapper for indices in a DICTIONARY type column.
Definition: dictionary.hpp:48
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.