|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
2 | 2 | // SPDX-FileCopyrightText: Copyright the Vortex contributors |
3 | 3 |
|
4 | | -//! End-to-end CUDA tests for Vortex. |
| 4 | +//! This file is a simple C-compatible API that is called from the cudf-test-harness at CI time. |
| 5 | +//! |
| 6 | +//! The flow is |
| 7 | +//! |
| 8 | +//! * test harness calls `dlopen` in this library |
| 9 | +//! * invokes the `export_array` function to get back the device array |
| 10 | +//! * pass the arrays to `cudf`'s `from_arrow_device_column` |
| 11 | +//! * run some operations on the loaded column view |
| 12 | +//! * call `array->release()` to drop the data allocated from the Rust side |
| 13 | +
|
| 14 | +#![allow(clippy::unwrap_used, clippy::expect_used)] |
| 15 | + |
| 16 | +use std::sync::LazyLock; |
| 17 | + |
| 18 | +use arrow_schema::ffi::FFI_ArrowSchema; |
| 19 | +use futures::executor::block_on; |
| 20 | +use vortex::array::Array; |
| 21 | +use vortex::array::IntoArray; |
| 22 | +use vortex::array::arrays::DecimalArray; |
| 23 | +use vortex::array::arrays::PrimitiveArray; |
| 24 | +use vortex::array::arrays::StructArray; |
| 25 | +use vortex::array::arrays::VarBinViewArray; |
| 26 | +use vortex::array::session::ArraySession; |
| 27 | +use vortex::array::validity::Validity; |
| 28 | +use vortex::dtype::DecimalDType; |
| 29 | +use vortex::dtype::FieldNames; |
| 30 | +use vortex::expr::session::ExprSession; |
| 31 | +use vortex::io::session::RuntimeSession; |
| 32 | +use vortex::layout::session::LayoutSession; |
| 33 | +use vortex::metrics::VortexMetrics; |
| 34 | +use vortex::session::VortexSession; |
| 35 | +use vortex_cuda::CudaSession; |
| 36 | +use vortex_cuda::arrow::ArrowDeviceArray; |
| 37 | +use vortex_cuda::arrow::DeviceArrayExt; |
| 38 | + |
| 39 | +static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { |
| 40 | + VortexSession::empty() |
| 41 | + .with::<VortexMetrics>() |
| 42 | + .with::<ArraySession>() |
| 43 | + .with::<LayoutSession>() |
| 44 | + .with::<ExprSession>() |
| 45 | + .with::<RuntimeSession>() |
| 46 | + .with::<CudaSession>() |
| 47 | +}); |
| 48 | + |
| 49 | +#[unsafe(no_mangle)] |
| 50 | +pub extern "C" fn export_array( |
| 51 | + schema_ptr: &mut FFI_ArrowSchema, |
| 52 | + array_ptr: &mut ArrowDeviceArray, |
| 53 | +) -> i32 { |
| 54 | + let mut ctx = CudaSession::create_execution_ctx(&SESSION).unwrap(); |
| 55 | + |
| 56 | + let primitive = PrimitiveArray::from_iter(0u32..1024); |
| 57 | + let string = |
| 58 | + VarBinViewArray::from_iter_str((0..1024).map(|idx| format!("this is string {idx}"))); |
| 59 | + let decimal = DecimalArray::from_iter(0i64..1024, DecimalDType::new(19, 2)); |
| 60 | + |
| 61 | + let array = StructArray::new( |
| 62 | + FieldNames::from_iter(["prims", "strings", "decimals"]), |
| 63 | + vec![ |
| 64 | + primitive.into_array(), |
| 65 | + string.into_array(), |
| 66 | + decimal.into_array(), |
| 67 | + ], |
| 68 | + 1024, |
| 69 | + Validity::NonNullable, |
| 70 | + ) |
| 71 | + .into_array(); |
| 72 | + |
| 73 | + let data_type = array |
| 74 | + .dtype() |
| 75 | + .to_arrow_dtype() |
| 76 | + .expect("converting schema to Arrow DataType"); |
| 77 | + |
| 78 | + *schema_ptr = FFI_ArrowSchema::try_from(data_type).expect("data_type to FFI_ArrowSchema"); |
| 79 | + |
| 80 | + match block_on(array.export_device_array(&mut ctx)) { |
| 81 | + Ok(exported) => { |
| 82 | + *array_ptr = exported; |
| 83 | + 0 |
| 84 | + } |
| 85 | + Err(err) => { |
| 86 | + eprintln!("error in export_device_array: {err}"); |
| 87 | + 1 |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments