Docsity

Cache Coherence: Ensuring Consistency in Multilevel Caches, Study notes of Computer Science

Toggle sidebar
bg1
Page 1
28
Cache Coherence
Multilevel caches included with each processor
Private and shared data
Cache Coherence problem
Event P1’s Cache P2’s Cache Memory
1
1: LD r1,[A] 1 1
2: LD r1,[A] 1 1 1
1: ADD r1,1,r1 1 1 1
1: ST r1,[A] 212
29
Cache Coherence
Coherent if:
1: Write by processor P to X
Read by processor P of X
No intervening write
Returns most recent value
2: Write by processor P1
Read by processor P2
Returns most recent value
if operations separated by enough time
3: Writes to same location are serialized
I.e., writes seen by all processors in the
same order
Preserves program
order, true even of
uniprocessors
Notion of coherency- get
the most recent value
Ensures a value is not
held indefinitely (if seen
in different order)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Cache Coherence: Ensuring Consistency in Multilevel Caches and more Study notes Computer Science in PDF only on Docsity!

28

Cache Coherence

Multilevel caches included with each processor

Private and shared data

Cache Coherence problem

Event P1’s Cache P2’s Cache Memory

1: LD r1,[A] 1 1

2: LD r1,[A] 1 1 1

1: ADD r1,1,r1 1 1 1

1: ST r1,[A] 2 1 2

29

Cache Coherence

Coherent if:

1: Write by processor P to X

Read by processor P of X

No intervening write

Returns most recent value

2: Write by processor P

Read by processor P

Returns most recent value

if operations separated by enough time

3: Writes to same location are serialized

I.e., writes seen by all processors in the

same order

Preserves program

order, true even of

uniprocessors

Notion of coherency- get

the most recent value

Ensures a value is not

held indefinitely (if seen

in different order)

30

Coherence Mechanisms

• Migration

  • Data moved to a local cache where it can be accessed locally
  • Reduces latency to shared data that is allocated remotely

• Replication

  • Copies of shared data that can be read by multiple processors
  • Reduces latency and contention for shared item

• Snooping - State of blocks kept at local caches

by watching bus transactions

31

Coherence Protocols

• Write invalidate

  • Only one processor has exclusive write access
  • No other readable/writable copies of to-be-written data exist
  • On write, invalidate all copies
  • Modify data, when other processors use it, they miss and get

the new data

• Write broadcast

  • On a write, broadcast the updated value to all caches holding

a copy of the data

  • Bandwidth requirements - keep track of whether a word is

shared or not so unnecessary broadcasts are avoided

34

Example: Invalidate

Cache Miss 0 0 for X

CPU A Reads X

Cache Miss 0 0 0 for X

CPU B Reads X

Memory Contents for location X

Cache Contents for CPU B

Cache Contents for Bus Activity CPU A

Processor Activity

35

Example: Invalidate

Cache Miss 0 0 for X

CPU A Reads X

CPU A writes 1 1 0 0 to X

Cache Miss 0 0 0 for X

CPU B Reads X

Memory Contents for location X

Cache Contents for CPU B

Cache Contents for Bus Activity CPU A

Processor Activity

36

Example: Invalidate

Cache Miss 0 0 for X

CPU A Reads X

Invalidation 1 0 0 for X

CPU A writes 1 to X

Cache Miss 0 0 0 for X

CPU B Reads X

Memory Contents for location X

Cache Contents for CPU B

Cache Contents for Bus Activity CPU A

Processor Activity

37

Example: Invalidate

Cache Miss 0 0 for X

CPU A Reads X

Cache Miss 1 1 1 for X

CPU B Reads X

Invalidation 1 0 for X

CPU A writes 1 to X

Cache Miss 0 0 0 for X

CPU B Reads X

Memory Contents for location X

Cache Contents for CPU B

Cache Contents for Bus Activity CPU A

Processor Activity

40

Example: Write Update

Cache Miss 0 0 for X

CPU A Reads X

CPU B Reads 1 1 1 X

Write update for X

CPU A writes 1 to X

Cache Miss 0 0 0 for X

CPU B Reads X

Memory Contents for location X

Cache Contents for CPU B

Cache Contents for Bus Activity CPU A

Processor Activity

41

Snooping Protocols

Cache tag and data

Processor

Single bus

Memory I/O

Snoop tag

Cache tag and data

Processor

Snoop tag

Cache tag and data

