computer science 246 computer architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – number...

27
Computer Science 246 Computer Architecture S i 2009 Spring 2009 Harvard University Instructor: Prof. David Brooks [email protected] Memory Hierarchy and Caches (Part 2) Computer Science 246 David Brooks

Upload: hangoc

Post on 19-Mar-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Computer Science 246Computer Architecture

S i 2009Spring 2009Harvard University

Instructor: Prof. David [email protected]

Memory Hierarchy and Caches (Part 2)

Computer Science 246David Brooks

Page 2: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

CachesCaches

• Monday lecture• Monday lecture– Review of cache basics, direct-mapped, set-associative

caches

• Today– More on cache performance, write strategies

Computer Science 246David Brooks

Page 3: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Summary of Set AssociativitySummary of Set Associativity• Direct Mappedpp

– One place in cache, One Comparator, No Muxes• Set Associative Caches

– Restricted set of places– N-way set associativity

Number of comparators = number of blocks per set– Number of comparators = number of blocks per set– N:1 mux

• Fully AssociativeFully Associative– Anywhere in cache– Number of comparators = number of blocks in cache

Computer Science 246David Brooks

– N:1 mux needed

Page 4: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

More Detailed QuestionsMore Detailed Questions

• Block placement policy?• Block placement policy?– Where does a block go when it is fetched?

• Block identification policy?• Block identification policy?– How do we find a block in the cache?

• Block replacement policy?Block replacement policy?– When fetching a block into a full cache, how do we

decide what other block gets kicked out?

• Write strategy?– Does any of this differ for reads vs. writes?

Computer Science 246David Brooks

Page 5: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Block Placement + IDBlock Placement + ID

• Placement• Placement– Invariant: block always goes in exactly one set– Fully-Associative: Cache is one set, block goesFully Associative: Cache is one set, block goes

anywhere– Direct-Mapped: Block goes in exactly one frame– Set-Associative: Block goes in one of a few frames

• Identification– Find Set– Search ways in parallel (compare tags, check valid bits)

Computer Science 246David Brooks

Page 6: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Block ReplacementBlock Replacement

• Cache miss requires a replacementCache miss requires a replacement• No decision needed in direct mapped cache• More than one place for memory blocks in set-More than one place for memory blocks in set

associative• Replacement Strategies

– Optimal• Replace Block used furthest ahead in time (oracle)

– Least Recently Used (LRU)– Least Recently Used (LRU)• Optimized for temporal locality

– (Pseudo) Random

Computer Science 246David Brooks

• Nearly as good as LRU, simpler

Page 7: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write PoliciesWrite Policies

• Writes are only about 21% of data cache traffic• Writes are only about 21% of data cache traffic• Optimize cache for reads, do writes “on the side”

Reads can do tag check/data read in parallel– Reads can do tag check/data read in parallel– Writes must be sure we are updating the correct data

and the correct amount of data (1-8 byte writes)– Serial process => slow

• What to do on a write hit?• What to do on a write miss?

Computer Science 246David Brooks

Page 8: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write Hit PoliciesWrite Hit Policies• Q1: When to propagate new values to memory?• Write back – Information is only written to the cache.

– Next lower level only updated when it is evicted (dirty bits h d t h b difi d)say when data has been modified)

– Can write at speed of cache – Caches become temporarily inconsistent with lower-levels of p y

hierarchy. – Uses less memory bandwidth/power (multiple consecutive

writes may require only 1 final write)writes may require only 1 final write)– Multiple writes within a block can be merged into one write– Evictions are longer latency now (must write back)

Computer Science 246David Brooks

Page 9: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write Hit PoliciesWrite Hit Policies

• Q1: When to propagate new values to memory?• Q1: When to propagate new values to memory?• Write through – Information is written to cache

and to the lower-level memoryand to the lower-level memory– Main memory is always “consistent/coherent”– Easier to implement – no dirty bitsp y– Reads never result in writes to lower levels (cheaper)– Higher bandwidth needed– Write buffers used to avoid write stalls

Computer Science 246David Brooks

Page 10: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write buffersWrite buffers

CPU • Small chunks of memory toCPU • Small chunks of memory to buffer outgoing writes

• Processor can continueCache Write Buffer

Processor can continue when data written to buffer

• Allows overlap of

Lower Levels of Memory

Allows overlap of processor execution with memory update

• Write buffers are essential for write-through caches

Lower Levels of Memory

Computer Science 246David Brooks

• Write buffers are essential for write-through caches

