---
title: Hierarchical Clustering for SpGEMM
url: https://www.emergentmind.com/topics/hierarchical-clustering-for-spgemm
type: topic
---

# Hierarchical Clustering for SpGEMM

Searching arXiv for the cited papers and closely related work on hierarchical clustering and SpGEMM.
Hierarchical clustering for sparse matrix–sparse matrix multiplication (SpGEMM) is a locality-oriented technique that groups rows of the first input matrix \(A\) so that rows with overlapping nonzero column indices are processed together, thereby improving reuse of rows of the second input matrix \(B\) during multiplication. In the formulation developed for Gustavson-style SpGEMM, the method combines row reordering, cluster formation, and a row-clustered sparse format to convert structural overlap in \(A\) into temporal locality for \(B\), with the explicit goal of reducing data movement in a kernel that is typically memory-bound rather than arithmetic-bound [2507.21253].

## 1. Computational setting and locality objective

SpGEMM is a key kernel in many scientific applications and graph workloads, yet it is bottlenecked by data movement due to irregular memory access patterns. In Gustavson’s row-wise algorithm, processing row \(i\) of \(A\) requires streaming the rows of \(B\) indexed by all nonzero column positions \(p\) in that row, and then scattering products into an irregular sparse accumulator for row \(i\) of \(C\). Two sources of irregularity dominate: the set of rows of \(B\) touched by adjacent rows of \(A\) changes rapidly and often overlaps, forcing repeated loads of the same \(B\) rows, and the accumulator accesses are sparse and unpredictable [2507.21253].

For \(A \in \mathbb{R}^{m\times k}\), \(B \in \mathbb{R}^{k\times n}\), and \(C \in \mathbb{R}^{m\times n}\), let the set of nonzero column indices in row \(i\) of \(A\) be
\[
N(i)=\{p\in [1..k] : a_{ip}\neq 0\}.
\]
The scalar multiply-add count in Gustavson’s algorithm is
\[
W=\sum_{i=1}^{m}\sum_{p\in N(i)} \big|\mathrm{NZrow}_p(B)\big|,
\]
where \(\mathrm{NZrow}_p(B)\) is the set of nonzeros in row \(p\) of \(B\) [2507.21253].

The locality opportunity is cross-row reuse in \(A\). If rows \(i\) and \(j\) satisfy \(N(i)\cap N(j)\neq \varnothing\), then they both require the same rows \(B[p]\) for \(p\in N(i)\cap N(j)\). When the two rows are processed with sufficient temporal proximity, those \(B\) rows can remain hot in cache and avoid reloads from main memory. This is the key distinction from many SpMV-oriented reorderings, which mostly target bandwidth reduction or adjacency of nonzeros to a dense vector. For SpGEMM, simply reordering rows of \(A\) while retaining CSR row-major traversal does not ensure that \(B\)’s hot rows remain in cache across adjacent \(A\) rows [2507.21253].

To formalize this dependence structure, the method defines a row-intersection graph \(G=(V,E)\) over rows of \(A\), with \(V=\{1,\dots,m\}\), and for \(i\neq j\), an undirected edge \((i,j)\) weighted by
\[
w_{ij}=o_{ij}=|N(i)\cap N(j)|.
\]
A partition \(\Pi=\{S_1,\dots,S_t\}\) of the rows induces a cut
\[
\mathrm{cut}(\Pi)=\{(i,j)\in E : i\in S_a,\ j\in S_b,\ a\neq b\}.
\]
A locality-maximizing clustering then seeks to minimize
\[
\sum_{(i,j)\in \mathrm{cut}(\Pi)} w_{ij}
\]
subject to balance constraints \(|S_a|\le s_{\max}\) and optionally \(|S_a|\ge s_{\min}\). Equivalently, it maximizes the total intra-cluster overlap [2507.21253].

The same objective can be expressed directly in terms of data movement on \(B\). For a cluster \(S\), define the set of unique \(B\) rows touched by the cluster as
\[
U(S)=\bigcup_{i\in S} N(i).
\]
If \(s_p\) is the number of bytes needed to stream row \(p\) of \(B\), then the row-wise traffic model is
\[
T_{\mathrm{row}}(B)=\sum_{i=1}^{m}\sum_{p\in N(i)} s_p,
\]
whereas the cluster-wise model is
\[
T_{\mathrm{cl}}(B)=\sum_{S\in \Pi}\sum_{p\in U(S)} s_p.
\]
The reduction factor is approximately
\[
\rho_B=\frac{T_{\mathrm{row}}(B)}{T_{\mathrm{cl}}(B)}
=
\frac{\sum_i \sum_{p\in N(i)} s_p}
{\sum_S \sum_{p\in \cup_{i\in S} N(i)} s_p}.
\]
If \(|U(S)|\) fits in cache, the benefit approaches the ideal suggested by \(\rho_B\) [2507.21253].

