Programming Paradigms for Concurrency Introduction

Similar documents
Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit. Art of Multiprocessor Programming

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Introduction to Concurrent Programming

Introduction to Concurrent Programming

Introduction to Multiprocessor Synchronization

COMP 322: Principles of Parallel Programming Lecture 11: Parallel Programming Issues (Chapter 6) Fall 2009

Introduction. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Lecture 7: Mutual Exclusion 2/16/12. slides adapted from The Art of Multiprocessor Programming, Herlihy and Shavit

Introduction. Chapter 1

Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Spin Locks and Contention

Linked Lists: Locking, Lock-Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Concurrent Skip Lists. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Locks Chapter 4. Overview. Introduction Spin Locks. Queue locks. Roger Wattenhofer. Test-and-Set & Test-and-Test-and-Set Backoff lock

Spin Locks and Contention. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Mutual Exclusion. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Linked Lists: Locking, Lock- Free, and Beyond. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Introduction to Concurrency and Multicore Programming. Slides adapted from Art of Multicore Programming by Herlihy and Shavit

Coarse-grained and fine-grained locking Niklas Fors

Locking. Part 2, Chapter 11. Roger Wattenhofer. ETH Zurich Distributed Computing

Distributed Computing through Combinatorial Topology MITRO207, P4, 2017

Programming Paradigms for Concurrency Lecture 2 - Mutual Exclusion

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Shared-Memory Computability

Universality of Consensus. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

What We'll Cover Today

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Hashing and Natural Parallism. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

CMSC Computer Architecture Lecture 12: Multi-Core. Prof. Yanjing Li University of Chicago

G52CON: Concepts of Concurrency

Kernel Synchronization I. Changwoo Min

Spin Locks and Contention. Companion slides for Chapter 7 The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

An Introduction to Parallel Programming

Modern High-Performance Locking

IT 540 Operating Systems ECE519 Advanced Operating Systems

Agenda. Lecture. Next discussion papers. Bottom-up motivation Shared memory primitives Shared memory synchronization Barriers and locks

Other consistency models

Peterson s Algorithm

THREADS & CONCURRENCY

COL 380: Introduc1on to Parallel & Distributed Programming. Lecture 1 Course Overview + Introduc1on to Concurrency. Subodh Sharma

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Concurrent Computing

Process Management And Synchronization

Parallel Programming: Background Information

Parallel Programming: Background Information

Concurrent Queues, Monitors, and the ABA problem. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

Programming at Scale: Concurrency

Multiprocessor Systems. Chapter 8, 8.1

Chapter 5: Thread-Level Parallelism Part 1

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Arvind Krishnamurthy Fall Collection of individual computing devices/processes that can communicate with each other

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

Computer Architecture Crash course

Interprocess Communication By: Kaushik Vaghani

Introduction to Concurrency Principles of Concurrent System Design

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Synchronization in Concurrent Programming. Amit Gupta

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 6: Process Synchronization

Midterm Exam Amy Murphy 19 March 2003

Concurrent Preliminaries

Operating Systems. Synchronization

Lecture 13: Memory Consistency. + a Course-So-Far Review. Parallel Computer Architecture and Programming CMU , Spring 2013

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

CS370 Operating Systems

Computer Architecture

18-447: Computer Architecture Lecture 30B: Multiprocessors. Prof. Onur Mutlu Carnegie Mellon University Spring 2013, 4/22/2013

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Timers 1 / 46. Jiffies. Potent and Evil Magic

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Design and Evaluation of Scalable Software

Threading and Synchronization. Fahd Albinali

Shared Memory Synchronization

What is uop Cracking?

Serial. Parallel. CIT 668: System Architecture 2/14/2011. Topics. Serial and Parallel Computation. Parallel Computing

Locking Granularity. CS 475, Spring 2019 Concurrent & Distributed Systems. With material from Herlihy & Shavit, Art of Multiprocessor Programming

Computer Architecture: Parallel Processing Basics. Prof. Onur Mutlu Carnegie Mellon University

