Concurrency and parallelism, in 16 keywords

Learn what limits parallel speedup, what synchronization costs, and which lock or pattern fits a workload.

Your win today You can solve the disk + 32-core example on Concurrency pp.10–18 with Amdahl's law, explain why more cores give only 1.04×, and say which redesign gives 16×. About 40 minutes.

Page numbers are the numbers printed on the slides (from slide 17 on, they differ from the PDF page).

1 · Levels of parallelism and Amdahl's law

parallelism taxonomy ILP · SIMD · threads · NUMA · distributed
Each level is extracted by someone different (hardware, compiler, programmer, OS, frameworks), coordinated differently (registers, masks, locks, messages) and bounded by something different (dependencies, memory wall, Amdahl, remote latency, network) Concurrency p.5. It tells you who controls the parallelism and what will limit it Concurrency p.39.
coordination cost grows with granularity
Coordinating gets more expensive the coarser the level, from registers inside a core to messages between machines Concurrency p.6. The picture mirrors the memory/storage pyramid Concurrency p.6.
Amdahl's law Speedup = 1 / ((1 − p) + p/s)
With p the parallelized fraction and s the parallelism factor, T_new = T(1 − p) + T·p/s Concurrency p.8. The serial part caps the speedup: with 95% parallel, no number of cores gives more than 20× Concurrency p.9.
redesign the data movement read less · overlap · cache
Read only the needed bytes (columnar, projection pushdown), overlap I/O with compute so T = max(T_io, T_scan), and keep the data cached Concurrency p.17. "The fix is architectural (move fewer bytes), not more cores" Concurrency p.18.

In Amdahl's law, what does p stand for?

s is the parallelism factor, p the parallelized fraction Concurrency p.8.

A program is 90% parallel. Roughly what speedup does it reach with thousands of cores?

As s grows, Speedup → 1 / (1 − p) = 1 / 0.1 = 10; the 90% curve flattens at 10 Concurrency pp.8–9.

2 · OS concepts

processes vs threads shared address space
A process has one or more threads; threads share the memory address space and switch with less overhead Concurrency p.20. But one crashing thread can take down the whole process (e.g. a DBMS), and a multi-threaded design does not scale across machines Concurrency p.20.
context switch dispatch cost
The OS may switch a core to another thread when a process exits, a thread blocks on I/O or a semaphore, a thread is created, or an interrupt occurs Concurrency p.21. A switch flushes the pipeline and loads registers, may flush the TLB and loses cache locality; scheduling time is not processing time Concurrency p.21.
system call cost orders of magnitude
Processes use system calls for I/O and OS features; a system call costs orders of magnitude more than a function call Concurrency p.22. They are not getting faster over time, partly because of CPU security patches Concurrency p.23.

Which of these is NOT listed as a cost of a context switch?

The listed costs are pipeline flush, loading register state, possibly flushing the TLB and losing cache locality Concurrency p.21.

What is a disadvantage of building a DBMS as one multi-threaded process?

Threads share one address space, so one crash may kill the process Concurrency p.20.

3 · Synchronization basics

IPC messages · shared memory
Processes synchronize through message protocols or a shared memory region; threads of one process share memory and can read and write the same variables Concurrency p.25. Shared variables are exactly what makes synchronization necessary Concurrency p.26.
what we can assume cache-line atomicity · dependent order
Loads and stores are atomic at cache-line level and dependent instructions stay ordered; everything else can interleave in unexpected ways because of OoO execution and compiler reordering Concurrency p.27. A flag-and-sum handoff between two threads is not safe without extra synchronization Concurrency pp.26–27.
semaphore / mutex software, sleeping
Locks provided by software or the OS; a waiting thread typically sleeps until it may proceed Concurrency p.28. Sleeping costs a system call and a context switch, but frees the core.
hardware atomics and fences CAS · fetch-and-add · spinning
Compare-and-swap, fetch-and-add and atomic logical operations come from hardware; waiters typically spin. Fences ensure an instruction does not pass earlier loads Concurrency p.28. Spinning burns CPU but reacts fast when the wait is short.

