December 1, 2006©2006 Craig Zilles1 Threads and Cache Coherence in Hardware  Previously, we introduced multi-cores. —Today we’ll look at issues related.

Slides:



Advertisements
Similar presentations
Cache Coherence. Memory Consistency in SMPs Suppose CPU-1 updates A to 200. write-back: memory and cache-2 have stale values write-through: cache-2 has.
Advertisements

1 Lecture 20: Synchronization & Consistency Topics: synchronization, consistency models (Sections )
The University of Adelaide, School of Computer Science
CSE 502: Computer Architecture
Multi-core systems System Architecture COMP25212 Daniel Goodman Advanced Processor Technologies Group.
Cache Coherent Distributed Shared Memory. Motivations Small processor count –SMP machines –Single shared memory with multiple processors interconnected.
Slides 8d-1 Programming with Shared Memory Specifying parallelism Performance issues ITCS4145/5145, Parallel Programming B. Wilkinson Fall 2010.
1 Lecture 21: Transactional Memory Topics: consistency model recap, introduction to transactional memory.
CS 7810 Lecture 19 Coherence Decoupling: Making Use of Incoherence J.Huh, J. Chang, D. Burger, G. Sohi Proceedings of ASPLOS-XI October 2004.
CIS629 Coherence 1 Cache Coherence: Snooping Protocol, Directory Protocol Some of these slides courtesty of David Patterson and David Culler.
Computer Architecture 2011 – coherency & consistency (lec 7) 1 Computer Architecture Memory Coherency & Consistency By Dan Tsafrir, 11/4/2011 Presentation.
CS252/Patterson Lec /23/01 CS213 Parallel Processing Architecture Lecture 7: Multiprocessor Cache Coherency Problem.
1 Lecture 1: Introduction Course organization:  4 lectures on cache coherence and consistency  2 lectures on transactional memory  2 lectures on interconnection.
1 COMP 206: Computer Architecture and Implementation Montek Singh Mon, Nov 14, 2005 Topic: Cache Coherence.
CPE 731 Advanced Computer Architecture Snooping Cache Multiprocessors Dr. Gheith Abandah Adapted from the slides of Prof. David Patterson, University of.
1 Lecture 20: Protocols and Synchronization Topics: distributed shared-memory multiprocessors, synchronization (Sections )
Lecture 37: Chapter 7: Multiprocessors Today’s topic –Introduction to multiprocessors –Parallelism in software –Memory organization –Cache coherence 1.
1 Shared-memory Architectures Adapted from a lecture by Ian Watson, University of Machester.
Multiprocessor Cache Coherency
Memory Consistency Models Some material borrowed from Sarita Adve’s (UIUC) tutorial on memory consistency models.
Computer Architecture 2015 – Cache Coherency & Consistency 1 Computer Architecture Memory Coherency & Consistency By Yoav Etsion and Dan Tsafrir Presentation.
ECE200 – Computer Organization Chapter 9 – Multiprocessors.
Games Development 2 Concurrent Programming CO3301 Week 9.
Lecture 13: Multiprocessors Kai Bu
Ch4. Multiprocessors & Thread-Level Parallelism 2. SMP (Symmetric shared-memory Multiprocessors) ECE468/562 Advanced Computer Architecture Prof. Honggang.
Memory Consistency Models Alistair Rendell See “Shared Memory Consistency Models: A Tutorial”, S.V. Adve and K. Gharachorloo Chapter 8 pp of Wilkinson.
Caltech CS184 Spring DeHon 1 CS184b: Computer Architecture (Abstractions and Optimizations) Day 12: May 3, 2003 Shared Memory.
Cache Coherence Protocols 1 Cache Coherence Protocols in Shared Memory Multiprocessors Mehmet Şenvar.
Shared Memory Consistency Models. SMP systems support shared memory abstraction: all processors see the whole memory and can perform memory operations.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
1 Lecture 19: Scalable Protocols & Synch Topics: coherence protocols for distributed shared-memory multiprocessors and synchronization (Sections )
1 Lecture 3: Coherence Protocols Topics: consistency models, coherence protocol examples.
Distributed shared memory u motivation and the main idea u consistency models F strict and sequential F causal F PRAM and processor F weak and release.
ECE/CS 552: Shared Memory © Prof. Mikko Lipasti Lecture notes based in part on slides created by Mark Hill, David Wood, Guri Sohi, John Shen and Jim Smith.
Multiprocessor  Use large number of processor design for workstation or PC market  Has an efficient medium for communication among the processor memory.
CS267 Lecture 61 Shared Memory Hardware and Memory Consistency Modified from J. Demmel and K. Yelick
CS61C L20 Thread Level Parallelism II (1) Garcia, Spring 2013 © UCB Senior Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CMSC 611: Advanced Computer Architecture Shared Memory Most slides adapted from David Patterson. Some from Mohomed Younis.
The University of Adelaide, School of Computer Science
Lecture 13: Multiprocessors Kai Bu
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
COSC6385 Advanced Computer Architecture
Cache Coherence: Directory Protocol
Cache Coherence: Directory Protocol
CS 152 Computer Architecture and Engineering Lecture 18: Snoopy Caches
Atomic Operations in Hardware
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Atomic Operations in Hardware
Lecture 18: Coherence and Synchronization
The University of Adelaide, School of Computer Science
Example Cache Coherence Problem
The University of Adelaide, School of Computer Science
Lecture 2: Snooping-Based Coherence
Cache Coherence Protocols 15th April, 2006
Chapter 5 Exploiting Memory Hierarchy : Cache Memory in CMP
Lecture 25: Multiprocessors
Lecture 25: Multiprocessors
The University of Adelaide, School of Computer Science
Lecture 17 Multiprocessors and Thread-Level Parallelism
Lecture 24: Multiprocessors
Coherent caches Adapted from a lecture by Ian Watson, University of Machester.
Lecture 17 Multiprocessors and Thread-Level Parallelism
Programming with Shared Memory Specifying parallelism
Lecture 19: Coherence and Synchronization
Problems with Locks Andrew Whitaker CSE451.
Lecture 18: Coherence and Synchronization
The University of Adelaide, School of Computer Science
CSE 486/586 Distributed Systems Cache Coherence
Lecture 17 Multiprocessors and Thread-Level Parallelism
Presentation transcript:

December 1, 2006©2006 Craig Zilles1 Threads and Cache Coherence in Hardware  Previously, we introduced multi-cores. —Today we’ll look at issues related to multi-core memory systems: 1.Threads 2.Cache coherence Intel Core i7

Process View  Process = thread + code, data, and kernel context shared libraries run-time heap 0 read/write data Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Code, data, and kernel context read-only code/data stack SP PC brk Thread Kernel context: VM structures brk pointer Descriptor table

Process with Two Threads shared libraries run-time heap 0 read/write data Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) Code, data, and kernel context read-only code/data stack SP PC brk Thread 1 Kernel context: VM structures brk pointer Descriptor table Program context: Data registers Condition codes Stack pointer (SP) Program counter (PC) stack SP Thread 2

Thread Execution  Single Core Processor —Simulate concurrency by time slicing  Multi-Core Processor —Can have true concurrency Time Thread AThread BThread C Thread AThread BThread C Run 3 threads on 2 cores

(Hardware) Shared Memory  The programming model for multi-core CPUs What is it?  Memory system with a single global physical address space —So processors can communicate by reading and writing same memory  Design Goal 1: Minimize memory latency —Use co-location & caches  Design Goal 2: Maximize memory bandwidth —Use parallelism & caches December 1, 2006©2006 Craig Zilles5

Some example memory-system designs December 1, 2006©2006 Craig Zilles6

December 7, The Cache Coherence Problem I/O devices Memory P 1 $$ $ P 2 P 3 5 u = ? 4 u u :5 1 u 2 u 3 u = 7 1. LD u 2. LD u 5. LD u 4. LD u 3. ST u —Multiple copies of a block can easily get inconsistent Processor writes; I/O writes —Processors could see different values for u after event 3

Cache Coherence  According to Webster’s dictionary … —Cache: a secure place of storage —Coherent: logically consistent  Cache Coherence: keep storage logically consistent —Coherence requires enforcement of 2 properties 1. Write propagation —All writes eventually become visible to other processors 2.Write serialization —All processors see writes to same block in same order December 1, 2006©2006 Craig Zilles8

Cache Coherence Invariant  Each block of memory is in exactly one of these 3 states: 1.Uncached: Memory has the only copy 2.Writable: Exactly 1 cache has the block and only that processor can write to it. 3.Read-only: Any number of caches can hold the block, and their processors can read it. December 1, 2006©2006 Craig Zilles9 invariant | in ˈ ve(ə)rēənt | noun Mathematics a function, quantity, or property that remains unchanged when a specified transformation is applied.

Snoopy Cache Coherence Schemes  Bus is a broadcast medium & caches know what they have  Cache controller “snoops” all transactions on the shared bus —Relevant transaction if for a block it contains —Take action to ensure coherence Invalidate or supply value Depends on state of the block and the protocol December 7,

December 7, Maintain the invariant by tracking “state”  Every cache block has an associated state —This will supplant the valid and dirty bits  A cache controller updates the state of blocks in response to processor and snoop events and generates bus transactions  Snoopy protocol —set of states —state-transition diagram —actions Snoop State Tag Data ° ° ° Cache Controller Processor Ld/St

MSI protocol This is the simplest possible protocol, corresponding directly to the 3 options in our invariant  Invalid State: the data in the cache is not valid.  Shared State: multiple caches potentially have copies of this data; they will all have it in shared state. Memory has a copy that is consistent with the cached copy.  Dirty or Modified: only 1 cache has a copy. Memory has a copy that is inconsistent with the cached copy. Memory needs to be updated when the data is displaced from the cache or another processor wants to read the same data. 12/7/201512

