GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 ·...

32
GPars Groovy Parallel Systems Václav Pech

Transcript of GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 ·...

Page 1: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

GPars

Groovy Parallel SystemsVáclav Pech

Page 2: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

We're all parallel now

Use them or leave them!

Page 4: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

# of cores

Today

Tomorrow

Page 5: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

images.eachParallel {it.process()}

documents.sumParallel()

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

Parallel Collections

Page 6: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Parallel Collections

progLanguages.parallel.filter {it.concurrent} .max {it.javaInteroperability} .map {it.logo} ==

Page 7: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Languages are either concurrent or obsolete.

Page 8: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Java 5

Asynchronous calculations

Page 9: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Java 7

Asynchronous calculationsFork/Join

Page 10: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Java 8

Asynchronous calculationsFork/JoinParallel collections

Page 11: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Scala

Asynchronous calculationsFork/JoinParallel collectionsActors

Page 12: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Clojure

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, Stm

Page 13: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Oz

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflow

Page 14: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Google's Go

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflowCSP

Page 15: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflowCSP

Page 16: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Asynchronous calculationsFork/JoinParallel collectionsActorsAgents, StmDataflowCSP

Agenda

Page 17: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Actors

Processes with a mail-boxShare no dataCommunicate by sending messagesUse a thread-pool

Page 18: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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

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

Page 19: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

int hash(String text) {…}

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

Page 23: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

int hash(String text) {…}

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

compare( hash( download() ), hash( loadFile() ))

Page 24: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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 25: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Composing async functions

Combine functions as usual

Parallelism is detected automatically

Page 26: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Dataflow Concurrency

No race-conditions No live-locks Deterministic deadlocks

Completely deterministic programs

BEAUTIFUL code (Jonas Bonér)

Page 27: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Dataflow Variables / Promisesmain task2 task3

x

yz

task1

Page 28: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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 29: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

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 30: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

Url resolverUrl resolver

Downloader

Groovy scanner Scala scanner

Url resolverUrl resolver

Reporter

Speculator

Evaluator

Splitter

Confirm

Cache updates

Approvals

Dataflow Operators

Page 31: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map
Page 32: GPars - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/VclavPech_Groovy... · 2011-11-23 · Parallel Collections progLanguages.parallel.filter {it.concurrent}.max {it.javaInteroperability}.map

'coz concurrency is Groovy

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

http://www.jroller.com/vaclav

http://twitter.com/vaclav_pech

GPars