Aleksandar Milenkovic, Electrical and Computer Engineering University of Alabama in Huntsville. Review: Small-Scale Shared Memory

Similar documents
CPE 631 Lecture 21: Multiprocessors

Review: Multiprocessor. CPE 631 Session 21: Multiprocessors (Part 2) Potential HW Coherency Solutions. Bus Snooping Topology

Page 1. Lecture 12: Multiprocessor 2: Snooping Protocol, Directory Protocol, Synchronization, Consistency. Bus Snooping Topology

Aleksandar Milenkovic, Electrical and Computer Engineering University of Alabama in Huntsville

Aleksandar Milenkovich 1

Computer Architecture. A Quantitative Approach, Fifth Edition. Chapter 5. Multiprocessors and Thread-Level Parallelism

Lecture 33: Multiprocessors Synchronization and Consistency Professor Randy H. Katz Computer Science 252 Spring 1996

Parallel Computers. CPE 631 Session 20: Multiprocessors. Flynn s Tahonomy (1972) Why Multiprocessors?

CMSC 611: Advanced. Distributed & Shared Memory

CS654 Advanced Computer Architecture Lec 14 Directory Based Multiprocessors

5008: Computer Architecture

Lecture 17: Multiprocessors: Size, Consitency. Review: Networking Summary

Multiprocessor Synchronization

Lecture 24: Thread Level Parallelism -- Distributed Shared Memory and Directory-based Coherence Protocol

Shared Memory SMP and Cache Coherence (cont) Adapted from UCB CS252 S01, Copyright 2001 USB

Computer Architecture Lecture 10: Thread Level Parallelism II (Chapter 5) Chih Wei Liu 劉志尉 National Chiao Tung University

Aleksandar Milenkovic, Electrical and Computer Engineering University of Alabama in Huntsville. Review: Bus Snooping Topology

Chapter-4 Multiprocessors and Thread-Level Parallelism

CMSC 611: Advanced Computer Architecture

COSC 6385 Computer Architecture - Thread Level Parallelism (III)

Chapter 5. Multiprocessors and Thread-Level Parallelism

COEN-4730 Computer Architecture Lecture 08 Thread Level Parallelism and Coherence

CSE 502 Graduate Computer Architecture Lec 19 Directory-Based Shared-Memory Multiprocessors & MP Synchronization

Chapter 5. Multiprocessors and Thread-Level Parallelism

COSC 6385 Computer Architecture. - Thread Level Parallelism (III)

Chapter 6: Multiprocessors and Thread-Level Parallelism

Multiprocessors: Basics, Cache coherence, Synchronization, and Memory consistency

Introduction to Multiprocessors (Part II) Cristina Silvano Politecnico di Milano

M4 Parallelism. Implementation of Locks Cache Coherence

Chapter 5. Thread-Level Parallelism

Multiprocessor Cache Coherence. Chapter 5. Memory System is Coherent If... From ILP to TLP. Enforcing Cache Coherence. Multiprocessor Types

Introduction. Coherency vs Consistency. Lec-11. Multi-Threading Concepts: Coherency, Consistency, and Synchronization

Chapter 5 Thread-Level Parallelism. Abdullah Muzahid

MULTIPROCESSORS AND THREAD LEVEL PARALLELISM

Chapter 8. Multiprocessors. In-Cheol Park Dept. of EE, KAIST

Parallel Computer Architecture Spring Distributed Shared Memory Architectures & Directory-Based Memory Coherence

ESE 545 Computer Architecture Symmetric Multiprocessors and Snoopy Cache Coherence Protocols CA SMP and cache coherence

