Memory Hierarchy and Cache Memory
In questa pagina 3
Memories trade capacity against speed: the larger, the slower. SRAM is small and very fast, DRAM larger and slower, disks very large and slow (Main Memory - RAM, ROM and Chip OrganisationA memory is an array of cells (words of several bits) with control circuitry; memories are classified as read-only or read/write, random, serial or hybrid access, volatile or non-volatile. A RAM of 2^k x n bits has k address bits (independent of n), n data bits, read/write and chip select. SRAM uses a bistable cell (latch), is fast and needs no refresh; DRAM stores charge in a capacitor, is denser and needs refresh. Chips use row and column decoders (coincident selection), tri-state outputs, and arrays of chips for more words or more bits per word.Main Memory - RAM, ROM and Chip Organisation →). To get speed and capacity a hierarchy of memories is used.
Hierarchy and locality
The hierarchy works because the CPU:
- often reuses the same data (temporal locality);
- often uses nearby data (spatial locality), e.g. vectors, whose elements are in contiguous locations.
Formula (average access time). The CPU should find the data in the cache as often as possible: cache hit = data in the cache; hit rate = percentage of hits. Otherwise a cache miss occurs (miss rate) and the data are fetched as a block from the other memories. A standard model is : for ns, miss rate and penalty ns, ns.
Example. ns, miss rate , penalty ns: ns.
Cache placement
A cache holds a copy of some main-memory locations. The question is where to put a main-memory datum. Three solutions.
Direct mapping
Definition (direct-mapped cache). Groups of contiguous main-memory locations are mapped directly to one cache location; the most significant address bits identify the group transferred (tag). Simple, but the hit rate is not high.
Example. A cache of words, -bit cache address; main memory of words, -bit word address. The last three bits are the cache index, the 5 most significant bits are the tag, stored with the datum. On an access the index selects the line and the stored tag is compared with the address tag: equal (and line valid) means hit, otherwise miss and the data are copied from memory, replacing the line.
Accesses (word addresses ; index , tag ):
| address | binary | index | tag | outcome |
|---|---|---|---|---|
| 19 | 00010011 | 3 | 2 | miss |
| 20 | 00010100 | 4 | 2 | miss |
| 19 | 00010011 | 3 | 2 | hit |
| 51 | 00110011 | 3 | 6 | miss (replaces 19) |
| 19 | 00010011 | 3 | 2 | miss (replaces 51) |
| 51 | 00110011 | 3 | 6 | miss |
| 83 | 01010011 | 3 | 10 | miss |
| 19 | 00010011 | 3 | 2 | miss |
One hit out of . If a program keeps accessing locations with the same 3 low address bits (19, 51, 83 all have index 3) there are continuous misses (conflict misses).
Fully associative
Definition (fully associative cache). The association between main-memory and cache locations is arbitrary: any datum can sit in any line. The tag is the whole address. Performance is optimal but complexity high: comparing all tags in sequence is not feasible, so an associative memory compares in parallel: writing is conventional, reading is in parallel; each line's match signal drives its word line and the OR of all matches is the hit/miss signal.
Example. With 8-bit word addresses the tag is the whole 8-bit address, compared in parallel with all lines.
-way set associative
Definition (-way cache). A compromise. For each value of the low-order bits (the set index) a limited number of locations is available (e.g. ), which greatly simplifies the match logic. A word can be only in locations (the ways of its set); three-state buffers connect the right output to the bus.
Example. lines, ways: sets, set ; address goes to set with tag .
Same sequence on the 2-way cache (set , tag , replacement LRU = least recently used):
| address | set | tag | outcome | set content after |
|---|---|---|---|---|
| 19 | 3 | 4 | miss | {4} |
| 20 | 0 | 5 | miss | {5} |
| 19 | 3 | 4 | hit | {4} |
| 51 | 3 | 12 | miss | {4, 12} |
| 19 | 3 | 4 | hit | {12, 4} |
| 51 | 3 | 12 | hit | {4, 12} |
| 83 | 3 | 20 | miss (LRU way: 4) | {12, 20} |
| 19 | 3 | 4 | miss | {20, 4} |
3 hits out of 8: associativity removes the conflict between 19 and 51.
Spatial locality and blocks. To exploit spatial locality several contiguous locations are brought into the cache at once (e.g. a 2-way cache with 4-word blocks, with the data memories replicated for the 4 words). A 256 K cache block diagram contains tag memories, data memories, comparators and an output MUX.
Common mistakes
- Saying that in a direct-mapped cache a datum can sit in several places: it has one.
- Confusing tag and index: the index (low bits) selects the line, the tag (high bits) is compared.
- Treating a cache miss and a page fault as the same: a miss is solved by main memory, a page fault by the disk (Virtual Memory and PagingVirtual memory shows every program the whole size of main memory plus disk, with the same addresses usable by all programs without interference; dedicated hardware translates virtual to physical addresses page by page using a page table whose rows carry valid, dirty and used bits. The offset is unchanged by the translation. The TLB caches recent translations to avoid extra memory accesses, and a page fault (valid bit 0) means the page must be fetched from disk.Virtual Memory and Paging →).
- Believing a cache as fast as main memory is useful: it only helps if it is faster.
Versione ripasso
- Hierarchy: SRAM (small, fast) DRAM disk, effective thanks to temporal and spatial locality.
- Average time: ; ns; ns.
- Direct mapped: 8 lines, 8-bit addresses: index = 3 low bits, tag = 5 high bits; each datum has one place. Sequence (index 3 for all but 20): miss, miss, hit, miss, miss, miss, miss, miss, so one hit in 8 (conflict misses).
- Fully associative: tag = whole address, parallel compare in an associative memory; OR of the matches gives hit/miss.
- -way: a datum only in the ways of its set. 8 lines, 2 ways: set , tag ; same sequence: miss, miss, hit, miss, hit, hit, miss (LRU drops 4), miss, so 3 hits in 8.
- Blocks: contiguous words are fetched together to exploit spatial locality.
- Mistakes: several places in direct mapping; tag and index swapped (index selects the line, tag is compared); miss equated with page fault (Virtual Memory and PagingVirtual memory shows every program the whole size of main memory plus disk, with the same addresses usable by all programs without interference; dedicated hardware translates virtual to physical addresses page by page using a page table whose rows carry valid, dirty and used bits. The offset is unchanged by the translation. The TLB caches recent translations to avoid extra memory accesses, and a page fault (valid bit 0) means the page must be fetched from disk.Virtual Memory and Paging →).