Skip to content

Commit 4fa5cdb

Browse files
committed
add memory tracking
1 parent b9d282f commit 4fa5cdb

38 files changed

Lines changed: 241 additions & 11 deletions

include/expr.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "utils/CSC_Matrix.h"
2222
#include "utils/CSR_Matrix.h"
2323
#include <stdbool.h>
24-
#include <stddef.h>
24+
#include <stddef.h> /* size_t */
2525
#include <string.h>
2626

2727
#define JAC_IDXS_NOT_SET -1
@@ -63,6 +63,8 @@ typedef struct expr
6363
// general quantities
6464
// ------------------------------------------------------------------------
6565
int d1, d2, size, n_vars, refcount, var_id;
66+
size_t memory_bytes;
67+
bool visited;
6668
struct expr *left;
6769
struct expr *right;
6870

include/problem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct
3838
int nnz_hessian;
3939
int n_vars;
4040
int total_constraint_size;
41+
size_t memory_bytes;
4142
} Diff_engine_stats;
4243

4344
typedef struct problem

include/utils/COO_Matrix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define COO_MATRIX_H
33

44
#include "CSR_Matrix.h"
5+
#include <stddef.h>
56

67
/* COO (Coordinate) Sparse Matrix Format
78
*
@@ -40,4 +41,8 @@ void refresh_lower_triangular_coo(COO_Matrix *coo, const double *vals);
4041

4142
void free_coo_matrix(COO_Matrix *matrix);
4243

44+
/* Returns total bytes used by rows, cols, x, value_map arrays
45+
(0 if A is NULL) */
46+
size_t coo_memory_bytes(const COO_Matrix *A);
47+
4348
#endif /* COO_MATRIX_H */

include/utils/CSC_Matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CSC_MATRIX_H
33

44
#include "CSR_Matrix.h"
5+
#include <stddef.h>
56

67
/* CSC (Compressed Sparse Column) Matrix Format
78
*
@@ -60,4 +61,7 @@ void csr_to_csc_fill_values(const CSR_Matrix *A, CSC_Matrix *C, int *iwork);
6061
CSR_Matrix *csc_to_csr_alloc(const CSC_Matrix *A, int *iwork);
6162
void csc_to_csr_fill_values(const CSC_Matrix *A, CSR_Matrix *C, int *iwork);
6263

64+
/* Returns total bytes used by p, i, x arrays (0 if A is NULL) */
65+
size_t csc_memory_bytes(const CSC_Matrix *A);
66+
6367
#endif /* CSC_MATRIX_H */

include/utils/CSR_Matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CSR_MATRIX_H
22
#define CSR_MATRIX_H
33
#include <stdbool.h>
4+
#include <stddef.h>
45

56
/* CSR (Compressed Sparse Row) Matrix Format
67
*
@@ -49,6 +50,9 @@ void insert_idx(int idx, int *arr, int len);
4950
/* get value at position (row, col) in A */
5051
double csr_get_value(const CSR_Matrix *A, int row, int col);
5152

53+
/* Returns total bytes used by p, i, x arrays (0 if A is NULL) */
54+
size_t csr_memory_bytes(const CSR_Matrix *A);
55+
5256
/* Expand symmetric CSR matrix A to full matrix C. A is assumed to store
5357
only upper triangle. C must be pre-allocated with sufficient nnz */
5458
void symmetrize_csr(const int *Ap, const int *Ai, int m, CSR_Matrix *C);

src/atoms/affine/add.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static void jacobian_init_impl(expr *node)
4343
/* we never have to store more than the sum of children's nnz */
4444
int nnz_max = node->left->jacobian->nnz + node->right->jacobian->nnz;
4545
node->jacobian = new_csr_matrix(node->size, node->n_vars, nnz_max);
46+
node->memory_bytes += csr_memory_bytes(node->jacobian);
4647

4748
/* fill sparsity pattern */
4849
sum_csr_alloc(node->left->jacobian, node->right->jacobian, node->jacobian);
@@ -67,6 +68,7 @@ static void wsum_hess_init_impl(expr *node)
6768
/* we never have to store more than the sum of children's nnz */
6869
int nnz_max = node->left->wsum_hess->nnz + node->right->wsum_hess->nnz;
6970
node->wsum_hess = new_csr_matrix(node->n_vars, node->n_vars, nnz_max);
71+
node->memory_bytes += csr_memory_bytes(node->wsum_hess);
7072