Page 11: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write buffersWrite buffers• Writes can now be pipelined (rather than serial)Writes can now be pipelined (rather than serial)

• Check tag + Write store data into Write Buffer• Write data from Write buffer to L2 cache (tags ok)• Write data from Write buffer to L2 cache (tags ok)

Address| Data• Loads must check write buffer for

pending stores to same addressStore Op

Address| Data

pending stores to same address• Loads Check:

• Write BufferWrite Buffer

EntryWrite Buffer• Cache• Subsequent Levels of Memory Tag Data

Entry

Computer Science 246David Brooks

• Subsequent Levels of Memory g

Data Cache

Page 12: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write buffer policies:Performance/Complexity Tradeoffs

Stores L2 Cache

Loads

• Allow merging of multiple stores? (“coalescing”)• “Flush Policy” How to do flushing of entries?• Flush Policy – How to do flushing of entries?• “Load Servicing Policy” – What happens when a

load occurs to data currently in write buffer?Computer Science 246

David Brooks

load occurs to data currently in write buffer?

Page 13: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Write misses?Write misses?• Write Allocate

– Block is allocated on a write miss– Standard write hit actions follow the block allocation– Write misses = Read Misses– Goes well with write-back

• No-write Allocate– Write misses do not allocate a block

O l d t l l l– Only update lower-level memory– Blocks only allocate on Read misses!– Goes well with write-through

Computer Science 246David Brooks

– Goes well with write-through

Page 14: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Summary of Write PoliciesSummary of Write Policies

Write Policy Hit/Miss Writes to

WriteBack/Allocate Both L1 Cache

WriteBack/NoAllocate Hit L1 Cache

WriteBack/NoAllocate Miss L2 CacheWriteBack/NoAllocate Miss L2 Cache

WriteThrough/Allocate Both Both

WriteThrough/NoAllocate Hit Both

WriteThrough/NoAllocate Miss L2 Cache

Computer Science 246David Brooks

Page 15: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Cache PerformanceCache Performance

