• No results found

Asynchronous Programming Promises + Futures Consistency. Chris Rossbach

N/A
N/A
Protected

Academic year: 2021

Share "Asynchronous Programming Promises + Futures Consistency. Chris Rossbach"

Copied!
108
0
0

Loading.... (view fulltext now)

Full text

(1)

Asynchronous Programming Promises + Futures

Consistency

Chris Rossbach

(2)

Today

• Questions?

• Administrivia

• Material for the day

• Events / Asynchronous programming

• Promises & Futures

• Bonus: memory consistency models

• Acknowledgements

• Consistency slides borrow some materials from Kevin Boos. Thanks!

(3)

Asynchronous Programming

Events, Promises, and Futures

(4)

Asynchronous Programming

Events, Promises, and Futures

(5)

Programming Models for Concurrency

(6)

Programming Models for Concurrency

• Hardware execution model:

(7)

Programming Models for Concurrency

• Hardware execution model:

• CPU(s) execute instructions sequentially

(8)

Programming Models for Concurrency

• Hardware execution model:

• CPU(s) execute instructions sequentially

• Programming model dimensions:

• How to specify computation

• How to specify communication

• How to specify coordination/control transfer

(9)

Programming Models for Concurrency

• Hardware execution model:

• CPU(s) execute instructions sequentially

• Programming model dimensions:

• How to specify computation

• How to specify communication

• How to specify coordination/control transfer

• Techniques/primitives

• Message passing vs shared memory

• Preemption vs Non-preemption

(10)

Programming Models for Concurrency

• Hardware execution model:

• CPU(s) execute instructions sequentially

• Programming model dimensions:

• How to specify computation

• How to specify communication

• How to specify coordination/control transfer

• Techniques/primitives

• Message passing vs shared memory

• Preemption vs Non-preemption

• Dimensions/techniques not always orthogonal

(11)

Programming Models for Concurrency

• Hardware execution model:

• CPU(s) execute instructions sequentially

• Programming model dimensions:

• How to specify computation

• How to specify communication

• How to specify coordination/control transfer

• Techniques/primitives

• Message passing vs shared memory

• Preemption vs Non-preemption

• Dimensions/techniques not always orthogonal

Futures &

Promises touch all three

dimension

(12)

Futures & Promises

(13)

Futures & Promises

• Values that will eventually become available

(14)

Futures & Promises

• Values that will eventually become available

• Time-dependent states:

• Completed/determined

• Computation complete, value concrete

• Incomplete/undetermined

• Computation not complete yet

(15)

Futures & Promises

• Values that will eventually become available

• Time-dependent states:

• Completed/determined

• Computation complete, value concrete

• Incomplete/undetermined

• Computation not complete yet

• Construct ( future X )

• immediately returns value

• concurrently executes X

(16)

Java Example

(17)

Java Example

(18)

Java Example

• CompletableFuture is a container for Future object type

(19)

Java Example

• CompletableFuture is a container for Future object type

• cf is an instance

(20)

Java Example

• CompletableFuture is a container for Future object type

• cf is an instance

• runAsync() accepts

• Lambda expression

• Anonymous function

• Functor

(21)

Java Example

• CompletableFuture is a container for Future object type

• cf is an instance

• runAsync() accepts

• Lambda expression

• Anonymous function

• Functor

• runAsync() immediately returns a waitable object (cf)

(22)

Java Example

• CompletableFuture is a container for Future object type

• cf is an instance

• runAsync() accepts

• Lambda expression

• Anonymous function

• Functor

• runAsync() immediately returns a waitable object (cf)

• Where (on what thread) does the lambda expression run?

(23)

Futures and Promises:

Why two kinds of objects?

(24)

Futures and Promises:

Why two kinds of objects?

Promise: “thing to be done”

(25)

Futures and Promises:

Why two kinds of objects?

Promise: “thing to be done”

Future: encapsulation (something to give caller)

(26)

Futures and Promises:

Why two kinds of objects?

Promise: “thing to be done”

Future: encapsulation (something to give caller)

Promise to do something in the future

(27)

Futures vs Promises

• Future: read-only reference to uncompleted value

• Promise: single-assignment variable that the future refers to

• Promises complete the future with:

• Result with success/failure

• Exception

(28)

Futures vs Promises

• Future: read-only reference to uncompleted value

• Promise: single-assignment variable that the future refers to

• Promises complete the future with:

• Result with success/failure

• Exception

Language Promise Future

Algol Thunk Address of async result

Java Future<T> CompletableFuture<T>

C#/.NET TaskCompletionSource<T> Task<T>

JavaScript Deferred Promise

C++ std::promise std::future

(29)

Futures vs Promises

• Future: read-only reference to uncompleted value

• Promise: single-assignment variable that the future refers to

