---
title: Adaptive Row-grouped CSR (AR-CSR)
url: https://www.emergentmind.com/topics/adaptive-row-grouped-csr-ar-csr
type: topic
---

# Adaptive Row-grouped CSR (AR-CSR)

Adaptive Row-grouped CSR (AR-CSR) is an adaptive sparse matrix storage format designed for efficient sparse matrix-vector multiplication (SpMV) on GPUs. It addresses the primary performance bottlenecks of standard CSR and CUSPARSE formats—namely, uncoalesced memory access due to high variance in row length and load imbalance across threads—by introducing hierarchical row grouping and chunking strategies. AR-CSR achieves significantly higher SpMV throughput for a broad class of irregular sparse matrices after a one-time format conversion from standard CSR [1203.5737].

## 1. Motivation and Comparative Foundations

Sparse matrix-vector multiplication (SpMV) is a key computational kernel in scientific computing. Standard CSR (Compressed Sparse Row) storage—comprising values, column indices, and row pointer arrays—assigns one GPU thread per matrix row. This approach is efficient for matrices with near-constant row lengths but suffers from severe load imbalance and scattered memory accesses in the presence of highly variable row lengths. CUSPARSE, the widely used CUDA library, adopts a similar thread-per-row approach but incorporates tuned kernels for latency hiding and improved but non-ideal memory coalescing. Neither CSR nor CUSPARSE can fundamentally eliminate thread divergence and the resultant performance degradation in real-world, irregular matrices.

AR-CSR introduces multi-row grouping and intra-group parallel chunking. Consecutive rows are aggregated into groups, each processed by a CUDA block. Individual threads within a block process fixed-size "chunks," allowing long rows to be split among several threads and addressing both workload disparity and memory access inefficiency [1203.5737].

## 2. Data Structures and Memory Layout

The AR-CSR format structures a matrix $A \in \mathbb{R}^{m \times m}$ with $\mathtt{nnz}$ non-zeros into $G$ groups, with each group assigned to a CUDA block of $B$ threads. The primary data arrays and mapping structures are:

- $\texttt{grp}[g] = (\texttt{firstRow}_g,~\texttt{size}_g,~\texttt{offset}_g,~\texttt{chunkSize}_g)$ for $g \in [0,G-1]$, encoding each group’s starting row, size, global offset in the data arrays, and chunk size.
- $\texttt{globalThreadsMapping}[n_\mathrm{row}]$, a prefix sum array indicating, for each row, the cumulative count of threads assigned to all preceding rows.
- $\texttt{values}[N']$ and $\texttt{columns}[N']$, real-valued and integer arrays of length $N' = \sum_{g=0}^{G-1} (B \cdot \texttt{chunkSize}_g)$, storing the grouped matrix entries and their column indices; padding is denoted by the sentinel value $-1$ for columns.

Within each group $g$, the memory layout is organized such that for chunk (thread) $c = 0 \ldots B-1$ and position $k = 0 \ldots \texttt{chunkSize}_g - 1$:
$$
\texttt{values}[\texttt{offset}_g + c + k \cdot B] = A_{r,c,k}
$$
where $r = \texttt{firstRow}_g + \texttt{rowOfChunk}(c)$ and $\texttt{rowOfChunk}(c)$ is defined by the thread-to-row mapping.

This organization enables fully coalesced memory accesses for both values and column indices within each block and distributes work among threads proportionally to the nnz-per-row profile [1203.5737].

## 3. Conversion Algorithm from CSR to AR-CSR

AR-CSR construction from existing CSR-formatted data proceeds as follows:

1. **Partitioning Rows into Groups:** Sequentially process rows, accumulating the local nnz and row count until either exceeds $D \cdot B$ (desired chunk size times block size) or the maximum block size is met; this marks a group boundary.
2. **Thread Allocation within Groups:** Initially assign one thread per row; then, iteratively distribute remaining threads (until all $B$ are used) to rows with the largest reduction in per-thread nnz after additional assignment, aiming to minimize intra-group chunk size variation.
3. **Prefix-Sum Calculation:** Compute exclusive prefix-sums of thread allocations within each group for efficient chunk-to-row mappings.
4. **Populating Data Arrays:** For each group, thread, and chunk position, copy matrix elements from CSR (or pad with artificial zeros if needed) into the $\texttt{values}$ and $\texttt{columns}$ arrays.

