GPars
-
Upload
russel-winder -
Category
Technology
-
view
1.694 -
download
1
description
Transcript of GPars
GParsThe concurrency and parallelism framework
for Groovy and Java applications
Russel Winder
@russel_winder [email protected]
http://www.russel.org.uk
Aims and Goals
Convince people that they:– Can deal with parallelism and concurrency without fear.
– Already understand GPars.
– Can use GPars tonight.
– …
…should tremble in fear atthe prospect of using
shared-memory multi-threading
with Java…
…and even with Groovy.
Concurrency
Execution as co-routines:
Sequences of code give up the executionto pass it to another coroutine.
More Concurrency
Concurrency is a technique founded in auniprocessor view of the world.
Time-division multiplexing.
Parallelism
Having multiple executions activeall at the same time.
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.
Squirrel behaviour emulatessynchronized software behaviour.
Thanks to Paul King …
Who uses synchronized?
Thanks to Paul King …
You did it wrong.
Small addition by me…
Who uses lock objects?
Small addition by me…
You definitely did it wrong.
Locks deny parallelism.
The whole purpose of a lock is toprevent parallelism.
Parallelism is performance improvement.
Performance improvement requires parallelism.
Locks deny performance improvement.
Locks are needed only ifthere is mutable shared state.
Avoid mutable shared state.
Use processes and message passing.
It's all easier if processesare single threaded.
…but how…
Use appropriate architectural models.
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.
32
ActorsIndependent processes communicating via asynchronous exchange of messages
33
DataflowOperators connected by channels with activity triggered by arrival of data on the channels.
34
CSPSequential processes connected by channels using synchronous message exchange (rendezvous).
35
We need examples.
36
37
What is the Value of ?
Easy, it's known exactly, it's (obviously).
38
It's simples
Александр Орлов 2009
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
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.
41
Commutative and Associative
Because addition is commutative andassociative, expression can be
decomposed into sums of partial sums.
42
a + b + c + d + e + f
=
( a + b ) + ( c + d ) + ( e + f )
43
Scatter – Gather
map reduce
44
Code…
45
But isn't this just data parallelism?
46
More code…
47
If you want the code, clone the Git repository:
http://www.russel.org.uk/Git/Pi_Quadrature.git
49
The Sleeping Barber Problem
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.
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.
52
Wikipedia article presents the classic operatingsystems approach using locks and semaphores.
http://en.wikipedia.org/wiki/Sleeping_barber_problem
53
More code…
54
If you want the code, clone the Git repository:
http://www.russel.org.uk/Git/SleepingBarber.git
56
57
58
59
60
61
62
Squirrels deny parallelism.
63
Squirrels deny performance enhancement.
64
Don't be a squirrel.
65
Do not use explicit locking algorithms.
66
Use computational architectures that promoteparallelism and hence performance improvement:
Actors
Dataflow
CSP
Data Parallelism
67
Use GPars.
Go on, you know you want to…