Salta al contenuto
Note per Studenti Memory Hierarchy and Cache Memory Report

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 tavg=thit+miss rate⋅tpenaltyt_{avg}=t_{hit}+\text{miss rate}\cdot t_{\text{penalty}}: for thit=1t_{hit}=1 ns, miss rate 5%5\% and penalty 5050 ns, 1+0.05⋅50=3.51+0.05\cdot50=3.5 ns.

Example. thit=2t_{hit}=2 ns, miss rate 10%10\%, penalty 4040 ns: tavg=2+0.10⋅40=6t_{avg}=2+0.10\cdot40=6 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 88 words, 33-bit cache address; main memory of 256256 words, 88-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 19,20,19,51,19,51,83,1919,20,19,51,19,51,83,19; index =a mod 8=a\bmod8, tag =⌊a/8⌋=\lfloor a/8\rfloor):

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

kk-way set associative

Definition (kk-way cache). A compromise. For each value of the low-order bits (the set index) a limited number kk of locations is available (e.g. k=2k=2), which greatly simplifies the match logic. A word can be only in kk locations (the kk ways of its set); three-state buffers connect the right output to the bus.

Example. 88 lines, 22 ways: 8/2=48/2=4 sets, set =a mod 4=a\bmod4; address 1919 goes to set 33 with tag ⌊19/4⌋=4\lfloor19/4\rfloor=4.

Same sequence on the 2-way cache (set =a mod 4=a\bmod4, tag =⌊a/4⌋=\lfloor a/4\rfloor, 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

Versione ripasso

Teoria collegata