High-performance mixed-precision linear solvers using CUDA
View the Project on GitHub OleBo/mixed-precision-iterative-refinement-gpu
We consider the numerical solution of a linear system:
\[A x = b, \quad A \in \mathbb{R}^{n \times n}, \; x,b \in \mathbb{R}^n\]where:
The goal is to compute an accurate solution efficiently using mixed precision arithmetic on GPUs.
We:
This yields:
Remark: Hardware Acceleration Modern GPU architectures (e.g., NVIDIA Ampere/Hopper) utilize Tensor Cores to significantly accelerate low-precision matrix math. While this provides a massive throughput boost for the “Correction Solve” phase, it requires careful monitoring of the matrix condition number to ensure convergence.
The code implements a mixed-precision iterative refinement scheme.
Solve: \(A^{(f)} x^{(0)} = b^{(f)}\)
where $A^{(f)}$, $b^{(f)}$ are FP32 versions of $A$, $b$.
This is done on the GPU via:
gpuSolve(...)
Mathematically: \(x^{(0)} \approx A^{-1} b\)
For $k = 0,1,2,\dots$:
Solve: \(A \, \delta x^{(k)} = r^{(k)}\)
BUT in practice:
Stop when: \(\|r^{(k)}\| \leq \varepsilon\)
or after maxIter iterations.
Let $x^*$ be the exact solution to $Ax = b$. We define the error at iteration $k$ as: \(e^{(k)} = x^* - x^{(k)}\)
The residual $r^{(k)}$, which represents the remaining error in the system, is computed as: \(r^{(k)} = b - Ax^{(k)} = A(x^* - x^{(k)}) = Ae^{(k)}\)
By solving the system for the correction term $\delta x^{(k)}$: \(A \delta x^{(k)} = r^{(k)} \implies \delta x^{(k)} = e^{(k)}\)
In exact arithmetic, the solution is updated as $x^{(k+1)} = x^{(k)} + \delta x^{(k)}$, which yields the true solution $x^*$ in a single step.
In mixed-precision arithmetic, errors introduced during computation prevent one-step convergence:
Because $\widehat{\delta x}^{(k)}$ is an approximation, the update $x^{(k+1)} = x^{(k)} + \widehat{\delta x}^{(k)}$ behaves as a contraction mapping: \(\|e^{(k+1)}\| \leq C \cdot \|e^{(k)}\|, \quad \text{where } C < 1\)
The refinement converges toward FP64 accuracy if the error introduced by the low-precision solve does not exceed the information provided by the residual: \(\kappa(A) \cdot u_{\text{low}} < 1\)
Where:
If $\kappa(A) \gtrsim 10^7$, the low-precision solver cannot distinguish the solution signal from rounding noise, causing the process to stagnate or diverge.
Implementing this foundation on a GPU provides two primary performance advantages:
The following pseudo-code describes the integration of FP32 and FP64 operations within the GPU solver loop.
Input: Matrix $A$, Right-hand side $b$, Tolerance $\epsilon$
Output: Refined solution $x$
# --- Setup Phase (Low Precision) ---
A_f32 = convert_to_fp32(A)
b_f32 = convert_to_fp32(b)
# Perform LU Factorization in FP32 (O(n^3) - GPU Bottleneck)
L_f32, U_f32 = factorize_fp32(A_f32)
# Initial Solve
x_f64 = solve_fp32(L_f32, U_f32, b_f32) # Result promoted to FP64
# --- Refinement Phase (High Precision) ---
for k in range(max_iterations):
# 1. Compute Residual in FP64 (Prevents cancellation)
# r = b - Ax
r_f64 = compute_residual_fp64(A, x_f64, b)
# Check convergence
if norm(r_f64) < epsilon:
break
# 2. Correction Solve in FP32 (Fast path)
# Solve: A * delta_x = r
r_f32 = convert_to_fp32(r_f64)
delta_x_f32 = solve_fp32(L_f32, U_f32, r_f32)
# 3. Update Solution in FP64
# x = x + delta_x
x_f64 = x_f64 + convert_to_fp64(delta_x_f32)
return x_f64
namespace mixed_precision
Encapsulates all solver functionality.
initializeCuda()shutdownCuda()gpuSolve(const float* d_A, const float* d_b, float* d_x, int n)d_A: matrix in device memory (FP32)d_b: RHS vector (FP32)d_x: solution (FP32)Mathematically: \(\text{solve } A x = b\)
Implementation likely uses:
refineSolution(const double* A, const double* b, double* x, int n, int maxIter)This is the main algorithmic component.
Responsibilities:
gpuSolve for correction| Stage | Precision | Location |
|---|---|---|
| Initial solve | FP32 | GPU |
| Residual computation | FP64 | CPU (or GPU FP64) |
| Correction solve | FP32 | GPU |
| Update | FP64 | CPU |
CUDA_CHECK(call)
Wraps CUDA calls and reports:
CUBLAS_CHECK(call)
Handles non-CUDA return types.
The entire solver is based on the identity:
\[x = x + A^{-1}(b - A x)\]which is implemented iteratively using:
This is a classic HPC technique used in:
Possible improvements:
Mixed precision works because:
The expensive part (matrix factorization) is done in low precision, while the accuracy-critical part (residual) is done in high precision.
This exploits modern GPU architectures where FP32 is extremely fast.
End of Documentation