Mixed-Precision Iterative Refinement on GPUs

High-performance mixed-precision linear solvers using CUDA

View the Project on GitHub OleBo/mixed-precision-iterative-refinement-gpu

CUDA C++ Implementation — Mixed Precision Solver

1. Overview

This document explains the CUDA C++ implementation of the mixed-precision linear solver with a focus on:

The implementation consists of:


2. High-Level Execution Flow

GPU Solve (FP32)

  1. Copy matrix $A$ and vector $b$ to device memory
  2. Factorize $A$ (typically LU)
  3. Solve $Ax = b$
  4. Copy solution back

Refinement Loop

  1. Compute residual in FP64 (CPU or GPU)
  2. Cast residual to FP32
  3. Solve correction system on GPU
  4. Update solution in FP64

3. Memory Management in CUDA

3.1 Device Memory Allocation

Typical pattern:

float* d_A;
cudaMalloc((void**)&d_A, n * n * sizeof(float));

Key points:


3.2 Host–Device Transfers

cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);

Performance implications:


3.3 RAII and Templates (Best Practice)

Although not fully shown in the header, a modern CUDA design often uses templates:

template<typename T>
struct DeviceArray {
    T* data;
    size_t size;

    DeviceArray(size_t n) : size(n) {
        cudaMalloc(&data, n * sizeof(T));
    }

    ~DeviceArray() {
        cudaFree(data);
    }
};

Advantages:


4. Use of Templates in the Solver

4.1 Motivation

Templates allow writing precision-independent code:

Example:

template<typename T>
void copyToDevice(T* d, const T* h, int n);

4.2 Mixed Precision Pattern

Your implementation uses:

Templates could generalize this to:

template<typename Low, typename High>
void refine(...);

4.3 Benefits


5. Parallelization Strategy

5.1 CUDA Execution Model

Each thread executes the same kernel on different data.


5.2 Data Parallelism in Linear Algebra

For matrix operations:

__global__ void matvec(float* A, float* x, float* y, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        float sum = 0;
        for (int j = 0; j < n; ++j)
            sum += A[i*n + j] * x[j];
        y[i] = sum;
    }
}

5.3 cuBLAS / cuSOLVER Usage

Instead of writing kernels manually, the code uses:

Advantages:


6. Memory Hierarchy and Tiling

6.1 GPU Memory Types

Memory Type Speed Scope
Registers Fastest Per-thread
Shared Memory Very fast Per-block
Global Memory Slow All threads

6.2 Memory Tiling Concept

Tiling improves performance by:


6.3 Example: Tiled Matrix Multiply

__shared__ float tileA[32][32];
__shared__ float tileB[32][32];

Steps:

  1. Load tiles from global memory
  2. Synchronize threads
  3. Compute partial products
  4. Repeat for next tile

6.4 In Your Solver

Even if not explicitly written:


7. Numerical Kernels

7.1 LU Factorization (GPU)

The solver likely performs:

\[A = LU\]

Then solves:

\[Ly = b, \quad Ux = y\]

7.2 Stability Considerations


8. Mixed Precision Refinement (Implementation View)

Key steps in code:

  1. Compute residual (double precision)
  2. Convert residual to float
  3. Call gpuSolve
  4. Update solution (double precision)

9. Performance Considerations

9.1 Bottlenecks


9.2 Optimization Strategies


10. Error Handling

Macros:

CUDA_CHECK(...)
CUBLAS_CHECK(...)

Provide:


11. Key GPU Insight

Performance comes from parallelism + memory locality, not just FLOPs.

Your solver leverages:


12. Summary

This CUDA implementation demonstrates:


Status: CUDA Implementation Documentation — COMPLETE