Skip to content

Commit a8349ef

Browse files
committed
wrap malloc/alloc
1 parent b9d282f commit a8349ef

44 files changed

Lines changed: 185 additions & 121 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

src/atoms/affine/add.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "utils/CSR_sum.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -91,7 +92,7 @@ static bool is_affine(const expr *node)
9192
expr *new_add(expr *left, expr *right)
9293
{
9394
assert(left->d1 == right->d1 && left->d2 == right->d2);
94-
expr *node = (expr *) calloc(1, sizeof(expr));
95+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
9596
init_expr(node, left->d1, left->d2, left->n_vars, forward, jacobian_init_impl,
9697
eval_jacobian, is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
9798
node->left = left;

src/atoms/affine/broadcast.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
2020
#include "utils/mini_numpy.h"
21+
#include "utils/tracked_alloc.h"
2122
#include <assert.h>
2223
#include <stdio.h>
2324
#include <stdlib.h>
@@ -194,7 +195,7 @@ static void wsum_hess_init_impl(expr *node)
194195
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
195196

196197
/* allocate space for weight vector */
197-
node->work->dwork = malloc(node->size * sizeof(double));
198+
node->work->dwork = SP_MALLOC(node->size * sizeof(double));
198199
}
199200

200201
static void eval_wsum_hess(expr *node, const double *w)
@@ -273,7 +274,7 @@ expr *new_broadcast(expr *child, int d1, int d2)
273274
exit(1);
274275
}
275276

276-
broadcast_expr *bcast = (broadcast_expr *) calloc(1, sizeof(broadcast_expr));
277+
broadcast_expr *bcast = (broadcast_expr *) SP_CALLOC(1, sizeof(broadcast_expr));
277278
expr *node = (expr *) bcast;
278279

279280
// --------------------------------------------------------------------------

src/atoms/affine/const_scalar_mult.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -97,7 +98,7 @@ static bool is_affine(const expr *node)
9798
expr *new_const_scalar_mult(double a, expr *child)
9899
{
99100
const_scalar_mult_expr *mult_node =
100-
(const_scalar_mult_expr *) calloc(1, sizeof(const_scalar_mult_expr));
101+
(const_scalar_mult_expr *) SP_CALLOC(1, sizeof(const_scalar_mult_expr));
101102
expr *node = &mult_node->base;
102103

103104
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init_impl,

src/atoms/affine/const_vector_mult.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <stdio.h>
2122
#include <stdlib.h>
2223
#include <string.h>
@@ -77,7 +78,7 @@ static void wsum_hess_init_impl(expr *node)
7778
/* same sparsity as child */
7879
node->wsum_hess = new_csr_copy_sparsity(x->wsum_hess);
7980

80-
node->work->dwork = (double *) malloc(node->size * sizeof(double));
81+
node->work->dwork = (double *) SP_MALLOC(node->size * sizeof(double));
8182
}
8283

