12. Data-Level Parallelism: Vector, SIMD, GPUs and CUDA
Description
DLP applies the same operation to many data elements. Three hardware families implement it:
| Family | Idea | Examples |
|---|---|---|
| Vector architectures | Long vector registers, a vector length register (VL), pipelined functional units, strided/gather-scatter memory ops | Cray-style, RISC-V "V" extension, ARM SVE |
| SIMD extensions | Fixed-width registers (128/256/512 bits) split into lanes | x86 SSE/AVX/AVX-512, ARM NEON |
| GPUs | Thousands of threads, hardware-grouped into SIMD-like warps (SIMT) | NVIDIA GeForce/Tesla, AMD Radeon |
Vector concepts:
- Vector length (VL): how many elements one instruction processes.
- Strip mining: if the loop count N exceeds the maximum vector length (MVL), process the loop in strips of ≤ MVL plus a remainder.
- Chaining: forward each element result to the next vector instruction without waiting for the whole vector (vector forwarding).
- Masked (predicated) instructions: a mask register enables or disables individual elements so
ifstatements vectorize without branches. - Gather/scatter: indexed loads/stores for irregular access.
SIMD extension issues: data alignment, tail handling, no standard vector length (code must be rewritten for each new width), and the data must be laid out contiguously (prefer structure of arrays). Overlapping computation with memory loads helps hide latency.
GPUs and CUDA:
- CUDA is NVIDIA's platform for programming GPUs. You write a kernel run by many threads.
- Hierarchy: thread → warp (32 threads) → thread block → grid. Blocks run on streaming multiprocessors (SMs).
- SIMT: all threads of a warp execute one instruction in lock-step. Divergence (threads of a warp taking different branch paths) serializes the paths with masks and wastes lanes.
- Memory: registers, fast on-chip shared memory (per block), caches, global memory (GDDR/HBM). Coalescing: consecutive threads accessing consecutive addresses merge into few memory transactions.
- Applications: graphics, scientific simulation, deep learning (frameworks such as TensorFlow run on CUDA).
Roofline model: performance is bounded either by peak compute or by memory bandwidth, depending on arithmetic intensity (FLOPs per byte moved).
Key Formulas
Lanes per register = register width / element width (256-bit AVX2, 32-bit float → 8)
Peak FLOPS = cores × (lanes × FLOP per lane per cycle) × frequency
(FMA counts as 2 FLOP)
Strips needed = ceil(N / MVL)
Vector time ≈ (convoys) × VL + startup (one chime per convoy)
Roofline: Attainable GFLOP/s = min( Peak GFLOP/s , Peak bandwidth × Arithmetic intensity )
Ridge point = Peak GFLOP/s / Peak bandwidth
CUDA blocks = ceil(N / threads per block); warps per block = threads per block / 32
Steps
Vectorize a loop: (1) confirm iterations are independent (no loop-carried dependence), (2) replace scalar ops by vector ops, (3) strip-mine to the hardware vector length, (4) handle if with masks, (5) align or pad data.
Write a CUDA kernel: (1) compute global index i = blockIdx.x * blockDim.x + threadIdx.x, (2) guard if (i < n), (3) copy data to the device, (4) launch <<<blocks, threads>>>, (5) copy results back.
Worked Example
A. SIMD width. AVX2 (256-bit) holds 8 floats or 4 doubles; AVX-512 holds 16 floats. Adding two arrays of 1,000,000 floats takes about 1,000,000 / 8 = 125,000 AVX2 additions (ideal), versus 1,000,000 scalar additions.
B. Strip mining. N = 200, MVL = 64: strips of 64, 64, 64 and 8 elements (4 vector iterations).
C. DAXPY chimes (y = a·x + y, one load/store unit): LV V1,Rx ; MULVS.D V2,V1,F0 ; LV V3,Ry ; ADDV.D V4,V2,V3 ; SV Ry,V4. With chaining the schedule needs 3 convoys (load+multiply, load+add, store), so 64 elements take roughly 3 × 64 = 192 cycles plus startup, about 3 cycles per element.
D. Masked operation. for i: if (A[i] != 0) A[i] = A[i] - B[i]; → compute mask = (A != 0), then subtract only where the mask is true, no branch.
E. Peak FLOPS. 8 cores, 3 GHz, 2 AVX2 FMA units each (2 × 4 doubles × 2 FLOP = 16 FLOP/cycle): 8 × 3 × 16 = 384 GFLOP/s.
F. Roofline. Peak = 10 TFLOP/s, bandwidth = 500 GB/s → ridge point = 20 FLOP/byte.
- AI = 4 FLOP/byte → attainable = 500 × 4 = 2 TFLOP/s (memory-bound).
- AI = 40 FLOP/byte → attainable = 10 TFLOP/s (compute-bound).
G. CUDA launch. N = 1,000,000, 256 threads per block → ceil(1,000,000 / 256) = 3907 blocks; each block has 8 warps; the last block has 64 idle threads, which is why the if (i < n) guard is needed.
H. Divergence. In a warp where half the threads take the if path and half the else path, the hardware executes both paths one after the other, so throughput for that code halves.
Python / Assembly
import numpy as np
x = np.arange(8, dtype=np.float32)
y = np.ones(8, dtype=np.float32)
# DAXPY-style vector update (NumPy executes this with SIMD kernels)
z = 2.0 * x + y
# Masked operation: A[i] = A[i] - B[i] only where A[i] != 0
A = np.array([3, 0, 5, 0, 7, 8, 0, 1], dtype=np.float32)
B = np.array([1, 1, 1, 1, 1, 1, 1, 1], dtype=np.float32)
mask = A != 0
A = np.where(mask, A - B, A)
print(A) # [2. 0. 4. 0. 6. 7. 0. 0.]
def roofline(peak_gflops, bw_gbs, ai):
return min(peak_gflops, bw_gbs * ai)
print(roofline(10_000, 500, 4), roofline(10_000, 500, 40)) # 2000 10000
RISC-V Vector (RVV 1.0) DAXPY, strip-mined by vsetvli:
# a0 = n, a1 = &x, a2 = &y, fa0 = a y[i] = a * x[i] + y[i]
daxpy:
loop:
vsetvli t0, a0, e64, m1, ta, ma # t0 = elements handled this strip (<= VLMAX)
vle64.v v0, (a1) # load x strip
vle64.v v1, (a2) # load y strip
vfmacc.vf v1, fa0, v0 # v1 += fa0 * v0
vse64.v v1, (a2) # store y strip
slli t1, t0, 3 # bytes processed = elements * 8
add a1, a1, t1
add a2, a2, t1
sub a0, a0, t0 # remaining elements
bnez a0, loop
ret
Masked version of if (A[i] != 0) A[i] -= B[i] (mask lives in v0):
fmv.d.x ft0, zero # ft0 = 0.0
vle64.v v1, (a1) # A
vle64.v v2, (a2) # B
vmfne.vf v0, v1, ft0 # mask = (A != 0)
vfsub.vv v1, v1, v2, v0.t # A = A - B only where mask set
vse64.v v1, (a1)
x86 AVX2 intrinsics (8 floats per instruction):
#include <immintrin.h>
void add8(const float *a, const float *b, float *c, int n) {
int i = 0;
for (; i + 8 <= n; i += 8) {
__m256 va = _mm256_loadu_ps(a + i);
__m256 vb = _mm256_loadu_ps(b + i);
_mm256_storeu_ps(c + i, _mm256_add_ps(va, vb));
}
for (; i < n; i++) c[i] = a[i] + b[i]; // scalar tail
}
CUDA kernel and launch:
__global__ void vadd(const float *a, const float *b, float *c, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) c[i] = a[i] + b[i];
}
// host: vadd<<<(n + 255) / 256, 256>>>(d_a, d_b, d_c, n);
Notes
- SIMD vs vector: SIMD exposes a fixed register width, vector architectures expose a flexible vector length, so one binary adapts to different hardware.
- A GPU is a throughput machine: it hides memory latency by switching among many warps instead of using big caches.
- Masked instructions trade a branch for wasted lanes; they win when branches would be unpredictable.
- Poor alignment or non-contiguous data can erase SIMD gains.
- Quick facts: DLP = same operation on multiple data; SIMD processors and GPUs support DLP; masked vector instructions apply an operation only to chosen elements; CUDA is NVIDIA's GPU programming platform; GPUs run thousands of threads at once.
References: [HP6] Ch. 4 (4.1-4.4 vector, SIMD, GPUs, roofline); [PH-RV] Ch. 6 (6.6-6.7); [RV-Spec] "V" Vector Extension 1.0; Williams, Waterman & Patterson, "Roofline: An Insightful Visual Performance Model for Multicore Architectures," CACM, 2009; NVIDIA, CUDA C++ Programming Guide;
All rights remain with the original author (Algo-holics). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me