CONCURRENCY:
                            RUBIES, PLURAL




                               Elise Huard - RubyAndRails 2010...
MULTIPROCESSOR/MULTICORE




Thursday 21 October 2010
NETWORK ON CHIP
                            (50..96..100 CORES)




Thursday 21 October 2010
“... for the first time in history, no one is building a
                 much faster sequential processor. If you want you...
But ...
                           forget all that
                                (mostly)




Thursday 21 October 2010
Your program




                                      language VM



                                           OS
      ...
Concurrent programs
                               !=
                      Parallel computing


Thursday 21 October 2010
Thursday 21 October 2010
Thursday 21 October 2010
Scheduling


                    • preemptive -> thread is told to yield
                           (by kernel or other th...
Processes, Threads
                                 memory space              RAM               memory space



          ...
Ruby


                    • Process
                    • Thread - green in MRI 1.8, native
                           th...
MRI: GIL
                   • only one thread is executed at a time
                   • fair scheduling: timer thread!
  ...
MRI: GIL




                           from http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ @igrigorik


...
Other Rubies

                    • @evanphx working on removing the
                           GIL on Rubinius (Hydra bra...
Parallel quicksort

                                                          [5, 3, 22, 12, 15, -112, 6]




            ...
multiprocess

                    •      advantage: separate state
                    • disadvantage: overhead to
       ...
Ruby: multiprocess

                    •      fork and IPC: IO.pipe,
                           Mmap, ...
               ...
Ruby: multiprocess
                           def execute(&block)
                                   rd, wr = IO.pipe # to...
Threads

                           Shared state:
                           Mutex
                           ConditionVar...
Ruby threads
                           def execute(&block)
                                    sorted = nil
             ...
Fibers

                    • cooperative scheduling
                    • coroutines
                    • for MRI: light...
Ruby: Coroutines
         require 'fiber'

         # coroutines
         ary = []
         f2 = nil
         f1 = Fiber.n...
MVM
                           Rubinius (2008): no parallel execution
                           of threads in one VM ... ...
Thursday 21 October 2010
shared state:
                           will melt your brain
                    • non-determinism
                    • ...
actor model


                    • named actors: have no shared state
                    • asynchronous message passing ...
CSP

                    • member of family of Process Calculi
                           (mathematical theory)
          ...
Concurrency oriented
                         languages
                    • Erlang (Actors)
                    • Clojur...
Ideas
                    • functional programming: side effect
                           free function calls
           ...
erlang

                    • Actor model: Actors, asynchronous
                           message passing
               ...
Erlang

               spawn(fun() ->sort(Self, List) end)
               pmap_gather([]) ->
                 [];
        ...
Rubinius: Actors

                    • actors in the language: threads with
                           inbox
            ...
Ruby: Revactor

                    • erlang-like semantics: actor spawn/receive,
                           filter
       ...
Go
                    • Fairly low-level - fit for systems
                           programming (close to C)
           ...
Go
               lessReply = make(chan []int)
               (...)
               lessReq.data = less
               less...
clojure
                           functional, Lisp-like
                           concurrency: Software Transactional
  ...
Ruby: STM


                    • @mentalguy thought experiment
                    • @technomancy clojure-gem


Thursday ...
others to handle
                                concurrency
                           futures
                          ...
Kernel stuff


                       Some of these problems have
                       been solved before ...



Thursda...
FUN :)




Thursday 21 October 2010
References:

                     http://www.delicious.com/elisehuard/concurrency

                     http://github.com/...
Upcoming SlideShare
Loading in …5
×

Concurrency

1,808 views
1,708 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,808
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Concurrency

  1. 1. CONCURRENCY: RUBIES, PLURAL Elise Huard - RubyAndRails 2010 Thursday 21 October 2010
  2. 2. MULTIPROCESSOR/MULTICORE Thursday 21 October 2010
  3. 3. NETWORK ON CHIP (50..96..100 CORES) Thursday 21 October 2010
  4. 4. “... for the first time in history, no one is building a much faster sequential processor. If you want your programs to run significantly faster (...) you’re going to have to parallelize your program.” Hennessy and Patterson “Computer Architectures” (4th edition, 2007) Thursday 21 October 2010
  5. 5. But ... forget all that (mostly) Thursday 21 October 2010
  6. 6. Your program language VM OS (kernel processes, other processes) multicore - multiCPU Thursday 21 October 2010
  7. 7. Concurrent programs != Parallel computing Thursday 21 October 2010
  8. 8. Thursday 21 October 2010
  9. 9. Thursday 21 October 2010
  10. 10. Scheduling • preemptive -> thread is told to yield (by kernel or other thread) • cooperative -> thread yields control Thursday 21 October 2010
  11. 11. Processes, Threads memory space RAM memory space Process 1 Process 2 thread1 thread2 t1 t2 t3 scheduler (OS) CPU CPU Thursday 21 October 2010
  12. 12. Ruby • Process • Thread - green in MRI 1.8, native threads in MRI 1.9, Rubinius, JRuby Thursday 21 October 2010
  13. 13. MRI: GIL • only one thread is executed at a time • fair scheduling: timer thread! (10 μs for Linux, 10 ms for Windows) • blocking region to allow limited concurrency Thursday 21 October 2010
  14. 14. MRI: GIL from http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ @igrigorik Thursday 21 October 2010
  15. 15. Other Rubies • @evanphx working on removing the GIL on Rubinius (Hydra branch) • JRuby, IronRuby, MacRuby don’t have GIL Thursday 21 October 2010
  16. 16. Parallel quicksort [5, 3, 22, 12, 15, -112, 6] [-122, 5, 3, 6] 12 [22, 15] [-122] 3 [5, 6] Thursday 21 October 2010
  17. 17. multiprocess • advantage: separate state • disadvantage: overhead to spawning + context switching Thursday 21 October 2010
  18. 18. Ruby: multiprocess • fork and IPC: IO.pipe, Mmap, ... • DRb Thursday 21 October 2010
  19. 19. Ruby: multiprocess def execute(&block) rd, wr = IO.pipe # to retrieve results pid = fork do rd.close result = block.call wr.write result.to_json wr.close end wr.close sorted = JSON.parse(rd.read) rd.close Process.waitpid(pid) sorted end Thursday 21 October 2010
  20. 20. Threads Shared state: Mutex ConditionVariable (semaphore) MonitorMixin, Sync_m Thursday 21 October 2010
  21. 21. Ruby threads def execute(&block) sorted = nil thread = Thread.new do sorted = block.call end thread.join sorted end Thursday 21 October 2010
  22. 22. Fibers • cooperative scheduling • coroutines • for MRI: lightweight • JRuby, Rubinius: Fiber mapped to native thread Thursday 21 October 2010
  23. 23. Ruby: Coroutines require 'fiber' # coroutines ary = [] f2 = nil f1 = Fiber.new{ output: puts "please give your login" please give your login login = f2.transfer johndoe puts login puts "give password" give password pass = f2.transfer ultrasecret puts pass ***** no cigar ***** f2.transfer f2.transfer('***** no cigar *****') } f2 = Fiber.new{ f1.transfer('johndoe') f1.transfer('ultrasecret') answer = f1.transfer puts answer } vaguely inspired by http://sheddingbikes.com/posts/1287306747.html f1.resume Thursday 21 October 2010
  24. 24. MVM Rubinius (2008): no parallel execution of threads in one VM ... so let’s create one VM per native thread vm = Rubinius::VM.spawn "blah", "-e", "puts 'hellon'" Thursday 21 October 2010
  25. 25. Thursday 21 October 2010
  26. 26. shared state: will melt your brain • non-determinism • atomicity • deadlock • livelock • fairness/starvation • race conditions Thursday 21 October 2010
  27. 27. actor model • named actors: have no shared state • asynchronous message passing (fire and forget) Thursday 21 October 2010
  28. 28. CSP • member of family of Process Calculi (mathematical theory) • events, processes • synchronous (rendez-vous) message passing • named channels - dual to Actor model Thursday 21 October 2010
  29. 29. Concurrency oriented languages • Erlang (Actors) • Clojure • Go (CSP) • Haskell (several) • Scala (Actors) • ... Thursday 21 October 2010
  30. 30. Ideas • functional programming: side effect free function calls - immutable data • nothing shared (advantage: distributed = local) • message passing Thursday 21 October 2010
  31. 31. erlang • Actor model: Actors, asynchronous message passing • actors = “green processes” • efficient VM (SMP enabled since R12B) • high reliability © ericsson 2007 Thursday 21 October 2010
  32. 32. Erlang spawn(fun() ->sort(Self, List) end) pmap_gather([]) -> []; pmap_gather([H|T]) -> receive {H, Ret} -> [Ret|pmap_gather(T)] end; Thursday 21 October 2010
  33. 33. Rubinius: Actors • actors in the language: threads with inbox • VM actors to communicate between actors in different VMs Thursday 21 October 2010
  34. 34. Ruby: Revactor • erlang-like semantics: actor spawn/receive, filter • Fibers (so cooperative scheduling) • Revactor::TCP for non-blocking network access (1.9.2) (rev eventloop) Thursday 21 October 2010
  35. 35. Go • Fairly low-level - fit for systems programming (close to C) • static typing • goroutines: parallel execution - sort of async lightweight thread • channels ! Thursday 21 October 2010
  36. 36. Go lessReply = make(chan []int) (...) lessReq.data = less lessReq.replyChannel = lessReply go sort(&lessReq) // asyncstart parallel execution of sort listener: append(<-lessReply, pivot, <-greaterReply) Thursday 21 October 2010
  37. 37. clojure functional, Lisp-like concurrency: Software Transactional Memory System: • Vars = variable state is thread isolated • Refs = shared, and mutation within a transaction (atomic, consistent, isolated) - Multiversion Concurrency Control - Thursday 21 October 2010
  38. 38. Ruby: STM • @mentalguy thought experiment • @technomancy clojure-gem Thursday 21 October 2010
  39. 39. others to handle concurrency futures joins see http://moonbase.rydia.net/mental/ blog/programming/concurrency-five- ways.html (@mentalguy) Thursday 21 October 2010
  40. 40. Kernel stuff Some of these problems have been solved before ... Thursday 21 October 2010
  41. 41. FUN :) Thursday 21 October 2010
  42. 42. References: http://www.delicious.com/elisehuard/concurrency http://github.com/elisehuard/rubyandrails-2010 Elise Huard @elise_huard http://jabberwocky.eu Thursday 21 October 2010

×