|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Widening casts of decimal values buffers on the device. |
| 5 | +//! |
| 6 | +//! The Arrow export widens decimal values to the width that the Arrow schema fixes, and the |
| 7 | +//! Widen executor widens a narrow decimal child to the canonical width of its decimal dtype. Both |
| 8 | +//! launch the `decimal_cast` kernels through this module. |
| 9 | +//! |
| 10 | +//! These casts only widen. A narrowing cast can overflow, and to detect that on the device, the |
| 11 | +//! kernel would need an overflow flag that synchronizes with the host. So this module rejects a |
| 12 | +//! narrowing cast, and each caller decides how to handle values that are wider than its target. |
| 13 | +
|
| 14 | +use std::sync::Arc; |
| 15 | + |
| 16 | +use vortex::array::buffer::BufferHandle; |
| 17 | +use vortex::array::match_each_decimal_value_type; |
| 18 | +use vortex::buffer::Buffer; |
| 19 | +use vortex::dtype::DecimalType; |
| 20 | +use vortex::dtype::NativeDecimalType; |
| 21 | +use vortex::dtype::i256; |
| 22 | +use vortex::error::VortexResult; |
| 23 | +use vortex::error::vortex_ensure; |
| 24 | +use vortex::error::vortex_ensure_eq; |
| 25 | +use vortex::error::vortex_panic; |
| 26 | +use vortex_cuda_driver::DeviceRepr; |
| 27 | +use vortex_cuda_driver::PushKernelArg; |
| 28 | + |
| 29 | +use crate::CudaBufferExt; |
| 30 | +use crate::CudaDeviceBuffer; |
| 31 | +use crate::CudaExecutionCtx; |
| 32 | + |
| 33 | +/// Widen `len` decimal values from `values_type` to the wider `target_type` on the device. |
| 34 | +/// |
| 35 | +/// The values can be on the host or on the device, and the result is on the device. Each value is |
| 36 | +/// sign-extended, so the result holds the same values as the input. |
| 37 | +/// |
| 38 | +/// # Errors |
| 39 | +/// |
| 40 | +/// Returns an error if `target_type` is not wider than `values_type`, if the buffer does not |
| 41 | +/// contain `len` complete values, or if the copy to the device or the kernel launch fails. |
| 42 | +pub(crate) async fn widen_decimal_values( |
| 43 | + values: BufferHandle, |
| 44 | + values_type: DecimalType, |
| 45 | + target_type: DecimalType, |
| 46 | + len: usize, |
| 47 | + ctx: &mut CudaExecutionCtx, |
| 48 | +) -> VortexResult<BufferHandle> { |
| 49 | + vortex_ensure!( |
| 50 | + values_type < target_type, |
| 51 | + "a decimal cast on the GPU must widen to a type wider than {values_type}, got \ |
| 52 | + {target_type}" |
| 53 | + ); |
| 54 | + vortex_ensure_eq!(values.len(), len * values_type.byte_width()); |
| 55 | + |
| 56 | + let values = ctx.ensure_on_device(values).await?; |
| 57 | + |
| 58 | + match_each_decimal_value_type!(values_type, |S| { |
| 59 | + decimal_cast_to::<S>(values, target_type, len, ctx).await |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +/// Dispatch [`decimal_cast`] from `S` on the runtime `target_type`. |
| 64 | +async fn decimal_cast_to<S>( |
| 65 | + values: BufferHandle, |
| 66 | + target_type: DecimalType, |
| 67 | + len: usize, |
| 68 | + ctx: &mut CudaExecutionCtx, |
| 69 | +) -> VortexResult<BufferHandle> |
| 70 | +where |
| 71 | + S: NativeDecimalType + DeviceRepr, |
| 72 | +{ |
| 73 | + match target_type { |
| 74 | + DecimalType::I8 => vortex_panic!( |
| 75 | + "widen_decimal_values ensures a target wider than the values type, and no decimal \ |
| 76 | + type is narrower than i8, got {target_type}" |
| 77 | + ), |
| 78 | + DecimalType::I16 => decimal_cast::<S, i16>(values, len, ctx).await, |
| 79 | + DecimalType::I32 => decimal_cast::<S, i32>(values, len, ctx).await, |
| 80 | + DecimalType::I64 => decimal_cast::<S, i64>(values, len, ctx).await, |
| 81 | + DecimalType::I128 => decimal_cast::<S, i128>(values, len, ctx).await, |
| 82 | + DecimalType::I256 => decimal_cast::<S, i256>(values, len, ctx).await, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/// Launch the `decimal_cast_<S>_<D>` kernel, which casts device values of type `S` to type `D`. |
| 87 | +/// |
| 88 | +/// The cast must widen. A narrowing cast truncates each value with no overflow check. |
| 89 | +async fn decimal_cast<S, D>( |
| 90 | + values: BufferHandle, |
| 91 | + len: usize, |
| 92 | + ctx: &mut CudaExecutionCtx, |
| 93 | +) -> VortexResult<BufferHandle> |
| 94 | +where |
| 95 | + S: NativeDecimalType + DeviceRepr, |
| 96 | + D: NativeDecimalType + DeviceRepr, |
| 97 | +{ |
| 98 | + if len == 0 { |
| 99 | + return ctx |
| 100 | + .ensure_on_device(BufferHandle::new_host( |
| 101 | + Buffer::<D>::empty().into_byte_buffer(), |
| 102 | + )) |
| 103 | + .await; |
| 104 | + } |
| 105 | + |
| 106 | + let mut output_buffer = ctx.device_alloc::<D>(len)?; |
| 107 | + |
| 108 | + let values_view = values.cuda_view::<S>()?; |
| 109 | + let len_u64 = len as u64; |
| 110 | + let cuda_function = ctx.load_function_with_suffixes( |
| 111 | + "decimal_cast", |
| 112 | + &[&S::DECIMAL_TYPE.to_string(), &D::DECIMAL_TYPE.to_string()], |
| 113 | + )?; |
| 114 | + |
| 115 | + ctx.launch_kernel(&cuda_function, len, |args| { |
| 116 | + args.arg(&values_view).arg(&mut output_buffer).arg(&len_u64); |
| 117 | + })?; |
| 118 | + |
| 119 | + Ok(BufferHandle::new_device(Arc::new(CudaDeviceBuffer::new( |
| 120 | + output_buffer, |
| 121 | + )?))) |
| 122 | +} |
0 commit comments