Skip to content

Commit d673b62

Browse files
Transurgeonclaude
andcommitted
Merge origin/main into parameter-support-v2
Resolve conflicts from tracked allocation PR (#70): use SP_CALLOC/SP_MALLOC macros in parameter-support files while keeping parameter-specific types. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2 parents dc8c22e + deb7cab commit d673b62

46 files changed

Lines changed: 215 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

include/utils/tracked_alloc.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef TRACKED_ALLOC_H
2+
#define TRACKED_ALLOC_H
3+
4+
#include <stddef.h>
5+
#include <stdlib.h>
6+
7+
extern size_t g_allocated_bytes;
8+
9+
static inline void *SP_MALLOC(size_t size)
10+
{
11+
void *ptr = malloc(size);
12+
if (ptr) g_allocated_bytes += size;
13+
return ptr;
14+
}
15+
16+
static inline void *SP_CALLOC(size_t count, size_t size)
17+
{
18+
void *ptr = calloc(count, size);
19+
if (ptr) g_allocated_bytes += count * size;
20+
return ptr;
21+
}
22+
23+
#endif /* TRACKED_ALLOC_H */

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/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 refresh_param_values(left_matmul_expr *lnode)
@@ -148,7 +149,7 @@ static void wsum_hess_init_impl(expr *node)
148149
/* work for computing A^T w*/
149150
int n_blocks = ((left_matmul_expr *) node)->n_blocks;
150151
int dim = ((left_matmul_expr *) node)->AT->m * n_blocks;
151-
node->work->dwork = (double *) malloc(dim * sizeof(double));
152+
node->work->dwork = (double *) SP_MALLOC(dim * sizeof(double));
152153
}
153154

154155
static void eval_wsum_hess(expr *node, const double *w)
@@ -218,7 +219,7 @@ expr *new_left_matmul(expr *param_node, expr *u, const CSR_Matrix *A)
218219

219220
/* Allocate the type-specific struct */
220221
left_matmul_expr *lnode =
221-
(left_matmul_expr *) calloc(1, sizeof(left_matmul_expr));
222+
(left_matmul_expr *) SP_CALLOC(1, sizeof(left_matmul_expr));
222223
expr *node = &lnode->base;
223224
init_expr(node, d1, d2, u->n_vars, forward, jacobian_init_impl, eval_jacobian,
224225
is_affine, wsum_hess_init_impl, eval_wsum_hess, free_type_data);
@@ -229,8 +230,8 @@ expr *new_left_matmul(expr *param_node, expr *u, const CSR_Matrix *A)
229230
(requiring size node->n_vars) and for transposing A (requiring size A->n).
230231
csc_to_csr_work is used for converting J_CSC to CSR (requiring
231232
node->size) */
232-
node->work->iwork = (int *) malloc(MAX(A->n, node->n_vars) * sizeof(int));
233-
lnode->csc_to_csr_work = (int *) malloc(node->size * sizeof(int));
233+
node->work->iwork = (int *) SP_MALLOC(MAX(A->n, node->n_vars) * sizeof(int));
234+
lnode->csc_to_csr_work = (int *) SP_MALLOC(node->size * sizeof(int));
234235
lnode->n_blocks = n_blocks;
235236

236237
/* store A and AT */
@@ -272,15 +273,15 @@ expr *new_left_matmul_dense(expr *param_node, expr *u, int m, int n,
272273
}
273274

274275
left_matmul_expr *lnode =
275-
(left_matmul_expr *) calloc(1, sizeof(left_matmul_expr));
276+
(left_matmul_expr *) SP_CALLOC(1, sizeof(left_matmul_expr));
276277
expr *node = &lnode->base;
277278
init_expr(node, d1, d2, u->n_vars, forward, jacobian_init_impl, eval_jacobian,
278279
is_affine, wsum_hess_init_impl, eval_wsum_hess, free_type_data);
279280
node->left = u;
280281
expr_retain(u);
281282

282-
node->work->iwork = (int *) malloc(MAX(n, node->n_vars) * sizeof(int));
283-
lnode->csc_to_csr_work = (int *) malloc(node->size * sizeof(int));
283+
node->work->iwork = (int *) SP_MALLOC(MAX(n, node->n_vars) * sizeof(int));
284+
lnode->csc_to_csr_work = (int *) SP_MALLOC(node->size * sizeof(int));
284285
lnode->n_blocks = n_blocks;
285286

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

src/atoms/affine/neg.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 <stdio.h>
2021
#include <stdlib.h>
2122
#include <string.h>
@@ -87,7 +88,7 @@ static bool is_affine(const expr *node)
8788

8889
expr *new_neg(expr *child)
8990
{
90-
expr *node = (expr *) calloc(1, sizeof(expr));
91+
expr *node = (expr *) SP_CALLOC(1, sizeof(expr));
9192
init_expr(node, child->d1, child->d2, child->n_vars, forward, jacobian_init_impl,
9293
eval_jacobian, is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);
9394
node->left = child;

src/atoms/affine/parameter.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 <stdlib.h>
2122
#include <string.h>
2223

@@ -58,7 +59,7 @@ static bool is_affine(const expr *node)
5859

5960
expr *new_parameter(int d1, int d2, int param_id, int n_vars, const double *values)
6061
{
61-
parameter_expr *pnode = (parameter_expr *) calloc(1, sizeof(parameter_expr));
62+
parameter_expr *pnode = (parameter_expr *) SP_CALLOC(1, sizeof(parameter_expr));
6263
expr *node = &pnode->base;
6364
init_expr(node, d1, d2, n_vars, forward, jacobian_init_impl, eval_jacobian,
6465
is_affine, wsum_hess_init_impl, eval_wsum_hess, NULL);

0 commit comments

Comments
 (0)