[][src]Trait emu_core::boxed::AsDeviceBoxed

pub trait AsDeviceBoxed<T: ?Sized> {
    fn as_device_boxed(&self) -> Result<DeviceBox<T>, NoDeviceError>;
fn as_device_boxed_mut(&self) -> Result<DeviceBox<T>, NoDeviceError>; }

A trait for creating a DeviceBox<T> from a reference to an object T

It is implemented for all T (even unsized) 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 AsDeviceBoxed is already implemented, then just go to the docs for DeviceBox.

You can implement this trait for your own collection if you would like to have your collection somehow sends its encapsulated data over to a DeviceBox on the GPU.

use {emu_core::prelude::*, emu_glsl::*, zerocopy::*};

// for some reason, we want to store Molecules as an array-of-structures on the GPU
// so we define this type for each element of the array
#[repr(C)]
#[derive(AsBytes, FromBytes, Copy, Clone, Default, Debug, PartialEq)]
struct Molecule {
    position: f64,
    velocities: f64,
    forces: f64,
}

// this is the collection we would like to be able to move to the GPU easily
#[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 AsDeviceBoxed<[Molecule]> for Molecules {
    fn as_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],
        }).collect::<Vec<Molecule>>().as_device_boxed()?)
    }

    fn as_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],
        }).collect::<Vec<Molecule>>().as_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: DeviceBox<[Molecule]> = molecules.as_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 as_device_boxed(&self) -> Result<DeviceBox<T>, NoDeviceError>

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

Loading content...

Implementors

impl<T: AsBytes + ?Sized, U: Borrow<T>> AsDeviceBoxed<T> for U[src]

Loading content...