Lecture 18: Coherence and Synchronization. Topics: directory-based coherence protocols, synchronization primitives (Sections

EN2910A: Advanced Computer Architecture Topic 05: Coherency of Memory Hierarchy

EN2910A: Advanced Computer Architecture Topic 05: Coherency of Memory Hierarchy Prof. Sherief Reda School of Engineering Brown University

Thread-Level Parallelism

ECSE 425 Lecture 30: Directory Coherence

EC 513 Computer Architecture

EEC 581 Computer Architecture. Lec 11 Synchronization and Memory Consistency Models (4.5 & 4.6)

Lecture: Coherence, Synchronization. Topics: directory-based coherence, synchronization primitives (Sections )

Parallel Architecture. Hwansoo Han

Lecture 30: Multiprocessors Flynn Categories, Large vs. Small Scale, Cache Coherency Professor Randy H. Katz Computer Science 252 Spring 1996

Chapter - 5 Multiprocessors and Thread-Level Parallelism

Lecture 19: Coherence and Synchronization. Topics: synchronization primitives (Sections )

Thread- Level Parallelism. ECE 154B Dmitri Strukov

CMSC 411 Computer Systems Architecture Lecture 21 Multiprocessors 3

CISC 662 Graduate Computer Architecture Lectures 15 and 16 - Multiprocessors and Thread-Level Parallelism

250P: Computer Systems Architecture. Lecture 14: Synchronization. Anton Burtsev March, 2019

Computer Architecture

Handout 3 Multiprocessor and thread level parallelism

Lecture 25: Multiprocessors

Multiprocessors and Thread Level Parallelism CS 282 KAUST Spring 2010 Muhamed Mudawar

Flynn s Classification

Directory Implementation. A High-end MP

Computer Architecture

Lecture: Coherence and Synchronization. Topics: synchronization primitives, consistency models intro (Sections )

Fall 2012 EE 6633: Architecture of Parallel Computers Lecture 4: Shared Address Multiprocessors Acknowledgement: Dave Patterson, UC Berkeley

CMSC Computer Architecture Lecture 15: Memory Consistency and Synchronization. Prof. Yanjing Li University of Chicago

Lecture 24: Multiprocessing Computer Architecture and Systems Programming ( )

Lecture 25: Multiprocessors. Today s topics: Snooping-based cache coherence protocol Directory-based cache coherence protocol Synchronization

Synchronization. Coherency protocols guarantee that a reading processor (thread) sees the most current update to shared data.

Multiprocessors & Thread Level Parallelism

Cache Coherence and Atomic Operations in Hardware

Page 1. Instruction-Level Parallelism (ILP) CISC 662 Graduate Computer Architecture Lectures 16 and 17 - Multiprocessors and Thread-Level Parallelism

Flynn's Classification

Multiprocessor Cache Coherency. What is Cache Coherence?

Lecture 3: Snooping Protocols. Topics: snooping-based cache coherence implementations

Scalable Cache Coherence

Chapter 9 Multiprocessors

Lecture 10: Cache Coherence: Part I. Parallel Computer Architecture and Programming CMU , Spring 2013

CSE502: Computer Architecture CSE 502: Computer Architecture

SHARED-MEMORY COMMUNICATION

1. Memory technology & Hierarchy

Shared Memory Multiprocessors

Cache Coherence in Bus-Based Shared Memory Multiprocessors

Limitations of parallel processing

Shared Symmetric Memory Systems

MULTIPROCESSORS AND THREAD-LEVEL. B649 Parallel Architectures and Programming

Page 1. Cache Coherence

MULTIPROCESSORS AND THREAD-LEVEL PARALLELISM. B649 Parallel Architectures and Programming

Multiprocessors II: CC-NUMA DSM. CC-NUMA for Large Systems

Chapter 6. Parallel Processors from Client to Cloud Part 2 COMPUTER ORGANIZATION AND DESIGN. Homogeneous & Heterogeneous Multicore Architectures

Multiprocessors 1. Outline

Review. EECS 252 Graduate Computer Architecture. Lec 13 Snooping Cache and Directory Based Multiprocessors. Outline. Challenges of Parallel Processing

Computer Systems Architecture

Page 1. SMP Review. Multiprocessors. Bus Based Coherence. Bus Based Coherence. Characteristics. Cache coherence. Cache coherence

Lecture 4: Directory Protocols and TM. Topics: corner cases in directory protocols, lazy TM

Lecture 26: Multiprocessors. Today s topics: Directory-based coherence Synchronization Consistency Shared memory vs message-passing

4 Chip Multiprocessors (I) Chip Multiprocessors (ACS MPhil) Robert Mullins

Computer Science 146. Computer Architecture

Lecture 8: Snooping and Directory Protocols. Topics: split-transaction implementation details, directory implementations (memory- and cache-based)

Case Study 1: Single-Chip Multicore Multiprocessor

Lecture 10: Cache Coherence: Part I. Parallel Computer Architecture and Programming CMU /15-618, Spring 2015

Cache Coherence. CMU : Parallel Computer Architecture and Programming (Spring 2012)

EN164: Design of Computing Systems Lecture 34: Misc Multi-cores and Multi-processors

Transcription:

Lecture 20: Multiprocessors Aleksandar Milenkovic, milenka@ece.uah.edu Electrical and Computer Engineering University of Alabama in Huntsville Review: Small-Scale Shared Memory Caches serve to: Increase bandwidth versus bus/memory Reduce latency of access Valuable for both private data and shared data What about cache consistency? Time Event $A $B X (memory) 0 1 1 CPU A: R x 1 1 2 CPU B: R x 1 1 1 3 CPU A: W x,0 0 1 0 29/03/2004 UAH- 2 Aleksandar Milenkovich 1

Snoopy-Cache State Machine-III State machine for CPU requests for each cache block and for bus requests for each cache block Cache Block State Write miss for this block Write Back Block; (abort memory access) CPU read hit CPU write hit Write miss for this block Invalid CPU Read Place read miss CPU Write on bus Place Write Miss on bus Exclusive (read/write) CPU read miss Write back block, Place read miss on bus CPU Read hit Shared (read/only) CPU Read miss Place read miss on bus CPU Write Place Write Miss on Bus Read miss Write Back for this block Block; (abort memory access) CPU Write Miss Write back cache block Place write miss on bus 29/03/2004 UAH- 3 MESI: CPU Requests Invalid CPU Read BusRd / NoSh CPU Read hit Exclusive CPU Read miss BusRd / NoSh CPU Write /BusRdEx CPU read miss BusWB, BusRd / NoSh CPU read miss BusWB, BusRd / NoSh CPU write hit /- CPU read hit CPU write hit Modified (read/write) CPU read miss BusWB, BusRd / Sh Shared CPU read miss BusWB, BusRd / Sh CPU Write Miss BusRdEx CPU Write Hit CPU Read hit BusInv 29/03/2004 UAH- 4 Aleksandar Milenkovich 2

MESI: Bus Requests Invalid BusRdEx Exclusive BusRdEx BusRd / => Sh BusRdEx / =>BusWB Modified (read/write) BusRd / =>BusWB Shared 29/03/2004 UAH- 5 Fundamental Issues 3 Issues to characterize parallel machines 1) Naming 2) Synchronization 3) Performance: Latency and Bandwidth (covered earlier) 29/03/2004 UAH- 6 Aleksandar Milenkovich 3

