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.

Five questions this page answers
Question
1Why does a larger block remove misses?
2What are the two prices of a larger block?
3Why must a block size be chosen by cycles, not by misses?
4What does next-line prefetch do?
5When 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

New words on this page
WordMeaning
Block sizeThe number of words in one block of a cache.
PrefetchTo fetch a block into the cache before the program asks for it.
Next-line prefetchA prefetch of the block that follows the block which just missed.
Useful prefetchA prefetched block that the program reads before the cache pushes it out.
Cache pollutionBlocks 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

One Miss Brings More Than One WordStep through the picture. A miss fetches a whole block, and then the block after it.
  • Processor
  • Memory
  • Bus
One Miss Brings More Than One WordStep through the picture. A miss fetches a whole block, and then the block after it. component Processor reads word 0, then 1,2, 3 ... component Cache lines of one blockeach component Slow memory sends whole blocks component Block 0 words 0 to 3 in onefetch component Block 1 words 4 to 7, notasked for yet read word 0a missfour wordsinto one linethe next block too
  1. component Processorreads word 0, then 1, 2, 3 ...
    • read word 0 → Cache
  2. component Cachelines of one block each
    • a miss → Slow memory
  3. component Slow memorysends whole blocks
    • four words → Block 0
    • the next block too → Block 1
  4. component Block 0words 0 to 3 in one fetch
    • into one line → Cache
  5. component Block 1words 4 to 7, not asked for yet
  1. Word 0 Is Not in the Cache

    The processor reads word 0. The cache does not have it and asks the slow memory.

  2. A whole block comes back

    The memory sends words 0 to 3. The processor wanted one of them. The others ride along.

  3. The neighbours now hit

    A program that walks through an array reads words 1, 2 and 3 next. All three are hits.

  4. 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.

Eight words in a row, three ways
DesignWhere the misses areMissesCycles for one missCycles in all
Blocks of 1 wordall eight81 + 8 + 2 = 118 x 11 = 88
Blocks of 2 wordswords 0, 2, 4, 641 + 8 + 4 = 134 x 13 + 4 = 56
Blocks of 2 words, with prefetchwords 0 and 42132 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

  1. 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.

  2. Rule 2: The same size in larger blocks means fewer lines.

    Blocks that are far apart then fight over them.

  3. Rule 3: A larger block makes every miss longer.

    More words cross the bus, wanted or not.

  4. Rule 4: Choose a block size by cycles.

    The miss count alone hides the longer miss.

  5. 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.

Printed by the lab on Icarus. Five block sizes with no prefetch, and then the same five with next-line prefetch.
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
TransientCycles Against Block Size, with and Without PrefetchRecorded by the lab: the cycles of one program of 64 reads. The cache always holds 16 words.
  • No prefetch
  • Next-line prefetch
050010001500200025003000Cycles for 64 reads246810121416Words in a block050010001500200025003000Cycles for 64 reads51015Words in a block
  1. 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.

  2. 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.

  3. 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.

  4. 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 blockNo prefetchNext-line prefetch
1704564
2664616
4800864
610321232
812641600
1016041856
1219442112
1422842368
1626242624

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 choiceThe alternativeWhat the choice buys, and costs
Blocks of several wordsBlocks of one wordOne miss serves a whole run of reads. Fewer lines for the same size, and a longer miss.
A block of moderate sizeThe largest block that fitsMost of the gain on a walk. Programs that jump still have lines to use.
Prefetch the next blockWait for the missA walk finds its next block ready. A wrong bet takes a line and bus time.
Choose by cycles on a traceChoose by the miss countThe 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.

What the team saw, and what the program felt
BlockMissesCycles
1 word64704
2 words50664
4 words46800

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 pageblocks of 1 to 16 words
A real cacheblocks of 64 bytes are common practice

This section looks ahead. Real caches settled on a block size long ago, and prefetch has grown far beyond the next line.

  1. 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.

  2. Smarter prefetch

    Real prefetchers watch for a steady step between misses, and fetch several blocks ahead. That is common practice, not checked here.

  3. Prefetch and power

    Every wrong bet costs bus traffic and energy. Small chips often leave prefetch out.

  4. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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

Check Yourself