CPU time = (CPU execution cycles + Memory StallCPU time = (CPU execution cycles + Memory Stall Cycles)*Clock Cycle Time

AMAT = Hit Time + Miss Rate * Miss Penalty

• Reducing these three parameters can have a big impact on performanceimpact on performance

• Out-of-order processors can hide some of the miss penalty

Computer Science 246David Brooks

penalty

Page 16: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Miss PenaltyReducing Miss Penalty

• Have already seen two examples of techniques to• Have already seen two examples of techniques to reduce miss penalty– Write buffers give priority to read misses over writesWrite buffers give priority to read misses over writes– Merging write buffers

• Multiword writes are faster than many single word writes

• Now we consider several more– Victim Caches– Critical Word First/Early Restart– Multilevel caches

Computer Science 246David Brooks

Page 17: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Miss Penalty:Victim Caches

• Direct mapped caches => many conflict misses• Direct mapped caches => many conflict misses• Solution 1: More associativity (expensive)• Solution 2: Victim Cache• Solution 2: Victim Cache• Victim Cache

Small (4 to 8 entry) fully associative cache between L1– Small (4 to 8-entry), fully-associative cache between L1 cache and refill path

– Holds blocks discarded from cache because of evictions– Checked on a miss before going to L2 cache– Hit in victim cache => swap victim block with cache block

Computer Science 246David Brooks

Page 18: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Miss Penalty:Victim Caches

• Even one entry helps some benchmarks!

• Helps more for smaller caches, larger block sizes

Computer Science 246David Brooks

sizes

Page 19: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Miss Penalty:Critical Word First/Early Restart

• CPU normally just needs one word at a timeCPU normally just needs one word at a time• Large cache blocks have long transfer times• Don’t wait for the full block to be loaded beforeDon t wait for the full block to be loaded before

sending requested data word to the CPU• Critical Word First

– Request the missed word first from memory and send it to the CPU and continue execution

• Early Restart• Early Restart– Fetch in order, but as soon as the requested block

arrives send it to the CPU and continue execution

Computer Science 246David Brooks

Page 20: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Review: Improving Cache Performance

• How to improve cache performance?• How to improve cache performance?– Reducing Cache Miss Penalty– Reducing Miss RateReducing Miss Rate– Reducing Miss Penalty/Rate via parallelism– Reducing Hit Time

Computer Science 246David Brooks

Page 21: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Non-blocking Caches to reduce stalls on misses

• Non-blocking cache or lockup-free cache allow data cache toNon blocking cache or lockup free cache allow data cache to continue to supply cache hits during a miss– requires out-of-order execution– requires multi-bank memoriesrequires multi-bank memories

• “hit under miss” reduces the effective miss penalty by working during miss vs. ignoring CPU requests“h d l l ” “ d ” f h• “hit under multiple miss” or “miss under miss” may further lower the effective miss penalty by overlapping multiple misses– Significantly increases the complexity of the cache controller as there

can be multiple outstanding memory accesses– Requires multiple memory banks (otherwise cannot support)

Computer Science 246David Brooks

– Penium Pro allows 4 outstanding memory misses

Page 22: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Value of Hit Under Miss for SPECP t M St ll Ti f Bl ki C hPercentage Memory Stall Time of a Blocking Cache

• FP programs on average: AMAT= 0.68 -> 0.52 -> 0.34 -> 0.26p g g• Int programs on average: AMAT= 0.24 -> 0.20 -> 0.19 -> 0.19• 8 KB Data Cache, Direct Mapped, 32B block, 16 cycle miss

Page 23: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Misses by HardwarePrefetching of Instructions & Data

• Instruction Prefetching– Alpha 21064 fetches 2 blocks on a miss– Extra block placed in “stream buffer” not the cache– On Access: check both cache and stream buffer– On SB Hit: move line into cache– On SB Miss: Clear and refill SB with successive lines

• Works with data blocks too:– Jouppi [1990] 1 data stream buffer got 25% misses from 4KB cache; 4

streams got 43%– Palacharla & Kessler [1994] for scientific programs for 8 streams got [ ] p g g

50% to 70% of misses from 2 64KB, 4-way set associative caches

• Prefetching relies on having extra memory bandwidth that can

Computer Science 246David Brooks

be used without penalty

Page 24: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Hardware PrefetchingHardware Prefetching• What to prefetch?p

– One block ahead (spatially)• What will this work well for?

– Address prediction for non-sequential data• Correlated predictors (store miss, next_miss pairs in table)• Jump-pointers (augment data structures with prefetch pointers)p p ( g p p )

• When to prefetch?– On every referencey– On a miss (basically doubles block size!)– When resident data becomes “dead” -- how do we know?

Computer Science 246David Brooks

• No one will use it anymore, so it can be kicked out

Page 25: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Misses by S f P f hi DSoftware Prefetching Data

• Data PrefetchData Prefetch– Load data into register (HP PA-RISC loads)– Cache Prefetch: load into cache (MIPS IV, PowerPC, SPARC v. 9)– Special prefetching instructions cannot cause faults; a form of speculative– Special prefetching instructions cannot cause faults; a form of speculative

execution• Prefetching comes in two flavors:

Binding prefetch: Requests load directly into register– Binding prefetch: Requests load directly into register.• Must be correct address and register!

– Non-Binding prefetch: Load into cache. • Can be incorrect Faults?• Can be incorrect. Faults?

• Issuing Prefetch Instructions takes time– Is cost of prefetch issues < savings in reduced misses?

Hi h l d diffi lt f i b d idth

Computer Science 246David Brooks

– Higher superscalar reduces difficulty of issue bandwidth

Page 26: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Reducing Hit TimesReducing Hit Times• Some common techniques/trendsq

– Small and simple caches• Pentium III – 16KB L1

i 4 8 1• Pentium 4 – 8KB L1

– Pipelined Caches (actually bandwidth increase)• Pentium – 1 clock cycle I-Cachey• Pentium III – 2 clock cycle I-Cache• Pentium 4 – 4 clock cycle I-Cache

T C h– Trace Caches• Beyond spatial locality• Dynamic sequences of instruction (including taken branches)

Computer Science 246David Brooks

Page 27: Computer Science 246 Computer Architecturedbrooks/cs246/cs246-lecture-cache-part2.pdf · – Number of comparators = number of blocks in cache Computer Science 246 David Brooks –

Cache BandwidthCache Bandwidth

• Superscalars need multiple memory access per cycleSuperscalars need multiple memory access per cycle • Parallel cache access: more difficult than parallel ALUs

– Caches have state so multiple accesses will affect each otherp• “True Multiporting”

– Multiple decoders, read/write wordlines per SRAM cell– Pipeline a single port by “double pumping” Alpha 21264– Multiple cache copies (like clustered register file) POWER4

• Interleaved Multiporting• Interleaved Multiporting– Cache divides into banks – two accesses to same bank =>

conflict

Computer Science 246David Brooks