Concurrency Unleash your processor(s) Vclav Pech About me - - PowerPoint PPT Presentation

concurrency
SMART_READER_LITE
LIVE PREVIEW

Concurrency Unleash your processor(s) Vclav Pech About me - - PowerPoint PPT Presentation

Concurrency Unleash your processor(s) Vclav Pech About me Passionate programmer Concurrency enthusiast GPars @ Codehaus lead Groovy contributor Technology evangelist @ JetBrains http://www.jroller.com/vaclav


slide-1
SLIDE 1

Concurrency

Unleash your processor(s) Václav Pech

slide-2
SLIDE 2

About me

Passionate programmer Concurrency enthusiast GPars @ Codehaus lead Groovy contributor Technology evangelist @ JetBrains

http://www.jroller.com/vaclav http://twitter.com/vaclav_pech

slide-3
SLIDE 3

We’re all in the parallel computing business!

slide-4
SLIDE 4
slide-5
SLIDE 5

# of cores

slide-6
SLIDE 6

# of cores

Today Soon

slide-7
SLIDE 7

Dealing with threads sucks!

public class Counter { private static long count = 0; public Counter() { count++; } }

slide-8
SLIDE 8

Dealing with threads sucks!

public class Counter { private static long count = 0; public Counter() { synchronized (this) { count++; } } }

slide-9
SLIDE 9

Dealing with threads sucks!

public class Counter { private static long count = 0; public Counter() { synchronized (this.getClass()) { count++; } } }

slide-10
SLIDE 10

Dealing with threads sucks!

public class ClickCounter implements ActionListener { public ClickCounter(JButton button) { button.addActionListener(this); } public void actionPerformed(final ActionEvent e) { ... } }

slide-11
SLIDE 11

Stone age of parallel SW

Dead-locks Live-locks Race conditions Starvation Shared Mutable State

slide-12
SLIDE 12

Multithreaded programs today work mostly by accident!

slide-13
SLIDE 13

Map/Reduce Fork/Join Actors STM Dataflow Agent

Can we do better?

slide-14
SLIDE 14

Asynchronous invocation

Future f = threadPool.submit(calculation); … System.out.println(“Result: “ + f.get());

slide-15
SLIDE 15

Thread Pool

Tasks Worker threads Queue

slide-16
SLIDE 16

Thread Pool

Contention!

slide-17
SLIDE 17

Fork/Join Thread Pool

slide-18
SLIDE 18

Fork/Join Thread Pool

Work stealing

slide-19
SLIDE 19

Fork/Join

 Solve hierarchical

problems

 Divide and conquer

 Merge sort, Quick sort  Tree traversal  File scan / search  …

[a, b, c, d, e, f, g, h]

[a, b, c, d] [e, f, g, h]

[a, b] [c, d] [e, f] [g, h]

slide-20
SLIDE 20

Fork/Join (GPars)

runForkJoin(new File(“./src”)) {currentDir -> long count = 0; currentDir.eachFile { if (it.isDirectory()) { forkOffChild it } else { count++ } } return count + childrenResults.sum(0) }

Waits for children without blocking the thread!

slide-21
SLIDE 21

Collections (Groovy/GPars)

images.eachParallel {it.process()} documents.sumParallel() candidates.maxParallel {it.salary}.marry()

slide-22
SLIDE 22

Parallel Arrays (jsr-166y)

ParallelArray namesOfWomen = people.withFilter(aWoman).withMapping(retrieveName).all();

Ops.Predicate aWoman = new Ops.Predicate() { public boolean op(Person friend) {return !friend.isMale();} }; Ops.Op retrieveName = new Ops.Op() { public Object op(Person friend) {return friend.getName();} };

slide-23
SLIDE 23

Dataflow Concurrency

 No race-conditions  No live-locks  Deterministic deadlocks

Completely deterministic programs

BEAUTIFUL code

(Jonas Bonér)

slide-24
SLIDE 24

Milestone

Asynchronous calculations Fork/Join Parallel collection processing Dataflow variables/streams

slide-25
SLIDE 25

Message Passing

Actors

Processes with mailboxes

Communicating Sequential Processes (CSP)

Sequential processes synchronously talking through channels

Dataflow Operators

Event-triggered computations connected by channels into a graph

slide-26
SLIDE 26

Actors

 Isolated  Communicating

Immutable messages

 Active

Pooled shared threads

 Activities

Create a new actor Send a message Receive a message

Actor Actor Actor Actor Actor Actor Actor TTT Thread pool

slide-27
SLIDE 27

Actors use

Gate Keeper Form HTTP Finger Prints Address Check Email Check Process Fraud Detect Response SOAP SMTP

slide-28
SLIDE 28

Actors patterns

Enricher Router Translator Endpoint Splitter Aggregator Filter Resequencer Checker

slide-29
SLIDE 29

Sending messages

buddy.send 10.eur buddy << new Book(title:’Groovy Recipes’, author:’Scott Davis’) def canChat = buddy.sendAndWait ‘Got time?’ buddy.sendAndContinue ‘Need money!’, {cash-> pocket.add cash }

slide-30
SLIDE 30

Stateless Actors (pure Java)

class MyActor extends DynamicDispatchActor { private Account account = ... public void onMessage(String msg) { String encripted = encrypt(msg); reply(encripted); } public void onMessage(Integer number) { reply(2 * number); } public void onMessage(Money cash) { System.out.println("Received a donation " + cash); account.deposit(cash); } }

slide-31
SLIDE 31

Stateful Actors

class MyActor extends DefaultActor { void act() { def buddy = new YourActor() buddy << ‘Hi man, how\’re things?’ def response = receive() } }

slide-32
SLIDE 32

Implicit State in Actors

val me = actor { react {msg1 -> switch (msg1) { case Work: reply “I don't work so early” ; stop(); case Breakfast: msg1.eat() react {msg2 → switch (msg2) { case Work: reply “OK, time to work”; msg2.do() case Lunch: ... } } } }

slide-33
SLIDE 33

Continuation Style

loop { … react { … react {/* schedule the block and exit */ … } //Never reached } //Never reached } //Never reached

slide-34
SLIDE 34

Java actor frameworks

Jetlang Kilim ActorFoundry Actorom Akka GPars (Yes!) …

slide-35
SLIDE 35

Shared Mutable State

Misused most of the time When really needed, use

 Agents  Software Transactional Memory  Locks

slide-36
SLIDE 36

No more threads and locks

images.eachParallel { //concurrency agnostic code here } def myActor = actor { //concurrency agnostic code here } atomic { /*concurrency agnostic code here*/ } …

slide-37
SLIDE 37

Summary

Parallelism is not hard, multi-threading is

Jon Kerridge, Napier University

slide-38
SLIDE 38

Questions?

Find more at:

http://gpars.codehaus.org http://www.jroller.com/vaclav http://twitter.com/vaclav_pech