Foundations of Shared Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.

Slides:



Advertisements
Similar presentations
Time-based Transactional Memory with Scalable Time Bases Torvald Riegel, Christof Fetzer, Pascal Felber Presented By: Michael Gendelman.
Advertisements

Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 4: Mutual Exclusion.
Monitors & Blocking Synchronization 1. Producers & Consumers Problem Two threads that communicate through a shared FIFO queue. These two threads can’t.
Introduction Companion slides for
Foundations of Shared Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Art of Multiprocessor Programming.
It Ain’t the Meat, it’s the Notion Why Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University.
Foundations of Shared Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Rajeev Alur for CIS.
1 Principles of Reliable Distributed Systems Lecture 10: Atomic Shared Memory Objects and Shared Memory Emulations Spring 2007 Prof. Idit Keidar.
Shared Counters and Parallelism Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Introduction to Lock-free Data-structures and algorithms Micah J Best May 14/09.
CPSC 668Set 17: Fault-Tolerant Register Simulations1 CPSC 668 Distributed Algorithms and Systems Fall 2009 Prof. Jennifer Welch.
Distributed Algorithms (22903) Lecturer: Danny Hendler Shared objects: linearizability, wait-freedom and simulations Most of this presentation is based.
What Can Be Implemented Anonymously ? Paper by Rachid Guerraui and Eric Ruppert Presentation by Amir Anter 1.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Art of Multiprocessor Programming 1 Universality of Consensus Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Two-Process Systems TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: A A AA Companion slides for Distributed Computing.
Multicore Programming
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 3 (26/01/2006) Instructor: Haifeng YU.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Chapter 9 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
DISTRIBUTED ALGORITHMS AND SYSTEMS Spring 2014 Prof. Jennifer Welch CSCE
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Software Transactional Memory Should Not Be Obstruction-Free Robert Ennals Presented by Abdulai Sei.
Concurrent Queues Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
1 © R. Guerraoui Registers Prof R. Guerraoui Distributed Programming Laboratory.
Programming Language Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Multiprocessor Architecture Basics Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Concurrent Stacks Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Priority Queues Dan Dvorin Based on ‘The Art of Multiprocessor Programming’, by Herlihy & Shavit, chapter 15.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
“Towards Self Stabilizing Wait Free Shared Memory Objects” By:  Hopeman  Tsigas  Paptriantafilou Presented By: Sumit Sukhramani Kent State University.
Distributed Algorithms (22903) Lecturer: Danny Hendler The Atomic Snapshot Object The Renaming Problem This presentation is based on the book “Distributed.
Distributed Algorithms (22903) Lecturer: Danny Hendler Shared objects: linearizability, wait-freedom and simulations Most of this presentation is based.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Spin Locks and Contention Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Queue Locks and Local Spinning Some Slides based on: The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
The Relative Power of Synchronization Operations Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
6.852: Distributed Algorithms Spring, 2008 Class 14.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
Mutual Exclusion Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Atomic Operations in Hardware
Concurrent Objects Companion slides for
Mutual Exclusion Companion slides for
Foundations of Shared Memory
Mutual Exclusion Companion slides for
Foundations of Shared Memory
Implementing Mutual Exclusion
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
Lecture 1: Introduction
Distributed Algorithms (22903)
CSE 153 Design of Operating Systems Winter 19
Distributed Algorithms (22903)
CSCI1600: Embedded and Real Time Software
Problems with Locks Andrew Whitaker CSE451.
Nir Shavit Multiprocessor Synchronization Spring 2003
Presentation transcript:

Foundations of Shared Memory Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

© 2007 Herlihy & Shavit2 Last Lecture Defined concurrent objects using linearizability and sequential consistency Fact: implemented linearizable objects (Two thread FIFO Queue) in read-write memory without mutual exclusion Fact: hardware does not provide linearizable read-write memory

© 2007 Herlihy & Shavit3 Fundamentals What is the weakest form of communication that supports mutual exclusion? What is the weakest shared object that allows shared-memory computation?

© 2007 Herlihy & Shavit4 Alan Turing Helped us understand what is and is not computable on a sequential machine. Still best model available

© 2007 Herlihy & Shavit Turing Machine Reads and Writes Infinite tape Finite State Controller

