Concurrent Programming with Ruby and Tuple Spaces
- 2. The Free Lunch is Over: A Fundamental Turn Toward Concurrency in Software Source: http://www.gotw.ca/publications/concurrency-ddj.htm “ The major processor manufacturers and architectures have run out of room with most of their traditional approaches to boosting CPU performance. Instead of driving clock speeds ands straight-line instruction throughput ever higher, they are instead turning en masse to hyperthreading and multicore architectures. […] And that puts us at a fundamental turning point in software development, at least for the next few years...” – Herb Sutter – March 2005
- 3. Outline 1. The problem with Ruby Threads 2. Multiple Ruby Processes 3. Inter-process Communication with TupleSpaces
- 4. PART 1 The Problem With Threads A closer look at the Ruby threading model
- 6. 3 Types of Threading Models: 1 : N 1 : 1 M : N Kernel Threads User-Space Threads
- 7. 1 : N -> Green Threads One kernel thread for N user threads aka “lightweight threads”
- 18. Pros and Cons Pros: Thread creation, execution, and cleanup are cheap
- 19. Lots of threads can be created Cons: Not really parallel because kernel scheduler doesn't know about threads and can't schedule them across CPUs or take advantage of SMP
- 23. 1 : 1 -> Native Threads 1 kernel thread for each user thread
- 25. Pros and Cons Pros: Threads can execute on different CPUs (truly parallel)
- 31. Global Interpreter Lock A Global Interpreter Lock (GIL) is a mutual exclusion lock held by a programming language interpreter thread to avoid sharing code that is not thread-safe with other threads. There is always one GIL for one interpreter process. Usage of a Global Interpreter Lock in a language effectively limits concurrency of a single interpreter process with multiple threads – there is no or very little increase in speed when running the process on a multiprocessor machine. Source: Wikipedia
- 32. A person (male or female) who intentionally or unintentionally stops the progress of two others getting their game on.
- 37. A note on Fibers Ruby 1.9 introduces fibers.
- 38. Fibers are green threads, but scheduling must be done by the programmer and not the VM.
- 44. M : N -> Hybrid Model M kernel threads for N user threads “ best of both worlds”
- 49. Green threads doing blocking I/O operations will block all other green threads sharing same kernel thread
- 52. “ Writing multi-threaded code is really, really hard. And it is hard because of Shared Memory.” – Jim Weirich The Other Problem with Threads http://rubyconf2008.confreaks.com/what-all-rubyist-should-know-about-threads.html
- 62. Uses a lot of memory (loading Ruby VM for every process)
- 88. “ Erlang provides a sledgehammer for the problems of concurrent programming. But, sometimes you don't need a sledgehammer... just a flyswatter will do.” – Tony Arcieri
- 90. Journeta Journeta is a dirt simple library for peer discovery and message passing between Ruby applications on a LAN
- 92. “ Uses the fucked up Ruby socket API” -> from their RDOC Demo(?)
- 94. A tuple space provides a repository of tuples that can be accessed concurrently.
- 95. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor
- 96. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [:add, nil, nil]
- 97. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [nil]
- 98. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [:where_is, :waldo]
- 103. Rinda is a built-in library, so no need to install.
- 113. notify Reads tuple, but does not remove it. Blocking, by default, but takes an additional timeout argument.
- 128. notify Atomic Read + Delete Blocking, by default, but takes an additional timeout argument.
- 136. Key Features Spaces are shared Space handles details of concurrent access Spaces are persistent If agent process dies, data is still in space
- 137. However, if space process dies, data is lost (?) Spaces are associative Associative lookups rather than memory location or identifier Spaces are transactionally secure Atomic Operations Spaces allow us to exchange executable content
- 139. A Rinda tuple can be an array or a hash ( But let's stick with the array, I like that better! )
- 152. Rinda is not persistent... If it crashes while you have tuples in the space, you lose them all.
- 154. Introducing Blackboard TupleSpace implementation on top of Redis -> Persistent Redis is a really fast key-value database. Like memcached but data is not volatile. Same API -> Plug & Play
- 162. I would like that Erlang implementation to be easily used from other programming languages also.
- 167. Resources / References Part 1: Threading Models http://timetobleed.com/threading-models-so-many-different-ways-to-get-stuff-done/
- 193. Things I wish I had time to spend on MPI and Ruby-MPI http://github.com/abedra/mpi-ruby/tree/master Ruby forkoff: http://tinyurl.com/forkoff