column_view.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 #pragma once
17 
18 #include <cudf/types.hpp>
19 #include <cudf/utilities/error.hpp>
20 #include <cudf/utilities/prefetch.hpp>
21 #include <cudf/utilities/span.hpp>
24 
25 #include <limits>
26 #include <type_traits>
27 #include <vector>
28 
33 namespace CUDF_EXPORT cudf {
34 namespace detail {
54  public:
71  template <typename T = void,
72  CUDF_ENABLE_IF(std::is_same_v<T, void> or is_rep_layout_compatible<T>())>
73  T const* head() const noexcept
74  {
75  return static_cast<T const*>(get_data());
76  }
77 
90  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
91  T const* data() const noexcept
92  {
93  return head<T>() + _offset;
94  }
95 
106  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
107  T const* begin() const noexcept
108  {
109  return data<T>();
110  }
111 
122  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
123  T const* end() const noexcept
124  {
125  return begin<T>() + size();
126  }
127 
133  [[nodiscard]] size_type size() const noexcept { return _size; }
134 
140  [[nodiscard]] bool is_empty() const noexcept { return size() == 0; }
141 
147  [[nodiscard]] data_type type() const noexcept { return _type; }
148 
158  [[nodiscard]] bool nullable() const noexcept { return nullptr != _null_mask; }
159 
165  [[nodiscard]] size_type null_count() const { return _null_count; }
166 
181  [[nodiscard]] size_type null_count(size_type begin, size_type end) const;
182 
190  [[nodiscard]] bool has_nulls() const { return null_count() > 0; }
191 
204  [[nodiscard]] bool has_nulls(size_type begin, size_type end) const
205  {
206  return null_count(begin, end) > 0;
207  }
208 
217  [[nodiscard]] bitmask_type const* null_mask() const noexcept { return _null_mask; }
218 
225  [[nodiscard]] size_type offset() const noexcept { return _offset; }
226 
227  protected:
237  [[nodiscard]] virtual void const* get_data() const noexcept { return _data; }
238 
239  data_type _type{type_id::EMPTY};
240  size_type _size{};
241  void const* _data{};
242  bitmask_type const* _null_mask{};
245  mutable size_type _null_count{};
246  size_type _offset{};
248 
249  column_view_base() = default;
250  virtual ~column_view_base() = default;
251  column_view_base(column_view_base const&) = default;
265 
291  size_type size,
292  void const* data,
293  bitmask_type const* null_mask,
295  size_type offset = 0);
296 };
297 
298 } // namespace detail
299 
321  public:
322  column_view() = default;
323 
324  // these pragmas work around the nvcc issue where if a column_view is used
325  // inside of a __device__ code path, these functions will end up being created
326  // as __host__ __device__ because they are explicitly defaulted. However, if
327  // they then end up being called by a simple __host__ function
328  // (eg std::vector destructor) you get a compile error because you're trying to
329  // call a __host__ __device__ function from a __host__ function.
330 #ifdef __CUDACC__
331 #pragma nv_exec_check_disable
332 #endif
333  ~column_view() override = default;
334 #ifdef __CUDACC__
335 #pragma nv_exec_check_disable
336 #endif
337  column_view(column_view const&) = default;
338  column_view(column_view&&) = default;
344  column_view& operator=(column_view const&) = default;
351 
379  size_type size,
380  void const* data,
381  bitmask_type const* null_mask,
383  size_type offset = 0,
384  std::vector<column_view> const& children = {});
385 
392  [[nodiscard]] column_view child(size_type child_index) const noexcept
393  {
394  return _children[child_index];
395  }
396 
402  [[nodiscard]] size_type num_children() const noexcept { return _children.size(); }
403 
409  auto child_begin() const noexcept { return _children.cbegin(); }
410 
416  auto child_end() const noexcept { return _children.cend(); }
417 
426  template <typename T, CUDF_ENABLE_IF(cudf::is_numeric<T>() or cudf::is_chrono<T>())>
427  column_view(device_span<T const> data)
428  : column_view(
429  cudf::data_type{cudf::type_to_id<T>()}, data.size(), data.data(), nullptr, 0, 0, {})
430  {
431  CUDF_EXPECTS(
432  data.size() <= static_cast<std::size_t>(std::numeric_limits<cudf::size_type>::max()),
433  "Data exceeds the column size limit",
434  std::overflow_error);
435  }
436 
448  template <typename T, CUDF_ENABLE_IF(cudf::is_numeric<T>() or cudf::is_chrono<T>())>
449  [[nodiscard]] operator device_span<T const>() const
450  {
451  CUDF_EXPECTS(type() == cudf::data_type{cudf::type_to_id<T>()},
452  "Device span type must match column view type.");
453  CUDF_EXPECTS(!nullable(), "A nullable column view cannot be converted to a device span.");
454  return device_span<T const>(data<T>(), size());
455  }
456 
457  protected:
467  void const* get_data() const noexcept override;
468 
469  private:
470  friend column_view bit_cast(column_view const& input, data_type type);
471 
472  std::vector<column_view> _children{};
474 }; // namespace cudf
475 
497  public:
498  mutable_column_view() = default;
499 
500  ~mutable_column_view() override{
501  // Needed so that the first instance of the implicit destructor for any TU isn't 'constructed'
502  // from a host+device function marking the implicit version also as host+device
503  };
504 
519 
546  size_type size,
547  void* data,
548  bitmask_type* null_mask,
550  size_type offset = 0,
551  std::vector<mutable_column_view> const& children = {});
552 
568  template <typename T = void,
569  CUDF_ENABLE_IF(std::is_same_v<T, void> or is_rep_layout_compatible<T>())>
570  T* head() const noexcept
571  {
572  return const_cast<T*>(detail::column_view_base::head<T>());
573  }
574 
587  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
588  T* data() const noexcept
589  {
590  return const_cast<T*>(detail::column_view_base::data<T>());
591  }
592 
603  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
604  T* begin() const noexcept
605  {
606  return const_cast<T*>(detail::column_view_base::begin<T>());
607  }
608 
619  template <typename T, CUDF_ENABLE_IF(is_rep_layout_compatible<T>())>
620  T* end() const noexcept
621  {
622  return const_cast<T*>(detail::column_view_base::end<T>());
623  }
624 
634  [[nodiscard]] bitmask_type* null_mask() const noexcept
635  {
636  return const_cast<bitmask_type*>(detail::column_view_base::null_mask());
637  }
638 
646  void set_null_count(size_type new_null_count);
647 
654  [[nodiscard]] mutable_column_view child(size_type child_index) const noexcept
655  {
656  return mutable_children[child_index];
657  }
658 
664  [[nodiscard]] size_type num_children() const noexcept { return mutable_children.size(); }
665 
671  auto child_begin() const noexcept { return mutable_children.begin(); }
672 
678  auto child_end() const noexcept { return mutable_children.end(); }
679 
685  operator column_view() const;
686 
687  protected:
697  [[nodiscard]] void const* get_data() const noexcept override;
698 
699  private:
701 
702  std::vector<mutable_column_view> mutable_children;
703 };
704 
712 
735 
758 
759 namespace detail {
775 std::size_t shallow_hash(column_view const& input);
776 
798 bool is_shallow_equivalent(column_view const& lhs, column_view const& rhs);
799 
800 } // namespace detail
801 } // namespace CUDF_EXPORT cudf
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
column_view(column_view &&)=default
Move constructor.
size_type num_children() const noexcept
Returns the number of child columns.
auto child_begin() const noexcept
Returns iterator to the beginning of the ordered sequence of child column-views.
column_view & operator=(column_view &&)=default
Move assignment operator.
column_view & operator=(column_view const &)=default
Copy assignment operator.
void const * get_data() const noexcept override
Returns pointer to the base device memory allocation.
auto child_end() const noexcept
Returns iterator to the end of the ordered sequence of child column-views.
column_view child(size_type child_index) const noexcept
Returns the specified child.
column_view(data_type type, size_type size, void const *data, bitmask_type const *null_mask, size_type null_count, size_type offset=0, std::vector< column_view > const &children={})
Construct a column_view from pointers to device memory for the elements and bitmask of the column.
column_view(column_view const &)=default
Copy constructor.
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
Definition: column_view.hpp:53
size_type null_count(size_type begin, size_type end) const
Returns the count of null elements in the range [begin, end)
T const * end() const noexcept
Return one past the last element after underlying data is casted to the specified type.
data_type type() const noexcept
Returns the element data_type
column_view_base(column_view_base &&)=default
Move constructor.
column_view_base(data_type type, size_type size, void const *data, bitmask_type const *null_mask, size_type null_count, size_type offset=0)
Construct a column_view_base from pointers to device memory for the elements and bitmask of the colum...
size_type null_count() const
Returns the count of null elements.
column_view_base & operator=(column_view_base const &)=default
Copy assignment operator.
column_view_base & operator=(column_view_base &&)=default
Move assignment operator.
size_type size() const noexcept
Returns the number of elements in the column.
T const * begin() const noexcept
Return first element (accounting for offset) after underlying data is casted to the specified type.
T const * data() const noexcept
Returns the underlying data casted to the specified type, plus the offset.
Definition: column_view.hpp:91
size_type offset() const noexcept
Returns the index of the first element relative to the base memory allocation, i.e....
virtual void const * get_data() const noexcept
Returns pointer to the base device memory allocation.
T const * head() const noexcept
Returns pointer to the base device memory allocation casted to the specified type.
Definition: column_view.hpp:73
bool has_nulls(size_type begin, size_type end) const
Indicates if the column contains null elements in the range [begin, end), i.e., null_count(begin,...
bool has_nulls() const
Indicates if the column contains null elements, i.e., null_count() > 0
bitmask_type const * null_mask() const noexcept
Returns raw pointer to the underlying bitmask allocation.
bool nullable() const noexcept
Indicates if the column can contain null elements, i.e., if it has an allocated bitmask.
column_view_base(column_view_base const &)=default
Copy constructor.
bool is_empty() const noexcept
Returns true if size() returns zero, or false otherwise.
A non-owning, mutable view of device data as a column of elements, some of which may be null as indic...
T * head() const noexcept
Returns pointer to the base device memory allocation casted to the specified type.
size_type num_children() const noexcept
Returns the number of child columns.
T * begin() const noexcept
Return first element (accounting for offset) after underlying data is casted to the specified type.
void set_null_count(size_type new_null_count)
Set the null count.
auto child_begin() const noexcept
Returns iterator to the beginning of the ordered sequence of child column-views.
mutable_column_view(data_type type, size_type size, void *data, bitmask_type *null_mask, size_type null_count, size_type offset=0, std::vector< mutable_column_view > const &children={})
Construct a mutable_column_view from pointers to device memory for the elements and bitmask of the co...
T * data() const noexcept
Returns the underlying data casted to the specified type, plus the offset.
mutable_column_view(mutable_column_view const &)=default
Copy constructor.
mutable_column_view & operator=(mutable_column_view &&)=default
Move assignment operator.
mutable_column_view & operator=(mutable_column_view const &)=default
Copy assignment operator.
auto child_end() const noexcept
Returns iterator to the end of the ordered sequence of child column-views.
bitmask_type * null_mask() const noexcept
Returns raw pointer to the underlying bitmask allocation.
void const * get_data() const noexcept override
Returns pointer to the base device memory allocation.
mutable_column_view child(size_type child_index) const noexcept
Returns a reference to the specified child.
mutable_column_view(mutable_column_view &&)=default
Move constructor.
T * end() const noexcept
Return one past the last element after underlying data is casted to the specified type.
std::size_t shallow_hash(column_view const &input)
Computes a hash value from the shallow state of the specified column.
bool is_shallow_equivalent(column_view const &lhs, column_view const &rhs)
Uses only shallow state to determine if two column_views view equivalent columns.
cudf::size_type null_count(bitmask_type const *bitmask, size_type start, size_type stop, rmm::cuda_stream_view stream=cudf::get_default_stream())
Given a validity bitmask, counts the number of null elements (unset bits) in the range [start,...
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition: error.hpp:178
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
constexpr bool is_chrono()
Indicates whether the type T is a chrono type.
Definition: traits.hpp:469
#define CUDF_ENABLE_IF(...)
Convenience macro for SFINAE as an unnamed template parameter.
Definition: traits.hpp:48
cuDF interfaces
Definition: aggregation.hpp:35
bool nullable(table_view const &view)
Returns True if any of the columns in the table is nullable. (not entire hierarchy)
mutable_column_view bit_cast(mutable_column_view const &input, data_type type)
Zero-copy cast between types with the same size and compatible underlying representations.
size_type count_descendants(column_view parent)
Counts the number of descendants of the specified parent.
APIs for spans.
Device version of C++20 std::span with reduced feature set.
Definition: span.hpp:338
Defines the mapping between cudf::type_id runtime type information and concrete C++ types.
Type declarations for libcudf.