host_udf.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/aggregation.hpp>
10 #include <cudf/types.hpp>
11 #include <cudf/utilities/export.hpp>
12 #include <cudf/utilities/span.hpp>
13 
14 #include <rmm/resource_ref.hpp>
15 
16 #include <cuda/stream>
17 
18 #include <functional>
19 #include <optional>
20 
27 namespace CUDF_EXPORT cudf {
41  // Declare constructor private to prevent the users from deriving from this class.
42  private:
43  host_udf_base() = default;
44 
45  // Only allow deriving from the structs below.
46  friend struct reduce_host_udf;
47  friend struct segmented_reduce_host_udf;
48  friend struct groupby_host_udf;
49 
50  public:
51  virtual ~host_udf_base() = default;
52 
61  [[nodiscard]] virtual std::size_t do_hash() const
62  {
63  return std::hash<int>{}(static_cast<int>(aggregation::Kind::HOST_UDF));
64  }
65 
71  [[nodiscard]] virtual bool is_equal(host_udf_base const& other) const = 0;
72 
80  [[nodiscard]] virtual std::unique_ptr<host_udf_base> clone() const = 0;
81 };
82 
132  [[nodiscard]] virtual std::unique_ptr<scalar> operator()(
133  column_view const& input,
134  data_type output_dtype,
135  std::optional<std::reference_wrapper<scalar const>> init,
136  cuda::stream_ref stream,
137  rmm::device_async_resource_ref mr) const = 0;
138 };
139 
194  [[nodiscard]] virtual std::unique_ptr<column> operator()(
195  column_view const& input,
197  data_type output_dtype,
198  null_policy null_handling,
199  std::optional<std::reference_wrapper<scalar const>> init,
200  cuda::stream_ref stream,
201  rmm::device_async_resource_ref mr) const = 0;
202 };
203 
204 // Forward declaration.
205 namespace groupby ::detail {
206 struct aggregate_result_functor;
207 }
208 
268  [[nodiscard]] virtual std::unique_ptr<column> get_empty_output(
269  cuda::stream_ref stream, rmm::device_async_resource_ref mr) const = 0;
270 
278  [[nodiscard]] virtual std::unique_ptr<column> operator()(
279  cuda::stream_ref stream, rmm::device_async_resource_ref mr) const = 0;
280 
281  private:
282  // Allow the struct `aggregate_result_functor` to set its private callback variables.
283  friend struct groupby::detail::aggregate_result_functor;
284 
288  std::function<column_view(void)> callback_input_values;
289 
294  std::function<column_view(void)> callback_grouped_values;
295 
300  std::function<column_view(void)> callback_sorted_grouped_values;
301 
305  std::function<size_type(void)> callback_num_groups;
306 
310  std::function<device_span<size_type const>(void)> callback_group_offsets;
311 
315  std::function<device_span<size_type const>(void)> callback_group_labels;
316 
320  std::function<column_view(std::unique_ptr<aggregation>)> callback_compute_aggregation;
321 
322  protected:
328  [[nodiscard]] column_view get_input_values() const
329  {
330  CUDF_EXPECTS(callback_input_values, "Uninitialized callback_input_values.");
331  return callback_input_values();
332  }
333 
340  [[nodiscard]] column_view get_grouped_values() const
341  {
342  CUDF_EXPECTS(callback_grouped_values, "Uninitialized callback_grouped_values.");
343  return callback_grouped_values();
344  }
345 
353  {
354  CUDF_EXPECTS(callback_sorted_grouped_values, "Uninitialized callback_sorted_grouped_values.");
355  return callback_sorted_grouped_values();
356  }
357 
363  [[nodiscard]] size_type get_num_groups() const
364  {
365  CUDF_EXPECTS(callback_num_groups, "Uninitialized callback_num_groups.");
366  return callback_num_groups();
367  }
368 
375  {
376  CUDF_EXPECTS(callback_group_offsets, "Uninitialized callback_group_offsets.");
377  return callback_group_offsets();
378  }
379 
386  {
387  CUDF_EXPECTS(callback_group_labels, "Uninitialized callback_group_labels.");
388  return callback_group_labels();
389  }
390 
400  [[nodiscard]] column_view compute_aggregation(std::unique_ptr<aggregation> other_agg) const
401  {
402  CUDF_EXPECTS(callback_compute_aggregation, "Uninitialized callback for computing aggregation.");
403  return callback_compute_aggregation(std::move(other_agg));
404  }
405 };
406  // end of group
408 } // namespace CUDF_EXPORT cudf
Representation for specifying desired aggregations from aggregation-based APIs, e....
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
Indicator for the logical data type of an element in a column.
Definition: types.hpp:279
The fundamental interface for host-based UDF implementation.
Definition: host_udf.hpp:40
virtual ~host_udf_base()=default
Default destructor.
virtual bool is_equal(host_udf_base const &other) const =0
Compares two instances of the derived class for equality.
virtual std::unique_ptr< host_udf_base > clone() const =0
Clones the instance.
virtual std::size_t do_hash() const
Computes hash value of the instance.
Definition: host_udf.hpp:61
column view class definitions
cuda::mr::resource_ref< cuda::mr::device_accessible > device_async_resource_ref
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition: error.hpp:182
cuda::std::span< T, Extent > device_span
Device span is an alias of cuda::std::span.
Definition: span.hpp:300
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:76
null_policy
Enum to specify whether to include nulls or exclude nulls.
Definition: types.hpp:107
cuDF interfaces
Definition: host_udf.hpp:27
APIs for spans.
The interface for host-based UDF implementation for groupby aggregation context.
Definition: host_udf.hpp:257
device_span< size_type const > get_group_labels() const
Access the group labels (which is also the same as group indices).
Definition: host_udf.hpp:385
size_type get_num_groups() const
Access the number of groups (i.e., number of distinct keys).
Definition: host_udf.hpp:363
column_view get_grouped_values() const
Access the input values grouped according to the input keys for which the values within each group ma...
Definition: host_udf.hpp:340
column_view get_sorted_grouped_values() const
Access the input values grouped according to the input keys and sorted within each group.
Definition: host_udf.hpp:352
column_view get_input_values() const
Access the input values column.
Definition: host_udf.hpp:328
column_view compute_aggregation(std::unique_ptr< aggregation > other_agg) const
Compute a built-in groupby aggregation and access its result.
Definition: host_udf.hpp:400
virtual std::unique_ptr< column > operator()(cuda::stream_ref stream, rmm::device_async_resource_ref mr) const =0
Perform the main groupby computation for the host-based UDF.
device_span< size_type const > get_group_offsets() const
Access the offsets separating groups.
Definition: host_udf.hpp:374
virtual std::unique_ptr< column > get_empty_output(cuda::stream_ref stream, rmm::device_async_resource_ref mr) const =0
Get the output when the input values column is empty.
The interface for host-based UDF implementation for reduction contexts.
Definition: host_udf.hpp:121
virtual std::unique_ptr< scalar > operator()(column_view const &input, data_type output_dtype, std::optional< std::reference_wrapper< scalar const >> init, cuda::stream_ref stream, rmm::device_async_resource_ref mr) const =0
Perform reduction operations.
The interface for host-based UDF implementation for segmented reduction context.
Definition: host_udf.hpp:179
virtual std::unique_ptr< column > operator()(column_view const &input, device_span< size_type const > offsets, data_type output_dtype, null_policy null_handling, std::optional< std::reference_wrapper< scalar const >> init, cuda::stream_ref stream, rmm::device_async_resource_ref mr) const =0
Perform segmented reduction operations.
Type declarations for libcudf.