© 2007 Herlihy & Shavit6 Turing Computability Mathematical model of computation What is (and is not) computable Efficiency (mostly) irrelevant

© 2007 Herlihy & Shavit7 Shared-Memory Computability? Mathematical model of concurrent computation What is (and is not) concurrently computable Efficiency (mostly) irrelevant Shared Memory

© 2007 Herlihy & Shavit8 Foundations of Shared Memory To understand modern multiprocessors we need to ask some basic questions …

© 2007 Herlihy & Shavit9 Foundations of Shared Memory To understand modern multiprocessors we need to ask some basic questions … What is the weakest useful form of shared memory?

© 2007 Herlihy & Shavit10 Foundations of Shared Memory To understand modern multiprocessors we need to ask some basic questions … What is the weakest useful form of shared memory? What can it do?

© 2007 Herlihy & Shavit11 Foundations of Shared Memory To understand modern multiprocessors we need to ask some basic questions … What is the weakest useful form of shared memory? What can it do? What can’t it do?

© 2007 Herlihy & Shavit12 Register Holds a (binary) value * * A memory location: name is historical

© 2007 Herlihy & Shavit13 Register Can be read

© 2007 Herlihy & Shavit14 Register Can be written

© 2007 Herlihy & Shavit15 public interface Register { public T read(); public void write(T v); } Registers

© 2007 Herlihy & Shavit16 public interface Register { public T read(); public void write(T v); } Registers Type of register (usually Boolean or m-bit Integer)

© 2007 Herlihy & Shavit Single-Reader/Single-Writer Register

© 2007 Herlihy & Shavit Multi-Reader/Single-Writer Register

© 2007 Herlihy & Shavit19 mumble Multi-Reader/Multi-Writer Register mumble

© 2007 Herlihy & Shavit20 Jargon Watch SRSW –Single-reader single-writer MRSW –Multi-reader single-writer MRMW –Multi-reader multi-writer

© 2007 Herlihy & Shavit21 Safe Register write(1001) read(1001) OK if reads and writes don’t overlap (2)

© 2007 Herlihy & Shavit22 Safe Register write(1001) Some valid value if reads and writes do overlap read(????) $*&v

© 2007 Herlihy & Shavit23 Regular Register write(0) read(1) write(1) read(0) Single Writer Readers return: –Old value if no overlap (safe) –Old or one of new values if overlap

© 2007 Herlihy & Shavit24 Regular or Not? write(0) read(1) write(1) read(0)

© 2007 Herlihy & Shavit25 Regular or Not? write(0) read(1) write(1) read(0) Overlap: returns new value

© 2007 Herlihy & Shavit26 Regular or Not? write(0)write(1) read(0) Overlap: returns old value

© 2007 Herlihy & Shavit27 Regular or Not? write(0) read(1) write(1) read(0) regular

© 2007 Herlihy & Shavit28 Regular ≠ Atomic write(0) read(1) write(1) read(0) write(1) already happened explain this!

© 2007 Herlihy & Shavit29 Atomic Register write(1001) read(1001) Linearizable to sequential safe register write(1010) read(1010)

© 2007 Herlihy & Shavit30 Atomic Register write(1001) read(1001) write(1010) read(1010)

© 2007 Herlihy & Shavit31 Register Space MRMW MRSW SRSW Safe Regular Atomic M-valued Boolean

© 2007 Herlihy & Shavit32 Weakest Register Single readerSingle writer Safe Boolean register

© 2007 Herlihy & Shavit33 Weakest Register Single readerSingle writer Get correct reading if not during state transition flipflop

© 2007 Herlihy & Shavit34 Results From SRSW safe Boolean register –All the other registers –Mutual exclusion But not everything! –Consensus hierarchy Foundations of the field The really cool stuff … (2)

© 2007 Herlihy & Shavit35 Locking within Registers Not interesting to rely on mutual exclusion in register constructions We want registers to implement mutual exclusion! No fun to use mutual exclusion to implement itself!

© 2007 Herlihy & Shavit36 Wait-Free Implementations Definition: An object implementation is wait-free if every method call completes in a finite number of steps No mutual exclusion –Thread could halt in critical section –Build mutual exclusion from registers

© 2007 Herlihy & Shavit37 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot

© 2007 Herlihy & Shavit38 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next

© 2007 Herlihy & Shavit39 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names

