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
impl ComputeDispatcher
Sourcepub fn create_buffer(
&mut self,
size: usize,
initial_data: Option<&[f64]>,
) -> BufferId
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.
Sourcepub fn write_buffer(
&mut self,
id: BufferId,
data: &[f64],
) -> Result<(), GpuError>
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.
Sourcepub fn read_buffer(&self, id: BufferId) -> Result<Vec<f64>, GpuError>
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.
Sourcepub fn num_buffers(&self) -> usize
pub fn num_buffers(&self) -> usize
Return the number of buffers currently managed.
Sourcepub fn has_buffer(&self, id: BufferId) -> bool
pub fn has_buffer(&self, id: BufferId) -> bool
Check if a buffer exists.
Sourcepub fn buffer_size(&self, id: BufferId) -> Result<usize, GpuError>
pub fn buffer_size(&self, id: BufferId) -> Result<usize, GpuError>
Return the size of a buffer.
Sourcepub fn destroy_buffer(&mut self, id: BufferId) -> Result<(), GpuError>
pub fn destroy_buffer(&mut self, id: BufferId) -> Result<(), GpuError>
Destroy (remove) a buffer.
Sourcepub fn copy_buffer(
&mut self,
src: BufferId,
dst: BufferId,
) -> Result<(), GpuError>
pub fn copy_buffer( &mut self, src: BufferId, dst: BufferId, ) -> Result<(), GpuError>
Copy data from one buffer to another.
Sourcepub fn dispatch_map(
&mut self,
buf_in: BufferId,
buf_out: BufferId,
f: impl Fn(f64) -> f64,
) -> Result<(), GpuError>
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.
Sourcepub fn dispatch_map_indexed(
&mut self,
buf_in: BufferId,
buf_out: BufferId,
f: impl Fn(usize, f64) -> f64,
) -> Result<(), GpuError>
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]).
Sourcepub fn dispatch_zip_map(
&mut self,
buf_a: BufferId,
buf_b: BufferId,
buf_out: BufferId,
f: impl Fn(f64, f64) -> f64,
) -> Result<(), GpuError>
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]).
Sourcepub fn dispatch_reduce(
&self,
buf: BufferId,
f: impl Fn(f64, f64) -> f64,
) -> Result<f64, GpuError>
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.
Sourcepub fn dispatch_sph_density(
&mut self,
pos_buf: BufferId,
mass_buf: BufferId,
h: f64,
out_density_buf: BufferId,
) -> Result<(), GpuError>
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.
Sourcepub fn dispatch_reduction_tree(&self, buf: BufferId) -> Result<f64, GpuError>
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).
Sourcepub fn dispatch_inclusive_scan(
&mut self,
buf_in: BufferId,
buf_out: BufferId,
) -> Result<(), GpuError>
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.
Sourcepub fn dispatch_radix_sort(&self, buf: BufferId) -> Result<Vec<f64>, GpuError>
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§
Auto Trait Implementations§
impl Freeze for ComputeDispatcher
impl RefUnwindSafe for ComputeDispatcher
impl Send for ComputeDispatcher
impl Sync for ComputeDispatcher
impl Unpin for ComputeDispatcher
impl UnsafeUnpin for ComputeDispatcher
impl UnwindSafe for ComputeDispatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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