LMMC
Lamina Mathematic core
载入中...
搜索中...
未找到
tensor_nd_internal.h
浏览该文件的文档.
1
6#ifndef LMMC_TENSOR_ND_INTERNAL_H
7#define LMMC_TENSOR_ND_INTERNAL_H
8
9#include <string.h>
10#include "internal.h"
11#include "memory_bridge.h"
12#include "lmmc/tensor_nd.h"
13
14static inline lmmc_status_t lmmc_tensor_nd_layout(
15 size_t ndim, const size_t* dims, size_t* strides, size_t* total) {
16 size_t count = 1;
17 if (dims == NULL || total == NULL ||
18 ndim == 0 || ndim > LMMC_TENSOR_MAX_NDIM) {
20 }
21 for (size_t i = ndim; i > 0; --i) {
22 const size_t axis = i - 1;
23 if (dims[axis] == 0) {
25 }
26 if (strides != NULL) {
27 strides[axis] = count;
28 }
29 if (!lmmc_safe_mul_size(count, dims[axis], &count)) {
31 }
32 }
33 *total = count;
34 return LMMC_STATUS_OK;
35}
36
37static inline int lmmc_tensor_nd_descriptor_is_valid(const lmmc_tensor_nd_t* tensor) {
38 size_t total;
39 size_t logical_bytes;
40 size_t max_offset = 0;
41 size_t elements;
42 size_t bytes;
43 if (tensor == NULL || tensor->data == NULL ||
44 lmmc_tensor_nd_layout(tensor->ndim, tensor->dims, NULL, &total) != LMMC_STATUS_OK ||
45 !lmmc_safe_mul_size(total, sizeof(lmmc_real_t), &logical_bytes)) {
46 return 0;
47 }
48 for (size_t i = 0; i < tensor->ndim; ++i) {
49 size_t axis_offset;
50 if (tensor->strides[i] == 0 ||
51 !lmmc_safe_mul_size(tensor->dims[i] - 1, tensor->strides[i], &axis_offset) ||
52 !lmmc_safe_add_size(max_offset, axis_offset, &max_offset)) {
53 return 0;
54 }
55 }
56 return lmmc_safe_add_size(max_offset, 1, &elements) &&
57 lmmc_safe_mul_size(elements, sizeof(lmmc_real_t), &bytes) &&
58 (uintptr_t)bytes <= UINTPTR_MAX - (uintptr_t)tensor->data;
59}
60
62static inline lmmc_status_t lmmc_tensor_nd_allocate_result(
63 size_t ndim, const size_t* dims, lmmc_tensor_nd_t* out) {
64 size_t total;
65 size_t bytes;
66 lmmc_tensor_nd_t result = {0};
67 if (out == NULL ||
68 lmmc_tensor_nd_layout(ndim, dims, result.strides, &total) != LMMC_STATUS_OK ||
69 !lmmc_safe_mul_size(total, sizeof(lmmc_real_t), &bytes)) {
71 }
72 for (size_t i = 0; i < ndim; ++i) {
73 result.dims[i] = dims[i];
74 }
76 if (data == NULL) {
78 }
79 memset(data, 0, bytes);
80 for (size_t i = 0; i < total; ++i) {
81 LMMC_REAL_INIT(&data[i]);
82 }
83
84 result.ndim = ndim;
85 result.data = data;
86 result.owns_data = 1;
87 *out = result;
88 return LMMC_STATUS_OK;
89}
90
91static inline size_t lmmc_tensor_nd_offset(
92 size_t ndim, const size_t* strides, const size_t* idx) {
93 size_t offset = 0;
94 for (size_t i = 0; i < ndim; ++i) {
95 offset += idx[i] * strides[i];
96 }
97 return offset;
98}
99
101static inline int lmmc_tensor_nd_next_index(
102 size_t ndim, const size_t* dims, size_t* idx) {
103 while (ndim > 0) {
104 --ndim;
105 idx[ndim]++;
106 if (idx[ndim] < dims[ndim]) {
107 return 1;
108 }
109 idx[ndim] = 0;
110 }
111 return 0;
112}
113
114static inline void lmmc_tensor_nd_add_product(
115 lmmc_real_t* sum, const lmmc_real_t* a, const lmmc_real_t* b) {
116 lmmc_real_t va, vb, prod, new_sum;
117 LMMC_REAL_INIT(&va);
118 LMMC_REAL_INIT(&vb);
119 LMMC_REAL_INIT(&prod);
120 LMMC_REAL_INIT(&new_sum);
121 LMMC_REAL_SET(&va, a);
122 LMMC_REAL_SET(&vb, b);
123 LMMC_REAL_MUL(&prod, &va, &vb);
124 LMMC_REAL_ADD(&new_sum, sum, &prod);
125 LMMC_REAL_SET(sum, &new_sum);
126 LMMC_REAL_CLEAR(&va);
127 LMMC_REAL_CLEAR(&vb);
128 LMMC_REAL_CLEAR(&prod);
129 LMMC_REAL_CLEAR(&new_sum);
130}
131
132#endif
#define LMMC_REAL_CLEAR(x)
释放一个 lmmc_real_t 变量(POD 类型为空操作)。
#define LMMC_REAL_INIT(x)
初始化一个 lmmc_real_t 变量(POD 类型为空操作)。
#define LMMC_REAL_ADD(res, a, b)
计算 *res = *a + *b 。
double lmmc_real_t
Public and internal LMMC real type.
#define LMMC_REAL_SET(dst, src)
将一个 lmmc_real_t 赋值到另一个:*dst = *src 。
#define LMMC_REAL_MUL(res, a, b)
计算 *res = *a * *b 。
LMMC 内部使用的公共工具(仅源文件可见).
void * lmmc_memory_alloc(size_t size)
Private recoverable allocator for persistent LMMC ownership.
lmmc_status_t
LMMC 库统一返回的状态码.
@ LMMC_STATUS_OK
@ LMMC_STATUS_ALLOCATION_FAILED
@ LMMC_STATUS_INVALID_ARGUMENT
N-D 稠密张量数据结构、收缩与视图.
#define LMMC_TENSOR_MAX_NDIM
支持的最大张量维度.
lmmc_real_t * data
size_t dims[LMMC_TENSOR_MAX_NDIM]
size_t strides[LMMC_TENSOR_MAX_NDIM]
N-D 张量结构.