Skip to main content

vector_add_gpu

Function vector_add_gpu 

Source
pub fn vector_add_gpu(
    ctx: &GpuContext,
    a: &[f32],
    b: &[f32],
) -> Result<Vec<f32>, GpuOpError>
Expand description

The wgpu compute path. Fallible: any wgpu-level problem returns Err so the crate::Executor can fall back to vector_add_cpu.

The dispatch shape, binding by binding, mirrors shaders/vector_add.wgsl:

  1. Upload a and b into read-only STORAGE buffers via create_buffer_init (bytemuck reinterprets &[f32] as the &[u8] wgpu wants — same bytes, no copy beyond the upload itself).
  2. Allocate an out STORAGE buffer with COPY_SRC (the shader writes it; we then copy it to a mappable staging buffer — you cannot map a STORAGE buffer directly for reading).
  3. Bind all three at group 0, bindings 0/1/2, matching the WGSL exactly.
  4. Dispatch ceil(n / 64) workgroups in x; the shader’s bounds guard handles the rounded-up tail invocations.
  5. Read back: copy out -> staging, submit, map_async + poll(Wait) to block until the GPU is done, then bytemuck-cast the mapped bytes back to f32. poll(Wait) is the synchronous join point — without it the map callback may not have fired yet.