device.c file
Classes
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
indexin[0, vm_, else NULL.device_ count()) -
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
outwith 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
devrunsopondt, 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
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. -
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
bytesfrom hostsrcintodstatoffset. -
auto vm_queue_read(vm_
queue* q, const vm_ buffer* src, const size_t offset, void* dst, const size_t bytes) -> int - Copy
bytesfromsrcatoffsetinto hostdst. -
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]fornelements ofdtfrom 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 asvm_.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];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.
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_); 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.
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.