Predict the outputTwelve words are read in a row through 4 lines with blocks of 4 words. First with no prefetch, then with next-line prefetch. What does the last row say?
// A counting model of one cache. A read names a word. The model counts hits, misses and cycles.
module tb;
  int LINES = 4;               // lines in the whole cache
  int WAYS  = 1;               // lines in one set
  int WORDS = 1;               // words in one block
  bit NEXT  = 0;               // 1: a miss also fetches the block after it (next-line prefetch)
  int SETS;
  localparam T_HIT = 1;        // cycles for a look in the cache
  localparam T_BASE = 8;       // a miss on top: 8 cycles to start the fetch ...
  localparam T_WORD = 2;       // ... and 2 more for each word of the block. All three are choices of this lab

  logic valid [0:63][0:15];    // the cache: [set][way]
  int   holds [0:63][0:15];
  int   used  [0:63][0:15];
  // Two helpers, only for naming a miss.
  bit   seen  [0:1023];        // every block that was ever read
  logic any_valid [0:63];      // a shadow cache: the same number of lines, but a block may use any of them
  int   any_holds [0:63], any_used [0:63];
  int now, reads, hits, misses, compulsory, capacity, conflict, cycles, prefetches;
  bit quiet = 1;

  task automatic start(input int lines, input int ways, input int words, input bit next);
    LINES = lines;  WAYS = ways;  WORDS = words;  NEXT = next;  SETS = LINES / WAYS;
    for (int s = 0; s < 64; s++) begin
      any_valid[s] = 0;
      for (int w = 0; w < 16; w++) valid[s][w] = 0;
    end
    for (int b = 0; b < 1024; b++) seen[b] = 0;
    now = 0;  reads = 0;  hits = 0;  misses = 0;  compulsory = 0;  capacity = 0;  conflict = 0;  cycles = 0;  prefetches = 0;
  endtask

  function automatic int way_of(input int block);        // the way that holds the block, or -1
    int set;
    set = block % SETS;
    way_of = -1;
    for (int w = 0; w < WAYS; w++) if (valid[set][w] && holds[set][w] == block) way_of = w;
  endfunction

  task automatic bring_in(input int block);              // an empty way first, or else the way used longest ago
    int set, victim;
    set = block % SETS;  victim = -1;
    for (int w = WAYS - 1; w >= 0; w--) if (!valid[set][w]) victim = w;
    if (victim < 0) begin
      victim = 0;
      for (int w = 1; w < WAYS; w++) if (used[set][w] < used[set][victim]) victim = w;
    end
    valid[set][victim] = 1;  holds[set][victim] = block;  used[set][victim] = now;
  endtask

  function automatic bit in_shadow(input int block);     // was the block in the shadow cache? Then use it there too
    int at, victim;
    at = -1;  victim = -1;
    for (int i = 0; i < LINES; i++) if (any_valid[i] && any_holds[i] == block) at = i;
    in_shadow = (at >= 0);
    if (at < 0) begin
      for (int i = LINES - 1; i >= 0; i--) if (!any_valid[i]) victim = i;
      if (victim < 0) begin
        victim = 0;
        for (int i = 1; i < LINES; i++) if (any_used[i] < any_used[victim]) victim = i;
      end
      at = victim;  any_valid[at] = 1;  any_holds[at] = block;
    end
    any_used[at] = now;
  endfunction

  task automatic read(input int word);
    int block, way;
    bit shadow_had_it;
    string kind;
    block = word / WORDS;
    now++;  reads++;
    shadow_had_it = in_shadow(block);
    way = way_of(block);
    if (way >= 0) begin
      hits++;  cycles += T_HIT;  kind = "hit";
      used[block % SETS][way] = now;
    end else begin
      misses++;  cycles += T_HIT + T_BASE + T_WORD * WORDS;
      if (!seen[block])        begin compulsory++;  kind = "miss, compulsory: the first read of this block"; end
      else if (!shadow_had_it) begin capacity++;    kind = "miss, capacity: the shadow cache lost it too"; end
      else                     begin conflict++;    kind = "miss, conflict: the shadow cache still had it"; end
      bring_in(block);
      if (NEXT && way_of(block + 1) < 0) begin
        prefetches++;  bring_in(block + 1);
      end
    end
    seen[block] = 1;
    if (!quiet) $display("read word %2d: block %2d, set %0d | %s", word, block, block % SETS, kind);
  endtask

  task automatic kinds;        // one row: the misses by kind
    $display("lines=%0d ways=%0d: reads=%0d hits=%0d misses=%0d | compulsory=%0d capacity=%0d conflict=%0d", LINES, WAYS, reads, hits, misses, compulsory, capacity, conflict);
  endtask
  task automatic costs;        // one row: the misses and the cycles
    $display("words=%0d lines=%0d next=%0d: reads=%0d misses=%0d cycles=%0d", WORDS, LINES, NEXT, reads, misses, cycles);
  endtask

  initial begin
    start(4, 1, 4, 0);  for (int i = 0; i < 12; i++) read(i);  costs;
    start(4, 1, 4, 1);  for (int i = 0; i < 12; i++) read(i);  costs;
  end
endmodule
ConceptWhy does a larger block remove misses in a walk through an array?
ConceptA cache keeps its size, and its blocks go from 2 words to 8. What happens to the number of lines?
ConceptA change of block size removes 4 misses and adds 136 cycles. What should the team do?
ConceptWhat does next-line prefetch fetch?
ConceptWhen does a prefetched block do harm?

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.

Practice

Report a problem with this page