T1 waits in while (done==0) while T2 does sum = sum + 1; done = 1. Why can T1 still read a stale sum?

Only dependent instructions keep their order; OoO execution and compiler reordering can interleave the rest Concurrency pp.26–27.

How does a thread typically wait on a hardware atomic such as compare-and-swap?

Hardware mechanisms typically require spinning; OS locks typically require sleeping Concurrency p.28.

4 · Choosing a lock

futex fast userspace mutex
Try the lock in user space with test-and-set; only if that fails, ask the kernel to queue the thread; on unlock, ask the kernel to wake a waiter Concurrency p.31. Built on the observation that most locks are uncontended; up to 100s of thousands of ops/s, and a good general choice Concurrency pp.31, 39.
reader/writer lock shared reads, exclusive writes
Readers access concurrently; writers get exclusive access, e.g. many balance queries vs few money transfers Concurrency p.33. A writer that stays long in its exclusive section blocks all readers Concurrency p.33.
copy-on-write lockless reads
With one level of indirection, readers read without locks; a writer copies the data, modifies the copy, then atomically swaps the pointer in the parent Concurrency p.34. Comparing pointers is not enough if, after several changes, the pointer returns to an old value Concurrency p.34.

When does the futex fast path avoid the kernel entirely?

Test-and-set succeeds in user space; the kernel is only called on failure or to wake a waiter Concurrency p.31.

When do the lock implementations in the benchmark perform about the same?

If writes are very rare, all methods perform similarly Concurrency p.30.

5 · Parallel processing patterns

dataflow / pipeline pattern stages + FIFO buffers
Split the work into stages, each parallel or sequential, and pass batches through circular FIFO buffers; used for PCIe devices, disks and networking Concurrency p.35. Throughput is set by the slowest stage, so size each stage's parallelism to equal throughput Concurrency p.37.
NUMA and synchronization latency vs bandwidth
Locking is latency-sensitive, so lock-heavy code suffers on NUMA; pipelines send batches, which is bandwidth-sensitive Concurrency p.38. If the application is compute-heavy, more parallel threads outweigh the extra sync cost, also in distributed pipelines Concurrency p.38.

A three-stage pipeline processes 2 M, 0.5 M and 1 M batches/s per stage. What is its throughput?

The pipeline speed is determined by the slowest stage Concurrency p.37.

Why does lock-heavy code degrade more on a NUMA machine than a batch pipeline?

Syncing is latency-sensitive, while sending batches is bandwidth-sensitive Concurrency p.38.

6 · Use the keywords: the exam question

Read 1 000 pages of 4 KB from an SSD at 1 GB/s, then scan 64 B tuples for the maximum age on a 2 GHz CPU with 32 cores at 1 instruction/cycle. What speedup do 32 cores give, and what would help instead? Concurrency pp.10, 17–18 Try it on paper first, then open each step.

Step 1: I/O time

1 000 × 4 KB = 4 MB; 4 MB / 1 GB/s = 4 ms of serial, bandwidth-bound I/O Concurrency pp.10, 18.

Step 2: Scan time

4 MB / 64 B = 64 000 tuples. The slides' breakdown shows 0.16 ms for the scan on 1 core, i.e. 320 000 cycles at 2 GHz, about 5 instructions per tuple Concurrency p.18.

Step 3: Amdahl with 32 cores

T = 4.16 ms, p = 0.16 / 4.16 ≈ 0.04. T_new = 4 + 0.16 / 32 ≈ 4.005 ms ⇒ speedup ≈ 1.04 Concurrency pp.8, 18.

Step 4: Read less

Only age is needed: 4 B of each 64 B tuple, so I/O drops 16× to 0.25 ms. 1 core: 0.25 + 0.16 = 0.41 ms ⇒ 10.1×; 32 cores: ≈ 0.255 ms ⇒ 16.3× Concurrency pp.17–18.

Step 5: Overlap and cache