Fundamental Issue #1: Naming Naming: how to solve large problem fast what data is shared how it is addressed what operations can access data how processes refer to each other Choice of naming affects code produced by a compiler; via load where just remember address or keep track of processor number and local virtual address for msg. passing Choice of naming affects replication of data; via load in cache memory hierarchy or via SW replication and consistency 29/03/2004 UAH- 7 Fundamental Issue #1: Naming Global physical address space: any processor can generate, address and access it in a single operation memory can be anywhere: virtual addr. translation handles it Global virtual address space: if the address space of each process can be configured to contain all shared data of the parallel program Segmented shared address space: locations are named <process number, address> uniformly for all processes of the parallel program 29/03/2004 UAH- 8 Aleksandar Milenkovich 4

Fundamental Issue #2: Synchronization To cooperate, processes must coordinate Message passing is implicit coordination with transmission or arrival of data Shared address => additional operations to explicitly coordinate: e.g., write a flag, awaken a thread, interrupt a processor 29/03/2004 UAH- 9 Summary: Parallel Framework Layers: Programming Model: Multiprogramming : lots of jobs, no communication Shared address space: communicate via memory Message passing: send and receive messages Programming Model Communication Abstraction Interconnection SW/OS Interconnection HW Data Parallel: several agents operate on several data sets simultaneously and then exchange information globally and simultaneously (shared or message passing) Communication Abstraction: Shared address space: e.g., load, store, atomic swap Message passing: e.g., send, receive library calls Debate over this topic (ease of programming, scaling) => many hardware designs 1:1 programming model 29/03/2004 UAH- 10 Aleksandar Milenkovich 5

