include/vecmat/device.h file

Minimal device API: discovery, buffers, one in-order queue, a few ops.

A device is anything that runs vecmat ops on its own memory: the CPU, a GPU, an NPU, or an accelerator. Device 0 is always the built-in CPU device; other devices exist only after vm_device_register(). The CPU feature mask in cpu.h stays CPU-only. NVIDIA, AMD, Intel, and Arm Mali GPUs are vendor modules, not kernels in this library.

Status: every call that can fail returns VM_DEVICE_OK (0) or a negative VM_DEVICE_ERR_*. Argument errors are reported before anything is queued. A device may report a failed queued op from a later call or from vm_queue_finish(); the queue then keeps that error until it is destroyed.

Counts are int: n == 0 (or m == 0 / n == 0 for GEMM) is a no-op that reads nothing; a negative count is VM_DEVICE_ERR_INVAL. Byte ranges are checked against the buffer size without overflow; a range past the end, or an element count whose byte size overflows size_t, is VM_DEVICE_ERR_INVAL.

Ownership: buffers and queues belong to the caller and are destroyed by the caller. A buffer must outlive every queued op that uses it, and host memory passed to vm_queue_write() / vm_queue_read() must stay valid until vm_queue_finish() returns. vm_buffer_wrap() never takes ownership of the host memory.

Threads: discovery and registration are thread-safe. A queue is used by one thread at a time. Buffers may be shared between queues of the same device only when the caller orders them with vm_queue_finish().

Classes

struct vm_device_info
struct vm_device_cmd
One op, already validated by the core.
struct vm_device_ops
Backend function table.

Enums

enum vm_device_kind { VM_DEVICE_CPU = 0, VM_DEVICE_GPU = 1, VM_DEVICE_NPU = 2, VM_DEVICE_ACCEL = 3 }
enum vm_dtype { VM_DTYPE_F32 = 0, VM_DTYPE_F64 = 1 }
Element types.
enum vm_device_op { VM_DEVICE_OP_ARRAY_ADD = 0, VM_DEVICE_OP_ARRAY_MUL = 1, VM_DEVICE_OP_AXPY = 2, VM_DEVICE_OP_GEMM = 3 }

Typedefs

using vm_device = struct vm_device
Registered device; never freed.
using vm_buffer = struct vm_buffer
Device memory or wrapped host memory.
using vm_queue = struct vm_queue
In-order command queue.
using vm_device_kind = enum vm_device_kind
using vm_dtype = enum vm_dtype
Element types.
using vm_device_op = enum vm_device_op
using vm_device_info = struct vm_device_info

Functions

auto vm_device_count(void) -> int
Number of devices; at least 1 (the CPU device).
auto vm_device_get(int index) -> vm_device*
Device index in [0, vm_device_count()), else NULL.
auto vm_device_info_get(const vm_device* dev, vm_device_info* out) -> int
Fill out with the device description.
auto vm_device_supports(const vm_device* dev, vm_device_op op, vm_dtype dt) -> int
1 if dev runs op on dt, else 0 (also for bad arguments).
auto vm_buffer_create(vm_device* dev, size_t bytes, vm_buffer** out) -> int
Allocate bytes of device memory (contents undefined).
auto vm_buffer_wrap(vm_device* dev, void* host, size_t bytes, vm_buffer** out) -> int
Use caller memory as a buffer without copying or taking ownership.
void vm_buffer_destroy(vm_buffer* buf)
Release a buffer; NULL is a no-op.
auto vm_buffer_size(const vm_buffer* buf) -> size_t
Size in bytes; 0 for NULL.
auto vm_queue_create(vm_device* dev, vm_queue** out) -> int
Create an in-order queue on dev.
void vm_queue_destroy(vm_queue* q)
Finish pending work, then release the queue; NULL is a no-op.
auto vm_queue_finish(vm_queue* q) -> int
Wait for every queued op; returns the first error seen, if any.
auto vm_queue_write(vm_queue* q, vm_buffer* dst, size_t offset, const void* src, size_t bytes) -> int
Copy bytes from host src into dst at offset.
auto vm_queue_read(vm_queue* q, const vm_buffer* src, size_t offset, void* dst, size_t bytes) -> int
Copy bytes from src at offset into host dst.
auto vm_queue_copy(vm_queue* q, vm_buffer* dst, size_t dst_offset, const vm_buffer* src, size_t src_offset, size_t bytes) -> int
Copy between buffers of the queue's device; ranges may overlap.
auto vm_queue_array_add(vm_queue* q, vm_dtype dt, vm_buffer* res, const vm_buffer* a, const vm_buffer* b, int n) -> int
res[i] = a[i] + b[i] for n elements of dt from offset 0.
auto vm_queue_array_mul(vm_queue* q, vm_dtype dt, vm_buffer* res, const vm_buffer* a, const vm_buffer* b, int n) -> int
res[i] = a[i] * b[i]; same rules as vm_queue_array_add().
auto vm_queue_axpy(vm_queue* q, vm_dtype dt, vm_buffer* y, double alpha, const vm_buffer* x, int n) -> int
y[i] += alpha * x[i]; alpha is rounded to dt.
auto vm_queue_gemm(vm_queue* q, vm_dtype dt, int m, int n, int k, double alpha, const vm_buffer* A, const vm_buffer* B, double beta, vm_buffer* C) -> int
C = alpha * A * B + beta * C, row-major, packed, no transpose.

