Urgent.News

What's breaking now, across thousands of outlets.

Tech

A Pedagogical Introduction to Porting a Conjugate Gradient Solver to CUDA

GPU programming tutorials often begin with isolated examples: vector addition, reductions, matrix multiplication, and memory coalescing. These are useful for learning individual CUDA concepts, but there is another question that quickly arises when working with real scientific software: How do we actually port an existing CPU application to a GPU? In this article, I will walk through a small but…

The article walks through porting a Conjugate Gradient solver from CPU to CUDA, using HPCCG as a case study. The solver uses an unpreconditioned Conjugate Gradient method to solve a linear system arising from a 27-point stencil on a structured 3D grid. The CPU implementation is simple, with three kernels: SpMV, WAXPBY, and DDOT.

The profiling shows that SpMV dominates the CPU runtime, accounting for more than 80%. The first step is to profile the CPU baseline, which is already provided by HPCCG. The main obstacle in moving the porting is the matrix representation. HPCCG stores the matrix as arrays of per-row pointers, which is convenient on the CPU but doesn't work on the GPU.

The solution is to flatten the matrix into Compressed Sparse Row (CSR) form, which makes it easier to allocate and copy to the device independently. The initial CUDA kernel uses one CUDA thread per matrix row, which is a simple mapping. The article emphasizes the iterative process of profiling, forming a hypothesis, changing one part of the application, measuring again, and following the bottleneck.

The source code and intermediate CUDA-porting commits are available in the author's GitHub repository, allowing readers to check out and reproduce the individual stages while following along.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

iOS 27.2 Could Save Your Marriage

The iOS 27.2 beta that Apple released today adds a feature that reminds you it's your anniversary when you call someone, as long as you've added the date in the Contacts app.

More from Wednesday 16 September →