concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · concurrency (and parallelism)...

26
CS 251 Fall 2019 Principles of Programming Languages Ben Wood λ CS 251 Spring 2020 Principles of Programming Languages Ben Wood λ https://cs.wellesley.edu/~ cs251/s20/ Concurrency (and Parallelism) Concurrency 1

Upload: others

Post on 17-Oct-2020

33 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

CS 251 Fall 2019Principles of Programming LanguagesBen Woodλ CS 251 Spring 2020Principles of Programming LanguagesBen Woodλ

https://cs.wellesley.edu/~cs251/s20/

Concurrency(and Parallelism)

Concurrency 1

Page 2: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Parallelism and Concurrency in 251

• Goal: encounter– essence, key concerns– non-sequential thinking– some high-level models– some mid-to-high-level mechanisms

• Non-goals:– performance engineering / measurement– deep programming proficiency– exhaustive survey of models and mechanisms

Parallelism 2

Page 3: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Parallelism Concurrency

data / work

data = resources

workers = computations

workers = resources

divided among

share

Use more resourcesto complete work faster.

Coordinate accessto shared resources.

Both can be expressed using a variety of primitives.Concurrency 3

Page 4: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Concurrency via Concurrent ML

• Extends SML with language features for concurrency.• Included in SML/NJ and Manticore• Model:

– explicitly threaded– synchronous message-passing over channels– first-class events

Concurrency 4

Page 5: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

CML: spawn explicit threadsvs. Manticore's "hints" for implicit parallelism.

val spawn : (unit -> unit) -> thread_id

let fun f () = new thread's work…val t2 = spawn f

inthis thread's work …

end

Concurrency 5

spawn f

new thread runs f

time

Thread 1 Thread 2

thread 1 continues

workload thunk

Page 6: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

(Aside: different model, fork-join)

Concurrency 6

forkfork

fork

fork

joinjoin

joinjoin

