just keep sending the messages

53
Copyright © 2011 Russel Winder 1 Just Keep Sending the Messages Dr Russel Winder It’z Interactive Ltd [email protected] @russel_winder

Upload: russel-winder

Post on 17-Jan-2015

766 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 1

Just Keep Sending the Messages

Dr Russel Winder

It’z Interactive [email protected]

@russel_winder

Page 2: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 2

Just Keep Sending the Messages

Dr Russel Winder

Independent [email protected]

@russel_winder

Page 3: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 3

Just Keep Sending the Messages

Prof Russel Winder

Wolverhampton [email protected]

@russel_winder

Page 4: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 4

Aims and Objectives

● Investigate why message passing architectures are the software architectures of the future.

● Have some fun (whilst hopefully learning something).

Page 5: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 5

Audience Aims and Objectives

?

Page 6: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 6

Structure of the Session

● Introduction.

● Do stuff.

● Exit stage (left|right).

There will be significant dynamic binding of the session.

Page 7: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 7

Protocol

● Some slides, to kick things off.

● Some programs to really demonstrate things.

● NB Interaction between audience and presenter is entirely mandatory.

We reserve the right to (shelve|stash) forlater any interaction that appears to goon longer than seems appropriate.

Note the use of Git, Mercurial, Bazaar jargonin a (gratuitous) attempt to make a connectionwith other sessions at ACCU 2011.

Page 8: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 8

BEGIN

● History is important:– To know today, we look to yesterday. To know tomorrow,

we see today as yesterday.

http://wiki.answers.com/Q/Why_history_is_important

Page 9: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 9

One program at a time.

Multi-tasking

Processes / IPC

Threads / Shared memory

Processes / Threads Processes / RPC

Page 10: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 10

Historical Summary

● Shared-memory multi-threading for applications programming is a total aberration:

– Consequences of operating systems handling of concurrency have been imposed on all programmers.

● Cooperating processes is where applications development sanity is:

– Operating system interprocess communication was slow, hence threads, but this lead directly to the shared-memory, multi-threading quagmire.

– Erlang has shown that processes and message passing can do the job properly even after the “mainstream” had rejected process-based working.

Page 11: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 11

Concurrency vs.

Parallelism

Page 12: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 12

Concurrency

● Running multiple tasks using time-division multiplexing on a single processor.

Page 13: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 13

Parallelism

● Running multiple tasks concurrently.

Note that the word concurrency here is the Englishmeaning whereas the previous slide gave the computing jargon meaning of the word.

Page 14: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 14

Concurrency . . .

● . . . is a technique required for operating systems.

● . . . can sometimes be required for applications but not as much as might be thought.

● . . . is an implementation detail (sort of).

● Applications can use alternative models, for example event-driven systems.

– Abstract over the control flow rather than manage it with locks, semaphores, monitors, etc.

Page 15: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 15

Parallelism . . .

● . . . is about making an algorithm execute more quickly in the presence of multiple processors.

● . . . is an architectural and design issue for applications.

Page 16: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 16

Concurrency

● The problem with threads is shared memory:– Without writeable shared memory there is no need for

synchronization.

● Why impose a 1960s, operating system driven view of concurrency management on applications programmers?

Page 17: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 17

Shared Memory is . . .

● . . . anathema to parallelism.

● . . . anathema to concurrency.

● . . . anathema to modern applications programming.

● . . . anathema.

Page 18: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 18

Solution . . .

● . . . do not use mutable shared memory.

Note the caveat here that opens the doorto using shared immutable data.

Page 19: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 19

Operating Systems . . .

● . . . are inherently a concurrency problem.

● Applications on the other hand are not, they should be using higher level abstractions.

Page 20: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 20

ObjectOrientation

Page 21: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 21

Original Model

● Object-based:– A set of (small) closed namespaces, with methods,

exchanging messages requesting services.

● Object-oriented:– Object-based plus classes and inheritance

Page 22: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 22

Implementations

● Smalltalk realized the object-oriented correctly.

● C++ did not: message passing replaced by function call.

C++ destroyed correct appreciation of the object-oriented model of computation.

Page 23: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 23

Non-contentious (sort of) Syllogism

● Object-orientation is about objects passing messages to each other: object-orientation is not about function call in a shared memory context.

● C++ is a programming language based on function call in a shared memory context: C++ does not have objects passing messages to each other.

● C++ is not an object-oriented programming language.

Page 24: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 24

Java

Page 25: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 25

Object-orientation

● Java follows C++:– Functional replaces message passing.

● Java beats C++, had threads 15 years before C++.

● Shared memory multi-threading requires all the locks, semaphores, monitors, etc. and Java has it all.

Page 26: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 26

Partial Solution for JVM

● Use java.util.concurrent:– Provides many high-level tools.

– Has many low-level tools.

Use jsr166y (Fork/Join) and extra166y (ParallelArray) in preference to using stuff in JDK6

If you are using the low-level tools then you are lost to the cause of quality application programming.

Page 27: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 27

High Performance Computing

(HPC)

aka

Real Computing

Page 28: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 28

Parallelism Rules

● HPC has been doing parallelism for 40+ years.

● Combinations of architectures:– Vector processors

– Multi-bus multiprocessors

– Clusters

Page 29: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 29

HPC Extant Solution

● Message Passing Interface (MPI)

MPI addresses the problem of SPMD or MIMD parallelism in the context of multiple, possibly multicore, systems.

Page 30: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 30