7173
/* fill sparsity pattern of hessian */
7274
sum_csr_alloc(node->left->wsum_hess, node->right->wsum_hess, node->wsum_hess);

src/atoms/affine/broadcast.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static void jacobian_init_impl(expr *node)
9393
}
9494

9595
node->jacobian = new_csr_matrix(node->size, node->n_vars, total_nnz);
96+
node->memory_bytes += csr_memory_bytes(node->jacobian);
9697

9798
// ---------------------------------------------------------------------
9899
// fill sparsity pattern
@@ -192,9 +193,11 @@ static void wsum_hess_init_impl(expr *node)
192193

193194
/* Same sparsity as child - weights get summed */
194195
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
196+
node->memory_bytes += csr_memory_bytes(node->wsum_hess);
195197

196198
/* allocate space for weight vector */
197199
node->work->dwork = malloc(node->size * sizeof(double));
200+
node->memory_bytes += node->size * sizeof(double);
198201
}
199202

200203
static void eval_wsum_hess(expr *node, const double *w)

src/atoms/affine/const_scalar_mult.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static void jacobian_init_impl(expr *node)
4848

4949
/* same sparsity as child */
5050
node->jacobian = new_csr_copy_sparsity(x->jacobian);
51+
node->memory_bytes += csr_memory_bytes(node->jacobian);
5152
}
5253

5354
static void eval_jacobian(expr *node)
@@ -74,6 +75,7 @@ static void wsum_hess_init_impl(expr *node)
7475

7576
/* same sparsity as child */
7677
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
78+
node->memory_bytes += csr_memory_bytes(node->wsum_hess);
7779
}
7880

7981
static void eval_wsum_hess(expr *node, const double *w)

src/atoms/affine/const_vector_mult.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static void jacobian_init_impl(expr *node)
4747

4848
/* same sparsity as child */
4949
node->jacobian = new_csr_copy_sparsity(x->jacobian);
50+
node->memory_bytes += csr_memory_bytes(node->jacobian);
5051
}
5152

5253
static void eval_jacobian(expr *node)
@@ -76,8 +77,10 @@ static void wsum_hess_init_impl(expr *node)
7677

7778
/* same sparsity as child */
7879
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
80+
node->memory_bytes += csr_memory_bytes(node->wsum_hess);
7981

8082
node->work->dwork = (double *) malloc(node->size * sizeof(double));
83+
node->memory_bytes += node->size * sizeof(double);
8184
}
8285

8386
static void eval_wsum_hess(expr *node, const double *w)
@@ -123,6 +126,7 @@ expr *new_const_vector_mult(const double *a, expr *child)
123126

124127
/* copy a vector */
125128
vnode->a = (double *) malloc(child->size * sizeof(double));
129+
node->memory_bytes += child->size * sizeof(double);
126130
memcpy(vnode->a, a, child->size * sizeof(double));
127131

128132
return node;

src/atoms/affine/constant.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static void jacobian_init_impl(expr *node)
3131
/* Constant jacobian is all zeros: size x n_vars with 0 nonzeros.
3232
* new_csr_matrix uses calloc for row pointers, so they're already 0. */
3333
node->jacobian = new_csr_matrix(node->size, node->n_vars, 0);
34+
node->memory_bytes += csr_memory_bytes(node->jacobian);
3435
}
3536

3637
static void eval_jacobian(expr *node)
@@ -43,6 +44,7 @@ static void wsum_hess_init_impl(expr *node)
4344
{
4445
/* Constant Hessian is all zeros: n_vars x n_vars with 0 nonzeros. */
4546
node->wsum_hess = new_csr_matrix(node->n_vars, node->n_vars, 0);
47+
node->memory_bytes += csr_memory_bytes(node->wsum_hess);
4648
}
4749

4850
static void eval_wsum_hess(expr *node, const double *w)

0 commit comments

Comments
 (0)