Skip to main content

ComputeDispatcher

Struct ComputeDispatcher 

Source
pub struct ComputeDispatcher { /* private fields */ }
Expand description

Manages GPU buffers and dispatches parallel map/reduce operations.

This is a CPU-side simulation of GPU compute dispatch that uses a thread pool metaphor (sequential execution) for testing without a real GPU.

Implementations§

Source§

impl ComputeDispatcher

Source

pub fn new() -> Self

Create a new dispatcher with no buffers.

Source

pub fn create_buffer( &mut self, size: usize, initial_data: Option<&[f64]>, ) -> BufferId

Allocate a new buffer of size f64 elements, optionally pre-loaded with initial_data. Returns the new buffer’s BufferId.

Source

pub fn write_buffer( &mut self, id: BufferId, data: &[f64], ) -> Result<(), GpuError>

Write data into the buffer identified by id.

§Errors

Returns GpuError::InvalidBuffer if id is not registered.

Source

pub fn read_buffer(&self, id: BufferId) -> Result<Vec<f64>, GpuError>

Read the contents of the buffer identified by id.

§Errors

Returns GpuError::InvalidBuffer if id is not registered.

Source

pub fn num_buffers(&self) -> usize

Return the number of buffers currently managed.

Source

pub fn has_buffer(&self, id: BufferId) -> bool

Check if a buffer exists.

Source

pub fn buffer_size(&self, id: BufferId) -> Result<usize, GpuError>

Return the size of a buffer.

Source

pub fn destroy_buffer(&mut self, id: BufferId) -> Result<(), GpuError>

Destroy (remove) a buffer.

Source

pub fn copy_buffer( &mut self, src: BufferId, dst: BufferId, ) -> Result<(), GpuError>

Copy data from one buffer to another.

Source

pub fn dispatch_map( &mut self, buf_in: BufferId, buf_out: BufferId, f: impl Fn(f64) -> f64, ) -> Result<(), GpuError>

Dispatch a parallel map: out[i] = f(in[i]) for every element.

§Errors

Returns an error if either buffer is invalid or sizes differ.

Source

pub fn dispatch_map_indexed( &mut self, buf_in: BufferId, buf_out: BufferId, f: impl Fn(usize, f64) -> f64, ) -> Result<(), GpuError>

Dispatch a parallel map with index: out[i] = f(i, in[i]).

Source

pub fn dispatch_zip_map( &mut self, buf_a: BufferId, buf_b: BufferId, buf_out: BufferId, f: impl Fn(f64, f64) -> f64, ) -> Result<(), GpuError>

Dispatch a zip-map: out[i] = f(a[i], b[i]).

Source

pub fn dispatch_reduce( &self, buf: BufferId, f: impl Fn(f64, f64) -> f64, ) -> Result<f64, GpuError>

Dispatch a parallel reduce: folds all elements in buf using f.

Mimics a GPU tree-reduction (sequential here for correctness).

§Errors

Returns GpuError::InvalidBuffer or GpuError::EmptyBuffer.

Source

pub fn dispatch_sph_density( &mut self, pos_buf: BufferId, mass_buf: BufferId, h: f64, out_density_buf: BufferId, ) -> Result<(), GpuError>

Dispatch a mock SPH density kernel.

Computes a simplified SPH density estimate for each particle:

rho_i = sum_j m_j * W(|r_i - r_j|, h)

where W(r, h) = max(0, 1 - (r/h)^2) (simplified poly-6 mock).

Buffer layout (flat f64):

  • pos_buf[x0, y0, z0, x1, y1, z1, ...]
  • mass_buf[m0, m1, ...]
  • out_density_buf — written with [rho0, rho1, ...]
§Errors

Returns an error if any buffer id is invalid.

Source

pub fn dispatch_reduction_tree(&self, buf: BufferId) -> Result<f64, GpuError>

Dispatch a tree-based parallel reduction on a buffer.

Simulates a GPU tree reduction: repeatedly halves the active range, summing adjacent elements until one value remains.

Returns the reduced value (identity 0.0 for an empty buffer).

Source

pub fn dispatch_inclusive_scan( &mut self, buf_in: BufferId, buf_out: BufferId, ) -> Result<(), GpuError>

Dispatch an inclusive prefix scan (cumulative sum) on a buffer.

Writes out[i] = sum(in[0..=i]) into out_buf.

Uses a sequential Hillis-Steele-style scan for correctness.

Source

pub fn dispatch_radix_sort(&self, buf: BufferId) -> Result<Vec<f64>, GpuError>

Dispatch a 2-bit radix sort on a buffer of non-negative f64 values.

Values are cast to u64 (bit-cast) and sorted by their bits using counting sort passes with 2-bit digits. 32 passes cover all 64 bits. For non-negative IEEE 754 doubles the bit pattern order matches numeric order. Returns the sorted data as a new Vecf64` (input unchanged).

Trait Implementations§

Source§

impl Default for ComputeDispatcher

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.