std1.0.0-beta-7 SafeC docs

std :: ml::gpu_webgpu

Struct std::WebGpuBuf

#include <std/ml/gpu_webgpu.h> source

struct WebGpuBuf {
    unsigned long handle;
    unsigned long bytes;
    int dtype;
}

Memory on the WebGPU device -- the WebGPU twin of gpu_mps.h's MpsBuf. 'handle' is the backend's device handle (a byte offset into its heap, the element type in its top two bits as device.h's DEV_TAG_*), 0 for an invalid (failed or freed) buffer; every op returns 0 for one. 'bytes' is its size, 'dtype' its element type: 0 = fp32, WEBGPU_FP16 or WEBGPU_BF16.

Ops on buffers are queued on the device's stream and return without waiting; read() and read_f32() wait for the work queued before them, and write() is ordered after it, so a buffer may be written, used and read back without explicit synchronization. free() doesn't wait either: work already queued still reads the memory before anything queued after it can reuse it. Owned by the caller: free() exactly once.

Fields

§unsigned long handlesource
§unsigned long bytessource
§int dtypesource

Methods

§int valid() constsource · definition

handle != 0

§void free()source · definition

back to the device heap; handle := 0

§int read([]unsigned char dst) constsource · definition

Copies the first dst.len (<= this.bytes) bytes into 'dst', once the work queued before has run. 1 = done.

§int read_f32([]float dst) constsource · definition

The first n elements as fp32 (16-bit ones converted on the CPU).

§int write([]const unsigned char src)source · definition

Copies src.len bytes (<= this.bytes) from 'src' into the buffer, after the work queued before. 1 = done.

§static struct WebGpuBuf alloc(unsigned long n, int dtype)source · definition

n uninitialized elements

§static struct WebGpuBuf upload([]const float host)source · definition
§static struct WebGpuBuf upload_f16([]const float host)source · definition

n fp32 values rounded on the CPU (float16.h's f32_to_fp16_bulk / f32_to_bf16_bulk) and stored as fp16 / bf16.

§static struct WebGpuBuf upload_bf16([]const float host)source · definition
§static struct WebGpuBuf upload_bits([]const unsigned short bits, int dtype)source · definition

n raw fp16 or bf16 values (dtype WEBGPU_FP16 / WEBGPU_BF16), copied as is.