## 2. Construction of hierarchical clusters

The hierarchical clustering method is a bottom-up merging scheme driven by overlap estimates obtained from one binary SpGEMM between \(A\) and \(A^T\). The procedure sets all values of \(A\) to \(1\), computes
\[
S=A\times A^T,
\]
and interprets each entry \(s_{ij}\) as the exact overlap count \(|N(i)\cap N(j)|\). For each row \(i\), it keeps the topK neighbors \(j\) by Jaccard similarity
\[
J(i,j)=\frac{|N(i)\cap N(j)|}{|N(i)\cup N(j)|}
\]
above a threshold \(\tau\), producing candidate pairs. The paper states that this replaces expensive LSH while yielding exact overlap counts [2507.21253].

Cluster growth proceeds by greedily merging disjoint-set representatives using a max-heap keyed by similarity, subject to a maximum cluster size \(K_{\max}\) and a minimum similarity threshold \(\tau\). The method is hierarchical in the sense of progressively coarsening row groups guided by overlap. In the reported experiments, practical defaults were \(\tau \approx 0.3\) and \(K_{\max} \approx 8\) [2507.21253].

A central design choice is the decoupling of reordering from the clustered matrix format. Any reordering can be applied to \(A\), or no reordering can be applied, and clusters can then be formed using one of three schemes: fixed-length clustering, variable-length clustering, or hierarchical clustering. Fixed-length clustering uses contiguous blocks of \(s\) rows. Variable-length clustering uses contiguous blocks determined by Jaccard similarity to a representative row, subject to \(K_{\max}\) and \(\tau\). Hierarchical clustering merges noncontiguous rows via bottom-up union-find using \( \mathrm{SpGEMM}(A,A^T)\)-derived candidates, followed by relayout [2507.21253].

This decoupling is conceptually important. The row-clustered format is independent of how clusters were chosen, and its benefits accrue whenever intra-cluster overlap is high, regardless of the reordering heuristic used. A plausible implication is that hierarchical clustering should be viewed less as a single monolithic preprocessing step than as one element in a modular locality pipeline [2507.21253].

## 3. Row-clustered sparse format and cluster-wise execution

To realize the locality implied by clustering, the method introduces a row-clustered CSR layout, denoted CSR\(_{\text{cluster}}\), that stores nonzeros grouped by column within a cluster, enabling column-wise traversal over merged rows. The format contains a cluster pointer array, and for each cluster \(S\), a column header array listing the union
\[
U(S)=\bigcup_{i\in S} N(i)
\]
in ascending column order. For each column \(p\in U(S)\), it stores a compact list of \((\text{row-local index}, \text{value})\) pairs for rows \(i\in S\) that satisfy \(a_{ip}\neq 0\). Variable-length CSR\(_{\text{cluster}}\) uses sparse per-column lists and an additional pointer array to values; a fixed-length variant can encode a dense row mask for the cluster [2507.21253].

Indices are remapped so that rows are local within a cluster, \(0..|S|-1\), allowing tight loops over cluster rows. Metadata per cluster includes the cluster size, a mapping from row-local indices to global row IDs or an implicit mapping via original IDs and cluster sizes, and offsets to each column’s sublists. The reported space overhead stems from padding when \(U(S)\) is large but many rows in \(S\) do not have that column; empirically it is \(<2\times\) in \(>80\%\) of cases, and is often smaller than standard CSR because indices are shared across rows within a column of a cluster [2507.21253].

The associated access pattern is column-major within each cluster. For every cluster \(S\), the algorithm traverses each column \(p\in U(S)\) once, loads row \(B[p]\) into cache, and then updates every row \(i\in S\) for which \(a_{ip}\) is present. In condensed form, the multiply phase is:

- parallelize over clusters \(S\);
- for each column \(p\in U(S)\), load row \(B[p]\);
- for each \((i_{\text{local}}, a_{\text{val}})\) in the column-sublist of \(p\), recover the global row \(i\);
- for each \((j,b_{\text{val}})\in \mathrm{NZrow}_p(B)\), update the accumulator entry for \(C[i,*]\).

