Struct cust::memory::DeviceSlice
source · [−]#[repr(C)]pub struct DeviceSlice<T: DeviceCopy> { /* private fields */ }
Expand description
Fixed-size device-side slice.
Implementations
sourceimpl<T: DeviceCopy + Default + Clone> DeviceSlice<T>
impl<T: DeviceCopy + Default + Clone> DeviceSlice<T>
pub fn as_host_vec(&self) -> CudaResult<Vec<T>>
sourceimpl<T: DeviceCopy> DeviceSlice<T>
impl<T: DeviceCopy> DeviceSlice<T>
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the slice.
Examples
use cust::memory::*;
let a = DeviceBuffer::from_slice(&[1, 2, 3]).unwrap();
assert_eq!(a.len(), 3);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the slice has a length of 0.
Examples
use cust::memory::*;
let a : DeviceBuffer<u64> = unsafe { DeviceBuffer::uninitialized(0).unwrap() };
assert!(a.is_empty());
sourcepub fn as_device_ptr(&self) -> DevicePointer<T>
pub fn as_device_ptr(&self) -> DevicePointer<T>
Return a raw device-pointer to the slice’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage. The caller must also ensure that the pointer is not dereferenced by the CPU.
Examples:
use cust::memory::*;
let a = DeviceBuffer::from_slice(&[1, 2, 3]).unwrap();
println!("{:p}", a.as_ptr());
sourcepub unsafe fn from_raw_parts(
ptr: DevicePointer<T>,
len: usize
) -> DeviceSlice<T>
pub unsafe fn from_raw_parts(
ptr: DevicePointer<T>,
len: usize
) -> DeviceSlice<T>
Forms a slice from a DevicePointer
and a length.
The len
argument is the number of elements, not the number of bytes.
Safety
This function is unsafe as there is no guarantee that the given pointer is valid for len
elements, nor whether the lifetime inferred is a suitable lifetime for the returned slice.
Caveat
The lifetime for the returned slice is inferred from its usage. To prevent accidental misuse, it’s suggested to tie the lifetime to whatever source lifetime is safe in the context, such as by providing a helper function taking the lifetime of a host value for the slice or by explicit annotation.
Examples
use cust::memory::*;
let mut x = DeviceBuffer::from_slice(&[0u64, 1, 2, 3, 4, 5]).unwrap();
// Manually slice the buffer (this is not recommended!)
let ptr = unsafe { x.as_device_ptr().offset(1) };
let slice = unsafe { DeviceSlice::from_raw_parts(ptr, 2) };
let mut host_buf = [0u64, 0];
slice.copy_to(&mut host_buf).unwrap();
assert_eq!([1u64, 2], host_buf);
sourcepub unsafe fn from_raw_parts_mut(
ptr: DevicePointer<T>,
len: usize
) -> DeviceSlice<T>
pub unsafe fn from_raw_parts_mut(
ptr: DevicePointer<T>,
len: usize
) -> DeviceSlice<T>
Performs the same functionality as from_raw_parts
, except that a
mutable slice is returned.
Safety
This function is unsafe as there is no guarantee that the given pointer is valid for len
elements, nor whether the lifetime inferred is a suitable lifetime for the returned slice.
This function is unsafe as there is no guarantee that the given pointer is valid for len
elements, not whether the lifetime inferred is a suitable lifetime for the returned slice,
as well as not being able to provide a non-aliasing guarantee of the returned
mutable slice. data
must be non-null and aligned even for zero-length
slices as with from_raw_parts
.
See the documentation of from_raw_parts
for more details.
sourceimpl<T: DeviceCopy + Pod> DeviceSlice<T>
impl<T: DeviceCopy + Pod> DeviceSlice<T>
sourcepub fn set_8(&mut self, value: u8) -> CudaResult<()>
This is supported on crate feature bytemuck
only.
pub fn set_8(&mut self, value: u8) -> CudaResult<()>
bytemuck
only.Sets the memory range of this buffer to contiguous 8-bit
values of value
.
In total it will set sizeof<T> * len
values of value
contiguously.
sourcepub unsafe fn set_8_async(
&mut self,
value: u8,
stream: &Stream
) -> CudaResult<()>
This is supported on crate feature bytemuck
only.
pub unsafe fn set_8_async(
&mut self,
value: u8,
stream: &Stream
) -> CudaResult<()>
bytemuck
only.Sets the memory range of this buffer to contiguous 8-bit
values of value
asynchronously.
In total it will set sizeof<T> * len
values of value
contiguously.
Safety
This operation is async so it does not complete immediately, it uses stream-ordering semantics. Therefore you should not read/write from/to the memory range until the operation is complete.
sourcepub fn set_16(&mut self, value: u16) -> CudaResult<()>
This is supported on crate feature bytemuck
only.
pub fn set_16(&mut self, value: u16) -> CudaResult<()>
bytemuck
only.Sets the memory range of this buffer to contiguous 16-bit
values of value
.
In total it will set (sizeof<T> / 2) * len
values of value
contiguously.
Panics
Panics if:
self.ptr % 2 != 0
(the pointer is not aligned to at least 2 bytes).(size_of::<T>() * self.len) % 2 != 0
(the data size is not a multiple of 2 bytes)
sourcepub unsafe fn set_16_async(
&mut self,
value: u16,
stream: &Stream
) -> CudaResult<()>
This is supported on crate feature bytemuck
only.
pub unsafe fn set_16_async(
&mut self,
value: u16,
stream: &Stream
) -> CudaResult<()>
bytemuck
only.Sets the memory range of this buffer to contiguous 16-bit
values of value
asynchronously.
In total it will set (sizeof<T> / 2) * len
values of value
contiguously.
Panics
Panics if:
self.ptr % 2 != 0
(the pointer is not aligned to at least 2 bytes).(size_of::<T>() * self.len) % 2 != 0
(the data size is not a multiple of 2 bytes)
Safety
This operation is async so it does not complete immediately, it uses stream-ordering semantics. Therefore you should not read/write from/to the memory range until the operation is complete.
sourcepub fn set_32(&mut self, value: u32) -> CudaResult<()>
This is supported on crate feature bytemuck
only.
pub fn set_32(&mut self, value: u32) -> CudaResult<()>
bytemuck
only.Sets the memory range of this buffer to contiguous 32-bit
values of value
.
In total it will set (sizeof<T> / 4) * len
values of value
contiguously.
Panics
Panics if:
self.ptr % 4 != 0
(the pointer is not aligned to at least 4 bytes).(size_of::<T>() * self.len) % 4 != 0
(the data size is not a multiple of 4 bytes)
sourcepub unsafe fn set_32_async(
&mut self,
value: u32,
stream: &Stream
) -> CudaResult<()>
This is supported on crate feature bytemuck
only.
pub unsafe fn set_32_async(
&mut self,
value: u32,
stream: &Stream
) -> CudaResult<()>
bytemuck
only.Sets the memory range of this buffer to contiguous 32-bit
values of value
asynchronously.
In total it will set (sizeof<T> / 4) * len
values of value
contiguously.
Panics
Panics if:
self.ptr % 4 != 0
(the pointer is not aligned to at least 4 bytes).(size_of::<T>() * self.len) % 4 != 0
(the data size is not a multiple of 4 bytes)
Safety
This operation is async so it does not complete immediately, it uses stream-ordering semantics. Therefore you should not read/write from/to the memory range until the operation is complete.
sourceimpl<T: DeviceCopy + Zeroable> DeviceSlice<T>
impl<T: DeviceCopy + Zeroable> DeviceSlice<T>
sourcepub fn set_zero(&mut self) -> CudaResult<()>
pub fn set_zero(&mut self) -> CudaResult<()>
Sets this slice’s data to zero.
sourcepub unsafe fn set_zero_async(&mut self, stream: &Stream) -> CudaResult<()>
pub unsafe fn set_zero_async(&mut self, stream: &Stream) -> CudaResult<()>
Sets this slice’s data to zero asynchronously.
Safety
This operation is async so it does not complete immediately, it uses stream-ordering semantics. Therefore you should not read/write from/to the memory range until the operation is complete.
sourceimpl<T: DeviceCopy> DeviceSlice<T>
impl<T: DeviceCopy> DeviceSlice<T>
pub fn index<Idx: DeviceSliceIndex<T>>(&self, idx: Idx) -> DeviceSlice<T>
Trait Implementations
sourceimpl<T: DeviceCopy> AsyncCopyDestination<DeviceBuffer<T>> for DeviceSlice<T>
impl<T: DeviceCopy> AsyncCopyDestination<DeviceBuffer<T>> for DeviceSlice<T>
sourceunsafe fn async_copy_from(
&mut self,
val: &DeviceBuffer<T>,
stream: &Stream
) -> CudaResult<()>
unsafe fn async_copy_from(
&mut self,
val: &DeviceBuffer<T>,
stream: &Stream
) -> CudaResult<()>
Asynchronously copy data from source
. source
must be the same size as self
. Read more
sourceunsafe fn async_copy_to(
&self,
val: &mut DeviceBuffer<T>,
stream: &Stream
) -> CudaResult<()>
unsafe fn async_copy_to(
&self,
val: &mut DeviceBuffer<T>,
stream: &Stream
) -> CudaResult<()>
Asynchronously copy data to dest
. dest
must be the same size as self
. Read more
sourceimpl<T: DeviceCopy> AsyncCopyDestination<DeviceSlice<T>> for DeviceSlice<T>
impl<T: DeviceCopy> AsyncCopyDestination<DeviceSlice<T>> for DeviceSlice<T>
sourceunsafe fn async_copy_from(
&mut self,
val: &DeviceSlice<T>,
stream: &Stream
) -> CudaResult<()>
unsafe fn async_copy_from(
&mut self,
val: &DeviceSlice<T>,
stream: &Stream
) -> CudaResult<()>
Asynchronously copy data from source
. source
must be the same size as self
. Read more
sourceunsafe fn async_copy_to(
&self,
val: &mut DeviceSlice<T>,
stream: &Stream
) -> CudaResult<()>
unsafe fn async_copy_to(
&self,
val: &mut DeviceSlice<T>,
stream: &Stream
) -> CudaResult<()>
Asynchronously copy data to dest
. dest
must be the same size as self
. Read more
sourceimpl<T: DeviceCopy, I: AsRef<[T]> + AsMut<[T]> + ?Sized> AsyncCopyDestination<I> for DeviceSlice<T>
impl<T: DeviceCopy, I: AsRef<[T]> + AsMut<[T]> + ?Sized> AsyncCopyDestination<I> for DeviceSlice<T>
sourceunsafe fn async_copy_from(&mut self, val: &I, stream: &Stream) -> CudaResult<()>
unsafe fn async_copy_from(&mut self, val: &I, stream: &Stream) -> CudaResult<()>
Asynchronously copy data from source
. source
must be the same size as self
. Read more
sourceunsafe fn async_copy_to(&self, val: &mut I, stream: &Stream) -> CudaResult<()>
unsafe fn async_copy_to(&self, val: &mut I, stream: &Stream) -> CudaResult<()>
Asynchronously copy data to dest
. dest
must be the same size as self
. Read more
sourceimpl<T: Clone + DeviceCopy> Clone for DeviceSlice<T>
impl<T: Clone + DeviceCopy> Clone for DeviceSlice<T>
sourcefn clone(&self) -> DeviceSlice<T>
fn clone(&self) -> DeviceSlice<T>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<T: DeviceCopy> CopyDestination<DeviceBuffer<T>> for DeviceSlice<T>
impl<T: DeviceCopy> CopyDestination<DeviceBuffer<T>> for DeviceSlice<T>
sourcefn copy_from(&mut self, val: &DeviceBuffer<T>) -> CudaResult<()>
fn copy_from(&mut self, val: &DeviceBuffer<T>) -> CudaResult<()>
Copy data from source
. source
must be the same size as self
. Read more
sourcefn copy_to(&self, val: &mut DeviceBuffer<T>) -> CudaResult<()>
fn copy_to(&self, val: &mut DeviceBuffer<T>) -> CudaResult<()>
Copy data to dest
. dest
must be the same size as self
. Read more
sourceimpl<T: DeviceCopy> CopyDestination<DeviceSlice<T>> for DeviceSlice<T>
impl<T: DeviceCopy> CopyDestination<DeviceSlice<T>> for DeviceSlice<T>
sourcefn copy_from(&mut self, val: &DeviceSlice<T>) -> CudaResult<()>
fn copy_from(&mut self, val: &DeviceSlice<T>) -> CudaResult<()>
Copy data from source
. source
must be the same size as self
. Read more
sourcefn copy_to(&self, val: &mut DeviceSlice<T>) -> CudaResult<()>
fn copy_to(&self, val: &mut DeviceSlice<T>) -> CudaResult<()>
Copy data to dest
. dest
must be the same size as self
. Read more
sourceimpl<T: DeviceCopy, I: AsRef<[T]> + AsMut<[T]> + ?Sized> CopyDestination<I> for DeviceSlice<T>
impl<T: DeviceCopy, I: AsRef<[T]> + AsMut<[T]> + ?Sized> CopyDestination<I> for DeviceSlice<T>
sourceimpl<T: Debug + DeviceCopy> Debug for DeviceSlice<T>
impl<T: Debug + DeviceCopy> Debug for DeviceSlice<T>
sourceimpl<T: DeviceCopy> DeviceMemory for DeviceSlice<T>
impl<T: DeviceCopy> DeviceMemory for DeviceSlice<T>
sourcefn as_raw_ptr(&self) -> CUdeviceptr
fn as_raw_ptr(&self) -> CUdeviceptr
Get the raw cuda device pointer
sourcefn size_in_bytes(&self) -> usize
fn size_in_bytes(&self) -> usize
Get the size of the memory region in bytes
impl<T: Copy + DeviceCopy> Copy for DeviceSlice<T>
impl<T: Send + DeviceCopy> Send for DeviceSlice<T>
impl<T: Sync + DeviceCopy> Sync for DeviceSlice<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for DeviceSlice<T> where
T: RefUnwindSafe,
impl<T> Unpin for DeviceSlice<T>
impl<T> UnwindSafe for DeviceSlice<T> where
T: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more