Project notes · · 7 min read
Making a sparse solver faster
We tested different ways to solve sparse triangular systems on a GPU, looking at how sharing work and reducing waiting affected performance.
The project, in five reports
GitHubThe implementation, measurements and details behind each step.
What were we trying to solve?
For our GPU computing project, we studied sparse triangular systems. The task is to find the unknown values in a set of equations. A sequential solver works through the rows in order, using the answers it has already found to solve the next row. We wanted to see how much of this work could happen in parallel, and what would still have to wait.
Here, “sparse” means that many entries in the matrix are zero. A row only depends on the earlier answers with nonzero coefficients. If two rows do not need each other’s answers, they may be solved together. This gave us a way to look for parallel work without changing the calculation.
We started with a CPU version to check our answers and measure speedup. We tested three matrices, each with 128, 256 and 512 right-hand sides. These are different sets of inputs for the same matrix. They follow the same dependencies, but each produces its own answers.
Which rows can we solve together?
In Kernel 0, we gave each input its own GPU block. The threads in a block shared the work of solving one value and using it to update later rows. This was faster than the CPU in our tests, but the pivots were still processed in order. We then asked whether independent rows could also run at the same time.
For Kernel 1, we grouped rows into levels. Rows in the same level do not depend on one another. They can run together once the earlier levels have finished. We assigned a block to each row and let its threads work on the different inputs. This approach follows level scheduling for sparse triangular solves. [1]
The example below uses the same small system for Kernels 0, 1 and 2. We can follow the values through each calculation, then switch to the launch view to see when the CPU waits. Selecting a different input or row shows its own calculation. The long-chain example shows what happens when every row needs the answer before it.
Inside one GPU block
B Inputs
Each column is a separate input.
L Sparse matrix
6 × 6Only nonzero entries enter the calculation.
X Answers
Select any answer to follow its calculation.
Who is waiting?
Execution order, not elapsed timeFollow the kernel Simplified view of the selected implementation
Could we reduce the time spent waiting?
Level scheduling worked well on the small and medium matrices. On the large matrix, we had hundreds of thousands of levels, often with very little work in each one. Kernel 1 launched a level and made the CPU wait for it to finish before launching the next. Repeating this for so many small levels added considerable overhead.
In Kernel 2, we kept the same row calculation and changed how the work was submitted. We queued the levels in one ordered stream and waited once at the end. The GPU still respected the dependencies between levels. We also saved the level analysis so that another solve with the same matrix could reuse it.
This reduced waiting, but it did not remove the large number of launches. We also tried a persistent version, where one kernel stayed on the GPU for the whole solve. The threads then had to coordinate inside that kernel. In our tests, this coordination added overhead, so we did not use the persistent version in the final solver.
What worked in the final version?
We used a different approach for each matrix in Kernel 3. For the small matrix, we cleaned up the data layout and simplified the inner calculation. For the medium matrix, we kept Kernel 2. For the large matrix, we grouped dependent work into larger chunks to reduce the overhead of many small steps. [2, 3]
On the large matrix with 256 right-hand sides, the final report shows a reduction from about 2.46 seconds in the previous version to 0.48 seconds in the hybrid version. Across the nine matrix and input-size combinations, the geometric mean speedup was about 136× over our sequential CPU baseline.
For us, the useful result was seeing how the matrix structure affected which approach worked best. Finding independent rows helped, but the amount of work in each level also mattered. We had to consider the calculation, the launches and the waits together. The five reports above follow these changes and include the measurements behind them.
We finished with the highest overall speedup on the final project leaderboard, with a geometric mean of 132.02× across the nine test cases.
References
- Maxim Naumov. Parallel Solution of Sparse Triangular Linear Systems in the Preconditioned Iterative Methods on the GPU. NVIDIA Technical Report NVR-2011-001, 2011.
- Weifeng Liu, Ang Li, Jonathan D. Hogg, Iain S. Duff, and Brian Vinter. Fast Synchronization-Free Algorithms for Parallel Sparse Triangular Solves with Multiple Right-Hand Sides. Concurrency and Computation: Practice and Experience, 29(21), e4244, 2017.
- Jiya Su, Feng Zhang, Weifeng Liu, Bingsheng He, Ruofan Wu, Xiaoyong Du, and Rujia Wang. CapelliniSpTRSV: A Thread-Level Synchronization-Free Sparse Triangular Solve on GPUs. ICPP, 2020.
More visual explanations: GPU Puzzles and CUDA Tiled GEMM Visualizer.