Skip to main content

ThreadIndexedSlice

Struct ThreadIndexedSlice 

Source
pub struct ThreadIndexedSlice<'a, T> { /* private fields */ }
Expand description

Safely pass a list to a kernel and let every thread mutably access one element of the list.

The size of the list needs to be equal to the number of launched threads otherwise launching the kernel panics.

§Example

use gpu_kernel::{kernel, ThreadIndexedSlice};

gpu_kernel::kernel_lib!();

#[kernel]
fn kernel(elem: ThreadIndexedSlice<'_, i32>) {
    // Every thread writes two into the element assigned to this thread
    *elem.get_mut() = 2;
}

fn main() {
    // Needs the same length as the number of threads launched
    let mut data = vec![0; 10];
    kernel.launch(
        gpu_kernel::LaunchConfig::new()
            .threads_per_workgroup([data.len() as u32, 1, 1])
            .workgroups([1, 1, 1]),
        &mut data,
    );
}

Implementations§

Source§

impl<'a, T> ThreadIndexedSlice<'a, T>

Source

pub unsafe fn from_ptr(ptr: *mut T) -> Self

Constructs a ThreadIndexedSlice from a raw base pointer.

§Safety
  • ptr must point to at least number of GPU threads consecutive properly initialized values of type T.
  • No constant or mutable reference to the data must exist for the lifetime of this struct.
Source

pub fn as_mut_base_ptr(&mut self) -> *mut T

Returns the base pointer of the wrapped list.

Source

pub fn get(&self) -> &T

Get a reference to the element for the current thread index.

Source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the element for the current thread index.

Auto Trait Implementations§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.