TactivergeLite

Lite / Textbooks / Digital Logic and Computer Architecture / Chapter 9

9. The memory hierarchy

Version 1 · Technically checked (automated checks only) · 3 source lessons · Full interactive version

Introduction

A processor can work far faster than main memory can supply data. Caches close the gap by keeping recently and nearby used data in small, fast storage close to the processor. This chapter explains why programs show locality, how an address is split into tag, index and offset, and how to measure the result with the average memory access time.

Figure 9.1. The memory hierarchy and AMAT (described in words)

A stack of levels from top to bottom: registers, L1 cache, L2 cache, main memory, storage. An arrow on one side says smaller and faster towards the top; an arrow on the other says larger and slower towards the bottom. Below: AMAT = hit time + miss rate × miss penalty, for example 1 + 0.05 × 100 = 6 cycles.

Explanation

Built from 3 School lessons. Each part is the lesson's own explanation.

9.1 Locality and the memory hierarchy

Two loops add up the same million numbers. One is ten times slower. Same chip, same maths. What is different?

The hierarchy

Registers (bytes to a few KB, under a nanosecond), L1 cache (tens of KB, about 1 ns), L2/L3 (hundreds of KB to tens of MB, several to tens of ns), DRAM (GB, around 100 ns), flash or disk (TB, microseconds to milliseconds). Each level is bigger, slower and cheaper per byte than the one above. Typical figures vary widely between chips.

Temporal locality

Data used now is likely to be used again soon: loop counters, a filter's coefficients, the top of the stack. Keeping recently used data in a small fast memory serves those repeat visits.

Spatial locality

Data near what you just used is likely to be used next: the next array element, the next instruction. Caches move whole blocks (often 64 bytes) so one miss pre-loads the neighbours. Walking a 2-D array row by row in a row-major language uses this; walking it column by column wastes it.

Common mix-ups

Open the Lite lesson: Locality and the memory hierarchy

9.2 Inside a cache: tags, sets and ways

A cache holds a tiny fraction of memory. When the CPU asks for address 0x1F40, how does the cache know in one nanosecond whether it has it?

Offset, index, tag

With B-byte blocks, the lowest log₂ B bits are the offset (which byte in the block). With S sets, the next log₂ S bits are the index (which set to look in). The remaining high bits are the tag, stored with the block and compared on every access. Sets S = cache size ÷ (block size × ways).

Direct-mapped versus set-associative

Direct-mapped (1 way): each block has exactly one possible line. Fast and simple: one tag comparison. N-way set-associative: each set holds N blocks, so N comparisons happen in parallel and a multiplexer picks the hit way. Conflict misses fall; area, energy and sometimes hit time rise. Fully associative is one set holding everything.

Replacement and writes

When a set is full, LRU (least recently used) evicts the block untouched for longest; real caches often approximate it. Stores need a policy too: write-through sends every write to the next level; write-back marks the block dirty and writes it only on eviction, saving bandwidth.

The hardware cost

Each way needs a tag-width equality comparator: one XNOR per bit plus an AND tree, and a valid bit. A 4-way cache with 18-bit tags needs 4 × 18 = 72 XNORs before the AND trees and way multiplexer. That is the area and energy behind 'just add associativity'.

Common mix-ups

Open the Lite lesson: Inside a cache: tags, sets and ways

9.3 Average memory access time

A cache hits 95% of the time. Is that good? It depends entirely on what the other 5% cost.

One formula, three levers

AMAT = hit time + miss rate × miss penalty. Reduce hit time (small, simple L1), reduce miss rate (bigger, more associative, better prefetching) or reduce miss penalty (an L2, faster memory, critical-word-first). They fight: bigger and more associative usually means slower hits.

Local and global miss rates

L2's local miss rate looks bad (20–50% is common) because L1 has already filtered out the easy accesses. What matters for AMAT is the global rate m₁·m₂, often well under 1%.

Back to the Iron Law

Memory stalls add to CPI. A program with 0.3 memory accesses per instruction and 1.5 extra cycles per access from misses gains 0.45 CPI: as much as many branch-prediction improvements. This is why memory is often called the bottleneck.

Derivation: AMAT for two levels, and back into CPI
  1. One level: every access pays h₁; a fraction m₁ also pays the penalty P₁. AMAT = h₁ + m₁P₁.

  2. With L2, an L1 miss is served by L2, whose own average time is h₂ + m₂·t_mem, where m₂ is L2's local miss rate (misses ÷ accesses that reach L2).

  3. So P₁ = h₂ + m₂·t_mem and AMAT = h₁ + m₁(h₂ + m₂·t_mem).

  4. Global L2 miss rate = m₁·m₂: the fraction of all accesses that reach memory.

  5. Memory stall cycles per instruction = (memory accesses per instruction) × (AMAT − h₁), if the hit time is already counted in base CPI.

  6. Example: h₁ = 1, m₁ = 0.05, h₂ = 10, m₂ = 0.2, t_mem = 100: AMAT = 1 + 0.05 × 30 = 2.5 cycles, versus 6 without L2.

Common mix-ups

