Concurrency Patterns

1,506 views

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,506
On SlideShare
0
From Embeds
0
Number of Embeds
855
Actions
Shares
0
Downloads
25
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Concurrency Patterns

  1. 1. Author:RyanStout ConcurrencyAnoverviewofconcurrentprocessingmodels Date:June22nd,2013 Wednesday, July 10, 13
  2. 2. Wednesday, July 10, 13
  3. 3. singlecoreperformanceisn’tincreasinganymore ConcurrencyModels NoMoreFreeLunch Wednesday, July 10, 13
  4. 4. singlecoreperformanceisn’tincreasinganymore ConcurrencyModels NoMoreFreeLunch Wednesday, July 10, 13
  5. 5. ConcurrencyModels Question:Howdowetake advantageoftheextracores? Wednesday, July 10, 13
  6. 6. ConcurrencyModels Wednesday, July 10, 13
  7. 7. ConcurrencyModels Answer:Concurrency&Parallelism A Concurrent Parallel B Sequential A A B B A B Wednesday, July 10, 13
  8. 8. •Onlydifficultwhensharingstate •Hardtoreasonabout •Raceconditions •Deadlock ConcurrencyModels Problem:Parallelcanbedifficult Wednesday, July 10, 13
  9. 9. 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
  10. 10. separatememorymodel ConcurrencyModels 1.Processes Wednesday, July 10, 13
  11. 11. separatememorymodel ConcurrencyModels 1.Processes Wednesday, July 10, 13
  12. 12. •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
  13. 13. ConcurrencyModels 2.Threads Wednesday, July 10, 13
  14. 14. ConcurrencyModels 2.Threads Process Thread A Thread B Wednesday, July 10, 13
  15. 15. 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
  16. 16. SharedMemoryModel •Pros: •Lessramthanprocesses • Cons: •Manuallyhandlinglocks •Deadlocks •Non-determanistic •HardtoDebug thewayJavaintended ConcurrencyModels 2.Threads Wednesday, July 10, 13
  17. 17. ConcurrencyModels 3.Evented-io Process Reactor A B A Wednesday, July 10, 13
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. 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
  24. 24. __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
  25. 25. SIMD=SingleInstructionMultipleData •Pros: •Veryfastforsomeoperations(~50xspeedimporvementinsome cases) • Cons: •Onlyworkswhenworkingwithvectordata •Nostandardwaytoprogram(CUDA,OpenCL,SSE,BLAS) •TimetocopydatabetweenCPUandGPUaddsup thewayNvidiaintended ConcurrencyModels 4.Vectorization/SIMD Wednesday, July 10, 13
  26. 26. “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
  27. 27. •Stateisencapsulatedin“Actors” •Actorssendmessagestocommunicate •Messagesareasynchronous •Messagesbufferina“mailbox” Libraries:Akka(scala),Celluloid(ruby),Erlang(builtin) thewayErlangintended ConcurrencyModels 5.ActorModel Wednesday, July 10, 13
  28. 28. thewayErlangintended ConcurrencyModels 5.ActorModel Process Actor A Actor B Actor C Wednesday, July 10, 13
  29. 29. 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
  30. 30. •Pros: •Nodeadlocks(ifyoufollowtherules) •Fairlyeasytoreasonabout • Cons: •Connectingactorsisdifficult •Errorhandlingistricky thewayErlangintended ConcurrencyModels 5.ActorModel Wednesday, July 10, 13
  31. 31. ConcurrencyModels Process A C 6.CommunicatingSequentialProcesses B Channel A Wednesday, July 10, 13
  32. 32. •GoogleGo’sModel •Variablesarealwaysthreadlocal •Stateissharedbysizedchannels •Functionscanberunasa“goroutine” •Anonymousthreadofexecution •Cannotbereferencedorjoined •Communicationtakesplaceoverchannels •Channelsaresizedsynchronizedbuffers,whichcanbe“closed” thewaygoogleintended ConcurrencyModels 6.CommunicatingSequentialProcesses Wednesday, July 10, 13
  33. 33. •Pros: •Easytoreasonabout •Simplemodelfordistributingdataprocessingwork •Simpletoimplementbyhand(sizedqueue’s) •Cons: •Canbeextracodeforsimplesynchrnoization Libraries:GoogleGo(language),Agent(ruby) thewaygoogleintended ConcurrencyModels 6.CommunicatingSequentialProcesses Wednesday, July 10, 13
  34. 34. •InActorwenametheActors •InCSPwenamethecommunicationchannels •Bothallowustokeepoutnon-determinism(ifwefollow therules) ConcurrencyModels DifferencebetweenCSPandActor? Wednesday, July 10, 13
  35. 35. ConcurrencyModels 7.SoftwareTransactionalMemory Wednesday, July 10, 13
  36. 36. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
  37. 37. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
  38. 38. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
  39. 39. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
  40. 40. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write Wednesday, July 10, 13
  41. 41. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Block A read process transactional write SUCCESS Wednesday, July 10, 13
  42. 42. 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
  43. 43. 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
  44. 44. ConcurrencyModels 7.SoftwareTransactionalMemory Wednesday, July 10, 13
  45. 45. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Wednesday, July 10, 13
  46. 46. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Wednesday, July 10, 13
  47. 47. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write Wednesday, July 10, 13
  48. 48. ConcurrencyModels Process Memory Variable A 7.SoftwareTransactionalMemory Block B read process transactional write SUCCESS Wednesday, July 10, 13
  49. 49. •Pros: •Easytoreasonabout • Cons: •Moreresourcecontention=worseperformance •Inordertopreventcontention,wemustuseimmutabledata structures--whichmakesitmoredifficulttoreasonaboutagain thewayClojureintended ConcurrencyModels 7.SoftwareTransactionalMemory Wednesday, July 10, 13
  50. 50. •FunctionalReactiveProgramming ConcurrencyModels 8.BonusModel Wednesday, July 10, 13
  51. 51. ConcurrencyModels WhichModelisBest? Wednesday, July 10, 13
  52. 52. •Dependsontheproblem ConcurrencyModels WhichModelisBest? Wednesday, July 10, 13
  53. 53. •Dependsontheproblem •Dependsonthelanguage ConcurrencyModels WhichModelisBest? Wednesday, July 10, 13
  54. 54. •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
  55. 55. Author:RyanStout Thanks!anyquestions? Date:June22nd,2013 Wednesday, July 10, 13

×