8384
static void eval_wsum_hess(expr *node, const double *w)
@@ -112,7 +113,7 @@ static bool is_affine(const expr *node)
112113
expr *new_const_vector_mult(const double *a, expr *child)
113114
{
114115
const_vector_mult_expr *vnode =
115-
(const_vector_mult_expr *) calloc(1, sizeof(const_vector_mult_expr));
116+
(const_vector_mult_expr *) SP_CALLOC(1, sizeof(const_vector_mult_expr));
116117
expr *node = &vnode->base;
117118

118119
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init_impl,
@@ -122,7 +123,7 @@ expr *new_const_vector_mult(const double *a, expr *child)
122123
expr_retain(child);
123124

124125
/* copy a vector */
125-
vnode->a = (double *) malloc(child->size * sizeof(double));
126+
vnode->a = (double *) SP_MALLOC(child->size * sizeof(double));
126127
memcpy(vnode->a, a, child->size * sizeof(double));
127128

128129
return node;

src/atoms/affine/constant.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818
#include "atoms/affine.h"
19+
#include "utils/tracked_alloc.h"
1920
#include <stdlib.h>
2021
#include <string.h>
2122

@@ -60,7 +61,7 @@ static bool is_affine(const expr *node)
6061

6162
expr *new_constant(int d1, int d2, int n_vars, const double *values)
6263
{
63-
expr *node = (expr *) calloc(1, sizeof(expr));
64+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
6465
init_expr(node, d1, d2, n_vars, forward, jacobian_init_impl, eval_jacobian,
6566
is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
6667
memcpy(node->value, values, node->size * sizeof(double));

src/atoms/affine/diag_vec.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// SPDX-License-Identifier: Apache-2.0
1919

2020
#include "atoms/affine.h"
21+
#include "utils/tracked_alloc.h"
2122
#include <assert.h>
2223
#include <stdlib.h>
2324
#include <string.h>
@@ -100,7 +101,7 @@ static void wsum_hess_init_impl(expr *node)
100101
wsum_hess_init(x);
101102

102103
/* workspace for extracting diagonal weights */
103-
node->work->dwork = (double *) calloc(x->size, sizeof(double));
104+
node->work->dwork = (double *) SP_CALLOC(x->size, sizeof(double));
104105

105106
/* Copy child's Hessian structure (diag_vec is linear, so its own Hessian is
106107
* zero) */
@@ -136,7 +137,7 @@ expr *new_diag_vec(expr *child)
136137

137138
/* n is the number of elements (works for both row and column vectors) */
138139
int n = child->size;
139-
expr *node = (expr *) calloc(1, sizeof(expr));
140+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
140141
init_expr(node, n, n, child->n_vars, forward, jacobian_init_impl, eval_jacobian,
141142
is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
142143
node->left = child;

src/atoms/affine/hstack.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "utils/CSR_sum.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -185,14 +186,14 @@ expr *new_hstack(expr **args, int n_args, int n_vars)
185186
}
186187

187188
/* Allocate the type-specific struct */
188-
hstack_expr *hnode = (hstack_expr *) calloc(1, sizeof(hstack_expr));
189+
hstack_expr *hnode = (hstack_expr *) SP_CALLOC(1, sizeof(hstack_expr));
189190
expr *node = &hnode->base;
190191
init_expr(node, args[0]->d1, d2, n_vars, forward, jacobian_init_impl,
191192
eval_jacobian, is_affine, wsum_hess_init_impl, wsum_hess_eval,
192193
free_type_data);
193194

194195
/* Set type-specific fields (deep copy args array) */
195-
hnode->args = (expr **) calloc(n_args, sizeof(expr *));
196+
hnode->args = (expr **) SP_CALLOC(n_args, sizeof(expr *));
196197
hnode->n_args = n_args;
197198
for (int i = 0; i < n_args; i++)
198199
{

src/atoms/affine/index.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
#include "atoms/affine.h"
1919
#include "subexpr.h"
20+
#include "utils/tracked_alloc.h"
2021
#include <assert.h>
2122
#include <stdio.h>
2223
#include <stdlib.h>
@@ -28,7 +29,7 @@
2829
* Returns true if duplicates exist, false otherwise. */
2930
static bool check_for_duplicates(const int *indices, int n_idxs, int max_idx)
3031
{
31-
bool *seen = (bool *) calloc(max_idx, sizeof(bool));
32+
bool *seen = (bool *) SP_CALLOC(max_idx, sizeof(bool));
3233
bool has_dup = false;
3334
for (int i = 0; i < n_idxs && !has_dup; i++)
3435
{
@@ -104,7 +105,7 @@ static void wsum_hess_init_impl(expr *node)
104105
wsum_hess_init(x);
105106

106107
/* for setting weight vector to evaluate hessian of child */
107-
node->work->dwork = (double *) calloc(x->size, sizeof(double));
108+
node->work->dwork = (double *) SP_CALLOC(x->size, sizeof(double));
108109

109110
/* in the implementation of eval_wsum_hess we evaluate the
110111
child's hessian with a weight vector that has w[i] = 0
@@ -163,7 +164,7 @@ expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)
163164
{
164165
assert(d1 * d2 == n_idxs);
165166
/* allocate type-specific struct */
166-
index_expr *idx = (index_expr *) calloc(1, sizeof(index_expr));
167+
index_expr *idx = (index_expr *) SP_CALLOC(1, sizeof(index_expr));
167168
expr *node = &idx->base;
168169

169170
init_expr(node, d1, d2, child->n_vars, forward, jacobian_init_impl,
@@ -174,7 +175,7 @@ expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)
174175
expr_retain(child);
175176

176177
/* copy indices */
177-
idx->indices = (int *) malloc(n_idxs * sizeof(int));
178+
idx->indices = (int *) SP_MALLOC(n_idxs * sizeof(int));
178179
memcpy(idx->indices, indices, n_idxs * sizeof(int));
179180
idx->n_idxs = n_idxs;
180181

src/atoms/affine/left_matmul.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
vector-valued or matrix-valued.
4747
*/
4848

49+
#include "utils/tracked_alloc.h"
4950
#include "utils/utils.h"
5051

5152
static void forward(expr *node, const double *u)
@@ -125,7 +126,7 @@ static void wsum_hess_init_impl(expr *node)
125126
/* work for computing A^T w*/
126127
int n_blocks = ((left_matmul_expr *) node)->n_blocks;
127128
int dim = ((left_matmul_expr *) node)->AT->m * n_blocks;
128-
node->work->dwork = (double *) malloc(dim * sizeof(double));
129+
node->work->dwork = (double *) SP_MALLOC(dim * sizeof(double));
129130
}
130131

131132
static void eval_wsum_hess(expr *node, const double *w)
@@ -167,7 +168,7 @@ expr *new_left_matmul(expr *u, const CSR_Matrix *A)
167168

168169
/* Allocate the type-specific struct */
169170
left_matmul_expr *lnode =
170-
(left_matmul_expr *) calloc(1, sizeof(left_matmul_expr));
171+
(left_matmul_expr *) SP_CALLOC(1, sizeof(left_matmul_expr));
171172
expr *node = &lnode->base;
172173
init_expr(node, d1, d2, u->n_vars, forward, jacobian_init_impl, eval_jacobian,
173174
is_affine, wsum_hess_init_impl, eval_wsum_hess, free_type_data);
@@ -178,8 +179,8 @@ expr *new_left_matmul(expr *u, const CSR_Matrix *A)
178179
(requiring size node->n_vars) and for transposing A (requiring size A->n).
179180
csc_to_csr_work is used for converting J_CSC to CSR (requiring
180181
node->size) */
181-
node->work->iwork = (int *) malloc(MAX(A->n, node->n_vars) * sizeof(int));
182-
lnode->csc_to_csr_work = (int *) malloc(node->size * sizeof(int));
182+
node->work->iwork = (int *) SP_MALLOC(MAX(A->n, node->n_vars) * sizeof(int));
183+
lnode->csc_to_csr_work = (int *) SP_MALLOC(node->size * sizeof(int));
183184
lnode->n_blocks = n_blocks;
184185

185186
/* store A and AT */
@@ -212,15 +213,15 @@ expr *new_left_matmul_dense(expr *u, int m, int n, const double *data)
212213
}
213214

214215
left_matmul_expr *lnode =
215-
(left_matmul_expr *) calloc(1, sizeof(left_matmul_expr));
216+
(left_matmul_expr *) SP_CALLOC(1, sizeof(left_matmul_expr));
216217
expr *node = &lnode->base;
217218
init_expr(node, d1, d2, u->n_vars, forward, jacobian_init_impl, eval_jacobian,
218219
is_affine, wsum_hess_init_impl, eval_wsum_hess, free_type_data);
219220
node->left = u;
220221
expr_retain(u);
221222

222-
node->work->iwork = (int *) malloc(MAX(n, node->n_vars) * sizeof(int));
223-
lnode->csc_to_csr_work = (int *) malloc(node->size * sizeof(int));
223+
node->work->iwork = (int *) SP_MALLOC(MAX(n, node->n_vars) * sizeof(int));
224+
lnode->csc_to_csr_work = (int *) SP_MALLOC(node->size * sizeof(int));
224225
lnode->n_blocks = n_blocks;
225226

226227
lnode->A = new_dense_matrix(m, n, data);

0 commit comments

Comments
 (0)