types.cuh
1 /*
2  * Copyright (c) 2025, 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/types.hpp>
20 #include <cudf/utilities/bit.hpp>
22 
23 #include <cuda/std/type_traits>
24 
25 namespace cudf {
26 namespace jit {
27 namespace detail {
28 
30 class alignas(16) column_device_view_base {
31  public:
43  void const* data,
44  bitmask_type const* null_mask,
47  {
48  }
49 
53  template <typename T = void,
54  CUDF_ENABLE_IF(cuda::std::is_same_v<T, void> or is_rep_layout_compatible<T>())>
55  [[nodiscard]] CUDF_HOST_DEVICE T const* head() const noexcept
56  {
57  return static_cast<T const*>(_data);
58  }
59 
63  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
64  [[nodiscard]] CUDF_HOST_DEVICE T const* data() const noexcept
65  {
66  return head<T>() + _offset;
67  }
68 
72  [[nodiscard]] CUDF_HOST_DEVICE size_type size() const noexcept { return _size; }
73 
77  [[nodiscard]] CUDF_HOST_DEVICE data_type type() const noexcept { return _type; }
78 
82  [[nodiscard]] CUDF_HOST_DEVICE bool nullable() const noexcept { return nullptr != _null_mask; }
83 
87  [[nodiscard]] CUDF_HOST_DEVICE bitmask_type const* null_mask() const noexcept
88  {
89  return _null_mask;
90  }
91 
95  [[nodiscard]] CUDF_HOST_DEVICE size_type offset() const noexcept { return _offset; }
96 
100  [[nodiscard]] __device__ bool is_valid(size_type element_index) const noexcept
101  {
102  return not nullable() or is_valid_nocheck(element_index);
103  }
104 
108  [[nodiscard]] __device__ bool is_valid_nocheck(size_type element_index) const noexcept
109  {
110  return bit_is_set(_null_mask, offset() + element_index);
111  }
112 
116  [[nodiscard]] __device__ bool is_null(size_type element_index) const noexcept
117  {
118  return not is_valid(element_index);
119  }
120 
124  [[nodiscard]] __device__ bool is_null_nocheck(size_type element_index) const noexcept
125  {
126  return not is_valid_nocheck(element_index);
127  }
128 
132  [[nodiscard]] __device__ bitmask_type get_mask_word(size_type word_index) const noexcept
133  {
134  return null_mask()[word_index];
135  }
136 
137  protected:
142 
147 
151  void const* _data;
152 
157 
162 };
163 
164 } // namespace detail
165 
168  public:
182  size_type size,
183  void const* data,
184  bitmask_type const* null_mask,
186  column_device_view* children,
187  size_type num_children)
189  d_children(children),
190  _num_children(num_children)
191  {
192  }
193 
197  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
198  [[nodiscard]] __device__ T element(size_type element_index) const noexcept
199  {
200  return data<T>()[element_index];
201  }
202 
206  template <typename T, CUDF_ENABLE_IF(is_fixed_point<T>())>
207  [[nodiscard]] __device__ T element(size_type element_index) const noexcept
208  {
209  using namespace numeric;
210  using rep = typename T::rep;
211  auto const scale = scale_type{_type.scale()};
212  return T{scaled_integer<rep>{data<rep>()[element_index], scale}};
213  }
214 
218  [[nodiscard]] __device__ column_device_view child(size_type child_index) const noexcept
219  {
220  return d_children[child_index];
221  }
222 
226  [[nodiscard]] CUDF_HOST_DEVICE size_type num_child_columns() const noexcept
227  {
228  return _num_children;
229  }
230 
231  protected:
236 
241 };
242 
245  public:
259  size_type size,
260  void* data,
261  bitmask_type const* null_mask,
263  mutable_column_device_view* children,
264  size_type num_children)
266  d_children(children),
267  _num_children(num_children)
268  {
269  }
270 
274  template <typename T = void,
275  CUDF_ENABLE_IF(cuda::std::is_same_v<T, void> or is_rep_layout_compatible<T>())>
276  CUDF_HOST_DEVICE T* head() const noexcept
277  {
278  return const_cast<T*>(detail::column_device_view_base::head<T>());
279  }
280 
284  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
285  CUDF_HOST_DEVICE T* data() const noexcept
286  {
287  return const_cast<T*>(detail::column_device_view_base::data<T>());
288  }
289 
293  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
294  [[nodiscard]] __device__ T& element(size_type element_index) const noexcept
295  {
296  return data<T>()[element_index];
297  }
298 
302  template <typename T, CUDF_ENABLE_IF(is_fixed_point<T>())>
303  [[nodiscard]] __device__ T element(size_type element_index) const noexcept
304  {
305  using namespace numeric;
306  using rep = typename T::rep;
307  auto const scale = scale_type{_type.scale()};
308  return T{scaled_integer<rep>{data<rep>()[element_index], scale}};
309  }
310 
318  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
319  __device__ void assign(size_type element_index, T value) const noexcept
320  {
321  data<T>()[element_index] = value;
322  }
323 
332  template <typename T, CUDF_ENABLE_IF(is_fixed_point<T>())>
333  __device__ void assign(size_type element_index, T value) const noexcept
334  {
335  // consider asserting that the scale matches
336  using namespace numeric;
337  using rep = typename T::rep;
338  data<rep>()[element_index] = value.value();
339  }
340 
344  [[nodiscard]] __device__ mutable_column_device_view child(size_type child_index) const noexcept
345  {
346  return d_children[child_index];
347  }
348 
354  [[nodiscard]] CUDF_HOST_DEVICE size_type num_child_columns() const noexcept
355  {
356  return _num_children;
357  }
358 
359  private:
365 };
366 
367 } // namespace jit
368 } // namespace cudf
Utilities for bit and bitmask operations.
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
constexpr CUDF_HOST_DEVICE int32_t scale() const noexcept
Returns the scale (for fixed_point types)
Definition: types.hpp:294
A minified version of cudf::column_device_view for use in JIT kernels.
Definition: types.cuh:167
size_type _num_children
The number of child columns.
Definition: types.cuh:240
T element(size_type element_index) const noexcept
Returns reference to element at the specified index.
Definition: types.cuh:198
column_device_view child(size_type child_index) const noexcept
Returns the specified child.
Definition: types.cuh:218
CUDF_HOST_DEVICE column_device_view(data_type type, size_type size, void const *data, bitmask_type const *null_mask, size_type offset, column_device_view *children, size_type num_children)
Creates an instance of this class using pre-existing device memory pointers to data,...
Definition: types.cuh:181
column_device_view * d_children
Definition: types.cuh:235
CUDF_HOST_DEVICE size_type num_child_columns() const noexcept
Returns the number of child columns.
Definition: types.cuh:226
A minified version of cudf::detail::column_device_view_base for use in JIT kernels.
Definition: types.cuh:30
CUDF_HOST_DEVICE column_device_view_base(data_type type, size_type size, void const *data, bitmask_type const *null_mask, size_type offset)
Constructs a column with the specified type, size, data, nullmask and offset.
Definition: types.cuh:41
CUDF_HOST_DEVICE size_type size() const noexcept
Returns the number of elements in the column.
Definition: types.cuh:72
bool is_null_nocheck(size_type element_index) const noexcept
Returns whether the specified element is null.
Definition: types.cuh:124
bool is_null(size_type element_index) const noexcept
Returns whether the specified element is null.
Definition: types.cuh:116
CUDF_HOST_DEVICE size_type offset() const noexcept
Returns the index of the first element relative to the base memory allocation, i.e....
Definition: types.cuh:95
CUDF_HOST_DEVICE T const * data() const noexcept
Returns the underlying data casted to the specified type, plus the offset.
Definition: types.cuh:64
void const * _data
Pointer to device memory containing elements.
Definition: types.cuh:151
size_type _size
Number of elements.
Definition: types.cuh:146
bool is_valid(size_type element_index) const noexcept
Returns whether the specified element holds a valid value (i.e., not null).
Definition: types.cuh:100
CUDF_HOST_DEVICE data_type type() const noexcept
Returns the element type.
Definition: types.cuh:77
bool is_valid_nocheck(size_type element_index) const noexcept
Returns whether the specified element holds a valid value (i.e., not null)
Definition: types.cuh:108
bitmask_type get_mask_word(size_type word_index) const noexcept
Returns the specified bitmask word from the null_mask().
Definition: types.cuh:132
CUDF_HOST_DEVICE T const * head() const noexcept
Returns pointer to the base device memory allocation casted to the specified type.
Definition: types.cuh:55
CUDF_HOST_DEVICE bool nullable() const noexcept
Indicates whether the column can contain null elements, i.e., if it has an allocated bitmask.
Definition: types.cuh:82
CUDF_HOST_DEVICE bitmask_type const * null_mask() const noexcept
Returns raw pointer to the underlying bitmask allocation.
Definition: types.cuh:87
A minified version of cudf::mutable_column_device_view for use in JIT kernels.
Definition: types.cuh:244
mutable_column_device_view(data_type type, size_type size, void *data, bitmask_type const *null_mask, size_type offset, mutable_column_device_view *children, size_type num_children)
Creates an instance of this class using pre-existing device memory pointers to data,...
Definition: types.cuh:258
CUDF_HOST_DEVICE size_type num_child_columns() const noexcept
Returns the number of child columns.
Definition: types.cuh:354
mutable_column_device_view child(size_type child_index) const noexcept
Returns the specified child.
Definition: types.cuh:344
CUDF_HOST_DEVICE T * head() const noexcept
Returns pointer to the base device memory allocation casted to the specified type.
Definition: types.cuh:276
T & element(size_type element_index) const noexcept
Returns reference to element at the specified index.
Definition: types.cuh:294
CUDF_HOST_DEVICE T * data() const noexcept
Returns the underlying data casted to the specified type, plus the offset.
Definition: types.cuh:285
T element(size_type element_index) const noexcept
Returns reference to element at the specified index.
Definition: types.cuh:303
void assign(size_type element_index, T value) const noexcept
Assigns value to the element at element_index
Definition: types.cuh:319
scale_type
The scale type for fixed_point.
Definition: fixed_point.hpp:43
constexpr CUDF_HOST_DEVICE size_type word_index(size_type bit_index)
Returns the index of the word containing the specified bit.
Definition: bit.hpp:74
CUDF_HOST_DEVICE bool bit_is_set(bitmask_type const *bitmask, size_type bit_index)
Indicates whether the specified bit is set to 1
Definition: bit.hpp:128
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
uint32_t bitmask_type
Bitmask type stored as 32-bit unsigned integer.
Definition: types.hpp:96
#define CUDF_ENABLE_IF(...)
Convenience macro for SFINAE as an unnamed template parameter.
Definition: traits.hpp:50
cuDF interfaces
Definition: host_udf.hpp:37
fixed_point and supporting types
Definition: fixed_point.hpp:33
Helper struct for constructing fixed_point when value is already shifted.
Type declarations for libcudf.
#define CUDF_HOST_DEVICE
Indicates that the function or method is usable on host and device.
Definition: types.hpp:32