Larger MPs Separate Memory per Processor Local or Remote access via memory controller One Cache Coherency solution: non-cached pages Alternative: directory per cache that tracks state of every block in every cache Which caches have a copies of block, dirty vs. clean,... Info per memory block vs. per cache block? PLUS: In memory => simpler protocol (centralized/one location) MINUS: In memory => directory is ƒ(memory size) vs. ƒ(cache size) Prevent directory as bottleneck? distribute directory entries with memory, each keeping track of which Procs have copies of their blocks 29/03/2004 UAH- 11 Distributed Directory MPs P0 P1 Pn M C IO M C IO... M C IO Interconnection Network C - Cache M - Memory IO - Input/Output 29/03/2004 UAH- 12 Aleksandar Milenkovich 6

Directory Protocol Similar to Snoopy Protocol: Three states Shared: = 1 processors have data, memory up-to-date Uncached (no processor has it; not valid in any cache) Exclusive: 1 processor (owner) has data; memory out-of-date In addition to cache state, must track which processors have data when in the shared state (usually bit vector, 1 if processor has copy) Keep it simple(r): Writes to non-exclusive data => write miss Processor blocks until access completes Assume messages received and acted upon in order sent 29/03/2004 UAH- 13 Directory Protocol No bus and don t want to broadcast: interconnect no longer single arbitration point all messages have explicit responses Terms: typically 3 processors involved Local node where a request originates Home node where the memory location of an address resides Remote node has a copy of a cache block, whether exclusive or shared Example messages on next slide: P = processor number, A = address 29/03/2004 UAH- 14 Aleksandar Milenkovich 7

Directory Protocol Messages Message type Source Destination Msg Content Read miss Local cache Home directory P, A Processor P reads data at address A; make P a read sharer and arrange to send data back Write miss Local cache Home directory P, A Processor P writes data at address A; make P the exclusive owner and arrange to send data back Invalidate Home directory Remote caches A Invalidate a shared copy at address A. Fetch Home directory Remote cache A Fetch the block at address A and send it to its home directory Fetch/Invalidate Home directory Remote cache A Fetch the block at address A and send it to its home directory; invalidate the block in the cache Data value reply Home directory Local cache Data Return a data value from the home memory (read miss response) Data write-back Remote cache Home directory A, Data Write-back a data value for address A (invalidate response) 29/03/2004 UAH- 15 State Transition Diagram for an Individual Cache Block in a Directory Based System States identical to snoopy case; transactions very similar Transitions caused by read misses, write misses, invalidates, data fetch requests Generates read miss & write miss msg to home directory Write misses that were broadcast on the bus for snooping => explicit invalidate & data fetch requests Note: on a write, a cache block is bigger, so need to read the full cache block 29/03/2004 UAH- 16 Aleksandar Milenkovich 8

CPU -Cache State Machine State machine for CPU requests for each memory block Invalid state if in memory Fetch/Invalidate send Data Write Back message to home directory Invalid Exclusive (read/writ) Invalidate CPU Read Send Read Miss message CPU Write: Send Write Miss msgto h.d. Shared (read/only) CPU Read hit CPU read miss: Send Read Miss CPU Write:Send Write Miss message to home directory Fetch: send Data Write Back message to home directory CPU read miss:send Data Write Back message and read miss to home directory CPU read hit CPU write miss: CPU write hit send Data Write Back message and Write Miss to home 29/03/2004 UAH- directory 17 State Transition Diagram for the Directory Same states & structure as the transition diagram for an individual cache 2 actions: update of directory state & send msgs to statisfy requests Tracks all copies of memory block. Also indicates an action that updates the sharing set, Sharers, as well as sending a message. 29/03/2004 UAH- 18 Aleksandar Milenkovich 9

