src/features/device.c file

Classes

struct vm_device
struct vm_buffer
struct vm_queue

Functions

void vm_array_add32(vm_float32_t* res, const vm_float32_t* a, const vm_float32_t* b, int n)
void vm_array_mul32(vm_float32_t* res, const vm_float32_t* a, const vm_float32_t* b, int n)
void vm_axpy32(vm_float32_t* y, vm_float32_t alpha, const vm_float32_t* x, int n)
void vm_gemm32(vm_float32_t* C, int ldc, const vm_float32_t* A, int lda, const vm_float32_t* B, int ldb, int M, int N, int K, vm_float32_t alpha, vm_float32_t beta, bool transA, bool transB, vm_layout_t layout)
void vm_array_add64(vm_float64_t* res, const vm_float64_t* a, const vm_float64_t* b, int n)
void vm_array_mul64(vm_float64_t* res, const vm_float64_t* a, const vm_float64_t* b, int n)
void vm_axpy64(vm_float64_t* y, vm_float64_t alpha, const vm_float64_t* x, int n)
void vm_gemm64(vm_float64_t* C, int ldc, const vm_float64_t* A, int lda, const vm_float64_t* B, int ldb, int M, int N, int K, vm_float64_t alpha, vm_float64_t beta, bool transA, bool transB, vm_layout_t layout)
static void cpu_info(void* ctx, vm_device_info* out)
static auto cpu_supports(void* ctx, vm_device_op op, vm_dtype dt) -> int
static auto cpu_buffer_create(void* ctx, size_t bytes, void** native) -> int
static auto cpu_buffer_wrap(void* ctx, void* host, size_t bytes, void** native) -> int
static void cpu_buffer_destroy(void* ctx, void* native, int wrapped)
static auto cpu_write(void* ctx, void* q, void* dst, size_t offset, const void* src, size_t bytes) -> int
static auto cpu_read(void* ctx, void* q, const void* src, size_t offset, void* dst, size_t bytes) -> int
static auto cpu_copy(void* ctx, void* q, void* dst, size_t dst_offset, const void* src, size_t src_offset, size_t bytes) -> int
static auto cpu_aligned(const void* p, size_t align) -> bool
static auto cpu_submit(void* ctx, void* q, const vm_device_cmd* c) -> int
static auto cpu_finish(void* ctx, void* q) -> int
auto vm_device_count(void) -> int
Number of devices; at least 1 (the CPU device).
auto vm_device_get(const int index) -> vm_device*
Device index in [0, vm_device_count()), else NULL.
static auto ops_complete(const vm_device_ops* o) -> bool
auto vm_device_register(const vm_device_ops* ops, void* ctx) -> int
Add a device after the module has probed its hardware.
auto vm_device_info_get(const vm_device* dev, vm_device_info* out) -> int
Fill out with the device description.
static auto dtype_valid(const vm_dtype dt) -> bool
auto vm_device_supports(const vm_device* dev, const vm_device_op op, const vm_dtype dt) -> int
1 if dev runs op on dt, else 0 (also for bad arguments).
static auto buffer_new(vm_device* dev, void* host, const size_t bytes, vm_buffer** out) -> int
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.
static auto queue_note(vm_queue* q, int status) -> int
auto vm_queue_finish(vm_queue* q) -> int
Wait for every queued op; returns the first error seen, if any.
void vm_queue_destroy(vm_queue* q)
Finish pending work, then release the queue; NULL is a no-op.
static auto range_ok(const vm_buffer* buf, const size_t offset, const size_t bytes) -> bool
static auto same_dev(const vm_queue* q, const vm_buffer* buf) -> bool
auto vm_queue_write(vm_queue* q, vm_buffer* dst, const size_t offset, const void* src, const size_t bytes) -> int
Copy bytes from host src into dst at offset.
auto vm_queue_read(vm_queue* q, const vm_buffer* src, const size_t offset, void* dst, const size_t bytes) -> int
Copy bytes from src at offset into host dst.
auto vm_queue_copy(vm_queue* q, vm_buffer* dst, const size_t dst_offset, const vm_buffer* src, const size_t src_offset, const size_t bytes) -> int
Copy between buffers of the queue's device; ranges may overlap.
static auto dtype_size(const vm_dtype dt) -> size_t
static auto fits(const vm_buffer* buf, const size_t rows, const size_t cols, const vm_dtype dt) -> bool
static auto op_begin(const vm_queue* q, const vm_device_op op, const vm_dtype dt) -> int
static auto submit(vm_queue* q, const vm_device_cmd* cmd) -> int
static auto queue_binary(vm_queue* q, const vm_device_op op, const vm_dtype dt, vm_buffer* res, const vm_buffer* a, const vm_buffer* b, const int n) -> int
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, const vm_dtype dt, vm_buffer* res, const vm_buffer* a, const vm_buffer* b, const int n) -> int
res[i] = a[i] * b[i]; same rules as vm_queue_array_add().
auto vm_queue_axpy(vm_queue* q, const vm_dtype dt, vm_buffer* y, const double alpha, const vm_buffer* x, const 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.

Variables

static const vm_device_ops cpu_ops
static vm_device devices
static atomic_int device_count
static atomic_flag device_lock

Function documentation

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.

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.