Introduction to OS Synchronization MOS 2.3

Dealing with Issues for Interprocess Communication

Concurrency Terminology

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Concurrent Objects. Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

The concept of concurrency is fundamental to all these areas.

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

2 Threads vs. Processes

CPSC/ECE 3220 Summer 2017 Exam 2

a. A binary semaphore takes on numerical values 0 and 1 only. b. An atomic operation is a machine instruction or a sequence of instructions

Multiprocessors - Flynn s Taxonomy (1966)

Online Course Evaluation. What we will do in the last week?

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Multiprocessors & Thread Level Parallelism

Transcription:

Programming Paradigms for Concurrency Introduction Based on companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Thomas Wies New York University

Moore s Law Transistor count still rising Clock speed flattening sharply 2

Moore s Law (in practice) 3

Nearly Extinct: the Uniprocesor cpu memory 4

Endangered: The Shared Memory Multiprocessor (SMP) cache cache Bus cache Bus shared memory 5

All on the same chip The New Boss: The Multicore Processor cache (CMP) cache Bus cache shared memory Bus NVidia Tegra 4 with ARM Cortex- A15 6

Why is Mooly Eden Smiling? 7

Traditional Scaling Process Speedup 1.8x 3.6x 7x User code Traditional Uniprocessor Time: Moore s law 9

Ideal Scaling Process Speedup 1.8x 3.6x 7x User code Multicore Unfortunately, not so simple 10

Actual Scaling Process Speedup 1.8x 2x 2.9x User code Multicore Parallelization and Synchronization require great care 11

Course Overview Fundamentals models, algorithms, impossibility Real-World programming architectures synchronization primitives spin-locks, monitors, barriers paradigms and techniques shared memory concurrency transactional memories message passing 12

Languages Used Java shared memory concurrency Scala software transactional memory message passing (actors) 13

Administrative Issues Office Hours: Tue 3-4pm, or by appointment Office: CIWW 407 Course web site: http://cs.nyu.edu/wies/teaching/ppc-14 Mailing list: csci_ga_3033_014_sp14@cs.nyu.edu 14

Grading Weekly Assignments: 30% Term Project: 30% Final Exam: 40% 15

Sequential Computation thread memory object object 16

Concurrent Computation memory object object 17

Asynchrony Sudden unpredictable delays Cache misses (short) Page faults (long) Scheduling quantum used up (really long) 18

Model Summary Multiple threads Sometimes called processes Single shared memory Objects live in memory Unpredictable asynchronous delays 19

Road Map We are going to focus on principles first, then practice Start with idealized models Look at simplistic problems Emphasize correctness over pragmatism Correctness may be theoretical, but incorrectness has practical impact 20

Toyota Unintended Acceleration Incidents (2009-2010) at least one fatal accident more than 9 million vehicles recalled In a 2013 court case, unprotected critical variables in Toyota s real-time OS were linked to the incidents 21

Concurrency Jargon Hardware Processors Software Threads, processes Sometimes OK to confuse them, sometimes not. 22

Parallel Primality Testing Challenge Print primes from 1 to 10 10 Given Ten-processor multiprocessor One thread per processor Goal Get ten-fold speedup (or close) 23

Load Balancing 1 10 9 2 10 9 10 10 P 0 P 1 P 9 Split the work evenly Each thread tests range of 10 9 24

Procedure for Thread i void primeprint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*10 9 +1, j<(i+1)*10 9 ; j++) { if (isprime(j)) print(j); } } 25

Issues Higher ranges have fewer primes Yet larger numbers harder to test Thread workloads Uneven Hard to predict 26

Issues Higher ranges have fewer primes Yet larger numbers harder to test Thread workloads Uneven Hard to predict Need dynamic load balancing 27

Shared Counter 19 18 each thread takes a number 17 28

Procedure for Thread i int counter = new Counter(1); void primeprint { long j = 0; while (j < 10 10 ) { j = counter.getandincrement(); if (isprime(j)) print(j); } } 29

