Salta al contenuto
Note per Studenti Memory Hierarchy and Locality Report

Memory Hierarchy and Locality

In questa pagina 5
In questa pagina 3

Characteristics of a memory

Characteristic Possibilities
location registers (CPU), internal (cache, main memory), external (disks, tapes)
capacity in bytes or in words
transfer unit word (between CPU and memory), block (between cache and main memory, between memory and disk)
access method sequential (tape), direct (disk: jump to the zone, then search), random (RAM: same time for every address), associative (by content: cache)
performance access time, cycle time, transfer rate
technology semiconductor, magnetic, optical
physical characteristics volatile / non-volatile, rewritable / read-only

The trade-off

The faster a memory, the more it costs per bit; the larger, the slower. Solution: a hierarchy of levels.

Level Typical capacity Typical access time
registers hundreds of bytes < 1 ns
cache L1, L2, L3 tens of KB to tens of MB 1-20 ns
main memory (DRAM) GB 50-100 ns
SSD hundreds of GB - TB 25-100 microseconds
magnetic disk TB 5-10 ms

Going down: lower cost per bit, larger capacity, longer access time, lower frequency of access by the CPU. Each level holds a subset of the data of the level below; data move between adjacent levels in blocks.

Principle of locality

The hierarchy works because programs do not access memory at random:

Definition (temporal and spatial locality)

  • temporal locality: a datum used recently will probably be used again soon (loop variables, instructions of a loop body);
  • spatial locality: after one address the nearby ones are likely to be used (sequential instructions, consecutive array elements).

Example. In for (i = 0; i < n; i++) s += a[i]; the loop instructions and s show temporal locality, a[i] shows spatial locality. By bringing a block of several words into the cache, the first access to a[0] also brings a[1], a[2], a[3].

Hit, miss and average access time

Definition (hit, miss and hit rate)

  • Hit: the datum is in the upper level; miss: it is not, and must be fetched from the lower level.
  • Hit rate hh: fraction of accesses that are hits; miss rate 1−h1 - h.

Example. Out of 100100 accesses with 9595 hits, h=0,95h=0{,}95 and the miss rate is 1−h=0,051-h=0{,}05.

Formula (average access time with a cache). If a miss costs the cache access plus the memory access:

Tavg=h⋅T1+(1−h)(T1+T2)=T1+(1−h) T2T_{avg} = h \cdot T_1 + (1 - h)(T_1 + T_2) = T_1 + (1 - h)\,T_2

Example. Cache 1 ns, memory 100 ns, h=0,95h=0{,}95: Tavg=1+0,05⋅100=6T_{avg}=1+0{,}05\cdot100=6 ns; with h=0,99h=0{,}99 it becomes 22 ns. Without a cache it would be 100 ns: even an apparently "high" hit rate makes a big difference.

If instead the request goes in parallel to cache and memory and a miss costs only T2T_2: Tavg=h T1+(1−h) T2T_{avg} = h\,T_1 + (1-h)\,T_2.

Formula (average time with two cache levels). With two cache levels: Tavg=TL1+mL1 (TL2+mL2 Tmem)T_{avg} = T_{L1} + m_{L1}\,(T_{L2} + m_{L2}\,T_{mem}), where mm are the local miss rates.

Example. With TL1=1T_{L1}=1 ns, mL1=0,1m_{L1}=0{,}1, TL2=10T_{L2}=10 ns, mL2=0,2m_{L2}=0{,}2 and Tmem=100T_{mem}=100 ns: Tavg=1+0,1 (10+0,2⋅100)=4T_{avg}=1+0{,}1\,(10+0{,}2\cdot100)=4 ns.

The concrete implementations are in Cache MemoryBlocks, lines and tags; address decomposition; direct, fully associative and set-associative mapping with field calculation; replacement policies (LRU, FIFO, random); write policies (write-through, write-back with dirty bit, write-allocate); block size; multilevel and split caches; reducing misses; typical address-field questions.Cache Memory → (between CPU and main memory) and in Virtual MemoryVirtual and physical addresses, pages and frames; translation with the page table and field calculation; page fault and page replacement; TLB and effective access time; protection and sharing; comparison with the cache.Virtual Memory → (between main memory and disk). The technologies of the levels are in Semiconductor MemoryMemory cell and operations; dynamic RAM (capacitor, refresh) and static RAM (flip-flop) compared; ROM, PROM, EPROM, EEPROM and flash; organisation of chips and modules with calculation of address lines and number of chips; synchronous DRAM and DDR; errors and correcting codes.Semiconductor Memory → and External Memory - Disks, RAID and Optical StorageMagnetic disk (platters, surfaces, tracks, sectors, cylinders); access time as the sum of seek, rotational latency and transfer, with formulas and an example; scheduling algorithms FCFS, SSTF and elevator; RAID levels 0 to 6; SSD; CD, DVD and tapes.External Memory - Disks, RAID and Optical Storage →.

Typical mistakes

  • Using the wrong formula for TavgT_{avg}: read in the text whether the miss time includes the attempt in the cache or not.
  • Thinking locality holds for every program: random accesses to large data structures (for example huge hash tables) have little of it.

Versione ripasso

Hierarchy

Faster means costlier per bit and smaller. Levels: registers (< 1 ns), cache L1-L3 (1-20 ns), DRAM main memory (50-100 ns), SSD (25-100 microseconds), disk (5-10 ms). Going down: cheaper, larger, slower, accessed less often; data move in blocks. Access methods: sequential (tape), direct (disk), random (RAM), associative (cache).

Locality

Temporal: recently used data are reused soon. Spatial: neighbouring addresses are used next. In s += a[i], s is temporal and a[i] spatial; a block brings a[1]-a[3] with a[0].

Hit and miss

Hit rate hh, miss rate 1−h1-h. Tavg=T1+(1−h) T2T_{avg} = T_1 + (1-h)\,T_2 (miss includes the cache attempt). Cache 1 ns, memory 100 ns: h=0,95h = 0{,}95 gives 6 ns, h=0,99h = 0{,}99 gives 2 ns, no cache 100 ns. Parallel lookup: Tavg=h T1+(1−h) T2T_{avg} = h\,T_1 + (1-h)\,T_2. Two levels: Tavg=TL1+mL1(TL2+mL2Tmem)T_{avg} = T_{L1} + m_{L1}(T_{L2} + m_{L2}T_{mem}), e.g. 1+0,1(10+0,2⋅100)=41 + 0{,}1(10 + 0{,}2 \cdot 100) = 4 ns. See Cache MemoryBlocks, lines and tags; address decomposition; direct, fully associative and set-associative mapping with field calculation; replacement policies (LRU, FIFO, random); write policies (write-through, write-back with dirty bit, write-allocate); block size; multilevel and split caches; reducing misses; typical address-field questions.Cache Memory →, Virtual MemoryVirtual and physical addresses, pages and frames; translation with the page table and field calculation; page fault and page replacement; TLB and effective access time; protection and sharing; comparison with the cache.Virtual Memory →.

Typical mistakes: wrong TavgT_{avg} formula (check whether the miss time includes the cache); assuming locality for every program.

Teoria collegata