• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Concurrency Patterns
 

Concurrency Patterns

on

  • 1,117 views

 

Statistics

Views

Total Views
1,117
Views on SlideShare
310
Embed Views
807

Actions

Likes
3
Downloads
11
Comments
0

4 Embeds 807

http://www.agileproductions.com 328
http://agileproductions.com 224
http://ipv6.agileproductions.com 128
http://www.ipv6.agileproductions.com 127

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Concurrency Patterns Concurrency Patterns Presentation Transcript

    • Author:RyanStout ConcurrencyAnoverviewofconcurrentprocessingmodels Date:June22nd,2013 Wednesday, July 10, 13
    • Wednesday, July 10, 13
    • singlecoreperformanceisn’tincreasinganymore ConcurrencyModels NoMoreFreeLunch Wednesday, July 10, 13
    • singlecoreperformanceisn’tincreasinganymore ConcurrencyModels NoMoreFreeLunch Wednesday, July 10, 13
    • ConcurrencyModels Question:Howdowetake advantageoftheextracores? Wednesday, July 10, 13
    • ConcurrencyModels Wednesday, July 10, 13
    • ConcurrencyModels Answer:Concurrency&Parallelism A Concurrent Parallel B Sequential A A B B A B Wednesday, July 10, 13
    • •Onlydifficultwhensharingstate •Hardtoreasonabout •Raceconditions •Deadlock ConcurrencyModels Problem:Parallelcanbedifficult Wednesday, July 10, 13
    • 1.Processes 2.ThreadsandLocks 3.Evented-io 4.Vectorization/SIMD 5.ActorModel-Erlang,Celluloid,Akka 6.CSP-GoogleGo 7.SoftwareTransactionalMemory Differentwaytodothingsatthesametime ConcurrencyModels Solution(sortof): BetterConcurrencyModels Wednesday, July 10, 13
    • separatememorymodel ConcurrencyModels 1.Processes Wednesday, July 10, 13
    • separatememorymodel ConcurrencyModels 1.Processes Wednesday, July 10, 13
    • •Pros: •Easy •OShandleseverything •Goodforstatelessoperations(Respondingtowebrequests) •NoMemoryLeaks • Cons: •Communicationbetweenprocessesisdifficult •Ramgetsusedforeachprocess Thewayunixintended ConcurrencyModels 1.Processes Process A Thread A Process B Thread B Wednesday, July 10, 13
    • ConcurrencyModels 2.Threads Wednesday, July 10, 13
    • ConcurrencyModels 2.Threads Process Thread A Thread B Wednesday, July 10, 13
    • require 'thread' class ThreadExample def initialize @count = 0 @count_mutex = Mutex.new # Start a few readers threads = [] 3.times do threads << Thread.new do read_values end end threads.each(&:join) end ... ConcurrencyModels 2.Threaded/MutexExample ... def read_values loop do @count_mutex.synchronize do local_count = @count # Get the "value" local_count += rand(10) @count = local_count puts "Value: #{local_count}" end end end end ThreadExample.new Wednesday, July 10, 13
    • SharedMemoryModel •Pros: •Lessramthanprocesses • Cons: •Manuallyhandlinglocks •Deadlocks •Non-determanistic •HardtoDebug thewayJavaintended ConcurrencyModels 2.Threads Wednesday, July 10, 13
    • ConcurrencyModels 3.Evented-io Process Reactor A B A Wednesday, July 10, 13
    • require 'socket' server = TCPServer.open(host, port) while client = server.accept Thread.new do line = client.gets client.puts(line) client.close end end ConcurrencyModels ThreadedEchoServer Wednesday, July 10, 13
    • require 'eventmachine' class Echo < EventMachine::Connection def receive_data(data) send_data(data) end end EventMachine.run { EventMachine.connect '127.0.0.1', 8081, Echo } ConcurrencyModels Evented-io-EchoServer Wednesday, July 10, 13
    • module ProxyConnection def initialize(local_connection) @local_connection = local_connection end def receive_data(data) @local_connection.send_data(data) end end module ProxyServer def post_init # Make a connection to the remote server @connection = EventMachine.connect 'www.zeebly.com', 80, ProxyConnection, self end def receive_data(data) @connection.send_data(data) end end EventMachine::run do EventMachine::start_server "127.0.0.1", 8080, ProxyServer end ConcurrencyModels Evented-io-ProxyServer Wednesday, July 10, 13
    • Aformofcooperativemultitasking •Pros: •GreatforheavyIOwork •Savesthreadswitchingcost •GoodOSlevelsupport(epoll,kqueue) • Cons: •Longrunningoperationsblockallotheroperations •Doesnotusemultiplecores •Somewhatdifficulttoreasonabout Libraries:EventMachine(ruby),Tornado(python),nodejs,libev(C) thewaynginxintended-concurrent,notparallel ConcurrencyModels 3.Evented-io Process Reactor A B A Wednesday, July 10, 13
    • ConcurrencyModels 4.Vectorization/SIMD Process Data A Data B 0.51 0.98 1.5 0.29 0.75 0.11 0.39 0.19 1.22 1.6 0.84 0.90 Instruction: Multiply(A,B) Wednesday, July 10, 13
    • void MatrixMulOnHost(float* M, float* N, float* P, int Width) { for (int i = 0; i < Width; ++i) { for (int j = 0; j < Width; ++j) { float sum = 0; for (int k = 0; k < Width; ++k) { float a = M[i * Width + k]; float b = N[k * Width + j]; sum += a * b; } P[i * Width + j] = sum; } } } ConcurrencyModels ExampleMatrixMultiplication-C Wednesday, July 10, 13
    • __global__ void MatrixMulKernel(float* d_M, float* d_N, float* d_P, int Width) { int row = threadIdx.y; int col = threadIdx.x; float P_val = 0; for (int k = 0; k < Width; ++k) { float M_elem = d_M[row * Width + k]; float N_elem = d_N[k * Width + col]; P_val += M_elem * N_elem; } d_p[row*Widthh+col] = P_val; } ConcurrencyModels ExampleMatrixMultiplication- CUDA Wednesday, July 10, 13
    • SIMD=SingleInstructionMultipleData •Pros: •Veryfastforsomeoperations(~50xspeedimporvementinsome cases) • Cons: •Onlyworkswhenworkingwithvectordata •Nostandardwaytoprogram(CUDA,OpenCL,SSE,BLAS) •TimetocopydatabetweenCPUandGPUaddsup thewayNvidiaintended ConcurrencyModels 4.Vectorization/SIMD Wednesday, July 10, 13
    • “Don’tcommunicatebysharingstate;share statebycommunicating” http://www.igvita.com/2010/12/02/concurrency-with-actors-goroutines-ruby/ ConcurrencyModels 5.&6.ActorModel&CSP Wednesday, July 10, 13
    • •Stateisencapsulatedin“Actors” •Actorssendmessagestocommunicate •Messagesareasynchronous •Messagesbufferina“mailbox” Libraries:Akka(scala),Celluloid(ruby),Erlang(builtin) thewayErlangintended ConcurrencyModels 5.ActorModel Wednesday, July 10, 13
    • thewayErlangintended ConcurrencyModels 5.ActorModel Process Actor A Actor B Actor C Wednesday, July 10, 13
    • require 'actor' ping = nil ping = Actor.spawn do loop do msg = Actor.receive puts "Ping #{msg}" pong << (msg + 1) end end ... ConcurrencyModels ActorExample ... pong = Actor.spawn do loop do msg = Actor.receive puts "Ping #{msg}" break if msg > 10000 ping << (msg + 1) end end ping << 0 Wednesday, July 10, 13
    • •Pros: •Nodeadlocks(ifyoufollowtherules) •Fairlyeasytoreasonabout • Cons: •Connectingactorsisdifficult •Errorhandlingistricky thewayErlangintended ConcurrencyModels 5.ActorModel Wednesday, July 10, 13
    • ConcurrencyModels Process A C 6.CommunicatingSequentialProcesses B Channel A Wednesday, July 10, 13
    • •GoogleGo’sModel •Variablesarealwaysthreadlocal •Stateissharedbysizedchannels •Functionscanberunasa“goroutine” •Anonymousthreadofexecution •Cannotbereferencedorjoined •Communicationtakesplaceoverchannels •Channelsaresizedsynchronizedbuffers,whichcanbe“closed” thewaygoogleintended ConcurrencyModels 6.CommunicatingSequentialProcesses Wednesday, July 10, 13
    • •Pros: •Easytoreasonabout •Simplemodelfordistributingdataprocessingwork •Simpletoimplementbyhand(sizedqueue’s) •Cons: •Canbeextracodeforsimplesynchrnoization Libraries:GoogleGo(language),Agent(ruby) thewaygoogleintended ConcurrencyModels 6.CommunicatingSequentialProcesses Wednesday, July 10, 13
    • •InActorwenametheActors •InCSPwenamethecommunicationchannels •Bothallowustokeepoutnon-determinism(ifwefollow therules) ConcurrencyModels DifferencebetweenCSPandActor? Wednesday, July 10, 13
    • ConcurrencyModels 7.SoftwareTransactionalMemory Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write SUCCESS Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write SUCCESS FAIL Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write SUCCESS FAIL Wednesday, July 10, 13
    • ConcurrencyModels 7.SoftwareTransactionalMemory Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Wednesday, July 10, 13
    • ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write SUCCESS Wednesday, July 10, 13
    • •Pros: •Easytoreasonabout • Cons: •Moreresourcecontention=worseperformance •Inordertopreventcontention,wemustuseimmutabledata structures--whichmakesitmoredifficulttoreasonaboutagain thewayClojureintended ConcurrencyModels 7.SoftwareTransactionalMemory Wednesday, July 10, 13
    • •FunctionalReactiveProgramming ConcurrencyModels 8.BonusModel Wednesday, July 10, 13
    • ConcurrencyModels WhichModelisBest? Wednesday, July 10, 13
    • •Dependsontheproblem ConcurrencyModels WhichModelisBest? Wednesday, July 10, 13
    • •Dependsontheproblem •Dependsonthelanguage ConcurrencyModels WhichModelisBest? Wednesday, July 10, 13
    • •http://www.igvita.com/2010/12/02/concurrency-with-actors-goroutines-ruby/ •http://en.wikipedia.org/wiki/Dining_philosophers_problem •DiningPhilosophersProblem-http://rosettacode.org/wiki/Dining_philosophers ConcurrencyModels MoreInfo Wednesday, July 10, 13
    • Author:RyanStout Thanks!anyquestions? Date:June22nd,2013 Wednesday, July 10, 13