#[repr(C)]
pub struct DeviceSlice<T: DeviceCopy> { /* private fields */ }
Expand description

Fixed-size device-side slice.

Implementations

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);

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());

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());

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);

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.

This is supported on crate feature 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.

This is supported on crate feature 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.

This is supported on crate feature 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)
This is supported on crate feature 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.

This is supported on crate feature 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)
This is supported on crate feature 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.

Sets this slice’s data to zero.

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.

Trait Implementations

Asynchronously copy data from source. source must be the same size as self. Read more

Asynchronously copy data to dest. dest must be the same size as self. Read more

Asynchronously copy data from source. source must be the same size as self. Read more

Asynchronously copy data to dest. dest must be the same size as self. Read more

Asynchronously copy data from source. source must be the same size as self. Read more

Asynchronously copy data to dest. dest must be the same size as self. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Copy data from source. source must be the same size as self. Read more

Copy data to dest. dest must be the same size as self. Read more

Copy data from source. source must be the same size as self. Read more

Copy data to dest. dest must be the same size as self. Read more

Copy data from source. source must be the same size as self. Read more

Copy data to dest. dest must be the same size as self. Read more

Formats the value using the given formatter. Read more

Get the raw cuda device pointer

Get the size of the memory region in bytes

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.