• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Concurrency Constructs Overview
 

Concurrency Constructs Overview

on

  • 1,061 views

Functional Programming ...

Functional Programming
Actors
CSP
CCS
petri-net
s pi-calculus
join-calculus
Transactional Memory
Persistent data structures
Actors
Dataflow
Tuple spaces

Statistics

Views

Total Views
1,061
Views on SlideShare
1,061
Embed Views
0

Actions

Likes
1
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

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
  • and sometimes called the instruction address register (IAR)[1] or just part of the instruction sequencer,[2] is a processor register that indicates where a computer is in its programsequence.
  • For the concept, see Mutually exclusive events."mutex" redirects here. For the computer program object that negotiates mutual exclusion among threads, see lock (computer science).Coarse-grained systems consist of fewer, larger components than fine-grained systems; a coarse-grained description of a system regards large subcomponents while a fine-grained description regards smaller components of which the larger ones are composed.
  • - the process calculi (or process algebras) are a diverse family of related approaches for formally modellingconcurrent systems.Communicating Sequential Processes (limbo and Go) - process calculusThe approach taken in developing CSP into a process algebra was influenced by Robin Milner's work on theCalculus of Communicating Systems (CCS), and vice versa. CCS is useful for evaluating the qualitative correctness of properties of a system such as deadlock or livelockA Petri net (also known as a place/transition net or P/T net) is one of several mathematical modeling languages for the description of distributed systemsPi - as a continuation of work on the process calculus CCS (Calculus of Communicating Systems). The π-calculus allows channel names to be communicated along the channels themselves, and in this way it is able to describe concurrent computations whose network configuration may change during the computation.The join-calculus is a member of the -calculus family of process calculi, and can be considered, at its core, an asynchronous -calculus with several strong restrictions[3]:Scope restriction, reception, and replicated reception are syntactically merged into a single construct, the definition;Communication occurs only on defined names;For every defined name there is exactly one replicated reception.
  • Transactional memory attempts to simplify parallel programming by allowing a group of load and store instructions to execute in an atomic way. It is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing.A dataflow network is a network of concurrently executing processes or automata that can communicate by sending data over channels (seemessage passing.)A tuple space is an implementation of the associative memory paradigm for parallel/distributed computing. It provides a repository of tuples that can be accessed concurrently. As an illustrative example, consider that there are a group of processors that produce pieces of data and a group of processors that use the data. Producers post their data as tuples in the space, and the consumers then retrieve data from the space that match a certain pattern. This is also known as the blackboard metaphor. Tuple space may be thought as a form of distributed shared memory.Tuple spaces were the theoretical underpinning of the Linda language developed by David Gelernter and Nicholas Carriero at Yale University.Implementations of tuple spaces have also been developed for Java (JavaSpaces), Lisp, Lua, Prolog, Python, Ruby, Smalltalk, Tcl, and the.NET framework.
  • . A contention management process is invoked when a conflict occurs between a first transaction and a second transaction. The pre-determined commit order is used in the contention management process to aid in determining whether the first transaction or the second transaction should win the conflict and be allowed to proceed."
  • Dataflow concurrency is deterministic. This means that it will always behave the same. If you run it once and it yields output 5 then it will do that every time, run it 10 million times, same result. If it on the other hand deadlocks the first time you run it, then it will deadlock every single time you run it. Also, there is no difference between sequential code and concurrent code. These properties makes it very easy to reason about concurrency. The limitation is that the code needs to be side-effect free, e.g. deterministic. You can’t use exceptions, time, random etc., but need to treat the part of your program that uses dataflow concurrency as a pure function with input and output.The best way to learn how to program with dataflow variables is to read the fantastic book Concepts, Techniques, and Models of Computer Programming. By Peter Van Roy and Seif Haridi.

