GPars in Saga Groovy Study

1,258 views
1,121 views

Published on

佐賀Groovy勉強会のGParsの資料です。

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,258
On SlideShare
0
From Embeds
0
Number of Embeds
289
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

GPars in Saga Groovy Study

  1. 1. Groovy with GPars @Naoki RinGParsGParsGPars 6 GroovyMap/ReduceFork/Join withAgentDataflowVariableDataflowsDataflowQueue @Naoki Rin
  2. 2. Groovy with GPars @Naoki RinGParsGParsGPars 6 Twitter @Naoki RinMap/ReduceFork/Join Hatena id:naokirin Groovy 1 JavaAgent Groovy GroovyDataflowVariableDataflowsDataflowQueue
  3. 3. Groovy with GPars @Naoki RinGParsGParsGPars 6 . .1 GPars 2Map/ReduceFork/Join . .3 4Agent . .5DataflowVariableDataflows 6DataflowQueue ..
  4. 4. GParsGroovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  5. 5. GParsGroovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join Groovy DSL Groovy1.8AgentDataflowVariableDataflowsDataflowQueue
  6. 6. GParsGroovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join Groovy DSL Groovy1.8Agent GroovyDataflowVariableDataflowsDataflowQueue
  7. 7. GParsGroovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join Groovy DSL Groovy1.8Agent GroovyDataflowVariableDataflowsDataflowQueue
  8. 8. GParsGroovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join Groovy DSL Groovy1.8Agent GroovyDataflowVariableDataflowsDataflowQueue
  9. 9. GParsGroovy with GPars @Naoki RinGPars Groovy 1.8 GPars 0.11GParsGPars 6 GroovyMap/ReduceFork/Join Groovy 1.8.2 GPars 0.12 GPars 0.12 Groovy gpars-0.11.jarAgent gpars-0.12.jar OK Groovy 1.8 @Grab OKDataflowVariableDataflowsDataflowQueue GPars 0.12
  10. 10. GPars 6Groovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  11. 11. GPars 6Groovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join StmAgent Groovy CSPDataflowVariableDataflowsDataflowQueue
  12. 12. GPars 6Groovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join StmAgent Groovy CSPDataflowVariableDataflowsDataflowQueue
  13. 13. GPars 6Groovy with GPars @Naoki RinGParsGParsGPars 6 GParsMap/ReduceFork/Join StmAgent Groovy CSPDataflowVariableDataflows 4DataflowQueue
  14. 14. Groovy with GPars @Naoki RinGPars (Data Parallelism)GParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  15. 15. Groovy with GPars @Naoki RinGPars (Data Parallelism)GParsGPars 6 Go!Map/ReduceFork/Join ( ) Go! Map/Reduce Go!Agent Map/Reduce Fork/Join Go!DataflowVariableDataflows Fork/JoinDataflowQueue
  16. 16. Groovy with GPars @Naoki RinGParsGParsGPars 6 GroovyMap/ReduceFork/Join GParsPool - JSR-166y(ParallelArray) DSLAgent GParsExecutorsPool -DataflowVariable Java ExecutorDataflowsDataflowQueue
  17. 17. Groovy with GPars @Naoki RinGParsGParsGPars 6 import groovyx.gpars.GParsPoolMap/ReduceFork/Join . GParsPool.withPool { assert ([2, 3, 4, 5]== [1, 2, 3, 4].collectParallel {it + 1}) } .Agent withPool collectParallel() collectDataflowVariableDataflowsDataflowQueue .. .
  18. 18. xxxParallelGroovy with GPars @Naoki RinGPars withPool xxxParallel()GParsGPars 6Map/ReduceFork/Join withPool xxx findAll() findAllParallel() import groovyx.gpars.GParsPoolAgent GParsPool.withPool { assert ([1, 2, 3, 4 ].findAll{it % 2 == 0}DataflowVariableDataflows == [1, 2, 3, 4 ].findAllParallel{it % 2 == 0})DataflowQueue } .
  19. 19. xxxParallelGroovy with GPars @Naoki RinGParsGPars GParsPool.withPoolGPars 6Map/ReduceFork/Join anyParallel() findParallel() collectParallel() foldParallel() countParallel() grepParallel() eachParallel() groupByParallel() eachWithIndexParallel() maxParallel()Agent everyParallel() minParallel() findAllParallel() splitParallel()DataflowVariableDataflowsDataflowQueue findAnyParallel() sumParallel()
  20. 20. Groovy with GPars @Naoki RinGParsGParsGPars 6 withPoolMap/ReduceFork/Join import groovyx.gpars.GParsPool . GParsPool.withPool(10) { // }Agent withPool . +1DataflowVariableDataflowsDataflowQueue .. .
  21. 21. Groovy with GPars @Naoki RinGParsGPars withPoolGPars 6 . import groovyx.gpars.ParallelEnhancerMap/Reduce def list = [4, 1, 5, 3, 2 ]Fork/Join ParallelEnhancer.enhanceInstance(list) assert (list.findParallel {it == 3} == 3) . Parallel import groovyx.gpars.GParsPoolAgent .GParsPool.withPool{ . . def data = (1..3).makeConcurrent()DataflowVariableDataflows assert( data.collect {it ** 2} == [1, 4, 9] )DataflowQueue } .
  22. 22. ( )Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/Join GParsPool.withPool() { Future result = {it * 2}.callAsync(3) assert 6 == result.get() .}Agent withPool callAsync() . Future get()DataflowVariableDataflowsDataflowQueue .. .
  23. 23. Map/ReduceGroovy with GPars @Naoki RinGParsGParsGPars 6 Map/ReduceMap/ReduceFork/Join GPars map /Agent reduce map (DataflowVariableDataflows )DataflowQueue
  24. 24. GPars Map/ReduceGroovy with GPars @Naoki RinGParsGPars GPars Map/Reduce Parallel ArrayGPars 6Map/Reduce Parallel ArrayFork/Join parallel Parallel Array getParallel() ParallelAgent ArrayDataflowVariable Parallel Array MapDataflowsDataflowQueue collection
  25. 25. Map/ReduceGroovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/Join map() Parallel Array(AbstractPAWrapper) reduce() T filter() Parallel Array(AbstractPAWrapper) size() int sum() TAgent min() T max() TDataflowVariable sort() Parallel Array(AbstractPAWrapper)DataflowsDataflowQueue groupBy() Map combine() Map
  26. 26. Map/ReduceGroovy with GPars @Naoki RinGParsGParsGPars 6 Map . GParsPool.withPool {Map/Reduce println (”Hello World”.parallelFork/Join .map{[it, 1]} .combine(0) { sum, value -> sum + value} .getParallel() .sort{-it.value} .collection) }Agent .DataflowVariableDataflows [l=3, o=2, H=1, e=1, W=1, =1, d=1, r=1]DataflowQueue
  27. 27. Fork/JoinGroovy with GPars @Naoki RinGPars forkGParsGPars 6Map/ReduceFork/Join join forkAgentDataflowVariableDataflowsDataflowQueue
  28. 28. Fork/JoinGroovy with GPars @Naoki RinGParsGParsGPars 6 forkOffChild() -Map/ReduceFork/Join runChildDirectly() - GPars 0.12Agent getChildrenResults() - forkDataflowVariable runForkJoin() -DataflowsDataflowQueue withPool Fork/Join
  29. 29. Fork/JoinGroovy with GPars @Naoki RinGParsGPars fibonacciGPars 6 . import groovyx.gpars.GParsPoolMap/Reduce Closure fib = {number -> if (number <= 2) {Fork/Join return 1 } forkOffChild(number - 1) final def result = runChildDirectly(number - 2) return (Integer) getChildrenResults().sum() + result }Agent GParsPool.withPool {DataflowVariable assert 55 == GParsPool.runForkJoin(10, fib)DataflowsDataflowQueue } .
  30. 30. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  31. 31. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  32. 32. GParsGroovy with GPars @Naoki RinGParsGPars .GPars 6 import static groovyx.gpars.actor.Actors.* def console = actor { //Map/ReduceFork/Join loop { react {msg -> println msg } } }Agent . console.send ’Hello World!’ //DataflowVariableDataflowsDataflowQueue Hello World
  33. 33. Groovy with GPars @Naoki RinGParsGParsGPars 6 1 import static groovyx.gpars.actor.Actors.*Map/ReduceFork/Join . def console = actor { react {msg -> println msg } }Agent react . reactDataflowVariableDataflowsDataflowQueue
  34. 34. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/Join console.send ’Hello World!’ // console ’Hello World!’ // . console << ’Hello World!’ // console.call ’Hello World!’ //Agent .DataflowVariableDataflowsDataflowQueue .. .
  35. 35. Groovy with GPars @Naoki RinGParsGParsGPars 6 . import static groovyx.gpars.actor.Actors.*Map/ReduceFork/Join def console = actor { loop{ react {msg -> println msg } }Agent } . loop reactDataflowVariableDataflowsDataflowQueue
  36. 36. reactorGroovy with GPars @Naoki RinGParsGParsGPars 6Map/Reduce reactorFork/Join import static groovyx.gpars.actor.Actors.* def console = reactor { // } . actor{loop{react{ }}} reactor{}Agent .DataflowVariableDataflowsDataflowQueue .. .
  37. 37. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/Join console.send ’Hello!’ console.send ’Hello!!’ . console.stop() console.join() join() .AgentDataflowVariableDataflowsDataflowQueue .. .
  38. 38. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/Reduce def console = reactor {reply it.reverse()}Fork/Join assert ’Hello!’ == console.sendAndWait(’ !olleH’) . reply sendAndWait() .Agent .. .DataflowVariableDataflowsDataflowQueue
  39. 39. Groovy with GPars @Naoki RinGParsGParsGPars 6 def parrot = reactor { .Map/Reduce println ’Parrot: ’ + itFork/Join sender.send it } def mary = actor { println ’Mary: Hello!’ parrot.send ’Hello!’Agent react{println ’Mary: Good!’} } .DataflowVariableDataflows senderDataflowQueue
  40. 40. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  41. 41. Groovy with GPars @Naoki RinGParsGParsGPars 6 ( )Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  42. 42. GParsGroovy with GPars @Naoki RinGParsGParsGPars 6 . import groovyx.gpars.GParsPool import groovyx.gpars.agent.AgentMap/ReduceFork/Join def agent = new Agent(1) GParsPool.withPool { (1..10).eachParallel {value -> agent {updateValue(it * value)} } }Agent println agent.val .DataflowVariableDataflowsDataflowQueue 3628800
  43. 43. AgentGroovy with GPars @Naoki RinGParsGParsGPars 6 Agent import groovyx.gpars.agent.AgentMap/Reduce .Fork/Join def intAgent = new Agent(0) def stringAgent = new Agent<String>() assert 0 == intAgent.val . AgentAgent GroovyDataflowVariableDataflows .valDataflowQueue .. .
  44. 44. Groovy with GPars @Naoki RinGParsGPars .GPars 6 import groovyx.gpars.agent.Agent def jvmLangList = new Agent<List<String>>()Map/ReduceFork/Join jvmLangList {updateValue([’Java’])} jvmLangList.send {it << ’Scala’} jvmLangList.call {it.add ’Clojure’} jvmLangList jvmLangList.val + ’Groovy’ assert [’Java’, ’Scala’, ’Clojure’, ’Groovy’] . == jvmLangList.valAgent updateValue()DataflowVariableDataflowsDataflowQueue
  45. 45. Groovy with GPars @Naoki Rin .GParsGPars def members = new Agent([’Jones’])GPars 6 final Thread t1 = Thread.start { members {it.add ’Bob’}Map/Reduce members {it.add ’Davis’}Fork/Join } final Thread t2 = Thread.start { members {it.add ’Alice’} members {it.remove(0)} }Agent // [Jones, Bob, Davis, Alice] . members.valAsync {println it}DataflowVariable .valDataflowsDataflowQueue valAsync() valAsync()
  46. 46. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  47. 47. ( )Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue
  48. 48. GPars DataflowGroovy with GPars @Naoki RinGPars .GParsGPars 6 import groovyx.gpars.dataflow.Dataflow import groovyx.gpars.dataflow.DataflowVariableMap/Reduce final def x = new DataflowVariable()Fork/Join final def y = new DataflowVariable() final def z = new DataflowVariable() Dataflow.task { z << x.val + y.val } Dataflow.task { x << 5 } Dataflow.task { y << 10 }Agent . println z.valDataflowVariableDataflowsDataflowQueue 15
  49. 49. DataflowVariableGroovy with GPars @Naoki RinGParsGPars . DataflowVariableGPars 6 import static groovyx.gpars.dataflow.Dataflow.task import groovyx.gpars.dataflow.DataflowVariableMap/ReduceFork/Join final def x = new DataflowVariable() final def y = new DataflowVariable() task{y << x.val + 1} task{x << 1} .Agent assert 2 == y.val DataflowVariableDataflowVariableDataflowsDataflowQueue Dataflow.task task
  50. 50. Groovy with GPars @Naoki RinGParsGParsGPars 6 . import static groovyx.gpars.dataflow.Dataflow.task import groovyx.gpars.dataflow.DataflowVariableMap/ReduceFork/Join def x = new DataflowVariable() def y = new DataflowVariable() task {x << y.val} task {y << x.val}Agent println x.val // ! .DataflowVariableDataflowsDataflowQueue
  51. 51. DataflowsGroovy with GPars @Naoki RinGParsGPars DataflowsGPars 6 . import static groovyx.gpars.dataflow.Dataflow.task import groovyx.gpars.dataflow.DataflowsMap/ReduceFork/Join final def dfs = new Dataflows() task {dfs[0] = dfs.x + ” ” + dfs.y} task {dfs.y = ”Groovy!”} task {dfs.x = ”Hello”} assert ”Hello Groovy!” == dfs[0] .Agent Dataflows DataflowVariableDataflowVariableDataflowsDataflowQueue
  52. 52. DataflowQueueGroovy with GPars @Naoki RinGParsGParsGPars 6 DatafowQueue . import groovyx.gpars.dataflow.DataflowQueueMap/ReduceFork/Join final def queue = new DataflowQueue() queue << ’a’ queue << ’b’ queue << ’c’ assert ’a’ == queue.valAgent assert ’b’ == queue.val assert ’c’ == queue.val .DataflowVariable DataflowQueueDataflowsDataflowQueue .val
  53. 53. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/Join GPars GroovyAgentDataflowVariableDataflowsDataflowQueue
  54. 54. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/Join Groovy http://gihyo.jp/book/2011/978-4-7741-4727-7 GPars Users Guide http://gpars.org/0.12/guide/index.htmlAgent GPars JavadocDataflowVariable http://gpars.org/0.12/javadocDataflowsDataflowQueue
  55. 55. Groovy with GPars @Naoki RinGParsGParsGPars 6Map/ReduceFork/JoinAgentDataflowVariableDataflowsDataflowQueue

×