Lecture 02 · Main Memory & Caching
Keywords → one line each. Page numbers refer to ../lectures/Lecture_02_MemoryAndCache.pdf. Slides 13–15, 30–32 are mostly figures.
Memory technology
| Keyword | One line | p. |
|---|---|---|
| von Neumann memory | Holds both instructions and data. Registers (≤1 cycle) → main memory (10s of cycles, random access) → external (100s+ cycles, block access) | 4 |
| ROM vs RAM | Non-volatile (ROM, PROM, EPROM, Flash) vs volatile (SRAM, DRAM) | 5–6 |
| static vs dynamic | SRAM keeps data while powered; DRAM leaks and needs refresh | 6 |
| access types | Random (RAM), sequential (tape), associative (by tag, e.g. TCAM), stack | 7 |
| UMA vs NUMA | Uniform access time vs access time depending on where the memory sits relative to the CPU | 7 |
| cell / location | Cell = 1 bit (flip-flop in SRAM, capacitor in DRAM); location = addressable group of 8/16/32 bits | 8 |
| matrix organisation | A location sits at the intersection of a row and a column; SRAM uses a decoder, cells and a data amplifier | 10–11 |
| bank · chip · rank · DIMM | Rows×columns · several banks (capacity) · chips read at the same address (bandwidth) · module; ≤16 DIMMs per CPU; DDR4 12–25 GB/s per DIMM | 12 |
| HBM | 3D-stacked, on-package, wide bus, >512-bit access unit, used on GPUs | 16 |
Virtual memory
| Keyword | One line | p. |
|---|---|---|
| virtual address space | Private and contiguous per process; can map to physical memory or to devices | 19 |
| MMU + OS | Translate every access, one page at a time; the page table lives in memory | 19 |
| lazy allocation | Allocating touches no physical memory; the mapping is made on first access | 20 |
| TLB | Associative cache of page-table entries, 1000s of entries, multi-level; a miss costs extra memory reads | 20–21 |
| huge pages | MB or 1 GB instead of 4 KB → fewer TLB entries needed, contiguous memory for DMA; risk of fragmentation | 21 |
Caches
| Keyword | One line | p. |
|---|---|---|
| temporal / spatial locality | Reused soon / neighbours used soon. This is why the hierarchy exists | 23–24 |
| inclusivity | Lower levels usually also hold the data of the higher levels | 25 |
| replacement policy | Chooses which line to evict; “a crucial optimization” | 25 |
| write-through / write-back | Write immediately / write when evicted (dirty line) | 25, 29 |
| caching everywhere | Web or DBMS results → DDR in front of flash → OS page buffer → CPU caches. Common challenges: invalidation, sizing, constant-cost lookup, coherence | 26 |
| prefetching | Next-line, stream, stride; kept simple; fails on pointer chasing; branch prediction plays this role for instructions | 27 |
| storage prefetching | Transparent prefetching from storage costs too much when it guesses wrong; prefetch from the app instead (async, pipelining) | 28 |
| cache line | 64 B; always loaded and evicted whole | 29 |
| conflicts / cache coloring | Many addresses map to the same slot; choose a data layout that avoids collisions | 30 |
| multi-level path | Reads fill L1; evicted lines move down to larger levels; dirty lines leaving the LLC go to DRAM; non-allocating loads skip the cache | 31 |
| MESI / snooping | Modified/Exclusive/Shared/Invalid; intentions broadcast on the interconnect (MOESI, MESIF in practice) | 33 |
Best practices p.34
- Random access across many pages → TLB bottleneck → sorted array instead of a hash table for range queries; huge pages
- Pointer chasing → can't prefetch → move the pointed-to data up into the parent structure
- Working set larger than the LLC → memory-bound → batch, then combine
- Measure with
perfand hardware performance counters
Exam-style questions from the deck p.36
- T/F: transistors added to a CPU but not used directly inside a core are not very useful.
- T/F: in directory-based coherence, a write to an Exclusive line incurs no coherence messages.
- Example of an application or data structure that suffers from TLB misses.
- Example of one that benefits from prefetching in main memory.
- What issue(s) do huge pages solve?