Skip to content

Add LAGraph_Matrix_Sum utility#407

Draft
michelp wants to merge 2 commits into
stablefrom
matrix-sum
Draft

Add LAGraph_Matrix_Sum utility#407
michelp wants to merge 2 commits into
stablefrom
matrix-sum

Conversation

@michelp

@michelp michelp commented Jun 12, 2026

Copy link
Copy Markdown
Member

Summary

Adds a new utility LAGraph_Matrix_Sum in src/utility/ that combines an array of GrB_Matrix objects into a single matrix using a binary operator to resolve duplicate entries.

The algorithm:

  1. Computes the total number of entries (nvals) across all input matrices.
  2. Allocates a single tuple buffer (I, J, X) large enough to hold every entry.
  3. Calls GrB_Matrix_extractTuples on each input matrix, appending into the shared buffer at a running offset.
  4. Calls GrB_Matrix_build with the provided dup binary operator to combine duplicate (i,j) coordinates.

With dup = GrB_PLUS_FP64 (for example) this computes the element-wise sum of all the matrices; other operators generalize it (max, times, etc.).

All input matrices must have identical dimensions and the same built-in type; C is created with that type and dimensions. User-defined types return GrB_NOT_IMPLEMENTED.

Changes

  • src/utility/LAGraph_Matrix_Sum.c — new implementation (a LG_SUM_CASE macro generates the per-built-in-type extract/build branches).
  • Config/LAGraph.h.in — public declaration (include/LAGraph.h is generated from this template).
  • src/test/test_Matrix_Sum.c — tests.

Testing

ctest -R Matrix_Sum passes. The test binary covers:

  • correctness (vs GrB_eWiseAdd, single-matrix copy, empty-matrix inclusion)
  • every built-in type branch
  • error handling (NULL args, nmatrices == 0, NULL array entry, dimension/type mismatch, UDT)
  • a brutal malloc-failure variant exercising the cleanup paths

LAGraph_Matrix_Sum combines an array of GrB_Matrix objects into a single
matrix using a binary operator to resolve duplicate entries.  It computes
the total number of entries across all inputs, allocates one shared tuple
buffer (I, J, X), extracts the tuples of each input matrix into the buffer,
and calls GrB_Matrix_build with the dup operator to combine duplicate (i,j)
coordinates.  With dup = GrB_PLUS_FP64 (for example) this computes the
element-wise sum of all the matrices.

All input matrices must have identical dimensions and the same built-in
type; user-defined types return GrB_NOT_IMPLEMENTED.

Includes a test (src/test/test_Matrix_Sum.c) covering correctness, all
built-in type branches, error handling, and a brutal malloc-failure variant.
@michelp michelp marked this pull request as draft June 12, 2026 01:26
The per-matrix extraction loop precomputes an offset (prefix-sum) array so
each matrix's tuples occupy a disjoint region of the shared (I, J, X) buffer.
That removes the loop-carried offset dependency and lets the extraction run
across LG_nthreads_outer threads with OpenMP; each GrB_Matrix_extractTuples
is still parallelized internally by GraphBLAS with LG_nthreads_inner threads
(the two-level nested model).  The public signature is unchanged: the thread
count follows the usual LAGraph convention via LAGraph_SetNumThreads.

Because GRB_TRY cannot return out of an OpenMP region, the first extraction
error is captured under a critical section and checked after the loop.

Adds test_Matrix_Sum_parallel, which sums many overlapping matrices with
multiple outer threads and compares against an independently accumulated
result.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant