interop.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/column/column.hpp>
9 #include <cudf/detail/transform.hpp>
11 #include <cudf/table/table.hpp>
13 #include <cudf/types.hpp>
14 #include <cudf/utilities/export.hpp>
16 #include <cudf/utilities/span.hpp>
17 
18 #include <rmm/resource_ref.hpp>
19 
20 #include <span>
21 #include <utility>
22 
23 struct DLManagedTensor;
24 
25 struct ArrowDeviceArray;
26 
27 struct ArrowSchema;
28 
29 struct ArrowArray;
30 
31 struct ArrowArrayStream;
32 
34 // These are types from arrow that we are forward declaring for our API to
35 // avoid needing to include nanoarrow headers.
36 typedef int32_t ArrowDeviceType; // NOLINT
37 
38 #define ARROW_DEVICE_CUDA 2 // NOLINT
40 
47 namespace CUDF_EXPORT cudf {
71 std::unique_ptr<table> from_dlpack(
72  DLManagedTensor const* managed_tensor,
75 
95 DLManagedTensor* to_dlpack(
96  table_view const& input,
99  // end of group
101 
113  std::string name;
114  std::string timezone;
115  std::optional<int32_t> precision;
116  std::vector<column_metadata> children_meta;
117 
123  column_metadata(std::string _name) : name(std::move(_name)) {}
124  column_metadata() = default;
125 };
126 
131 using unique_schema_t = std::unique_ptr<ArrowSchema, void (*)(ArrowSchema*)>;
132 
137 using unique_device_array_t = std::unique_ptr<ArrowDeviceArray, void (*)(ArrowDeviceArray*)>;
138 
143 using owned_columns_t = std::vector<std::unique_ptr<cudf::column>>;
144 
153 template <typename ViewType>
160  explicit custom_view_deleter(owned_columns_t&& owned) : owned_mem_{std::move(owned)} {}
161 
167  void operator()(ViewType* ptr) const { delete ptr; }
168 
170 };
171 
177  std::unique_ptr<cudf::table_view, custom_view_deleter<cudf::table_view>>;
178 
184  std::unique_ptr<cudf::column_view, custom_view_deleter<cudf::column_view>>;
185 
186 namespace interop {
187 
188 struct arrow_array_container;
189 
204 
218 std::vector<cudf::column_metadata> get_table_metadata(cudf::table_view const& input);
219 
229  public:
242  column_metadata const& metadata,
245 
258  arrow_column(ArrowSchema&& schema,
259  ArrowDeviceArray&& input,
262 
275  arrow_column(ArrowSchema&& schema,
276  ArrowArray&& input,
279 
291  arrow_column(ArrowArrayStream&& input,
294 
306  ArrowSchema* output,
309 
318  void to_arrow(ArrowDeviceArray* output,
319  ArrowDeviceType device_type = ARROW_DEVICE_CUDA,
322 
330  [[nodiscard]] column_view view() const;
331 
332  private:
333  std::shared_ptr<arrow_array_container>
334  container;
336  owned_columns_t view_columns;
337  column_view cached_view;
338 };
339 
348 class arrow_table {
349  public:
362  std::span<column_metadata const> metadata,
365 
378  arrow_table(ArrowSchema&& schema,
379  ArrowDeviceArray&& input,
382 
395  arrow_table(ArrowSchema&& schema,
396  ArrowArray&& input,
399 
411  arrow_table(ArrowArrayStream&& input,
414 
426  ArrowSchema* output,
429 
438  void to_arrow(ArrowDeviceArray* output,
439  ArrowDeviceType device_type = ARROW_DEVICE_CUDA,
442 
450  [[nodiscard]] table_view view() const;
451 
452  private:
453  std::shared_ptr<arrow_array_container>
454  container;
456  owned_columns_t view_columns;
457  table_view cached_view;
458 };
459 
460 } // namespace interop
461 
478  std::span<column_metadata const> metadata);
479 
505  cudf::table&& table,
508 
534  cudf::column&& col,
537 
566  cudf::table_view const& table,
569 
598  cudf::column_view const& col,
601 
623  cudf::table_view const& table,
626 
648  cudf::column_view const& col,
651 
668  cudf::strings_column_view const& col,
671 
689 std::unique_ptr<cudf::table> from_arrow(
690  ArrowSchema const* schema,
691  ArrowArray const* input,
694 
708 std::unique_ptr<cudf::column> from_arrow_column(
709  ArrowSchema const* schema,
710  ArrowArray const* input,
713 
734 std::unique_ptr<table> from_arrow_host(
735  ArrowSchema const* schema,
736  ArrowDeviceArray const* input,
739 
753 std::unique_ptr<table> from_arrow_stream(
754  ArrowArrayStream* input,
757 
771 std::unique_ptr<column> from_arrow_stream_column(
772  ArrowArrayStream* input,
775 
795 std::unique_ptr<column> from_arrow_host_column(
796  ArrowSchema const* schema,
797  ArrowDeviceArray const* input,
800 
839  ArrowSchema const* schema,
840  ArrowDeviceArray const* input,
843 
877  ArrowSchema const* schema,
878  ArrowDeviceArray const* input,
881  // end of group
883 } // 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...
A container of nullable device data as a column of elements.
Definition: column.hpp:36
A standard interchange medium for ArrowDeviceArray data in cudf.
Definition: interop.hpp:228
arrow_column(ArrowSchema &&schema, ArrowDeviceArray &&input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow column object.
arrow_column(ArrowSchema &&schema, ArrowArray &&input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow column object.
column_view view() const
Get a view of the column data.
void to_arrow(ArrowDeviceArray *output, ArrowDeviceType device_type=ARROW_DEVICE_CUDA, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref()) const
Convert the column to an ArrowDeviceArray.
arrow_column(ArrowArrayStream &&input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow column object.
arrow_column(cudf::column &&input, column_metadata const &metadata, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow column object.
void to_arrow_schema(ArrowSchema *output, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref()) const
Convert the column to an ArrowSchema.
A standard interchange medium for ArrowDeviceArray data in cudf.
Definition: interop.hpp:348
arrow_table(ArrowSchema &&schema, ArrowDeviceArray &&input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow table object.
arrow_table(ArrowArrayStream &&input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow table object.
arrow_table(cudf::table &&input, std::span< column_metadata const > metadata, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow table object.
void to_arrow(ArrowDeviceArray *output, ArrowDeviceType device_type=ARROW_DEVICE_CUDA, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref()) const
Convert the table to an ArrowDeviceArray.
arrow_table(ArrowSchema &&schema, ArrowArray &&input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new arrow table object.
table_view view() const
Get a view of the table data.
void to_arrow_schema(ArrowSchema *output, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref()) const
Convert the table to an ArrowSchema.
Given a column-view of strings type, an instance of this class provides a wrapper on this compound co...
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:206
A set of cudf::column's of the same size.
Definition: table.hpp:31
Class definition for cudf::column.
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
std::unique_ptr< cudf::table_view, custom_view_deleter< cudf::table_view > > unique_table_view_t
typedef for a unique_ptr to a cudf::table_view with custom deleter
Definition: interop.hpp:177
std::vector< std::unique_ptr< cudf::column > > owned_columns_t
typedef for a vector of owning columns, used for conversion from ArrowDeviceArray
Definition: interop.hpp:143
std::unique_ptr< column > from_arrow_host_column(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::column from given ArrowDeviceArray input.
std::unique_ptr< cudf::column > from_arrow_column(ArrowSchema const *schema, ArrowArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::column from a given ArrowArray and ArrowSchema input.
unique_schema_t to_arrow_schema(cudf::table_view const &input, std::span< column_metadata const > metadata)
Create ArrowSchema from cudf table and metadata.
std::unique_ptr< table > from_arrow_stream(ArrowArrayStream *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::table from given ArrowArrayStream input.
unique_table_view_t from_arrow_device(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::table_view from given ArrowDeviceArray and ArrowSchema
unique_device_array_t to_arrow_host_stringview(cudf::strings_column_view const &col, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Copy strings column data to host and create ArrowDeviceArray for it using the ArrowBinaryView format.
unique_column_view_t from_arrow_device_column(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::column_view from given ArrowDeviceArray and ArrowSchema
unique_device_array_t to_arrow_host(cudf::column_view const &col, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Copy column view data to host and create ArrowDeviceArray for it.
unique_device_array_t to_arrow_device(cudf::column_view const &col, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create ArrowDeviceArray from a column view.
std::unique_ptr< cudf::column_view, custom_view_deleter< cudf::column_view > > unique_column_view_t
typedef for a unique_ptr to a cudf::column_view with custom deleter
Definition: interop.hpp:184
std::unique_ptr< ArrowSchema, void(*)(ArrowSchema *)> unique_schema_t
typedef for a unique_ptr to an ArrowSchema with custom deleter
Definition: interop.hpp:131
std::unique_ptr< ArrowDeviceArray, void(*)(ArrowDeviceArray *)> unique_device_array_t
typedef for a unique_ptr to an ArrowDeviceArray with a custom deleter
Definition: interop.hpp:137
std::unique_ptr< table > from_arrow_host(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::table from given ArrowDeviceArray input.
std::unique_ptr< column > from_arrow_stream_column(ArrowArrayStream *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::column from given ArrowArrayStream input.
std::unique_ptr< cudf::table > from_arrow(ArrowSchema const *schema, ArrowArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Create cudf::table from given ArrowArray and ArrowSchema input.
std::unique_ptr< table > from_dlpack(DLManagedTensor const *managed_tensor, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Convert a DLPack DLTensor into a cudf table.
DLManagedTensor * to_dlpack(table_view const &input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Convert a cudf table into a DLPack DLTensor.
rmm::device_async_resource_ref get_current_device_resource_ref()
Get the current device memory resource reference.
cuda::mr::resource_ref< cuda::mr::device_accessible > device_async_resource_ref
std::vector< cudf::column_metadata > get_table_metadata(cudf::table_view const &input)
Helper function to generate empty table metadata (all columns with no names) for arrow conversion.
cudf::column_metadata get_column_metadata(cudf::column_view const &input)
Helper function to generate empty column metadata (column with no name) for arrow conversion.
APIs for getting and setting the current device memory resource.
cuDF interfaces
Definition: host_udf.hpp:26
APIs for spans.
Class definition for cudf::strings_column_view.
Detailed metadata information for arrow array.
Definition: interop.hpp:112
std::optional< int32_t > precision
Resulting decimal precision of the column.
Definition: interop.hpp:115
column_metadata(std::string _name)
Construct a new column metadata object.
Definition: interop.hpp:123
std::vector< column_metadata > children_meta
Metadata of children of the column.
Definition: interop.hpp:116
std::string name
Name of the column.
Definition: interop.hpp:113
std::string timezone
Timezone of the column.
Definition: interop.hpp:114
functor for a custom deleter to a unique_ptr of table_view
Definition: interop.hpp:154
void operator()(ViewType *ptr) const
operator to delete the unique_ptr
Definition: interop.hpp:167
owned_columns_t owned_mem_
Owned columns that must be deleted.
Definition: interop.hpp:169
custom_view_deleter(owned_columns_t &&owned)
Construct a new custom view deleter object.
Definition: interop.hpp:160
Class definition for cudf::table.
Class definitions for (mutable)_table_view
Type declarations for libcudf.