• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Gpars workshop
 

Gpars workshop

on

  • 559 views

 

Statistics

Views

Total Views
559
Views on SlideShare
432
Embed Views
127

Actions

Likes
0
Downloads
12
Comments
0

3 Embeds 127

http://gr8conf.eu 113
http://m.gr8conf.eu 9
http://2013.gr8conf.eu 5

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

    Gpars workshop Gpars workshop Presentation Transcript

    • GPars workshopVáclav Pechhttp://jroller.com/vaclavRussel Winderhttp://www.russel.org.uk
    • Agenda● Threads, thread pools, tasks● Agents● Fork/Join● Parallel collections● Dataflow● Actorshttp://gpars.org/GPars_workshop.zip
    • We’re all in the parallel computing business!
    • Multithreaded programs today work mostly by accident!
    • Map/ReduceMap/ReduceFork/JoinFork/JoinActorsActorsSTMSTMDataflowDataflowAgentAgentCan we do better?
    • Part 1Thread management
    • Asynchronous invocationFuture f = threadPool.submit(calculation);…System.out.println(“Result: “ + f.get());
    • Async the Groovy waydef group = new NonDaemonPGroup(10)group.task {calculation.process()}
    • Part 2Agents
    • Shared Mutable StateMisused most of the timeWhen really needed, use Agents Software Transactional Memory Locks
    • Agent insideDouble IncrementAdd 25Message Queue36 thread
    • Agent Listdef jugMembers = new Agent([Me]) //add Metask {jugMembers.send {it.add Joe} //add Joe}task {jugMembers << {it.add Dave} //add DavejugMembers << {it.add Alice} //add Alice}...println jugMembers.val
    • Part 3Fork/Join
    • Thread PoolTasksWorker threadsQueue
    • Thread PoolContention!
    • Fork/Join Thread Pool
    • Fork/Join Thread PoolWork stealing
    • Fork/Join Solve hierarchicalproblems Divide and conquer Merge sort, Quick sort Tree traversal File scan / search …[a, b, c, d, e, f, g, h][a, b, c, d] [e, f, g, h][a, b] [c, d] [e, f] [g, h]
    • Fork/Join (GPars)runForkJoin(new File(“./src”)) {currentDir ->long count = 0;currentDir.eachFile {if (it.isDirectory()) {forkOffChild it} else {count++}}return count + childrenResults.sum(0)}Waits for childrenwithout blocking thethread!
    • Part 4Parallel collections
    • Parallel collectionsimages.eachParallel {it.process()}documents.sumParallel()candidates.maxParallel {it.salary}.marry()
    • Parallel collectionsregistrations = submissions.collectParallel { form -> form.process()}.findAllParallel { it.valid }registrations = submissions.parallel.map { form -> form.process()}.filter { it.valid }.collection
    • GPUJava watch list:https://github.com/pcpratts/rootbeer1/http://openjdk.java.net/projects/sumatra/
    • Part 5Dataflow
    • Composing async functionsint hash1 = hash(download(http://www.gpars.org))int hash2 = hash(loadFile(/gpars/website/index.html))boolean result = compare(hash1, hash2)println result
    • Composing async functions@AsyncFun hash = oldHash@AsyncFun compare = oldCompare@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFiledef hash1 = hash(download(http://www.gpars.org))def hash2 = hash(loadFile(/gpars/website/index.html))def result = compare(hash1, hash2)println result.get()
    • Composing async functions@AsyncFun hash = oldHash@AsyncFun(blocking = true) compare = oldCompare@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFiledef hash1 = hash(download(http://www.gpars.org))def hash2 = hash(loadFile(/gpars/website/index.html))boolean result = compare(hash1, hash2)println result
    • int hash(String text) {…}Promise<int> hash(Promise<String> | String text)
    • int hash(String text) {…}Promise<int> hash(Promise<String> | String text)compare(hash( download() ),hash( loadFile() ))
    • 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}
    • Composing async functionsCombine functions as usualParallelism is detected automatically
    • Composing async functions@AsyncFun hash = oldHash@AsyncFun compare = oldCompare@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFiledef hash1 = hash(download(http://www.gpars.org))def hash2 = hash(loadFile(/gpars/website/index.html))def result = compare(hash1, hash2)println result.get()
    • Promise chaining@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFiledef h1 = download(http://www.gpars.org) then hashdef h2 = loadFile(/gpars/website/index.html) then hashwhenAllBound([h1, h2], compare) then {println it}
    • Promise chaining@AsyncFun download = oldDownload@AsyncFun loadFile = oldLoadFilewhenAllBound ([download(http://www.gpars.org) >> hash,loadFile(/gpars/website/index.html) >> hash], compare) >> {println it}
    • Dataflow Concurrency No race-conditions No live-locks Deterministic deadlocksCompletely deterministic programsBEAUTIFUL code(Jonas Bonér)
    • Dataflow Variables / Promisesmain task2 task3xyztask1
    • Dataflow Variables / Promisestask2xtask1
    • Promises to exchange datatask { z << x.val + y.val }task { x << 10 }task {println ”I am task 3”y << 5}assert 15 == z.val
    • Dataflow Variables / Promisestask2xtask1
    • Dataflow Channelstask2x|y|ztask1
    • Synchronous Channelstask2x|y|ztask1
    • 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
    • Url resolverUrl resolverDownloaderGroovy scanner Scala scannerUrl resolverUrl resolverReporterSpeculatorEvaluatorSplitterConfirmCacheupdatesApprovalsDataflow Operators
    • Part 6Actors
    • ActorsProcesses with a mail-boxShare no dataCommunicate by sending messagesUse a thread-pool
    • Actors Isolated CommunicatingImmutable messages ActivePooled shared threads ActivitiesCreate a new actorSend a messageReceive a messageActorActorActorActorActorActorActorTTTThread pool
    • Actors useGateKeeperFormHTTPFingerPrintsAddressCheckEmailCheckProcessFraudDetectResponseSOAPSMTP
    • Sending messagesbuddy.send 10.eurbuddy << new Book(title:’Groovy Recipes’,author:’Scott Davis’)def canChat = buddy.sendAndWait ‘Got time?’buddy.sendAndContinue ‘Need money!’, {cash->pocket.add cash}
    • Stateless Actors (pure Java)class 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);}}
    • Stateful Actorsclass MyActor extends DefaultActor {void act() {def buddy = new YourActor()buddy << ‘Hi man, how’re things?’def response = receive()}}
    • Implicit State in Actorsval me = actor {react {msg1 ->switch (msg1) {case Work: reply “I dont work so early” ; stop();case Breakfast:msg1.eat()react {msg2 →switch (msg2) {case Work: reply “OK, time to work”; msg2.do()case Lunch: ...} } } }
    • Continuation Styleloop {…react {…react {/*schedule the block; throw CONTINUE*/…}//Never reached}//Never reached}//Never reached
    • Active Objects@ActiveObjectclass MyCounter {private int counter = 0@ActiveMethoddef incrementBy(int value) {println "Received an integer: $value"this.counter += value}}
    • coz concurrency is Groovy