Add LAGraph_Matrix_Sum and LAGraph_Matrix_Binary_Sum utilities#407
Draft
michelp wants to merge 3 commits into
Draft
Add LAGraph_Matrix_Sum and LAGraph_Matrix_Binary_Sum utilities#407michelp wants to merge 3 commits into
michelp wants to merge 3 commits into
Conversation
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.
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.
Adds LAGraph_Matrix_Binary_Sum, which combines an array of matrices into a single matrix with the same interface and result as LAGraph_Matrix_Sum but a different technique: a pairwise binary reduction tree built from GrB_eWiseAdd (per the GraphChallenge "Anonymized Network Sensing" paper, "Binary Summation of Traffic Matrices"), rather than one large extract/build. At each level the matrices are summed in disjoint adjacent pairs using the dup operator; an unpaired (odd) trailing matrix is carried up to the next level unchanged, until a single matrix remains. The independent pair-sums within a level are parallelized across LG_nthreads_outer threads. Working on the smaller intermediate matrices of the tree keeps the active data in faster memory. All intermediate matrices are tracked in a single pre-NULLed Pool array with deterministic per-pair slots, so cleanup is a simple sweep that is correct on any failure path. Unlike LAGraph_Matrix_Sum, dup must be non-NULL since GrB_eWiseAdd requires a binary operator. Includes tests for correctness, odd counts (carry path), all built-in types, parallel reduction with multiple outer threads, error handling, and a brutal malloc-failure variant.
michelp
commented
Jun 26, 2026
| // funding and support from the U.S. Government (see Acknowledgments.txt file). | ||
| // DM22-0790 | ||
|
|
||
| // Contributed by Michel Pelletier. |
Member
Author
There was a problem hiding this comment.
Original idea by Tim Davis.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two
src/utility/functions that combine an array ofGrB_Matrixobjects into a single matrix, using a binary operator to resolve duplicate(i,j)entries. They compute the same result via two different techniques, so they can be compared:LAGraph_Matrix_Sum— concatenate-and-build: extract every matrix's tuples into one shared buffer, then a singleGrB_Matrix_build.LAGraph_Matrix_Binary_Sum— pairwise binary reduction tree ofGrB_eWiseAdd(per the GraphChallenge "Anonymized Network Sensing" paper, Fig. 3 "Binary Summation of Traffic Matrices").With
dup = GrB_PLUS_FP64(for example) either 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;Cis created with that type and dimensions. User-defined types returnGrB_NOT_IMPLEMENTED.LAGraph_Matrix_Sum(concatenate + build)nvalsacross all inputs and a per-matrix prefix-sum offset.I,J,X) large enough for every entry.LG_nthreads_outerthreads, since the regions never overlap.GrB_Matrix_buildwith the provideddupoperator to combine duplicate coordinates.LAGraph_Matrix_Binary_Sum(pairwise binary reduction)At each level the matrices are summed in disjoint adjacent pairs with
GrB_eWiseAddusingdup; an unpaired (odd) trailing matrix is carried up to the next level unchanged, until a single matrix remains. The independent pair-sums within a level run concurrently acrossLG_nthreads_outerthreads. Working on the smaller intermediate matrices of the tree keeps the active data in faster memory: adding two matrices each with N entries yields a matrix with fewer than 2N entries, so the relative work shrinks as the matrices grow.All intermediate matrices are tracked in a single pre-NULLed pool with deterministic per-pair slots, so cleanup is a simple sweep that is correct on any failure path. Unlike
LAGraph_Matrix_Sum,dupmust be non-NULL, sinceGrB_eWiseAddrequires a binary operator (NULLdupreturnsGrB_NULL_POINTER).Changes
src/utility/LAGraph_Matrix_Sum.c— concatenate/build implementation.src/utility/LAGraph_Matrix_Binary_Sum.c— binary-reduction implementation.Config/LAGraph.h.in— public declarations (include/LAGraph.his generated from this template).src/test/test_Matrix_Sum.c,src/test/test_Matrix_Binary_Sum.c— tests.Testing
ctest -R Matrix_Sumandctest -R Matrix_Binary_Sumpass; clean builds with OpenMP on and off. The test binaries cover:GrB_eWiseAdd, single-matrix copy, empty-matrix inclusion)nmatrices == 0, NULL array entry, dimension/type mismatch, UDT, and NULLdupfor the binary variant)