L2 Intermediate · Module 4: CUDA programming fundamentals

4.4Shared memory and __syncthreads

Shared memory is a small, fast scratchpad that the threads of one block share, and __syncthreads is the barrier that makes sharing safe. Together they let a block load data once and reuse it many times, which is how most fast kernels work.

GPU required (Colab or a rented GPU)~20 min

By the end you will be able to

  • ✓Declare and use shared memory, and state its scope, lifetime and size limits
  • ✓Use __syncthreads correctly, and recognize the two ways to misuse it
  • ✓Write a block-level tree reduction in shared memory
  • ✓Explain why shared memory is the foundation of tiled matmul and attention kernels

Threads in a block can do something threads in different blocks can’t: share a fast on-chip memory and wait for each other.

Shared memory

Declare an array with __shared__ inside a kernel:

__shared__ float s[256];
  • Scope: one copy per block. All threads in the block see the same array; other blocks have their own.
  • Lifetime: created when the block starts, gone when it ends. Results must be written to global memory to survive.
  • Speed: it sits in the SM next to the cores, with latency of roughly 30 cycles versus hundreds for HBM (2.3).
  • Size: 48 KB per block by default; more with an opt-in (up to about 100 KB on consumer Ampere GPUs, 227 KB on an H100). Using more shared memory per block means fewer blocks fit per SM (2.5).

__syncthreads

__syncthreads() is a barrier: no thread in the block continues past it until every thread in the block has reached it. It also makes all shared-memory writes before the barrier visible to all threads after it.

You need it whenever a thread reads a value that a different thread wrote. Without it, the reader may run ahead of the writer.

A block-level sum

The classic first use is a tree reduction (5.1 goes much further). Each block sums 256 numbers:

// labs/m04/4.4/shared_sum.cu, without the template switch that turns the barriers off
__global__ void block_sum(const float* in, float* out, int n) {
__shared__ float s[256];
int tid = threadIdx.x;
int i = blockIdx.x * 256 + tid;
s[tid] = (i < n) ? in[i] : 0.0f; // 1. each thread loads one element
__syncthreads(); // every load must land before anyone reads
for (int stride = 128; stride > 0; stride /= 2) {
if (tid < stride) s[tid] += s[tid + stride]; // 2. pairwise sums: 128, 64, ..., 1
__syncthreads(); // finish this level before the next reads it
}
if (tid == 0) out[blockIdx.x] = s[0]; // 3. one thread writes the block total
}

256 values become 1 in log⁡2256=8\log_2 256 = 8 levels, with every load from HBM done exactly once.

What a missing barrier does

The lab runs the kernel 20 times with and without the __syncthreads() calls:

output (RTX 3050 6GB Laptop)
Summing 65536 blocks of 256 elements:
with __syncthreads() wrong block sums: 0 of 1310720 (20 runs)
without __syncthreads() wrong block sums: 1310720 of 1310720 (20 runs)

Every single block sum was wrong. Different warps in a block run independently, so without the barrier a thread reads s[tid + stride] before the warp responsible for it has written it.

Why it matters for LLM kernels

Lab: shared memory and the barrier 🔴

Terminal window
cd labs/m04/4.4 && nvcc -O2 -arch=native shared_sum.cu -o shared_sum && ./shared_sum

Sources

Check your understanding

0 / 4 answered
  1. 1.Which threads can see a __shared__ array?

  2. 2.What does __syncthreads() guarantee?

  3. 3.Why is __syncthreads() inside if (threadIdx.x < 32) a bug?

  4. 4.Why do tiled matmul kernels use shared memory?