Block Size and Next-Line Prefetch
A first read always misses. But one miss can bring the neighbours of a word along, and even the block after them. Both tricks have a point where they start to hurt.
You Will Be Able To
- Explain why a larger block removes misses of a program that walks through memory.
- Explain the two prices of a larger block, and choose a block size by cycles.
- Describe next-line prefetch, and say when it pushes out useful blocks.
- Lesson
- S3.19
- Time
- 24 min
- Runs on
- Your browser
- Check
- 6 questions
A team doubles the block of its cache, and the misses fall. They double it again, and the misses fall again. The program is now slower than before the first change.
Before You Start
You need S3.6: compulsory, capacity and conflict misses.
You also need spatial locality and the miss penalty, from S3.2.
| Question | |
|---|---|
| 1 | Why does a larger block remove misses? |
| 2 | What are the two prices of a larger block? |
| 3 | Why must a block size be chosen by cycles, not by misses? |
| 4 | What does next-line prefetch do? |
| 5 | When does a prefetch do harm? |
The Idea: Bet on the Neighbours
The last page ended with misses that no size and no shape removes: the first read of each block. This page removes some of them after all.
Programs often walk through memory word after word. So when word 0 misses, fetch words 1, 2 and 3 with it. One compulsory miss now pays for four reads. That is all a larger block is. The number of words in a block is the block size.
The bet has two prices. First, the cache holds the same number of words. With four words in a block it has a quarter of the lines. Fewer lines mean more fights, and more capacity and conflict misses.
Second, a miss takes longer, because more words must cross the bus. A miss that fetches words nobody reads is slower for nothing.
So the miss count falls and later rises again, and the time of each miss only rises. The cycles are what the program feels. A block size is chosen by cycles.
A prefetch makes the same bet in another way. On a miss, the cache also fetches the next block, before anyone asks for it. If the walk goes on, that block is already there.
New Words
| Word | Meaning |
|---|---|
| Block size | The number of words in one block of a cache. |
| Prefetch | To fetch a block into the cache before the program asks for it. |
| Next-line prefetch | A prefetch of the block that follows the block which just missed. |
| Useful prefetch | A prefetched block that the program reads before the cache pushes it out. |
| Cache pollution | Blocks that were brought in on a bet and never read. They take lines from blocks that are needed. |
One Picture: A Block, and the Block After It
- Processor
- Memory
- Bus
- Word 0 Is Not in the Cache
The processor reads word 0. The cache does not have it and asks the slow memory.
- A whole block comes back
The memory sends words 0 to 3. The processor wanted one of them. The others ride along.
- The neighbours now hit
A program that walks through an array reads words 1, 2 and 3 next. All three are hits.
- The next block, before it is asked for
A prefetch goes one step further. On the miss it also fetches block 1, because a walk will want it soon.
Read the diagram as text
- Processor: reads word 0, then 1, 2, 3 ...
- Cache: lines of one block each
- Slow memory: sends whole blocks
- Block 0: words 0 to 3 in one fetch
- Block 1: words 4 to 7, not asked for yet
- Processor → Cache: read word 0
- Cache → Slow memory: a miss
- Slow memory → Block 0: four words
- Block 0 → Cache: into one line
- Slow memory → Block 1: the next block too
A larger block and a prefetch are the same bet: the program will soon read what lies next to this word.
The Smallest Example, Worked by Hand
Take a direct-mapped cache of four lines, and read eight words in a row: 0 to 7. In this page a hit costs 1 cycle. A miss costs 8 cycles more, and 2 more for each word of the block.
| Design | Where the misses are | Misses | Cycles for one miss | Cycles in all |
|---|---|---|---|---|
| Blocks of 1 word | all eight | 8 | 1 + 8 + 2 = 11 | 8 x 11 = 88 |
| Blocks of 2 words | words 0, 2, 4, 6 | 4 | 1 + 8 + 4 = 13 | 4 x 13 + 4 = 56 |
| Blocks of 2 words, with prefetch | words 0 and 4 | 2 | 13 | 2 x 13 + 6 = 32 |
A hit adds 1 cycle. In this model a prefetch costs the processor no waiting time.
The second row halves the misses. Each miss is two cycles dearer, and the sum still falls from 88 to 56.
The third row: the miss on word 0 fetches block 0, and block 1 with it. So word 2 hits. Word 4 misses, and brings blocks 2 and 3. Two misses in all.
Why not blocks of 8 words, then? Here that would be one miss. The lab below asks the same question of a harder program.
The Rules
Rule 1: A larger block turns the first reads of its neighbours into hits.
It helps a program that walks through memory, and no other.
Rule 2: The same size in larger blocks means fewer lines.
Blocks that are far apart then fight over them.
Rule 3: A larger block makes every miss longer.
More words cross the bus, wanted or not.
Rule 4: Choose a block size by cycles.
The miss count alone hides the longer miss.
Rule 5: A prefetch that is not read is pollution.
It took a line, and it may have pushed out a block that was needed.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.3. See the part on larger blocks. They use spatial locality. Past a point both the miss rate and the miss penalty grow. Section 5.16 names prefetching.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.4: miss rate against block size, and why a small cache suffers first.
The prices of 1, 8 and 2 cycles are choices of this lab. So is the rule that a prefetch costs no waiting time. Real prices depend on the bus and the memory.
The program of the lab is our own teaching trace.
Now Run It
The lab at the end of this page is the counting model of the last page. Its cache always holds 16 words, cut into blocks of 1, 2, 4, 8 or 16 words.
The program walks through an array of 32 words. After each word it reads one of four counters, which lie far apart. So it has one part that walks, and one part that jumps.
words=1 lines=16 next=0: reads=64 misses=64 cycles=704
words=2 lines=8 next=0: reads=64 misses=50 cycles=664
words=4 lines=4 next=0: reads=64 misses=46 cycles=800
words=8 lines=2 next=0: reads=64 misses=50 cycles=1264
words=16 lines=1 next=0: reads=64 misses=64 cycles=2624
words=1 lines=16 next=1: reads=64 misses=50 cycles=564
words=2 lines=8 next=1: reads=64 misses=46 cycles=616
words=4 lines=4 next=1: reads=64 misses=50 cycles=864
words=8 lines=2 next=1: reads=64 misses=64 cycles=1600
words=16 lines=1 next=1: reads=64 misses=64 cycles=2624- No prefetch
- Next-line prefetch
- Words in a block 1No prefetch = 704 · Next-line prefetch = 564
Blocks of one word. Without prefetch no read hits. Prefetch already helps the walk here.
- Words in a block 2No prefetch = 664 · Next-line prefetch = 616
Blocks of two words: the lowest point without prefetch. One miss in the walk serves two reads.
- Words in a block 4No prefetch = 800 · Next-line prefetch = 864
Blocks of four words: the fewest misses without prefetch, but more cycles. Each miss now moves four words.
- Words in a block 16No prefetch = 2624 · Next-line prefetch = 2624
One block of 16 words is the whole cache. Every read pushes it out again.
Read the plot as a table
| Words in a block | No prefetch | Next-line prefetch |
|---|---|---|
| 1 | 704 | 564 |
| 2 | 664 | 616 |
| 4 | 800 | 864 |
| 6 | 1032 | 1232 |
| 8 | 1264 | 1600 |
| 10 | 1604 | 1856 |
| 12 | 1944 | 2112 |
| 14 | 2284 | 2368 |
| 16 | 2624 | 2624 |
Recorded by the lab. The lines join five measured points each. Both curves fall a little and then rise steeply.
Rule 1 at work: the first two rows. Blocks of two words cut the misses from 64 to 50. The walk now misses on every second word only.
Rules 2 and 3 at work: the fourth and fifth rows. With 8 words in a block the cache has two lines. With 16 it has one, and every read pushes that line out. The 64 misses now cost 41 cycles each.
Rule 4 at work: the third row. Blocks of four words have the fewest misses, 46. But the cycles rise from 664 to 800. The best block here has two words.
Now the last five rows. With one word in a block, prefetch saves 14 misses, and the cycles fall from 704 to 564. That is the best row of all ten.
Rule 5 at work: the row with 8 words and prefetch. The cache has two lines, and each miss fills both. The misses rise from 50 to 64. The prefetch pushes out the block that the next read wants.
This lab leaves out the data and the bus. Its prefetch is the simplest one: it acts only on a miss.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Blocks of several words | Blocks of one word | One miss serves a whole run of reads. Fewer lines for the same size, and a longer miss. |
| A block of moderate size | The largest block that fits | Most of the gain on a walk. Programs that jump still have lines to use. |
| Prefetch the next block | Wait for the miss | A walk finds its next block ready. A wrong bet takes a line and bus time. |
| Choose by cycles on a trace | Choose by the miss count | The answer includes the longer miss. It needs a price for each miss. |
The Trap: Fewer Misses, More Cycles
This is the team from the top of the page. They watch one number, the misses. Here are their three designs, taken from the run above.
| Block | Misses | Cycles |
|---|---|---|
| 1 word | 64 | 704 |
| 2 words | 50 | 664 |
| 4 words | 46 | 800 |
From the first three recorded rows. No prefetch.
The first change is a real gain: fewer misses and fewer cycles. The second change looks like a gain, and it is a loss. Four misses fewer, and 136 cycles more.
Each miss with a block of four words costs 17 cycles, where a block of two costs 13. The four misses saved do not pay for that. Rule 4 is the check: add up the cycles.
At Scale
This section looks ahead. Real caches settled on a block size long ago, and prefetch has grown far beyond the next line.
One block size for the chip
Common practice is one block size for all cache levels of a chip. It keeps the moves between levels simple.
Smarter prefetch
Real prefetchers watch for a steady step between misses, and fetch several blocks ahead. That is common practice, not checked here.
Prefetch and power
Every wrong bet costs bus traffic and energy. Small chips often leave prefetch out.
The next cure
Capacity misses that remain need another cure. The next lesson has it.
Try It
Run the lab. Find the row with the fewest misses and the row with the fewest cycles.
In program_run, remove the read of the counters. Predict which block size wins now, and run it.
Change T_BASE to 40, as for a far slower memory. Does the best block size move?
words=1 lines=16 next=0: reads=64 misses=64 cycles=704 words=2 lines=8 next=0: reads=64 misses=50 cycles=664 words=4 lines=4 next=0: reads=64 misses=46 cycles=800 words=8 lines=2 next=0: reads=64 misses=50 cycles=1264 words=16 lines=1 next=0: reads=64 misses=64 cycles=2624 words=1 lines=16 next=1: reads=64 misses=50 cycles=564 words=2 lines=8 next=1: reads=64 misses=46 cycles=616 words=4 lines=4 next=1: reads=64 misses=50 cycles=864 words=8 lines=2 next=1: reads=64 misses=64 cycles=1600 words=16 lines=1 next=1: reads=64 misses=64 cycles=2624Check Yourself
Common Mistakes
Wrong Choose the block size with the fewest misses.
A larger block makes every miss longer.
Fix Give each miss its price, and compare the cycles.
Wrong Expect a larger block to help every program.
It helps only reads that lie next to each other.
Fix Look at how the program moves. A program that jumps wants many lines.
Wrong Add prefetch to a cache with very few lines.
Each bet takes a line, and with few lines that line was in use.
Fix Count the prefetched blocks that were read before they were pushed out.
Remember
- A larger block brings the neighbours of a word on the same miss.
- It costs lines, and it makes each miss longer.
- Choose a block size by cycles on a trace, not by misses.
- Next-line prefetch fetches the block after a miss. A wrong bet is pollution.