GR8Conf 2011: GPars
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

GR8Conf 2011: GPars

on

  • 2,025 views

 

Statistics

Views

Total Views
2,025
Views on SlideShare
2,015
Embed Views
10

Actions

Likes
2
Downloads
34
Comments
0

2 Embeds 10

http://universegroovygrails.blogspot.com 8
http://paper.li 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

GR8Conf 2011: GPars Presentation Transcript

  • 1. GPars – coz concurrency is GroovyVáclav Pech, JetBrains
  • 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. We can do better! Agent Map/Reduce Dataflow Fork/Join AsyncFun Actors
  • 4. Intel I7 quad-core
  • 5. Collectionsimages.eachParallel {it.process()}documents.sumParallel()candidates.maxParallel {it.salary}.marry()
  • 6. Transparently parallelwithPool(4) { images.makeConcurrent() .findAll {it.contains me} .collect {convert it} .groupBy {it.size()}}
  • 7. Parallelize existing codebuyBargain ( stocks.makeConcurrent() )def buyBargain(stocks) { buy stocks.findAll { it.download().price < 50 }} Use with CAUTION!
  • 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. Asynchronous invocationFuture f = threadPool.submit(calculation);…System.out.println(“Result: “ + f.get());main calculation get()
  • 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. Asynchronous closuresdef resize = {img -> img.resize(64, 64)}def fastResize = resize.asyncFun()def resized = images.collect fastResize…createAlbum resized*.get()
  • 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. 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. 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. Composable Asynchronous FunctionsValid combinations of functions are also valid combinations oftheir asynchronous variantsParallelism is detected automatically without programmerintervention
  • 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. Hierarchical / Recursive Functionsdef fib = {n -> if (n <=2) return 1 forkOffChild(n - 1) forkOffChild(n - 2) return childrenResults.sum()}
  • 18. Dataflow Concurrency● No race-conditions● No live-locks● Deterministic deadlocks Completely deterministic programs BEAUTIFUL code (Jonas Bonér)
  • 19. Dataflow Variables / Promisesmain task1 task2 task3 x z y
  • 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. 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. Dataflow Operators Url resolver Url resolver Downloader Groovy scanner Scala scanner Reporter
  • 23. Dataflow Operators Url resolver Speculator Cache Downloader updates Evaluator Approvals Splitter Groovy scanner Scala scanner Reporter Confirm
  • 24. A CSP flavourCommunicating Sequential Processes Focus on composable processes more than on data
  • 25. Dataflow Processes Out a Prefix - 0 Copy b Successorc
  • 26. Dataflow Processes Out a Prefix - 0 Copy b Successorc Numbers Out
  • 27. Dataflow ProcessesIn Out a Plus Copy b Prefix - 0 c
  • 28. Dataflow ProcessesIn Out a Plus Copy b Prefix - 0 c In Integrate Out
  • 29. Dataflow Processes Numbers Out In Integrate Out
  • 30. Dataflow Processes Numbers Integrate Out
  • 31. Dataflow Processes Numbers Integrate Out
  • 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. 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. Stateful Actorsclass MyActor extends AbstractPooledActor { void act() { def buddy = new YourActor() buddy << ‘Hi man, how’re things?’ def response = receive() }}
  • 35. Active Objects@ActiveObjectclass MyCounter { private int counter = 0@ActiveMethod def incrementBy(int value) { println "Received an integer: $value" this.counter += value }}
  • 36. IntegrationBundled with Groovy distsAvailable the maven central repository(Maven, Ivy, Gradle, Grape)@Grab(group=org.codehaus.gpars, module=gpars, version=0.11)
  • 37. RoadmapSTMActor and DataFlow remoting
  • 38. GPars – coz concurrency is Groovy!Find more at: http://gpars.codehaus.org http://www.jroller.com/vaclav http://twitter.com/vaclav_pech