Directory State Machine State machine for Directory requests for each memory block Uncached state if in memory Data Write Back: Sharers = {} (Write back block) Write Miss: Sharers = {P}; send Fetch/Invalidate; send Data Value Reply msg to remote cache Uncached Exclusive (read/writ) Read miss: Sharers = {P} send Data Value Reply Write Miss: Sharers = {P}; send Data Value Reply msg Shared (read only) Read miss: Sharers += {P}; send Fetch; send Data Value Reply msg to remote cache (Write back block) Read miss: Sharers += {P}; send Data Value Reply Write Miss: send Invalidate to Sharers; then Sharers = {P}; send Data Value Reply msg 29/03/2004 UAH- 19 Example Directory Protocol Message sent to directory causes two actions: Update the directory More messages to satisfy request Block is in Uncached state: the copy in memory is the current value; only possible requests for that block are: Read miss: requesting processor sent data from memory &requestor made only sharing node; state of block made Shared. Write miss: requesting processor is sent the value & becomes the Sharing node. The block is made Exclusive to indicate that the only valid copy is cached. Sharers indicates the identity of the owner. Block is Shared => the memory value is up-to-date: Read miss: requesting processor is sent back the data from memory & requesting processor is added to the sharing set. Write miss: requesting processor is sent the value. All processors in the set Sharers are sent invalidate messages, & Sharers is set to identity of requesting processor. The state of the block is made Exclusive. 29/03/2004 UAH- 20 Aleksandar Milenkovich 10

Example Directory Protocol Block is Exclusive: current value of the block is held in the cache of the processor identified by the set Sharers (the owner) => three possible directory requests: Read miss: owner processor sent data fetch message, causing state of block in owner s cache to transition to Shared and causes owner to send data to directory, where it is written to memory & sent back to requesting processor. Identity of requesting processor is added to set Sharers, which still contains the identity of the processor that was the owner (since it still has a readable copy). State is shared. Data write-back: owner processor is replacing the block and hence must write it back, making memory copy up-to-date (the home directory essentially becomes the owner), the block is now Uncached, and the Sharer set is empty. Write miss: block has a new owner. A message is sent to old owner causing the cache to send the value of the block to the directory from which it is sent to the requesting processor, which becomes the new owner. Sharers is set to identity of new owner, and state of block is made Exclusive. 29/03/2004 UAH- 21 Example Processor 1 Processor 2 Interconnect Directory Memory P1 P2 Bus Directory Memory step State Addr Value State Addr Value Action Proc. Addr Value Addr State {Procs} Value P1: Write 10 to A1 P1: Read A1 P2: Read A1 P2: Write 20 to A1 P2: Write 40 to A2 A1 and A2 map to the same cache block 29/03/2004 UAH- 22 Aleksandar Milenkovich 11

Example Processor 1 Processor 2 Interconnect Directory Memory P1 P2 Bus Directory Memory step State Addr Value State Addr Value Action Proc. Addr Value Addr State {Procs} Value P1: Write 10 to A1 WrMs P1 A1 A1 Ex {P1} Excl. A1 10 DaRp P1 A1 0 P1: Read A1 P2: Read A1 P2: Write 20 to A1 P2: Write 40 to A2 A1 and A2 map to the same cache block 29/03/2004 UAH- 23 Example Processor 1 Processor 2 Interconnect Directory Memory P1 P2 Bus Directory Memory step State Addr Value State Addr Value Action Proc. Addr Value Addr State {Procs} Value P1: Write 10 to A1 WrMs P1 A1 A1 Ex {P1} Excl. A1 10 DaRp P1 A1 0 P1: Read A1 Excl. A1 10 P2: Read A1 P2: Write 20 to A1 P2: Write 40 to A2 A1 and A2 map to the same cache block 29/03/2004 UAH- 24 Aleksandar Milenkovich 12