© 2007 Herlihy & Shavit40 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names property

© 2007 Herlihy & Shavit41 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names property Size matters

© 2007 Herlihy & Shavit42 public class SafeBoolMRSWRegister implements Register { public boolean read() { … } public void write(boolean x) { … } } (3) Register Names property type How many readers & writers?

© 2007 Herlihy & Shavit43 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements Register { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }} (2)

© 2007 Herlihy & Shavit44 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements BooleanRegister { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }} (2) Each thread has own safe SRSW register

© 2007 Herlihy & Shavit45 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements BooleanRegister { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }} (2) write method

© 2007 Herlihy & Shavit46 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements BooleanRegister { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }} (2) Write each thread’s register one at a time

© 2007 Herlihy & Shavit47 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements BooleanRegister { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }} (2) read method

© 2007 Herlihy & Shavit48 Safe Boolean MRSW from Safe Boolean SRSW public class SafeBoolMRSWRegister implements BooleanRegister { private SafeBoolSRSWRegister[] r = new SafeBoolSRSWRegister[N]; public void write(boolean x) { for (int j = 0; j < N; j++) r[j].write(x); } public boolean read() { int i = ThreadID.get(); return r[i].read(); }} (2) Read my own register

© 2007 Herlihy & Shavit49 Safe Boolean MRSW from Safe Boolean SRSW or

© 2007 Herlihy & Shavit Q: Safe Multi-Valued MRSW Safe Multi-Valued SRSW? Any value in range 0 0 Yes, it works!Yes, it works! 1011

© 2007 Herlihy & Shavit51 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?

© 2007 Herlihy & Shavit52 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next

© 2007 Herlihy & Shavit53 Regular Boolean MRSW from Safe Boolean MRSW Regular: if it changed, OK to read 0 or 1 Safe register can return 0 or 1 even if the same value is written Regular: But only old value if not changed

© 2007 Herlihy & Shavit54 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { private boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; }} public boolean read() { return value.read(); }} (2)

© 2007 Herlihy & Shavit55 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; }} public boolean read() { return value.read(); }} (2) Last bit this thread wrote (OK, we’re cheating here on Java syntax)

© 2007 Herlihy & Shavit56 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; }} public boolean read() { return value.read(); }} (2) Actual value

© 2007 Herlihy & Shavit57 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; }} public boolean read() { return value.read(); }} (2) Is new value different from last value I wrote?

© 2007 Herlihy & Shavit58 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; }} public boolean read() { return value.read(); }} (2) If so, change it (otherwise don’t!)

© 2007 Herlihy & Shavit59 Regular Boolean MRSW from Safe Boolean MRSW public class RegBoolMRSWRegister implements Register { threadLocal boolean old; private SafeBoolMRSWRegister value; public void write(boolean x) { if (old != x) { value.write(x); old = x; }} public boolean read() { return value.read(); }} (2) Overlap? No Overlap? No problem either Boolean value works

© 2007 Herlihy & Shavit60 Regular Multi-Valued MRSW from Safe Multi-Valued MRSW? Multi- valued Regular register can return only old or new when value changes! Safe register can return value in range other than old or new when value changes No, it does not work!

© 2007 Herlihy & Shavit61 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?

© 2007 Herlihy & Shavit62 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next

© 2007 Herlihy & Shavit63 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegister implements Register{ RegBoolMRSWRegister[M] bit; public void write(int x) { this.bit[x].write(true); for (int i=x-1; i>=0; i--) this.bit[i].write(false); } public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }}

© 2007 Herlihy & Shavit64 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegister implements Register{ RegBoolMRSWRegister[M] bit; public void write(int x) { this.bit[x].write(true); for (int i=x-1; i>=0; i--) this.bit[i].write(false); } public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }} Unary representation: bit[i] means value i

© 2007 Herlihy & Shavit65 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegisterimplements Register { RegBoolMRSWRegister[m] bit; public void write(int x) { this.bit[x].write(true); for (int i=x-1; i>=0; i--) this.bit[i].write(false); } public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }} (1) Set bit x

© 2007 Herlihy & Shavit66 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegisterimplements Register { RegBoolMRSWRegister[m] bit; public void write(int x) { this.bit[x].write(true); for (int i=x-1; i>=0; i--) this.bit[i].write(false); } public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }} (1) Clear bits from higher to lower

