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)
Friday 29 October 2010
15. 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
Friday 29 October 2010
16. Other Rubies
• @evanphx working on removing the
GIL on Rubinius (Hydra branch)
• JRuby, IronRuby, MacRuby don’t have
GIL
Friday 29 October 2010
19. 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
Friday 29 October 2010
20. Threads
Shared state: locking to avoid race
conditions
Mutex.synchronize do
...
end
ConditionVariable (semaphore)
MonitorMixin, Sync_m
Friday 29 October 2010
21. Ruby threads
require 'thread'
threads = []
account = UnsafeAccount.new
mutex = Mutex.new
10.times do
threads << Thread.new do
mutex.synchronize do
account.receive(10)
end
end
end
threads.each {|t| t.join }
puts account.balance
Friday 29 October 2010
22. Fibers
• cooperative scheduling
• coroutines
• for MRI: lightweight
• JRuby, Rubinius: Fiber mapped to
native thread
Friday 29 October 2010
23. Ruby: Coroutines
require 'fiber'
# coroutines
ary = []
f2 = nil
f1 = 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 login
johndoe
give password
ultrasecret
***** no cigar *****
Friday 29 October 2010
24. MVM
Rubinius (2008): no parallel execution
of threads in oneVM ... so let’s create
oneVM per native thread
vm = Rubinius::VM.spawn "blah", "-e", "puts 'hellon'"
Friday 29 October 2010
28. Actor model
•named actors: no shared state
•asynchronous message passing
(fire and forget)
Friday 29 October 2010
29. CSP
• member of family of Process Calculi
(mathematical theory)
• events, processes
• synchronous (rendez-vous) message passing
• named channels - dual to Actor model
Friday 29 October 2010
34. Rubinius:Actors
• actors in the language: threads with
inbox
• VM actors to communicate between
actors in differentVMs
Friday 29 October 2010
35. 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)
Friday 29 October 2010
36. Go
• Fairly low-level - fit for systems
programming (close to C)
• static typing
• goroutines: parallel execution - sort of
async lightweight thread
• channels !
Friday 29 October 2010
37. Go
ReplyChannel = make(chan []int)
(...)
request.data = less
request.replyChannel = ReplyChannel
go sort(&request) // async start parallel
execution
in sort:
request.replyChannel <- sorted
listener:
result := <-replyChannel
Friday 29 October 2010
38. Ruby: futures
Lazy.rb gem (@mentalguy)
puts "before first future"
future1 = Lazy::Future.new { fib(40) }
puts "before second future"
future2 = Lazy::Future.new { fib(40) }
puts "and now we're waiting for results ..."
puts future1
# maybe some more code here
puts future2
Friday 29 October 2010
39. Clojure
functional, Lisp-like, on JVM
concurrency: Software Transactional
Memory System:
• Refs = shared, and mutation within a
transaction (atomic, consistent, isolated) -
Multiversion Concurrency Control -
retried if conflict
• Agents: independent, asynchronous change
Friday 29 October 2010
41. Ruby: STM
• @mentalguy thought experiment
• @technomancy clojure-gem
Friday 29 October 2010
42. other ways to handle
concurrency
see http://moonbase.rydia.net/mental/
blog/programming/concurrency-five-
ways.html (@mentalguy)
Friday 29 October 2010
43. Kernel stuff
Some of these problems have
been solved before ...
Friday 29 October 2010