Upload
andy-su
View
189
Download
0
Tags:
Embed Size (px)
Citation preview
Parallel Computing
with ruby
My Company
My Company
Challenges
• Learning Ruby
Challenges
• Learning Ruby
• Rails 2 to Rails 3
Challenges
• Learning Ruby
• Rails 2 to Rails 3
• Integrating with Banking APIs
Challenges
What is Parallel Computing?
Multithreading!vs!
Multiprocessing
Share Memory Space!
Lightweight!
May Have Memory Management Issues!
Can Sometimes Take Advantage of Multiple CPUs
Thread ProcessSeparate Memory Space!
Requires More Memory!
No Multithreading Issues!
Can Take Advantage of Multiple CPUs
CPU Scheduling
High IO
High CPU
http://www.cs.rutgers.edu/~pxk/416/notes/07-scheduling.html
High I/O
High IO
Fill these I/O blocks with other CPU tasks!
Multithreading ticker
t1 = Thread.new do puts "[Thread 1] Started" sleep 2 puts "[Thread 1] Completed"end!t2 = Thread.new do 5.times do |n| puts "[Thread 2] tick #{n}" endend
Multithreading ticker
t1 = Thread.new do puts "[Thread 1] Started" sleep 2 puts "[Thread 1] Completed"end!t2 = Thread.new do 5.times do |n| puts "[Thread 2] tick #{n}" endend
$ ruby thread_ticker.rb [Thread 1] Started [Thread 2] tick 0 [Thread 2] tick 1 [Thread 2] tick 2 [Thread 2] tick 3 [Thread 2] tick 4
Multithreading ticker
t1 = Thread.new do puts "[Thread 1] Started" sleep 2 puts "[Thread 1] Completed"end!t2 = Thread.new do 5.times do |n| puts "[Thread 2] tick #{n}" endend
Where’s Completed ?
$ ruby thread_ticker.rb [Thread 1] Started [Thread 2] tick 0 [Thread 2] tick 1 [Thread 2] tick 2 [Thread 2] tick 3 [Thread 2] tick 4
Multithreading tickert1 = Thread.new do puts "[Thread 1] Started" sleep 2 puts "[Thread 1] Completed"end!t2 = Thread.new do 5.times do |n| puts "[Thread 2] tick #{n}" endend!t1.joint2.join
Multithreading tickert1 = Thread.new do puts "[Thread 1] Started" sleep 2 puts "[Thread 1] Completed"end!t2 = Thread.new do 5.times do |n| puts "[Thread 2] tick #{n}" endend!t1.joint2.join
$ ruby thread_ticker.rb [Thread 1] Started [Thread 2] tick 0 [Thread 2] tick 1 [Thread 2] tick 2 [Thread 2] tick 3 [Thread 2] tick 4 [Thread 1] Completed
Issues with Threads
No control when Threads are preempted!
Deadlock!
Race conditions!
Hard to debug
Race Condition
Race Condition
Race Condition
Fibers
Fibers
Programmers specify when to give up control!
Prevents concurrency issues!
Lightweight
Fibers vs Threads
http://oldmoe.blogspot.com/2008/08/ruby-fibers-vs-ruby-threads.html
Fibonacci
fib = Fiber.new do f1 = f2 = 1 loop do Fiber.yield f1 f1, f2 = f2, f1 + f2 endend!5.times { p fib.resume }
$ ruby fiber_fib.rb11235
Fiber Tickerrequire 'fiber'!fb1 = Fiber.new do puts "[Fiber 1] Started" sleep 2 puts "[Fiber 1] Completed"end!fb2 = Fiber.new do 5.times do |n| puts "[Fiber 2] tick #{n}" endend!fb1.resumefb2.resume
Fiber Tickerrequire 'fiber'!fb1 = Fiber.new do puts "[Fiber 1] Started" sleep 2 puts "[Fiber 1] Completed"end!fb2 = Fiber.new do 5.times do |n| puts "[Fiber 2] tick #{n}" endend!fb1.resumefb2.resume
$ ruby fiber_tick.rb [Fiber 1] Started [Fiber 1] Completed [Fiber 2] tick 0 [Fiber 2] tick 1 [Fiber 2] tick 2 [Fiber 2] tick 3 [Fiber 2] tick 4
http://schmurfy.github.io/2011/09/25/on_fibers_and_threads.html
Event Machine
https://github.com/eventmachine/eventmachine
Evented Tickerrequire 'fiber'require 'eventmachine'!EM::run do fb1 = Fiber.new do puts "[Fiber 1] Started" EM::add_timer(2){ fb1.resume } Fiber.yield puts "[Fiber 1] Completed" EM::stop() end! fb2 = Fiber.new do 5.times {|n| puts "[Fiber 2] tick #{n}" } end! fb1.resume fb2.resumeend
$ ruby evented_ticker.rb [Fiber 1] Started [Fiber 2] tick 0 [Fiber 2] tick 1 [Fiber 2] tick 2 [Fiber 2] tick 3 [Fiber 2] tick 4 [Fiber 1] Completed
When does Multithreading help?
High I/O time such as
File I/O DB call API request
Demo
https://github.com/sudizhe/parallel_programming_demo
Fibers
Programmers specify when to give up control!
Prevents concurrency issues!
Lightweight
Multiprocessing?
http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
Multiprocessing?
http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
Global Interpreter Lock (GIL)
Multiprocessing?
http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
Global Interpreter Lock (GIL)
Rails Deploy
Spawning Processes
Parallel Gem
inDinero Enterprise
inDinero Enterprise
150,000 Transactions
inDinero Enterprise
150,000 Transactions x 15,000 Rules
RecapMultithreading
Thread Class
Fibers
Event Machine
Multiprocessing
Process Class
Unicorn Magic
Hadoop
Q & A