© 2007 Herlihy & Shavit67 MRSW Regular M-valued from MRSW Regular Boolean public class RegMRSWRegisterimplements Register { RegBoolMRSWRegister[m] bit; public void write(int x) { this.bit[x].write(true); for (int i=x-1; i>=0; i--) this.bit[i].write(false); } public int read() { for (int i=0; i < M; i++) if (this.bit[i].read()) return i; }} (1) Scan from lower to higher & return first bit set

© 2007 Herlihy & Shavit68 Writing M-Valued Write 5 Initially 0

© 2007 Herlihy & Shavit69 Writing M-Valued Write 5 5 0

© 2007 Herlihy & Shavit70 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?Questions?

© 2007 Herlihy & Shavit71 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot

© 2007 Herlihy & Shavit72 Road Map (Slight Detour) SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot SWSW Atomic

Concurrent Reading © 2007 Herlihy & Shavit73 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader Instead of 5678… When is this a problem?

© 2007 Herlihy & Shavit74 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader 5678 time Reg write(5678) Reg read(5678) Initially 1234 Same as Atomic

© 2007 Herlihy & Shavit75 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader Instead of 5678… time Reg write(5678) Reg read(1234) Initially 1234 Same as Atomic

© 2007 Herlihy & Shavit76 SRSW Atomic From SRSW Regular 1234 Regular writer Regular reader Instead of 5678… time Reg write(5678) Reg read(1234) Initially 1234 Reg read(5678) not Atomic! Write 5678 happened

© 2007 Herlihy & Shavit77 Timestamped Values Writer writes value and stamp together Reader saves last read (value,stamp) and returns new value only if higher stamp : : :00

© 2007 Herlihy & Shavit78 SRSW Atomic From SRSW Regular writer reader 1: Less than 2: So stick with 5678 time Reg write(2: ) read(1: ) 1: read(2: ) : old = 2:00 > :00 Same as Atomic

© 2007 Herlihy & Shavit79 Atomic Single Reader to Atomic Multi-Reader 1: : : : stampvalue One per reader

© 2007 Herlihy & Shavit80 Another Scenario 1: : : : stampvalue Writer starts write…

© 2007 Herlihy & Shavit81 Another Scenario 1: : : : stampvalue reader reads 2:00, 56782:00, 5678 zzz…zzz… 1: : later reader Yellow was completely after blue but read earlier value…not linearizable!Yellow was completely after blue but read earlier value…not linearizable!

© 2007 Herlihy & Shavit82 Multi-Reader Redux 1: : :451:45 1: :451:45 1:451:45 One per threadOne per thread 1: :451:45 1:

© 2007 Herlihy & Shavit83 Multi-Reader Redux 1: : : : : : : : Writer writes column… 2: : : : reader reads row 2:00,

© 2007 Herlihy & Shavit84 Multi-Reader Redux 1: : : : : : : :451:45 2: : reader writes column to notify others of what it read : : : : zzz…after second write 2:00, 5678 Yellow reader will read new value in column written by earlier Blue reader

© 2007 Herlihy & Shavit85 Can’t Yellow Miss Blue’s Update? … Only if Readers Overlap… time write(2: ) read(1: ) 1: read(2: ) In which case its OK to read 1234

© 2007 Herlihy & Shavit86 Bad Case Only When Readers Don’t Overlap time write(2: ) read(2: ) 1: read(2: ) In which case Blue will complete writing 2: to its column

© 2007 Herlihy & Shavit87 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next

© 2007 Herlihy & Shavit88 Multi-Writer Atomic From Multi-Reader Atomic 1: : : : stampvalue Readers read all and take max (Lexicographic like Bakery) Each writer reads all then writes Max+1 to its register 2: :15XYZW Max is 2:15, return XYZW

© 2007 Herlihy & Shavit89 Atomic Execution Means its Linearizable time write(1) time (4) Read(max= 2)write(4) write(2) write(3)Read(max = 3) Read (max = 1) write(2)Read(max = 4)

© 2007 Herlihy & Shavit90 Linearization Points time write(1) time (4) Read(max= 2)write(4) write(2) write(3)Read(max = 3) Read (max = 1) write(2)Read(max = 4)

