LADEL main
Sparse LDL factorization package with rank 1 and rowadd/rowdel updates
ladel_global.c
Go to the documentation of this file.
1#include "ladel_global.h"
2#include "ladel_types.h"
3#include "ladel_constants.h"
4#include "ladel_copy.h"
5#include "ladel_permutation.h"
6#include <assert.h>
7#include <stdlib.h>
8#include <stdio.h>
9
15};
16
17static struct ladel_alloc_config_t ladel_alloc_config = {
18 .calloc = &calloc,
19 .malloc = &malloc,
20 .realloc = &realloc,
21 .free = &free,
22};
23
25 assert(calloc);
26 calloc_sig *old = ladel_alloc_config.calloc;
27 ladel_alloc_config.calloc = calloc;
28 return old;
29}
30
32 assert(malloc);
33 malloc_sig *old = ladel_alloc_config.malloc;
34 ladel_alloc_config.malloc = malloc;
35 return old;
36}
37
39 assert(realloc);
40 realloc_sig *old = ladel_alloc_config.realloc;
41 ladel_alloc_config.realloc = realloc;
42 return old;
43}
44
46 assert(free);
47 free_sig *old = ladel_alloc_config.free;
48 ladel_alloc_config.free = free;
49 return old;
50}
51
52void *ladel_malloc(ladel_int n, size_t size) {
53 return ladel_alloc_config.malloc(LADEL_MAX(n, 1) * size);
54}
55
56void *ladel_calloc(ladel_int n, size_t size) {
57 return ladel_alloc_config.calloc(LADEL_MAX(n, 1), size);
58}
59
60void *ladel_free(void *p) {
61 if (p)
62 ladel_alloc_config.free(p);
63 return NULL;
64}
65
66void *ladel_realloc(void *p, ladel_int n, size_t size, ladel_int *status) {
67 void *p_new;
68 p_new = ladel_alloc_config.realloc(p, LADEL_MAX(n, 1) * size);
69 *status = (p_new != NULL);
70 return (*status) ? p_new : p;
71}
72
75};
76static struct ladel_print_config_t ladel_print_config = {
77 .printf = &printf,
78};
79
81 assert(printf);
82 printf_sig *old = ladel_print_config.printf;
83 ladel_print_config.printf = printf;
84 return old;
85}
86
88 return ladel_print_config.printf;
89}
90
92{
93 if (!M) return NULL;
94 ladel_free(M->p);
95 ladel_free(M->i);
96 ladel_free(M->x);
97 ladel_free(M->nz);
98 return ((ladel_sparse_matrix *) ladel_free(M));
99}
100
102 ladel_int nzmax, ladel_int symmetry,
103 ladel_int values, ladel_int nz)
104{
106 if (!M) return NULL;
107 M->nrow = nrow;
108 M->ncol = ncol;
109 M->nzmax = nzmax;
110 nzmax = LADEL_MAX(nzmax, 1);
111 M->values = values;
112 M->symmetry = symmetry;
113 if (M->nzmax) M->p = (ladel_int *) ladel_malloc(ncol+1, sizeof(ladel_int));
114 else M->p = (ladel_int *) ladel_calloc(ncol+1, sizeof(ladel_int)); //Initialize col counts to zero for empty matrix
115 M->i = (ladel_int *) ladel_malloc(nzmax, sizeof(ladel_int));
116 M->x = values ? (ladel_double *) ladel_malloc(nzmax, sizeof(ladel_double)) : NULL;
117 M->nz = (nz && ncol) ? (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int)) : NULL;
118 if (!M->p || !M->i || (values && !M->x) || (nz && !M->nz)) M = ladel_sparse_free(M);
119 return M;
120}
121
123 ladel_int symmetry, ladel_int values,
124 ladel_int nz)
125{
127 if (!M) return NULL;
128 M->nrow = nrow;
129 M->ncol = ncol;
130 M->nzmax = 0;
131 M->values = values;
132 M->symmetry = symmetry;
133 M->p = (ladel_int *) ladel_calloc(ncol+1, sizeof(ladel_int));
134 M->i = (ladel_int *) ladel_malloc(1, sizeof(ladel_int));
135 M->x = values ? (ladel_double *) ladel_malloc(1, sizeof(ladel_double)) : NULL;
136 M->nz = (nz && ncol) ? (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int)) : NULL;
137 if (!M->p || !M->i || (values && !M->x) || (nz && !M->nz)) M = ladel_sparse_free(M);
138 return M;
139}
140
142{
143 ladel_int status_i, status, status_x = SUCCESS;
144 if (!M) return FAIL;
145 if (nzmax <= 0) nzmax = M->p[M->ncol];
146 M->i = (ladel_int *) ladel_realloc(M->i, nzmax, sizeof(ladel_int), &status_i);
147 if (M->values) M->x = (ladel_double *) ladel_realloc(M->x, nzmax, sizeof(ladel_double), &status_x);
148 status = status_i && status_x;
149 if (status == SUCCESS) M->nzmax = nzmax;
150 return status;
151}
152
154{
155 if (!sym) return NULL;
156 ladel_free(sym->etree);
157 ladel_free(sym->postorder);
159 ladel_free(sym->p);
160 ladel_free(sym->pinv);
161 ladel_free(sym->pattern);
162 ladel_free(sym->nodes);
163 return (ladel_symbolics *) ladel_free(sym);
164}
165
167{
169 if (!sym) return NULL;
170 sym->ncol = ncol;
171 sym->etree = (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int));
172 sym->postorder = (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int));
173 sym->col_counts = (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int));
174 sym->p = (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int));
175 sym->pinv = (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int));
176 sym->pattern = (ladel_int *) ladel_malloc(ncol, sizeof(ladel_int));
177 sym->nodes = (ladel_int *) ladel_calloc(ncol, sizeof(ladel_int));
178 if (!sym->etree || !sym->postorder || !sym->col_counts || !sym->pattern || !sym->nodes)
179 sym = ladel_symbolics_free(sym);
180 return sym;
181}
182
184{
185 if (!LD) return NULL;
186 ladel_sparse_free(LD->L);
187 ladel_free(LD->D);
188 ladel_free(LD->Dinv);
189 ladel_free(LD->p);
190 ladel_free(LD->pinv);
191 return (ladel_factor *) ladel_free(LD);
192}
193
195{
197 if (!LD || !sym) return NULL;
198 ladel_int ncol = LD->ncol = sym->ncol;
199 LD->L = ladel_sparse_alloc(ncol, ncol, sym->col_counts[ncol-1], UNSYMMETRIC, TRUE, TRUE);
200 LD->D = ladel_malloc(ncol, sizeof(ladel_double));
201 LD->Dinv = ladel_malloc(ncol, sizeof(ladel_double));
202 if (!LD->L || !LD->D || !LD->Dinv)
203 {
205 return NULL;
206 }
207 if (sym->p)
208 {
209 LD->p = ladel_malloc(ncol, sizeof(ladel_int));
210 LD->pinv = ladel_malloc(ncol, sizeof(ladel_int));
211 if (!LD->p || !LD->pinv)
212 {
214 return NULL;
215 }
216 ladel_int_vector_copy(sym->p, ncol, LD->p);
217 ladel_int_vector_copy(sym->pinv, ncol, LD->pinv);
218 } else
219 {
220 LD->p = NULL;
221 LD->pinv = NULL;
222 }
223 return LD;
224}
225
227{
228 if (!set) return NULL;
229 ladel_free(set->set);
230 return (ladel_set *) ladel_free(set);
231}
232
234{
235 ladel_set* set = ladel_malloc(1, sizeof(ladel_set));
236 if (!set) return NULL;
237 set->set = ladel_malloc(max_size, sizeof(ladel_int));
238 if (!set->set)
239 {
240 ladel_set_free(set);
241 return NULL;
242 }
243 set->max_size_set = max_size;
244 return set;
245}
246
247void ladel_set_set(ladel_set *set, ladel_int *set_vals, ladel_int size_set, ladel_int max_size_set)
248{
249 set->set = set_vals;
250 set->size_set = size_set;
251 set->max_size_set = max_size_set;
252}
253
255{
256 if (!work) return NULL;
270 return (ladel_work *) ladel_free(work);
271}
272
274{
275 ladel_work *work = ladel_malloc(1, sizeof(ladel_work));
276 if (!work) return NULL;
283 work->array_int_ncol1 = ladel_malloc(ncol, sizeof(ladel_int));
284 work->array_int_ncol2 = ladel_malloc(ncol, sizeof(ladel_int));
285 work->array_int_ncol3 = ladel_malloc(ncol, sizeof(ladel_int));
286 work->array_int_ncol4 = ladel_malloc(ncol, sizeof(ladel_int));
287 work->array_int_ncol_flag = ladel_calloc(ncol, sizeof(ladel_int));
288 work->flag = 1;
289 work->array_double_ncol1 = ladel_malloc(ncol, sizeof(ladel_double));
291
292 if (!work->set_preallocated1 || !work->set_preallocated2 || !work->set_preallocated3
294 || !work->array_int_ncol1 || !work->array_int_ncol2 || !work->array_int_ncol3
296 {
298 return NULL;
299 }
300 return work;
301}
#define UNSYMMETRIC
No symmetry is assumed in the matrix.
#define TRUE
For booleans.
#define SUCCESS
For status returns.
#define FAIL
For status returns.
#define LADEL_MAX(a, b)
Return the maximum of two numbers.
Constants and macros used in LADEL.
Routines to copy matrices and vectors.
void ladel_int_vector_copy(ladel_int *x, ladel_int size, ladel_int *y)
Copies an integer vector (preallocated)
Definition: ladel_copy.c:38
ladel_work * ladel_workspace_free(ladel_work *work)
Free a LADEL workspace.
Definition: ladel_global.c:254
void * ladel_calloc(ladel_int n, size_t size)
Version of calloc (for mex or for regular C).
Definition: ladel_global.c:56
malloc_sig * ladel_set_alloc_config_malloc(malloc_sig *malloc)
Set the malloc function used by LADEL.
Definition: ladel_global.c:31
calloc_sig * ladel_set_alloc_config_calloc(calloc_sig *calloc)
Set the calloc function used by LADEL.
Definition: ladel_global.c:24
ladel_factor * ladel_factor_free(ladel_factor *LD)
Free a factor.
Definition: ladel_global.c:183
ladel_symbolics * ladel_symbolics_free(ladel_symbolics *sym)
Free a symbolics struct (and return NULL).
Definition: ladel_global.c:153
printf_sig * ladel_set_print_config_printf(printf_sig *printf)
Set the printf function used by LADEL.
Definition: ladel_global.c:80
realloc_sig * ladel_set_alloc_config_realloc(realloc_sig *realloc)
Set the realloc function used by LADEL.
Definition: ladel_global.c:38
ladel_sparse_matrix * ladel_sparse_alloc(ladel_int nrow, ladel_int ncol, ladel_int nzmax, ladel_int symmetry, ladel_int values, ladel_int nz)
Allocate a sparse matrix.
Definition: ladel_global.c:101
void ladel_set_set(ladel_set *set, ladel_int *set_vals, ladel_int size_set, ladel_int max_size_set)
Fill in the fields of the given set.
Definition: ladel_global.c:247
ladel_symbolics * ladel_symbolics_alloc(ladel_int ncol)
Allocate a symbolics struct.
Definition: ladel_global.c:166
ladel_set * ladel_set_allocate(ladel_int max_size)
Allocate a set struct.
Definition: ladel_global.c:233
void * ladel_realloc(void *p, ladel_int n, size_t size, ladel_int *status)
Version of realloc (for mex or for regular C).
Definition: ladel_global.c:66
void * ladel_malloc(ladel_int n, size_t size)
Version of malloc (for mex or for regular C).
Definition: ladel_global.c:52
ladel_sparse_matrix * ladel_sparse_alloc_empty(ladel_int nrow, ladel_int ncol, ladel_int symmetry, ladel_int values, ladel_int nz)
Allocate a sparse empty matrix (used in special cases).
Definition: ladel_global.c:122
void * ladel_free(void *p)
Version of free (for mex or for regular C).
Definition: ladel_global.c:60
ladel_work * ladel_workspace_allocate(ladel_int ncol)
Allocate a LADEL workspace.
Definition: ladel_global.c:273
ladel_factor * ladel_factor_allocate(ladel_symbolics *sym)
Allocate a factors struct.
Definition: ladel_global.c:194
printf_sig * ladel_get_print_config_printf(void)
Get the printf function used by LADEL.
Definition: ladel_global.c:87
free_sig * ladel_set_alloc_config_free(free_sig *free)
Set the free function used by LADEL.
Definition: ladel_global.c:45
ladel_set * ladel_set_free(ladel_set *set)
Free a set.
Definition: ladel_global.c:226
ladel_int ladel_sparse_realloc(ladel_sparse_matrix *M, ladel_int nzmax)
Reallocate a sparse matrix with a new size.
Definition: ladel_global.c:141
ladel_sparse_matrix * ladel_sparse_free(ladel_sparse_matrix *M)
Free a sparse matrix (and return NULL).
Definition: ladel_global.c:91
Memory allocation routines.
void *() malloc_sig(size_t)
Definition: ladel_global.h:20
void() free_sig(void *)
Definition: ladel_global.h:22
void *() calloc_sig(size_t, size_t)
Definition: ladel_global.h:19
LADEL_ATTR_PRINTF_LIKE int() printf_sig(const char *,...)
Definition: ladel_global.h:87
void *() realloc_sig(void *, size_t)
Definition: ladel_global.h:21
Routines to permute vectors and matrices.
Structures and types used in LADEL routines.
int64_t ladel_int
Type for integer numbers (default: int64_t)
Definition: ladel_types.h:24
double ladel_double
Type for floating point numbers (default: double)
Definition: ladel_types.h:20
Sparse matrix in compressed column storage.
Definition: ladel_types.h:35
ladel_int symmetry
type of symmetry (UNSYMMETRIC, UPPER or LOWER)
Definition: ladel_types.h:46
ladel_int ncol
number of columns
Definition: ladel_types.h:38
ladel_double * x
numerical values (size nzmax)
Definition: ladel_types.h:42
ladel_int nzmax
number of nonzeros
Definition: ladel_types.h:36
ladel_int * p
column pointers (size ncol+1)
Definition: ladel_types.h:40
ladel_int * nz
(optional) number of elements in each column (size ncol)
Definition: ladel_types.h:43
ladel_int values
has numerical values
Definition: ladel_types.h:45
ladel_int nrow
number of rows
Definition: ladel_types.h:37
ladel_int * i
row pointers (size nzmax)
Definition: ladel_types.h:41
malloc_sig * malloc
Definition: ladel_global.c:12
realloc_sig * realloc
Definition: ladel_global.c:13
calloc_sig * calloc
Definition: ladel_global.c:11
printf_sig * printf
Definition: ladel_global.c:74
Set of integers.
Definition: ladel_types.h:81
ladel_int max_size_set
Maximum (allocated) size of the list.
Definition: ladel_types.h:84
ladel_int * set
List of integers representing the set.
Definition: ladel_types.h:82
ladel_int size_set
Size of the list.
Definition: ladel_types.h:83
Factors of an factorization.
Definition: ladel_types.h:69
ladel_int ncol
number of columns in the analyzed matrix
Definition: ladel_types.h:70
ladel_int * p
permutation vector
Definition: ladel_types.h:74
ladel_sparse_matrix * L
L in LDL' factorization.
Definition: ladel_types.h:71
ladel_double * Dinv
D^-1 in LDL' factorization (stored as vector)
Definition: ladel_types.h:73
ladel_double * D
D in LDL' factorization (stored as vector), not used but is useful for returning.
Definition: ladel_types.h:72
ladel_int * pinv
inverse permutation vector
Definition: ladel_types.h:75
Structure capturing symbolic information used for and during the factorization.
Definition: ladel_types.h:54
ladel_int * nodes
keeps track of which nodes have been marked
Definition: ladel_types.h:62
ladel_int * pinv
inverse permutation vector
Definition: ladel_types.h:60
ladel_int ncol
number of columns in the analyzed matrix
Definition: ladel_types.h:55
ladel_int * pattern
stores the nonzero pattern of a row of L
Definition: ladel_types.h:61
ladel_int * etree
eliminations tree
Definition: ladel_types.h:56
ladel_int * p
fill-reducing ordering (possibly AMD)
Definition: ladel_types.h:59
ladel_int * postorder
postordiring of the elimination tree
Definition: ladel_types.h:57
ladel_int * col_counts
column counts, stored as column pointers
Definition: ladel_types.h:58
Workspace required for various routines in LADEL.
Definition: ladel_types.h:109
ladel_int * array_int_ncol2
An array of ncol integers.
Definition: ladel_types.h:117
ladel_set * set_unallocated_values3
An unallocated set structure.
Definition: ladel_types.h:115
ladel_set * set_preallocated2
A preallocated set structure.
Definition: ladel_types.h:111
ladel_int flag
Flag used to mark nodes, used in conjunction with array_int_ncol_flag.
Definition: ladel_types.h:121
ladel_int * array_int_ncol1
An array of ncol integers.
Definition: ladel_types.h:116
ladel_int * array_int_ncol_flag
An array of ncol integers, assumed to be < flag.
Definition: ladel_types.h:120
ladel_int * array_int_ncol3
An array of ncol integers.
Definition: ladel_types.h:118
ladel_int * array_int_ncol4
An array of ncol integers.
Definition: ladel_types.h:119
ladel_set * set_unallocated_values2
An unallocated set structure.
Definition: ladel_types.h:114
ladel_double * array_double_all_zeros_ncol1
An array of ncol doubles, on input and output this should be all zeros.
Definition: ladel_types.h:122
ladel_set * set_preallocated1
A preallocated set structure.
Definition: ladel_types.h:110
ladel_double * array_double_ncol1
An array of ncol doubles.
Definition: ladel_types.h:123
ladel_set * set_preallocated3
A preallocated set structure.
Definition: ladel_types.h:112
ladel_set * set_unallocated_values1
An unallocated set structure.
Definition: ladel_types.h:113