LMMC
Lamina Mathematic core
载入中...
搜索中...
未找到
tensor_nd_storage.c
浏览该文件的文档.
1
6
7#include <string.h>
8
9lmmc_status_t lmmc_tensor_nd_create(size_t ndim, const size_t* dims, lmmc_tensor_nd_t* out) {
10 return lmmc_tensor_nd_allocate_result(ndim, dims, out);
11}
12
14 size_t i;
15 size_t offset = 0;
16
17 if (!lmmc_tensor_nd_descriptor_is_valid(t) || idx == NULL || out == NULL) {
19 }
20
21 for (i = 0; i < t->ndim; ++i) {
22 if (idx[i] >= t->dims[i]) {
24 }
25 offset += idx[i] * t->strides[i];
26 }
27
28 LMMC_REAL_SET(out, &t->data[offset]);
29 return LMMC_STATUS_OK;
30}
31
33 size_t i;
34 size_t offset = 0;
35
36 if (!lmmc_tensor_nd_descriptor_is_valid(t) || idx == NULL) {
38 }
39
40 for (i = 0; i < t->ndim; ++i) {
41 if (idx[i] >= t->dims[i]) {
43 }
44 offset += idx[i] * t->strides[i];
45 }
46
47 LMMC_REAL_SET(&t->data[offset], &value);
48 return LMMC_STATUS_OK;
49}
50
52 if (t == NULL) return;
53
54 if (t->owns_data && t->data != NULL) {
55 size_t total;
56 if (lmmc_tensor_nd_layout(t->ndim, t->dims, NULL, &total) == LMMC_STATUS_OK) {
57 for (size_t i = 0; i < total; ++i) {
58 LMMC_REAL_CLEAR(&t->data[i]);
59 }
60 }
62 }
63
64 t->ndim = 0;
65 memset(t->dims, 0, sizeof(t->dims));
66 memset(t->strides, 0, sizeof(t->strides));
67 t->data = NULL;
68 t->owns_data = 0;
69}
#define LMMC_REAL_CLEAR(x)
释放一个 lmmc_real_t 变量(POD 类型为空操作)。
double lmmc_real_t
Public and internal LMMC real type.
#define LMMC_REAL_SET(dst, src)
将一个 lmmc_real_t 赋值到另一个:*dst = *src 。
void lmmc_memory_free(void *pointer)
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_OK
@ LMMC_STATUS_INVALID_ARGUMENT
void lmmc_tensor_nd_destroy(lmmc_tensor_nd_t *t)
销毁 N-D 张量(仅当 owns_data 为 1 时释放缓冲区).
lmmc_status_t lmmc_tensor_nd_set(lmmc_tensor_nd_t *t, const size_t *idx, lmmc_real_t value)
写入 N-D 张量元素.
lmmc_status_t lmmc_tensor_nd_get(const lmmc_tensor_nd_t *t, const size_t *idx, lmmc_real_t *out)
读取 N-D 张量元素.
lmmc_real_t * data
lmmc_status_t lmmc_tensor_nd_create(size_t ndim, const size_t *dims, lmmc_tensor_nd_t *out)
创建 N-D 张量,行优先连续存储,元素初始化为零.
size_t dims[LMMC_TENSOR_MAX_NDIM]
size_t strides[LMMC_TENSOR_MAX_NDIM]
N-D 张量结构.
N 维张量结果存储、索引遍历与乘积累加。