• Promises complete the future with:

• Result with success/failure

• Exception

Language Promise Future

Algol Thunk Address of async result

Java Future<T> CompletableFuture<T>

C#/.NET TaskCompletionSource<T> Task<T>

JavaScript Deferred Promise

C++ std::promise std::future

(30)

Futures vs Promises

• Future: read-only reference to uncompleted value

• Promise: single-assignment variable that the future refers to

• Promises complete the future with:

• Result with success/failure

• Exception

Language Promise Future

Algol Thunk Address of async result

Java Future<T> CompletableFuture<T>

C#/.NET TaskCompletionSource<T> Task<T>

JavaScript Deferred Promise

C++ std::promise std::future

Mnemonic:

Promise to do something Make a promise for the future

(31)

GUI Programming Distilled

(32)

GUI Programming Distilled

How can we parallelize

this?

(33)

Parallel GUI Implementation 1

(34)

Parallel GUI Implementation 1

(35)

Parallel GUI Implementation 1

(36)

Parallel GUI Implementation 1

(37)

Parallel GUI Implementation 1

DoThisProc

DoThatProc

OtherThing

(38)

Parallel GUI Implementation 1

DoThisProc

DoThatProc

OtherThing

Pros/cons?

(39)

Parallel GUI Implementation 1

Pros:

• Encapsulates parallel work Cons:

• Obliterates original code structure

• How to assign handlers→CPUs?

• Load balance?!?

• Utilization

DoThisProc

DoThatProc

OtherThing

Pros/cons?

(40)

Parallel GUI Implementation 2

(41)

Parallel GUI Implementation 2

Pros/cons?

(42)

Parallel GUI Implementation 2

Pros:

• Preserves programming model

• Can recover some parallelism Cons:

• Workers still have same problem

• How to load balance?

• Shared mutable state a problem

Pros/cons?

(43)

Parallel GUI Implementation 2

Pros:

• Preserves programming model

• Can recover some parallelism Cons:

• Workers still have same problem

• How to load balance?

• Shared mutable state a problem

Extremely difficult to solve without changing the whole

programming model…so

change it

Pros/cons?

(44)

Event-based Programming: Motivation

(45)

Event-based Programming: Motivation

• Threads have a *lot* of down-sides:

• Tuning parallelism for different environments

• Load balancing/assignment brittle

• Shared state requires locks →

• Priority inversion

• Deadlock

• Incorrect synchronization

• …

(46)

Event-based Programming: Motivation

• Threads have a *lot* of down-sides:

• Tuning parallelism for different environments

• Load balancing/assignment brittle

• Shared state requires locks →

• Priority inversion

• Deadlock

• Incorrect synchronization

• …

• Events: restructure programming model so threads are not exposed!

(47)

Event Programming Model Basics

(48)

Event Programming Model Basics

• Programmer only writes events

(49)

Event Programming Model Basics

• Programmer only writes events

• Event: an object queued for a module (think future/promise)

(50)

Event Programming Model Basics

• Programmer only writes events

• Event: an object queued for a module (think future/promise)

• Basic primitives

• create_event_queue(handler) → event_q

• enqueue_event(event_q, event-object)

• Invokes handler (eventually)

(51)

Event Programming Model Basics

• Programmer only writes events

• Event: an object queued for a module (think future/promise)

• Basic primitives

• create_event_queue(handler) → event_q

• enqueue_event(event_q, event-object)

• Invokes handler (eventually)

• Scheduler decides which event to execute next

• E.g. based on priority, CPU usage, etc.

(52)

Event-based programming

(53)

Event-based programming

(54)

Event-based programming

(55)

Event-based programming

(56)

Event-based programming

Runtime

(57)

Event-based programming

Runtime

(58)

Event-based programming

Runtime

Is the problem solved?

(59)

Another Event-based Program

(60)

Another Event-based Program

(61)

Another Event-based Program

Blocks!

(62)

Another Event-based Program

Blocks!

Burns CPU!

(63)

Another Event-based Program

Blocks!

Burns CPU!

Uses Other Handlers!

(call OnPaint?)

(64)

No problem!

Just use more events/handlers, right?

(65)

Continuations, BTW

(66)

Stack-Ripping

(67)

Stack-Ripping

(68)

Stack-Ripping

(69)

Stack-Ripping

Stack-based state out-of-scope!

Requests must carry state

(70)

Threads vs Events

• Thread Pros

• Overlap I/O and computation

• While looking sequential

• Intermediate state on stack

• Control flow naturally expressed

• Thread Cons

• Synchronization required

• Overflowable stack

• Stack memory pressure

• Event Pros

• Easier to create well-conditioned system

• Easier to express dynamic change in level of parallelism

• Event Cons

• Difficult to program

• Control flow between callbacks obscure

• When to deallocate memory