HPC Proposed Solution

● Partitioned Global Address Space (PGAS)

● Champions:– Chapel

– X10

– Fortress

Structure the global address space to allow for multiple processors sharing a single memory and/or to deal with distributed memory systems.

Page 31: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 31

Return to a Better Way

Page 32: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 32

Real Solutions?

● Actor Model

● Dataflow architectures

● CSP (Communicating Sequential Processes)

Return to a process and message passing view of applications.

Nothing wrong with threads as a tool.The problem is using shared memory.

Page 33: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 33

Actor Model

● A program is a collection of actors that send messages to each other.

● An actor is a process with a “mailbox” for receiving messages.

● A mailbox is a (thread-safe) queue of messages.

Page 34: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 34

Dataflow Model

● A program is a graph of operators with some data sources and some data sinks.

● An operator is an event-triggered computation with some inputs and some output.

● An operator triggers for a certain state of its inputs.

Page 35: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 35

CSP is . . .

● . . . mathematics:

● . . . but not scary since the mathematics can be hidden in an API, so it just becomes a programming tool.

VMS = X. ( in2p (chocolate X|out1p toffee X)μ → → → → | in1p (toffee X|in1p (chocolate X|in1p STOP X )))→ → → → → α

Page 36: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 36

CSP

● A program is a graph of processes running a sequential computation that take input from input channels and write output to output channels.

● Data exchange down a channel realizes a rendezvous.

Page 37: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 37

Commentary

● Actors are non-deterministic, with chaotic communications and hence complex.

● Dataflow and CSP have much greater determinism with fixed communications channels.

Note the use of the term complex in a(gratuitous) attempt to make a connectionwith other sessions at ACCU 2011.

Page 38: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 38

Implementations

● Actor Model:– JVM: GPars, Scala, Akka, Fantom

– Native: C++/Just::Thread Pro, D

– Alternative: Erlang

● Dataflow Model:– JVM: GPars, Pervasive DataRush

– Native: C++/Just::Thread Pro

● CSP:– JVM: GPars, JCSP

– Native: C++CSP2What, no mention of Groovy?

Page 39: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 39

Acknowledgements

Page 40: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 40

First Example Problem

● Something small, so the code is small.

● Something not too “toy”.

● Something with good parallelism.– Embarrassingly parallel to allow checking of scaling.

Page 41: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 41

Page 42: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 42

What is the Value of ?

● Easy, it's known exactly, it's (obviously).

It's simples

Note the use of image of cuddly animals in a(gratuitous) attempt to make a connectionwith other sessions at ACCU 2011.

Page 43: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 43

Approximating

● What is it's value represented as a floating point number?

– We can only obtain an approximation.

– A plethora of possible algorithms to choose from, a popular one is to employ the following integral equation.

4=∫0

1 1

1x2dx

Page 44: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 44

One Possible Algorithm

● Use quadrature to estimate the value of the integral – which is the area under the curve.

=4n∑i=1

n 1

1i−0.5n

2

With n = 3 not much to do, but potentially lots of error.

Embarrassingly parallel.

Page 45: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 45

Major Minor Hardware Problem

● Multiple hyperthreads per core on multicore processors can be a serious waste of time.

Ed: Rant about chip manufacturers andoperating systems elided to avoid persecutionprosecution.

Page 46: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 46

Second Example Problem

● Sleeping Barber Problem– A barber shop has a cutting chair and some waiting chairs.

The barber sleeps in the cutting chair if there are no customers. If a customer enters, the customer checks the cutting chair and wakes the barber if the barber is asleep in the chair, sits in the chair and gets a hair cut. If the entering customer sees the cutting chair in use, the customer checks to see if there is a waiting chair free. If there is the customer sits and waits, otherwise the customer leaves dissatisfied. On finishing a customer cut, the customer leaves satisfied (we assume), and the barber checks for waiting customers. If there are any waiting customers, the customer moves to the cutting chair. If there are no waiting customers, the barber returns to sleeping in the cutting chair.

Page 47: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 47

Sleeping Barber Problem . . .

● . . . is an interesting recasting of a process synchronization problem in operating systems.

● . . . is due to Edsgar Dykstra

http://en.wikipedia.org/wiki/Sleeping_barber_problem

Page 48: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 48

If the examples haven't been shown yet, now is the time to show them!

Page 49: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 49

Summary

● Multiprocessor programming is now the norm – even if you don't actually need it.

● Hardware is rapidly heading towards distributed memory architectures, instead of shared memory multi-threading.

● Shared memory multi-threading requires locks, semaphores, monitors, etc. and programmers find it hard to get that stuff right.

● Actor Model, Dataflow Model, and CSP are higher level abstractions of managed control flow that are easier for programmers to deal with.

Page 50: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 50

Summary of the Summary

● Shared-memory multi-threading is like stacks, you know it's there but you just don't worry about it.

Page 51: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 51

Summary of the Summary of the Summary

● If you think locks, semaphores, monitors, etc. are important for your work, you are either working in the concurrency frameworks business (*) OR you are doing it wrong.

● (*) Which includes operating systems.

Page 52: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 52

END

//G.SYSIN DD *

Notice the lack of gratuitous adverts forbooks such as “Python for Rookies” and“Developing Java Software” that areauthored by the presenter.

Page 53: Just Keep Sending The Messages

Copyright © 2011 Russel Winder 53

Just Keep Sending the Messages

Dr Russel Winder

It’z Interactive [email protected]

@russel_winder