Example Processor 1 Processor 2 Interconnect Directory Memory P1 P2 Bus Directory Memory step State Addr Value State Addr Value Action Proc. Addr Value Addr State {Procs} Value P1: Write 10 to A1 WrMs P1 A1 A1 Ex {P1} Excl. A1 10 DaRp P1 A1 0 P1: Read A1 Excl. A1 10 P2: Read A1 Shar. A1 RdMs P2 A1 Shar. A1 10 Ftch P1 A1 10 A1 A1 10 Shar. A1 10 DaRp P2 A1 10 A1 Shar. P1,P2} 10 P2: Write 20 to A1 10 10 P2: Write 40 to A2 10 Write Write Back Back A1 and A2 map to the same cache block 29/03/2004 UAH- 25 Example Processor 1 Processor 2 Interconnect Directory Memory P1 P2 Bus Directory Memory step State Addr Value State Addr Value Action Proc. Addr Value Addr State {Procs} Value P1: Write 10 to A1 WrMs P1 A1 A1 Ex {P1} Excl. A1 10 DaRp P1 A1 0 P1: Read A1 Excl. A1 10 P2: Read A1 Shar. A1 RdMs P2 A1 Shar. A1 10 Ftch P1 A1 10 A1 A1 10 Shar. A1 10 DaRp P2 A1 10 A1 Shar. P1,P2} 10 P2: Write 20 to A1 Excl. A1 20 WrMs P2 A1 10 Inv. Inval. P1 A1 A1 Excl. {P2} 10 P2: Write 40 to A2 10 A1 and A2 map to the same cache block 29/03/2004 UAH- 26 Aleksandar Milenkovich 13

Example Processor 1 Processor 2 Interconnect Directory Memory P1 P2 Bus Directory Memory step State Addr Value State Addr Value Action Proc. Addr Value Addr State {Procs} Value P1: Write 10 to A1 WrMs P1 A1 A1 Ex {P1} Excl. A1 10 DaRp P1 A1 0 P1: Read A1 Excl. A1 10 P2: Read A1 Shar. A1 RdMs P2 A1 Shar. A1 10 Ftch P1 A1 10 A1 A1 10 Shar. A1 10 DaRp P2 A1 10 A1 Shar. P1,P2} 10 P2: Write 20 to A1 Excl. A1 20 WrMs P2 A1 10 Inv. Inval. P1 A1 A1 Excl. {P2} 10 P2: Write 40 to A2 WrMs P2 A2 A2 Excl. {P2} 0 WrBk P2 A1 20 A1 Unca. {} 20 Excl. A2 40 DaRp P2 A2 0 A2 Excl. {P2} 0 A1 and A2 map to the same cache block 29/03/2004 UAH- 27 Implementing a Directory We assume operations atomic, but they are not; reality is much harder; must avoid deadlock when run out of buffers in network (see Appendix I) The devil is in the details Optimizations: read miss or write miss in Exclusive: send data directly to requestor from owner vs. 1st to memory and then from memory to requestor 29/03/2004 UAH- 28 Aleksandar Milenkovich 14

Synchronization Why Synchronize? Need to know when it is safe for different processes to use shared data Issues for Synchronization: Uninterruptable instruction to fetch and update memory (atomic operation); User level synchronization operation using this primitive; For large scale MPs, synchronization can be a bottleneck; techniques to reduce contention and latency of synchronization 29/03/2004 UAH- 29 Uninterruptable Instruction to Fetch and Update Memory Atomic exchange: interchange a value in a register for a value in memory 0 => synchronization variable is free 1 => synchronization variable is locked and unavailable Set register to 1 & swap New value in register determines success in getting lock 0 if you succeeded in setting the lock (you were first) 1 if other processor had already claimed access Key is that exchange operation is indivisible Test-and-set: tests a value and sets it if the value passes the test Fetch-and-increment: it returns the value of a memory location and atomically increments it 0 => synchronization variable is free 29/03/2004 UAH- 30 Aleksandar Milenkovich 15