Processor

Snoop tag

  • Each processor monitors the activity on the bus
  • Dealing with write through is simpler than dealing with write back
  • In WB, on a read miss, all caches check to see if they have a copy of the

requested block. If yes, they supply the data (will see how).

  • In WB, on a write miss, all caches check to see if they have a copy of the

requested data. Yes: invalidate the local copy or update it with the new value.

Keeps tags to avoid

interference with the

CPU

42

Snoopy Protocol

• Invalidation protocol

• Each block of memory is in one state:

  • Clean in all caches and up-to-date in memory (Read-Only),
  • Dirty in exactly one cache (Read/Write), OR
  • Not in any caches

• Each cache block is in one state:

  • Shared : block can be read (clean, read-only)
  • Exclusive : cache has only copy, its writeable, and dirty
  • Invalid : block contains no data

• Read misses: cause all caches to snoop bus

• Writes to clean (shared) blocks are treated as

misses -- invalidates all other caches

43

Snoopy Protocol

• Bus based systems

  • Acquire bus to do invalidation - write not complete until

invalidated (and have the bus)

  • Causes serialization of writes

• Finite state machine to maintain coherence

  • Status bits for each cache line (protocol state)
  • 2 parts to FSM

» CPU activity on one processor (CPU events)

» other processors see activity (bus events)

• Only one processor has write access at a time

• All processors can have read access together

46

Example

B = invalid B = invalid

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

47

Example

B = invalid B = invalid

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

48

Example

B = invalid

A1 = 10 (exclusive)

B = invalid

B = invalid

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

49

Example

B = invalid

A1 = 10 (exclusive)

B = invalid

B = invalid

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

P1 reads A

52

Example

B = invalid

A1 = 10 (exclusive)

A1 = 10 (exclusive)

A1 = 10 (shared)

B = invalid

B = invalid

B = invalid

A1 = 10 (shared)

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

P1 reads A1 ( RH )

P2 reads A1 ( RM )

53

Example

B = invalid

A1 = 10 (exclusive)

A1 = 10 (exclusive)

A1 = 10 (shared)

B = invalid

B = invalid

B = invalid

A1 = 10 (shared)

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

P1 reads A1 ( RH )

P2 reads A1 ( RM )

P2 write 20 to A

54

Example

B = invalid

A1 = 10 (exclusive)

A1 = 10 (exclusive)

A1 = 10 (shared)

B = invalid

B = invalid

B = invalid

B = invalid

A1 = 10 (shared)

A1 = 20 (exclusive)

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

P1 reads A1 ( RH )

P2 reads A1 ( RM )

P2 write 20 to A1 ( WH )

55

Example

B = invalid

A1 = 10 (exclusive)

A1 = 10 (exclusive)

A1 = 10 (shared)

B = invalid

B = invalid

B = invalid

B = invalid

A1 = 10 (shared)

A1 = 20 (exclusive)

Event In P1’s cache In P2’s cache

Assume: A1 and A2 map to same cache block B, initial cache state is invalid

P1 writes 10 to A

P1 reads A1 ( RH )

P2 reads A1 ( RM )

P2 write 20 to A1 ( WH )

P2 writes 40 to A

58

Synchronization

• Communication points (when sharing data)

• Protect shared variables when >1 processor may

update them

• Use locks around critical regions

while locked do {};

acquire lock {

...do something…

release lock

59

Supporting Locks

• We need:

  • an atomic (non-interruptable) operation
  • test-and-set

• For some lock variable “lock”, we use:

while (test_and_set(lock)) {};

...do something…

release_lock(lock)

60

Test and Set

test_and_set(var lock) {

value = *lock;

*lock = 1;

return value;

The write of 1 to the value is OK, because it will already be set to 1

Example of non-atomic operation

B: value = *lock (1)

A: *lock = 0 (releases the lock)

B: *lock = 1 (sets lock)

now, B returns 1 saying it’s still locked but A has released the lock.

The value set by A was overwritten by B and lock is never released

All done atomically

61

Implementing Test and Set

Special atomic instruction: swap(a,b)

r1 = 1;

swap [r3+r2],r

r1 now has value of MEM[r3+r2]

MEM[r3+r2] has value of r1 (1)

• For bus-based systems, bus is held during the

entire time to read and write memory for swap

• Not particularly good for cache based systems -

want to take advantage of cache and avoid using

the bus

The test_and_set operation