host_udf.hpp
前往此文件的文档。
1 /*
2  * Copyright (c) 2024-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  * https://apache.ac.cn/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/aggregation.hpp>
21 #include <cudf/types.hpp>
22 #include <cudf/utilities/export.hpp>
23 #include <cudf/utilities/span.hpp>
24 
25 #include <rmm/cuda_stream_view.hpp>
26 #include <rmm/resource_ref.hpp>
27 
28 #include <functional>
29 #include <optional>
30 
37 namespace CUDF_EXPORT cudf {
51  // Declare constructor private to prevent the users from deriving from this class.
52  private
53  host_udf_base() = default;
54 
55  // Only allow deriving from the structs below.
56  friend struct reduce_host_udf;
57  friend struct segmented_reduce_host_udf;
58  friend struct groupby_host_udf;
59 
60  public
61  virtual ~host_udf_base() = default;
62 
71  [[nodiscard]] virtual std::size_t do_hash() const
72  {
73  return std::hash<int>{}(static_cast<int>(aggregation::Kind::HOST_UDF));
74  }
75 
81  [[nodiscard]] virtual bool is_equal(host_udf_base const& other) const = 0;
82 
90  [[nodiscard]] virtual std::unique_ptr<host_udf_base> clone() const = 0;
91 };
92 
142  [[nodiscard]] virtual std::unique_ptr<scalar> operator()(
143  column_view const& input,
144  data_type output_dtype,
145  std::optional<std::reference_wrapper<scalar const>> init,
146  rmm::cuda_stream_view stream,
147  rmm::device_async_resource_ref mr) const = 0;
148 };
149 
204  [[nodiscard]] virtual std::unique_ptr<column> operator()(
205  column_view const& input,
207  data_type output_dtype,
208  null_policy null_handling,
209  std::optional<std::reference_wrapper<scalar const>> init,
210  rmm::cuda_stream_view stream,
211  rmm::device_async_resource_ref mr) const = 0;
212 };
213 
214 // Forward declaration.
215 namespace groupby ::detail {
216 struct aggregate_result_functor;
217 }
218 
278  [[nodiscard]] virtual std::unique_ptr<column> get_empty_output(
280 
288  [[nodiscard]] virtual std::unique_ptr<column> operator()(
290 
291  private
292  // Allow the struct `aggregate_result_functor` to set its private callback variables.
293  friend struct groupby::detail::aggregate_result_functor;
294 
298  std::function<column_view(void)> callback_input_values;
299 
304  std::function<column_view(void)> callback_grouped_values;
305 
310  std::function<column_view(void)> callback_sorted_grouped_values;
311 
315  std::function<size_type(void)> callback_num_groups;
316 
320  std::function<device_span<size_type const>(void)> callback_group_offsets;
321 
325  std::function<device_span<size_type const>(void)> callback_group_labels;
326 
330  std::function<column_view(std::unique_ptr<aggregation>)> callback_compute_aggregation;
331 
332  protected
338  [[nodiscard]] column_view get_input_values() const
339  {
340  CUDF_EXPECTS(callback_input_values, "Uninitialized callback_input_values.");
341  return callback_input_values();
342  }
343 
350  [[nodiscard]] column_view get_grouped_values() const
351  {
352  CUDF_EXPECTS(callback_grouped_values, "Uninitialized callback_grouped_values.");
353  return callback_grouped_values();
354  }
355 
363  {
364  CUDF_EXPECTS(callback_sorted_grouped_values, "Uninitialized callback_sorted_grouped_values.");
365  return callback_sorted_grouped_values();
366  }
367 
373  [[nodiscard]] size_type get_num_groups() const
374  {
375  CUDF_EXPECTS(callback_num_groups, "Uninitialized callback_num_groups.");
376  return callback_num_groups();
377  }
378 
385  {
386  CUDF_EXPECTS(callback_group_offsets, "Uninitialized callback_group_offsets.");
387  return callback_group_offsets();
388  }
389 
396  {
397  CUDF_EXPECTS(callback_group_labels, "Uninitialized callback_group_labels.");
398  return callback_group_labels();
399  }
400 
410  [[nodiscard]] column_view compute_aggregation(std::unique_ptr<aggregation> other_agg) const
411  {
412  CUDF_EXPECTS(callback_compute_aggregation, "Uninitialized callback for computing aggregation.");
413  return callback_compute_aggregation(std::move(other_agg));
414  }
415 };
416  // end of group
418 } // namespace CUDF_EXPORT cudf
用于指定基于聚合的 API(例如...)所需聚合的表示形式。
设备数据作为元素列的非拥有、不可变视图,其中一些元素可能为 null,例如...
列中元素的逻辑数据类型指示符。
定义: types.hpp:243
基于主机的 UDF 实现的基本接口。
定义: host_udf.hpp:50
virtual ~host_udf_base()=default
默认析构函数。
virtual bool is_equal(host_udf_base const &other) const =0
比较派生类的两个实例是否相等。
virtual std::unique_ptr< host_udf_base > clone() const =0
克隆实例。
virtual std::size_t do_hash() const
计算实例的哈希值。
定义: host_udf.hpp:71
列视图类定义
cuda::mr::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
#define CUDF_EXPECTS(...)
用于检查(前置)条件的宏,当条件被违反时抛出异常。
定义: error.hpp:178
int32_t size_type
列和表的行索引类型。
定义: types.hpp:95
null_policy
枚举,指定是否包含 null 或排除 null。
定义: types.hpp:126
cuDF 接口
定义: host_udf.hpp:37
spans 的 API。
功能集缩减的 C++20 std::span 设备版本。
定义: span.hpp:355
用于 groupby 聚合上下文的基于主机的 UDF 实现接口。
device_span< size_type const > get_group_labels() const
访问组标签(与组索引相同)。
size_type get_num_groups() const
访问组的数量(即不同键的数量)。
column_view get_grouped_values() const
访问根据输入键分组的输入值,其中每个组内的值可能...
column_view get_sorted_grouped_values() const
访问根据输入键分组并在每个组内排序的输入值。
column_view get_input_values() const
访问输入值列。
column_view compute_aggregation(std::unique_ptr< aggregation > other_agg) const
计算内置的 groupby 聚合并访问其结果。
virtual std::unique_ptr< column > get_empty_output(rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr) const =0
当输入值列为空时获取输出。
virtual std::unique_ptr< column > operator()(rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr) const =0
执行基于主机的 UDF 的主要 groupby 计算。
device_span< size_type const > get_group_offsets() const
访问分隔组的偏移量。
用于归约上下文的基于主机的 UDF 实现接口。
virtual std::unique_ptr< scalar > operator()(column_view const &input, data_type output_dtype, std::optional< std::reference_wrapper< scalar const >> init, rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr) const =0
执行归约操作。
用于分段归约上下文的基于主机的 UDF 实现接口。
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, rmm::cuda_stream_view stream, rmm::device_async_resource_ref mr) const =0
执行分段归约操作。
libcudf 的类型声明。