© 2007 Herlihy & Shavit91 Linearization Points time write(1) time Look at Writes First (4) write(4) write(2) write(3) write(2)

© 2007 Herlihy & Shavit92 Linearization Points time write(1) time (4) write(4) write(2) write(3) write(2) Order writes by TimeStamp

© 2007 Herlihy & Shavit93 Linearization Points time write(1) time (4) write(4) write(2) write(3) write(2) Read(max= 2) Read(max = 3) Read (max = 1) Read(max = 4) Order reads by max stamp read

© 2007 Herlihy & Shavit94 Linearization Points time write(1) time (4) write(4) write(2) write(3) write(2) Read(max= 2) Read(max = 3) Read (max = 1) Read(max = 4) Order reads by max stamp read

© 2007 Herlihy & Shavit95 Linearization Points time write(1) time (4) write(4) write(2) write(3) write(2) Read(max= 2) Read(max = 3) Read (max = 1) Read(max = 4) The linearization point depends on the execution (not a line in the code)!

© 2007 Herlihy & Shavit96 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Questions?

© 2007 Herlihy & Shavit97 Road Map SRSW safe Boolean MRSW safe Boolean MRSW regular Boolean MRSW regular MRSW atomic MRMW atomic Atomic snapshot Next

© 2007 Herlihy & Shavit98 Atomic Snapshot update scan

© 2007 Herlihy & Shavit99 Atomic Snapshot Array of SWMR atomic registers Take instantaneous snapshot of all Generalizes to MRMW registers …

© 2007 Herlihy & Shavit100 Snapshot Interface public interface Snapshot { public int update(int v); public int[] scan(); } (2)

© 2007 Herlihy & Shavit101 Snapshot Interface public interface Snapshot { public int update(int v); public int[] scan(); } Thread i writes v to its register (2)

© 2007 Herlihy & Shavit102 Snapshot Interface public interface Snapshot { public int update(int v); public int[] scan(); } Instantaneous snapshot of all theads’ registers (2)

© 2007 Herlihy & Shavit103 Atomic Snapshot Collect –Read values one at a time Problem –Incompatible concurrent collects –Result not linearizable

© 2007 Herlihy & Shavit104 Clean Collects Clean Collect –Collect during which nothing changed –Can we make it happen? –Can we detect it?

© 2007 Herlihy & Shavit105 Simple Snapshot Put increasing labels on each entry Collect twice If both agree, –We’re done Otherwise, –Try again = Collect2 Collect

© 2007 Herlihy & Shavit106 Simple Snapshot: Update public class SimpleSnapshot implements Snapshot { private AtomicMRSWRegister[] register; public void update(int value) { int i = Thread.myIndex(); LabeledValue oldValue = register[i].read(); LabeledValue newValue = new LabeledValue(oldValue.label+1, value); register[i].write(newValue); } (1)

© 2007 Herlihy & Shavit107 Simple Snapshot: Update public class SimpleSnapshot implements Snapshot { private AtomicMRSWRegister[] register; public void update(int value) { int i = Thread.myIndex(); LabeledValue oldValue = register[i].read(); LabeledValue newValue = new LabeledValue(oldValue.label+1, value); register[i].write(newValue); } (1) One single-writer register per thread

© 2007 Herlihy & Shavit108 Simple Snapshot: Update public class SimpleSnapshot implements Snapshot { private AtomicMRSWRegister[] register; public void update(int value) { int i = Thread.myIndex(); LabeledValue oldValue = register[i].read(); LabeledValue newValue = new LabeledValue(oldValue.label+1, value); register[i].write(newValue); } (1) Write each time with higher label

© 2007 Herlihy & Shavit109 Simple Snapshot: Collect private LabeledValue[] collect() { LabeledValue[] copy = new LabeledValue[n]; for (int j = 0; j < n; j++) copy[j] = this.register[j].read(); return copy; } (1)

© 2007 Herlihy & Shavit110 Simple Snapshot private LabeledValue[] collect() { LabeledValue[] copy = new LabeledValue[n]; for (int j = 0; j < n; j++) copy[j] = this.register[j].read(); return copy; } (1) Just read each register into array

© 2007 Herlihy & Shavit111 Simple Snapshot: Scan public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); }}} (1)