This changes the reuse mechanism from accidental cache residency under row-wise traversal to explicit one-read-per-cluster reuse of \(B[p]\) when overlap is present [2507.21253].

The arithmetic work remains
\[
W=\sum_{i=1}^{m}\sum_{p\in N(i)} \big|\mathrm{NZrow}_p(B)\big|,
\]
but memory traffic for \(B\) approaches \(T_{\mathrm{cl}}(B)\) rather than \(T_{\mathrm{row}}(B)\). Preprocessing complexity depends on the clustering method. Fixed-length clustering requires \(O(\mathrm{nnz}(A))\) conversion. Variable-length clustering requires \(O(m+\sum_i \deg(i))\) for similarity scans of consecutive rows, plus \(O(\mathrm{nnz}(A))\) conversion. Hierarchical clustering requires one \(\mathrm{SpGEMM}(A,A^T)\) with unit values, topK filtering, union-find merges with complexity \(O(m\alpha(m)+M\log M)\) where \(M\approx m\cdot \text{topK}\), followed by \(O(\mathrm{nnz}(A))\) conversion [2507.21253].

## 4. Relationship to matrix reordering

The study evaluates ten reordering algorithms: Original, Random (Shuffled), Reverse Cuthill–McKee (RCM), Approximate Minimum Degree (AMD), Nested Dissection (ND), Graph Partitioning (GP via METIS, edge cut), Hypergraph Partitioning (HP via PaToH, cut-net/quality), Gray code ordering, Rabbit, Degree-based, and SlashBurn. Their objectives differ. CM/RCM target bandwidth reduction to improve locality. AMD/ND target fill-in reduction. GP/HP minimize cut and thereby implicitly maximize intra-part overlap of \(N(i)\), which aligns well with the cut objective on the row-intersection graph. Gray, Rabbit, Degree, and SlashBurn aim to group structurally similar rows or communities and to reduce random accesses [2507.21253].

The empirical finding is that reordering based on graph partitioning provides better SpGEMM performance than existing alternatives at the cost of high preprocessing time. The paper also states that GP/HP perform best for SpGEMM because their optimization more directly minimizes cross-part edges weighted by overlap, which is strongly correlated with reducing duplicate reads of \(B\) across parts. However, many GP/HP instances require \(>100\times\) the cost of one SpGEMM to amortize, whereas hierarchical clustering achieves competitive speedups at \(<20\times\) cost in \(\sim 90\%\) of inputs [2507.21253].

This creates a cost–benefit distinction between reordering and clustering. When preprocessing can be amortized over many multiplies, GP or HP may be justified. When preprocessing budget is tighter, hierarchical clustering offers a lower-cost route to exploiting overlap. The paper’s explicit recommendation is to treat reordering and clustering independently. If a domain already provides a good ordering, fixed-length clustering may suffice. If ordering is unknown or expensive, hierarchical clustering yields high-quality clusters, and an implied reordering, in one pass. Combining reordering and clustering can be synergistic but is matrix-dependent [2507.21253].

A common misconception is that row reordering alone is sufficient for SpGEMM locality. The study directly argues against this: simply reordering rows of \(A\) and keeping CSR row-major traversal does not ensure that frequently used rows of \(B\) remain in cache across adjacent rows of \(A\). Cluster-wise computation is therefore not merely an implementation detail of reordering, but the mechanism that converts structural overlap into realized reuse [2507.21253].

## 5. Empirical characterization

The evaluation uses 110 square matrices from SuiteSparse, including 26 from prior SpGEMM studies and 32 from recent graph-oriented suites. Selection ensures \(\mathrm{NNZ}>8\)M, \(\mathrm{NNZ}<10\)B, and reduced redundancy among grouped publishers, while full SNAP and DIMACS10 are kept. Results are averaged over 10 runs on Perlmutter CPU nodes with AMD EPYC 7763, 64 cores, DDR4, \(L2=64\) MiB/core, OpenMP with 64 threads, Intel icpc 2024.1 \(-O3\), and a hash-table accumulator. The workloads are \(A^2\) and \(A\times\) tall-skinny frontier matrices from CombBLAS BC [2507.21253].

For hierarchical clustering, the reported average speedup is \(1.39\times\), with improvements on \(\sim 70\%\) of matrices. The distribution shows speedups up to \(4.68\times\), with most between \(0.96\times\) and \(1.75\times\). The preprocessing cost is \(<20\times\) the cost of a single SpGEMM on \(\sim 90\%\) of inputs [2507.21253].

