Repository navigation
Execute Widen arrays on CUDA #11303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
connortsui20
wants to merge
1
commit into
ct/dbp-aggregates
Choose a base branch
from
ct/cuda-widen
base: ct/dbp-aggregates
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Execute Widen arrays on CUDA #11303
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #include "scalar_kernel.cuh" | ||
| #include "types.cuh" | ||
| #include <type_traits> | ||
|
|
||
| template <typename InputT, typename OutputT> | ||
| struct WidenOp { | ||
| static_assert(sizeof(InputT) < sizeof(OutputT)); | ||
| static_assert(std::is_signed_v<InputT> == std::is_signed_v<OutputT>); | ||
|
|
||
| __device__ OutputT operator()(InputT value) const { | ||
| return static_cast<OutputT>(value); | ||
| } | ||
| }; | ||
|
|
||
| #define GENERATE_WIDEN(input_suffix, InputT, output_suffix, OutputT) \ | ||
| extern "C" __global__ void widen_##input_suffix##_##output_suffix(const InputT *__restrict input, \ | ||
| OutputT *__restrict output, \ | ||
| uint64_t array_len) { \ | ||
| scalar_kernel(input, output, array_len, WidenOp<InputT, OutputT> {}); \ | ||
| } | ||
|
|
||
| GENERATE_WIDEN(i8, int8_t, i16, int16_t) | ||
| GENERATE_WIDEN(i8, int8_t, i32, int32_t) | ||
| GENERATE_WIDEN(i8, int8_t, i64, int64_t) | ||
| GENERATE_WIDEN(i16, int16_t, i32, int32_t) | ||
| GENERATE_WIDEN(i16, int16_t, i64, int64_t) | ||
| GENERATE_WIDEN(i32, int32_t, i64, int64_t) | ||
| GENERATE_WIDEN(u8, uint8_t, u16, uint16_t) | ||
| GENERATE_WIDEN(u8, uint8_t, u32, uint32_t) | ||
| GENERATE_WIDEN(u8, uint8_t, u64, uint64_t) | ||
| GENERATE_WIDEN(u16, uint16_t, u32, uint32_t) | ||
| GENERATE_WIDEN(u16, uint16_t, u64, uint64_t) | ||
| GENERATE_WIDEN(u32, uint32_t, u64, uint64_t) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! Widening casts of decimal values buffers on the device. | ||
| //! | ||
| //! The Arrow export widens decimal values to the width that the Arrow schema fixes, and the | ||
| //! Widen executor widens a narrow decimal child to the canonical width of its decimal dtype. Both | ||
| //! launch the `decimal_cast` kernels through this module. | ||
| //! | ||
| //! These casts only widen. A narrowing cast can overflow, and to detect that on the device, the | ||
| //! kernel would need an overflow flag that synchronizes with the host. So this module rejects a | ||
| //! narrowing cast, and each caller decides how to handle values that are wider than its target. | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use vortex::array::buffer::BufferHandle; | ||
| use vortex::array::match_each_decimal_value_type; | ||
| use vortex::buffer::Buffer; | ||
| use vortex::dtype::DecimalType; | ||
| use vortex::dtype::NativeDecimalType; | ||
| use vortex::dtype::i256; | ||
| use vortex::error::VortexResult; | ||
| use vortex::error::vortex_ensure; | ||
| use vortex::error::vortex_ensure_eq; | ||
| use vortex::error::vortex_panic; | ||
| use vortex_cuda_driver::DeviceRepr; | ||
| use vortex_cuda_driver::PushKernelArg; | ||
|
|
||
| use crate::CudaBufferExt; | ||
| use crate::CudaDeviceBuffer; | ||
| use crate::CudaExecutionCtx; | ||
|
|
||
| /// Widen `len` decimal values from `values_type` to the wider `target_type` on the device. | ||
| /// | ||
| /// The values can be on the host or on the device, and the result is on the device. Each value is | ||
| /// sign-extended, so the result holds the same values as the input. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if `target_type` is not wider than `values_type`, if the buffer does not | ||
| /// contain `len` complete values, or if the copy to the device or the kernel launch fails. | ||
| pub(crate) async fn widen_decimal_values( | ||
| values: BufferHandle, | ||
| values_type: DecimalType, | ||
| target_type: DecimalType, | ||
| len: usize, | ||
| ctx: &mut CudaExecutionCtx, | ||
| ) -> VortexResult<BufferHandle> { | ||
| vortex_ensure!( | ||
| values_type < target_type, | ||
| "a decimal cast on the GPU must widen to a type wider than {values_type}, got \ | ||
| {target_type}" | ||
| ); | ||
| vortex_ensure_eq!(values.len(), len * values_type.byte_width()); | ||
|
|
||
| let values = ctx.ensure_on_device(values).await?; | ||
|
|
||
| match_each_decimal_value_type!(values_type, |S| { | ||
| decimal_cast_to::<S>(values, target_type, len, ctx).await | ||
| }) | ||
| } | ||
|
|
||
| /// Dispatch [`decimal_cast`] from `S` on the runtime `target_type`. | ||
| async fn decimal_cast_to<S>( | ||
| values: BufferHandle, | ||
| target_type: DecimalType, | ||
| len: usize, | ||
| ctx: &mut CudaExecutionCtx, | ||
| ) -> VortexResult<BufferHandle> | ||
| where | ||
| S: NativeDecimalType + DeviceRepr, | ||
| { | ||
| match target_type { | ||
| DecimalType::I8 => vortex_panic!( | ||
| "widen_decimal_values ensures a target wider than the values type, and no decimal \ | ||
| type is narrower than i8, got {target_type}" | ||
| ), | ||
| DecimalType::I16 => decimal_cast::<S, i16>(values, len, ctx).await, | ||
| DecimalType::I32 => decimal_cast::<S, i32>(values, len, ctx).await, | ||
| DecimalType::I64 => decimal_cast::<S, i64>(values, len, ctx).await, | ||
| DecimalType::I128 => decimal_cast::<S, i128>(values, len, ctx).await, | ||
| DecimalType::I256 => decimal_cast::<S, i256>(values, len, ctx).await, | ||
| } | ||
| } | ||
|
|
||
| /// Launch the `decimal_cast_<S>_<D>` kernel, which casts device values of type `S` to type `D`. | ||
| /// | ||
| /// The cast must widen. A narrowing cast truncates each value with no overflow check. | ||
| async fn decimal_cast<S, D>( | ||
| values: BufferHandle, | ||
| len: usize, | ||
| ctx: &mut CudaExecutionCtx, | ||
| ) -> VortexResult<BufferHandle> | ||
| where | ||
| S: NativeDecimalType + DeviceRepr, | ||
| D: NativeDecimalType + DeviceRepr, | ||
| { | ||
| if len == 0 { | ||
| return ctx | ||
| .ensure_on_device(BufferHandle::new_host( | ||
| Buffer::<D>::empty().into_byte_buffer(), | ||
| )) | ||
| .await; | ||
| } | ||
|
|
||
| let mut output_buffer = ctx.device_alloc::<D>(len)?; | ||
|
|
||
| let values_view = values.cuda_view::<S>()?; | ||
| let len_u64 = len as u64; | ||
| let cuda_function = ctx.load_function_with_suffixes( | ||
| "decimal_cast", | ||
| &[&S::DECIMAL_TYPE.to_string(), &D::DECIMAL_TYPE.to_string()], | ||
| )?; | ||
|
|
||
| ctx.launch_kernel(&cuda_function, len, |args| { | ||
| args.arg(&values_view).arg(&mut output_buffer).arg(&len_u64); | ||
| })?; | ||
|
|
||
| Ok(BufferHandle::new_device(Arc::new(CudaDeviceBuffer::new( | ||
| output_buffer, | ||
| )?))) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unfamiliar with the GPU code but from my LLM conversations, it seems like we do not know if the optimizer can remove intermediate conversions? i.e. i8->i64->i16?