pwrliang commented on code in PR #586:
URL: https://github.com/apache/sedona-db/pull/586#discussion_r2820055407


##########
c/sedona-libgpuspatial/src/lib.rs:
##########
@@ -15,6 +15,305 @@
 // specific language governing permissions and limitations
 // under the License.
 
-// Module declarations
+use arrow_schema::DataType;
+use geo_types::Rect;
+
+mod error;
 #[cfg(gpu_available)]
+mod libgpuspatial;
 mod libgpuspatial_glue_bindgen;
+mod options;
+mod predicate;
+
+pub use error::GpuSpatialError;
+pub use options::GpuSpatialOptions;
+pub use predicate::GpuSpatialRelationPredicate;
+pub use sys::{GpuSpatialIndex, GpuSpatialRefiner};
+
+#[cfg(gpu_available)]
+mod sys {
+    use super::libgpuspatial;
+    use super::*;
+    use libgpuspatial::GpuSpatialRuntimeWrapper;
+    use std::sync::{Arc, Mutex};
+
+    pub type Result<T> = std::result::Result<T, GpuSpatialError>;
+
+    // Global Runtime State
+    unsafe impl Send for GpuSpatialRuntimeWrapper {}
+    unsafe impl Sync for GpuSpatialRuntimeWrapper {}
+
+    static GLOBAL_GPUSPATIAL_RUNTIME: 
Mutex<Option<Arc<GpuSpatialRuntimeWrapper>>> =
+        Mutex::new(None);
+    /// Handles initialization of the GPU runtime.
+    pub struct SpatialContext {
+        runtime: Arc<GpuSpatialRuntimeWrapper>,
+    }
+
+    impl SpatialContext {
+        pub fn try_new(options: &GpuSpatialOptions) -> Result<Self> {
+            // Lock the mutex globally
+            let mut guard = GLOBAL_GPUSPATIAL_RUNTIME
+                .lock()
+                .map_err(|_| GpuSpatialError::Init("Global mutex 
poisoned".into()))?;
+
+            // Check if it already exists
+            if let Some(existing_runtime) = guard.as_ref() {
+                if existing_runtime.device_id != options.device_id {
+                    return Err(GpuSpatialError::Init(format!(
+                        "Runtime conflict: Initialized on Device {}, requested 
Device {}.",
+                        existing_runtime.device_id, options.device_id
+                    )));
+                }
+                // Return the existing one
+                return Ok(Self {
+                    runtime: existing_runtime.clone(),
+                });
+            }

Review Comment:
   I need `get_or_try_init`, which is unstable in the latest Rust stable 
release. Can we replace this later?
   ```rust
               let runtime_ref = GLOBAL_GPUSPATIAL_RUNTIME.get_or_try_init(|| {
                   let out_path = PathBuf::from(env!("OUT_DIR"));
                   let ptx_root = out_path.join("share/gpuspatial/shaders");
   
                   let ptx_root_str = ptx_root
                       .to_str()
                       .ok_or_else(|| GpuSpatialError::Init("Invalid PTX 
path".to_string()))?;
   
                   // Return the raw Wrapper here (not an Arc)
                   libgpuspatial::GpuSpatialRuntimeWrapper::try_new(
                       options.device_id,
                       ptx_root_str,
                       options.cuda_use_memory_pool,
                       options.cuda_memory_pool_init_percent,
                   )
               })?;
   ```



-- 
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]

Reply via email to