Master the fundamental concepts of cache optimization through this focused micro-challenge.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksC 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.
Why is summing a matrix row by row so much faster than column by column? Wall-clock timings are noisy, so this task measures the cause directly. Simulate a set-associative cache with LRU replacement, and count the misses that each traversal order produces on a row-major C array.
cLoading…
(r * COLS + c) * ELEM, starting at 0.SIZE / (LINE * WAYS) sets. An address maps to block addr / LINE, which goes to set block % sets with tag block / sets. On a miss, fill an empty way, or else evict the least recently used way. Hits and fills both count as a use.cache SIZE LINE WAYS: invalid geometry and keep the old cache.ROWS * COLS <= 4000000 (otherwise matrix: invalid size).cLoading…
The miss rate and the ratio are rounded half up to 2 decimals using integer arithmetic. Bytes fetched = misses * LINE.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover a matrix small enough to fit (both orders equal), a direct-mapped cache where a power-of-two row length makes every column access conflict, a padded row that removes the conflicts, 8-byte elements, a single traversal, and invalid geometry.