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_. 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_ (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_; 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_. 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_.
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_ / vm_ must stay valid until vm_ returns. vm_ 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_.
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
indexin[0, vm_, else NULL.device_ count()) -
auto vm_device_info_get(const vm_
device* dev, vm_ device_ info* out) -> int - Fill
outwith the device description. -
auto vm_device_supports(const vm_
device* dev, vm_ device_ op op, vm_ dtype dt) -> int - 1 if
devrunsopondt, else 0 (also for bad arguments). -
auto vm_buffer_create(vm_
device* dev, size_t bytes, vm_ buffer** out) -> int - Allocate
bytesof 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
bytesfrom hostsrcintodstatoffset. -
auto vm_queue_read(vm_
queue* q, const vm_ buffer* src, size_t offset, void* dst, size_t bytes) -> int - Copy
bytesfromsrcatoffsetinto hostdst. -
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]fornelements ofdtfrom 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 asvm_.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];alphais rounded todt.-
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
dtinvm_.device_ info.dtypes - #define VM_DEVICE_OP_COUNT
- Number of
vm_values; grows when ops are appended.device_ op
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_ 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_device_op
| Enumerators | |
|---|---|
| VM_DEVICE_OP_ARRAY_ADD |
|
| VM_DEVICE_OP_ARRAY_MUL |
|
| VM_DEVICE_OP_AXPY | |
| VM_DEVICE_OP_GEMM |
Row-major packed 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_ERR_CONFLICT |
Device name already registered. |
Typedef documentation
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_. 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_ 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_.
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_. Devices that cannot use host memory return VM_.
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_.
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_); an unknown version (VM_); a name already registered (VM_); a full table (VM_). ops and ctx must outlive the process. Devices are never removed, so an index stays valid; new devices are appended.