Many Cores and Caches: What Changes

Everything this module did rests on one fact: with one core and no cache, a load returns what the program last stored. Add a second core and caches, and that stops being simple. On the lab models of module S9, this page shows what changes for the reference model. It shows how a cache subsystem is checked.

You Will Be Able To

  • Explain why a second core and caches break per-core lockstep of loads.
  • Tell true sharing from false sharing, and say what each costs.
  • Compare agent-driven and model-driven checking of a cache subsystem.
  • Say why ap_soc has neither, and what that leaves out of this track.
Lesson
V5.7
Time
22 min
Runs on
Your browser
Check
5 questions

Core A stores 11 to a word. Core B loads the same word one cycle later and gets 0. Is that a bug? It depends on which store the bus put first, and no model of one core can say.

Two Cores and One Shared LineStep through the S9.3 lab: two caches hold copies of one line, one core writes, the other must see it.
  • Processor
  • Bus
  • Memory
  • Data
Two Cores and One Shared LineStep through the S9.3 lab: two caches hold copies of one line, one core writes, the other must see it. processor Core A and itscache a copy of the line processor Core B and itscache a copy of the line bus Coherent bus invalidate, read miss,write back memory Memory may be out of date commandscommandsreads andwrite-backs
  1. processor Core A and its cachea copy of the line
    • commands → Coherent bus
  2. processor Core B and its cachea copy of the line
    • commands → Coherent bus
  3. memory Memorymay be out of date
  4. bus Coherent businvalidate, read miss, write back
    • reads and write-backs → Memory
  1. Both read

    A and B read the line: both copies are Shared, and agree with memory.

  2. B writes

    B must remove A's copy first: an invalidate on the bus. B's copy is now Modified; memory is old.

  3. A reads again

    A misses; B answers with its changed copy and writes it back. A must see B's value, not memory's old one.

Read the diagram as text
  • Core A and its cache: a copy of the line
  • Core B and its cache: a copy of the line
  • Coherent bus: invalidate, read miss, write back
  • Memory: may be out of date
  • Core A and its cache → Coherent bus: commands
  • Core B and its cache → Coherent bus: commands
  • Coherent bus → Memory: reads and write-backs

The cache lab of S9.3 (MSI, two caches, one line of two bytes). ap_soc has neither a second core nor a cache.

Before You Start

Read More Than One Core and Cache Coherence: MSI and MESI first, and Step Compare Against End-of-Test Memory Compare.

Five questions this page answers
Question
1What does a second core change for a model in lockstep?
2What must a cache subsystem guarantee, and how is that checked?
3What is false sharing, and why does it cost?
4When are bus agents the better stimulus, and when are core models?
5Why does ap_soc have neither, and what does that leave out?

The Idea: A Load Stops Being Predictable

With one core and plain memory, a load returns the last value the program stored there. The lockstep model of this module relies on that: memory is its own, and only devices come from the record. With two cores, the last store to a word may be the other core's, and which one was last depends on timing.

Caches add copies. Each core may hold the line in its own cache, and only a coherence protocol keeps the copies in agreement. The rule a checker needs is simple to state. Every read returns the value of the last write to that word, in the order the interconnect chose.

So the checking splits. Each core can still be checked in lockstep, with shared loads treated like device reads, taken from the record. Coherence itself gets a checker of its own: a model of memory fed with the writes in bus order, judging every read.

New Words

New words on this page
WordMeaning
HartA hardware thread that runs instructions: here, one core.
Cache lineThe block a cache copies at once; in the S9.3 lab, two bytes.
CoherenceAll copies of a line agree: a read sees the last write in one agreed order.
True sharingTwo cores use the same bytes of a line.
False sharingTwo cores use different bytes of the same line, and the line moves between them anyway.
Agent-drivenBus agents in the cores' seats make the traffic; a reference model of memory checks it.
Model-drivenInstruction-level models of the cores run programs; memory images and checkers check the result.

The Smallest Example: Check Four Reads by Hand

Here are the steps of the S9.3 lab's shared run. Keep one flat memory on paper: a write sets a word; a read must return what the paper says.

