Skip to content

Commit

Permalink
Add to_arrow_device() functions that accept views (#15465)
Browse files Browse the repository at this point in the history
Adds the following new interop functions
```
unique_device_array_t to_arrow_device(cudf::table_view const& table, 
                                      rmm::cuda_stream_view stream,
                                      rmm::device_async_resource_ref mr);
unique_device_array_t to_arrow_device(cudf::column_view const& col, 
                                      rmm::cuda_stream_view stream,
                                      rmm::device_async_resource_ref mr);
```
Also refactors some common code with the ownership transfer version of these APIs.
And moves the `to_arrow_schema()` functions to a separate .cpp file.

Authors:
  - David Wendt (https://github.com/davidwendt)

Approvers:
  - Matt Topol (https://github.com/zeroshade)
  - Nghia Truong (https://github.com/ttnghia)
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #15465
  • Loading branch information
davidwendt authored Apr 22, 2024
1 parent 14854b1 commit 9fa247f
Show file tree
Hide file tree
Showing 7 changed files with 801 additions and 399 deletions.
2 changes: 2 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ add_library(
src/interop/from_arrow.cu
src/interop/to_arrow.cu
src/interop/to_arrow_device.cu
src/interop/to_arrow_schema.cpp
src/interop/to_arrow_utilities.cpp
src/interop/detail/arrow_allocator.cpp
src/io/avro/avro.cpp
src/io/avro/avro_gpu.cu
Expand Down
66 changes: 64 additions & 2 deletions cpp/include/cudf/interop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,70 @@ unique_device_array_t to_arrow_device(
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());

/**
* @brief Create `ArrowDeviceArray` from a table view
*
* Populates the C struct ArrowDeviceArray performing copies only if necessary.
* This wraps the data on the GPU device and gives a view of the table data
* to the ArrowDeviceArray struct. If the caller frees the data referenced by
* the table_view, using the returned object results in undefined behavior.
*
* After calling this function, the release callback on the returned ArrowDeviceArray
* must be called to clean up any memory created during conversion.
*
* @note For decimals, since the precision is not stored for them in libcudf
* it will be converted to an Arrow decimal128 with the widest-precision the cudf decimal type
* supports. For example, numeric::decimal32 will be converted to Arrow decimal128 of the precision
* 9 which is the maximum precision for 32-bit types. Similarly, numeric::decimal128 will be
* converted to Arrow decimal128 of the precision 38.
*
* Copies will be performed in the cases where cudf differs from Arrow:
* - BOOL8: Arrow uses a bitmap and cudf uses 1 byte per value
* - DECIMAL32 and DECIMAL64: Converted to Arrow decimal128
* - STRING: Arrow expects a single value int32 offset child array for empty strings columns
*
* @param table Input table
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used for any allocations during conversion
* @return ArrowDeviceArray which will have ownership of any copied data
*/
unique_device_array_t to_arrow_device(
cudf::table_view const& table,
rmm::cuda_stream_view stream = cudf::get_default_stream(),
rmm::device_async_resource_ref mr = rmm::mr::get_current_device_resource());

/**
* @brief Create `ArrowDeviceArray` from a column view
*
* Populates the C struct ArrowDeviceArray performing copies only if necessary.
* This wraps the data on the GPU device and gives a view of the column data
* to the ArrowDeviceArray struct. If the caller frees the data referenced by
* the column_view, using the returned object results in undefined behavior.
*
* After calling this function, the release callback on the returned ArrowDeviceArray
* must be called to clean up any memory created during conversion.
*
* @note For decimals, since the precision is not stored for them in libcudf
* it will be converted to an Arrow decimal128 with the widest-precision the cudf decimal type
* supports. For example, numeric::decimal32 will be converted to Arrow decimal128 of the precision
* 9 which is the maximum precision for 32-bit types. Similar, numeric::decimal128 will be
* converted to Arrow decimal128 of the precision 38.
*
* Copies will be performed in the cases where cudf differs from Arrow:
* - BOOL8: Arrow uses a bitmap and cudf uses 1 byte per value
* - DECIMAL32 and DECIMAL64: Converted to Arrow decimal128
* - STRING: Arrow expects a single value int32 offset child array for empty strings columns
*
* @param col Input column
* @param stream CUDA stream used for device memory operations and kernel launches
* @param mr Device memory resource used for any allocations during conversion
* @return ArrowDeviceArray which will have ownership of any copied data
*/
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 = rmm::mr::get_current_device_resource());

/**
* @brief Create `cudf::table` from given arrow Table input
*
Expand All @@ -266,7 +330,6 @@ unique_device_array_t to_arrow_device(
* @param mr Device memory resource used to allocate `cudf::table`
* @return cudf table generated from given arrow Table
*/

std::unique_ptr<table> from_arrow(
arrow::Table const& input,
rmm::cuda_stream_view stream = cudf::get_default_stream(),
Expand All @@ -280,7 +343,6 @@ std::unique_ptr<table> from_arrow(
* @param mr Device memory resource used to allocate `cudf::scalar`
* @return cudf scalar generated from given arrow Scalar
*/

std::unique_ptr<cudf::scalar> from_arrow(
arrow::Scalar const& input,
rmm::cuda_stream_view stream = cudf::get_default_stream(),
Expand Down
Loading

0 comments on commit 9fa247f

Please sign in to comment.