Cache Set Collisions, False Sharing, and GPU Bank Conflicts
System DesignModel answerLast updated
Work through how addresses map to cache sets to show conflict misses, then separate them from false sharing and GPU shared-memory bank conflicts, with the right fix for each.
Three different performance problems are often lumped together under the vague label "cache-line conflict". Your task is to pull them apart.
Part 1: Set conflicts. Assume a set-associative cache with line size L bytes, S sets and A ways. An address maps to set floor(address / L) mod S. Using concrete numbers, show how a program can repeatedly evict its own data even though the working set is much smaller than the cache.
Part 2: Related problems. Explain how each of the following differs from a set conflict, and how you would diagnose and fix it:
- False sharing between threads running on different CPU cores.
- Bank conflicts in GPU shared memory (scratchpad).
What to cover
- Before answering, state which mechanism you think the interviewer means, and which questions you would ask (hardware, access pattern, alignment, thread-to-data mapping).
- A worked address example with actual numbers.
- For each of the three problems: the cause, the symptom, how to confirm it, and a fix that targets that cause specifically.
- Explain why a fix for one problem (for example, padding) can be useless or harmful for another.
Hints
Hint 1
Start by writing the address as tag | set index | line offset, and pick numbers where the stride between accesses is a multiple of S * L.
Hint 2
A set conflict is about one core's data fighting for ways in a set. False sharing is about different cores writing different variables that live in the same line. Ask which of those is happening.
Hint 3
On a GPU, shared memory is split into banks (typically 32, each 4 bytes wide). Threads in the same warp hitting different addresses in the same bank are serialized; think about what stride across a 2D tile causes this.
Hint 4
Match each fix to its cause: change stride or layout for set conflicts, align or pad per-thread data to line size for false sharing, add a column of padding or swizzle indices for bank conflicts.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarify first
"Cache-line conflict" is ambiguous. I would ask:
- Is the symptom on a CPU or a GPU?
- Is it single-threaded or multi-threaded? Are there writes?
- What is the access pattern (stride, array shapes) and the alignment?
- What are the cache parameters?
The answer depends on which of three mechanisms is at work.
1. Set conflicts (conflict misses)
Mapping. Split an address into tag | set index | offset. The offset is log2(L) bits, and the set index is floor(addr / L) mod S.
Worked example. Take L = 64 B, S = 64 sets, A = 8 ways. Total capacity is 64 * 64 * 8 = 32 KiB. The set index repeats every S * L = 4096 bytes.
Suppose a program walks a column of a row-major matrix of float with 1024 columns. The row stride is 1024 * 4 = 4096 bytes. Addresses base, base + 4096, base + 8192, ... all map to the same set. Only 8 of those lines can live in the cache at once, so touching 9 or more rows in a column evicts earlier rows. Total data touched may be only a few KiB, and the other 63 sets sit mostly empty, yet nearly every access misses on the next pass.
Symptoms. High L1/L2 miss rate with a small working set. Performance falls off sharply at power-of-two sizes and recovers at sizes like 1000 or 1025.
Confirm. Use hardware counters (L1D misses, conflict-related events), then sweep the matrix width and look for spikes at powers of two.
Fixes (all aimed at the mapping):
- Pad the row length (for example 1024 + 16 floats) so consecutive rows land in different sets.
- Tile or block the loop so the working set per tile spans different sets and fits within A ways.
- Reorder loops so the inner loop is unit-stride.
- Avoid power-of-two strides between hot arrays, or offset their base addresses.
- Higher associativity helps, but is a hardware choice.
2. False sharing
Cause. Coherence works at line granularity (typically 64 B). If thread 0 writes counter[0] and thread 1 writes counter[1], and both 8-byte values sit in the same line, the line bounces between cores in the modified state. Each write invalidates the other core's copy. The data is logically independent but physically shared.
Difference from set conflicts. The issue is not capacity or mapping into a set. It is cross-core invalidation traffic. It can occur in a nearly empty cache and gets worse with more threads.
Symptoms. Multi-threaded code that scales negatively (four threads slower than one), high coherence misses or "HITM" events, and a profile where the hot spot is a trivial increment.
Confirm. Use perf c2c or equivalent counters for cache-to-cache transfers. A quick experiment: pad the suspect fields and see if time drops.
Fixes:
- Align and pad per-thread data to the line size (
alignas(64)). - Accumulate into thread-local variables and merge once at the end.
- Separate read-mostly data from frequently written data.
Note that padding rows to avoid set conflicts does not help here, and padding per-thread structs to 64 B does nothing for a column-stride set conflict.
3. GPU shared-memory bank conflicts
Cause. Shared memory is split into banks (commonly 32 banks, 4 bytes wide). Word w is in bank w mod 32. A warp of 32 threads accesses in parallel if each thread hits a different bank, or if several threads read the same word (broadcast). If k threads access different words in the same bank, the access is serialized into k passes.
Worked example. A 32 x 32 float tile is stored row-major, and threads of a warp read down a column: thread t reads tile[t][c], at word index t * 32 + c. Since 32 mod 32 = 0, every thread lands in bank c, giving a 32-way conflict and 32x slower access.
Confirm. Use the profiler's shared-memory bank-conflict counters (for example Nsight Compute).
Fixes:
- Pad the tile to 32 x 33, so the word index is
t * 33 + cand bank(t + c) mod 32is distinct per thread. - Swizzle indices (for example XOR the column with the row).
- Change the thread-to-data mapping so a warp reads along a row.
How the three compare
| Set conflict | False sharing | Bank conflict | |
|---|---|---|---|
| Scope | One core's cache sets | Multiple cores' coherence | One warp, one SM |
| Resource contended | Ways in a set | Ownership of a line | Banks |
| Trigger | Strides that are multiples of S * L | Writes to different variables in one line | Same-bank, different-word accesses in a warp |
| Fix | Pad strides, tile | Pad/align per-thread data, use locals | Pad tile, swizzle |
Common mistakes
- Treating every slowdown as "cache thrashing" and padding blindly.
- Confusing capacity misses (working set too large) with conflict misses (working set fits but maps badly).
- Thinking false sharing needs a full cache; it does not.
- Assuming read-only sharing causes false sharing. It does not; lines stay in the shared state. Writes are required.
- Forgetting that a pad that fixes a bank conflict (33 columns) does not change the global-memory cache behavior, and vice versa.
- Ignoring that real caches may hash the index bits, so the simple modulo model is an approximation. Always verify with measurements.
Takeaway
Name the mechanism first, then use counters to confirm it. Set conflicts are fixed by changing strides or tiling, false sharing by separating per-thread writes onto different lines, and bank conflicts by changing the tile layout or the thread mapping.