diff --git a/components/payloads/cu_sensor_payloads/src/image.rs b/components/payloads/cu_sensor_payloads/src/image.rs index 33a7363ee..39a21954c 100644 --- a/components/payloads/cu_sensor_payloads/src/image.rs +++ b/components/payloads/cu_sensor_payloads/src/image.rs @@ -83,7 +83,7 @@ where ); let raw_pixels: &[P::Subpixel] = self.buffer_handle.with_inner(|inner| unsafe { - let data = inner.slice(); + let data: &[u8] = inner; core::slice::from_raw_parts(data.as_ptr() as *const P::Subpixel, data.len()) }); ImageBuffer::from_raw(width, height, raw_pixels) @@ -102,7 +102,7 @@ where let size = width * height * C; let raw_pixels: &[T] = self.buffer_handle.with_inner(|inner| unsafe { - let data = inner.slice(); + let data: &[u8] = inner; core::slice::from_raw_parts( data.as_ptr() as *const T, data.len() / std::mem::size_of::(), diff --git a/components/sources/cu_v4l/src/lib.rs b/components/sources/cu_v4l/src/lib.rs index 6c0d7d57b..84bc1ea0c 100644 --- a/components/sources/cu_v4l/src/lib.rs +++ b/components/sources/cu_v4l/src/lib.rs @@ -312,9 +312,7 @@ mod linux_impl { for _ in 0..1000 { let _output = v4l.process(&clock, &mut msg); if let Some(frame) = msg.payload() { - let arc = frame.buffer_handle.inner_handle(); - let guard = arc.lock().unwrap(); - let slice = guard.slice(); + let slice: &[u8] = &frame.buffer_handle.lock().unwrap(); let arrow_buffer = ArrowBuffer::from(slice); let blob = Blob::from(arrow_buffer); let rerun_img = ImageBuffer::from(blob); diff --git a/components/sources/cu_v4l/src/v4lstream.rs b/components/sources/cu_v4l/src/v4lstream.rs index 41b5a0ccc..7213a9d40 100644 --- a/components/sources/cu_v4l/src/v4lstream.rs +++ b/components/sources/cu_v4l/src/v4lstream.rs @@ -194,7 +194,7 @@ impl CaptureStream<'_> for CuV4LStream { let buffer_handle = self.memory_pool.acquire().unwrap(); self.arena[index] = (Metadata::default(), Some(buffer_handle.clone())); let mut v4l2_buf = buffer_handle.with_inner_mut(|inner| { - let destination = inner.slice_mut(); + let destination: &mut [u8] = inner; v4l2_buffer { index: index as u32, diff --git a/core/cu29_runtime/Cargo.toml b/core/cu29_runtime/Cargo.toml index 3c57c1445..6a45e7c48 100644 --- a/core/cu29_runtime/Cargo.toml +++ b/core/cu29_runtime/Cargo.toml @@ -44,7 +44,4 @@ cudarc = { version = "0.13", optional = true, features = ["cuda-version-from-bui [features] default = [] -cuda = [] - -[target.'cfg(target_os = "linux")'.features] -cuda = ["cudarc"] +cuda = ["cudarc"] \ No newline at end of file diff --git a/core/cu29_runtime/src/pool.rs b/core/cu29_runtime/src/pool.rs index 95f2ac54a..7fc681745 100644 --- a/core/cu29_runtime/src/pool.rs +++ b/core/cu29_runtime/src/pool.rs @@ -43,61 +43,53 @@ where } } -impl CuHandleInner -where - T: ArrayLike, -{ - pub fn slice(&self) -> &[T::Element] { +impl Deref for CuHandleInner { + type Target = [T::Element]; + + fn deref(&self) -> &Self::Target { match self { CuHandleInner::Pooled(pooled) => pooled, CuHandleInner::Detached(detached) => detached, } } - pub fn slice_mut(&mut self) -> &mut [T::Element] { +} + +impl DerefMut for CuHandleInner { + fn deref_mut(&mut self) -> &mut Self::Target { match self { CuHandleInner::Pooled(pooled) => pooled.deref_mut(), CuHandleInner::Detached(detached) => detached, } } - - pub fn len(&self) -> usize { - match self { - CuHandleInner::Pooled(pooled) => pooled.len(), - CuHandleInner::Detached(detached) => detached.len(), - } - } - - pub fn is_empty(&self) -> bool { - match self { - CuHandleInner::Pooled(pooled) => pooled.is_empty(), - CuHandleInner::Detached(detached) => detached.is_empty(), - } - } } /// A shareable handle to an Array coming from a pool (either host or device). #[derive(Clone, Debug)] pub struct CuHandle(Arc>>); +impl Deref for CuHandle { + type Target = Arc>>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + impl CuHandle { /// Create a new CuHandle not part of a Pool (not for onboard usages, use pools instead) pub fn new_detached(inner: T) -> Self { CuHandle(Arc::new(Mutex::new(CuHandleInner::Detached(inner)))) } - pub fn inner_handle(&self) -> Arc>> { - self.0.clone() - } - /// Safely access the inner value, applying a closure to it. pub fn with_inner(&self, f: impl FnOnce(&CuHandleInner) -> R) -> R { - let lock = self.0.lock().unwrap(); + let lock = self.lock().unwrap(); f(&*lock) } /// Mutably access the inner value, applying a closure to it. pub fn with_inner_mut(&self, f: impl FnOnce(&mut CuHandleInner) -> R) -> R { - let mut lock = self.0.lock().unwrap(); + let mut lock = self.lock().unwrap(); f(&mut *lock) } } @@ -107,7 +99,7 @@ where ::Element: 'static, { fn encode(&self, encoder: &mut E) -> Result<(), EncodeError> { - let inner = self.0.lock().unwrap(); + let inner = self.lock().unwrap(); match inner.deref() { CuHandleInner::Pooled(pooled) => pooled.encode(encoder), CuHandleInner::Detached(detached) => detached.encode(encoder), @@ -177,8 +169,8 @@ impl CuPool for CuHostMemoryPool { fn copy_from>(&self, from: &mut CuHandle) -> CuHandle { let to_handle = self.acquire().expect("No available buffers in the pool"); - match from.0.lock().unwrap().deref() { - CuHandleInner::Detached(source) => match to_handle.0.lock().unwrap().deref_mut() { + match from.lock().unwrap().deref() { + CuHandleInner::Detached(source) => match to_handle.lock().unwrap().deref_mut() { CuHandleInner::Detached(destination) => { destination.copy_from_slice(source); } @@ -186,7 +178,7 @@ impl CuPool for CuHostMemoryPool { destination.copy_from_slice(source); } }, - CuHandleInner::Pooled(source) => match to_handle.0.lock().unwrap().deref_mut() { + CuHandleInner::Pooled(source) => match to_handle.lock().unwrap().deref_mut() { CuHandleInner::Detached(destination) => { destination.copy_from_slice(source); } @@ -234,7 +226,7 @@ mod cuda { fn deref(&self) -> &Self::Target { // Implement logic to return a slice - panic!("You need to copy data to host memory before accessing it."); + panic!("You need to copy data to host memory pool before accessing it."); } } @@ -243,7 +235,7 @@ mod cuda { E: ElementType, { fn deref_mut(&mut self) -> &mut Self::Target { - panic!("You need to copy data to host memory before accessing it."); + panic!("You need to copy data to host memory pool before accessing it."); } } @@ -270,6 +262,7 @@ mod cuda { } impl CuCudaPool { + #[allow(dead_code)] pub fn new( device: Arc, nb_buffers: usize, @@ -317,8 +310,8 @@ mod cuda { { let to_handle = self.acquire().expect("No available buffers in the pool"); - match from_handle.0.lock().unwrap().deref() { - CuHandleInner::Detached(from) => match to_handle.0.lock().unwrap().deref_mut() { + match from_handle.lock().unwrap().deref() { + CuHandleInner::Detached(from) => match to_handle.lock().unwrap().deref_mut() { CuHandleInner::Detached(CudaSliceWrapper(to)) => { self.device .htod_sync_copy_into(from, to) @@ -330,7 +323,7 @@ mod cuda { .expect("Failed to copy data to device"); } }, - CuHandleInner::Pooled(from) => match to_handle.0.lock().unwrap().deref_mut() { + CuHandleInner::Pooled(from) => match to_handle.lock().unwrap().deref_mut() { CuHandleInner::Detached(CudaSliceWrapper(to)) => { self.device .htod_sync_copy_into(from, to) @@ -364,9 +357,9 @@ mod cuda { .acquire() .expect("No available buffers in the pool"); - match device_handle.0.lock().unwrap().deref() { + match device_handle.lock().unwrap().deref() { CuHandleInner::Pooled(source) => { - match destination_handle.0.lock().unwrap().deref_mut() { + match destination_handle.lock().unwrap().deref_mut() { CuHandleInner::Pooled(ref mut destination) => { self.device .dtoh_sync_copy_into(source.as_cuda_slice(), destination) @@ -380,7 +373,7 @@ mod cuda { } } CuHandleInner::Detached(source) => { - match destination_handle.0.lock().unwrap().deref_mut() { + match destination_handle.lock().unwrap().deref_mut() { CuHandleInner::Pooled(ref mut destination) => { self.device .dtoh_sync_copy_into(source.as_cuda_slice(), destination) @@ -464,15 +457,14 @@ mod tests { let obj1 = pool.acquire().unwrap(); { let obj2 = pool.acquire().unwrap(); - - assert!(objs_as_slices.contains(&obj1.0.lock().unwrap().slice())); - assert!(objs_as_slices.contains(&obj2.0.lock().unwrap().slice())); + assert!(objs_as_slices.contains(&obj1.lock().unwrap().deref().deref())); + assert!(objs_as_slices.contains(&obj2.lock().unwrap().deref().deref())); assert_eq!(pool.space_left(), 1); } assert_eq!(pool.space_left(), 2); let obj3 = pool.acquire().unwrap(); - assert!(objs_as_slices.contains(&obj3.0.lock().unwrap().slice())); + assert!(objs_as_slices.contains(&obj3.lock().unwrap().deref().deref())); assert_eq!(pool.space_left(), 1); @@ -521,7 +513,7 @@ mod tests { let cuda_handle = { let mut initial_handle = host_pool.acquire().unwrap(); { - let mut inner_initial_handle = initial_handle.0.lock().unwrap(); + let mut inner_initial_handle = initial_handle.lock().unwrap(); if let CuHandleInner::Pooled(ref mut pooled) = *inner_initial_handle { pooled[0] = 42.0; } else { @@ -536,7 +528,7 @@ mod tests { // get it back to the host let final_handle = cuda_pool.copy_into(&cuda_handle, &mut host_pool); - let value = final_handle.0.lock().unwrap().slice()[0]; + let value = final_handle.lock().unwrap().deref().deref()[0]; assert_eq!(value, 42.0); } }