Uninterruptable Instruction to Fetch and Update Memory Hard to have read & write in 1 instruction: use 2 instead Load linked (or load locked) + store conditional Load linked returns the initial value Store conditional returns 1 if it succeeds (no other store to same memory location since preceeding load) and 0 otherwise Example doing atomic swap with LL & SC: try: mov R3,R4 ; mov exchange value ll R2,0(R1) ; load linked sc R3,0(R1) ; store conditional beqz R3,try ; branch store fails (R3 = 0) mov R4,R2 ; put load value in R4 Example doing fetch & increment with LL & SC: try: ll R2,0(R1) ; load linked addi R2,R2,#1 ; increment (OK if reg reg) sc R2,0(R1) ; store conditional beqz R2,try ; branch store fails (R2 = 0) 29/03/2004 UAH- 31 User Level Synchronization Operation Using this Primitive Spin locks: processor continuously tries to acquire, spinning around a loop trying to get the lock li R2,#1 lockit: exch R2,0(R1) ;atomic exchange bnez R2,lockit ;already locked? What about MP with cache coherency? Want to spin on cache copy to avoid full memory latency Likely to get cache hits for such variables Problem: exchange includes a write, which invalidates all other copies; this generates considerable bus traffic Solution: start by simply repeatedly reading the variable; when it changes, then try exchange ( test and test&set ): try: li R2,#1 lockit: lw R3,0(R1) ;load var bnez R3,lockit ;not free=>spin exch R2,0(R1) ;atomic exchange bnez R2,try ;already locked? 29/03/2004 UAH- 32 Aleksandar Milenkovich 16

Another MP Issue: Memory Consistency Models What is consistency? When must a processor see the new value? e.g., seems that P1: A = 0; P2: B = 0;...... A = 1; B = 1; L1: if (B == 0)... L2: if (A == 0)... Impossible for both if statements L1 & L2 to be true? What if write invalidate is delayed & processor continues? Memory consistency models: what are the rules for such cases? Sequential consistency: result of any execution is the same as if the accesses of each processor were kept in order and the accesses among different processors were interleaved => assignments before ifs above SC: delay all memory accesses until all invalidates done 29/03/2004 UAH- 33 Parallel Program: An Example /* * Title: Matrix multiplication kernel * Author: Aleksandar Milenkovic, milenkovic@computer.org * Date: November, 1997 * *------------------------------------------------------------ * Command Line Options * -pp: P = number of processors; must be a power of 2. * -nn: N = number of columns (even integers). * -h : Print out command line options. *------------------------------------------------------------ * */ void main(int argc, char*argv[]) { /* Define shared matrix */ ma = (double **) G_MALLOC(N*sizeof(double *)); mb = (double **) G_MALLOC(N*sizeof(double *)); for(i=0; i<n; i++) { ma[i] = (double *) G_MALLOC(N*sizeof(double)); mb[i] = (double *) G_MALLOC(N*sizeof(double)); }; /* Initialize the Index */ Index = 0; /* Initialize the barriers and the lock */ LOCKINIT(indexLock) BARINIT(bar_fin) /* read/initialize data */... /* do matrix multiplication in parallel a=a*b */ /* Create the slave processes. */ for (i = 0; i < numprocs-1; i++) CREATE(SlaveStart) /* Make the master do slave work so we don't waste a processor */ SlaveStart(); }... 29/03/2004 UAH- 34 Aleksandar Milenkovich 17

Parallel Program: An Example /*====== SlaveStart ================*/ /* This is the routine that each processor will be executing in parallel */ void SlaveStart() { int myindex, i, j, k, begin, end; double tmp; LOCK(indexLock); /* enter the critical section */ myindex = Index; /* read your ID */ ++Index; /* increment it, so the next will operate on ID+1 */ UNLOCK(indexLock); /* leave the critical section */ /* Initialize begin and end */ begin = (N/numProcs)* myindex; end = (N/numProcs)*(myIndex+1); /* the main body of a thread */ } for(i=begin; i<end; i++) { } for(j=0; j<n; j++) { tmp=0.0; for(k=0; k<n; k++) { tmp = tmp + ma[i][k]* mb[k][j]; } ma[i][j] = tmp; } BARRIER(bar_fin, numprocs); 29/03/2004 UAH- 35 Synchronization Why Synchronize? Need to know when it is safe for different processes to use shared data Issues for Synchronization: Uninterruptable instruction to fetch and update memory (atomic operation); User level synchronization operation using this primitive; For large scale MPs, synchronization can be a bottleneck; techniques to reduce contention and latency of synchronization 29/03/2004 UAH- 36 Aleksandar Milenkovich 18