Procedure for Thread i Counter counter = new Counter(1); void primeprint { long j = 0; while (j < 10 10 ) { j = counter.getandincrement(); } } if (isprime(j)) print(j); Shared counter object 30

Where Things Reside void primeprint { int i = ThreadID.get(); // IDs in {0..9} for (j = i*10 9 +1, j<(i+1)*10 9 ; j++) { if (isprime(j)) print(j); } } code Local variables cache cache Bus cache Bus 1 shared memory shared counter 31

Procedure for Thread i Counter counter = new Counter(1); void primeprint { long j = 0; while (j < 10 10 ) { j = counter.getandincrement(); if (isprime(j)) print(j); } } Stop when every value taken 32

Procedure for Thread i Counter counter = new Counter(1); void primeprint { long j = 0; while (j < 10 10 ) { j = counter.getandincrement(); if (isprime(j)) print(j); } } Increment & return each new value 33

Counter Implementation public class Counter { private long value; } public long getandincrement() { return value++; } 34

Counter Implementation public class Counter { private long value; } public long getandincrement() { return value++; } 35

What It Means public class Counter { private long value; } public long getandincrement() { return value++; } 36

What It Means public class Counter { private long value; } public long getandincrement() { return value++; temp = value; } value = temp + 1; return temp; 37

Not so good Value 1 2 3 2 read 1 write 2 read 2 write 3 read 1 write 2 time 38

Is this problem inherent?!!!! read write write read If we could only glue reads and writes together 39

Challenge public class Counter { private long value; } public long getandincrement() { temp = value; value = temp + 1; return temp; } 40

Challenge public class Counter { private long value; } public long getandincrement() { temp = value; value = temp + 1; return temp; } Make these steps atomic (indivisible) 41

Hardware Solution public class Counter { private long value; public long getandincrement() { temp = value; value = temp + 1; return temp; } } ReadModifyWrite() instruction 42

An Aside: Java public class Counter { private long value; } public long getandincrement() { synchronized { temp = value; value = temp + 1; } return temp; } 43

An Aside: Java public class Counter { private long value; } public long getandincrement() { synchronized { temp = value; value = temp + 1; } return temp; } Synchronized block 44

An Aside: Java public class Counter { private long value; } Mutual Exclusion public long getandincrement() { synchronized { temp = value; value = temp + 1; } return temp; } 45

Mutual Exclusion, or Alice & Bob share a pond A B 46

Alice has a pet A B 47

Bob has a pet A B 48

The Problem A B The pets don t get along 49

Formalizing the Problem Two types of formal properties in asynchronous computation: Safety Properties Nothing bad happens ever Liveness Properties Something good happens eventually 50

Formalizing our Problem Mutual Exclusion Both pets never in pond simultaneously This is a safety property No Deadlock if only one wants in, it gets in if both want in, one gets in. This is a liveness property 51

Simple Protocol Idea Just look at the pond Gotcha Not atomic Trees obscure the view 52

Interpretation Threads can t see what other threads are doing Explicit communication required for coordination 53

Cell Phone Protocol Idea Bob calls Alice (or vice-versa) Gotcha Bob takes shower Alice recharges battery Bob out shopping for pet food 54

Interpretation Message-passing doesn t work Recipient might not be Listening There at all Communication must be Persistent (like writing) Not transient (like speaking) 55

cola cola Can Protocol 56

cola Bob conveys a bit A B 57

Bob conveys a bit A B 58

Can Protocol Idea Cans on Alice s windowsill Strings lead to Bob s house Bob pulls strings, knocks over cans Gotcha Cans cannot be reused Bob runs out of cans 59

Interpretation Cannot solve mutual exclusion with interrupts Sender sets fixed bit in receiver s space Receiver resets bit when ready Requires unbounded number of interrupt bits 60

Flag Protocol A B 61

Alice s Protocol (sort of) A B 62

Bob s Protocol (sort of) A B 63

Alice s Protocol Raise flag Wait until Bob s flag is down Unleash pet Lower flag when pet returns 64

Bob s Protocol Raise flag Wait until Alice s flag is down Unleash pet Lower flag when pet returns 65

Bob s Protocol (2 nd try) Raise flag While Alice s flag is up Lower flag Wait for Alice s flag to go down Raise flag Unleash pet Lower flag when pet returns 66

Bob s Protocol Bob defers to Raise flag Alice While Alice s flag is up Lower flag Wait for Alice s flag to go down Raise flag Unleash pet Lower flag when pet returns 67

The Flag Principle Raise the flag Look at other s flag Flag Principle: If each raises and looks, then Last to look must see both flags up 68

Proof of Mutual Exclusion Assume both pets in pond Derive a contradiction By reasoning backwards Consider the last time Alice and Bob each looked before letting the pets in Without loss of generality assume Alice was the last to look 69

Proof Bob last raised flag Alice last raised her flag Alice s last look Bob s last look time Alice must have seen Bob s Flag. A Contradiction 70

Proof of No Deadlock If only one pet wants in, it gets in. 71

Proof of No Deadlock If only one pet wants in, it gets in. Deadlock requires both continually trying to get in. 72

Proof of No Deadlock If only one pet wants in, it gets in. Deadlock requires both continually trying to get in. If Bob sees Alice s flag, he gives her priority (a gentleman ) 73

Remarks Protocol is unfair Bob s pet might never get in Protocol uses waiting If Bob is eaten by his pet, Alice s pet might never get in 74

Moral of Story Mutual Exclusion cannot be solved by transient communication (cell phones) interrupts (cans) It can be solved by one-bit shared variables that can be read or written 75

The Arbiter Problem (an aside) Pick a point Pick a point 76

The Fable Continues Alice and Bob fall in love & marry 77

The Fable Continues Alice and Bob fall in love & marry Then they fall out of love & divorce She gets the pets He has to feed them 78

The Fable Continues Alice and Bob fall in love & marry Then they fall out of love & divorce She gets the pets He has to feed them Leading to a new coordination problem: Producer-Consumer 79

Bob Puts Food in the Pond A 80

Alice releases her pets to Feed mmm mmm B 81

Producer/Consumer Alice and Bob can t meet Each has restraining order on other So he puts food in the pond And later, she releases the pets Avoid Releasing pets when there s no food Putting out food if uneaten food remains 82

Producer/Consumer Need a mechanism so that Bob lets Alice know when food has been put out Alice lets Bob know when to put out more food 83

cola Surprise Solution A B 84

cola Bob puts food in Pond A B 85

Bob knocks over Can A B 86

Alice Releases Pets A yum yum B 87

cola Alice Resets Can when Pets are Fed A B 88

Pseudocode while (true) { while (can.isup()){}; pet.release(); pet.recapture(); can.reset(); } Alice s code 89

Pseudocode while (true) { while (can.isup()){}; pet.release(); pet.recapture(); can.reset(); while (true) { } while (can.isdown()){}; pond.stockwithfood(); can.knockover(); } Alice s code Bob s code 90

Mutual Exclusion Correctness Pets and Bob never together in pond 91

Mutual Exclusion Correctness Pets and Bob never together in pond No Starvation if Bob always willing to feed, and pets always famished, then pets eat infinitely often. 92

Correctness Mutual Exclusion Pets and Bob never together in pond No Starvation if Bob always willing to feed, and pets always famished, then pets eat infinitely often. safety liveness safety Producer/Consumer The pets never enter pond unless there is food, and Bob never provides food if there is unconsumed food. 93

Could Also Solve Using Flags A B 94

Waiting Both solutions use waiting while(mumble){} In some cases waiting is problematic If one participant is delayed So is everyone else But delays are common & unpredictable 95

The Fable drags on Bob and Alice still have issues 96

The Fable drags on Bob and Alice still have issues So they need to communicate 97

The Fable drags on Bob and Alice still have issues So they need to communicate They agree to use billboards 98

Billboards are Large B A 3 C 1 3 D 2 E 1 Letter Tiles From Scrabble box 99

Write One Letter at a Time W 4 A 1 S 1 H 4 B A 3 C 1 3 D 2 E 1 100

To post a message W A S H T H E C A R 4 1 1 4 1 4 1 3 1 1 whew 101

Let s send another message L S E L L L A V A A 1 1 1 1 1 1 1 1 4 1 M P 3 3 S 1 102

Uh-Oh S 1 E 1 L 1 L 1 T 1 H 4 E 1 C A R 3 1 1 L 1 OK 103

Readers/Writers Devise a protocol so that Writer writes one letter at a time Reader reads one letter at a time Reader sees snapshot Old message or new message No mixed messages 104

Readers/Writers (continued) Easy with mutual exclusion But mutual exclusion requires waiting One waits for the other Everyone executes sequentially Remarkably We can solve R/W without mutual exclusion 105

Esoteric? Java container size() method Single shared counter? incremented with each add() and decremented with each remove() Threads wait to exclusively access counter 106

Readers/Writers Solution Each thread i has size[i] counter only it increments or decrements. To get object s size, a thread reads a snapshot of all counters This eliminates the bottleneck 107

Why do we care? We want as much of the code as possible to execute concurrently (in parallel) A larger sequential part implies reduced performance Amdahl s law: this relation is not linear 108

Amdahl s Law Speedup= 1-thread execution time n-thread execution time 109

Amdahl s Law Speedup= 1 1 p + p n 110

Amdahl s Law 1 Parallel fraction Speedup= 1 p + p n 111

Amdahl s Law Sequential fraction Speedup= 1 Parallel fraction 1 p + p n 112

Amdahl s Law Sequential fraction Speedup= 1 Parallel fraction 1 p + p n Number of threads 113

Amdahl s Law (in practice) 114

Example Ten processors 60% concurrent, 40% sequential How close to 10-fold speedup? 115

Example Ten processors 60% concurrent, 40% sequential How close to 10-fold speedup? Speedup = 2.17= 1 1 0.6 0.6 10 116

Example Ten processors 80% concurrent, 20% sequential How close to 10-fold speedup? 117

Example Ten processors 80% concurrent, 20% sequential How close to 10-fold speedup? Speedup = 3.57= 1 1 0.8 0.8 10 118

Example Ten processors 90% concurrent, 10% sequential How close to 10-fold speedup? 119

Example Ten processors 90% concurrent, 10% sequential How close to 10-fold speedup? Speedup = 5.26= 1 1 0.9 0.9 10 120

Example Ten processors 99% concurrent, 01% sequential How close to 10-fold speedup? 121

Example Ten processors 99% concurrent, 01% sequential How close to 10-fold speedup? Speedup = 9.17= 1 1 0.99 0.99 10 122

Back to Real-World Multicore Scaling Speedup 1.8x 2x 2.9x User code Multicore Not reducing sequential % of code 123

Shared Data Structures Coarse Grained 25% Shared Fine Grained 25% Shared 75% Unshared 75% Unshared 124

Shared Data Structures Honk! Honk! Honk! Why only 2.9 speedup Coarse Grained 25% Shared Fine Grained 25% Shared 75% Unshared 75% Unshared 125

Honk! Shared Data Structures Honk! Honk! Why fine-grained parallelism maters Coarse Grained 25% Shared Fine Grained 25% Shared 75% Unshared 75% Unshared 126

This work is licensed under a Creative Commons Attribution- ShareAlike 2.5 License. You are free: to Share to copy, distribute and transmit the work to Remix to adapt the work Under the following conditions: 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. For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to http://creativecommons.org/licenses/by-sa/3.0/. Any of the above conditions can be waived if you get permission from the copyright holder. Nothing in this license impairs or restricts the author's moral rights. 127