SlideShare uma empresa Scribd logo
1 de 55
Baixar para ler offline
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

Mais conteĂşdo relacionado

Semelhante a Concurrency Patterns

MongoDB Indexing Constraints and Creative Schemas
MongoDB Indexing Constraints and Creative SchemasMongoDB Indexing Constraints and Creative Schemas
MongoDB Indexing Constraints and Creative Schemas
MongoDB
 
Angular js 24 april 2013 amsterdamjs
Angular js   24 april 2013 amsterdamjsAngular js   24 april 2013 amsterdamjs
Angular js 24 april 2013 amsterdamjs
Marcin Wosinek
 

Semelhante a Concurrency Patterns (6)

MongoDB Indexing Constraints and Creative Schemas
MongoDB Indexing Constraints and Creative SchemasMongoDB Indexing Constraints and Creative Schemas
MongoDB Indexing Constraints and Creative Schemas
 
K-Means, its Variants and its Applications
K-Means, its Variants and its ApplicationsK-Means, its Variants and its Applications
K-Means, its Variants and its Applications
 
Ten Groovy Little JavaScript Tips
Ten Groovy Little JavaScript TipsTen Groovy Little JavaScript Tips
Ten Groovy Little JavaScript Tips
 
Angular js 24 april 2013 amsterdamjs
Angular js   24 april 2013 amsterdamjsAngular js   24 april 2013 amsterdamjs
Angular js 24 april 2013 amsterdamjs
 
Musings of kaggler
Musings of kagglerMusings of kaggler
Musings of kaggler
 
On-Homomorphic-Encryption-and-Secure-Computation.ppt
On-Homomorphic-Encryption-and-Secure-Computation.pptOn-Homomorphic-Encryption-and-Secure-Computation.ppt
On-Homomorphic-Encryption-and-Secure-Computation.ppt
 

Mais de ryanstout (8)

Neural networks - BigSkyDevCon
Neural networks - BigSkyDevConNeural networks - BigSkyDevCon
Neural networks - BigSkyDevCon
 
Volt 2015
Volt 2015Volt 2015
Volt 2015
 
Isomorphic App Development with Ruby and Volt - Rubyconf2014
Isomorphic App Development with Ruby and Volt - Rubyconf2014Isomorphic App Development with Ruby and Volt - Rubyconf2014
Isomorphic App Development with Ruby and Volt - Rubyconf2014
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 
EmberJS
EmberJSEmberJS
EmberJS
 
Practical Machine Learning and Rails Part2
Practical Machine Learning and Rails Part2Practical Machine Learning and Rails Part2
Practical Machine Learning and Rails Part2
 
Practical Machine Learning and Rails Part1
Practical Machine Learning and Rails Part1Practical Machine Learning and Rails Part1
Practical Machine Learning and Rails Part1
 
Intro to Advanced JavaScript
Intro to Advanced JavaScriptIntro to Advanced JavaScript
Intro to Advanced JavaScript
 

Último

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Concurrency Patterns