LMMC
Lamina Mathematic core
Toggle main menu visibility
载入中...
搜索中...
未找到
sparse_storage.c
浏览该文件的文档.
1
5
#include <string.h>
6
7
#include "
memory_bridge.h
"
8
#include "
internal.h
"
9
#include "
sparse_internal.h
"
10
#include "
lmmc/config.h
"
11
#include "
lmmc/sparse.h
"
12
13
14
lmmc_status_t
lmmc_sparse_create_csr
(
size_t
rows,
size_t
cols,
size_t
nnz,
lmmc_sparse_mat_t
* out_sparse) {
15
return
lmmc_sparse_create(rows, cols, nnz,
LMMC_SPARSE_CSR
, out_sparse);
16
}
17
18
lmmc_status_t
lmmc_sparse_create_csc
(
size_t
rows,
size_t
cols,
size_t
nnz,
lmmc_sparse_mat_t
* out_sparse) {
19
return
lmmc_sparse_create(rows, cols, nnz,
LMMC_SPARSE_CSC
, out_sparse);
20
}
21
22
lmmc_status_t
lmmc_sparse_wrap_csr
(
23
size_t
rows,
24
size_t
cols,
25
size_t
nnz,
26
size_t
* row_ptr,
27
size_t
* col_idx,
28
lmmc_real_t
* values,
29
lmmc_sparse_mat_t
* out_sparse
30
) {
31
lmmc_sparse_mat_t
candidate = {0};
32
if
(out_sparse == NULL || rows == 0 || cols == 0 || row_ptr == NULL) {
33
return
LMMC_STATUS_INVALID_ARGUMENT
;
34
}
35
if
(nnz > 0 && (col_idx == NULL || values == NULL)) {
36
return
LMMC_STATUS_INVALID_ARGUMENT
;
37
}
38
39
candidate.
rows
= rows;
40
candidate.
cols
= cols;
41
candidate.
nnz
= nnz;
42
candidate.
row_ptr
= row_ptr;
43
candidate.
col_idx
= col_idx;
44
candidate.
values
= values;
45
candidate.
format
=
LMMC_SPARSE_CSR
;
46
candidate.
owns_data
= 0;
47
48
if
(lmmc_sparse_validate(&candidate) !=
LMMC_STATUS_OK
) {
49
return
LMMC_STATUS_INVALID_ARGUMENT
;
50
}
51
52
*out_sparse = candidate;
53
return
LMMC_STATUS_OK
;
54
}
55
56
lmmc_status_t
lmmc_sparse_wrap_csc
(
57
size_t
rows,
58
size_t
cols,
59
size_t
nnz,
60
size_t
* col_ptr,
61
size_t
* row_idx,
62
lmmc_real_t
* values,
63
lmmc_sparse_mat_t
* out_sparse
64
) {
65
lmmc_sparse_mat_t
candidate = {0};
66
if
(out_sparse == NULL || rows == 0 || cols == 0 || col_ptr == NULL) {
67
return
LMMC_STATUS_INVALID_ARGUMENT
;
68
}
69
if
(nnz > 0 && (row_idx == NULL || values == NULL)) {
70
return
LMMC_STATUS_INVALID_ARGUMENT
;
71
}
72
73
candidate.
rows
= rows;
74
candidate.
cols
= cols;
75
candidate.
nnz
= nnz;
76
candidate.
row_ptr
= col_ptr;
77
candidate.
col_idx
= row_idx;
78
candidate.
values
= values;
79
candidate.
format
=
LMMC_SPARSE_CSC
;
80
candidate.
owns_data
= 0;
81
82
if
(lmmc_sparse_validate(&candidate) !=
LMMC_STATUS_OK
) {
83
return
LMMC_STATUS_INVALID_ARGUMENT
;
84
}
85
86
*out_sparse = candidate;
87
return
LMMC_STATUS_OK
;
88
}
89
90
void
lmmc_sparse_destroy
(
lmmc_sparse_mat_t
* sparse) {
91
if
(sparse == NULL) {
92
return
;
93
}
94
if
(sparse->
owns_data
) {
95
if
(sparse->
row_ptr
!= NULL)
lmmc_memory_free
(sparse->
row_ptr
);
96
if
(sparse->
col_idx
!= NULL)
lmmc_memory_free
(sparse->
col_idx
);
97
if
(sparse->
values
!= NULL) {
98
for
(
size_t
i = 0; i < sparse->
nnz
; ++i) {
99
LMMC_REAL_CLEAR
(&sparse->
values
[i]);
100
}
101
lmmc_memory_free
(sparse->
values
);
102
}
103
}
104
sparse->
rows
= 0;
105
sparse->
cols
= 0;
106
sparse->
nnz
= 0;
107
sparse->
row_ptr
= NULL;
108
sparse->
col_idx
= NULL;
109
sparse->
values
= NULL;
110
sparse->
owns_data
= 0;
111
}
config.h
LMMC binary real type and arithmetic spelling.
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
internal.h
LMMC 内部使用的公共工具(仅源文件可见).
lmmc_memory_free
void lmmc_memory_free(void *pointer)
定义
memory_bridge.c:75
memory_bridge.h
Private recoverable allocator for persistent LMMC ownership.
sparse.h
sparse_internal.h
稀疏矩阵模块内部共享辅助函数(仅源文件可见)。
lmmc_sparse_create_csc
lmmc_status_t lmmc_sparse_create_csc(size_t rows, size_t cols, size_t nnz, lmmc_sparse_mat_t *out_sparse)
为指定 nnz 的 CSC 矩阵分配数组。
定义
sparse_storage.c:18
lmmc_sparse_wrap_csc
lmmc_status_t lmmc_sparse_wrap_csc(size_t rows, size_t cols, size_t nnz, size_t *col_ptr, size_t *row_idx, lmmc_real_t *values, lmmc_sparse_mat_t *out_sparse)
将调用方缓冲区包装为 CSC 视图。
定义
sparse_storage.c:56
lmmc_sparse_destroy
void lmmc_sparse_destroy(lmmc_sparse_mat_t *sparse)
销毁稀疏矩阵,释放其拥有的缓冲区并清零全部字段。
定义
sparse_storage.c:90
lmmc_sparse_create_csr
lmmc_status_t lmmc_sparse_create_csr(size_t rows, size_t cols, size_t nnz, lmmc_sparse_mat_t *out_sparse)
为指定 nnz 的 CSR 矩阵分配数组。
定义
sparse_storage.c:14
lmmc_sparse_wrap_csr
lmmc_status_t lmmc_sparse_wrap_csr(size_t rows, size_t cols, size_t nnz, size_t *row_ptr, size_t *col_idx, lmmc_real_t *values, lmmc_sparse_mat_t *out_sparse)
将调用方缓冲区包装为 CSR 视图。
定义
sparse_storage.c:22
LMMC_SPARSE_CSR
@ LMMC_SPARSE_CSR
定义
sparse_types.h:14
LMMC_SPARSE_CSC
@ LMMC_SPARSE_CSC
定义
sparse_types.h:15
lmmc_sparse_mat_t::rows
size_t rows
定义
sparse_types.h:35
lmmc_sparse_mat_t::row_ptr
size_t * row_ptr
定义
sparse_types.h:38
lmmc_sparse_mat_t::col_idx
size_t * col_idx
定义
sparse_types.h:39
lmmc_sparse_mat_t::values
lmmc_real_t * values
定义
sparse_types.h:40
lmmc_sparse_mat_t::owns_data
int owns_data
定义
sparse_types.h:42
lmmc_sparse_mat_t::nnz
size_t nnz
定义
sparse_types.h:37
lmmc_sparse_mat_t::format
lmmc_sparse_format_t format
定义
sparse_types.h:41
lmmc_sparse_mat_t::cols
size_t cols
定义
sparse_types.h:36
lmmc_sparse_mat_t
通用稀疏矩阵(CSR / CSC 共用同一结构)。
定义
sparse_types.h:34
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
src
sparse_storage.c
生成于
, 为 LMMC使用
1.18.0