Skip to content

Commit a9970b3

Browse files
committed
Clarify CUDA stream ownership
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
1 parent b2b0cbd commit a9970b3

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

vortex-cuda/ffi/cinclude/vortex_cuda.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ int vx_cuda_array_export_arrow_device(const vx_session *session,
8282
vx_error **error_out);
8383

8484
/**
85-
* Scan a Vortex partition as an Arrow C Device stream.
85+
* Consume a Vortex partition and scan it as an Arrow C Device stream.
86+
*
87+
* This function takes ownership of `partition`. Callers must not free or reuse
88+
* it after calling this function, regardless of success or failure.
8689
*
8790
* On success returns 0 and writes an owned `ArrowDeviceArrayStream` to `out_stream`. The stream owns
88-
* the partition and must be released through its embedded Arrow `release` callback. Each produced
89-
* `ArrowDeviceArray` is exported on one CUDA device and must be released independently by the
90-
* consumer through its embedded `ArrowArray.release` callback.
91+
* the resulting scan iterator and must be released through its embedded Arrow `release` callback.
92+
* Each produced `ArrowDeviceArray` must be released through its embedded `ArrowArray.release`
93+
* callback.
9194
*
92-
* On error returns 1 and, when `error_out` is non-NULL, writes a `vx_error` (free with
93-
* `vx_error_free`). If `partition` is consumed by this call, callers must not free or reuse it.
95+
* On error returns 1 and writes a `vx_error` to `error_out` when non-NULL.
9496
*/
9597
int vx_cuda_partition_scan_arrow_device(const vx_session *session,
9698
vx_partition *partition,

vortex-cuda/ffi/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ pub unsafe extern "C-unwind" fn vx_cuda_array_export_arrow_device(
103103
})
104104
}
105105

106-
/// Scan a Vortex partition as an Arrow C Device stream.
106+
/// Consume a Vortex partition and scan it as an Arrow C Device stream.
107+
///
108+
/// This function takes ownership of `partition`. Callers must not free or reuse it after calling
109+
/// this function, regardless of success or failure.
107110
///
108111
/// On success returns `0` and writes an owned `ArrowDeviceArrayStream` to `out_stream`. The stream
109-
/// owns the partition and must be released through its embedded Arrow `release` callback. Each
110-
/// produced `ArrowDeviceArray` is exported on one CUDA device and is independently released by the
111-
/// consumer through the embedded `ArrowArray.release` callback.
112+
/// owns the resulting scan iterator. The caller must release the stream through its embedded Arrow
113+
/// `release` callback, and must release each produced `ArrowDeviceArray` through its embedded
114+
/// `ArrowArray.release` callback.
112115
///
113116
/// On error returns `1` and, when `error_out` is non-null, writes a `vx_error` (free with
114-
/// `vx_error_free`). If `partition` is consumed by this call, callers must not free or reuse it.
117+
/// `vx_error_free`).
115118
///
116119
/// # Safety
117120
///
@@ -129,9 +132,9 @@ pub unsafe extern "C-unwind" fn vx_cuda_partition_scan_arrow_device(
129132
vortex_ensure!(!partition.is_null(), "null vx_partition");
130133
vortex_ensure!(!out_stream.is_null(), "null ArrowDeviceArrayStream output");
131134

135+
let (dtype, array_iter) = unsafe { vx_partition_into_array_iter(partition) }?;
132136
let session = session_with_cuda(unsafe { vx_session_ref(session) }?)?;
133137
let ctx = CudaSession::create_execution_ctx(&session)?;
134-
let (dtype, array_iter) = unsafe { vx_partition_into_array_iter(partition) }?;
135138
let device_stream =
136139
vortex_cuda::export_device_array_stream_from_iter_with_ctx(array_iter, dtype, ctx);
137140

vortex-cuda/src/arrow/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ pub fn export_device_array_stream_from_iter(
473473
/// Export a blocking Vortex array iterator as an [`ArrowDeviceArrayStream`] using an existing CUDA
474474
/// execution context.
475475
///
476-
/// This is useful for FFI entry points that must finish all fallible CUDA initialization before
477-
/// consuming an owned input handle. Each yielded array must have `dtype`; every exported batch is
478-
/// validated to stay on the CUDA device selected by `ctx`.
476+
/// Use this helper when the caller has already selected the CUDA execution context that must drive
477+
/// the exported stream. Each yielded array must have `dtype`; every exported batch is validated to
478+
/// stay on the CUDA device selected by `ctx`.
479479
pub fn export_device_array_stream_from_iter_with_ctx(
480480
array_iter: impl Iterator<Item = VortexResult<ArrayRef>> + 'static,
481481
dtype: DType,

0 commit comments

Comments
 (0)