Struct cudarc::driver::safe::CudaSlice

source ·
pub struct CudaSlice<T> { /* private fields */ }
Expand description

Contains a reference counted pointer to both device and host memory allocated for type T.

§Host data

This owns the host data it is associated with. However it is possible to create device memory without having a corresponding host memory, so the host memory is actually Option.

§Reclaiming host data

To reclaim the host data for this device data, use CudaDevice::sync_reclaim(). This will perform necessary synchronization to ensure that the device data finishes copying over.

§Mutating device data

This can only be done by launching kernels via crate::driver::LaunchAsync which is implemented by CudaDevice. Pass &mut CudaSlice<T> if you want to mutate the rc, and &CudaSlice<T> otherwise.

Unfortunately, &CudaSlice<T> can still be mutated by the CudaFunction.

Implementations§

source§

impl<T> CudaSlice<T>

source

pub fn leak(self) -> CUdeviceptr

Takes ownership of the underlying sys::CUdeviceptr. It is up to the owner to free this value.

Drops the underlying host_buf if there is one.

source§

impl<T> CudaSlice<T>

source

pub fn device(&self) -> Arc<CudaDevice>

Get a clone of the underlying CudaDevice.

source§

impl<T: DeviceRepr> CudaSlice<T>

source

pub fn try_clone(&self) -> Result<Self, DriverError>

Allocates copy of self and schedules a device to device copy of memory.

source§

impl<T> CudaSlice<T>

source

pub fn slice(&self, range: impl RangeBounds<usize>) -> CudaView<'_, T>

Creates a CudaView at the specified offset from the start of self.

Panics if range.start >= self.len.

§Example
let mut slice = dev.alloc_zeros::<u8>(100).unwrap();
let mut view = slice.slice(0..50);
do_something(&view);

Like a normal slice, borrow checking prevents the underlying CudaSlice from being dropped.

let view = {
    let mut slice = dev.alloc_zeros::<u8>(100).unwrap();
    let mut view = slice.slice(0..50);
    // cannot return view, since it borrows from slice
    view
};
do_something(&view);
source

pub fn try_slice( &self, range: impl RangeBounds<usize>, ) -> Option<CudaView<'_, T>>

Fallible version of CudaSlice::slice().

source

pub unsafe fn transmute<S>(&self, len: usize) -> Option<CudaView<'_, S>>

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.

source§

impl<T> CudaSlice<T>

source

pub fn slice_mut( &mut self, range: impl RangeBounds<usize>, ) -> CudaViewMut<'_, T>

Creates a CudaViewMut at the specified offset from the start of self.

Panics if range and 0...self.len() are not overlapping.

§Example
let mut slice = dev.alloc_zeros::<u8>(100).unwrap();
let mut view = slice.slice_mut(0..50);
do_something(&mut view);

Like a normal mutable slice, borrow checking prevents the underlying CudaSlice from being dropped.

let mut view = {
    let mut slice = dev.alloc_zeros::<u8>(100).unwrap();
    let view = slice.slice_mut(0..50);
    // cannot return view, since it borrows from slice
    view
};
do_something(&mut view);

Like with normal mutable slices, one cannot mutably slice twice into the same CudaSlice:

let mut slice = dev.alloc_zeros::<u8>(100).unwrap();
let mut view1 = slice.slice_mut(0..50);
// cannot borrow twice from slice
let mut view2 = slice.slice_mut(50..100);
do_something(view1, view2);

If you need non-overlapping mutable views into a CudaSlice, you can use CudaSlice::split_at_mut().

source

pub fn try_slice_mut( &mut self, range: impl RangeBounds<usize>, ) -> Option<CudaViewMut<'_, T>>

Fallible version of CudaSlice::slice_mut

source

pub unsafe fn transmute_mut<S>( &mut self, len: usize, ) -> Option<CudaViewMut<'_, S>>

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.

source

pub fn split_at_mut( &mut self, mid: usize, ) -> (CudaViewMut<'_, T>, CudaViewMut<'_, T>)

Splits the CudaSlice into two at the given index, returning two CudaViewMut for the two halves.

Panics if mid > self.len.

This method can be used to create non-overlapping mutable views into a CudaSlice.

let mut slice = dev.alloc_zeros::<u8>(100).unwrap();
// split the slice into two non-overlapping, mutable views
let (mut view1, mut view2) = slice.split_at_mut(50);
do_something(view1, view2);
source

pub fn try_split_at_mut( &mut self, mid: usize, ) -> Option<(CudaViewMut<'_, T>, CudaViewMut<'_, T>)>

Fallible version of CudaSlice::split_at_mut.

Returns None if mid > self.len.

Trait Implementations§

source§

impl<T: DeviceRepr> Clone for CudaSlice<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for CudaSlice<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> DevicePtr<T> for CudaSlice<T>

source§

impl<T> DevicePtrMut<T> for CudaSlice<T>

source§

fn device_ptr_mut(&mut self) -> &mut CUdeviceptr

source§

impl<T: DeviceRepr> DeviceRepr for &CudaSlice<T>

source§

impl<T: DeviceRepr> DeviceRepr for &mut CudaSlice<T>

source§

impl<T> DeviceSlice<T> for CudaSlice<T>

source§

fn len(&self) -> usize

source§

fn num_bytes(&self) -> usize

source§

fn is_empty(&self) -> bool

source§

impl<T> Drop for CudaSlice<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: Clone + Default + DeviceRepr + Unpin> TryFrom<CudaSlice<T>> for Vec<T>

§

type Error = DriverError

The type returned in the event of a conversion error.
source§

fn try_from(value: CudaSlice<T>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T: Send> Send for CudaSlice<T>

source§

impl<T: Sync> Sync for CudaSlice<T>

Auto Trait Implementations§

§

impl<T> Freeze for CudaSlice<T>

§

impl<T> RefUnwindSafe for CudaSlice<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for CudaSlice<T>
where T: Unpin,

§

impl<T> UnwindSafe for CudaSlice<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.