concurrency constructs overview
Post on 06-May-2015
996 Views
Preview:
DESCRIPTION
TRANSCRIPT
Concurrency Constructs overview
Let’s jump into
Who is there
Stas ShevchenkoAccenture
Based on Survey of Concurrency Constructs Ted LeungSun Microsystems
Mainstream is 10 years in the past
Something happened in
2002
Fraction of Chip reachable in one clock cycle
Converting to Figure
[Source:] A Wire-Delay Scalable Microprocessor Architecture for High Performance Systems
Processor Clock Speed
Growing CPU performance
Programming style impact on performance
Sequences
Multithreading
Mobile (up to 4)
1 Mainstream (4)
Office (12)
SUN Unit (up to 128)
Ignoring cores…2 cores won't hurt you
4 cores will hurt a little
8 cores will hurt a bit
16 will start hurting
32 cores will hurt a lot (2009)
...
1 M cores ouch (2019)
Java is so Java
Java Memory Modelwait - notifyThread.yield()Thread.sleep()
synchronized
Executor Framework -> NIO (2)
Books
Books
We DO
• Threads – Program counter – Own stack – Shared Memory
• Locks
Shared memory
Unpredictable state
Crash in critical region
Issues
• Locks– manually lock and unlock – lock ordering is a big problem – locks are not compositional
• How do we decide what is concurrent? • Need to pre-design, but now we have to
retrofit concurrency via new requirements
Design Goals/Space
Mutual ExclusionSerialization / OrderingInherent / Implicit vs Explicit Fine / Medium / Coarse grained Composability
A good solution
• Substantially less error prone• Makes it much easier to identify concurrency • Runs on today’s (and future) parallel hardware – Works if you keep adding cores/threads
Theory
Functional Programming ActorsCSPCCSpetri-netspi-calculusjoin-calculus
The models
• Transactional Memory – Persistent data structures
• Actors• Dataflow • Tuple spaces
Transactional memory
analogous to database transactionsHardware vs. software implementationsIdea goes as far back as 1986 First appearance in a programming language: Concurrent Haskell 2005
Example Clojure
(defn deposit [account amount] (dosync
(let [owner (account :owner) balance-ref
(account :balance-ref)] (do (alter balance-ref +
amount) (println “depositing” amount
(account :owner)))))))
STM Design Space
STM Algorithms / StrategiesGranularity
word vs blockLocks vs Optimistic concurrency Conflict detection
eager vs lazyContention management
STM Problems
• Non abortable operations – I/O
• STM Overhead – read/write barrier elimination
• Where to place transaction boundaries? • Still need condition variables – ordering problems are important
Implementations
Clojure STM - via RefsAkka/Scala
copy from ClojureHaskell/GHC
Use logs and aborts txns
Persistent Data Structures
In Clojure, combined with STM Motivated by copy on write hash-map, vector, sorted map
Available Data StructuresLists, Vectors, Mapshash list based on Vlists VDList - deques based on Vlists red-black trees Real Time Queues and Deques deques, output-restricted deques binary random access lists binomial heaps skew binary random access lists skew binomial heaps catenable listsheaps with efficient merging catenable deques
Problem
• Not really a full model• Oriented towards functional programming
Actors
Invented by Carl Hewitt at MIT (1973) Formal Model
Programming languagesHardware Led to continuations, Scheme
Recently revived by Erlang Erlang’s model is not derived explicitly
from Actors
Simple
Exampleobject account extends Actor {
private var balance = 0
def act() { loop {
react { case Withdraw(amount) => balance -= amount sender ! Balance(balance) case Deposit(amount) => balance += amount sender ! Balance(balance) case BalanceRequest => sender ! Balance(balance) case TerminateRequest =>
} }
}
Problems
DOS of the actor mail queue Multiple actor coordination
reinvent transactions?Actors can still deadlock and starveProgrammer defines granularity
by choosing what is an actor
Impelmentations
ScalaAkka ActorsScala ActorsLift Actors
ErlangOTP
CLR F# / Axum
Perfomance
Actor create/destroyMessage passingMemory usage
Erlang vs JVM
Erlangper process GC heap tail calldistributed
JVMper JVM heaptail call (fixed in JSR-292?, at least in scalac) not distributedfew kinds of actors (Scala)
Actor Variants
Clojure AgentsDesigned for loosely coupled stuff Code/actions sent to agentsCode is queued when it hits the agentAgent framework guarantees serializationState of agent is always available for read (unlike actors
which could be busy processing when you send a read message)
not in favor of transparent distribution Clojure agents can operate in an ‘open world’ - actors
answer a specific set of messages
Dataflow
Dataflow is a software architecture based on the idea that changing the value of a variable should automatically force recalculation of the values of variables which depend on its valueBill Ackerman’s PhD Thesis at MIT (1984)Declarative Concurrency in functional languagesResearch in the 1980’s and 90’sInherent concurrency
Turns out to be very difficult to implementInterest in declarative concurrency is slowly returning
The Model
Dataflow Variables create variablebind valueread value or block
ThreadsDataflow Streams
List whose tail is an unbound dataflow variableDeterministic computation!
Example: Variables 1object Test5 extends App { val x, y, z = new DataFlowVariable[Int]
val main = thread { x << 1
if (x() > y()) { z << x } else {z << y } }
val setY = thread { Thread.sleep(5000) y << 2 }
main ! 'exit setY ! 'exit}
Example: Streams (Oz) fun {Ints N Max}
if N == Max then nil else
{Delay 1000} N|{Ints N+1 Max}
end end fun {Sum S Stream}
case Stream of nil then S [] H|T then S|{Sum H+S T} end
end local X Y in
thread X = {Ints 0 1000} end thread Y = {Sum 0 X} end {Browse Y}
end
Implementations
Mozart Ozhttp://www.mozart-oz.org/
Akkahttp://github.com/jboner/scala-dataflow
dataflow variables and streams Ruby library
http://github.com/larrytheliquid/dataflow dataflow variables and streams Groovy
http://code.google.com/p/gparallelizer/
Problems
Can’t handle non-determinismlike a serverNeed ports
this leads to actor like things
Tuple Spaces Model
Originated in Linda (1984) Popularized by Jini
The Model
Three operations write() (out)take() (in)read()
The Model
Space uncouplingTime uncouplingReaders are decoupled from WritersContent addressable by pattern matchingCan emulate
Actor like continuationsCSPMessage PassingSemaphores
Example public class Account implements Entry {
public Integer accountNo; public Integer value; public Account() { ... } public Account(int accountNo, int value {
this.accountNo = newInteger(accountNo); this.value = newInteger(value);
} }
try { Account newAccount = new Account(accountNo, value); space.write(newAccount,
null, Lease.FOREVER); }
space.read(accountNo);
Implementations
Jini/JavaSpaceshttp://incubator.apache.org/river/RIVER/
index.html BlitzSpaces
http://www.dancres.org/blitz/blitz_js.html PyLinda
http://code.google.com/p/pylinda/ Rinda
built in to Ruby
Problems
Low levelHigh latency to the space - the space is contention point / hot spotScalabilityMore for distribution than concurrency
Projects
ScalaErlangClojureKamaeliaHaskellAxum/F#Mozart/OzAkka
Work to be done
More in depth comparisons on 4+ core platforms Higher level frameworksApplication architectures/patterns
WebMiddleware
Outcomes
F..ck the shared state,Mutable means non-scalable
It is not too early!
QA
top related