Uninterruptable Instruction to Fetch and Update Memory Atomic exchange: interchange a value in a register for a value in memory 0 => synchronization variable is free 1 => synchronization variable is locked and unavailable Set register to 1 & swap New value in register determines success in getting lock 0 if you succeeded in setting the lock (you were first) 1 if other processor had already claimed access Key is that exchange operation is indivisible Test-and-set: tests a value and sets it if the value passes the test Fetch-and-increment: it returns the value of a memory location and atomically increments it 0 => synchronization variable is free 29/03/2004 UAH- 37 Lock&Unlock: Test&Set /* Test&Set */ ============== loadi R2, #1 lockit: exch R2, location /* atomic operation*/ bnez R2, lockit /* test*/ unlock: 0) */ store location, #0 /* free the lock (write 29/03/2004 UAH- 38 Aleksandar Milenkovich 19

Lock&Unlock: Test and Test&Set /* Test and Test&Set */ ======================= lockit: load R2, location /* read lock varijable */ bnz R2, lockit /* check value */ loadi R2, #1 exch R2, location /* atomic operation */ bnz reg, lockit /* if lock is not acquired, repeat */ unlock: store location, #0 /* free the lock (write 0) */ 29/03/2004 UAH- 39 Lock&Unlock: Test and Test&Set /* Load-linked and Store-Conditional */ ======================================= lockit: ll R2, location /* load-linked read */ */ bnz R2, lockit /* if busy, try again */ load R2, #1 sc location, R2 /* conditional store */ beqz R2, lockit /* if sc unsuccessful, try again unlock: store location, #0 /* store 0 */ 29/03/2004 UAH- 40 Aleksandar Milenkovich 20

Uninterruptable Instruction to Fetch and Update Memory Hard to have read & write in 1 instruction: use 2 instead Load linked (or load locked) + store conditional Load linked returns the initial value Store conditional returns 1 if it succeeds (no other store to same memory location since preceeding load) and 0 otherwise Example doing atomic swap with LL & SC: try: mov R3,R4 ; mov exchange value ll R2,0(R1) ; load linked sc R3,0(R1) ; store conditional (returns 1, if Ok) beqz R3,try ; branch store fails (R3 = 0) mov R4,R2 ; put load value in R4 Example doing fetch & increment with LL & SC: try: ll R2,0(R1) ; load linked addi R2,R2,#1 ; increment (OK if reg reg) sc R2,0(R1) ; store conditional beqz R2,try ; branch store fails (R2 = 0) 29/03/2004 UAH- 41 User Level Synchronization Operation Using this Primitive Spin locks: processor continuously tries to acquire, spinning around a loop trying to get the lock li R2,#1 lockit: exch R2,0(R1) ;atomic exchange bnez R2,lockit ;already locked? What about MP with cache coherency? Want to spin on cache copy to avoid full memory latency Likely to get cache hits for such variables Problem: exchange includes a write, which invalidates all other copies; this generates considerable bus traffic Solution: start by simply repeatedly reading the variable; when it changes, then try exchange ( test and test&set ): try: li R2,#1 lockit: lw R3,0(R1) ;load var bnez R3,lockit ;not free=>spin exch R2,0(R1) ;atomic exchange bnez R2,try ;already locked? 29/03/2004 UAH- 42 Aleksandar Milenkovich 21

Another MP Issue: Memory Consistency Models What is consistency? When must a processor see the new value? e.g., seems that P1: A = 0; P2: B = 0;...... A = 1; B = 1; L1: if (B == 0)... L2: if (A == 0)... Impossible for both if statements L1 & L2 to be true? What if write invalidate is delayed & processor continues? Memory consistency models: what are the rules for such cases? Sequential consistency: result of any execution is the same as if the accesses of each processor were kept in order and the accesses among different processors were interleaved => assignments before ifs above SC: delay all memory accesses until all invalidates done 29/03/2004 UAH- 43 Memory Consistency Model Schemes faster execution to sequential consistency Not really an issue for most programs; they are synchronized A program is synchronized if all access to shared data are ordered by synchronization operations write (x)... release (s) {unlock}... acquire (s) {lock}... read(x) Only those programs willing to be nondeterministic are not synchronized: data race : outcome f(proc. speed) Several Relaxed Models for Memory Consistency since most programs are synchronized; characterized by their attitude towards: RAR, WAR, RAW, WAW to different addresses 29/03/2004 UAH- 44 Aleksandar Milenkovich 22