The shared run of the cache lab
StepRecorded
s1A read got=0000 bus=read-miss from=memory wb=- A=S B=I mem=0000
s2B read got=0000 bus=read-miss from=memory wb=- A=S B=S mem=0000
s3B write x=11 bus=invalidate from=- wb=- A=I B=M mem=0000
s4A read got=0011 bus=read-miss from=B wb=B A=S B=S mem=0011
s5A write x=22 bus=invalidate from=- wb=- A=M B=I mem=0011
s6A write x=33 bus=- from=- wb=- A=M B=I mem=0011
s7B write y=44 bus=write-miss from=A wb=A A=I B=M mem=0033
s8B evict bus=- from=- wb=B A=I B=I mem=4433

Recorded by the S9.3 lab on Icarus. got: the value a read returned.

A reads 0, B reads 0, B writes x = 11. Then A reads: the paper says 11, and A got the line 0011 (y = 00, x = 11) from B's cache. Every read agrees with the flat memory, although memory itself held 0 until B wrote its line back.

The Rules

  1. Rule 1: Every read returns the last write in bus order.

    A checker keeps one flat memory, updated in the order the interconnect serialised the writes.

  2. Rule 2: Shared loads come from the record.

    Per-core lockstep still works, if a load from shared memory is treated like a device read.

  3. Rule 3: Memory is right only after a flush.

    An end-of-test image compare needs every changed line written back first.

  4. Rule 4: Aim with agents, confirm with models.

    Agents put two cores on one line at once; models show what real code does.

  5. Rule 5: Count the line, not the byte.

    Cost follows lines: false sharing moves a line on every write.

Sources for These Rules

MEHTA-DV chapter 17: a cache subsystem checked with UVM agents in the processors' seats. Chapter 18: the same subsystem with instruction-level models of the processors and a final image of external memory. The book weighs the two and recommends agents first, then models.

The labs: content/modules/v3/soc/soc-s93-shared.mjs (two caches, MSI) and soc-s91-labs.mjs (two harts and a wake-up). The bus order of writes is the lab's own.

ap_rv32 has no cache, and Zifencei is left out with it: docs/soc/ap_rv32-blueprint.md, section 10. ap_soc v1.5 has one core: plan §3.2.

Now Run It: The Two-Cache Lab, Checked by a Model

Neither lab below is ap_soc. The S9.3 lab is a small model of two caches kept coherent by MSI. Its one line holds two bytes. The S9.1 lab has two hart bus-functional models, on a dual-hart option of the chip. A bus-functional model is bench code that drives the bus the way a processor would.

The slice at the end of this page is a model-driven checker of the S9.3 lab. It reads the recorded steps and checks each read against a flat memory: stale reads: 0.

The false-sharing run of S9.3: A writes x, B writes y, in one line, by turns.
s1 A write x=01 bus=write-miss from=memory wb=- A=M B=I mem=0000
s2 B write y=02 bus=write-miss from=A wb=A A=I B=M mem=0001
s3 A write x=03 bus=write-miss from=B wb=B A=M B=I mem=0201
s4 B write y=04 bus=write-miss from=A wb=A A=I B=M mem=0203
bus commands=4 flushes=3

Four writes to two different bytes took 4 bus commands, and every write found the line in the other cache. With x and y in two lines, the padded run took 1: rule 5. No byte was shared; the line was.

The two-hart lab of S9.1: hart 0 wakes hart 1 by its software interrupt word.
hart1 id=1: parks in wfi
hart0 id=0: boot hart, runs start-up code
e9 hart=0 op=store addr=02000004 wdata=00000001 result=00000000 resv=00 msip=00
e11 hart=1 op=store addr=02000004 wdata=00000000 result=00000000 resv=00 msip=10

Two harts already change the core's pin checks. Each has its own hart ID, its own software line and its own wake-up. A check per hart, not per chip.

Why It Is Built This Way

Agents are the better stimulus for a cache subsystem. They place two transactions on one line in the same cycle, again and again. A program on a model cannot aim that well. The book's advice follows. Use agents to find most of the bugs, then run models of the cores for the cases real code makes.