• Incomplete language/tool/debugger support

• Difficult to exploit concurrent hardware

(71)

Threads vs Events

• Thread Pros

• Overlap I/O and computation

• While looking sequential

• Intermediate state on stack

• Control flow naturally expressed

• Thread Cons

• Synchronization required

• Overflowable stack

• Stack memory pressure

• Event Pros

• Easier to create well-conditioned system

• Easier to express dynamic change in level of parallelism

• Event Cons

• Difficult to program

• Control flow between callbacks obscure

• When to deallocate memory

• Incomplete language/tool/debugger support

• Difficult to exploit concurrent hardware

(72)

Threads vs Events

• Thread Pros

• Overlap I/O and computation

• While looking sequential

• Intermediate state on stack

• Control flow naturally expressed

• Thread Cons

• Synchronization required

• Overflowable stack

• Stack memory pressure

• Event Pros

• Easier to create well-conditioned system

• Easier to express dynamic change in level of parallelism

• Event Cons

• Difficult to program

• Control flow between callbacks obscure

• When to deallocate memory

• Incomplete language/tool/debugger support

• Difficult to exploit concurrent hardware

Language-level Futures: the sweet spot?

(73)

Threads vs Events

• Thread Pros

• Overlap I/O and computation

• While looking sequential

• Intermediate state on stack

• Control flow naturally expressed

• Thread Cons

• Synchronization required

• Overflowable stack

• Stack memory pressure

• Event Pros

• Easier to create well-conditioned system

• Easier to express dynamic change in level of parallelism

• Event Cons

• Difficult to program

• Control flow between callbacks obscure

• When to deallocate memory

• Incomplete language/tool/debugger support

• Difficult to exploit concurrent hardware

Language-level Futures: the sweet spot?

(74)

Thread Pool Implementation

(75)

Thread Pool Implementation

Cool project idea: build a thread pool!

(76)

Thread Pool Implementation

(77)

ThreadPool Implementation

(78)

Redux: Futures in Context

(79)

Redux: Futures in Context

Futures:

(80)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

(81)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

• Programming remains mostly imperative

• Threads of control peppered with asynchronous/concurrent tasks

(82)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

• Programming remains mostly imperative

• Threads of control peppered with asynchronous/concurrent tasks

(83)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

• Programming remains mostly imperative

• Threads of control peppered with asynchronous/concurrent tasks

Compromise Model:

(84)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

• Programming remains mostly imperative

• Threads of control peppered with asynchronous/concurrent tasks

Compromise Model:

• Event-based programming

(85)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

• Programming remains mostly imperative

• Threads of control peppered with asynchronous/concurrent tasks

Compromise Model:

• Event-based programming

• Thread-based programming

(86)

Redux: Futures in Context

Futures:

• abstraction for concurrent work supported by

• Compiler: abstractions are language-level objects

• Runtime: scheduler, task queues, thread-pools are transparent

• Programming remains mostly imperative

• Threads of control peppered with asynchronous/concurrent tasks

Compromise Model:

• Event-based programming

• Thread-based programming

Currently: 2nd renaissance IMHO

(87)

Memory Consistency

25

(88)

Memory Consistency

• Formal specification of memory semantics

• Statement of how shared memory will behave with multiple CPUs

• Ordering of reads and writes

25

(89)

Memory Consistency

• Formal specification of memory semantics

• Statement of how shared memory will behave with multiple CPUs

• Ordering of reads and writes

• Memory Consistency != Cache Coherence

• Coherence: propagate updates to cached copies

• Invalidate vs. Update

• Coherence vs. Consistency?

• Coherence: ordering of ops. at a single location

• Consistency: ordering of ops. at multiple locations

25

(90)

Sequential Consistency

• Result of any execution is same as if all operations execute on a uniprocessor

• Operations on each processor are totally ordered in the

sequence and respect program order for each processor

P1 P2 P3

Pn

Memory

26

(91)

Sequential Consistency

• Result of any execution is same as if all operations execute on a uniprocessor

• Operations on each processor are totally ordered in the

sequence and respect program order for each processor

P1 P2 P3

Pn

Memory

26

Trying to mimic Uniprocessor semantics:

• Memory operations occur:

• One at a time

• In program order

• Read returns value of last write

(92)

Sequential Consistency: Canonical Example

Initially, Flag1 = Flag2 = 0

P1 P2

Flag1 = 1 Flag2 = 1

if (Flag2 == 0) if (Flag1 == 0) enter CS enter CS

27

(93)

Sequential Consistency: Canonical Example

Initially, Flag1 = Flag2 = 0

P1 P2

Flag1 = 1 Flag2 = 1

if (Flag2 == 0) if (Flag1 == 0) enter CS enter CS

27

Can both P1 and P2 wind up in the critical section at the same time?

