LMMC
Lamina Mathematic core
Toggle main menu visibility
载入中...
搜索中...
未找到
tensor_nd_storage.c
浏览该文件的文档.
1
5
#include "
internal/tensor_nd_internal.h
"
6
7
#include <string.h>
8
9
lmmc_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
13
lmmc_status_t
lmmc_tensor_nd_get
(
const
lmmc_tensor_nd_t
* t,
const
size_t
* idx,
lmmc_real_t
* out) {
14
size_t
i;
15
size_t
offset = 0;
16
17
if
(!lmmc_tensor_nd_descriptor_is_valid(t) || idx == NULL || out == NULL) {
18
return
LMMC_STATUS_INVALID_ARGUMENT
;
19
}
20
21
for
(i = 0; i < t->
ndim
; ++i) {
22
if
(idx[i] >= t->
dims
[i]) {
23
return
LMMC_STATUS_INVALID_ARGUMENT
;
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
32
lmmc_status_t
lmmc_tensor_nd_set
(
lmmc_tensor_nd_t
* t,
const
size_t
* idx,
lmmc_real_t
value) {
33
size_t
i;
34
size_t
offset = 0;
35
36
if
(!lmmc_tensor_nd_descriptor_is_valid(t) || idx == NULL) {
37
return
LMMC_STATUS_INVALID_ARGUMENT
;
38
}
39
40
for
(i = 0; i < t->
ndim
; ++i) {
41
if
(idx[i] >= t->
dims
[i]) {
42
return
LMMC_STATUS_INVALID_ARGUMENT
;
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
51
void
lmmc_tensor_nd_destroy
(
lmmc_tensor_nd_t
* t) {
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
}
61
lmmc_memory_free
(t->
data
);
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
}
LMMC_REAL_CLEAR
#define LMMC_REAL_CLEAR(x)
释放一个 lmmc_real_t 变量(POD 类型为空操作)。
定义
config.h:38
lmmc_real_t
double lmmc_real_t
Public and internal LMMC real type.
定义
config.h:28
LMMC_REAL_SET
#define LMMC_REAL_SET(dst, src)
将一个 lmmc_real_t 赋值到另一个:*dst = *src 。
定义
config.h:41
lmmc_memory_free
void lmmc_memory_free(void *pointer)
定义
memory_bridge.c:75
lmmc_status_t
lmmc_status_t
LMMC 库统一返回的状态码.
定义
status.h:22
LMMC_STATUS_OK
@ LMMC_STATUS_OK
定义
status.h:23
LMMC_STATUS_INVALID_ARGUMENT
@ LMMC_STATUS_INVALID_ARGUMENT
定义
status.h:24
lmmc_tensor_nd_destroy
void lmmc_tensor_nd_destroy(lmmc_tensor_nd_t *t)
销毁 N-D 张量(仅当 owns_data 为 1 时释放缓冲区).
定义
tensor_nd_storage.c:51
lmmc_tensor_nd_t::owns_data
int owns_data
定义
tensor_nd.h:30
lmmc_tensor_nd_set
lmmc_status_t lmmc_tensor_nd_set(lmmc_tensor_nd_t *t, const size_t *idx, lmmc_real_t value)
写入 N-D 张量元素.
定义
tensor_nd_storage.c:32
lmmc_tensor_nd_get
lmmc_status_t lmmc_tensor_nd_get(const lmmc_tensor_nd_t *t, const size_t *idx, lmmc_real_t *out)
读取 N-D 张量元素.
定义
tensor_nd_storage.c:13
lmmc_tensor_nd_t::ndim
size_t ndim
定义
tensor_nd.h:26
lmmc_tensor_nd_t::data
lmmc_real_t * data
定义
tensor_nd.h:29
lmmc_tensor_nd_create
lmmc_status_t lmmc_tensor_nd_create(size_t ndim, const size_t *dims, lmmc_tensor_nd_t *out)
创建 N-D 张量,行优先连续存储,元素初始化为零.
定义
tensor_nd_storage.c:9
lmmc_tensor_nd_t::dims
size_t dims[LMMC_TENSOR_MAX_NDIM]
定义
tensor_nd.h:27
lmmc_tensor_nd_t::strides
size_t strides[LMMC_TENSOR_MAX_NDIM]
定义
tensor_nd.h:28
lmmc_tensor_nd_t
N-D 张量结构.
定义
tensor_nd.h:25
tensor_nd_internal.h
N 维张量结果存储、索引遍历与乘积累加。
src
tensor_nd_storage.c
生成于
, 为 LMMC使用
1.18.0