Skip to content

Commit 121c2b3

Browse files
committed
permuted dense sum different rows
1 parent cf6a4e9 commit 121c2b3

6 files changed

Lines changed: 27 additions & 31 deletions

File tree

include/utils/matrix.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,15 @@ typedef void (*matrix_diag_vec_fill_values_fn)(matrix *A, matrix *out);
109109
/* Allocate C as a row-wise reduction of A. The reduction pattern is chosen by
110110
axis:
111111
- axis = -1: sum all rows of A. C has shape (1, A->n).
112-
C[0, k] = sum_{i in [0, A->m)} A[i, k].
113-
- axis = 0: block-sum rows in consecutive groups of d1 (requires
114-
A->m % d1 == 0). C has shape (A->m / d1, A->n).
115-
C[j, k] = sum_{i in [j*d1, (j+1)*d1)} A[i, k].
116-
- axis = 1: stride-sum rows at modular spacing d1 (requires
117-
A->m % d1 == 0). C has shape (d1, A->n).
118-
C[j, k] = sum_{i : i % d1 == j} A[i, k].
119-
d1 is ignored when axis == -1.
120-
121-
Caller pre-allocates idx_map of size A->nnz. On return idx_map[ii] holds the
122-
position in C's base.x where A's base.x[ii] scatter-adds. A values-fill pass
123-
can compute C->x by zeroing it and accumulating A->x[ii] into
124-
C->x[idx_map[ii]] for ii in [0, A->nnz). */
125-
typedef matrix *(*matrix_sum_alloc_fn)(matrix *A, int axis, int d1, int *idx_map);
112+
- axis = 0: block-sum rows in consecutive groups of d1. C has shape (A->m
113+
/ d1, A->n). C[j, :] = sum_{i in [j*d1, (j+1)*d1)} A[i, :].
114+
- axis = 1: stride-sum rows at modular spacing d1. C has shape (d1, A->n).
115+
C[j, :] = sum_{i : i % d1 == j} A[i, :].
116+
117+
Caller pre-allocates idx_map of size A->nnz that can be used to compute the
118+
numerical result of the operation using via accumulation. */
119+
typedef matrix *(*matrix_sum_row_partition_alloc_fn)(matrix *A, int axis, int d1,
120+
int *idx_map);
126121

127122
typedef void (*matrix_free_fn)(matrix *self);
128123

@@ -159,7 +154,7 @@ struct matrix
159154
matrix_broadcast_fill_values_fn broadcast_fill_values;
160155
matrix_diag_vec_alloc_fn diag_vec_alloc;
161156
matrix_diag_vec_fill_values_fn diag_vec_fill_values;
162-
matrix_sum_alloc_fn sum_alloc;
157+
matrix_sum_row_partition_alloc_fn sum_row_partition_alloc;
163158

164159
/* Lifecycle */
165160
matrix_free_fn free_fn;

src/atoms/affine/sum.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ static void jacobian_init_impl(expr *node)
8787
sum_expr *snode = (sum_expr *) node;
8888
jacobian_init(x);
8989

90-
/* sum_alloc fills idx_map so eval_jacobian can accumulate from
90+
/* sum_row_partition_alloc fills idx_map so eval_jacobian can accumulate from
9191
child->jacobian->x. */
9292
snode->idx_map = sp_malloc(x->jacobian->nnz * sizeof(int));
93-
node->jacobian =
94-
x->jacobian->sum_alloc(x->jacobian, snode->axis, x->d1, snode->idx_map);
93+
node->jacobian = x->jacobian->sum_row_partition_alloc(x->jacobian, snode->axis,
94+
x->d1, snode->idx_map);
9595
}
9696

9797
static void eval_jacobian(expr *node)

src/utils/permuted_dense.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ static matrix *sum_evenly_spaced_rows_pd_alloc(matrix *self, int d1, int *idx_ma
523523
return C;
524524
}
525525

526-
static matrix *permuted_dense_vtable_sum_alloc(matrix *self, int axis, int d1,
527-
int *idx_map)
526+
static matrix *permuted_dense_vtable_sum_row_partition_alloc(matrix *self, int axis,
527+
int d1, int *idx_map)
528528
{
529529
if (axis == -1)
530530
{
@@ -562,7 +562,7 @@ static void wire_vtable(permuted_dense *pd)
562562
pd->base.broadcast_fill_values = permuted_dense_vtable_broadcast_fill_values;
563563
pd->base.diag_vec_alloc = permuted_dense_vtable_diag_vec_alloc;
564564
pd->base.diag_vec_fill_values = permuted_dense_vtable_diag_vec_fill_values;
565-
pd->base.sum_alloc = permuted_dense_vtable_sum_alloc;
565+
pd->base.sum_row_partition_alloc = permuted_dense_vtable_sum_row_partition_alloc;
566566
pd->base.refresh_csc_values = permuted_dense_refresh_csc_values;
567567
}
568568

src/utils/sparse_matrix.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ static void sparse_refresh_csc_values(matrix *self)
312312
csr_to_csc_fill_values(sm->csr, sm->csc_cache, sm->csc_iwork);
313313
}
314314

