Skip to content

Commit 066ccbd

Browse files
committed
trim some memory
1 parent 55499c1 commit 066ccbd

11 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎include/utils/CSR_matrix.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ CSR_matrix *new_csr_copy_sparsity(const CSR_matrix *A);
4646
void free_CSR_matrix(CSR_matrix *matrix);
4747
void copy_CSR_matrix(const CSR_matrix *A, CSR_matrix *C);
4848

49+
/* Shrink a capacity-built CSR to its true size: realloc i and x down to
50+
p[m] and set nnz. Call once, after the pattern is complete and before
51+
the matrix is published to any consumer. */
52+
void CSR_trim(CSR_matrix *A);
53+
4954
/* transpose functionality (iwork must be of size A->n) */
5055
CSR_matrix *transpose(const CSR_matrix *A, int *iwork);
5156
CSR_matrix *AT_alloc(const CSR_matrix *A, int *iwork);

‎include/utils/sparse_matrix.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ matrix *new_sparse_matrix(CSR_matrix *A);
4444
Sparsity pattern and values are uninitialized. */
4545
matrix *new_sparse_matrix_alloc(int m, int n, int nnz);
4646

47+
/* Trim a wrapped capacity-built CSR to its true size (CSR_trim) and re-sync
48+
the base's cached x pointer and nnz, since realloc may move the buffer. */
49+
void sparse_matrix_trim(matrix *M);
50+
4751
/* Transpose helper */
4852
matrix *sparse_matrix_trans(const sparse_matrix *self, int *iwork);
4953

‎src/atoms/affine/hstack.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ static void wsum_hess_init_impl(expr *node)
128128
copy_CSR_matrix(H, hnode->CSR_work);
129129
sum_csr_alloc(hnode->CSR_work, child_hess->to_csr(child_hess), H);
130130
}
131+
132+
/* trim both buffers to the final pattern size; CSR_work must be re-synced
133+
from H first, since its row pointers still describe an older, smaller
134+
pattern */
135+
CSR_trim(H);
136+
copy_CSR_matrix(H, hnode->CSR_work);
137+
CSR_trim(hnode->CSR_work);
131138
node->wsum_hess = new_sparse_matrix(H);
132139
}
133140

‎src/atoms/affine/trace.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static void jacobian_init_impl(expr *node)
8282
tnode->idx_map = sp_malloc(A->nnz * sizeof(int));
8383
sum_spaced_rows_into_row_csr_alloc(A, jac, row_spacing, node->work->iwork,
8484
tnode->idx_map);
85+
CSR_trim(jac);
8586
node->jacobian = new_sparse_matrix(jac);
8687
}
8788

‎src/atoms/bivariate_full_dom/matmul.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ static void jacobian_init_chain_rule(expr *node)
244244
max_nnz = MIN(max_nnz, sat_mul_int(node->size, node->n_vars));
245245
CSR_matrix *jac = new_CSR_matrix(node->size, node->n_vars, max_nnz);
246246
sum_csr_alloc(mnode->term1_CSR, mnode->term2_CSR, jac);
247+
CSR_trim(jac);
247248
node->jacobian = new_sparse_matrix(jac);
248249
}
249250

‎src/problem.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,17 @@ void problem_init_hessian(problem *prob)
259259
int hess_nnz_ub = MIN(nnz, sat_mul_int(prob->n_vars, prob->n_vars));
260260
prob->lagrange_hessian = new_CSR_matrix(prob->n_vars, prob->n_vars, hess_nnz_ub);
261261

262-
/* affine shortcut */
263-
memset(prob->lagrange_hessian->x, 0, hess_nnz_ub * sizeof(double));
264-
265262
prob->hess_idx_map = (int *) sp_malloc(nnz * sizeof(int));
266263
int *iwork = (int *) sp_malloc(MAX(nnz, prob->n_vars) * sizeof(int));
267264
problem_lagrange_hess_fill_sparsity(prob, iwork);
265+
CSR_trim(prob->lagrange_hessian);
268266
prob->stats.nnz_hessian = prob->lagrange_hessian->nnz;
269267
sp_free(iwork);
270268

269+
/* affine shortcut */
270+
memset(prob->lagrange_hessian->x, 0,
271+
prob->lagrange_hessian->nnz * sizeof(double));
272+
271273
clock_gettime(CLOCK_MONOTONIC, &timer.end);
272274
prob->stats.time_init_derivatives += GET_ELAPSED_SECONDS(timer);
273275
}

‎src/utils/CSR_matrix.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ CSR_matrix *new_csr_copy_sparsity(const CSR_matrix *A)
5454
return copy;
5555
}
5656

57+
void CSR_trim(CSR_matrix *A)
58+
{
59+
int nnz = A->p[A->m];
60+
A->nnz = nnz;
61+
if (nnz == 0) return; /* realloc(ptr, 0) frees and returns NULL on MSVC */
62+
A->i = (int *) sp_realloc(A->i, nnz * sizeof(int));
63+
A->x = (double *) sp_realloc(A->x, nnz * sizeof(double));
64+
}
65+
5766
void free_CSR_matrix(CSR_matrix *matrix)
5867
{
5968
if (matrix)

‎src/utils/CSR_sum.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ CSR_matrix *sum_4_csr_alloc(const CSR_matrix *A, const CSR_matrix *B,
432432

433433
out->p[m] = nnz;
434434
out->nnz = nnz;
435+
CSR_trim(out);
435436
return out;
436437
}
437438

‎src/utils/matrix_sum.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
*/
1818
#include "utils/matrix_sum.h"
1919
#include "utils/CSR_sum.h"
20+
#include "utils/sparse_matrix.h"
2021

2122
void sum_matrices_alloc(matrix *A, matrix *B, matrix *C)
2223
{
2324
CSR_matrix *cc = C->to_csr(C);
2425
sum_csr_alloc(A->to_csr(A), B->to_csr(B), cc);
25-
C->nnz = cc->nnz;
26+
sparse_matrix_trim(C);
2627
}
2728

2829
void sum_matrices_fill_values(matrix *A, matrix *B, matrix *C)

‎src/utils/sparse_matrix.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ static matrix *sparse_sum_row_partition_alloc(matrix *self, int axis, int d1,
365365
}
366366

367367
sp_free(iwork);
368+
CSR_trim(out);
368369
return new_sparse_matrix(out);
369370
}
370371

@@ -410,6 +411,14 @@ matrix *new_sparse_matrix_alloc(int m, int n, int nnz)
410411
return new_sparse_matrix(new_CSR_matrix(m, n, nnz));
411412
}
412413

414+
void sparse_matrix_trim(matrix *M)
415+
{
416+
CSR_matrix *csr = ((sparse_matrix *) M)->csr;
417+
CSR_trim(csr);
418+
M->x = csr->x;
419+
M->nnz = csr->nnz;
420+
}
421+
413422
matrix *sparse_matrix_trans(const sparse_matrix *self, int *iwork)
414423
{
415424
CSR_matrix *AT = transpose(self->csr, iwork);

0 commit comments

Comments
 (0)