Your SlideShare is downloading. ×
0
GPars – coz concurrency is GroovyVáclav Pech, JetBrains
Whos this geek?Passionate programmerConcurrency enthusiastGPars leadGroovy contributorTechnology evangelist / developer @ ...
We can do better!                           Agent              Map/Reduce            Dataflow               Fork/Join     ...
Intel I7 quad-core
Collectionsimages.eachParallel {it.process()}documents.sumParallel()candidates.maxParallel {it.salary}.marry()
Transparently parallelwithPool(4) {  images.makeConcurrent()    .findAll {it.contains me}    .collect {convert it}    .gro...
Parallelize existing codebuyBargain ( stocks.makeConcurrent() )def buyBargain(stocks) {  buy stocks.findAll {     it.downl...
Functional flavorMap / Reduce   map, reduce, filter, min, max, sum, …(1..n).parallel.filter {it%2==0}  .map {it ** 2}  .re...
Asynchronous invocationFuture f = threadPool.submit(calculation);…System.out.println(“Result: “ + f.get());main           ...
Call closures asynchronouslydef isSelfPortrait = {image -> image.contains me}SYNC:def flag = isSelfPortrait.call(img1)ASYN...
Asynchronous closuresdef resize = {img -> img.resize(64, 64)}def fastResize = resize.asyncFun()def resized = images.collec...
Composable Asynchronous Functionsint hash1 = hash(download(http://www.gpars.org))int hash2 = hash(loadFile(/gpars/website/...
Composable Asynchronous Functionshash = hash.asyncFun(); compare = compare.asyncFun()download = download.asyncFun()loadFil...
Composable Asynchronous Functions                         int hash(String text) {…}Promise<int> hash(Promise<String> | Str...
Composable Asynchronous FunctionsValid combinations of functions are also valid combinations oftheir asynchronous variants...
Fork/Join                                                  [a, b, c, d, e, f, g, h]●    Solve hierarchical    problems    ...
Hierarchical / Recursive Functionsdef fib = {n ->     if (n <=2) return 1     forkOffChild(n - 1)     forkOffChild(n - 2) ...
Dataflow Concurrency●    No race-conditions●    No live-locks●    Deterministic deadlocks    Completely deterministic prog...
Dataflow Variables / Promisesmain                task1    task2   task3                              x                z   ...
DataFlow Variablesdef df = new DataFlows()task { df.z = df.x + df.y }task { df.x = 10 }task {  println ”I am task 3”  df.y...
Dataflow Operatorsoperator(inputs: [headers, bodies, footers],       outputs: [articles, summaries])  {header, body, foote...
Dataflow Operators                      Url resolver                       Url resolver                      Downloader   ...
Dataflow Operators                       Url resolver       Speculator       Cache              Downloader                ...
A CSP flavourCommunicating Sequential Processes           Focus on composable processes more than on data
Dataflow Processes                                        Out                        a       Prefix - 0                Cop...
Dataflow Processes                                           Out                        a       Prefix - 0                ...
Dataflow ProcessesIn                                       Out                         a       Plus                       ...
Dataflow ProcessesIn                                                 Out                         a       Plus             ...
Dataflow Processes   Numbers     Out                     In   Integrate   Out
Dataflow Processes   Numbers           Integrate   Out
Dataflow Processes   Numbers           Integrate                                 Out
Actors                                Actor    Isolated                     Actor●                                  Actor ...
Stateless Actorsclass MyActor extends DynamicDispatchActor {   Account account = ...   public void onMessage(String msg) {...
Stateful Actorsclass MyActor extends AbstractPooledActor {   void act() {     def buddy = new YourActor()     buddy << ‘Hi...
Upcoming SlideShare
Loading in...5
×

GR8Conf 2011: GPars

1,625

Published on

Published in: Technology, Design
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,625
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "GR8Conf 2011: GPars"

  1. 1. GPars – coz concurrency is GroovyVáclav Pech, JetBrains
  2. 2. Whos this geek?Passionate programmerConcurrency enthusiastGPars leadGroovy contributorTechnology evangelist / developer @ JetBrainsJetBrains Academy member http://www.jroller.com/vaclav http://twitter.com/vaclav_pech
  3. 3. We can do better! Agent Map/Reduce Dataflow Fork/Join AsyncFun Actors
  4. 4. Intel I7 quad-core
  5. 5. Collectionsimages.eachParallel {it.process()}documents.sumParallel()candidates.maxParallel {it.salary}.marry()
  6. 6. Transparently parallelwithPool(4) { images.makeConcurrent() .findAll {it.contains me} .collect {convert it} .groupBy {it.size()}}
  7. 7. Parallelize existing codebuyBargain ( stocks.makeConcurrent() )def buyBargain(stocks) { buy stocks.findAll { it.download().price < 50 }} Use with CAUTION!
  8. 8. Functional flavorMap / Reduce map, reduce, filter, min, max, sum, …(1..n).parallel.filter {it%2==0} .map {it ** 2} .reduce {a, b -> a + b}
  9. 9. Asynchronous invocationFuture f = threadPool.submit(calculation);…System.out.println(“Result: “ + f.get());main calculation get()
  10. 10. Call closures asynchronouslydef isSelfPortrait = {image -> image.contains me}SYNC:def flag = isSelfPortrait.call(img1)ASYNC:def future = isSelfPortrait.callAsync(img1)…def flag = future.get()
  11. 11. Asynchronous closuresdef resize = {img -> img.resize(64, 64)}def fastResize = resize.asyncFun()def resized = images.collect fastResize…createAlbum resized*.get()
  12. 12. Composable Asynchronous Functionsint hash1 = hash(download(http://www.gpars.org))int hash2 = hash(loadFile(/gpars/website/index.html))boolean result = compare(hash1, hash2)println result
  13. 13. Composable Asynchronous Functionshash = hash.asyncFun(); compare = compare.asyncFun()download = download.asyncFun()loadFile = loadFile.asyncFun()def hash1 = hash(download(http://www.gpars.org))def hash2 = hash(loadFile(/gpars/website/index.html))def result = compare(hash1, hash2)println result.get()
  14. 14. Composable Asynchronous Functions int hash(String text) {…}Promise<int> hash(Promise<String> | String text) { 1. Return a Promise for the result 2. Wait (non-blocking) for the text param 3. Call the original hash() 4. Bind the result}
  15. 15. Composable Asynchronous FunctionsValid combinations of functions are also valid combinations oftheir asynchronous variantsParallelism is detected automatically without programmerintervention
  16. 16. Fork/Join [a, b, c, d, e, f, g, h]● Solve hierarchical problems  Divide and conquer  Merge sort, Quick sort [a, b, c, d] [e, f, g, h]  Tree traversal  File scan / search … [a, b] [c, d] [e, f] [g, h]
  17. 17. Hierarchical / Recursive Functionsdef fib = {n -> if (n <=2) return 1 forkOffChild(n - 1) forkOffChild(n - 2) return childrenResults.sum()}
  18. 18. Dataflow Concurrency● No race-conditions● No live-locks● Deterministic deadlocks Completely deterministic programs BEAUTIFUL code (Jonas Bonér)
  19. 19. Dataflow Variables / Promisesmain task1 task2 task3 x z y
  20. 20. DataFlow Variablesdef 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
  21. 21. Dataflow Operatorsoperator(inputs: [headers, bodies, footers], outputs: [articles, summaries]) {header, body, footer -> def article = buildArticle(header, body, footer) bindOutput(0, article) bindOutput(1, buildSummary(article)) } * <> +
  22. 22. Dataflow Operators Url resolver Url resolver Downloader Groovy scanner Scala scanner Reporter
  23. 23. Dataflow Operators Url resolver Speculator Cache Downloader updates Evaluator Approvals Splitter Groovy scanner Scala scanner Reporter Confirm
  24. 24. A CSP flavourCommunicating Sequential Processes Focus on composable processes more than on data
  25. 25. Dataflow Processes Out a Prefix - 0 Copy b Successorc
  26. 26. Dataflow Processes Out a Prefix - 0 Copy b Successorc Numbers Out
  27. 27. Dataflow ProcessesIn Out a Plus Copy b Prefix - 0 c
  28. 28. Dataflow ProcessesIn Out a Plus Copy b Prefix - 0 c In Integrate Out
  29. 29. Dataflow Processes Numbers Out In Integrate Out
  30. 30. Dataflow Processes Numbers Integrate Out
  31. 31. Dataflow Processes Numbers Integrate Out
  32. 32. Actors Actor Isolated Actor● Actor Actor Communicating Actor● Actor Actor  Immutable messages● Active  Pooled shared threads● Activities  Create a new actor  Send a message TT  Receive a message T Thread pool
  33. 33. Stateless Actorsclass MyActor extends DynamicDispatchActor { 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); }}
  34. 34. Stateful Actorsclass MyActor extends AbstractPooledActor { void act() { def buddy = new YourActor() buddy << ‘Hi man, how’re things?’ def response = receive() }}
  35. 35. Active Objects@ActiveObjectclass MyCounter { private int counter = 0@ActiveMethod def incrementBy(int value) { println "Received an integer: $value" this.counter += value }}
  36. 36. IntegrationBundled with Groovy distsAvailable the maven central repository(Maven, Ivy, Gradle, Grape)@Grab(group=org.codehaus.gpars, module=gpars, version=0.11)
  37. 37. RoadmapSTMActor and DataFlow remoting
  38. 38. GPars – coz concurrency is Groovy!Find more at: http://gpars.codehaus.org http://www.jroller.com/vaclav http://twitter.com/vaclav_pech
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×