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