Concurrency

Post on 15-Jan-2015

1.550 views 7 download

Tags:

description

 

Transcript of Concurrency

CONCURRENCY:RUBIES, PLURAL

Elise Huard - RubyAndRails 2010Thursday 21 October 2010

MULTIPROCESSOR/MULTICORE

Thursday 21 October 2010

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

Thursday 21 October 2010

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

But ...forget all that

(mostly)

Thursday 21 October 2010

language VM

OS(kernel processes, other processes)

Your program

multicore - multiCPU

Thursday 21 October 2010

Concurrent programs!=

Parallel computing

Thursday 21 October 2010

Thursday 21 October 2010

Thursday 21 October 2010

Scheduling

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

• cooperative -> thread yields control

Thursday 21 October 2010

Processes, Threads

Process 2

RAMmemory space

Process 1

thread1

scheduler (OS)

CPU CPU

memory space

thread2 t1 t2 t3

Thursday 21 October 2010

Ruby

• Process

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

Thursday 21 October 2010

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

MRI: GIL

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

Thursday 21 October 2010

Other Rubies

• @evanphx working on removing the GIL on Rubinius (Hydra branch)

• JRuby, IronRuby, MacRuby don’t have GIL

Thursday 21 October 2010

Parallel quicksort

[5, 3, 22, 12, 15, -112, 6]

[-122, 5, 3, 6] [22, 15]

[-122] [5, 6]

12

3

Thursday 21 October 2010

multiprocess

•advantage: separate state

•disadvantage: overhead to spawning + context switching

Thursday 21 October 2010

Ruby: multiprocess

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

•DRb

Thursday 21 October 2010

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

Threads

Shared state:

Mutex

ConditionVariable (semaphore)

MonitorMixin, Sync_m

Thursday 21 October 2010

Ruby threads def execute(&block) sorted = nil thread = Thread.new do sorted = block.call end thread.join sorted end

Thursday 21 October 2010

Fibers

• cooperative scheduling

• coroutines

• for MRI: lightweight

• JRuby, Rubinius: Fiber mapped to native thread

Thursday 21 October 2010

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

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

Thursday 21 October 2010

shared state: will melt your brain

• non-determinism

• atomicity

• deadlock

• livelock

• fairness/starvation

• race conditions

Thursday 21 October 2010

actor model

• named actors: have no shared state

• asynchronous message passing (fire and forget)

Thursday 21 October 2010

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

Concurrency oriented languages

• Erlang (Actors)

• Clojure

• Go (CSP)

• Haskell (several)

• Scala (Actors)

• ...

Thursday 21 October 2010

Ideas

• functional programming: side effect free function calls

- immutable data

• nothing shared (advantage: distributed = local)

• message passing

Thursday 21 October 2010

erlang

• Actor model: Actors, asynchronous message passing

• actors = “green processes”

• efficient VM (SMP enabled since R12B)

• high reliability

© ericsson 2007

Thursday 21 October 2010

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

Rubinius: Actors

• actors in the language: threads with inbox

• VM actors to communicate between actors in different VMs

Thursday 21 October 2010

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

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

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

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

Ruby: STM

• @mentalguy thought experiment

• @technomancy clojure-gem

Thursday 21 October 2010

Kernel stuff

Some of these problems have been solved before ...

Thursday 21 October 2010

FUN :)

Thursday 21 October 2010

References:

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

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

Elise Huard

@elise_huard

http://jabberwocky.eu

Thursday 21 October 2010