Which shelf the data lands on: cache associativity and writes
The Memory Hierarchy · Open board
The small fast caches in front of RAM. This lesson opens one of those boxes and shows where each piece of data is allowed to sit.
A programmer has a grid of 512 rows and 512 columns of 4-byte numbers. They add up one column, top to bottom. Then they change the grid to 513 columns and run the same loop. The loop does the same work on almost the same amount of data. Yet the 512 version is far slower. Nothing in the code explains it. The cause is inside the cache.
By the end of this lesson you will be able to predict when a cache throws out data it still had room for, and say what a cache does when your program writes data instead of reading it.
The history (optional)
A well-known Stack Overflow question asks why transposing a 512 by 512 matrix is much slower than transposing a 513 by 513 one. The answers blame cache associativity, the subject of this lesson.
Before you start.The Memory Hierarchy (cache, locality, the memory wall) gave you the cache itself: a small fast copy of recent memory, moved in 64-byte blocks called cache lines, where a hit is fast and a miss is slow. The CPU (fetch-decode-execute, the ISA, assembly) gave you the clock tick, the unit used for costs below. This lesson answers a question those two left open: when a new line arrives, which spot does it take?
Lesson 7 of 41 in How Computers Work
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.