gr8conf 2011: gpars

34
Václav Pech JetBrains GPars

Upload: gr8conf

Post on 17-May-2015

1.603 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: GR8Conf 2011: GPars

Václav PechJetBrains

GPars

Page 2: GR8Conf 2011: GPars

About me

['Passionate programmer','Concurrency enthusiast','GPars lead','Developer/technology evangelist @ JetBrains'].eachParallel {say it}

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

Page 3: GR8Conf 2011: GPars

We're all parallel now

Use them or leave them!

Page 4: GR8Conf 2011: GPars
Page 5: GR8Conf 2011: GPars

# of cores

Today

Tomorrow

Page 6: GR8Conf 2011: GPars

Collections (GPars)

images.eachParallel {it.process()}

documents.sumParallel()

candidates.maxParallel {it.salary}.marry()

Page 7: GR8Conf 2011: GPars

Languages are either concurrent or obsolete.

Page 8: GR8Conf 2011: GPars

Java 5

Asynchronous calculations

Page 9: GR8Conf 2011: GPars

Java 7

Asynchronous calculationsFork/Join

Page 10: GR8Conf 2011: GPars

Java 8

Asynchronous calculationsFork/JoinParallel collections

Page 11: GR8Conf 2011: GPars

Scala

Asynchronous calculationsFork/JoinParallel collectionsActors

Page 12: GR8Conf 2011: GPars

Clojure

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, Stm

Page 13: GR8Conf 2011: GPars

Oz

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflow

Page 14: GR8Conf 2011: GPars

Google's Go

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflowCSP

Page 15: GR8Conf 2011: GPars

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflowCSP

Page 16: GR8Conf 2011: GPars

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflowCSP

Agenda

Page 17: GR8Conf 2011: GPars

Asynchronous invocation

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

main calculation

get()

Page 18: GR8Conf 2011: GPars

Asynchronous closures

def resize = {img -> img.resize(64, 64)}def fastResize = resize.asyncFun()

def resized = fastResize(myImage)…resized.get()

Page 19: GR8Conf 2011: GPars

Composing async functions

int hash1 = hash(download('http://www.gpars.org')) int hash2 = hash(loadFile('/gpars/website/index.html')) boolean result = compare(hash1, hash2)println result

Page 20: GR8Conf 2011: GPars

Composing async functions@AsyncFun hash = oldHash@AsyncFun compare = oldCompare@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFile

def hash1 = hash(download('http://www.gpars.org')) def hash2 = hash(loadFile('/gpars/website/index.html')) def result = compare(hash1, hash2)println result.get()

Page 21: GR8Conf 2011: GPars

Composing async functions@AsyncFun hash = oldHash@AsyncFun(blocking = true) compare = oldCompare@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFile

def hash1 = hash(download('http://www.gpars.org')) def hash2 = hash(loadFile('/gpars/website/index.html')) boolean result = compare(hash1, hash2)println result

Page 22: GR8Conf 2011: GPars

int hash(String text) {…}

Promise<int> hash(Promise<String> | String text)

Page 23: GR8Conf 2011: GPars

int hash(String text) {…}

Promise<int> hash(Promise<String> | String text) {1. Return a Promise for the result2. Wait (non-blocking) for the text param3. Call the original hash()4. Bind the result

}

Page 24: GR8Conf 2011: GPars

Composing async functions

Combine functions as usual

Parallelism is detected automatically

Page 25: GR8Conf 2011: GPars

Dataflow Concurrency

No race-conditions No live-locks Deterministic deadlocks

Completely deterministic programs

BEAUTIFUL code (Jonas Bonér)

Page 26: GR8Conf 2011: GPars

Dataflow Variables / Promisesmain task2 task3

x

yz

task1

Page 27: GR8Conf 2011: GPars

Dataflows

def df = new Dataflows()

task { df.z = df.x + df.y }task { df.x = 10 }task { println ”I am task 3” df.y = 5 }assert 15 == df.z

Page 28: GR8Conf 2011: GPars

operator(inputs: [headers, bodies, footers], outputs: [articles, summaries]) {header, body, footer -> def article = buildArticle(header, body, footer) bindOutput(0, article) bindOutput(1, buildSummary(article)) } *

+<>

Dataflow Operators

Page 29: GR8Conf 2011: GPars

Url resolverUrl resolver

Downloader

Groovy scanner Scala scanner

Url resolverUrl resolver

Reporter

Speculator

Evaluator

Splitter

Confirm

Cache updates

Approvals

Dataflow Operators

Page 30: GR8Conf 2011: GPars

Actors

Isolated Communicating

Immutable messages Active

Pooled shared threads Activities

Create a new actorSend a messageReceive a message

ActorActorActorActorActorActorActor

TTTThread pool

Page 31: GR8Conf 2011: GPars

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); }}

Page 32: GR8Conf 2011: GPars

Active Objects@ActiveObject class MyCounter { private int counter = 0

@ActiveMethod def incrementBy(int value) { println "Received an integer: $value" this.counter += value }}

Page 33: GR8Conf 2011: GPars

No more threads and locks

images.parallel.map { //concurrency agnostic code here}

def myActor = actor { //concurrency agnostic code here}

Identically with agents, fork/join, dataflow, …

Page 34: GR8Conf 2011: GPars

'coz concurrency is Groovy

Find more at:http://gpars.codehaus.org

http://www.jroller.com/vaclav

http://twitter.com/vaclav_pech

GPars