CS 401 Greedy Algorithms / Optimal Caching Xiaorui Sun 1
Recap and Outline • Homework 1 due today, Homework 2 will be out later today • Greedy algorithm: ‘Best’ current partial solution at each step • Greedy Analysis Strategies Greedy algorithm stays ahead: Interval Scheduling Structural: Interval Partition Exchange argument: Minimize Lateness • Today: Optimal Caching Complex exchange argument 2
Optimal Caching
Optimal Caching/Paging Memory systems • Many levels of storage with different access times • Smaller storage has shorter access time • To access an item it must be brought to the lowest level of the memory system Type Latency Capacity Registers 0.25 ns 36 KB L1 Cache 1 ns 192 KB L2 Cache 3 ns 1.5 MB L3 Cache 14 ns 15 MB DRAM (DDR4) 66 ns 32 GB SDD 0.15 ms 480 GB Internet 7 ms 2 GB left in Dropbox L 4 My computer at home (2020)
Optimal Caching/Paging Memory systems • Many levels of storage with different access times • Smaller storage has shorter access time • To access an item it must be brought to the lowest level of the memory system Consider the problem between 2 levels • Main memory with ! data items • Cache can hold " < ! items • Assume no restrictions about where items can be • Suppose cache is full initially Ø Holds " data items to start with 5
Optimal Offline Caching Caching • Cache with capacity to store ! items. Sequence of " item requests # $ , # & , ⋯ , # " . • • Cache hit: item already in cache when requested. • Cache miss: item not already in cache when requested: must bring requested item into cache, and evict some existing item, if full. a a b Goal b a b • Eviction schedule that minimizes number c c b of evictions. b c b c c b Example: ! = & , initial cache = ), * , a a b requests: ), *, +, *, +, ), ), * . a a b b a b requests cache Optimal eviction schedule: & evictions. 6
Optimal Offline Caching: Farthest-In-Future Which item we should evict? Farthest-in-future • Evict item in the cache that is not requested until farthest in the future. current cache: a b c d e f future queries: g a b c e d a b b a c d e a f a d e f g h ... eject this one cache miss Theorem • [Bellady, 1960s] FIF is an optimal eviction schedule. 7
Quiz Which item will be evicted next using farthest-in-future schedule? Answer: e d x y a b d b y a c d b c a e d e c a f ? ? ? ? c d a e a requests cache 8
Optimal Offline Caching: Farthest-In-Future Which item we should evict? Farthest-in-future • Evict item in the cache that is not requested until farthest in the future. current cache: a b c d e f future queries: g a b c e d a b b a c d e a f a d e f g h ... eject this one cache miss Theorem • [Bellady, 1960s] FIF is an optimal eviction schedule. Exchange Argument • We can swap choices to convert other schedules to Farthest-In-Future without losing quality 9
Reduced Eviction Schedules Definition • A reduced schedule is a schedule that only inserts an item into the cache in a step in which that item is requested. Intuition • Can transform an unreduced schedule into a reduced one with no more evictions. a a b c a a b c a a x c a a b c c a d c c a b c d a d b d a d c a a c b a a d c b a x b b a d b c a c b c a c b a a b c a a c b a a b c a a c b a reduced schedule an unreduced schedule 6
Reduced Eviction Schedules Claim Given any unreduced schedule ! , can transform it into a reduced • schedule !′ with no more evictions. Proof (by induction on number of unreduced items) • Suppose ! brings # into the cache at time $ , without a request. Let % be the item ! evicts when it brings # into the cache. • Case 1: # evicted at time $′ , before next request for # . Case 2: # requested at time $′ before # is evicted. ▪ S S' S S' c c c c c c t t t t d d d t' t' t' t' d d e e d requested at time t' d evicted at time t', 11 before next request
Farthest-In-Future: Analysis Theorem • FIF is optimal eviction algorithm. Proof. (by induction on number of requests ! ) Invariant: There exists an optimal reduced schedule " that makes the same eviction schedule as " ,-, through the first # + & requests. Let " be reduced schedule that satisfies invariant through # requests. We produce "′ that satisfies invariant after # + & requests. Consider (# + &) st request ) = ) #+& . • Since " and " ,-, have agreed up until now, they have the same cache • contents before request # + & . Case 1: ( ) is already in the cache). "′ = " satisfies invariant. (used " is reduced here) Case 2: ( ) is not in the cache and " and " ,-, evict the same element). "′ = " satisfies invariant. 12
Farthest-In-Future: Analysis Proof. (continued) Case 3: ( ! is not in the cache; " #$# evicts % ; " evicts & ≠ % ). • begin construction of "′ from " by evicting % instead of & evicted by " evicted by " #$# same e f same e f j S S' same e d same d f j j+1 S S' • now "′ agrees with " #$# on first ) + + requests; we show that having element & in cache is no worse than having element % – Continue building "’ to be the same as " until forced to be different 13
Farthest-In-Future: Analysis Proof. (continued) Let !′ be the first time after ! + $ that % and %′ must take a different action, and let & be item requested at time !′ . Action of S must involve e or f (or both) j' e f same same S S' Case 3a: & = ( . Can't happen: ( was evicted by Farthest-In-Future so there must be a request for ) before ( . Case 3b: & = ) . Element ) can't be in cache of % , so let (′ be the element that % evicts. • if (′ = ( , %′ accesses ) from cache; now % and %′ have same cache • if ( * ≠ ( , %′ evicts (′ and brings ( into the cache; now % and %′ have the same cache Note: %′ is no longer reduced, but can be transformed into a reduced schedule that agrees with % ,-, through step ! + $ 14
Farthest-In-Future: Analysis Proof. (continued) Let !′ be the first time after ! + $ that % and %′ must take a different action, and let & be item requested at time !′ . Action of S must involve e or f (or both) j' e f same same S S' Case 3c: & ≠ ( and & ≠ ) . % must evict ( . Make %′ evict ) ; now % and %′ have the same cache. ▪ otherwise %′ would take the same action g g j' same same S S' In each case can now extend %’ using rest of % at no extra cost. %’ is optimal, reduced, and agrees with % +,+ for ! + $ steps 15 Optimality of % +,+ follows by induction.
Online Caching • Online vs. offline algorithms. Offline: full sequence of requests is known a priori. Online (reality): requests are not known in advance. Caching is among most fundamental online problems in CS. • LIFO. Evict item brought in most recently. • LRU. Evict item whose most recent access was earliest. FIF with direction of time reversed! • Theorem. FIF is optimal offline eviction algorithm. Provides basis for understanding and analyzing online algorithms. LRU is k-competitive. [Section 13.8] LIFO is arbitrarily bad. 16
Recommend
More recommend