Concurrency Constructs Overview Concurrency Constructs Overview Presentation Transcript

  • Concurrency Constructs overview Let’s jump into
  • Who is thereStas ShevchenkoAccentureBased onSurvey of Concurrency ConstructsTed 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 PerformanceSystems
  • 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 wont hurt you4 cores will hurt a little8 cores will hurt a bit16 will start hurting32 cores will hurt a lot (2009)...1 M cores ouch (2019)
  • Java is so JavaJava Memory Modelwait - notifyThread.yield()Thread.sleep()synchronizedExecutor 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 ExclusionSerialization / OrderingInherent / 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
  • TheoryFunctional ProgrammingActorsCSPCCSpetri-netspi-calculusjoin-calculus
  • The models• Transactional Memory – Persistent data structures• Actors• Dataflow• Tuple spaces
  • Transactional memoryanalogous to database transactionsHardware vs. software implementationsIdea goes as far back as 1986First 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 SpaceSTM Algorithms / Strategies Granularity word vs blockLocks vs Optimistic concurrencyConflict 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
  • ImplementationsClojure STM - via RefsAkka/Scala copy from ClojureHaskell/GHC Use logs and aborts txns
  • Persistent Data StructuresIn Clojure, combined with STMMotivated by copy on writehash-map, vector, sorted map
  • Available Data StructuresLists, Vectors, Mapshash list based on VlistsVDList - deques based on Vlistsred-black treesReal Time Queues and Dequesdeques, output-restricted dequesbinary random access listsbinomial heapsskew binary random access listsskew binomial heapscatenable listsheaps with efficient mergingcatenable deques
  • Problem• Not really a full model• Oriented towards functional programming
  • ActorsInvented by Carl Hewitt at MIT (1973)Formal Model Programming languages Hardware Led to continuations, SchemeRecently revived by Erlang Erlang’s model is not derived explicitly fromActors
  • 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 => } }}
  • ProblemsDOS of the actor mail queueMultiple actor coordination reinvent transactions?Actors can still deadlock and starveProgrammer defines granularity by choosing what is an actor
  • ImpelmentationsScala Akka Actors Scala Actors Lift ActorsErlang OTPCLR F# / Axum
  • PerfomanceActor create/destroyMessage passingMemory usage
  • Erlang vs JVMErlang per process GC heap tail call distributedJVM per JVM heap tail call (fixed in JSR-292?, at least in scalac) not distributed few kinds of actors (Scala)
  • Actor VariantsClojure Agents Designed for loosely coupled stuff Code/actions sent to agents Code is queued when it hits the agent Agent framework guarantees serialization State of agent is always available for read (unlikeactors which could be busy processing when you send aread message) not in favor of transparent distribution Clojure agents can operate in an ‘open world’ - actorsanswer a specific set of messages
  • DataflowDataflow is a software architecture based on the ideathat changing the value of a variable should automaticallyforce recalculation of the values of variables whichdepend 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 ModelDataflow Variables create variable bind value read value or blockThreadsDataflow 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} endendfun {Sum S Stream} case Stream of nil then S [] H|T then S|{Sum H+S T} endendlocal X Y in thread X = {Ints 0 1000} end thread Y = {Sum 0 X} end {Browse Y}end
  • ImplementationsMozart Oz http://www.mozart-oz.org/Akka http://github.com/jboner/scala-dataflow dataflow variables and streamsRuby library http://github.com/larrytheliquid/dataflow dataflow variables and streamsGroovy http://code.google.com/p/gparallelizer/
  • ProblemsCan’t handle non-determinism like a server Need ports this leads to actor like things
  • Tuple Spaces ModelOriginated in Linda (1984)Popularized by Jini
  • The ModelThree operations write() (out) take() (in) read()
  • The ModelSpace uncouplingTime uncouplingReaders are decoupled from WritersContent addressable by pattern matchingCan emulate Actor like continuations CSP Message Passing Semaphores
  • Examplepublic 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);
  • ImplementationsJini/JavaSpaces http://incubator.apache.org/river/RIVER/index.htmlBlitzSpaces http://www.dancres.org/blitz/blitz_js.htmlPyLinda http://code.google.com/p/pylinda/Rinda built in to Ruby
  • ProblemsLow levelHigh latency to the space - the space iscontention point / hot spotScalabilityMore for distribution than concurrency
  • ProjectsScalaErlangClojureKamaeliaHaskellAxum/F#Mozart/OzAkka
  • Work to be doneMore in depth comparisons on 4+ coreplatformsHigher level frameworksApplication architectures/patterns Web Middleware
  • OutcomesF..ck the shared state,Mutable means non-scalableIt is not too early!
  • QA