fork : (unit -> 'a) -> 'a task"call" a function in a new thread

join : 'a task -> 'await for it to "return" a result

Mainly for explicit task parallelism(expressing dependences between tasks),not concurrency(interaction/coordination/cooperation between tasks).

(CML's threads are similar, but cooperation is different.)

Page 7: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

CML: How do threads cooperate?val spawn : (unit -> unit) -> thread_id

Concurrency 7

How do we pass values in? How do we get results of work out?

workload thunk

let val data_in_env = …fun closures_for_the_win x = …val _ = spawn (fn () =>

map closures_for_the_win data_in_env)in

…end

Page 8: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

CML: How do threads cooperate?val spawn : (unit -> unit) -> thread_id

Threads communicate by passing messages through channels.

type ’a chanval recv : ’a chan -> ’a val send : (’a chan * ’a) -> unit

Concurrency 8

How do we get results of work out?

workload thunk

Page 9: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Tiny channel exampleval channel : unit -> ’a chan

let val ch : int chan = channel ()fun inc () =

let val n = recv chval () = send (ch, n + 1)

in exit () endin

spawn inc;send (ch, 3);…;recv ch

end

Concurrency 9

spawn inc

send(ch,3)

recv ch

<start inc>

recv ch

send(ch,4)

3

4

time

Page 10: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Concurrent streamsfun makeNatStream () =let val ch = channel ()

fun count i = (send (ch, i);count (i + 1)

)in

spawn (fn () => count 0);ch

end

fun sum stream 0 acc = acc| sum stream n acc =sum stream (n - 1) (acc + recv stream)

val nats = makeNatStream ()val sumFirst2 = sum nats 2 0val sumNext2 = sum nats 2 0

Concurrency 10

time

spawn(fn()=> count 0)

recv stream

recv stream

<start count 0>

send(ch,0)

send(ch,1)

0

1

send(ch,2)

send(ch,3)

recv stream

recv stream

2

3

Page 11: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

A common pattern: looping thread

fun forever init f =let

fun loop s = loop (f s) in

spawn (fn () => loop init);()

end

Concurrency 11

Page 12: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Concurrent streams

fun makeNatStream () =let

val ch = channel ()in

forever 0 (fn i => (send (ch, i);i + 1));

chend

Concurrency 12see cml-sieve.sml, cml-stream.sml

Page 13: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Event ordering? (1)fun makeNatStream () =

let val ch = channel ()fun count i = (

send (ch, i);count (i + 1)

)in

spawn (fn () => count 0);ch

end

val nats = makeNatStream ()val _ = spawn (fn () => print ("Green "

^(Int.toString (recv nats))))val _ = print ("Blue "^(Int.toString (recv nats)))

Concurrency 13

time

spawn(fn()=> count 0)

<start count 0>

send(ch,0)recv nats0

<start fn …>

recv nats send(ch,1)1

spawn(fn()=> print…)

Page 14: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Event ordering? (2)fun makeNatStream () =

let val ch = channel ()fun count i = (

send (ch, i);count (i + 1)

)in

spawn (fn () => count 0);ch

end

val nats = makeNatStream ()val _ = spawn (fn () => print ("Green "

^(Int.toString (recv nats))))val _ = print ("Blue "^(Int.toString (recv nats)))

Concurrency 14

time

spawn(fn()=> count 0)

<start count 0>

send(ch,0)

recv nats

0<start fn …>

recv nats

send(ch,1)1

spawn(fn()=> print…)

Page 15: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Synchronous message passing (CML)

📞 message passing = handshakereceive blocks until a message is sentsend blocks until the message received

vs 📬 asynchronous message passingreceive blocks until a message has arrivedsend can finish immediately without blocking

Concurrency 15

Page 16: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Synchronous message passing (CML)

Concurrency 16

blocked untilanother threadreceives on ch. recv ch

send (ch, 1)

send (ch, 0)

recv ch

blocked untilanother threadsends on ch.

Thread 1 Thread 2

time

ch📞

ch📞

Page 17: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Asynchronous message passing(not CML)

Concurrency 17

send does notblock

recv ch

send (ch, 0)

blocked untila thread firstsends on ch.

Thread 1 Thread 2

time

send (ch, 0)

send (ch, 0)

📬 recv ch

📬

recv ch

ch

ch

Page 18: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

First-class events, combinators

Event constructorsval sendEvt : (’a chan * ’a) -> unit eventval recvEvt : ’a chan -> ’a event

Event combinatorsval sync : ’a event -> ’aval choose : ’a event list -> ’a event val wrap : (’a event * (’a -> ’b)) -> ’b event

val select = sync o choose

Concurrency 18

Page 19: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Utilitiesval recv = sync o recvEvtval send = sync o sendEvt

fun forever init f =letfun loop s = loop (f s)

inspawn (fn () => loop init);()

end

Concurrency 19

Page 20: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Why combinators?fun makeZipCh (inChA, inChB, outCh) =

forever () (fn () =>let

val (a, b) = select [wrap (recvEvt inChA,

fn a => (a, recv inChB)),wrap (recvEvt inChB,

fn b => (recv inChA, b)) ]

in send (outCh, (a, b))

end)

Concurrency 20

Remember:synchronous (blocking)message-passing

Page 21: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

More CML• Emulating mutable state via concurrency: cml-cell.sml• Dataflow / pipeline computation: cml-sieve.sml• Implement futures: cml-futures.sml

Concurrency 21

Page 22: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Why avoid mutation (of shared data)?

• For parallelism?• For concurrency?

Other models:Shared-memory multithreading + synchronization…

Concurrency 22

Page 23: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Shared-Memory Multithreading

pc pc pc

Unshared:locals andcontrol

Shared:heap and globals

Implicit communication through sharing.

Page 24: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Thread 1t1 = balbal = t1 + 10

Thread 2t2 = balbal = t2 - 10

t1 = bal

bal = t1 + 10

t2 = bal

bal = t2 - 10

Thread 1

Concurrency and Race Conditions

Thread 2int bal = 0;

bal == 0

Page 25: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

t1 = bal

bal = t1 + 10

t2 = bal

bal = t2 - 10

Concurrency and Race Conditionsint bal = 0;

bal == -10

Thread 1t1 = balbal = t1 + 10

Thread 2t2 = balbal = t2 - 10

Thread 1 Thread 2

Page 26: Concurrency - cs.wellesley.educs251/s20/slides/concurrency.pdf · Concurrency (and Parallelism) Concurrency 1. Parallelism and Concurrency in 251 •Goal: encounter –essence, key

Concurrency and Race Conditions

Thread 1synchronized(m) {

t1 = balbal = t1 + 10

}

Thread 2synchronized(m) {

t2 = balbal = t2 - 10

}

acquire(m)

release(m)

t2 = bal

bal = t2 - 10

t1 = bal

bal = t1 + 10

release(m)

acquire(m)

Lock m = new Lock();int bal = 0; Thread 1 Thread 2