Parallel and GPU Architecture (COA Core #8)
A CPU spends most of its silicon on making one thread fast: wide out-of-order execution, big caches, branch prediction. A GPU spends most of it on throughput: thousands of simple lanes, many threads in flight, and memory bandwidth. The programming model follows from that difference.
Multicore CPUs
A modern CPU package has several cores, each with private L1/L2 caches and a shared L3. SMT (simultaneous multithreading, Intel’s Hyper-Threading) exposes two logical threads per physical core, sharing the execution units. It helps when one thread stalls on memory, and hurts when both threads compete for the same units.
Limits on multicore scaling:
- shared memory bandwidth and LLC,
- coherence traffic,
- Amdahl’s law (serial sections),
- power and thermal budgets.
A 16-core chip does not run a single-threaded program 16× faster; it runs 16 single-threaded programs at roughly the same speed each.
ASolution
No. It fills otherwise idle issue slots, typically adding 10–30% throughput on mixed workloads and much less on ALU-saturated code.
SIMD versus SIMT
SIMD (single instruction, multiple data) is a CPU instruction that operates on a
vector register: one vaddps adds eight floats. The programmer or compiler must
express the data as vectors.
SIMT (single instruction, multiple threads) is the GPU model: each thread has its own registers and appears scalar, but the hardware groups threads into warps (32 on NVIDIA) and issues one instruction for the whole warp.
| SIMD | SIMT | |
|---|---|---|
| Visible unit | vector register | per-thread scalar |
| Width | 4–16 elements | 32 threads per warp |
| Divergence | rare; explicit masks | common; serialized paths |
| Programming | intrinsics or autovectorization | ordinary scalar code |
SIMT is SIMD with the bookkeeping hidden. The cost is divergence: if threads in a warp take different branches, both paths execute with the inactive lanes masked.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
c/.style={draw, thick, minimum width=0.45cm, minimum height=0.4cm, font=\tiny}]
\foreach \i in {0,...,15} \node[c, fill=blue!15] at (\i*0.5,0.8) {};
\node[font=\scriptsize, anchor=east] at (-0.3,0.8) {SIMD};
\node[font=\tiny, anchor=west] at (8.2,0.8) {one instruction, 16 lanes};
\foreach \i in {0,...,31} \node[c, fill=orange!20] at (\i*0.25,0) {};
\node[font=\scriptsize, anchor=east] at (-0.3,0) {warp};
\node[font=\tiny, anchor=west] at (8.2,0) {32 threads, one instruction};
\node[font=\scriptsize, anchor=north] at (4.0,-0.5) {divergence: lanes with different branches wait for each other};
\end{tikzpicture}
ASolution
Each warp stalls on memory often. The SM switches to another ready warp instead of waiting, which is how GPUs hide latency without large caches.
GPU versus CPU architecture
| CPU | GPU | |
|---|---|---|
| Goal | low latency per thread | high throughput |
| Cores | 4–64 complex cores | hundreds–thousands of simple lanes |
| Control | branch prediction, OoO | many warps, in-order |
| Caches | large, multi-level | small per-SM, rely on warps |
| Memory | DDR, ~50–100 GB/s | HBM, ~1–3 TB/s |
| Programming | threads, tasks | SIMT kernels |
The GPU’s bet: latency is hidden by having many warps, so it does not need a big cache or a clever predictor. That works for data-parallel work and fails for pointer-chasing, branchy code, or a single dependent chain.
ASolution
Each load depends on the previous pointer, so there is no parallelism to hide the latency, and the random addresses defeat coalescing. The GPU’s throughput advantage disappears.
GPU threads, blocks, and grids
CUDA’s execution hierarchy:
- Thread: one lane, identified by
threadIdx. - Block: up to 1024 threads, scheduled on one SM, with its own shared memory and
barrier (
__syncthreads). - Grid: all blocks of a kernel launch, identified by
blockIdx.
__global__ void add(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
int threads = 256;
int blocks = (n + threads - 1) / threads;
add<<<blocks, threads>>>(d_a, d_b, d_c, n);
Blocks are independent: no ordering guarantees, no shared state between blocks. That is what lets the scheduler place them on any SM, and it is why grid-wide synchronization needs a separate kernel launch or cooperative groups.
ASolution
Shared memory, registers, and barrier resources are allocated per block, and the hardware limits how many threads can synchronize and share memory at once. The limit is a design constant, not a law.
GPU memory hierarchy
| Memory | Scope | Latency | Notes |
|---|---|---|---|
| Registers | thread | 1 cycle | fastest; limited per thread |
| Shared memory | block | ~20–30 cycles | explicitly managed, ~48–228 KB per SM |
| L1 cache | SM | ~30 cycles | backs local/global |
| L2 cache | device | ~200 cycles | shared across SMs |
| Global (HBM) | device | ~400–800 cycles | high bandwidth, high latency |
| Constant | device | cached, broadcast | read-only, fast when uniform |
| Local | thread | global memory | register spills; slow |
Global memory is large but slow, so the kernel’s job is to move data into shared memory or registers, reuse it, and write back once. Shared memory is the GPU’s equivalent of a user-managed cache, and bank conflicts are its hazard: 32 banks, one 4-byte word each; two threads in a warp hitting the same bank serialize.
paths?
ASolution
Shared memory is per-SM SRAM with a short, fixed latency, and no coherence traffic. Global goes through L1/L2 and HBM, with much longer latency and shared bandwidth.
Memory coalescing
A warp’s memory request is split into transactions. If the 32 threads access 32 consecutive 4-byte words, the hardware issues one 128-byte transaction. If they access strided or scattered addresses, it issues many.
// coalesced: thread i reads a[i]
c[i] = a[i] + b[i];
// strided: thread i reads a[i * stride]
c[i] = a[i * stride];
AoS layouts hurt here too:
struct Particle { float x, y, z; }; // 12 bytes
// reading only x: stride 12 bytes, 3x the transactions
SoA (x[], y[], z[]) gives contiguous per-field access and coalesced loads.
ASolution
Reading rows is coalesced but writing columns is not (or vice versa). Tiling through shared memory makes both the read and the write coalesced.
Warp divergence
A warp executes one instruction for all active threads. If threads branch differently, the paths are serialized:
if (threadIdx.x % 2 == 0) {
// even lanes
} else {
// odd lanes
}
Half the warp runs each path with the other half masked. The cost is the sum of both paths, not the maximum. Deeply nested or data-dependent branches can destroy throughput.
Fixes:
- Replace branches with arithmetic or predication when the work is cheap.
- Sort or group data so warps take the same path.
- Restructure the algorithm to avoid per-thread divergence.
Loops with uniform trip counts are fine; divergence is about within-warp differences.
blockIdx.x cause divergence?ASolution
No. All threads in a block share the same blockIdx, so the whole warp takes the
same path. Divergence needs different threadIdx values to disagree.
Occupancy
Occupancy is the ratio of active warps to the maximum supported per SM:
\[ \text{occupancy} = \frac{\text{active warps per SM}}{\text{max warps per SM}} \]
It is limited by:
- registers per thread,
- shared memory per block,
- threads per block (a block is scheduled as a unit),
- hardware limits on blocks and warps per SM.
Example: an SM supports 2048 threads (64 warps) and 65536 registers. A kernel uses 64 registers per thread and 256 threads per block:
- registers per block = \(256 \times 64 = 16384\),
- blocks by registers = \(65536 / 16384 = 4\),
- threads = 1024 → occupancy 50%.
If shared memory allows only 2 blocks, occupancy drops to 25%. Higher occupancy gives the scheduler more warps to hide latency, but it is not the goal by itself: a kernel with high ILP can run well at 25%.
ASolution
Fewer registers may force spills to local memory, which is global memory. A slightly lower occupancy with data in registers can beat high occupancy with spills.
CUDA fundamentals
The minimum vocabulary:
| Concept | Meaning |
|---|---|
__global__ |
kernel, called from host, runs on device |
__device__ |
device-only function |
__host__ |
host-only function (default) |
__shared__ |
block-shared memory |
__syncthreads() |
barrier for all threads in a block |
cudaMalloc / cudaFree |
device memory allocation |
cudaMemcpy |
host↔device copy |
cudaDeviceSynchronize |
wait for kernels to finish |
A reduction in shared memory, the standard pattern:
__global__ void reduce(const float* in, float* out, int n) {
__shared__ float s[256];
int tid = threadIdx.x;
int i = blockIdx.x * blockDim.x + tid;
s[tid] = (i < n) ? in[i] : 0.0f;
__syncthreads();
for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
if (tid < stride) s[tid] += s[tid + stride];
__syncthreads();
}
if (tid == 0) out[blockIdx.x] = s[0];
}
Two details that cause most bugs: the __syncthreads() inside the loop (all threads
must reach it), and the bounds check for the last block.
__syncthreads() inside the loop?ASolution
Without it, a thread can read s[tid + stride] before the thread that owns it has
written the updated value, producing a race.
Bugs and edge cases
- Uncoalesced access. Strided or AoS reads multiply transactions and kill bandwidth.
- Warp divergence in hot loops. Branchy inner code serializes paths.
- Bank conflicts in shared memory. Pad arrays or change the access pattern.
- Missing
__syncthreads(). Races inside a block; extra barriers cost little. - Assuming block execution order. Blocks run in any order; do not rely on it.
- Ignoring the PCIe/NVLink transfer. Moving data to the GPU can dominate the runtime; keep data resident and batch transfers.
- Occupancy as a target. Measure; some kernels are fastest at moderate occupancy.
- Register spills. Watch for local memory traffic when the compiler runs out of registers.
ASolution
Coalescing and divergence: whether warps read contiguous addresses and whether threads in a warp take the same branches. Both are visible in profilers such as Nsight Compute.
That is the end of the COA series. The next step is measurement: write a loop,
compile it with and without vectorization, and read perf stat before and after.