With async I/O, T = max(T_io, T_scan); with 4 MB cached in RAM, repeated queries skip I/O entirely Concurrency p.17.

Exam answer pattern Split time into serial and parallel parts → Amdahl → name the serial bottleneck → fix the bytes moved, then add cores.

7 · Keyword recall drill

parallelism taxonomyEach level is extracted by someone different (hardware, compiler, programmer, OS, frameworks), coordinated differently (registers, masks, locks, messages) and bounded by something different (dependencies, memory wall, Amdahl, remote latency, network).
coordination costCoordinating gets more expensive the coarser the level, from registers inside a core to messages between machines.
Amdahl's lawWith p the parallelized fraction and s the parallelism factor, T_new = T(1 − p) + T·p/s.
redesign the data movementRead only the needed bytes (columnar, projection pushdown), overlap I/O with compute so T = max(T_io, T_scan), and keep the data cached.
processes vs threadsA process has one or more threads; threads share the memory address space and switch with less overhead.
context switchThe OS may switch a core to another thread when a process exits, a thread blocks on I/O or a semaphore, a thread is created, or an interrupt occurs.
system call costProcesses use system calls for I/O and OS features; a system call costs orders of magnitude more than a function call.
IPCProcesses synchronize through message protocols or a shared memory region; threads of one process share memory and can read and write the same variables.
what we can assumeLoads and stores are atomic at cache-line level and dependent instructions stay ordered; everything else can interleave in unexpected ways because of OoO execution and compiler reordering.
semaphore / mutexLocks provided by software or the OS; a waiting thread typically sleeps until it may proceed.
hardware atomics and fencesCompare-and-swap, fetch-and-add and atomic logical operations come from hardware; waiters typically spin.
futexTry the lock in user space with test-and-set; only if that fails, ask the kernel to queue the thread; on unlock, ask the kernel to wake a waiter.
reader/writer lockReaders access concurrently; writers get exclusive access, e.g.
copy-on-writeWith one level of indirection, readers read without locks; a writer copies the data, modifies the copy, then atomically swaps the pointer in the parent.
dataflow / pipeline patternSplit the work into stages, each parallel or sequential, and pass batches through circular FIFO buffers; used for PCIe devices, disks and networking.
NUMA and synchronizationLocking is latency-sensitive, so lock-heavy code suffers on NUMA; pipelines send batches, which is bandwidth-sensitive.

8 · True / False

T / FAdding cores always increases speedup proportionally.

False. The serial fraction caps it; you must increase the parallel portion Concurrency p.9.

T / FIn Amdahl's law, Speedup = 1 / ((1 − p) + p/s).

True Concurrency p.8.

T / FA multi-threaded application is always parallel.

False. It is not necessarily parallel, and a single thread can also contain parallelism Concurrency p.20.

T / FA context switch may flush the TLB and lose cache locality.

True Concurrency p.21.

T / FSystem calls have become much cheaper over the years.

False. They are not necessarily getting faster, partly due to CPU security patches Concurrency p.23.

T / FA CPU guarantees atomic loads and stores at cache-line level.

True Concurrency p.27.

T / FSemaphores and mutexes typically make waiting threads spin.

False. They typically sleep; hardware atomics typically spin Concurrency p.28.

T / FThe futex design is based on most locks being uncontended.

True Concurrency p.31.

T / FA software pipeline with one thread per stage needs no synchronization.

False. Stages exchange batches through shared FIFO buffers that must be synchronized Concurrency pp.35, 40.

T / FIn vectorization, compiler hints can always express the same detail as intrinsics.

False. Hints only tell the compiler what is safe; intrinsics choose the exact instructions Concurrency p.40; see lesson 0005.

9 · What is …?

What isthe five levels in the parallelism taxonomy

Show answer

Instruction-level, SIMD, thread-level on cores, thread-level across NUMA sockets, and distributed processes on many machines Concurrency p.5.

What iswhat bounds thread-level parallelism on one socket

Show answer

Amdahl's law and synchronization overhead Concurrency p.5.

