LESSON 26.03 · 6 STAGES · 3 CONTROLS · 3 CHECKS
Cores, threads and SIMD
Watch 4 cores, 8 threads and SIMD lanes share one vector add.
EXPLAINER · 26.03 · CORE BAY
01
Stage 1 of 6 · Split the work
4 threads split the 1,024 elements into chunks of 256. Each thread runs on its own core.
Explain it simply
A core is one processor inside the chip, and this toy chip has 4 of them. A thread is one list of instructions that a core runs. Here each thread gets its own chunk of the vectors, so the cores can work at the same time.
Three rows of 64 cache lines: a, b and c. Brackets split them into 4 chunks. Arrows lead to 4 cores.
a, bc, addscores, laneswaitsready, core busy
Toy: c = a + b, 1,024 floats, 4 cores
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
4
At 8 threads, two threads share each core.
8
Floats that one add instruction adds.
L1
Where the lines of a, b and c start.
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Each thread gets 1,024 over T elements.
Show the code ▾Hide the code ▴ · Try it yourself · Python 3
# 26.03 Cores, threads and SIMD: c = a + b on 4 cores, cycle by cycle
def mulberry32(seed): # the lesson's seeded random numbers
s = seed & 0xFFFFFFFF
def rnd():
nonlocal s
s = (s + 0x6D2B79F5) & 0xFFFFFFFF
t = ((s ^ (s >> 15)) * (1 | s)) & 0xFFFFFFFF
t = ((t + (((t ^ (t >> 7)) * (61 | t)) & 0xFFFFFFFF)) & 0xFFFFFFFF) ^ t
return (t ^ (t >> 14)) / 4294967296
return rnd
N, LINE_FLOATS, CORES = 1024, 16, 4
LATENCY = {"L1": 4, "RAM": 200} # cycles until a line arrives
HOLD = {"L1": 1, "RAM": 8} # cycles a 64-byte line holds the link
THREADS, LANES, DATA = 4, 8, "L1"
rnd = mulberry32(2601)
a = [int(rnd() * 1000) / 100 for _ in range(N)]
b = [int(rnd() * 1000) / 100 for _ in range(N)]
c = [0.0] * N
n = N // THREADS # elements a thread
core = [t % CORES for t in range(THREADS)]
free = [0] * (CORES if DATA == "L1" else 1) # when each link is free
def ask(t, now): # ask for 3 lines (a, b, c): return when the last arrives
link = core[t] if DATA == "L1" else 0
for _ in range(3):
free[link] = max(now + LATENCY[DATA] - HOLD[DATA], free[link]) + HOLD[DATA]
return free[link]
line = [0] * THREADS # line group each thread is on
left = [LINE_FLOATS // LANES] * THREADS # adds left in that group
ready = [ask(t, 0) for t in range(THREADS)]
last = [-1] * CORES # thread that added last on each core
waits, adds, turns = [0] * THREADS, [0] * THREADS, [0] * THREADS
done, now = [False] * THREADS, 0
while not all(done):
for k in range(CORES): # each core: one SIMD add a cycle
mine = [t for t in range(THREADS) if core[t] == k and not done[t]]
go = [t for t in mine if ready[t] <= now]
pick = next((t for t in go if t != last[k]), go[0]) if go else None
for t in mine:
if t == pick:
i = t * n + line[t] * LINE_FLOATS + (LINE_FLOATS // LANES - left[t]) * LANES
for j in range(i, i + LANES): # the lanes of one SIMD add
c[j] = round(a[j] + b[j], 2)
left[t] -= 1
adds[t] += 1
elif ready[t] <= now:
turns[t] += 1 # ready, but the other thread adds
else:
waits[t] += 1 # waiting for lines
if pick is not None:
last[k] = pick
now += 1
for t in range(THREADS):
if not done[t] and left[t] == 0:
line[t] += 1
if line[t] == n // LINE_FLOATS:
done[t] = True
else:
ready[t], left[t] = ask(t, now), LINE_FLOATS // LANES
print("time", now, "cycles") # time 128 cycles
print(f"thread 0: waits {waits[0]}, adds {adds[0]}, turns {turns[0]}") # thread 0: waits 96, adds 32, turns 0
print(f"c[0] = {c[0]:.2f}") # c[0] = 9.75
Copy it into a file and run it. Change THREADS, LANES and DATA to match the controls.
Common mix-ups
MYTHEight SIMD lanes make the add eight times faster.
FIXFrom RAM, a thread waits 216 cycles per line and adds for 2. It takes 13,952 cycles with 8 lanes, 14,848 with 1.
MYTHTwo threads on one core make it twice as fast.
FIXThe two threads share one SIMD unit and only fill its waits: 352 cycles become 283, not 176.
Predict, then reveal
0 / 3 REVEALEDQ01One thread reads from RAM. How much faster do 16 lanes make it than 1?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02From RAM with 8 lanes, do 8 threads beat 4 threads on 4 cores?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03From L1 with 4 threads, do 16 lanes run twice as fast as 8?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Takeaways
- Cores split the work and add their chunks at the same time.
- One SIMD add handles 4 to 16 floats.
- A second thread on a core shares its SIMD unit; it overlaps waits.