Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/atoms/affine.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
expr *new_add(expr *left, expr *right);
expr *new_neg(expr *child);

/* Is the node this atom? */
bool expr_is_add(const expr *node);
bool expr_is_broadcast(const expr *node);
bool expr_is_hstack(const expr *node);
bool expr_is_index(const expr *node);
bool expr_is_sum(const expr *node);
bool expr_is_scalar_mult(const expr *node);
bool expr_is_vector_mult(const expr *node);
/* A constant leaf: parameter node with param_id == PARAM_FIXED. */
bool expr_is_fixed_parameter(const expr *node);

expr *new_sum(expr *child, int axis);
expr *new_hstack(expr **args, int n_args, int n_vars);
expr *new_vstack(expr **args, int n_args, int n_vars);
Expand Down
11 changes: 11 additions & 0 deletions include/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ typedef bool (*is_affine_fn)(const struct expr *node);
typedef void (*free_type_data_fn)(struct expr *node);
typedef bool (*set_needs_refresh_children_fn)(struct expr *node);

/* Builds a new node of node's atom, with node's attributes, over other
children; right is NULL for a unary atom. Set by elementwise atoms only. */
typedef struct expr *(*new_same_atom_fn)(const struct expr *node, struct expr *left,
struct expr *right);
/* Do two nodes of one atom have the same attributes (e.g. the exponent of a
power)? Only atoms that set `new_same_atom` and have attributes need it. */
typedef bool (*same_attrs_fn)(const struct expr *a, const struct expr *b);

/* AFFINITY_UNKNOWN must be 0 (the workspace is calloc'ed) */
typedef enum
{
Expand Down Expand Up @@ -106,6 +114,9 @@ typedef struct expr
/* For nodes with children outside left/right (see expr_set_needs_refresh).
Must return whether those children contain an updatable parameter. */
set_needs_refresh_children_fn set_needs_refresh_children;
new_same_atom_fn new_same_atom; /* elementwise atoms; NULL otherwise */
same_attrs_fn same_attrs; /* NULL: no attributes */
int pass_id; /* scratch index of a graph pass; else 0 */
Expr_Work *work;
bool has_params; /* subtree has an updatable parameter */
bool needs_parameter_refresh; /* cached parameter data is stale */
Expand Down
8 changes: 6 additions & 2 deletions include/problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

typedef struct
{
double time_vectorize; /* re-vectorization pass in new_problem */
double time_init_derivatives;
double time_eval_jacobian;
double time_eval_gradient;
Expand Down Expand Up @@ -72,9 +73,12 @@ typedef struct problem
bool verbose;
} problem;

/* Retains objective and constraints (shared ownership with caller) */
/* Retains objective and constraints (shared ownership with caller). With
vectorize set, the roots are first re-vectorized (vectorize.h): the problem
then holds the roots the pass returns, which need not be the ones passed
in. */
problem *new_problem(expr *objective, expr **constraints, int n_constraints,
bool verbose);
bool verbose, bool vectorize);
void problem_init_jacobian(problem *prob);
void problem_init_hessian(problem *prob);
void problem_init_derivatives(problem *prob);
Expand Down
66 changes: 66 additions & 0 deletions include/vectorize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2026 Daniel Cederberg and William Zhang
*
* This file is part of the SparseDiffEngine project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef VECTORIZE_H
#define VECTORIZE_H

#include "expr.h"

/* Re-vectorization of expressions built by loops.
*
* A loop in the user's code leaves many small isomorphic expressions (the same
* atoms in the same arrangement, differing only in indices and constants): the
* terms of a chain of adds, the args of an hstack, or a list of constraints.
* The pass replaces k of them by one expression that computes their
* concatenation ("zip"), using that an elementwise atom commutes with
* concatenation:
*
* zip_j f(a_j, b_j) = f(zip_j a_j, zip_j b_j)
*
* At the leaves, entries of one array become a single gather (or the array),
* constants one constant, and k copies of a scalar a broadcast. For example
*
* cos(x[0]) + ... + cos(x[n-1]) -> sum(cos(x))
*
* Full sums and multiplications by constants have laws of their own; anything
* else is concatenated with an hstack.
*
* An elementwise atom sets `new_same_atom` in its constructor (expr.h), and
* `same_attrs` if it has attributes; atoms built on new_elementwise inherit
* `new_same_atom`. Other atoms need nothing.
*
* The pass must run before jacobian_init / wsum_hess_init. It changes the
* order of summation, so values agree to rounding only.
*/

/* Smaller groups are left alone, so that hand-vectorized code such as
sum(x**2) + sum(y**2) is not rewritten. */
#define VECTORIZE_MIN_GROUP 8

/* Vectorizes the DAG under roots and replaces each root by its equivalent.
The roots after the first n_separate are a concatenation (a problem's
constraints): runs of isomorphic ones are merged. Returns the new number
of roots.

The caller must hold a reference to each root it passes in, and keeps it.
Each root returned carries a new reference that belongs to the caller. */
int vectorize_exprs(expr **roots, int n_roots, int n_separate);

/* One root: expr *v = vectorize_expr(root); free_expr(root); */
expr *vectorize_expr(expr *root);

#endif /* VECTORIZE_H */
12 changes: 12 additions & 0 deletions src/atoms/affine/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ static bool is_affine(const expr *node)
return node->left->is_affine(node->left) && node->right->is_affine(node->right);
}

bool expr_is_add(const expr *node)
{
return node->forward_impl == forward;
}

static expr *new_same_atom(const expr *node, expr *left, expr *right)
{
(void) node;
return new_add(left, right);
}

