CS 401 Greedy Algorithms / Optimal Caching Xiaorui Sun 1 Recap - - PowerPoint PPT Presentation

cs 401
SMART_READER_LITE
LIVE PREVIEW

CS 401 Greedy Algorithms / Optimal Caching Xiaorui Sun 1 Recap - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

CS 401

Greedy Algorithms / Optimal Caching

Xiaorui Sun

1

slide-2
SLIDE 2

2

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

slide-3
SLIDE 3

Optimal Caching

slide-4
SLIDE 4

4

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

My computer at home (2020)

slide-5
SLIDE 5

5

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

slide-6
SLIDE 6

Optimal Offline Caching

6

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

  • Eviction schedule that minimizes number
  • f evictions.

Example: ! = &, initial cache = ), *, requests: ), *, +, *, +, ), ), *. Optimal eviction schedule: & evictions.

a b a b c b c b c b a b b c b c a a b a a b b cache requests a

slide-7
SLIDE 7

Optimal Offline Caching: Farthest-In-Future

7

Farthest-in-future

  • Evict item in the cache that is not requested until farthest

in the future. Theorem

  • [Bellady, 1960s] FIF is an optimal eviction schedule.

g a b c e d a b b a c d e a f a d e f g h ...

a b current cache: c d e f future queries:

cache miss eject this one

Which item we should evict?

slide-8
SLIDE 8

Quiz

8

Which item will be evicted next using farthest-in-future schedule? Answer: e

d b c d e c ? ? ? c e f c d a e a d b y b a a ? a cache requests d x y a

slide-9
SLIDE 9

Optimal Offline Caching: Farthest-In-Future

9

Farthest-in-future

  • Evict item in the cache that is not requested until farthest

in the future. 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

g a b c e d a b b a c d e a f a d e f g h ...

a b current cache: c d e f future queries:

cache miss eject this one

Which item we should evict?

slide-10
SLIDE 10

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 x an unreduced schedule c a d c a d b a c b a x b a c b a b c a b c a c d a b c a a a b a reduced schedule c a b c a d c a d c a d b a c b a c b a c b a c d a b c a a a b c a a b c a 6

slide-11
SLIDE 11

Reduced Eviction Schedules

11

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

t t' d c t t' c

S'

d

S

d requested at time t'

t t' d c t t' c

S'

e

S

d evicted at time t', before next request

e d c c d

slide-12
SLIDE 12

Farthest-In-Future: Analysis

12

Theorem

  • FIF is optimal eviction algorithm.
  • Proof. (by induction on number of 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. Invariant: There exists an optimal reduced schedule " that makes the same eviction schedule as ",-, through the first # + & requests.

slide-13
SLIDE 13

Farthest-In-Future: Analysis

13

  • Proof. (continued)

Case 3: (! is not in the cache; "#$# evicts %; " evicts & ≠ %).

  • begin construction of "′ from " by evicting % instead of &
  • 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 j same f same f e e S S' j same d same f d e S S' j+1 evicted by "#$# evicted by "

slide-14
SLIDE 14

Farthest-In-Future: Analysis

14

  • Proof. (continued)

Let !′ be the first time after ! + $ that % and %′ must take a different action, and let & be item requested at time !′.

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 ! + $

same

e

same

f S S' j'

Action of S must involve e or f (or both)

slide-15
SLIDE 15

Farthest-In-Future: Analysis

15

  • Proof. (continued)

Let !′ be the first time after ! + $ that % and %′ must take a different action, and let & be item requested at time !′.

Case 3c: & ≠ ( and & ≠ ). % must evict (. Make %′ evict ); now % and %′ have the same cache. ▪

same

e

same

f S S' j'

Action of S must involve e or f (or both)

In each case can now extend %’ using rest of % at no extra cost. %’ is optimal, reduced, and agrees with %+,+ for ! + $ steps Optimality of %+,+ follows by induction.

same

g

same

g S S' j'

  • therwise %′ would take the same action
slide-16
SLIDE 16

Online Caching

16

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

FIF with direction of time reversed!