315-
static matrix *sparse_sum_alloc(matrix *self, int axis, int d1, int *idx_map)
315+
static matrix *sparse_sum_row_partition_alloc(matrix *self, int axis, int d1,
316+
int *idx_map)
316317
{
317318
CSR_matrix *A = ((sparse_matrix *) self)->csr;
318319
int m;
@@ -369,7 +370,7 @@ static void wire_vtable(sparse_matrix *sm)
369370
sm->base.broadcast_fill_values = sparse_broadcast_fill_values;
370371
sm->base.diag_vec_alloc = sparse_diag_vec_alloc;
371372
sm->base.diag_vec_fill_values = sparse_diag_vec_fill_values;
372-
sm->base.sum_alloc = sparse_sum_alloc;
373+
sm->base.sum_row_partition_alloc = sparse_sum_row_partition_alloc;
373374
sm->base.refresh_csc_values = sparse_refresh_csc_values;
374375
sm->base.free_fn = sparse_free;
375376
}

src/utils/stacked_pd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ static void assert_disjoint_row_perms(int n_blocks, permuted_dense *const *block
394394
existing CSR helper, then re-index idx_map from CSR ordering into A's
395395
block-major base.x ordering so a downstream values-fill pass can read
396396
directly from A->base.x. */
397-
static matrix *stacked_pd_vtable_sum_alloc(matrix *self, int axis, int d1,
398-
int *idx_map)
397+
static matrix *stacked_pd_vtable_sum_row_partition_alloc(matrix *self, int axis,
398+
int d1, int *idx_map)
399399
{
400400
stacked_pd *spd = (stacked_pd *) self;
401401

@@ -488,7 +488,7 @@ static void wire_vtable(stacked_pd *spd)
488488
spd->base.diag_vec_fill_values = stacked_pd_vtable_diag_vec_fill_values;
489489
spd->base.broadcast_alloc = stacked_pd_vtable_broadcast_alloc;
490490
spd->base.broadcast_fill_values = stacked_pd_vtable_broadcast_fill_values;
491-
spd->base.sum_alloc = stacked_pd_vtable_sum_alloc;
491+
spd->base.sum_row_partition_alloc = stacked_pd_vtable_sum_row_partition_alloc;
492492
}
493493

494494
matrix *new_stacked_pd_unchecked(int m, int n, int n_blocks, permuted_dense **blocks,

tests/utils/test_permuted_dense.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,8 @@ const char *test_BA_pd_matrices_fast_path(void)
10471047
return 0;
10481048
}
10491049

1050-
/* Direct vtable tests for sum_alloc. The test PD represents a (6, 4) matrix
1051-
with a (3, 2) dense block at rows {0, 3, 4}, cols {1, 3}. We exercise all
1050+
/* Direct vtable tests for sum_row_partition_alloc. The test PD represents a (6, 4)
1051+
matrix with a (3, 2) dense block at rows {0, 3, 4}, cols {1, 3}. We exercise all
10521052
three axes; for axis=0 (d1=2) the buckets {0/2, 3/2, 4/2} = {0, 1, 2}
10531053
are all distinct and non-decreasing (linear-scan dedupe); for axis=1
10541054
(d1=3) the buckets {0%3, 3%3, 4%3} = {0, 0, 1} collapse two input rows
@@ -1062,7 +1062,7 @@ const char *test_permuted_dense_sum_all_rows(void)
10621062
matrix *M = new_permuted_dense(6, 4, 3, 2, row_perm, col_perm, X);
10631063

10641064
int idx_map[6];
1065-
matrix *out = M->sum_alloc(M, -1, /*d1 unused*/ 0, idx_map);
1065+
matrix *out = M->sum_row_partition_alloc(M, -1, /*d1 unused*/ 0, idx_map);
10661066

10671067
mu_assert("output is PD", out->is_permuted_dense);
10681068
permuted_dense *opd = (permuted_dense *) out;
@@ -1092,7 +1092,7 @@ const char *test_permuted_dense_sum_block_of_rows(void)
10921092

10931093
int d1 = 2; /* child shape (d1, d2) = (2, 3); output rows = d2 = 3 */
10941094
int idx_map[6];
1095-
matrix *out = M->sum_alloc(M, 0, d1, idx_map);
1095+
matrix *out = M->sum_row_partition_alloc(M, 0, d1, idx_map);
10961096

10971097
mu_assert("output is PD", out->is_permuted_dense);
10981098
permuted_dense *opd = (permuted_dense *) out;
@@ -1121,7 +1121,7 @@ const char *test_permuted_dense_sum_evenly_spaced_rows(void)
11211121

11221122
int d1 = 3; /* output rows = d1 = 3, buckets = {0%3, 3%3, 4%3} = {0, 0, 1} */
11231123
int idx_map[6];
1124-
matrix *out = M->sum_alloc(M, 1, d1, idx_map);
1124+
matrix *out = M->sum_row_partition_alloc(M, 1, d1, idx_map);
11251125

11261126
mu_assert("output is PD", out->is_permuted_dense);
11271127
permuted_dense *opd = (permuted_dense *) out;

0 commit comments

Comments
 (0)