Skip to content

Commit 6d3dc83

Browse files
committed
Add LAGraph_Matrix_Binary_Sum utility
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.
1 parent 1895a88 commit 6d3dc83

4 files changed

Lines changed: 793 additions & 0 deletions

File tree

Config/LAGraph.h.in

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,57 @@ int LAGraph_Matrix_Sum
16131613
char *msg
16141614
) ;
16151615

1616+
//------------------------------------------------------------------------------
1617+
// LAGraph_Matrix_Binary_Sum: sum an array of matrices by binary reduction
1618+
//------------------------------------------------------------------------------
1619+
1620+
/** LAGraph_Matrix_Binary_Sum: combines an array of matrices into a single
1621+
* matrix C, computing the same result as LAGraph_Matrix_Sum but with a
1622+
* different technique: a pairwise binary reduction tree built from
1623+
* GrB_eWiseAdd (as in the GraphChallenge "Anonymized Network Sensing" paper,
1624+
* "Binary Summation of Traffic Matrices"). At each level the matrices are
1625+
* summed in disjoint adjacent pairs using the binary operator dup; an unpaired
1626+
* (odd) trailing matrix is carried up to the next level unchanged, until a
1627+
* single matrix remains. The dup operator has the same set-union semantics as
1628+
* the dup of GrB_Matrix_build (applied where both inputs have an entry, lone
1629+
* entries pass through), so the result equals that of LAGraph_Matrix_Sum.
1630+
* With dup = GrB_PLUS_FP64 (for example) this computes the element-wise sum of
1631+
* all the matrices. The independent pair-sums within a level are parallelized
1632+
* across LG_nthreads_outer threads.
1633+
*
1634+
* All input matrices must have identical dimensions and identical built-in
1635+
* type; C is created with that same type and dimensions. Unlike
1636+
* LAGraph_Matrix_Sum, dup must be non-NULL, since GrB_eWiseAdd requires a
1637+
* binary operator.
1638+
*
1639+
* @param[out] C the resulting matrix.
1640+
* @param[in] Matrices array of nmatrices input matrices.
1641+
* @param[in] nmatrices number of matrices in Matrices (must be >= 1).
1642+
* @param[in] dup binary operator to combine (i,j) entries; must be
1643+
* non-NULL.
1644+
* @param[in,out] msg any error messages.
1645+
*
1646+
* @retval GrB_SUCCESS if successful.
1647+
* @retval GrB_NULL_POINTER if C, Matrices, any Matrices[k], or dup is NULL.
1648+
* @retval GrB_INVALID_VALUE if nmatrices is zero.
1649+
* @retval GrB_DIMENSION_MISMATCH if the inputs do not all share dimensions.
1650+
* @retval GrB_DOMAIN_MISMATCH if the inputs do not all share a type.
1651+
* @retval GrB_NOT_IMPLEMENTED if the matrix type is user-defined.
1652+
* @returns any GraphBLAS errors that may have been encountered.
1653+
*/
1654+
1655+
LAGRAPH_PUBLIC
1656+
int LAGraph_Matrix_Binary_Sum
1657+
(
1658+
// output:
1659+
GrB_Matrix *C, // result = combination of all input matrices
1660+
// input:
1661+
GrB_Matrix *Matrices, // array of nmatrices input matrices
1662+
GrB_Index nmatrices, // number of matrices in the array (must be >= 1)
1663+
GrB_BinaryOp dup, // operator to combine (i,j) entries (must be != NULL)
1664+
char *msg
1665+
) ;
1666+
16161667
//------------------------------------------------------------------------------
16171668
// LAGraph_Vector_Structure: return the structure of a vector
16181669
//------------------------------------------------------------------------------

include/LAGraph.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,57 @@ int LAGraph_Matrix_Sum
16131613
char *msg
16141614
) ;
16151615

1616+
//------------------------------------------------------------------------------
1617+
// LAGraph_Matrix_Binary_Sum: sum an array of matrices by binary reduction
1618+
//------------------------------------------------------------------------------
1619+
1620+
/** LAGraph_Matrix_Binary_Sum: combines an array of matrices into a single
1621+
* matrix C, computing the same result as LAGraph_Matrix_Sum but with a
1622+
* different technique: a pairwise binary reduction tree built from
1623+
* GrB_eWiseAdd (as in the GraphChallenge "Anonymized Network Sensing" paper,
1624+
* "Binary Summation of Traffic Matrices"). At each level the matrices are
1625+
* summed in disjoint adjacent pairs using the binary operator dup; an unpaired
1626+
* (odd) trailing matrix is carried up to the next level unchanged, until a
1627+
* single matrix remains. The dup operator has the same set-union semantics as
1628+
* the dup of GrB_Matrix_build (applied where both inputs have an entry, lone
1629+
* entries pass through), so the result equals that of LAGraph_Matrix_Sum.
1630+
* With dup = GrB_PLUS_FP64 (for example) this computes the element-wise sum of
1631+
* all the matrices. The independent pair-sums within a level are parallelized
1632+
* across LG_nthreads_outer threads.
1633+
*
1634+
* All input matrices must have identical dimensions and identical built-in
1635+
* type; C is created with that same type and dimensions. Unlike
1636+
* LAGraph_Matrix_Sum, dup must be non-NULL, since GrB_eWiseAdd requires a
1637+
* binary operator.
1638+
*
1639+
* @param[out] C the resulting matrix.
1640+
* @param[in] Matrices array of nmatrices input matrices.
1641+
* @param[in] nmatrices number of matrices in Matrices (must be >= 1).
1642+
* @param[in] dup binary operator to combine (i,j) entries; must be
1643+
* non-NULL.
1644+
* @param[in,out] msg any error messages.
1645+
*
1646+
* @retval GrB_SUCCESS if successful.
1647+
* @retval GrB_NULL_POINTER if C, Matrices, any Matrices[k], or dup is NULL.
1648+
* @retval GrB_INVALID_VALUE if nmatrices is zero.
1649+
* @retval GrB_DIMENSION_MISMATCH if the inputs do not all share dimensions.
1650+
* @retval GrB_DOMAIN_MISMATCH if the inputs do not all share a type.
1651+
* @retval GrB_NOT_IMPLEMENTED if the matrix type is user-defined.
1652+
* @returns any GraphBLAS errors that may have been encountered.
1653+
*/
1654+
1655+
LAGRAPH_PUBLIC
1656+
int LAGraph_Matrix_Binary_Sum
1657+
(
1658+
// output:
1659+
GrB_Matrix *C, // result = combination of all input matrices
1660+
// input:
1661+
GrB_Matrix *Matrices, // array of nmatrices input matrices
1662+
GrB_Index nmatrices, // number of matrices in the array (must be >= 1)
1663+
GrB_BinaryOp dup, // operator to combine (i,j) entries (must be != NULL)
1664+
char *msg
1665+
) ;
1666+
16161667
//------------------------------------------------------------------------------
16171668
// LAGraph_Vector_Structure: return the structure of a vector
16181669
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)