Add LAGraph_Matrix_Sum utility#407
Draft
michelp wants to merge 2 commits into
Draft
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.
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 a new utility
LAGraph_Matrix_Suminsrc/utility/that combines an array ofGrB_Matrixobjects into a single matrix using a binary operator to resolve duplicate entries.The algorithm:
nvals) across all input matrices.I,J,X) large enough to hold every entry.GrB_Matrix_extractTupleson each input matrix, appending into the shared buffer at a running offset.GrB_Matrix_buildwith the provideddupbinary 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;
Cis created with that type and dimensions. User-defined types returnGrB_NOT_IMPLEMENTED.Changes
src/utility/LAGraph_Matrix_Sum.c— new implementation (aLG_SUM_CASEmacro generates the per-built-in-type extract/build branches).Config/LAGraph.h.in— public declaration (include/LAGraph.his generated from this template).src/test/test_Matrix_Sum.c— tests.Testing
ctest -R Matrix_Sumpasses. The test binary covers:GrB_eWiseAdd, single-matrix copy, empty-matrix inclusion)nmatrices == 0, NULL array entry, dimension/type mismatch, UDT)