interop.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-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 
17 #pragma once
18 
19 // We disable warning 611 because the `arrow::TableBatchReader` only partially
20 // override the `ReadNext` method of `arrow::RecordBatchReader::ReadNext`
21 // triggering warning 611-D from nvcc.
22 #ifdef __CUDACC__
23 #pragma nv_diag_suppress 611
24 #pragma nv_diag_suppress 2810
25 #endif
26 #include <rmm/resource_ref.hpp>
27 
28 #include <arrow/api.h>
29 #ifdef __CUDACC__
30 #pragma nv_diag_default 611
31 #pragma nv_diag_default 2810
32 #endif
33 
34 #include <cudf/column/column.hpp>
35 #include <cudf/detail/transform.hpp>
36 #include <cudf/table/table.hpp>
38 #include <cudf/types.hpp>
39 #include <cudf/utilities/span.hpp>
40 
41 #include <rmm/mr/device/per_device_resource.hpp>
42 
43 #include <utility>
44 
45 struct DLManagedTensor;
46 
47 struct ArrowDeviceArray;
48 
49 struct ArrowSchema;
50 
51 struct ArrowArray;
52 
53 namespace cudf {
77 std::unique_ptr<table> from_dlpack(
78  DLManagedTensor const* managed_tensor,
79  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
80 
99 DLManagedTensor* to_dlpack(
100  table_view const& input,
101  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
102  // end of group
104 
118  std::string name;
119  std::vector<column_metadata> children_meta;
120 
126  column_metadata(std::string _name) : name(std::move(_name)) {}
127  column_metadata() = default;
128 };
129 
150 std::shared_ptr<arrow::Table> to_arrow(table_view input,
151  std::vector<column_metadata> const& metadata = {},
152  rmm::cuda_stream_view stream = cudf::get_default_stream(),
153  arrow::MemoryPool* ar_mr = arrow::default_memory_pool());
154 
172 std::shared_ptr<arrow::Scalar> to_arrow(cudf::scalar const& input,
173  column_metadata const& metadata = {},
174  rmm::cuda_stream_view stream = cudf::get_default_stream(),
175  arrow::MemoryPool* ar_mr = arrow::default_memory_pool());
176 
181 using unique_schema_t = std::unique_ptr<ArrowSchema, void (*)(ArrowSchema*)>;
182 
187 using unique_device_array_t = std::unique_ptr<ArrowDeviceArray, void (*)(ArrowDeviceArray*)>;
188 
206 
232  cudf::table&& table,
233  rmm::cuda_stream_view stream = cudf::get_default_stream(),
234  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
235 
261  cudf::column&& col,
262  rmm::cuda_stream_view stream = cudf::get_default_stream(),
263  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
264 
293  cudf::table_view const& table,
294  rmm::cuda_stream_view stream = cudf::get_default_stream(),
295  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
296 
325  cudf::column_view const& col,
326  rmm::cuda_stream_view stream = cudf::get_default_stream(),
327  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
328 
337 std::unique_ptr<table> from_arrow(
338  arrow::Table const& input,
339  rmm::cuda_stream_view stream = cudf::get_default_stream(),
340  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
341 
350 std::unique_ptr<cudf::scalar> from_arrow(
351  arrow::Scalar const& input,
352  rmm::cuda_stream_view stream = cudf::get_default_stream(),
353  rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());
354 
370 std::unique_ptr<cudf::table> from_arrow(ArrowSchema const* schema,
371  ArrowArray const* input,
372  rmm::cuda_stream_view stream,
373  rmm::mr::device_memory_resource* mr);
374 
388 std::unique_ptr<cudf::column> from_arrow_column(ArrowSchema const* schema,
389  ArrowArray const* input,
390  rmm::cuda_stream_view stream,
391  rmm::mr::device_memory_resource* mr);
392 
411 std::unique_ptr<table> from_arrow_host(
412  ArrowSchema const* schema,
413  ArrowDeviceArray const* input,
414  rmm::cuda_stream_view stream = cudf::get_default_stream(),
415  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
416 
434 std::unique_ptr<column> from_arrow_host_column(
435  ArrowSchema const* schema,
436  ArrowDeviceArray const* input,
437  rmm::cuda_stream_view stream = cudf::get_default_stream(),
438  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
439 
444 using owned_columns_t = std::vector<std::unique_ptr<cudf::column>>;
445 
454 template <typename ViewType>
461  explicit custom_view_deleter(owned_columns_t&& owned) : owned_mem_{std::move(owned)} {}
462 
468  void operator()(ViewType* ptr) const { delete ptr; }
469 
471 };
472 
478  std::unique_ptr<cudf::table_view, custom_view_deleter<cudf::table_view>>;
479 
516  ArrowSchema const* schema,
517  ArrowDeviceArray const* input,
518  rmm::cuda_stream_view stream = cudf::get_default_stream(),
519  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
520 
526  std::unique_ptr<cudf::column_view, custom_view_deleter<cudf::column_view>>;
527 
559  ArrowSchema const* schema,
560  ArrowDeviceArray const* input,
561  rmm::cuda_stream_view stream = cudf::get_default_stream(),
562  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
563  // end of group
565 } // namespace 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:48
An owning class to represent a singular value.
Definition: scalar.hpp:49
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:187
A set of cudf::column's of the same size.
Definition: table.hpp:41
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:478
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:444
std::unique_ptr< table > from_arrow_host(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Create cudf::table from given ArrowDeviceArray input.
std::unique_ptr< column > from_arrow_host_column(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Create cudf::column from given ArrowDeviceArray input.
unique_table_view_t from_arrow_device(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Create cudf::table_view from given ArrowDeviceArray and ArrowSchema
unique_schema_t to_arrow_schema(cudf::table_view const &input, cudf::host_span< column_metadata const > metadata)
Create ArrowSchema from cudf table and metadata.
std::unique_ptr< cudf::column > from_arrow_column(ArrowSchema const *schema, ArrowArray const *input, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource *mr)
Create cudf::column from a given ArrowArray and ArrowSchema input.
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:526
std::unique_ptr< table > from_arrow(arrow::Table const &input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=rmm::mr::get_current_device_resource())
Create cudf::table from given arrow Table input.
std::unique_ptr< ArrowSchema, void(*)(ArrowSchema *)> unique_schema_t
typedef for a unique_ptr to an ArrowSchema with custom deleter
Definition: interop.hpp:181
unique_column_view_t from_arrow_device_column(ArrowSchema const *schema, ArrowDeviceArray const *input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Create cudf::column_view from given ArrowDeviceArray and ArrowSchema
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:187
std::shared_ptr< arrow::Table > to_arrow(table_view input, std::vector< column_metadata > const &metadata={}, rmm::cuda_stream_view stream=cudf::get_default_stream(), arrow::MemoryPool *ar_mr=arrow::default_memory_pool())
Create arrow::Table from cudf table input
unique_device_array_t to_arrow_device(cudf::table &&table, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=rmm::mr::get_current_device_resource())
Create ArrowDeviceArray from cudf table and metadata.
DLManagedTensor * to_dlpack(table_view const &input, rmm::device_async_resource_ref mr=rmm::mr::get_current_device_resource())
Convert a cudf table into a DLPack DLTensor.
std::unique_ptr< table > from_dlpack(DLManagedTensor const *managed_tensor, rmm::device_async_resource_ref mr=rmm::mr::get_current_device_resource())
Convert a DLPack DLTensor into a cudf table.
cuDF interfaces
Definition: aggregation.hpp:34
APIs for spans.
Detailed metadata information for arrow array.
Definition: interop.hpp:117
column_metadata(std::string _name)
Construct a new column metadata object.
Definition: interop.hpp:126
std::vector< column_metadata > children_meta
Metadata of children of the column.
Definition: interop.hpp:119
std::string name
Name of the column.
Definition: interop.hpp:118
functor for a custom deleter to a unique_ptr of table_view
Definition: interop.hpp:455
void operator()(ViewType *ptr) const
operator to delete the unique_ptr
Definition: interop.hpp:468
owned_columns_t owned_mem_
Owned columns that must be deleted.
Definition: interop.hpp:470
custom_view_deleter(owned_columns_t &&owned)
Construct a new custom view deleter object.
Definition: interop.hpp:461
C++20 std::span with reduced feature set.
Definition: span.hpp:228
Class definition for cudf::table.
Class definitions for (mutable)_table_view
Type declarations for libcudf.