Top Banner
CONCURRENCY: RUBIES, PLURAL Elise Huard - RubyAndRails 2010 Thursday 21 October 2010
42
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrency

CONCURRENCY:RUBIES, PLURAL

Elise Huard - RubyAndRails 2010Thursday 21 October 2010

Page 2: Concurrency

MULTIPROCESSOR/MULTICORE

Thursday 21 October 2010

Page 3: Concurrency

NETWORK ON CHIP(50..96..100 CORES)

Thursday 21 October 2010

Page 4: Concurrency

“... 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

But ...forget all that

(mostly)

Thursday 21 October 2010

Page 6: Concurrency

language VM

OS(kernel processes, other processes)

Your program

multicore - multiCPU

Thursday 21 October 2010

Page 7: Concurrency

Concurrent programs!=

Parallel computing

Thursday 21 October 2010

Page 8: Concurrency

Thursday 21 October 2010

Page 9: Concurrency

Thursday 21 October 2010

Page 10: Concurrency

Scheduling

• preemptive -> thread is told to yield (by kernel or other thread)

• cooperative -> thread yields control

Thursday 21 October 2010

Page 11: Concurrency

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

Ruby

• Process

• Thread - green in MRI 1.8, native threads in MRI 1.9, Rubinius, JRuby

Thursday 21 October 2010

Page 13: Concurrency

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

MRI: GIL

from http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/ @igrigorik

Thursday 21 October 2010

Page 15: Concurrency

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

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

multiprocess

•advantage: separate state

•disadvantage: overhead to spawning + context switching

Thursday 21 October 2010

Page 18: Concurrency

Ruby: multiprocess

•fork and IPC: IO.pipe, Mmap, ...

•DRb

Thursday 21 October 2010

Page 19: Concurrency

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

Threads

Shared state:

Mutex

ConditionVariable (semaphore)

MonitorMixin, Sync_m

Thursday 21 October 2010

Page 21: Concurrency

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

Fibers

• cooperative scheduling

• coroutines

• for MRI: lightweight

• JRuby, Rubinius: Fiber mapped to native thread

Thursday 21 October 2010

Page 23: Concurrency

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

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

Thursday 21 October 2010

Page 26: Concurrency

shared state: will melt your brain

• non-determinism

• atomicity

• deadlock

• livelock

• fairness/starvation

• race conditions

Thursday 21 October 2010

Page 27: Concurrency

actor model

• named actors: have no shared state

• asynchronous message passing (fire and forget)

Thursday 21 October 2010

Page 28: Concurrency

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

Concurrency oriented languages

• Erlang (Actors)

• Clojure

• Go (CSP)

• Haskell (several)

• Scala (Actors)

• ...

Thursday 21 October 2010

Page 30: Concurrency

Ideas

• functional programming: side effect free function calls

- immutable data

• nothing shared (advantage: distributed = local)

• message passing

Thursday 21 October 2010

Page 31: Concurrency

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

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

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

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

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

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

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

Ruby: STM

• @mentalguy thought experiment

• @technomancy clojure-gem

Thursday 21 October 2010

Page 40: Concurrency

Kernel stuff

Some of these problems have been solved before ...

Thursday 21 October 2010

Page 41: Concurrency

FUN :)

Thursday 21 October 2010

Page 42: Concurrency

References:

http://www.delicious.com/elisehuard/concurrency

http://github.com/elisehuard/rubyandrails-2010

Elise Huard

@elise_huard

http://jabberwocky.eu

Thursday 21 October 2010