concurrency constructs overview

58
Concurrency Constructs overview Let’s jump into

Upload: stasimus

Post on 06-May-2015

996 views

Category:

Technology


1 download

DESCRIPTION

Functional Programming Actors CSP CCS petri-nets pi-calculus join-calculusTransactional Memory Persistent data structures ActorsDataflow Tuple spaces

TRANSCRIPT

Page 1: Concurrency Constructs Overview

Concurrency Constructs overview

Let’s jump into

Page 2: Concurrency Constructs Overview

Who is there

Stas ShevchenkoAccenture

Based on Survey of Concurrency Constructs Ted LeungSun Microsystems

Page 3: Concurrency Constructs Overview

Mainstream is 10 years in the past

Page 4: Concurrency Constructs Overview

Something happened in

2002

Page 5: Concurrency Constructs Overview

Fraction of Chip reachable in one clock cycle

Page 6: Concurrency Constructs Overview

Converting to Figure

[Source:] A Wire-Delay Scalable Microprocessor Architecture for High Performance Systems

Page 7: Concurrency Constructs Overview

Processor Clock Speed

Page 8: Concurrency Constructs Overview

Growing CPU performance

Page 9: Concurrency Constructs Overview

Programming style impact on performance

Sequences

Multithreading

Page 10: Concurrency Constructs Overview

Mobile (up to 4)

Page 11: Concurrency Constructs Overview

1 Mainstream (4)

Page 12: Concurrency Constructs Overview

Office (12)

Page 13: Concurrency Constructs Overview

SUN Unit (up to 128)

Page 14: Concurrency Constructs Overview

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)

Page 15: Concurrency Constructs Overview

Java is so Java

Java Memory Modelwait - notifyThread.yield()Thread.sleep()

synchronized

Executor Framework -> NIO (2)

Page 16: Concurrency Constructs Overview

Books

Page 17: Concurrency Constructs Overview

Books

Page 18: Concurrency Constructs Overview

We DO

• Threads – Program counter – Own stack – Shared Memory

• Locks

Page 19: Concurrency Constructs Overview

Shared memory

Page 20: Concurrency Constructs Overview

Unpredictable state

Page 21: Concurrency Constructs Overview

Crash in critical region

Page 22: Concurrency Constructs Overview

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

Page 23: Concurrency Constructs Overview

Design Goals/Space

Mutual ExclusionSerialization / OrderingInherent / Implicit vs Explicit Fine / Medium / Coarse grained Composability

Page 24: Concurrency Constructs Overview

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

Page 25: Concurrency Constructs Overview

Theory

Functional Programming ActorsCSPCCSpetri-netspi-calculusjoin-calculus

Page 26: Concurrency Constructs Overview

The models

• Transactional Memory – Persistent data structures

• Actors• Dataflow • Tuple spaces

Page 27: Concurrency Constructs Overview

Transactional memory

analogous to database transactionsHardware vs. software implementationsIdea goes as far back as 1986 First appearance in a programming language: Concurrent Haskell 2005

Page 28: Concurrency Constructs Overview

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)))))))

Page 29: Concurrency Constructs Overview

STM Design Space

STM Algorithms / StrategiesGranularity

word vs blockLocks vs Optimistic concurrency Conflict detection

eager vs lazyContention management

Page 30: Concurrency Constructs Overview

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

Page 31: Concurrency Constructs Overview

Implementations

Clojure STM - via RefsAkka/Scala

copy from ClojureHaskell/GHC

Use logs and aborts txns

Page 32: Concurrency Constructs Overview

Persistent Data Structures

In Clojure, combined with STM Motivated by copy on write hash-map, vector, sorted map

Page 33: Concurrency Constructs Overview

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

Page 34: Concurrency Constructs Overview

Problem

• Not really a full model• Oriented towards functional programming

Page 35: Concurrency Constructs Overview

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

Page 36: Concurrency Constructs Overview

Simple

Page 37: Concurrency Constructs Overview

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 =>

} }

}

Page 38: Concurrency Constructs Overview

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

Page 39: Concurrency Constructs Overview

Impelmentations

ScalaAkka ActorsScala ActorsLift Actors

ErlangOTP

CLR F# / Axum

Page 40: Concurrency Constructs Overview

Perfomance

Actor create/destroyMessage passingMemory usage

Page 41: Concurrency Constructs Overview

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)

Page 42: Concurrency Constructs Overview

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

Page 43: Concurrency Constructs Overview

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

Page 44: Concurrency Constructs Overview

The Model

Dataflow Variables create variablebind valueread value or block

ThreadsDataflow Streams

List whose tail is an unbound dataflow variableDeterministic computation!

Page 45: Concurrency Constructs Overview

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}

Page 46: Concurrency Constructs Overview

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

Page 47: Concurrency Constructs Overview

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/

Page 48: Concurrency Constructs Overview

Problems

Can’t handle non-determinismlike a serverNeed ports

this leads to actor like things

Page 49: Concurrency Constructs Overview

Tuple Spaces Model

Originated in Linda (1984) Popularized by Jini

Page 50: Concurrency Constructs Overview

The Model

Three operations write() (out)take() (in)read()

Page 51: Concurrency Constructs Overview

The Model

Space uncouplingTime uncouplingReaders are decoupled from WritersContent addressable by pattern matchingCan emulate

Actor like continuationsCSPMessage PassingSemaphores

Page 52: Concurrency Constructs Overview

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);

Page 53: Concurrency Constructs Overview

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

Page 54: Concurrency Constructs Overview

Problems

Low levelHigh latency to the space - the space is contention point / hot spotScalabilityMore for distribution than concurrency

Page 55: Concurrency Constructs Overview

Projects

ScalaErlangClojureKamaeliaHaskellAxum/F#Mozart/OzAkka

Page 56: Concurrency Constructs Overview

Work to be done

More in depth comparisons on 4+ core platforms Higher level frameworksApplication architectures/patterns

WebMiddleware

Page 57: Concurrency Constructs Overview

Outcomes

F..ck the shared state,Mutable means non-scalable

It is not too early!

Page 58: Concurrency Constructs Overview

QA