For reordering alone, HP achieves geomean \(1.77\times\) with positive speedups on \(\sim 79.6\%\) of inputs, and GP and RCM are also strong. Best-per-matrix reordering achieves up to \(2.90\times\) geomean across datasets, but with high overhead [2507.21253].

For clustering without reordering, fixed-length and variable-length clustering speed up \(\sim 45\%\) and \(\sim 40\%\) of cases, respectively. With HP or GP preprocessing, they improve \(\sim 70\%-80\%\) of inputs with \(\sim 1.5\times\) geomean. On tall-skinny SpGEMM, reorderings that help \(A^2\) also help \(A\times B_{\mathrm{ts}}\), and hierarchical clustering is often beneficial across frontier iterations, which the paper interprets as confirming reuse benefits independent of the specific \(B\) operand [2507.21253].

The paper also supplies an illustrative example. If
\[
N(0)=\{0,5\},\quad N(1)=\{0\},\quad N(2)=\{0,5\},\quad N(3)=\{5\},\quad N(4)=\{5\},\quad N(5)=\{0\},
\]
then \(w_{01}=1\), \(w_{02}=2\), \(w_{12}=1\), and so forth. For cluster \(S=\{0,1,2\}\), the touched set is \(U(S)=\{0,5\}\). In row-wise SpGEMM, \(B[0]\) is read three times across rows \(0,1,2\), and \(B[5]\) twice. In cluster-wise traversal, \(B[0]\) and \(B[5]\) are each read once per cluster, reducing \(T_{\mathrm{row}}(B)\) to \(T_{\mathrm{cl}}(B)\) by \(\sim 2\)–\(3\times\) for those rows [2507.21253].

## 6. Limitations, applicability, and related uses of hierarchy

The method is explicitly most beneficial when rows of \(A\) share substantial overlap in \(N(i)\), but similar rows are not adjacent. Low-overlap matrices, including diagonally dominant or random sparsity patterns, yield little reuse; clustering then adds overhead and padding. Fixed-length clustering can inflate memory because of padding columns with sparse participation. Excessively large clusters can expand \(U(S)\) beyond cache, negating locality. The paper therefore recommends choosing \(K_{\max}\) and \(\tau\) so that the expected \(|U(S)|\) fits in \(L2/L3\), and stopping merging when adding a row increases \(|U(S)|\) beyond cache-, NUMA-, or bandwidth-optimal bounds, or when \(J(i,i')<\tau\) [2507.21253].

There are also interactions with parallelism. Union-find merging can produce uneven clusters, and load imbalance may hurt parallel efficiency. Variable or heterogeneous cluster sizes require dynamic scheduling. Per-row accumulators should be private or lock-free. Symbolic computation can be fused with numeric by first-touch marker arrays per row. The format primarily improves \(B\)-row reuse; hash-based kernels benefit most, though heap- and SPA-based kernels can also benefit from reduced \(B\)-row read duplication [2507.21253].

GPU-specific constraints are not addressed in the row-clustering work; the paper states that extending CSR\(_{\text{cluster}}\) to GPUs requires careful tiling. This is distinct from the sense of hierarchy in "Communication-Avoiding SpGEMM via Trident Partitioning on Hierarchical GPU Interconnects," where “hierarchy-aware partitioning/clustering” refers to grouping computation and communication to match the hardware hierarchy of local and global interconnects, rather than clustering rows by overlap. That work introduces Trident, a hierarchy-aware 2D distributed SpGEMM algorithm for modern heterogeneous supercomputers, and explicitly clarifies that this usage of clustering is not graph or data clustering [2603.21444].

A broader antecedent appears in "Rapid Near-Neighbor Interaction of High-dimensional Data via Hierarchical Clustering," which introduced a method for obtaining a matrix permutation that renders a desirable sparsity profile, guided by the principle of a block-sparse matrix with dense blocks, using lower-dimensional embedding, hierarchical data clustering, multi-level matrix compression storage, and multi-level interaction computations [1709.03671]. This suggests a wider lineage of locality-oriented hierarchical organization for sparse computations. However, the row-overlap formulation for SpGEMM is more specific: it derives clusters directly from shared \(B\)-row access requirements and couples them to a cluster-wise access pattern and a row-clustered CSR layout [2507.21253].

Source: https://www.emergentmind.com/topics/hierarchical-clustering-for-spgemm