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 : fraction of accesses that are hits; miss rate .
Example. Out of accesses with hits, and the miss rate is .
Formula (average access time with a cache). If a miss costs the cache access plus the memory access:
Example. Cache 1 ns, memory 100 ns, : ns; with it becomes 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 : .
Formula (average time with two cache levels). With two cache levels: , where are the local miss rates.
Example. With ns, , ns, and ns: 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 : 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 , miss rate . (miss includes the cache attempt). Cache 1 ns, memory 100 ns: gives 6 ns, gives 2 ns, no cache 100 ns. Parallel lookup: . Two levels: , e.g. 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 formula (check whether the miss time includes the cache); assuming locality for every program.