Skip to content
Merged
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
8 changes: 2 additions & 6 deletions include/atoms/affine.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ expr *new_left_matmul_dense(expr *param_node, expr *u, int m, int n,
null. */
expr *new_right_matmul(expr *param_node, expr *u, const CSR_matrix *A);

/* Right matrix multiplication: f(x) @ A where A is a constant dense matrix
(in row-major, m x n, with values given by 'data') or a parameter
representing a dense m x n matrix in row-major.

The 'data' pointer only represents the values of the dense matrix if
param_node is null. If param_node is not null, data must be null. */
/* Right matrix multiplication: f(x) @ A, with A given as in
new_left_matmul_dense. */
expr *new_right_matmul_dense(expr *param_node, expr *u, int m, int n,
const double *data);

Expand Down
51 changes: 16 additions & 35 deletions include/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#define JAC_IDXS_NOT_SET -1
#define NOT_A_VARIABLE -1

/* Function pointer types */
struct expr;
typedef void (*forward_fn)(struct expr *node, const double *u);
typedef void (*jacobian_init_fn)(struct expr *node);
Expand All @@ -50,7 +49,7 @@ typedef enum
NON_AFFINE
} affinity;

/* Workspace for derivative computation */
/* derivative workspace */
typedef struct
{
double *dwork;
Expand All @@ -73,7 +72,6 @@ typedef struct
matrix *hess_term2; /* child wsum_hess workspace */
} Expr_Work;

/* Base expression node structure */
typedef struct expr
{
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -101,34 +99,16 @@ typedef struct expr
// ------------------------------------------------------------------------
is_affine_fn is_affine; /* memoizing wrapper */
is_affine_fn is_affine_impl; /* the atom's (recursive) rule */
local_jacobian_fn local_jacobian; /* used by elementwise univariate atoms*/
local_wsum_hess_fn local_wsum_hess; /* used by elementwise univariate atoms*/
free_type_data_fn free_type_data; /* Cleanup for type-specific fields */
/* Recursion hook for expr_set_needs_refresh, which on its own only
descends left/right. Nodes that hold parameter-bearing subtrees
elsewhere install this so the walk reaches them: the parameter leaf
(reports itself), hstack (args[]) and the coefficient atoms
scalar_mult, vector_mult, kron, convolve, left_matmul and quad_form
(param_source). NULL everywhere else.

Contract: the hook MUST return whether the nodes it reaches contain
an updatable parameter (param_id >= 0). The walk ORs the result into
has_params; a hook that returns false lets the subtree be memoized
parameter-free and pruned from the second update on. */
local_jacobian_fn local_jacobian; /* elementwise univariate atoms */
local_wsum_hess_fn local_wsum_hess; /* elementwise univariate atoms */
free_type_data_fn free_type_data; /* frees type-specific fields */

/* 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;
Expr_Work *work; /* derivative workspace */
/* Does this subtree contain an updatable parameter? Starts true so
nothing is pruned before the first walk; expr_set_needs_refresh
refines and memoizes it from the left/right walk and the
set_needs_refresh_children hook. */
bool has_params;
/* Set to true by problem_update_params() via expr_set_needs_refresh()
on every node whose subtree contains an updatable parameter;
parameter-free subtrees are skipped. Atoms that cache parameter data
(e.g. left_matmul_dense) check this flag before their forward
pass: if true, they refresh their cached matrices from
param_source->value and clear the flag to false. */
bool needs_parameter_refresh;
Expr_Work *work;
bool has_params; /* subtree has an updatable parameter */
bool needs_parameter_refresh; /* cached parameter data is stale */

// name of node just for debugging - should be removed later
char name[32];
Expand Down Expand Up @@ -168,13 +148,14 @@ void wsum_hess_init(expr *node);
void eval_jacobian(expr *node);
void eval_wsum_hess(expr *node, const double *w);

/* Mark the subtree dirty and report whether it contains an updatable
* parameter. A subtree known to be parameter-free is skipped entirely: its
* values and derivatives cannot have changed, so re-arming it would only
* force a recompute of a result we already hold. */
/* Mark the subtree dirty (needs_parameter_refresh) and report whether it
* contains an updatable parameter. The walk descends left/right and calls the
* set_needs_refresh_children hook for children held elsewhere. The answer is
* memoized in has_params, which starts true; a subtree known to be
* parameter-free is skipped from then on, so a hook that wrongly returns false
* gets its subtree pruned from later updates. */
bool expr_set_needs_refresh(expr *node);

/* Reference counting helpers */
void expr_retain(expr *node);

#endif /* EXPR_H */
Loading