© 2007 Herlihy & Shavit112 Simple Snapshot: Scan public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); }}} (1) Collect once

© 2007 Herlihy & Shavit113 Simple Snapshot: Scan public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect;}} return getValues(newCopy);}}}public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect;}} return getValues(newCopy);}}}} (1) Collect once Collect twice

© 2007 Herlihy & Shavit114 Simple Snapshot: Scan public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); }}} (1) Collect once Collect twice On mismatch, try again

© 2007 Herlihy & Shavit115 Simple Snapshot: Scan public int[] scan() { LabeledValue[] oldCopy, newCopy; oldCopy = collect(); collect: while (true) { newCopy = collect(); if (!equals(oldCopy, newCopy)) { oldCopy = newCopy; continue collect; }} return getValues(newCopy); }}} (1) Collect once Collect twice On match, return values

© 2007 Herlihy & Shavit116 Simple Snapshot Linearizable Update is wait-free –No unbounded loops But Scan can starve –If interrupted by concurrent update

© 2007 Herlihy & Shavit117 Wait-Free Snapshot Add a scan before every update Write resulting snapshot together with update value If scan is continuously interrupted by updates, scan can take the update’s snapshot

© 2007 Herlihy & Shavit118 Wait-free Snapshot If A’s scan observes that B moved twice, then B completed an update while A’s scan was in progress time Update B ≠ ≠ Collect Collect Collect

© 2007 Herlihy & Shavit119 Wait-free Snapshot time ≠ ≠ Collect Collect Collect Update A B

© 2007 Herlihy & Shavit120 Wait-free Snapshot time ≠ ≠ Collect Collect Collect A B ScanWrite Update

© 2007 Herlihy & Shavit121 Wait-free Snapshot time ≠ ≠ Collect Collect Collect A B ScanWrite Update Scan Write B’s 1 st update must have written during 1 st collect So scan of B’s second update must be within interval of A’s scan So A can steal result of B’s scan

© 2007 Herlihy & Shavit122 Wait-free Snapshot time ≠ ≠ Collect Collect Collect A B ScanWrite Scan Write But no guarantee that scan of B’s 1 st update can be used… Why?

© 2007 Herlihy & Shavit123 Once is not Enough time ≠ Collect Collect Update A B ScanWrite Why can’t A steal result of B’s scan Because another update might have interfered before the scan Update

© 2007 Herlihy & Shavit124 Someone Must Move Twice time Update B ≠ ≠ Collect Collect Collect If we collect n times…some thread Must move twice (Pigeon hole)

© 2007 Herlihy & Shavit125 Scan is Wait-free scan update So some thread must have had clean collect scan update scan At most n-1 depth

© 2007 Herlihy & Shavit126 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2)

© 2007 Herlihy & Shavit127 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2) Counter incremented with each snapshot

© 2007 Herlihy & Shavit128 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2) Actual value

© 2007 Herlihy & Shavit129 Wait-Free Snapshot Label public class SnapValue { public int label; public int value; public int[] snap; } (2) most recent snapshot

© 2007 Herlihy & Shavit130 Wait-Free Snapshot Label …00 label value Last snapshot (3)

© 2007 Herlihy & Shavit131 Wait-free Update public void update(int value) { int i = Thread.myIndex(); int[] snap = this.scan(); SnapValue oldValue = r[i].read(); SnapValue newValue = new SnapValue(oldValue.label+1, value, snap); r[i].write(newValue); } (2)

© 2007 Herlihy & Shavit132 Wait-free Scan public void update(int value) { int i = Thread.myIndex(); int[] snap = this.scan(); SnapValue oldValue = r[i].read(); SnapValue newValue = new SnapValue(oldValue.label+1, value, snap); r[i].write(newValue); } public void update(int value) { int i = Thread.myIndex(); int[] snap = this.scan(); SnapValue oldValue = r[i].read(); SnapValue newValue = new SnapValue(oldValue.label+1, value, snap); r[i].write(newValue); } (2) Take scan

© 2007 Herlihy & Shavit133 Wait-free Scan public void update(int value) { int i = Thread.myIndex(); int[] snap = this.scan(); SnapValue oldValue = r[i].read(); SnapValue newValue = new SnapValue(oldValue.label+1, value, snap); r[i].write(newValue); } (2) Take scan Label value with scan

