Skip to content
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

feat: add transmute for views #282

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/driver/safe/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
/// # Safety
/// 1. impl [Drop] to call all the corresponding resource cleanup methods
/// 2. Doesn't impl clone, so you can't have multiple device pointers
/// hanging around.

Check failure on line 32 in src/driver/safe/core.rs

View workflow job for this annotation

GitHub Actions / clippy

doc list item missing indentation
/// 3. Any allocations enforce that self is an [Arc], meaning no allocation
/// can outlive the [CudaDevice]

Check failure on line 34 in src/driver/safe/core.rs

View workflow job for this annotation

GitHub Actions / clippy

doc list item missing indentation
#[derive(Debug)]
pub struct CudaDevice {
pub(crate) cu_device: sys::CUdevice,
Expand Down Expand Up @@ -703,6 +703,21 @@
marker: PhantomData,
})
}

/// Reinterprets the slice of memory into a different type. `len` is the number
/// of elements of the new type `S` that are expected. If not enough bytes
/// are allocated in `self` for the view, then this returns `None`.
///
/// # Safety
/// This is unsafe because not the memory for the view may not be a valid interpretation
/// for the type `S`.
pub unsafe fn transmute<S>(&self, len: usize) -> Option<CudaView<'_, S>> {
(len * std::mem::size_of::<S>() <= self.num_bytes()).then_some(CudaView {
ptr: self.ptr,
len,
marker: PhantomData,
})
}
}

/// A mutable sub-view into a [CudaSlice] created by [CudaSlice::try_slice_mut()] or [CudaSlice::slice_mut()].
Expand Down Expand Up @@ -871,6 +886,21 @@
})
}

/// Reinterprets the slice of memory into a different type. `len` is the number
/// of elements of the new type `S` that are expected. If not enough bytes
/// are allocated in `self` for the view, then this returns `None`.
///
/// # Safety
/// This is unsafe because not the memory for the view may not be a valid interpretation
/// for the type `S`.
pub unsafe fn transmute<S>(&self, len: usize) -> Option<CudaView<'_, S>> {
(len * std::mem::size_of::<S>() <= self.num_bytes()).then_some(CudaView {
ptr: self.ptr,
len,
marker: PhantomData,
})
}

/// Creates a [CudaViewMut] at the specified offset from the start of `self`.
///
/// Panics if `range` and `0...self.len()` are not overlapping.
Expand Down Expand Up @@ -934,6 +964,21 @@
},
))
}

/// Reinterprets the slice of memory into a different type. `len` is the number
/// of elements of the new type `S` that are expected. If not enough bytes
/// are allocated in `self` for the view, then this returns `None`.
///
/// # Safety
/// This is unsafe because not the memory for the view may not be a valid interpretation
/// for the type `S`.
pub unsafe fn transmute_mut<S>(&mut self, len: usize) -> Option<CudaViewMut<'_, S>> {
(len * std::mem::size_of::<S>() <= self.num_bytes()).then_some(CudaViewMut {
ptr: self.ptr,
len,
marker: PhantomData,
})
}
}

trait RangeHelper: RangeBounds<usize> {
Expand Down Expand Up @@ -993,5 +1038,19 @@
assert!(unsafe { slice.transmute::<f32>(26) }.is_none());
assert!(unsafe { slice.transmute_mut::<f32>(25) }.is_some());
assert!(unsafe { slice.transmute_mut::<f32>(26) }.is_none());

{
let view = slice.slice(0..100);
assert!(unsafe { view.transmute::<f32>(25) }.is_some());
assert!(unsafe { view.transmute::<f32>(26) }.is_none());
}

{
let mut view_mut = slice.slice_mut(0..100);
assert!(unsafe { view_mut.transmute::<f32>(25) }.is_some());
assert!(unsafe { view_mut.transmute::<f32>(26) }.is_none());
assert!(unsafe { view_mut.transmute_mut::<f32>(25) }.is_some());
assert!(unsafe { view_mut.transmute_mut::<f32>(26) }.is_none());
}
}
}
Loading