Devices
vecmat/ runs a few vecmat ops on a device: the CPU, a GPU, an NPU, or an accelerator. The CPU feature mask in cpu.h stays CPU-only; a GPU is never a CPU feature bit or a vm_ table.
Model
Device 0 is always the built-in CPU device. It needs no setup and runs the same kernels as direct calls, so its results match vm_, vm_, vm_, and row-major vm_ bit for bit. Other devices exist only after a module registers them.
Buffers are device memory (vm_) or caller memory used in place (vm_, if the device can). The host moves data with vm_ / vm_; vm_ moves it inside one device. There is no cross-device copy: read to the host, then write.
A queue is in order. Each op starts after the previous one. On an async device (VM_) a call may return before its op ran; vm_ waits for all of them. The CPU device runs each op inside its call.
Ops take an explicit vm_, not vm_, so one device call does not depend on the TU's width. The CPU device offers the widths the library was built with (VECMAT_FLOAT_ABI). vm_device_supports(dev, op, dt) answers before you queue.
| Op | Call | Meaning |
|---|---|---|
VM_ | vm_ | res[i] = a[i] + b[i] |
VM_ | vm_ | res[i] = a[i] * b[i] |
VM_ | vm_ | y[i] += alpha * x[i] |
VM_ | vm_ | C = alpha A B + beta C, row-major, packed |
Errors
Every fallible call returns VM_ (0) or a negative code:
| Code | When |
|---|---|
VM_ | NULL handle, negative count, range past a buffer, size overflow, buffers of another device, GEMM C equal to A or B, a misaligned wrapped pointer on the CPU device |
VM_ | allocation failed |
VM_ | op or dtype not offered, or vm_ on a device without it; nothing was queued, so fall back to the host call |
VM_ | the device failed |
VM_ / _CONFLICT | registration only |
Core argument errors are found before anything is queued and leave no state. An error returned from the device, including the CPU device rejecting a misaligned wrapped pointer, is returned by that call and stays on the queue: every later vm_ returns the first one. Make a new queue to continue.
A count of 0 is a no-op that reads nothing (GEMM: m == 0 or n == 0; k == 0 scales C by beta). A transfer of 0 bytes is a no-op and the host pointer may be NULL. Buffer sizes must be in [1, PTRDIFF_MAX]. Every byte range and every count × element size is checked without overflow.
if (vm_queue_gemm(q, VM_DTYPE_F32, m, n, k, 1.0, A, B, 0.0, C) == VM_DEVICE_ERR_UNSUPPORTED) { /* run vm_gemm on host copies instead */ }
Ownership and threads
- The caller creates and destroys buffers and queues. Devices are never removed, so a
vm_and its index stay valid.device * vm_never takes ownership. The memory must outlive the buffer and be aligned to the element size of each dtype used on it.buffer_ wrap - A buffer must outlive the queued ops that use it. Host memory passed to
vm_/queue_ write vm_must stay valid untilqueue_ read vm_.queue_ finish vm_finishes first.queue_ destroy - Discovery (
vm_,device_ count vm_, info, supports) anddevice_ get vm_are thread-safe. Use a queue from one thread at a time. Two queues may share a buffer only when you order them withdevice_ register vm_.queue_ finish vm_memory is aligned for any element type.buffer_ create
Device modules
The core has no threads, no dlopen, and no vendor headers. A module (in vecmat-vendors or the app) probes its hardware and, only if that works, calls vm_. A failed probe registers nothing.
vm_ starts with struct_size and version (VM_, currently 1). The core rejects a table smaller than its own (INVAL) or a different version (UNSUPPORTED). A larger table with this version still loads; new slots are appended. All slots are required except buffer_wrap and the queue_create / queue_destroy pair (both present, or both absent). Registration is idempotent for the same (ops, ctx); a second device with the same name is a conflict. At most VM_ (8) devices, CPU included.
The core checks every argument, range, and (op, dtype) pair first. A backend gets native handles and one vm_ per op in submit, so a new op adds an enum value, not a table slot. There is no priority: the app picks a device from vm_ (kind, dtypes, caps, memory).
How each kind fits:
- CPU (
VM_): device 0. It setsDEVICE_ CPU VM_, runs each op inside the call, and keeps no queue state.DEVICE_ CAP_ HOST_ MEMORY - GPU (
VM_): native buffers, and a native stream per queue when the device has queue state. SetDEVICE_ GPU VM_when a call can return before the op finishes. Host-visible memory also setsDEVICE_ CAP_ ASYNC VM_and may offerDEVICE_ CAP_ HOST_ MEMORY buffer_wrap. - NPU (
VM_) and accelerator (DEVICE_ NPU VM_): offer the ops they run. Anything else returnsDEVICE_ ACCEL UNSUPPORTEDand the caller falls back.
Bit 2 of caps is reserved. The core does not ship a GPU, NPU, or accelerator kernel. A device module probes its hardware and calls vm_ with its kind. That is not a bit in cpu.h, and it does not fill vm_. nvidia/ga10x is the layout example in Modules. A module offers the ops it can run. Anything else returns UNSUPPORTED and the caller uses the CPU device.