[][src]Trait emu_core::boxed::IntoDeviceBoxed

pub trait IntoDeviceBoxed<T: ?Sized> {
    fn into_device_boxed(self) -> Result<DeviceBox<T>, NoDeviceError>;
fn into_device_boxed_mut(self) -> Result<DeviceBox<T>, NoDeviceError>; }

A trait for creating a DeviceBox<T> by consuming an object T

It is implemented for all T that is sized as well as iterators over T (for iterators, we just collect everything before uploading to a DeviceBox<T>) where T can be safely serialized. To ensure you can safely serialize your data, you should use #[derive(AsBytes)] from zerocopy. If you just want to see some examples of how to create a DeviceBox from types for which IntoDeviceBoxed is already implemented, then just go to the docs for DeviceBox.

Now, you can implement this for your own collection if you would like a way for your collection data structure to exist on the GPU.

#[repr(C)]
#[derive(AsBytes, FromBytes, Copy, Clone, Default, Debug, PartialEq)]
struct Molecule {
    position: f64,
    velocities: f64,
    forces: f64,
}

// aside: you're more likely to be implementing these traits for _general-purpose_ collections
// than something domain-specific like this
#[derive(Default)]
struct Molecules {
    num_molecules: usize,
    positions: Vec<f64>,
    velocities: Vec<f64>,
    forces: Vec<f64>,
}

impl Molecules {
    fn zero(num_molecules: usize) -> Self {
        Self {
            num_molecules,
            positions: vec![0.0; num_molecules],
            velocities: vec![0.0; num_molecules],
            forces: vec![0.0; num_molecules],
        }
    }
}

impl IntoDeviceBoxed<[Molecule]> for Molecules {
    fn into_device_boxed(self) -> Result<DeviceBox<[Molecule]>, NoDeviceError> {
        Ok((0..self.num_molecules).map(|idx| Molecule {
            position: self.positions[idx],
            velocities: self.velocities[idx],
            forces: self.forces[idx],
        }).into_device_boxed()?)
    }

    fn into_device_boxed_mut(self) -> Result<DeviceBox<[Molecule]>, NoDeviceError> {
        Ok((0..self.num_molecules).map(|idx| Molecule {
            position: self.positions[idx],
            velocities: self.velocities[idx],
            forces: self.forces[idx],
        }).into_device_boxed_mut()?)
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    futures::executor::block_on(assert_device_pool_initialized());
    let molecules = Molecules::zero(4096);
    let molecule_list_on_gpu = molecules.into_device_boxed_mut()?;
    assert_eq!(futures::executor::block_on(molecule_list_on_gpu.get())?,
        vec![Molecule::default(); 4096].into_boxed_slice());
    Ok(())
}

Required methods

fn into_device_boxed(self) -> Result<DeviceBox<T>, NoDeviceError>

fn into_device_boxed_mut(self) -> Result<DeviceBox<T>, NoDeviceError>

Loading content...

Implementors

impl<T: AsBytes> IntoDeviceBoxed<T> for T[src]

impl<T: AsBytes, U: Iterator<Item = T>> IntoDeviceBoxed<[T]> for U[src]

Loading content...