Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder...

49
Jeepers GPars, It's All So Easily Parallel Russel Winder email: [email protected] xmpp: [email protected] twitter: russel_winder

Transcript of Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder...

Page 1: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Jeepers GPars,It's All So Easily Parallel

Russel Winder

email: [email protected]: [email protected]

twitter: russel_winder

Page 2: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 2

Aims, Goals and Objectives

● Get people enthusiastic to use dataflow, actors and Communicating Sequential Processes (CSP) as their application structuring architecture.

● Get people enthusiastic to use GPars in all their Java and Groovy programming.

● Arrive at an hostelry in good time to have a nice drink and good conversation.

Page 3: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 3

Structure

● The Beginning.● The Middle.● The End.● The Extra Questions.● The Whisky.

Page 4: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 4

Protocol

● Interaction is allowed. Actually it may well, possibly, be mandatory.

● If an interjection leads to a “too long” side track, we will stack (or possibly even stash†) it for later.

†Depending on whether you want a code orientedor a version control oriented metaphor.

Page 5: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 5

The Beginning

Page 6: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 6

In the Beginning: The Hardware

Processor

Memory

Single ALU

Page 7: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 7

In the Beginning: The Software

Load a program into the memory andrun it to completion.

Page 8: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 8

Multitasking Operating Systems

Load many programs into memory andhave one of then run at any one time.

Page 9: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 9

Concurrency

● Multitasking operating systems introduce the need for concurrency in a shared memory context.

● Tools for managing this are created:● Locks● Semaphores● Monitors

Page 10: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 10

Higher Level Models

● Dataflow Model:● Bert Sutherland, 1966.

● Actor Model:● First published in 1973 by Carl Hewett, Peter Bishop and

Richard Steiger – IJCAI.

● Communicating Sequential Processes (CSP):● First published in a paper by Tony Hoare in 1978, but

only really became well known with the 1983 book.

Page 11: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 11

The Interregnum Begins

Programmers were taught that concurrent applications needed the same tools and

techniques that operating system implementation needed:

Shared memory multi-threading.

Page 12: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 12

The Interregnum Reified

1995, Java reifies shared memory multithreading as the obviously known right way of dealing with

concurrency…

Page 13: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 13

…after all C and C++ have been using pthreads (or the like) for many years.

Page 14: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 14

The Interregnum Reified, A Bit More

1995, Java reifies the mindset that concurrent programming is all about shared memory

multithreading by putting it in the language…

Page 15: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 15

…it takes till 2011 for C++ to do the same.

Page 16: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 16

The Interregnum Continues

Programmers discover that shared-memory multithreading is hard† to get right: that trying to get things right with lock, semaphores and

monitors is not entirely easy‡.

Page 17: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 17

†By hard, what is actually meant is usually:

Absolutely ####### impossible!

Page 18: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 18

‡Clearly “not entirely easy” is a euphemism,see the previous slide for the

more appropriate description.

Page 19: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 19

Concurrency Eschewed

Programmers know concurrent and parallel programming is hard, so they don't do it.

Page 20: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 20

Why Bother?

Processor speeds double every couple of years, so single thread applications get twice as fast

every two years, so who cares about concurrency and parallelism?

Page 21: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 21

Oh Dear

Processor speeds have to stop getting faster, Moore's Law still working, processor

manufacturers start increasing the core count to use all the extra transistors they have.

Page 22: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 22

Multicore Revolution: The Early Period

Memory

Core CoreCoreCore Core CoreCoreCore

Page 23: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 23

The Hardware Con Job

Each processor has N ALUs and so executes N instructions per unit time so is N times faster

than a single core processor.

Page 24: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 24

The Realization

For compute intensive applications, increased parallelism is now the only way to create

increased application performance.

Page 25: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 25

The Mechanisms

Kernel threads mean that applications can harness real parallelism with threads not just

time-division multiplexing concurrency.

Page 26: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 26

The Problem

Creating large, correct programs using shared memory multi-threading is:

Absolutely ####### impossible!

Page 27: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 27

The Middle

Page 28: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 28

Strategy

● Use high-level concurrency structures:● Actors● Dataflow● Communicating Sequential Processes (CSP)● Data Parallelism

Page 29: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 29

Actor Model

● A collection of processes that communicate by sending messages to each other.

● No global shared state.

Page 30: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 30

Dataflow Model

● A collection of processes that communicate by sending messages to each other.

● No global shared state.

Page 31: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 31

Communicating Sequential Processes

● A collection of processes that communicate by sending messages to each other.

● No global shared state.

Page 32: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 32

So what is the difference?

It's all in the message passing and hence synchronization.

And turning threads into a hidden and managed resource.

Page 33: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 33

The Abstract Model

Page 34: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 34

Actor Model

● Each actor has a message queue.● Actors can send messages asynchronously to any

other actor.● Actors read messages from their message queues, do

some work and send messages to other actors.

Page 35: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 35

Dataflow Model

● Each operator has a set of inputs: single assignment variables, or a queue of such things.

● Operator block until a given state of its inputs and then “fires” creating values on its outputs.

Page 36: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 36

Communicating Sequential Processes

● Each process has a set of input channels.● A process takes data from one of its channels

synchronously (rendezvous), computes and then writes to one of its output channels.

Page 37: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 37

Data Parallelism

● Data is in some array-like data structure.● At each stage of a computation, a transformation is

applied to all the items in the data structure.

Page 38: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 38

Sample Problems

● Sleeping Barber

● π by Quadrature

Page 39: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 39

The Sleeping Barber Problem

A barber sleeps in the cutting chair unless cutting someone's hair. Customers enter the shop: if the barber is asleep, the customer awakens

the barber, sits in the chair and gets a cut; if the barber is cutting the customer checks to see if there is a free waiting chair, and if there is sits to wait their turn or if not leaves the shop, uncut. On finishing a cut, the barber checks the waiting chairs to see if there is a new customer to cut. If there is, the customer moves to the cutting chair and gets a cut, if there

isn't the barber takes the cutting chair and sleeps.

Problem believed to be originally due to Edsger Dykstra, 1965.It is a model of a process management problem in operating systems.

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

Page 40: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 40

Operating Systems to Simulation

Implementing a solution to the problem in an operating systems context is essentially a

“solved” problem.

Extend the problem to be an example of concurrency and possible parallelism in simulation of a queueing

problem, and as a vehicle for trying various technologies.

Page 41: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 41

The Abstract Model

Barber

Shop

WorldCustomer

Customer

SuccessfulCustomer

SuccessfulCustomer

Customeror

Waiting ChairsCustomer

Page 42: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 42

π By Quadrature

4=∫0

1 1

1x2dx

=4n∑i=1

n 1

1i−0.5n

2

Page 43: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 43

The Code

Sleeping Barber : http://www.russel.org.uk/Bazaar/SleepingBarber

π By Quadrature: http://www.russel.org.uk/Bazaar/Pi_Quadrature

Page 44: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 44

The End

Page 45: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 45

Actors, dataflow, CSP, data parallelism are the high-level abstractions.

Shared memory multi-threading is low-level infrastructure.

Page 46: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 46

C++, Java, Groovy, Python, etc. are high-level programming languages.

Assembly language is low-level infrastructure.

Page 47: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 It'z Interactive Ltd 47

Advertising

Python for Rookies

Sarah Mount, James Shuttleworth andRussel Winder

Thomson Learning

Buy these books!Buy these books!

Now called Cengage Learning.

Developing Java Software Third Edition

Russel Winder and Graham Roberts

Wiley

Page 48: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 48

The Extra Questions

Page 49: Jeepers GPars, It's All So Easily ParallelJeepers GPars, It's All So Easily Parallel Russel Winder email: russel@russel.org.uk xmpp: russel@russel.org.uk twitter: russel_winder

Copyright © 2011 Russel Winder 49

The Whisky