Models are the better check of meaning. A program's result has one right answer whatever the interleaving. So an end-of-test image after a flush catches what agents never thought to send. Its weakness is the one the step and memory compares showed: late, and silent on masked faults.

ap_soc has one core and no cache on purpose. This track teaches the chip-level method on a chip a learner can hold in one head, and runs every test in seconds. So the per-core lockstep of this module is exact, and coherence appears only in the S9 labs and here. A chip with several cores would need the checkers of this page in its bench.

The Trap: The Lockstep That Broke on Shared Memory

A team adds a second core and keeps one model per core, each owning its copy of memory. The first program that shares a flag fails: core B's model loads its own stale copy while the chip loaded A's store. The bench reports a core bug in a correct core.

Shared memory must come from the record, like a device, and coherence must have a checker of its own. The worksheet counts what sharing costs and chooses a method for each job.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. This track's chip has one core.

  1. Clusters

    Application processors have clusters of cores with shared caches; checkers watch every coherent port.

  2. Memory models

    RISC-V's weak memory order (RVWMO) allows some reorderings; checkers then check a read against every order the model allows, not one.

  3. Coherent interconnects

    ACE and CHI carry the protocol between clusters (module S9); verification IP checks each transaction against the protocol.

Try It

Run the slice: it checks each recorded read of the cache lab against a flat memory.

Which read would a cache that missed the invalidate get wrong?

The predict question gives it such a read.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: A reads word 0: got 0, model says 0 B reads word 0: got 0, model says 0 A reads word 0: got 11, model says 11 stale reads: 0

Check Yourself

Predict the outputThe slice now gives A's second read of x the value 0, as a cache that missed the invalidate would. What is the last line it prints?
// Steps of the two-cache lab (module S9): who, read or write, the word, and the value a read returned.
// The reference model is one flat memory: a read must return the last value written to that word.
module tb;
  localparam int N = 7;
  string who [N];
  bit write [N];
  int word [N], value [N];
  int memory [4];
  int wrong = 0;
  initial begin
    for (int w = 0; w < 4; w++) memory[w] = 0;
    who[0] = "A"; write[0] = 0; word[0] = 0; value[0] = 0;
    who[1] = "B"; write[1] = 0; word[1] = 0; value[1] = 0;
    who[2] = "B"; write[2] = 1; word[2] = 0; value[2] = 17;
    who[3] = "A"; write[3] = 0; word[3] = 0; value[3] = 0;
    who[4] = "A"; write[4] = 1; word[4] = 0; value[4] = 34;
    who[5] = "A"; write[5] = 1; word[5] = 0; value[5] = 51;
    who[6] = "B"; write[6] = 1; word[6] = 1; value[6] = 68;
    for (int i = 0; i < N; i++)
      if (write[i]) memory[word[i]] = value[i];
      else begin
        if (value[i] == memory[word[i]]) $display("%s reads word %0d: got %0h, model says %0h", who[i], word[i], value[i], memory[word[i]]);
        else begin
          $display("%s reads word %0d: got %0h, model says %0h  STALE", who[i], word[i], value[i], memory[word[i]]);
          wrong++;
        end
      end
    $display("stale reads: %0d", wrong);
  end
endmodule
ConceptWhat does a second core change for a model in lockstep?
ConceptWhat must a cache subsystem guarantee?
ConceptA writes x and B writes y, in one line, by turns. What is this?
ConceptWhy does ap_soc have neither a second core nor a cache?

Common Mistakes

Wrong Give each core's model its own copy of shared memory.

The other core's stores never reach it, so a correct chip fails.

Fix Take shared loads from the record; check coherence separately.

Wrong Compare memory before the caches are flushed.

A Modified line holds the newest value; memory is legally old.

Fix Flush, then compare images.

Wrong Think data not shared costs nothing.

Two variables in one line move together on every write.

Fix Place data that cores write apart in separate lines.

Remember

  • With several cores, a load's value depends on the order of writes, not on one program.
  • Per-core lockstep survives if shared loads come from the record; coherence needs its own checker.
  • Agents aim at the corners; models show what real code does; both are needed.
  • ap_soc has one core and no cache, so this module's lockstep is exact; the S9 labs show what changes.

Practice

Report a problem with this page