Master the fundamental concepts of cache optimization through this focused micro-challenge.
False sharing happens when two threads write different variables that live in the same 64-byte cache line. Each write forces the line to bounce between cores even though the threads touch disjoint data. OpenMP reductions and per-thread counters often hit this bug.
Symptoms: scaling collapses when you add cores to an embarrassingly parallel loop. Fix: pad structs or arrays so each thread's hot field occupies its own cache line.
cLoading…
perf c2c on Linux maps line conflicts to source linesalignas(64)Keep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will demonstrate false sharing between two threads updating adjacent counters, then fix it with padding and compare timings. This exercise requires showing improved scaling after separation.
Demonstrate and fix false sharing in multithreaded code.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github