© 2007 Herlihy & Shavit134 Wait-free Scan public int[] scan() { SnapValue[] oldCopy, newCopy; boolean[] moved = new boolean[n]; oldCopy = collect(); collect: while (true) { newCopy = collect(); for (int j = 0; j < n; j++) { if (oldCopy[j].label != newCopy[j].label) { … }} return getValues(newCopy); }}} (2)

© 2007 Herlihy & Shavit135 Wait-free Scan public int[] scan() { SnapValue[] oldCopy, newCopy; boolean[] moved = new boolean[n]; oldCopy = collect(); collect: while (true) { newCopy = collect(); for (int j = 0; j < n; j++) { if (oldCopy[j].label != newCopy[j].label) { … }} return getValues(newCopy); }}} (2) Keep track of who moved

© 2007 Herlihy & Shavit136 Wait-free Scan public int[] scan() { SnapValue[] oldCopy, newCopy; boolean[] moved = new boolean[n]; oldCopy = collect(); collect: while (true) { newCopy = collect(); for (int j = 0; j < n; j++) { if (oldCopy[j].label != newCopy[j].label) { … }} return getValues(newCopy); }}} (2) Repeated double collect

© 2007 Herlihy & Shavit137 Wait-free Scan public int[] scan() { SnapValue[] oldCopy, newCopy; boolean[] moved = new boolean[n]; oldCopy = collect(); collect: while (true) { newCopy = collect(); for (int j = 0; j < n; j++) { if (oldCopy[j].label != newCopy[j].label) { … }} return getValues(newCopy); }}} (2) If mismatch detected…lets expand here…

© 2007 Herlihy & Shavit138 Mismatch Detected if (oldCopy[j].label != newCopy[j].label) { if (moved[j]) {// second move return newCopy[j].snap; } else { moved[j] = true; oldCopy = newCopy; continue collect; }}} return getValues(newCopy); }}} (2)

© 2007 Herlihy & Shavit139 Mismatch Detected if (oldCopy[j].label != newCopy[j].label) { if (moved[j]) { return newCopy[j].snap; } else { moved[j] = true; oldCopy = newCopy; continue collect; }}} return getValues(newCopy); }}} If thread moved twice, just steal its second snapshot (2)

© 2007 Herlihy & Shavit140 Mismatch Detected if (oldCopy[j].label != newCopy[j].label) { if (moved[j]) {// second move return newCopy[j].snap; } else { moved[j] = true; oldCopy = newCopy; continue collect; }}} return getValues(newCopy); }}} (2) Remember that thread moved

© 2007 Herlihy & Shavit141 Observations Uses unbounded counters –can be replaced with 2 bits Assumes SWMR registers –for labels –can be extended to MRMW

© 2007 Herlihy & Shavit142 Summary We saw we could implement MRMW multi valued snapshot objects From SRSW binary safe registers (simple flipflops) But what is the next step to attempt with read-write registers?

© 2007 Herlihy & Shavit143 Grand Challenge Snapshot means –Write any one array element –Read multiple array elements

© 2007 Herlihy & Shavit144 Grand Challenge Writes to 0 and 1 Writes to 1 and 2 What about atomic writes to multiple locations? Write many and snapshot

Art of Multiprocessor Programming 145 This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.Creative Commons Attribution-ShareAlike 2.5 License Y o u a r e f r e e : –to Share — to copy, distribute and transmit the work –to Remix — to adapt the work U n d e r t h e f o l l o w i n g c o n d i t i o n s : –Attribution. You must attribute the work to “The Art of Multiprocessor Programming” (but not in any way that suggests that the authors endorse you or your use of the work). –Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. F o r a n y r e u s e o r d i s t r i b u t i o n, y o u m u s t m a k e c l e a r t o o t h e r s t h e l i c e n s e t e r m s o f t h i s w o r k. T h e b e s t w a y t o d o t h i s i s w i t h a l i n k t o – A n y o f t h e a b o v e c o n d i t i o n s c a n b e w a i v e d i f y o u g e t p e r m i s s i o n f r o m t h e c o p y r i g h t h o l d e r. N o t h i n g i n t h i s l i c e n s e i m p a i r s o r r e s t r i c t s t h e a u t h o r ' s m o r a l r i g h t s.