Actions Processor Actions:  Load  Store  Eviction: processor wants to replace cache block Bus Actions:  GETS: request to get data in shared state  GETX: request for data in modified state (i.e., eXclusive access)  UPGRADE: request for exclusive access to data owned in shared state Cache Controller Actions:  Source Data: this cache provides the data to the requesting cache  Writeback: this cache updates the block in memory December 1, 2006©2006 Craig Zilles13

14 Modified SharedInvalid MSI Protocol December 7, 2015

15 The Cache Coherence Problem Solved I/O devices Memory P 1 $$ $ P 2 P 3 u :5 1 u :5 S 2 u 1. LD u 2. LD u 5. LD u 4. LD u 3. ST u GETS UPGRADE I M :7 GETS :7 u :7 S Source Data

Real Cache Coherence Protocols  Are more complex than MSI (see MESI and MEOSI)  Some modern chips don’t use buses (too slow) —Directory based: Alternate protocol doesn’t require snooping  But this gives you the basic idea. December 1, 2006©2006 Craig Zilles16

December 1, 2006©2006 Craig Zilles17 A simple piece of code unsigned counter1 = 0, counter2 = 0; void *do_stuff(void * arg) { for (int i = 0 ; i < ; ++ i) { counter1 ++; counter2 ++; } return arg; } How long does this program take? How can we make it faster?

December 1, 2006©2006 Craig Zilles18 unsigned counter1 = 0, counter2 = 0; void *do_stuff(void * arg) { for (int i = 0 ; i < ; ++ i) { counter1 ++; counter2 ++; } return arg; } Exploiting a multi-core processor #1#2 Split for-loop into two loops, one for each statement. Execute each loops on separate cores

December 1, 2006©2006 Craig Zilles19 Parallelized. unsigned counter1 = 0, counter2 = 0; void *do_stuff1(void * arg) { for (int i = 0 ; i < ; ++ i) { counter1 ++; } return arg; } void *do_stuff2(void * arg) { for (int i = 0 ; i < ; ++ i) { counter2 ++; } return arg; } How much faster?

December 1, 2006©2006 Craig Zilles20 What is going on?

Which is better?  If you wanted to parallelize work on an array for multi-core… —Would you rather distribute the work: 1.Round Robin? 2.Chunk-wise? December 1, 2006©2006 Craig Zilles21

But wait, there’s more! (Memory Consistency)  Cache coherence isn’t enough to define shared memory behavior —Coherence defines ordering of requests that access same bytes serializes all operations to that location such that, operations performed by any processor appear in program order  Program order = order defined by assembly code A read gets the value written by last store to that location  Memory Consistency —Defines allowed orderings for reads/writes to different locations —Your intuition is wrong. —One of the most difficult topics in CS. —I’m going to try to make you aware of the issues. December 1, 2006©2006 Craig Zilles22

What is the final value of A & B? /* initially, A = B = flag = 0 */ P1 P2 A = 1; while (flag == 0); /* spin */ B = 1;print A; flag = 1; print B; December 1, 2006©2006 Craig Zilles23

Sequential Consistency  Leslie Lamport 1979:  “A multiprocessor is sequentially consistent if the result of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in this sequence in the order specified by its program order specified by its program”  Is not what you get on almost any computer. Why? December 1, 2006©2006 Craig Zilles24

Weak Consistency Models  Done for performance; fewer constraints  Many such models: e.g., Total Store Ordering, Release Consistency —Not necessary for most programmers to know specifics  Key idea: —Fences: allow programmers to specify ordering constraints Hardware instructions E.g., All memory operations before fence must complete before any after the fence. /* initially, A = B = flag = 0 */ P1 P2 A = 1; while (flag == 0); /* spin */ B = 1;fence; fence;print A; flag = 1; print B; December 1, 2006©2006 Craig Zilles25

Fences in software practice  Most programmers don’t need to write their own fences:  Standard lock libraries include the necessary fences: —If you only touch shared data within locked critical sections, the necessary fences will be present.  If you want to define your own synchronization variables (wait flags) —Some languages include keywords that introduce necessary fences Java 5’s “volatile” keyword  Big takeaway: —Be very careful building your own synchronization. December 1, 2006©2006 Craig Zilles26

December 1, 2006©2006 Craig Zilles27 Conclusions  CPU-based multi-cores use (hardware) shared memory —All threads can read/write same physical memory locations —Per processor caches for low latency, high bandwidth  Cache coherence: —All writes to a location seen in same order by all processors —“Single writer or multiple readers” invariant —Modified, Shared, Invalid (MSI) protocol  False sharing: —Cache coherence at the granularity of cache blocks —Minimize intersection of processors data working set Although read-only sharing is okay  Memory Consistency: —Your intuition is wrong! Beware creating your own synchronization!