What isa user-space thread (co-routine)

Show answer

A thread provided by the application and mapped onto OS threads Concurrency p.20.

What isthe events on which the OS may reschedule a core

Show answer

Process exit, a thread blocking on I/O or a semaphore, thread creation, an I/O or clock interrupt Concurrency p.21.

What isthe two Linux schedulers named on the slides

Show answer

CFS and EEVDF, which balance scheduler complexity against schedule quality Concurrency p.21.

What isfetch-and-add

Show answer

A hardware atomic that adds to a memory value and returns the old value in one indivisible step Concurrency p.28.

What isa memory fence

Show answer

An instruction that ensures a later instruction is not executed before all previous loads have completed Concurrency p.28.

What isthe problem with checking only the pointer in copy-on-write

Show answer

After several concurrent changes the pointer can end up equal to an old value, so the reader cannot tell the data changed Concurrency p.34.

10 · Calculations

ComputeA program is 95% parallel. What speedup does Amdahl's law give with 32 cores, and what is the limit with unlimited cores?

Show solution

Speedup = 1 / (0.05 + 0.95 / 32) = 1 / (0.05 + 0.0297) ≈ 12.5×. Limit: 1 / 0.05 = 20× Concurrency pp.8–9.

ComputeA job takes 100 s on 1 core; 60 s of it can be parallelized. What is T_new on 8 cores, and the speedup?

Show solution

p = 0.6, s = 8. T_new = 100·0.4 + 100·0.6 / 8 = 40 + 7.5 = 47.5 s. Speedup = 100 / 47.5 ≈ 2.1× Concurrency p.8.

ComputeIn the disk example, how many 64 B tuples are in the 1 000 pages of 4 KB, and how many bytes are read with projection pushdown of the 4 B age column? How long does that take at 1 GB/s?

Show solution

Tuples: 1 000 × 4 096 / 64 = 64 000. Bytes: 64 000 × 4 B = 256 KB, i.e. 1/16 of 4 MB. Time: 4 ms / 16 = 0.25 ms Concurrency pp.10, 17.

ComputeColumnar layout, 1 core, but now I/O and scan overlap with async I/O. Using T_io = 0.25 ms and T_scan = 0.16 ms, what is T and the speedup versus the naive 4.16 ms?

Show solution

T = max(0.25, 0.16) = 0.25 ms instead of 0.41 ms. Speedup = 4.16 / 0.25 ≈ 16.6×, about the same as 32 cores without overlap Concurrency pp.17–18.

ComputeA pipeline has stages with throughput per thread of 4 M, 1 M and 2 M items/s. How many threads does each stage need for a balanced 4 M items/s, and what is the throughput with one thread per stage?

Show solution

One thread each: 1 M items/s (slowest stage). Balanced: stage 1 needs 1, stage 2 needs 4, stage 3 needs 2 threads ⇒ 4 M items/s with 7 threads Concurrency p.37.

11 · Exam-style questions (written)

  1. Explain. Explain what a loop-carried dependency and a branching instruction have in common, from the perspective of a superscalar, pipelined processor Concurrency p.40.
  2. Compare. Compare an OS mutex, a spinlock on compare-and-swap and a futex: how does each wait, and when is each the right tool? Concurrency pp.28–31
  3. Argue. Can for x in 0..n: for y in 0..16: sum[y] += A[x*16+y] be vectorized? Why or why not? Concurrency p.40
  4. Transfer. If an application is parallelized as a software pipeline with one thread per stage, does it need no synchronization between threads? Explain why or why not Concurrency p.40.

Bring your answers to the chat and I'll grade them the way an examiner would.

Read next

Primary source: the deck (../lectures/Lecture_06_ConcurrencyParallelism.pdf). The slides recommend for futexes Franke et al., Fuss, Futexes and Furwocks (https://cis.temple.edu/~giorgio/cis307/readings/futex.pdf) and Basics of Futexes (https://eli.thegreenplace.net/2018/basics-of-futexes/) Concurrency p.31. Review with the cheat sheet.