Skip to content

Commit 975676d

Browse files
dance858claude
andcommitted
Keep the retired CSR row-sum kernels and tile_int under old-code
sum_all_rows_csr_alloc, sum_block_of_rows_csr_alloc and sum_evenly_spaced_rows_csr_alloc are standalone CSR operations worth keeping for a sparse linear algebra library even though the engine no longer calls them; tile_int likewise. Moved verbatim into the old-code folder, which is still compiled. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B6Bs765i1HTbqx3LUu6LMP
1 parent 29ccaff commit 975676d

4 files changed

Lines changed: 268 additions & 0 deletions

File tree

include/old-code/old_CSR_sum.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,23 @@ void sum_all_rows_csr_fill_values(const CSR_matrix *A, CSR_matrix *C,
5858
void sum_block_of_rows_csr_fill_values(const CSR_matrix *A, CSR_matrix *C,
5959
const int *idx_map);
6060

61+
/* Row-sum kernels that also produce an idx_map (input nnz -> position in C->x),
62+
so values can be filled by zeroing C->x and calling accumulator() from
63+
utils/CSR_sum.h. C must be pre-allocated with capacity >= A->nnz; iwork must
64+
have size max(A->n, A->nnz); idx_map must have size A->nnz. C->nnz is set. */
65+
66+
/* All rows of A into the single row of C (C->m == 1). */
67+
void sum_all_rows_csr_alloc(const CSR_matrix *A, CSR_matrix *C, int *iwork,
68+
int *idx_map);
69+
70+
/* Consecutive blocks of row_block_size rows of A into one row of C each
71+
(C->m == A->m / row_block_size). */
72+
void sum_block_of_rows_csr_alloc(const CSR_matrix *A, CSR_matrix *C,
73+
int row_block_size, int *iwork, int *idx_map);
74+
75+
/* Rows of A at stride row_spacing into one row of C each (C->m == row_spacing):
76+
C[j, :] = sum_{i : i % row_spacing == j} A[i, :]. */
77+
void sum_evenly_spaced_rows_csr_alloc(const CSR_matrix *A, CSR_matrix *C,
78+
int row_spacing, int *iwork, int *idx_map);
79+
6180
#endif /* OLD_CSR_SUM_H */

include/old-code/old_mini_numpy.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2026 Daniel Cederberg and William Zhang
3+
*
4+
* This file is part of the SparseDiffEngine project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#ifndef OLD_MINI_NUMPY_H
19+
#define OLD_MINI_NUMPY_H
20+
21+
/* Example: a = [1, 2], len = 2, tiles = 3, result = [1, 2, 1, 2, 1, 2].
22+
Retired from utils/mini_numpy.h with the CSR broadcast kernel, its last
23+
caller. */
24+
void tile_int(int *result, const int *a, int len, int tiles);
25+
26+
#endif /* OLD_MINI_NUMPY_H */

src/old-code/old_CSR_sum.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "old-code/old_CSR_sum.h"
1919
#include "utils/CSR_matrix.h"
2020
#include "utils/int_double_pair.h"
21+
#include "utils/utils.h"
2122
#include <assert.h>
2223
#include <stdlib.h>
2324
#include <string.h>
@@ -330,3 +331,198 @@ void sum_spaced_rows_into_row_csr(const CSR_matrix *A, CSR_matrix *C,
330331

331332
C->p[1] = C->nnz;
332333
}
334+
335+
// ------------------------------------------------------------------------------------
336+
// Row-sum kernels with an idx_map (input nnz -> output position), retired from the
337+
// engine when the sum atom moved onto the generic row_reduce primitive. Kept as
338+
// standalone CSR operations. Fill values with accumulator() from utils/CSR_sum.h
339+
// after zeroing C->x.
340+
// ------------------------------------------------------------------------------------
341+
342+
/* iwork must have size max(A->n, A->nnz), and idx_map must have size A->nnz */
343+
void sum_all_rows_csr_alloc(const CSR_matrix *A, CSR_matrix *C, int *iwork,
344+
int *idx_map)
345+
{
346+
// -------------------------------------------------------------------
347+
// Build sparsity pattern of the summed row
348+
// -------------------------------------------------------------------
349+
int *cols = iwork;
350+
memcpy(cols, A->i, A->nnz * sizeof(int));
351+
sort_int_array(cols, A->nnz);
352+
353+
int unique_nnz = 0;
354+
int prev_col = -1;
355+
for (int j = 0; j < A->nnz; j++)
356+
{
357+
if (cols[j] != prev_col)
358+
{
359+
C->i[unique_nnz] = cols[j];
360+
prev_col = cols[j];
361+
unique_nnz++;
362+
}
363+
}
364+
365+
C->p[0] = 0;
366+
C->p[1] = unique_nnz;
367+
C->nnz = unique_nnz;
368+
369+
// -------------------------------------------------------------------
370+
// Map child values to summed-row positions. col_to_pos maps
371+
// column indices to positions in C's row.
372+
// -------------------------------------------------------------------
373+
int *col_to_pos = iwork;
374+
for (int idx = 0; idx < unique_nnz; idx++)
375+
{
376+
col_to_pos[C->i[idx]] = idx;
377+
}
378+
379+
for (int i = 0; i < A->m; i++)
380+
{
381+
for (int j = A->p[i]; j < A->p[i + 1]; j++)
382+
{
383+
idx_map[j] = col_to_pos[A->i[j]];
384+
}
385+
}
386+
}
387+
388+
/* iwork must have size max(A->n, A->nnz), and idx_map must have size A->nnz */
389+
void sum_block_of_rows_csr_alloc(const CSR_matrix *A, CSR_matrix *C,
390+
int row_block_size, int *iwork, int *idx_map)
391+
{
392+
assert(A->m % row_block_size == 0);
393+
int n_blocks = A->m / row_block_size;
394+
assert(C->m == n_blocks);
395+
396+
C->n = A->n;
397+
C->p[0] = 0;
398+
int cursor = 0;
399+
400+
int *cols = iwork;
401+
int *col_to_pos = iwork;
402+
403+
for (int block = 0; block < n_blocks; block++)
404+
{
405+
int start_row = block * row_block_size;
406+
int end_row = start_row + row_block_size;
407+
408+
// -----------------------------------------------------------------
409+
// Build sparsity pattern of the row resulting from summing
410+
// the block of rows from A
411+
// -----------------------------------------------------------------
412+
C->p[block] = cursor;
413+
int count = 0;
414+
for (int row = start_row; row < end_row; row++)
415+
{
416+
for (int j = A->p[row]; j < A->p[row + 1]; j++)
417+
{
418+
cols[count++] = A->i[j];
419+
}
420+
}
421+
422+
/* Sort columns and write unique pattern into C->i */
423+
sort_int_array(cols, count);
424+
425+
int unique_nnz = 0;
426+
int prev_col = -1;
427+
for (int t = 0; t < count; t++)
428+
{
429+
int col = cols[t];
430+
if (t == 0 || col != prev_col)
431+
{
432+
C->i[cursor + unique_nnz] = col;
433+
prev_col = col;
434+
unique_nnz++;
435+
}
436+
}
437+
438+
cursor += unique_nnz;
439+
C->p[block + 1] = cursor;
440+
441+
// -----------------------------------------------------------------
442+
// Build idx_map for all entries in this block
443+
// -----------------------------------------------------------------
444+
int row_start = C->p[block];
445+
for (int idx = 0; idx < unique_nnz; idx++)
446+
{
447+
col_to_pos[C->i[row_start + idx]] = row_start + idx;
448+
}
449+
450+
for (int row = start_row; row < end_row; row++)
451+
{
452+
for (int j = A->p[row]; j < A->p[row + 1]; j++)
453+
{
454+
idx_map[j] = col_to_pos[A->i[j]];
455+
}
456+
}
457+
}
458+
459+
C->nnz = cursor;
460+
}
461+
462+
/* iwork must have size max(A->n, A->nnz), and idx_map must have size A->nnz */
463+
void sum_evenly_spaced_rows_csr_alloc(const CSR_matrix *A, CSR_matrix *C,
464+
int row_spacing, int *iwork, int *idx_map)
465+
{
466+
assert(C->m == row_spacing);
467+
C->n = A->n;
468+
C->p[0] = 0;
469+
int cursor = 0;
470+
471+
int *cols = iwork;
472+
int *col_to_pos = iwork;
473+
474+
for (int C_row = 0; C_row < C->m; C_row++)
475+
{
476+
// -----------------------------------------------------------------
477+
// Build sparsity pattern of the row resulting from summing
478+
// evenly spaced rows from A
479+
// -----------------------------------------------------------------
480+
C->p[C_row] = cursor;
481+
int count = 0;
482+
for (int row = C_row; row < A->m; row += row_spacing)
483+
{
484+
for (int j = A->p[row]; j < A->p[row + 1]; j++)
485+
{
486+
cols[count++] = A->i[j];
487+
}
488+
}
489+
490+
/* Sort columns and write unique pattern into C->i */
491+
sort_int_array(cols, count);
492+
493+
int unique_nnz = 0;
494+
int prev_col = -1;
495+
for (int t = 0; t < count; t++)
496+
{
497+
int col = cols[t];
498+
if (t == 0 || col != prev_col)
499+
{
500+
C->i[cursor + unique_nnz] = col;
501+
prev_col = col;
502+
unique_nnz++;
503+
}
504+
}
505+
506+
cursor += unique_nnz;
507+
C->p[C_row + 1] = cursor;
508+
509+
// -----------------------------------------------------------------
510+
// Build idx_map for all entries in evenly spaced rows
511+
// -----------------------------------------------------------------
512+
int row_start = C->p[C_row];
513+
for (int idx = 0; idx < unique_nnz; idx++)
514+
{
515+
col_to_pos[C->i[row_start + idx]] = row_start + idx;
516+
}
517+
518+
for (int row = C_row; row < A->m; row += row_spacing)
519+
{
520+
for (int j = A->p[row]; j < A->p[row + 1]; j++)
521+
{
522+
idx_map[j] = col_to_pos[A->i[j]];
523+
}
524+
}
525+
}
526+
527+
C->nnz = cursor;
528+
}

src/old-code/old_mini_numpy.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2026 Daniel Cederberg and William Zhang
3+
*
4+
* This file is part of the SparseDiffEngine project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#include "old-code/old_mini_numpy.h"
19+
#include <string.h>
20+
21+
void tile_int(int *result, const int *a, int len, int tiles)
22+
{
23+
for (int i = 0; i < tiles; i++)
24+
{
25+
memcpy(result + i * len, a, len * sizeof(int));
26+
}
27+
}

0 commit comments

Comments
 (0)