Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 3: Introduction.

Slides:



Advertisements
Similar presentations
Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit TexPoint fonts used in EMF. Read the TexPoint manual.
Advertisements

The Art of Multiprocessor Programming Nir Shavit, Ori Shalev CS Spring 2007 (Based on the book by Herlihy and Shavit)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Mutual Exclusion.
Mutual Exclusion The Art of Multiprocessor Programming Spring 2007.
Concurrency The need for speed. Why concurrency? Moore’s law: 1. The number of components on a chip doubles about every 18 months 2. The speed of computation.
Introduction Companion slides for
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrency CS 510: Programming Languages David Walker.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 2: Introduction.
Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Modified by Rajeev Alur for CIS 640 at Penn, Spring.
Introduction to Multiprocessor Synchronization Maurice Herlihy TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAA.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 © R. Guerraoui Seth Gilbert Professor: Rachid Guerraoui Assistants: M. Kapalka and A. Dragojevic Distributed Programming Laboratory.
Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Multi-core Programming Introduction Topics. Topics General Ideas Moore’s Law Amdahl's Law Processes and Threads Concurrency vs. Parallelism.
Lecture 1: Performance EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2013, Dr. Rozier.
Multicore Programming Nir Shavit Tel Aviv University.
1 © R. Guerraoui Concurrent Algorithms (Overview) Prof R. Guerraoui Distributed Programming Laboratory.
Introduction to Multiprocessor Synchronization Maurice Herlihy TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAA.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit TexPoint fonts used in EMF. Read the TexPoint manual.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Multicore Programming Summer Daily Schedule 09:00am - 10:30am – Lecture 10:30am - 10:50pm - Break 10:50am - 12:20pm - Lecture 12:20pm - 01:20pm.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Lecture #21 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the.
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
Introduction Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the.
Lecture 27 Multiprocessor Scheduling. Last lecture: VMM Two old problems: CPU virtualization and memory virtualization I/O virtualization Today Issues.
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.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
Agenda  Quick Review  Finish Introduction  Java Threads.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Concurrency Idea. 2 Concurrency idea Challenge –Print primes from 1 to Given –Ten-processor multiprocessor –One thread per processor Goal –Get ten-fold.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Multiprocessor Synchronization Algorithms ( )
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Introduction to Multiprocessor Synchronization
Multiprocessor Synchronization
CS Multiprocessor Synchronization
CSCI1600: Embedded and Real Time Software
Introduction Companion slides for
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
CSCI1600: Embedded and Real Time Software
并发算法与理论 Concurrency: Algorithms and Theories
Presentation transcript:

Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 3: Introduction

2 Material From The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit

3 Moore‘s Law Clock speed flattening sharply Transistor count still rising

4 Uniprocessor memory cpu

5 Shared Memory Multiprocessor (SMP) cache Bus shared memory cache

6 Multicore Processor (CMP) Sun T2000 Niagara All on the same chip

7 Why do we care about multicore processors? Time no longer cures software bloat The “free ride” is over When you double your program’s path length You can’t just wait 6 months Your software must somehow exploit twice as much concurrency

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

9 Multicore Scaling Process User code Multicore Speedup 1.8x7x3.6x Unfortunately, not so simple…

10 Real-World Scaling Process 1.8x 2x 2.9x User code Multicore Speedup Parallelization and Synchronization require great care…

11 Sequential Computation memory object thread

12 Concurrent Computation memory object

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

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

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

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

17 Load Balancing Split the work evenly Each thread tests range of 10 9 … … · P0P0 P1P1 P9P9

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

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

20 Shared Counter each thread takes a number

21 Procedure for Thread i Counter counter = new Counter(1); void primePrint { long j = 0; while (j < ) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); } Shared counter object

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

23 Procedure for Thread i Counter counter = new Counter(1); void primePrint { long j = 0; while (j < ) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); } Stop when every value taken Increment & return each new value

24 Counter Implementation public class Counter { private long value; public long getAndIncrement() { return value++; } OK for single thread, not for concurrent threads temp = value; value = temp + 1; return temp;

25 Not so good… time Value… 1 read 1 read 1 write 2 read 2 write 3 write 2 232

26 Is this problem inherent? If we could only glue reads and writes… read write read write

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

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

29 An Aside: Java™ public class Counter { private long value; public long getAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } Synchronized block Mutual Exclusion

30 Mutual Exclusion or “Alice & Bob share a pond” AB

31 Alice has a pet AB

32 Bob has a pet AB

33 The Problem AB The pets don’t get along

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

35 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

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

37 Interpretation Threads can’t “see” what other threads are doing Explicit communication required for coordination

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

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

40 Can Protocol cola

41 Bob conveys a bit AB cola

42 Bob conveys a bit AB cola

43 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

44 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

45 Flag Protocol AB

46 Alice’s Protocol (sort of) AB

47 Bob’s Protocol (sort of) AB

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

49 Bob’s Protocol Raise flag Wait until Alice’s flag is down Unleash pet Lower flag when pet returns danger!

50 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 Bob defers to Alice

51 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

52 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…

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

54 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…) QED

55 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

56 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

57 The Arbiter Problem (an aside) Pick a point

58 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

59 Bob Puts Food in the Pond A

60 Alice releases her pets to Feed mmm… B

61 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 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

62 Surprise Solution AB cola

63 Bob puts food in Pond AB cola

64 Bob knocks over Can AB cola

65 Alice Releases Pets AB cola yum… B

66 Alice Resets Can when Pets are Fed AB cola

67 Pseudocode while (true) { while (can.isUp()){}; pet.release(); pet.recapture(); can.reset(); } Alice’s code while (true) { while (can.isDown()){}; pond.stockWithFood(); can.knockOver(); } Bob’s code

68 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. Producer/Consumer The pets never enter pond unless there is food, and Bob never provides food if there is unconsumed food. safety liveness safety

69 Could Also Solve Using Flags AB

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

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

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

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

74 To post a message W 4 A 1 S 1 H 4 A 1 C 3 R 1 T 1 H 4 E 1 whe w

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

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

77 Readers/Writers Devise a protocol so that Writer writes one letter at a time Reader reads one letter at a time Reader sees Old message or new message No mixed messages Easy with mutual exclusion But mutual exclusion requires waiting One waits for the other Everyone executes sequentially Remarkably We can solve the problem without mutual exclusion

78 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…

79 Amdahl’s Law …of computation given n CPUs instead of 1

80 Amdahl’s Law Parallel fraction Sequential fraction Number of processors

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

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

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

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

85 The Moral Making good use of our multiple processors (cores) means finding ways to effectively parallelize our code Minimize sequential parts Reduce idle time in which threads wait without doing something useful.