Concurrency
-
Upload
ehuard -
Category
Technology
-
view
1.550 -
download
7
description
Transcript of Concurrency
![Page 1: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/1.jpg)
CONCURRENCY:RUBIES, PLURAL
Elise Huard - RubyAndRails 2010Thursday 21 October 2010
![Page 2: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/2.jpg)
MULTIPROCESSOR/MULTICORE
Thursday 21 October 2010
![Page 3: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/3.jpg)
NETWORK ON CHIP(50..96..100 CORES)
Thursday 21 October 2010
![Page 4: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/4.jpg)
“... 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
![Page 5: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/5.jpg)
But ...forget all that
(mostly)
Thursday 21 October 2010
![Page 6: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/6.jpg)
language VM
OS(kernel processes, other processes)
Your program
multicore - multiCPU
Thursday 21 October 2010
![Page 7: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/7.jpg)
Concurrent programs!=
Parallel computing
Thursday 21 October 2010
![Page 8: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/8.jpg)
Thursday 21 October 2010
![Page 9: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/9.jpg)
Thursday 21 October 2010
![Page 10: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/10.jpg)
Scheduling
• preemptive -> thread is told to yield (by kernel or other thread)
• cooperative -> thread yields control
Thursday 21 October 2010
![Page 11: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/11.jpg)
Processes, Threads
Process 2
RAMmemory space
Process 1
thread1
scheduler (OS)
CPU CPU
memory space
thread2 t1 t2 t3
Thursday 21 October 2010
![Page 12: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/12.jpg)
Ruby
• Process
• Thread - green in MRI 1.8, native threads in MRI 1.9, Rubinius, JRuby
Thursday 21 October 2010
![Page 13: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/13.jpg)
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
![Page 14: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/14.jpg)
MRI: GIL
from http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ @igrigorik
Thursday 21 October 2010
![Page 15: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/15.jpg)
Other Rubies
• @evanphx working on removing the GIL on Rubinius (Hydra branch)
• JRuby, IronRuby, MacRuby don’t have GIL
Thursday 21 October 2010
![Page 16: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/16.jpg)
Parallel quicksort
[5, 3, 22, 12, 15, -112, 6]
[-122, 5, 3, 6] [22, 15]
[-122] [5, 6]
12
3
Thursday 21 October 2010
![Page 17: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/17.jpg)
multiprocess
•advantage: separate state
•disadvantage: overhead to spawning + context switching
Thursday 21 October 2010
![Page 18: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/18.jpg)
Ruby: multiprocess
•fork and IPC: IO.pipe, Mmap, ...
•DRb
Thursday 21 October 2010
![Page 19: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/19.jpg)
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
Ruby: multiprocess
Thursday 21 October 2010
![Page 20: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/20.jpg)
Threads
Shared state:
Mutex
ConditionVariable (semaphore)
MonitorMixin, Sync_m
Thursday 21 October 2010
![Page 21: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/21.jpg)
Ruby threads def execute(&block) sorted = nil thread = Thread.new do sorted = block.call end thread.join sorted end
Thursday 21 October 2010
![Page 22: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/22.jpg)
Fibers
• cooperative scheduling
• coroutines
• for MRI: lightweight
• JRuby, Rubinius: Fiber mapped to native thread
Thursday 21 October 2010
![Page 23: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/23.jpg)
Ruby: Coroutinesrequire 'fiber'
# coroutinesary = []f2 = nilf1 = Fiber.new{ puts "please give your login" login = f2.transfer puts login puts "give password" pass = f2.transfer puts pass f2.transfer f2.transfer('***** no cigar *****')}f2 = Fiber.new{ f1.transfer('johndoe') f1.transfer('ultrasecret') answer = f1.transfer puts answer}
f1.resume vaguely inspired by http://sheddingbikes.com/posts/1287306747.html
output:please give your loginjohndoegive passwordultrasecret***** no cigar *****
Thursday 21 October 2010
![Page 24: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/24.jpg)
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 'hello\n'"
Thursday 21 October 2010
![Page 25: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/25.jpg)
Thursday 21 October 2010
![Page 26: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/26.jpg)
shared state: will melt your brain
• non-determinism
• atomicity
• deadlock
• livelock
• fairness/starvation
• race conditions
Thursday 21 October 2010
![Page 27: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/27.jpg)
actor model
• named actors: have no shared state
• asynchronous message passing (fire and forget)
Thursday 21 October 2010
![Page 28: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/28.jpg)
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
![Page 29: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/29.jpg)
Concurrency oriented languages
• Erlang (Actors)
• Clojure
• Go (CSP)
• Haskell (several)
• Scala (Actors)
• ...
Thursday 21 October 2010
![Page 30: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/30.jpg)
Ideas
• functional programming: side effect free function calls
- immutable data
• nothing shared (advantage: distributed = local)
• message passing
Thursday 21 October 2010
![Page 31: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/31.jpg)
erlang
• Actor model: Actors, asynchronous message passing
• actors = “green processes”
• efficient VM (SMP enabled since R12B)
• high reliability
© ericsson 2007
Thursday 21 October 2010
![Page 32: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/32.jpg)
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
![Page 33: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/33.jpg)
Rubinius: Actors
• actors in the language: threads with inbox
• VM actors to communicate between actors in different VMs
Thursday 21 October 2010
![Page 34: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/34.jpg)
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
![Page 35: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/35.jpg)
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
![Page 36: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/36.jpg)
GolessReply = make(chan []int)
(...)lessReq.data = less
lessReq.replyChannel = lessReplygo sort(&lessReq) // asyncstart parallel execution of sort
listener:
append(<-lessReply, pivot, <-greaterReply)
Thursday 21 October 2010
![Page 37: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/37.jpg)
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
![Page 38: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/38.jpg)
Ruby: STM
• @mentalguy thought experiment
• @technomancy clojure-gem
Thursday 21 October 2010
![Page 39: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/39.jpg)
others to handle concurrency
futures
joins
see http://moonbase.rydia.net/mental/blog/programming/concurrency-five-ways.html (@mentalguy)
Thursday 21 October 2010
![Page 40: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/40.jpg)
Kernel stuff
Some of these problems have been solved before ...
Thursday 21 October 2010
![Page 41: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/41.jpg)
FUN :)
Thursday 21 October 2010
![Page 42: Concurrency](https://reader033.fdocuments.us/reader033/viewer/2022051611/54b7ad404a7959d3328b468c/html5/thumbnails/42.jpg)
References:
http://www.delicious.com/elisehuard/concurrency
http://github.com/elisehuard/rubyandrails-2010
Elise Huard
@elise_huard
http://jabberwocky.eu
Thursday 21 October 2010