expr *new_add(expr *left, expr *right)
{
assert(left->d1 == right->d1 && left->d2 == right->d2);
Expand All @@ -100,6 +111,7 @@ expr *new_add(expr *left, expr *right)
node->right = right;
expr_retain(left);
expr_retain(right);
node->new_same_atom = new_same_atom;

return node;
}
5 changes: 5 additions & 0 deletions src/atoms/affine/broadcast.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ static bool is_affine(const expr *node)
return node->left->is_affine(node->left);
}

bool expr_is_broadcast(const expr *node)
{
return node->forward_impl == forward;
}

expr *new_broadcast(expr *child, int d1, int d2)
{
// ---------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions src/atoms/affine/hstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ static void free_type_data(expr *node)
hnode->args = NULL;
}

bool expr_is_hstack(const expr *node)
{
return node->forward_impl == forward;
}

expr *new_hstack(expr **args, int n_args, int n_vars)
{
/* compute second dimension */
Expand Down
5 changes: 5 additions & 0 deletions src/atoms/affine/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ static void free_type_data(expr *node)
}
}

bool expr_is_index(const expr *node)
{
return node->forward_impl == forward;
}

expr *new_index(expr *child, int d1, int d2, const int *indices, int n_idxs)
{
assert(d1 * d2 == n_idxs);
Expand Down
8 changes: 8 additions & 0 deletions src/atoms/affine/neg.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ static bool is_affine(const expr *node)
return node->left->is_affine(node->left);
}

static expr *new_same_atom(const expr *node, expr *left, expr *right)
{
(void) node;
(void) right;
return new_neg(left);
}

expr *new_neg(expr *child)
{
expr *node = (expr *) sp_calloc(1, sizeof(expr));
Expand All @@ -94,6 +101,7 @@ expr *new_neg(expr *child)
eval_wsum_hess_impl, NULL);
node->left = child;
expr_retain(child);
node->new_same_atom = new_same_atom;

return node;
}
6 changes: 6 additions & 0 deletions src/atoms/affine/parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ static bool is_affine(const expr *node)
return true;
}

bool expr_is_fixed_parameter(const expr *node)
{
return node->forward_impl == forward &&
((const parameter_expr *) node)->param_id == PARAM_FIXED;
}

expr *new_parameter(int d1, int d2, int param_id, int n_vars, const double *values)
{
parameter_expr *pnode = (parameter_expr *) sp_calloc(1, sizeof(parameter_expr));
Expand Down
5 changes: 5 additions & 0 deletions src/atoms/affine/scalar_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ static void free_type_data(expr *node)
}
}

bool expr_is_scalar_mult(const expr *node)
{
return node->forward_impl == forward;
}

expr *new_scalar_mult(expr *param_node, expr *child)
{
scalar_mult_expr *mult_node =
Expand Down
5 changes: 5 additions & 0 deletions src/atoms/affine/sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ static bool is_affine(const expr *node)
return node->left->is_affine(node->left);
}

bool expr_is_sum(const expr *node)
{
return node->forward_impl == forward;
}

expr *new_sum(expr *child, int axis)
{
int d2 = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/atoms/affine/vector_mult.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ static bool is_affine(const expr *node)
return node->left->is_affine(node->left);
}

bool expr_is_vector_mult(const expr *node)
{
return node->forward_impl == forward;
}

expr *new_vector_mult(expr *param_node, expr *child)
{
vector_mult_expr *vnode =
Expand Down
7 changes: 7 additions & 0 deletions src/atoms/bivariate_full_dom/multiply.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ static bool is_affine(const expr *node)
return false;
}

static expr *new_same_atom(const expr *node, expr *left, expr *right)
{
(void) node;
return new_elementwise_mult(left, right);
}

expr *new_elementwise_mult(expr *left, expr *right)
{
elementwise_mult_expr *mul_node =
Expand All @@ -245,6 +251,7 @@ expr *new_elementwise_mult(expr *left, expr *right)
node->right = right;
expr_retain(left);
expr_retain(right);
node->new_same_atom = new_same_atom;

return node;
}
12 changes: 12 additions & 0 deletions src/atoms/elementwise_full_dom/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ bool is_affine_elementwise(const expr *node)
return false;
}

/* an attribute-free elementwise atom is its three functions */
static expr *new_same_atom(const expr *node, expr *left, expr *right)
{
(void) right;
expr *copy = new_elementwise(left);
copy->forward_impl = node->forward_impl;
copy->local_jacobian = node->local_jacobian;
copy->local_wsum_hess = node->local_wsum_hess;
return copy;
}

void init_elementwise(expr *node, expr *child)
{
/* Initialize base fields */
Expand All @@ -185,6 +196,7 @@ void init_elementwise(expr *node, expr *child)
/* Set left child */
node->left = child;
expr_retain(child);
node->new_same_atom = new_same_atom;
}

expr *new_elementwise(expr *child)
Expand Down
13 changes: 13 additions & 0 deletions src/atoms/elementwise_full_dom/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ static void local_wsum_hess(expr *node, double *out, const double *w)
}
}

static expr *new_same_atom(const expr *node, expr *left, expr *right)
{
(void) right;
return new_power(left, ((const power_expr *) node)->p);
}

static bool same_attrs(const expr *a, const expr *b)
{
return ((const power_expr *) a)->p == ((const power_expr *) b)->p;
}

expr *new_power(expr *child, double p)
{
/* Allocate the type-specific struct */
Expand All @@ -111,6 +122,8 @@ expr *new_power(expr *child, double p)

/* Set type-specific field */
pnode->p = p;
node->new_same_atom = new_same_atom;
node->same_attrs = same_attrs;

return node;
}
Loading
Loading