Defines

#define VM_DEVICE_MAX
Most devices, the built-in CPU device included.
#define VM_DTYPE_BIT(dt)
Bit of dt in vm_device_info.dtypes.
#define VM_DEVICE_OP_COUNT
Number of vm_device_op values; grows when ops are appended.

Device capability bits (vm_device_info.caps)

#define VM_DEVICE_CAP_HOST_MEMORY
The host can address buffer memory directly (CPU, unified memory).
#define VM_DEVICE_CAP_ASYNC
Queued ops may still be running when the queue call returns.

Backend interface

For device modules (for example in vecmat-vendors).

Applications only call vm_device_register() with the table a module gives them.

#define VM_DEVICE_OPS_VERSION
Current vm_device_ops.version.
using vm_device_cmd = struct vm_device_cmd
One op, already validated by the core.
using vm_device_ops = struct vm_device_ops
Backend function table.
auto vm_device_register(const vm_device_ops* ops, void* ctx) -> int
Add a device after the module has probed its hardware.

Status codes

enum (anonymous) { VM_DEVICE_OK = 0, VM_DEVICE_ERR_INVAL = -1, VM_DEVICE_ERR_NOMEM = -2, VM_DEVICE_ERR_UNSUPPORTED = -3, VM_DEVICE_ERR_DEVICE = -4, VM_DEVICE_ERR_FULL = -5, VM_DEVICE_ERR_CONFLICT = -6 }

Enum documentation

enum vm_dtype

Element types.

Explicit, so device ops do not depend on the TU width.

enum vm_device_op

Enumerators
VM_DEVICE_OP_ARRAY_ADD

res[i] = a[i] + b[i] (vm_array_add).

VM_DEVICE_OP_ARRAY_MUL

res[i] = a[i] * b[i] (vm_array_mul).

VM_DEVICE_OP_AXPY

y[i] += alpha * x[i] (vm_axpy).

VM_DEVICE_OP_GEMM

Row-major packed GEMM (vm_gemm).

enum (anonymous)

Enumerators
VM_DEVICE_OK

VM_DEVICE_ERR_INVAL

Bad argument, range, or handle mix.

VM_DEVICE_ERR_NOMEM

Host or device allocation failed.

VM_DEVICE_ERR_UNSUPPORTED

Op or dtype not offered; fall back.

VM_DEVICE_ERR_DEVICE

The device itself failed.

VM_DEVICE_ERR_FULL

VM_DEVICE_MAX devices registered.

VM_DEVICE_ERR_CONFLICT

Device name already registered.

Typedef documentation

typedef enum vm_dtype vm_dtype

Element types.

Explicit, so device ops do not depend on the TU width.

typedef struct vm_device_cmd vm_device_cmd

One op, already validated by the core.

Buffer fields are the backend's own native handles. alpha and beta are unused where the op has none. Counts are positive, except GEMM k, which may be 0 (then a and b must not be read).

typedef struct vm_device_ops vm_device_ops

Backend function table.

Every slot is required unless marked.

ctx is the pointer given to vm_device_register(). q is the native queue (NULL when queue_create is NULL). Ranges and counts are checked before a slot is called, and only supported (op, dtype) pairs reach submit. Slots return VM_DEVICE_OK or a negative code.

Function documentation

int vm_buffer_create(vm_device* dev, size_t bytes, vm_buffer** out)

Allocate bytes of device memory (contents undefined).

Aligned for any element type. bytes must be in [1, PTRDIFF_MAX], else VM_DEVICE_ERR_INVAL.

int vm_buffer_wrap(vm_device* dev, void* host, size_t bytes, vm_buffer** out)

Use caller memory as a buffer without copying or taking ownership.

host must stay valid until the buffer is destroyed and be aligned to the element size of every dtype used with it. bytes limits are as for vm_buffer_create(). Devices that cannot use host memory return VM_DEVICE_ERR_UNSUPPORTED.

void vm_buffer_destroy(vm_buffer* buf)

Release a buffer; NULL is a no-op.

Finish its queues first.

int vm_queue_array_add(vm_queue* q, vm_dtype dt, vm_buffer* res, const vm_buffer* a, const vm_buffer* b, int n)

res[i] = a[i] + b[i] for n elements of dt from offset 0.

Buffers may exactly alias each other, as in vm_array_add.

int vm_queue_gemm(vm_queue* q, vm_dtype dt, int m, int n, int k, double alpha, const vm_buffer* A, const vm_buffer* B, double beta, vm_buffer* C)

C = alpha * A * B + beta * C, row-major, packed, no transpose.

A is m×k, B is k×n, C is m×n. k == 0 scales C by beta without reading A or B. C must be a different buffer from A and B. alpha and beta are rounded to dt.

int vm_device_register(const vm_device_ops* ops, void* ctx)

Add a device after the module has probed its hardware.

Idempotent for the same (ops, ctx) pair. Rejects NULL ops, a struct_size smaller than this header's table, a missing required slot, or an empty name (VM_DEVICE_ERR_INVAL); an unknown version (VM_DEVICE_ERR_UNSUPPORTED); a name already registered (VM_DEVICE_ERR_CONFLICT); a full table (VM_DEVICE_ERR_FULL). ops and ctx must outlive the process. Devices are never removed, so an index stays valid; new devices are appended.