GPars

67
GPars The concurrency and parallelism framework for Groovy and Java applications Russel Winder @russel_winder [email protected] http://www.russel.org.uk

description

Slides from my session on GPars at Gr8Conf EU 2012. It was a manifesto speech for using actors, dataflow, CSP and data parallelism and avoiding all explicit locking and indeed explicit shared-memory multi-threading.

Transcript of GPars

Page 1: GPars

GParsThe concurrency and parallelism framework

for Groovy and Java applications

Russel Winder

@russel_winder [email protected]

http://www.russel.org.uk

Page 2: GPars

Aims and Goals

Convince people that they:– Can deal with parallelism and concurrency without fear.

– Already understand GPars.

– Can use GPars tonight.

– …

Page 3: GPars

…should tremble in fear atthe prospect of using

shared-memory multi-threading

with Java…

Page 4: GPars

…and even with Groovy.

Page 5: GPars

Concurrency

Execution as co-routines:

Sequences of code give up the executionto pass it to another coroutine.

Page 6: GPars

More Concurrency

Concurrency is a technique founded in auniprocessor view of the world.

Time-division multiplexing.

Page 7: GPars

Parallelism

Having multiple executions activeall at the same time.

Page 8: GPars

Concurrency is a tool for structuringexecution where a single processoris used by multiple computations.

Parallelism is about making acomputation complete faster than

using a single processor.

Page 9: GPars
Page 10: GPars
Page 11: GPars
Page 12: GPars
Page 13: GPars
Page 14: GPars
Page 15: GPars
Page 16: GPars

Squirrel behaviour emulatessynchronized software behaviour.

Page 17: GPars

Thanks to Paul King …

Who uses synchronized?

Page 18: GPars

Thanks to Paul King …

You did it wrong.

Page 19: GPars

Small addition by me…

Who uses lock objects?

Page 20: GPars

Small addition by me…

You definitely did it wrong.

Page 21: GPars

Locks deny parallelism.

Page 22: GPars

The whole purpose of a lock is toprevent parallelism.

Page 23: GPars

Parallelism is performance improvement.

Performance improvement requires parallelism.

Page 24: GPars

Locks deny performance improvement.

Page 25: GPars

Locks are needed only ifthere is mutable shared state.

Page 26: GPars

Avoid mutable shared state.

Page 27: GPars

Use processes and message passing.

Page 28: GPars

It's all easier if processesare single threaded.

Page 29: GPars

…but how…

Page 30: GPars

Use appropriate architectural models.

Page 31: GPars

31

ActorsIndependent processes communicating via asynchronous exchange of messages

CSPSequential processes connected by channels using synchronous message exchange (rendezvous).

DataflowOperators connected by channels with activity triggered by arrival of data on the channels.

Page 32: GPars

32

ActorsIndependent processes communicating via asynchronous exchange of messages

Page 33: GPars

33

DataflowOperators connected by channels with activity triggered by arrival of data on the channels.

Page 34: GPars

34

CSPSequential processes connected by channels using synchronous message exchange (rendezvous).

Page 35: GPars

35

We need examples.

Page 36: GPars

36

Page 37: GPars

37

What is the Value of ?

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

Page 38: GPars

38

It's simples

Александр Орлов 2009

Page 39: GPars

39

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 40: GPars

40

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. Use n = 107 or n = 109?

Embarrassingly parallel.

Page 41: GPars

41

Commutative and Associative

Because addition is commutative andassociative, expression can be

decomposed into sums of partial sums.

Page 42: GPars

42

a + b + c + d + e + f

=

( a + b ) + ( c + d ) + ( e + f )

Page 43: GPars

43

Scatter – Gather

map reduce

Page 44: GPars

44

Code…

Page 45: GPars

45

But isn't this just data parallelism?

Page 46: GPars

46

More code…

Page 47: GPars

47

If you want the code, clone the Git repository:

http://www.russel.org.uk/Git/Pi_Quadrature.git

Page 48: GPars

48

Or if you just want to browse:

http://www.russel.org.uk/gitweb

Page 49: GPars

49

The Sleeping Barber Problem

Page 50: GPars

50

The Sleeping Barber ProblemThe barber's shop has a single

cutting chair and a row of waiting seats.

The barber sleeps in the cutting chair unless trimming a customer.

Customers arrive at the shop at intervals.

If the barber is asleep, the customer wakes the barber sits in the cutting chair and gets a trim.

If the barber is cutting, a new customer checks to see if there is a free waiting seat.

– If there is join the queue to be trimmed.

– If there isn't leave disgruntled.

Problem originally dueto Edsger Dijkstra.

Page 51: GPars

51

A new customer enters the shop,check to see if they can go straightto the cutting chair, if not can theytake a waiting chair, if not leave.

The waiting chairs.

The cutting chair.

The barber's shop.

Page 52: GPars

52

Wikipedia article presents the classic operatingsystems approach using locks and semaphores.

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

Page 53: GPars

53

More code…

Page 54: GPars

54

If you want the code, clone the Git repository:

http://www.russel.org.uk/Git/SleepingBarber.git

Page 55: GPars

55

Or if you just want to browse:

http://www.russel.org.uk/gitweb

Page 56: GPars

56

Page 57: GPars

57

Page 58: GPars

58

Page 59: GPars

59

Page 60: GPars

60

Page 61: GPars

61

Page 62: GPars

62

Squirrels deny parallelism.

Page 63: GPars

63

Squirrels deny performance enhancement.

Page 64: GPars

64

Don't be a squirrel.

Page 65: GPars

65

Do not use explicit locking algorithms.

Page 66: GPars

66

Use computational architectures that promoteparallelism and hence performance improvement:

Actors

Dataflow

CSP

Data Parallelism

Page 67: GPars

67

Use GPars.

Go on, you know you want to…