Your SlideShare is downloading. ×
0
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Concurrent Programming with Ruby and Tuple Spaces
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Concurrent Programming with Ruby and Tuple Spaces

12,346

Published on

Ruby threads are limited due to the Global Interpreter Lock. Therefore, the best way to do parallel computing with Ruby is to use multiple processes but how do you get these processes to …

Ruby threads are limited due to the Global Interpreter Lock. Therefore, the best way to do parallel computing with Ruby is to use multiple processes but how do you get these processes to communicate?

This session will provide some strategies for handling multi-process communication in Ruby, with a focus on the use of TupleSpaces. A TupleSpace provides a repository of tuples that can be accessed concurrently to implement a Blackboard system. Ruby ships with a built-in implementation of a TupleSpace with the Rinda library.

During the session, Luc will demonstrate how to use Rinda and will highlight other libraries/projects that facilitate interprocess communication and parallel computing in Ruby.

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

No Downloads
Views
Total Views
12,346
On Slideshare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
295
Comments
0
Likes
22
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Transcript

    • 1. Concurrent Programming with Ruby and Tuple Spaces Luc Castera Founder / messagepub.com
    • 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
    • 5. 3 Types of Threading Models: 1 : N 1 : 1 M : N
    • 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”
    • 8.  
    • 9. 10 ms
    • 10. 10 ms
    • 11. 10 ms
    • 12. 10 ms
    • 13. 10 ms
    • 14. 10 ms
    • 15. 10 ms
    • 16. 10 ms
    • 17. RUBY 1.8
    • 18. Pros and Cons <ul><li>Pros: </li><ul><li>Thread creation, execution, and cleanup are cheap
    • 19. Lots of threads can be created </li></ul><li>Cons: </li><ul><li>Not really parallel because kernel scheduler doesn&apos;t know about threads and can&apos;t schedule them across CPUs or take advantage of SMP
    • 20. Blocking I/O operation can block all green threads </li><ul><li>Example: C Extension
    • 21. Example: mysql gem (solution: NeverBlock mysqlplus) </li></ul></ul></ul>
    • 22. blocking
    • 23. 1 : 1 -&gt; Native Threads 1 kernel thread for each user thread
    • 24. &nbsp;
    • 25. Pros and Cons <ul><li>Pros: </li><ul><li>Threads can execute on different CPUs (truly parallel)
    • 26. Threads do not block each other </li></ul><li>Cons: </li><ul><li>Setup Overhead
    • 27. Low limit on number of threads
    • 28. Linux kernel bug with lots of threads </li></ul></ul>
    • 29. RUBY 1.9
    • 30. I lied.
    • 31. Global Interpreter Lock <ul>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 </ul>
    • 32. <ul>A person (male or female) who intentionally or unintentionally stops the progress of two others getting their game on. </ul>
    • 33. &nbsp;
    • 34. “ Concurrency is a myth in Ruby” – Ilya Grigorik
    • 35. Unless you are using JRuby.
    • 36. &nbsp;
    • 37. A note on Fibers <ul><li>Ruby 1.9 introduces fibers.
    • 38. Fibers are green threads, but scheduling must be done by the programmer and not the VM.
    • 39. Faster and cheaper then native threads.
    • 40. Implemented for Ruby 1.8 by Aman Gupta.
    • 41. Learn More: </li><ul><li>http://tinyurl.com/rubyfibers
    • 42. http://all-thing.net/fibers
    • 43. http://all-thing.net/fibers-via-continuations </li></ul></ul>
    • 44. M : N -&gt; Hybrid Model M kernel threads for N user threads “ best of both worlds”
    • 45. &nbsp;
    • 46. Pros and Cons <ul><li>Pros: </li><ul><li>Take advantage of multiple CPUs
    • 47. Not all threads are blocked by blocking system calls
    • 48. Cheap creation, execution, and cleanup </li></ul><li>Cons: </li><ul><li>Need scheduler in userland and kernel to work with each other
    • 49. Green threads doing blocking I/O operations will block all other green threads sharing same kernel thread
    • 50. Difficult to write, maintain, and debug code </li></ul></ul>
    • 51. &nbsp;
    • 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
    • 53. Multi-Threaded Code is Hard + Concurrency is a myth = FAIL!
    • 54. Stop thinking in threads Design your application to use multiple processes
    • 55. PART 2 Multiple Ruby Processes
    • 56. &nbsp;
    • 57. Pros and Cons <ul><li>Pros: </li><ul><li>No longer sharing memory
    • 58. Take advantage of multiple CPUs (Performance)
    • 59. Not all threads are blocked by blocking system calls.
    • 60. Scalability
    • 61. Fault-Tolerance </li></ul><li>Cons: </li><ul><li>Process creation, execution and cleanup is expensive
    • 62. Uses a lot of memory (loading Ruby VM for every process)
    • 63. Need a way for processes to communicate! </li></ul></ul>
    • 64. Latency Starting/Stopping Fault-Tolerance Monitoring
    • 65. but we will focus on...
    • 66. How do the processes communicate?
    • 67. Options <ul><li>DRB
    • 68. Sockets
    • 69. Queues </li><ul><li>RabbitMQ
    • 70. ActiveMQ </li></ul><li>Key-Value Databases </li><ul><li>Redis
    • 71. Tokyo Cabinet
    • 72. Memcached </li></ul><li>Relational Databases
    • 73. XMPP
    • 74. TupleSpaces </li></ul>
    • 75. Examples
    • 76. Rails + Mongrel/Thin <ul><li>Cluster of application servers (Mongrel, Thin...)
    • 77. Communication between processes is done via the database. </li></ul>
    • 78. Nanite <ul><li>A self-assembling fabric of Ruby daemons
    • 79. http://github.com/ezmobius/nanite
    • 80. Uses RabbitMQ/AMQP for IPC </li></ul>
    • 81. Revactor <ul><li>Uses the actor model
    • 82. Actors are kinda like threads, with messaging baked-in.
    • 83. Each Actor has a mailbox.
    • 84. It&apos;s like coding erlang in Ruby.
    • 85. Messages are passed between actors using TCP sockets.
    • 86. Good Documentation
    • 87. http://revactor.org/
    • 88. “ Erlang provides a sledgehammer for the problems of concurrent programming. But, sometimes you don&apos;t need a sledgehammer... just a flyswatter will do.” – Tony Arcieri
    • 89. Discontinued for Reia </li></ul>
    • 90. Journeta <ul><li>Journeta is a dirt simple library for peer discovery and message passing between Ruby applications on a LAN
    • 91. Uses UDP Sockets for IPC
    • 92. “ Uses the fucked up Ruby socket API” </li><ul><li>-&gt; from their RDOC </li></ul><li>Demo(?) </li></ul>
    • 93. PART 3 TupleSpaces I nterprocess Communication with TupleSpaces
    • 94. <ul>A tuple space provides a repository of tuples that can be accessed concurrently. </ul>
    • 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]
    • 99. About Tuple Spaces <ul><li>First implementation was Linda.
    • 100. Linda was developed by David Gelernter and Nicholas Carriero at Yale University.
    • 101. Implementations exists for most languages.
    • 102. The Ruby implementation is Rinda.
    • 103. Rinda is a built-in library, so no need to install. </li></ul>
    • 104. 5 Basic Operations <ul><li>read
    • 105. read_all
    • 106. write
    • 107. take
    • 108. notify </li></ul>
    • 109. 5 Basic Operations <ul><li>read
    • 110. read_all
    • 111. write
    • 112. take
    • 113. notify </li></ul>Reads tuple, but does not remove it. Blocking, by default, but takes an additional timeout argument.
    • 114. 5 Basic Operations <ul><li>read
    • 115. read_all
    • 116. write
    • 117. take
    • 118. notify </li></ul>Returns all tuples matching tuple. Does not remove the found tuples.
    • 119. 5 Basic Operations <ul><li>read
    • 120. read_all
    • 121. write
    • 122. take
    • 123. notify </li></ul>Adds Tuple Takes an optional timeout parameter
    • 124. 5 Basic Operations <ul><li>read
    • 125. read_all
    • 126. write
    • 127. take
    • 128. notify </li></ul>Atomic Read + Delete Blocking, by default, but takes an additional timeout argument.
    • 129. 5 Basic Operations <ul><li>read
    • 130. read_all
    • 131. write
    • 132. take
    • 133. notify </li></ul>Registers for notifications of events: <ul><li>Write
    • 134. Take
    • 135. Delete </li></ul>
    • 136. Key Features <ul><li>Spaces are shared </li><ul><li>Space handles details of concurrent access </li></ul><li>Spaces are persistent </li><ul><li>If agent process dies, data is still in space
    • 137. However, if space process dies, data is lost (?) </li></ul><li>Spaces are associative </li><ul><li>Associative lookups rather than memory location or identifier </li></ul><li>Spaces are transactionally secure </li><ul><li>Atomic Operations </li></ul><li>Spaces allow us to exchange executable content </li></ul>
    • 138. <ul>A Rinda tuple can be an array or a hash </ul>
    • 139. <ul>A Rinda tuple can be an array or a hash </ul>( But let&apos;s stick with the array, I like that better! )
    • 140. Start a Tuple Space on port 1234
    • 141. Clients/Agents
    • 142. &nbsp;
    • 143. DEMO Rinda
    • 144. RingServer
    • 145. &nbsp;
    • 146. &nbsp;
    • 147. &nbsp;
    • 148. &nbsp;
    • 149. &nbsp;
    • 150. This is also a TupleSpace
    • 151. SPOF
    • 152. Rinda is not persistent... If it crashes while you have tuples in the space, you lose them all.
    • 153. Only Ruby
    • 154. Introducing Blackboard <ul><li>TupleSpace implementation on top of Redis </li><ul><li>-&gt; Persistent </li></ul><li>Redis is a really fast key-value database. </li><ul><li>Like memcached but data is not volatile. </li></ul><li>Same API -&gt; Plug &amp; Play
    • 155. For now, only supports: take, read, and write
    • 156. http://github.com/dambalah/blackboard </li></ul>
    • 157. Server Just start the redis-server: $ redis-server
    • 158. Client/Agents
    • 159. DEMO Blackboard
    • 160. Blackboard Benchmarks
    • 161. Blackboard: Future <ul><li>Move from Redis to a custom based Erlang blackboard implementation.
    • 162. I would like that Erlang implementation to be easily used from other programming languages also.
    • 163. So it&apos;s really two projects: </li><ul><li>Blackboard in erlang
    • 164. Ruby-library to talk to blackboard in erlang </li></ul></ul>
    • 165. Thank you! Luc Castera Founder / messagepub.com
    • 166. Questions?Feedback? [email_address] www.speakerrate.com Luc Castera Founder / messagepub.com
    • 167. Resources / References <ul><li>Part 1: Threading Models </li><ul><li>http://timetobleed.com/threading-models-so-many-different-ways-to-get-stuff-done/
    • 168. http://envycasts.com/products/scaling-ruby
    • 169. http://www.infoq.com/news/2007/05/ruby-threading-futures
    • 170. http://thebogles.com/blog/2006/11/ruby-threading/
    • 171. http://spec.ruby-doc.org/wiki/Ruby_Threading
    • 172. http://www.bitwiese.de/2007/09/on-processes-and-threads.html
    • 173. http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
    • 174. http://bartoszmilewski.wordpress.com/2008/08/24/threads-dont-scale-processes-do/
    • 175. http://en.wikipedia.org/wiki/Global_Interpreter_Lock
    • 176. http://www.gotw.ca/publications/concurrency-ddj.htm
    • 177. http://tinyurl.com/rubyfibers </li></ul></ul>
    • 178. Resources / References <ul><li>Part 2: Multiple Processes </li><ul><li>http://github.com/ezmobius/nanite
    • 179. http://erlang.org/
    • 180. http://www.rabbitmq.com/
    • 181. http://code.google.com/p/redis/
    • 182. http://revactor.org/
    • 183. http://journeta.rubyforge.org/
    • 184. http://home.mindspring.com/~eric_rollins/ParallelRuby.html </li></ul></ul>
    • 185. Resources / References <ul><li>Part 3: TupleSpaces </li><ul><li>http://c2.com/cgi/wiki?TupleSpace
    • 186. http://en.wikipedia.org/wiki/Tuplespace
    • 187. http://www.julianbrowne.com/article/viewer/space-based-architecture-example
    • 188. http://www.rubyagent.com/
    • 189. http://segment7.net/projects/ruby/drb/
    • 190. http://segment7.net/projects/ruby/drb/rinda/ringserver.html
    • 191. JavaSpaces Principles, Patterns, and Practice – Freeman, Hupfer, et. al.
    • 192. http://www.ruby-doc.org/stdlib/libdoc/rinda/rdoc/index.html </li></ul></ul>
    • 193. Things I wish I had time to spend on <ul><li>MPI and Ruby-MPI </li><ul><li>http://github.com/abedra/mpi-ruby/tree/master </li></ul><li>Ruby forkoff: </li><ul><li>http://tinyurl.com/forkoff </li></ul></ul>

    ×