The time complexity is $\mathcal{O}(\mathtt{nnz} + m + G\log B)$, and the space overhead is $\mathcal{O}(\mathtt{nnz} + m + G)$. Conversion typically incurs only a few milliseconds for large matrices and is amortized over many SpMV calls in iterative solvers [1203.5737].

## 4. SpMV Kernel Architecture and Execution Model

The SpMV operation in AR-CSR launches $G$ CUDA blocks of $B$ threads each. Group metadata and thread-to-row mappings are accessed from shared memory. Each thread computes the partial sum of its chunk, leveraging strided, coalesced accesses for optimal bandwidth utilization. Inter-thread reductions within each group aggregate per-thread partial sums for rows spanned by multiple threads.

Key characteristics of the AR-CSR kernel include:
- Perfect load balancing: Each CUDA block handles exactly $B$ chunks; work is evenly distributed by adapting the chunk size and per-row thread count.
- Coalesced memory reads: By construction, threads within a warp access contiguous memory regions.
- Shared memory efficiency: Thread mappings and partial sums occupy shared memory $O(B)$.
- Tunable parameters: Block size $B$ (best at $128$ on Tesla C2070) and desired chunk size $D$; these allow optimization to matrix structure and hardware [1203.5737].

## 5. Performance Evaluation and Empirical Results

On a dataset of $1{,}600$ matrices, AR-CSR was benchmarked against standard CPU-CSR and CUDA CUSPARSE implementations on a Tesla C2070 (144 GB/s, double precision), with the CPU baseline being CSR on an AMD Phenom II X6. For robust performance, $B=128$ and $D=1$ were used unless otherwise tuned.

- **Peak observed performance:** $18$ GFLOPS on "Schenk_AFE" (structural problem) at $D=32$; $11$ GFLOPS for $D=1$.
- **Median observed performance:** $\sim 4$ GFLOPS with $D=1$.
- **Relative speed-ups:**
  - Faster than CPU-CSR on $1{,}168/1{,}600$ matrices
  - Faster than CUSPARSE on $1{,}358/1{,}600$ matrices, with peak speed-ups up to $10\times$ (e.g., "rajat23").
- **Best-case matrices:** High variance in row length (common in circuit simulation, e.g., "raj," "rajat," "IBM_EDA") and mixed patterns (very long rows among short ones).
- **Worst-case/scenarios favoring alternatives:** Near-constant row lengths ("mesh" matrices), smaller problems ($<10$k rows), or regular sparsity patterns.

Summary of results:

| Format         | # Matrices Faster | Median Speed-up | Peak GFLOPS |
|----------------|------------------|-----------------|-------------|
| CPU-CSR        | —                | $1\times$       | $0.15$      |
| CUSPARSE       | $994/1600$       | $1.6\times$     | $12.5$      |
| AR-CSR ($D=1$) | $1168/1600$      | $2.5\times$     | $18.0$      |

[Table as in 1203.5737]

## 6. Usage Guidelines, Limitations, and Practical Considerations

- **Conversion and Storage:** AR-CSR requires a one-time conversion from CSR, with computational cost $\mathcal{O}(\mathtt{nnz} + m)$. The storage overhead, including mapping structures and group metadata plus padding, is typically below $10\%$.
- **Suitability:** Best applied when SpMV is invoked repeatedly, such as in iterative Krylov or multigrid methods. For highly variable row lengths, set $D\approx 1\ldots4$; for more regular matrices, larger $D$ (up to average nnz/row) may be optimal.
- **Limitations:** Not efficient for matrices with very regular sparsity or for very small matrices, where conversion overheads outweigh runtime benefits. The format must be rebuilt if matrix $A$ is significantly altered.
- **Parameter Tuning:** Empirical selection of $B$ and $D$ is recommended, as performance depends on the matrix profile and hardware characteristics.

AR-CSR demonstrates that the trade-off of minor conversion and storage overhead for substantial runtime acceleration is highly advantageous in many real-world applications involving heterogeneous sparse matrices, routinely surpassing both classic CSR and tuned vendor-provided libraries like CUSPARSE for SpMV on GPUs [1203.5737].

Source: https://www.emergentmind.com/topics/adaptive-row-grouped-csr-ar-csr