Skip to content

Commit ce5fa77

Browse files
committed
replace for loop with cblas
1 parent a7d8dfa commit ce5fa77

3 files changed

Lines changed: 12 additions & 14 deletions

File tree

include/problem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ typedef struct
3636
int nnz_affine;
3737
int nnz_nonlinear; /* jacobian of nonlinear constraints */
3838
int nnz_hessian;
39+
int n_vars;
40+
int total_constraint_size;
3941
} Diff_engine_stats;
4042

4143
typedef struct problem

src/other/quad_form.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ static void eval_jacobian(expr *node)
7979
{
8080
/* jacobian = 2 * (Q @ x)^T */
8181
csr_matvec(Q, x->value, node->jacobian->x, 0);
82-
83-
for (int j = 0; j < x->size; j++)
84-
{
85-
node->jacobian->x[j] *= 2.0;
86-
}
82+
cblas_dscal(x->size, 2.0, node->jacobian->x, 1);
8783
}
8884
else
8985
{
@@ -106,10 +102,7 @@ static void eval_jacobian(expr *node)
106102
csc_matvec_fill_values(x->work->jacobian_csc, node->work->dwork,
107103
node->jacobian);
108104

109-
for (int j = 0; j < node->jacobian->nnz; j++)
110-
{
111-
node->jacobian->x[j] *= 2.0;
112-
}
105+
cblas_dscal(node->jacobian->nnz, 2.0, node->jacobian->x, 1);
113106
}
114107
}
115108

@@ -179,11 +172,8 @@ static void eval_wsum_hess(expr *node, const double *w)
179172
{
180173
/* TODO: do we want to compute this hessian only once (up to a scaling)?
181174
* Maybe unnecessary optimization. */
182-
double *H = node->wsum_hess->x;
183-
for (int i = 0; i < Q->nnz; i++)
184-
{
185-
H[i] = two_w * Q->x[i];
186-
}
175+
memcpy(node->wsum_hess->x, Q->x, Q->nnz * sizeof(double));
176+
cblas_dscal(Q->nnz, two_w, node->wsum_hess->x, 1);
187177
}
188178
else
189179
{

src/problem.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ problem *new_problem(expr *objective, expr **constraints, int n_constraints,
6565
prob->stats.time_forward_constraints = 0.0;
6666
prob->stats.nnz_affine = 0;
6767
prob->stats.nnz_nonlinear = 0;
68+
prob->stats.nnz_hessian = 0;
69+
prob->stats.n_vars = prob->n_vars;
70+
prob->stats.total_constraint_size = prob->total_constraint_size;
6871

6972
prob->verbose = verbose;
7073

@@ -274,6 +277,9 @@ static inline void print_end_message(const Diff_engine_stats *stats)
274277
DIFF_ENGINE_VERSION);
275278

276279
printf("\nProblem statistics:\n");
280+
printf(" Number of variables: %d\n", stats->n_vars);
281+
printf(" Number of constraints: %d\n",
282+
stats->total_constraint_size);
277283
printf(" Affine constraints (nnz): %d\n", stats->nnz_affine);
278284
printf(" Jacobian nonlinear constraints (nnz): %d\n", stats->nnz_nonlinear);
279285
printf(" Lagrange Hessian (nnz): %d\n", stats->nnz_hessian);

0 commit comments

Comments
 (0)