(94)

Do we need Sequential Consistency?

Initially, A = B = 0

P1 P2 P3

A = 1

if (A == 1) B = 1

if (B == 1)

register1 = A

28

(95)

Do we need Sequential Consistency?

Initially, A = B = 0

P1 P2 P3

A = 1

if (A == 1) B = 1

if (B == 1)

register1 = A

28

Key issue:

• P2 and P3 may not see writes to A, B in the same order

• Implication: P3 can see B == 1, but A == 0 which is incorrect

• Wait! Why would this happen?

(96)

Do we need Sequential Consistency?

Initially, A = B = 0

P1 P2 P3

A = 1

if (A == 1) B = 1

if (B == 1)

register1 = A

28

Key issue:

• P2 and P3 may not see writes to A, B in the same order

• Implication: P3 can see B == 1, but A == 0 which is incorrect

• Wait! Why would this happen?

Write Buffers

P_0 write → queue op in write buffer, proceed

P_0 read → look in write buffer,

P_(x != 0) read → old value: write buffer hasn’t drained

(97)

Requirements for Sequential Consistency

29

(98)

Requirements for Sequential Consistency

• Program Order

• Processor’s memory operations must complete in program order

29

(99)

Requirements for Sequential Consistency

• Program Order

• Processor’s memory operations must complete in program order

• Write Atomicity

• Writes to the same location seen by all other CPUs

• Subsequent reads must not return value of a write until propagated to all

29

(100)

Requirements for Sequential Consistency

• Program Order

• Processor’s memory operations must complete in program order

• Write Atomicity

• Writes to the same location seen by all other CPUs

• Subsequent reads must not return value of a write until propagated to all

• Write acknowledgements are necessary

• Cache coherence provides these properties for a cache-only system

29

(101)

Requirements for Sequential Consistency

• Program Order

• Processor’s memory operations must complete in program order

• Write Atomicity

• Writes to the same location seen by all other CPUs

• Subsequent reads must not return value of a write until propagated to all

• Write acknowledgements are necessary

• Cache coherence provides these properties for a cache-only system

29

Disadvantages:

• Difficult to implement!

• Coherence to (e.g.) write buffers is hard

• Sacrifices many potential optimizations

• Hardware (cache) and software (compiler)

• Major performance hit

(102)

Relaxed Consistency Models

30

(103)

Relaxed Consistency Models

• Program Order relaxations (different locations)

• W → R; W → W; R → R/W

30

(104)

Relaxed Consistency Models

• Program Order relaxations (different locations)

• W → R; W → W; R → R/W

• Write Atomicity relaxations

• Read returns another processor’s Write early

30

(105)

Relaxed Consistency Models

• Program Order relaxations (different locations)

• W → R; W → W; R → R/W

• Write Atomicity relaxations

• Read returns another processor’s Write early

• Combined relaxations

• Read your own Write (okay for S.C.)

30

(106)

Relaxed Consistency Models

• Program Order relaxations (different locations)

• W → R; W → W; R → R/W

• Write Atomicity relaxations

• Read returns another processor’s Write early

• Combined relaxations

• Read your own Write (okay for S.C.)

• Requirement: synchronization primitives for safety

• Fence, barrier instructions etc

30

(107)

Relaxed Consistency Models

• Program Order relaxations (different locations)

• W → R; W → W; R → R/W

• Write Atomicity relaxations

• Read returns another processor’s Write early

• Combined relaxations

• Read your own Write (okay for S.C.)

• Requirement: synchronization primitives for safety

• Fence, barrier instructions etc

30

(108)

Questions?

References

Related documents

Using the case study of designing a research project on the Transition movement, it looks to the prevalent use and praising of community as a vehicle for social innovation,

Cabugueira (2005) argues that it is not tourism that fosters the development of a given country or region, but its own level of development, which converts tourism in an activity

We have distributed about 50 questionnaires among the participants in different organisations. There were about 22 IT subject matter expert respondents to the survey representing:

Deliver effective training and clear information to all customer-facing staff and their managers, to ensure they understand, at an appropriate level, each product and service

mismo, otro at the Biblioteca Nacional, Buenos Aires, 2016. Reproduction of first page only, Lame Duck Books, Catalogue No. Five pages are published in Borges el mismo, otro,

termediate and high risk groups as defined by Framing- ham Risk Score (FRS), comparative Framingham Risk Score (C-FRS), the Prospective Cardiovascular Mün- ster (PROCAM) or

Series resistor in INPUT and STATUS lines are also required to prevent that, during battery voltage transient, the current exceeds the absolute maximum rating. Safest configuration

Almighty God, whose Son our Savior Jesus Christ is the light of the world: Grant that thy people, illumined by thy Word and Sacraments, may shine with radiance of Christ’s glory,