pwrliang commented on code in PR #465: URL: https://github.com/apache/sedona-db/pull/465#discussion_r2696452190
########## rust/sedona-spatial-join-gpu/src/gpu_backend.rs: ########## @@ -0,0 +1,294 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::Result; +use arrow::compute::take; +use arrow_array::{Array, ArrayRef, BinaryArray, RecordBatch, UInt32Array}; +use arrow_schema::{DataType, Schema}; +use sedona_libgpuspatial::{GpuSpatialContext, SpatialPredicate}; +use std::sync::Arc; +use std::time::Instant; + +/// GPU backend for spatial operations +#[allow(dead_code)] +pub struct GpuBackend { + device_id: i32, + gpu_context: Option<GpuSpatialContext>, +} + +#[allow(dead_code)] +impl GpuBackend { + pub fn new(device_id: i32) -> Result<Self> { + Ok(Self { + device_id, + gpu_context: None, + }) + } + + pub fn init(&mut self) -> Result<()> { + // Initialize GPU context + log::info!( + "[GPU Join] Initializing GPU context (device {})", + self.device_id + ); + match GpuSpatialContext::new() { + Ok(mut ctx) => { + ctx.init().map_err(|e| { + crate::Error::GpuInit(format!("Failed to initialize GPU context: {e:?}")) + })?; + self.gpu_context = Some(ctx); + log::info!("[GPU Join] GPU context initialized successfully"); + Ok(()) + } + Err(e) => { + log::warn!("[GPU Join] GPU not available: {e:?}"); + // Gracefully handle GPU not being available + Ok(()) + } + } + } + + /// Convert BinaryView array to Binary array for GPU processing + /// OPTIMIZATION: Use Arrow's optimized cast instead of manual iteration + fn ensure_binary_array(array: &ArrayRef) -> Result<ArrayRef> { + match array.data_type() { + DataType::BinaryView => { + // OPTIMIZATION: Use Arrow's cast which is much faster than manual iteration + use arrow::compute::cast; + cast(array.as_ref(), &DataType::Binary).map_err(crate::Error::Arrow) + } Review Comment: I will change the C interface to let it accept the BinaryView, so the cast can be prevented, but the operation is very fast, though. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
