Master the fundamental concepts of cache optimization through this focused micro-challenge.
C stores two-dimensional arrays in row-major order: row 0's elements sit contiguously, then row 1, and so on. Ulrich Drepper's memory guide opens with loop interchange because traversing columns in the outer loop jumps by N * sizeof(int) bytes, blowing cache lines on every step.
CPUs fetch 64-byte cache lines. Sequential row access touches each line once per 16 ints (typical). Column-first access on a 1000x1000 matrix causes a miss almost every load.
cLoading…
perf stat -e cache-misses to see the gapKeep 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 benchmark row-major vs column-major traversal on a large matrix and print both timings. This exercise asks you to explain the ratio using cache lines and stride.
Demonstrate performance difference between row-major and column-major matrix traversal.
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