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 
41 namespace CUDF_EXPORT cudf {
67 std::unique_ptr<table> from_dlpack(
68  DLManagedTensor const* managed_tensor,
71 
91 DLManagedTensor* to_dlpack(
92  table_view const& input,
95  // end of group
97 
111  std::string name;
112  std::string timezone;
113  std::optional<int32_t> precision;
114  std::vector<column_metadata> children_meta;
115 
121  column_metadata(std::string _name) : name(std::move(_name)) {}
122  column_metadata() = default;
123 };
124 
129 using unique_schema_t = std::unique_ptr<ArrowSchema, void (*)(ArrowSchema*)>;
130 
135 using unique_device_array_t = std::unique_ptr<ArrowDeviceArray, void (*)(ArrowDeviceArray*)>;
136 
141 using owned_columns_t = std::vector<std::unique_ptr<cudf::column>>;
142 
151 template <typename ViewType>
158  explicit custom_view_deleter(owned_columns_t&& owned) : owned_mem_{std::move(owned)} {}
159 
165  void operator()(ViewType* ptr) const { delete ptr; }
166 
168 };
169 
175  std::unique_ptr<cudf::table_view, custom_view_deleter<cudf::table_view>>;
176 
182  std::unique_ptr<cudf::column_view, custom_view_deleter<cudf::column_view>>;
183 
184 namespace interop {
185 
186 struct arrow_array_container;
187 
202 
216 std::vector<cudf::column_metadata> get_table_metadata(cudf::table_view const& input);
217 
227  public:
240  column_metadata const& metadata,
243 
256  arrow_column(ArrowSchema&& schema,
257  ArrowDeviceArray&& input,
260 
273  arrow_column(ArrowSchema&& schema,
274  ArrowArray&& input,
277 
289  arrow_column(ArrowArrayStream&& input,
292 
304  ArrowSchema* output,
307 
316  void to_arrow(ArrowDeviceArray* output,
317  ArrowDeviceType device_type = ARROW_DEVICE_CUDA,
320 
328  [[nodiscard]] column_view view() const;
329 
330  private:
331  std::shared_ptr<arrow_array_container>
332  container;
334  owned_columns_t view_columns;
335  column_view cached_view;
336 };
337 
346 class arrow_table {
347  public:
360  std::span<column_metadata const> metadata,
363 
376  arrow_table(ArrowSchema&& schema,
377  ArrowDeviceArray&& input,
380 
393  arrow_table(ArrowSchema&& schema,
394  ArrowArray&& input,
397 
409  arrow_table(ArrowArrayStream&& input,
412 
424  ArrowSchema* output,
427 
436  void to_arrow(ArrowDeviceArray* output,
437  ArrowDeviceType device_type = ARROW_DEVICE_CUDA,
440 
448  [[nodiscard]] table_view view() const;
449 
450  private:
451  std::shared_ptr<arrow_array_container>
452  container;
454  owned_columns_t view_columns;
455  table_view cached_view;
456 };
457 
458 } // namespace interop
459 
476  std::span<column_metadata const> metadata);
477 
503  cudf::table&& table,
506 
532  cudf::column&& col,
535 
564  cudf::table_view const& table,
567 
596  cudf::column_view const& col,
599 
621  cudf::table_view const& table,
624 
646  cudf::column_view const& col,
649 
666  cudf::strings_column_view const& col,
669 
687 std::unique_ptr<cudf::table> from_arrow(
688  ArrowSchema const* schema,
689  ArrowArray const* input,
692 
706 std::unique_ptr<cudf::column> from_arrow_column(
707  ArrowSchema const* schema,
708  ArrowArray const* input,
711 
732 std::unique_ptr<table> from_arrow_host(
733  ArrowSchema const* schema,
734  ArrowDeviceArray const* input,
737 
751 std::unique_ptr<table> from_arrow_stream(
752  ArrowArrayStream* input,
755 
769 std::unique_ptr<column> from_arrow_stream_column(
770  ArrowArrayStream* input,
773 
793 std::unique_ptr<column> from_arrow_host_column(
794  ArrowSchema const* schema,
795  ArrowDeviceArray const* input,
798 
837  ArrowSchema const* schema,
838  ArrowDeviceArray const* input,
841 
875  ArrowSchema const* schema,
876  ArrowDeviceArray const* input,
879  // end of group
881 } // 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:226
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:346
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:189
A set of cudf::column's of the same size.
Definition: table.hpp:29
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:175
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:141
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:182
std::unique_ptr< ArrowSchema, void(*)(ArrowSchema *)> unique_schema_t
typedef for a unique_ptr to an ArrowSchema with custom deleter
Definition: interop.hpp:129
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:135
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:110
std::optional< int32_t > precision
Resulting decimal precision of the column.
Definition: interop.hpp:113
column_metadata(std::string _name)
Construct a new column metadata object.
Definition: interop.hpp:121
std::vector< column_metadata > children_meta
Metadata of children of the column.
Definition: interop.hpp:114
std::string name
Name of the column.
Definition: interop.hpp:111
std::string timezone
Timezone of the column.
Definition: interop.hpp:112
functor for a custom deleter to a unique_ptr of table_view
Definition: interop.hpp:152
void operator()(ViewType *ptr) const
operator to delete the unique_ptr
Definition: interop.hpp:165
owned_columns_t owned_mem_
Owned columns that must be deleted.
Definition: interop.hpp:167
custom_view_deleter(owned_columns_t &&owned)
Construct a new custom view deleter object.
Definition: interop.hpp:158
Class definition for cudf::table.
Class definitions for (mutable)_table_view
Type declarations for libcudf.