30
1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the text explanations given in class.

1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

Embed Size (px)

DESCRIPTION

3 Concurrent Programming object Shared Memory Challenge: coordinating access

Citation preview

Page 1: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

1

Lecture #24Shared Objects and Concurrent

Programming

This material is not available in the textbook. The online powerpoint presentations contain the text explanations given in class.

Page 2: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

2

A Multiprocessor Machine

P1

P2

P10

...

P1 memory

Shared memory

Uniprocessor

Multiprocessor

Page 3: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

3

Concurrent Programming

object

object

Shared Memory

Challenge: coordinating access

Page 4: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

4

Persistent vs Transient Communication

•Persistent Communication medium: the sending of information changes the state of the medium forever.Example: Blackboard.

•Transient communication medium: the change of state is only for some limited time period.Example: Talking.

Page 5: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

5

Parallel Primality Testing

Task: Print all primes from 1 to 1010 in some order Avaliable: A machine with 10 processors

Solution: Speed work up 10 times, that is, new time to print all primes will be 1/10 of time for single processor

Page 6: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

6

Parallel Primality Testing

P1 P2 P10

1 109 2x109 1010

Split the work among processors!

Each processor Pi gets 109 numbers to test.

Page 7: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

7

Parallel Primality Testing

(define (P i) (let ((counter (+ 1 (* (- i 1) (power 10 9)))) (upto (* i (power 10 9)))) (define (iter) (if (< counter upto) (begin (if (prime? counter) (display counter) #f) (increment-counter) (iter)) 'done)) (iter)))

(parallel-execute (P 1) (P 2) ... (P 10))

Page 8: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

8

Problem: work is split unevenly

Some processors have less primes to test… Some composite numbers are easier to test…

P1 P2 P10

1 109 2x109 1010

Need to split the work range dynamically!

Page 9: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

9

A Shared Counter Object

(define (make-shared-counter value) (define (fetch) value) (define (increment) (set! value (+ 1 value)) (define (dispatch m) (cond (((eq? m 'fetch) (fetch)) (eq? m 'increment) (increment)) (else (error “unknown request”)))) dispatch)

(define shared-counter (make-shared-counter 1))

Page 10: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

10

Using the Shared Counter

(define (P i) (define (iter) (let ((index (shared-counter 'fetch))) (if (< index (power 10 10)) (begin (if (prime? index) (display index) #f) (shared-counter 'increment) (iter)) 'done)) (iter)))

(parallel-execute (P 1) (P 2) ... (P 10))

Page 11: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

11

This Solution Doesn’t Work

time

Increment: (set! value (+ 1 value))

P1 read value77

77

P2 increment 10 times

87 set! value78 Error!

(let ((index (shared-counter 'fetch)))

77P1 fetch

P2 fetch

77

77Error!

Page 12: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

12

It Will Never Work!?

Real World Example: Walking in the Street

Look / Move Fetch / Increment

Computers: Accessing Memory

Fischer Lynch & Patterson: Impossible to solve!!!

Need to “glue” the Fetch / Increment pair into one indivisible operation: Fetch-and-Increment

Page 13: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

13

The Fetch-and-Increment Operation

(define (make-shared-counter value) (define (fetch-and-increment) (let ((old value)) (set! value (+ old 1)) old)) (define (dispatch m) (cond (((eq? m 'fetch-and-increment) (fetch-and-increment)) (else (error ``unknown request -- counter'' m)))) dispatch) 

Instanteneous

Shared CounterFetch-and-inc

Page 14: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

14

A Correct Shared Counter

(define shared-counter (make-shared-counter 1))(define (P i) (define (iter) (let ((index (shared-counter 'fetch-and-increment))) (if (< index (power 10 10)) (begin (if (prime? index) (display index) #f) (iter)) 'done)) (iter))) (parallel-execute (P 1) (P 2) ... (P 10))

Page 15: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

15

Implementing Fetch-and-Inc

To make the program work we need an “intantaneous” implementation of fetch-and-increment. How can we do this:

• Special Hardware. Built-in synchronization instructions. • Special Software. Use regular instructions -- the solution

will involve waiting.

Software: Mutual Exclusion

Page 16: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

16

Mutual Exclusion

(mutex 'start) (let ((old value)) (set! value (+ old 1)) old)(mutex 'end))

Only one process at a time can execute these instructions

P1

P2

P10

...11 P2

returns 1Mutex count

Page 17: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

17

The Story of Alice and Bob

Bob Alice

Yard

* As told by Leslie Lamport

Page 18: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

18

The Mutual Exclusion Problem

Requirements: • Mutual Exclusion: there will never be two dogs

simultaneously in the yard.• No Deadlock: if only one dog wants to be in the yard it will

succeed, and if both dogs want to go out, at least one of them will succeed.

Page 19: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

19

Cell Phone Solution

Bob Alice

Yard

Page 20: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

20

Coke Can Solution

Bob Alice

Yard

Page 21: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

21

Flag Solution -- Alice

(define (Alice) (loop ;; ``repeat forever'' (set! Alice-flag 'up) ;; Alice wants to enter (do ((= Bob-flag 'up)) (skip)) ;; loop until Bob lowers flag (Alice-dog-in-yard) ;; Dog can enter the yard (set! Alice-flag 'down) ;; Alice is leaving ))

(define (Alice) (loop ;; ``repeat forever'' (set! Alice-flag 'up) ;; Alice wants to enter (do ((= Bob-flag 'up)) (skip)) ;; loop until Bob lowers flag (Alice-dog-in-yard) ;; Dog can enter the yard (set! Alice-flag 'down) ;; Alice is leaving ))

Bob Alice

Page 22: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

22

Flag Solution -- Bob

(define (Bob) (loop ;; ``repeat forever'' (set! Bob-flag 'up) ;; Bob wants to enter (do ((= Alice-flag 'up)) ;; If Alice wants to enter (set! Bob-flag 'down) ;; Bob is a gentleman (do ((= Alice-flag 'up)) (skip)) ;; loop (skip) till Alice leaves (set! Bob-flag 'up) ;; raise flag ) ;; and go through the do again (Bob-dog-in-yard) ;; Dog can enter yard (set! Bob-flag 'down) ;; Bob is leaving ))

(define (Bob) (loop ;; ``repeat forever'' (set! Bob-flag 'up) ;; Bob wants to enter (do ((= Alice-flag 'up)) ;; If Alice wants to enter (set! Bob-flag 'down) ;; Bob is a gentleman (do ((= Alice-flag 'up)) (skip)) ;; loop (skip) till Alice leaves (set! Bob-flag 'up) ;; raise flag ) ;; and go through the do again (Bob-dog-in-yard) ;; Dog can enter yard (set! Bob-flag 'down) ;; Bob is leaving ))

Page 23: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

23

Flag Solution -- Both

(define (Alice) (loop ;; ``repeat forever'' (set! Alice-flag 'up) ;; Alice wants to enter (do ((= Bob-flag 'up)) (skip)) ;; loop until Bob lowers flag (Alice-dog-in-yard) ;; Dog can enter the yard (set! Alice-flag 'down) ;; Alice is leaving ))

(define (Alice) (loop ;; ``repeat forever'' (set! Alice-flag 'up) ;; Alice wants to enter (do ((= Bob-flag 'up)) (skip)) ;; loop until Bob lowers flag (Alice-dog-in-yard) ;; Dog can enter the yard (set! Alice-flag 'down) ;; Alice is leaving ))

(define (Bob) (loop ;; ``repeat forever'' (set! Bob-flag 'up) ;; Bob wants to enter (do ((= Alice-flag 'up)) ;; If Alice wants to enter (set! Bob-flag 'down) ;; Bob is a gentleman (do ((= Alice-flag 'up)) (skip)) ;; loop (skip) till Alice leaves (set! Bob-flag 'up) ;; raise flag ) ;; and go through the do again (Bob-dog-in-yard) ;; Dog can enter yard (set! Bob-flag 'down) ;; Bob is leaving ))

(define (Bob) (loop ;; ``repeat forever'' (set! Bob-flag 'up) ;; Bob wants to enter (do ((= Alice-flag 'up)) ;; If Alice wants to enter (set! Bob-flag 'down) ;; Bob is a gentleman (do ((= Alice-flag 'up)) (skip)) ;; loop (skip) till Alice leaves (set! Bob-flag 'up) ;; raise flag ) ;; and go through the do again (Bob-dog-in-yard) ;; Dog can enter yard (set! Bob-flag 'down) ;; Bob is leaving ))

Page 24: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

24

Intuition: Why Mutual Exclusion is Preserved

Each perform: • First raise the flag, to signal interest. Then• look to see if the other one has raised the flag.

One can claim that the following flag principle holds:

since Alice and Bob each raise their own flag and then look at the others flag, the last one to start looking must notice that both flags are up.

Page 25: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

25

Why is there no Deadlock?

Since Alice has priority over Bob…if neither is entering the critical section, both are repeatedly trying, and Bob will give Alice priority.

Unfortunately, the algorithm is not a fair one, and Bob's dogs might eventually grow very anxious :-)

Page 26: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

26

The Morals of our Story

• The Mutual Exclusion problem cannot be solved using transient communication. (I.e. Cell-phones.)

• The Mutual Exclusion problem cannot be solved using interrupts or interrupt bits (I.e. Cans)

• The Mutual Exclusion problem can be solved with one bit registers (I.e. Flags), memory locations that can be read and written (set!-ed).

We cheated a little: the arbiter problem…

Page 27: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

27

The Solution and Conclusion

(define (Alice) (loop (mutex 'begin) (Alice-dog-in-yard) ;; critical section (mutex 'end) ))

Question: then why not execute all the code of the parallel prime-printing algorithm in a critical section?

Page 28: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

28

Answer: Amdahl’s Law

Speedup Overall = Execution_time_Old / Execution_time_New =

1 Fraction_Enhanced Speedup_Enhanced

1- Fraction_Enhanced +

If 40\% of the execution time and can be sped-up by a factor of 10 by using 10 processors. Then

Speedup Overall = 1/(0.6 + (0.4/10)) = 1/0.64 = 1.56

Page 29: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

29

Length of Critical Sections

We want Linear Speedup: 100 processors = 100 times faster By Amdahl's law that means that Fraction_Enhanced = 1 No Sequential Parts!!!!!

For a speedup of only 99 times: 1-Fraction_Enhacned= 0.0001

In other words, we must try and make the parts that are sequential (the critical sections) as short as possible!!!!!

Page 30: 1 Lecture #24 Shared Objects and Concurrent Programming This material is not available in the textbook. The online powerpoint presentations contain the

30

Summarizing it all

To summarize it all, our world is asynchronous, and yet with a bit of luck we have ways of overcoming this asynchrony to create powerful concurrent algorithms. This was best summarized by the quote which some attribute to Tommy Lasorda:

“Life is the synchronicity of chance”

Good Luck on Your Final Exam!