Open the Lite lesson: Average memory access time

Worked examples

Locality and the memory hierarchy: example 1

A C program sums a 1000×1000 int matrix as sum += a[j][i] (column by column). Why is it slow, and what is the fix?

Solution

C stores rows contiguously, so a[j][i] then a[j+1][i] are 4000 bytes apart: every access touches a new block. Swap the loops to a[i][j] so consecutive accesses share blocks; the miss rate drops from about 100% to about 4/64 ≈ 6%.

Inside a cache: tags, sets and ways: example 1

A 32 KB direct-mapped cache, 64 B blocks, 32-bit addresses. Split the address.

Solution

Offset = log₂ 64 = 6. Sets = 32768/64 = 512, index = 9. Tag = 32 − 9 − 6 = 17 bits.

Inside a cache: tags, sets and ways: example 2

Same cache made 2-way. What changes?

Solution

Sets halve to 256, so index = 8 and tag = 18. Two 18-bit comparators run in parallel per access.

Average memory access time: example 1

Option A: 1-cycle L1, 6% miss rate. Option B: 2-cycle L1, 3% miss rate. Both backed by a 40-cycle L2 that never misses. Which is better?

Solution

A: 1 + 0.06×40 = 3.4. B: 2 + 0.03×40 = 3.2. B wins slightly; with a 30-cycle L2 they tie at 2.8 and 2.9 (A wins). The answer depends on the penalty.

Interactive figures, in words

Figure 9.2 from Locality and the memory hierarchy

Run 'Walk an array', then 'Small loop, twice', then 'Big stride', all direct-mapped with 16 B blocks. For each, predict the hit rate before you press Show all. Then try 32 B blocks on the array walk and on the stride.

The cache simulator lists each address, its set and tag, and whether it hits. With 16-byte blocks: walking 32 integers gives 8 misses and 24 hits; reading 16 integers twice gives 4 misses; a 64-byte stride misses every time.

Figure 9.3 from Average memory access time

Start with L2 off. Raise main-memory latency and watch AMAT climb. Turn L2 on and find the L2 miss rate at which it stops being worth having. Which change saves more: halving the L1 miss rate, or halving the L2 hit time?

The AMAT explorer shows a stacked bar: L1 hit time, plus L1 misses times L2 time, plus global misses times memory time. With h₁ = 1, m₁ = 5%, h₂ = 10, m₂ = 20% and memory 100 cycles, AMAT = 2.5 cycles; without L2 it is 6.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. A filter applies the same 5 coefficients to every sample. Which locality do the coefficients show?
    Answer it in “Locality and the memory hierarchy”
  2. A loop reads 256 consecutive 4-byte floats into an empty cache with 64-byte blocks. How many misses?
    Answer it in “Locality and the memory hierarchy”
  3. A 32 KB 4-way set-associative cache has 16-byte blocks and 64-bit byte addresses. How many offset bits are there?
    Answer it in “Inside a cache: tags, sets and ways”
  4. How many single-bit XNOR gates do the tag comparators of an 8-way cache with 20-bit tags need?
    Answer it in “Inside a cache: tags, sets and ways”
  5. A cache has a hit time of 3 cycles, a miss rate of 1% and a miss penalty of 100 cycles. What is the AMAT in cycles?
    Answer it in “Average memory access time”
  6. L1 miss rate is 4% and L2 local miss rate is 25%. What percentage of all accesses go to main memory?
    Answer it in “Average memory access time”
  7. Base CPI is 1. There are 0.4 memory accesses per instruction, L1 hit time is included in base CPI, and AMAT is 3 cycles with a 1-cycle hit time. What is the effective CPI?
    Answer it in “Average memory access time”

Engineering applications

Faster image processing

Looping over an image row by row reads neighbouring bytes, so each cache block fetched is fully used. Looping column by column can touch a new block on almost every read and run much slower on the same data.

Embedded memory budgets

Small microcontrollers may have no data cache at all, so their designers place time-critical data in on-chip memory instead. Knowing the hierarchy tells you where each variable should live.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Memory hierarchy and caches
Computers use a hierarchy of memories from tiny fast registers and caches to large slow main memory and storage; a cache keeps recently used data close to the processor. Field Guide
Average memory access time (AMAT)
Average memory access time is the cache hit time plus the miss rate multiplied by the miss penalty. Field Guide
RAM, flash and EEPROM
RAM is fast, volatile working memory that loses its contents without power; flash and EEPROM are non-volatile and keep data, but are slower to write and wear out after many write cycles. Field Guide
Address space
The address space is the range of addresses a processor can generate. With n address bits and byte addressing, it can reach 2^n bytes. Field Guide

Project: Size a cache for a sensor log

A controller reads a 4 KiB log sequentially, again and again. Compare a 1 KiB and an 8 KiB cache with 16-byte blocks: estimate the miss rate of each on repeated passes and the AMAT with a 1-cycle hit time and a 50-cycle miss penalty. Explain the difference using locality.

Deliverables

Write it up on paper or in your own notes. Lite never stores what you write.

References

← 8. Performance and pipelining10. Power, performance and the chip →