Concurrency control abstractions (PDCS 9, CPE 5*)

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
COMMUNICATING SEQUENTIAL PROCESSES C. A. R. Hoare The Queen’s University Belfast, North Ireland.
Models of Concurrency Manna, Pnueli.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
C. Varela1 Actors (PDCS 4) AMST actor language syntax, semantics, join continuations Carlos Varela Rennselaer Polytechnic Institute April 16, 2015.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
Erlang concurrency. Where were we? Finished talking about sequential Erlang Left with two questions  retry – not an issue; I mis-read the statement in.
Concurrency CS 510: Programming Languages David Walker.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
ITERATIVE COMPUTATIONS CONCURRENCY ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Scala Actors -Terrance Dsilva.  Thankfully, Scala offers a reasonable, flexible approach to concurrency  Actors aren’t a concept unique to Scala.
Comparative Programming Languages hussein suleman uct csc304s 2003.
9/10/2015CS2104, Lecture 81 Programming Language Concepts, COSC Lecture 8 (Multiple) Threads Semantics.
SALSA: Language and Architecture for Widely Distributed Actor Systems. Carlos Varela, Abe Stephens, Department of.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
- 1 - Embedded Systems - SDL Some general properties of languages 1. Synchronous vs. asynchronous languages Description of several processes in many languages.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Coordination Models and Languages Part I: Coordination Languages and Linda Part II: Technologies based on Tuple space concept beyond Linda Part III: Comparison.
The Complexity of Distributed Algorithms. Common measures Space complexity How much space is needed per process to run an algorithm? (measured in terms.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Applications Development
CSCI-383 Object-Oriented Programming & Design Lecture 12.
C. Varela1 Actors in SALSA and Erlang (PDCS 9, CPE 5*) Support for actor model in SALSA and Erlang Carlos Varela Rennselaer Polytechnic Institute October.
C. Varela1 Chapter 4: Actors Programming Distributed Computing Systems: A Foundational Approach Carlos Varela Rensselaer Polytechnic Institute.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
Hwajung Lee. Well, you need to capture the notions of atomicity, non-determinism, fairness etc. These concepts are not built into languages like JAVA,
Hwajung Lee. Why do we need these? Don’t we already know a lot about programming? Well, you need to capture the notions of atomicity, non-determinism,
C. Varela1 Distributed systems abstractions (PDCS 9, CPE 6*) Carlos Varela Rennselaer Polytechnic Institute October 20, 2015 * Concurrent Programming in.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
CS314 – Section 5 Recitation 9
Chapter 2: Lambda Calculus
Core Java Statements in Java.
Carlos Varela Rennselaer Polytechnic Institute November 1, 2016
Model and complexity Many measures Space complexity Time complexity
CS 326 Programming Languages, Concepts and Implementation
Distributed systems abstractions (PDCS 9, CPE 6*)
Python: Control Structures
Computer Engg, IIT(BHU)
Rennselaer Polytechnic Institute
Carlos Varela Rennselaer Polytechnic Institute November 3, 2017
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Chapter 4 Control Statements: Part I
Carlos Varela Rennselaer Polytechnic Institute October 6, 2017
Chapter 4: Control Structures I (Selection)
Object Oriented Programming
Threading And Parallel Programming Constructs
Multithreaded Programming
Carlos Varela Rennselaer Polytechnic Institute October 4, 2016
Semaphores Chapter 6.
Coordination Models and Languages
Declarative Computation Model Single assignment store (VRH 2
Computer Science 312 Concurrent Programming I Processes and Messages 1.
In this class, we will cover:
Assignments and Procs w/Params
Concurrent Programming Actors, SALSA, Coordination Abstractions
CS561 Computer Architecture Hye Yeon Kim
Problem 1 Given n, calculate 2n
Presentation transcript:

Concurrency control abstractions (PDCS 9, CPE 5*) Carlos Varela Rennselaer Polytechnic Institute October 14, 2016 * Concurrent Programming in Erlang, by J. Armstrong, R. Virding, C. Wikström, M. Williams C. Varela

Operational Semantics of Actors C. Varela

AMST Semantics Example k0 = [send(new(b5),a)]a || {} k6 = [nil]a, [ready(b5)]b || {< a <= 5 >} [new:a,b] [snd:a] [rcv:b,a] [fun:b] k0 k1 k2 k3 k4 [snd:a,5] [fun:b] k4 k5 k6 This sequence of (labeled) transitions from k0 to k6 is called a computation sequence. C. Varela

Nondeterministic Behavior k0 = [ready(cell(0))]a || {<a<=s(7)>, <a<=s(2)>, <a<=g(c)>} Three receive transitions are enabled at k0. [rcv:a,s(7)] k0 k1 [rcv:a,s(2)] k0 k1’ [rcv:a,g(c)] k0 k1” Multiple enabled transitions can lead to nondeterministic behavior The set of all computations sequences from k0 is called the computation tree τ(k0). C. Varela

Actors/SALSA Actor Model SALSA A reasoning framework to model concurrent computations Programming abstractions for distributed open systems G. Agha, Actors: A Model of Concurrent Computation in Distributed Systems. MIT Press, 1986. Agha, Mason, Smith and Talcott, “A Foundation for Actor Computation”, J. of Functional Programming, 7, 1-72, 1997. SALSA Simple Actor Language System and Architecture An actor-oriented language for mobile and internet computing Programming abstractions for internet-based concurrency, distribution, mobility, and coordination C. Varela and G. Agha, “Programming dynamically reconfigurable open systems with SALSA”, ACM SIGPLAN Notices, OOPSLA 2001, 36(12), pp 20-34. C. Varela

Reference Cell Example module cell; behavior Cell { Object content; Cell(Object initialContent) { content = initialContent; } Object get() { return content; } void set(Object newContent) { content = newContent; Encapsulated state content. Actor constructor. Message handlers. State change. C. Varela

Message passing (<-) Cell Tester Example module cell; behavior CellTester { void act( String[] args ) { Cell c = new Cell(0); c <- set(2); c <- set(7); token t = c <- get(); standardOutput <- println( t ); } Actor creation (new) Message passing (<-) println message can only be processed when token t from c’s get() message handler has been produced. C. Varela

Reference Cell in Erlang -module(cell). -export([cell/1]). cell(Content) -> receive {set, NewContent} -> cell(NewContent); {get, Customer} -> Customer ! Content, cell(Content) end. Encapsulated state Content. State change. Message handlers Explicit control loop: Actions at the end of a message need to include tail-recursive function call. Otherwise actor (process) terminates. C. Varela

Actor creation (spawn) receive waits until a message is available. Cell Tester in Erlang -module(cellTester). -export([main/0]). main() -> C = spawn(cell,cell,[0]), C!{set,2}, C!{set,7}, C!{get,self()}, receive Value -> io:format("~w~n”,[Value]) end. Actor creation (spawn) Message passing (!) receive waits until a message is available. C. Varela

Tree Product Behavior in AMST Btreeprod = rec(λb.λm. seq(if(isnat(tree(m)), send(cust(m),tree(m)), let newcust=new(Bjoincont(cust(m))), lp = new(Btreeprod), rp = new(Btreeprod) in seq(send(lp, pr(left(tree(m)),newcust)), send(rp, pr(right(tree(m)),newcust)))), ready(b))) C. Varela

Join Continuation in AMST Bjoincont = λcust.λfirstnum.ready(λnum. seq(send(cust,firstnum*num), ready(sink))) C. Varela

Sample Execution f(tree,cust) f(left(tree),JC) f(right(tree),JC) JC (b) C. Varela

Sample Execution f(left(tree),JC) JC’ JC firstnum cust cust cust (d) C. Varela

Sample Execution num Cust firstnum JC firstnum * num Cust Cust (e) (f) C. Varela

Tree Product Behavior in SALSA module jctreeprod; import tree.Tree; behavior TreeProduct { void compute(Tree t, UniversalActor c){ if (t.isLeaf()) c <- result(t.value()); else { JoinCont newCust = new JoinCont(c); TreeProduct lp = new TreeProduct(); TreeProduct rp = new TreeProduct(); lp <- compute(t.left(), newCust); rp <- compute(t.right(), newCust); } C. Varela

Join Continuation in SALSA module jctreeprod; behavior JoinCont { UniversalActor cust; int first; boolean receivedFirst; JoinCont(UniversalActor cust){ this.cust = cust; this.receivedFirst = false; } void result(int v) { if (!receivedFirst){ first = v; receivedFirst = true; else // receiving second value cust <- result(first*v); C. Varela

Tree Product Behavior in Erlang -module(treeprod). -export([treeprod/0,join/1]). treeprod() -> receive {{Left, Right}, Customer} -> NewCust = spawn(treeprod,join,[Customer]), LP = spawn(treeprod,treeprod,[]), RP = spawn(treeprod,treeprod,[]), LP!{Left,NewCust}, RP!{Right,NewCust}; {Number, Customer} -> Customer ! Number end, treeprod(). join(Customer) -> receive V1 -> receive V2 -> Customer ! V1*V2 end end. C. Varela

Tree Product Sample Execution 2> TP = spawn(treeprod,treeprod,[]). <0.40.0> 3> TP ! {{{{5,6},2},{3,4}},self()}. {{{{5,6},2},{3,4}},<0.33.0>} 4> flush(). Shell got 720 ok 5> C. Varela

Actor Languages Summary Actors are concurrent entities that react to messages. State is completely encapsulated. There is no shared memory! Message passing is asynchronous. Actors can create new actors. Run-time has to ensure fairness. AMST extends the call by value lambda calculus with actor primitives. State is modeled as function arguments. Actors use ready to receive new messages. SALSA extends an object-oriented programming language (Java) with universal actors. State is explicit, encapsulated in instance variables. Control loop is implicit: ending a message handler, signals readiness to receive a new message. Actors are garbage-collected. Erlang extends a functional programming language core with processes that run arbitrary functions. State is implicit in the function’s arguments. Control loop is explicit: actors use receive to get a message, and tail-form recursive call to continue. Ending a function denotes process (actor) termination. C. Varela

Causal order In a sequential program all execution states are totally ordered In a concurrent program all execution states of a given actor are totally ordered The execution state of the concurrent program as a whole is partially ordered C. Varela

Total order In a sequential program all execution states are totally ordered sequential execution computation step C. Varela

Causal order in the actor model In a concurrent program all execution states of a given actor are totally ordered The execution state of the concurrent program is partially ordered actor A3 actor A2 Create new actor Send a message actor A1 computation step C. Varela

Nondeterminism An execution is nondeterministic if there is a computation step in which there is a choice what to do next Nondeterminism appears naturally when there is asynchronous message passing Messages can arrive or be processed in an order different from the sending order. C. Varela

Example of nondeterminism Actor 1 Actor 2 Actor a m1 m2 time time time Actor a can receive messages m1 and m2 in any order. C. Varela

Concurrency Control in SALSA SALSA provides three main coordination constructs: Token-passing continuations To synchronize concurrent activities To notify completion of message processing Named tokens enable arbitrary synchronization (data-flow) Join blocks Used for barrier synchronization for multiple concurrent activities To obtain results from otherwise independent concurrent processes First-class continuations To delegate producing a result to a third-party actor C. Varela

Token Passing Continuations Ensures that each message in the continuation expression is sent after the previous message has been processed. It also enables the use of a message handler return value as an argument for a later message (through the token keyword). Example: a1 <- m1() @ a2 <- m2( token ); Send m1 to a1 asking a1 to forward the result of processing m1 to a2 (as the argument of message m2). C. Varela

Token Passing Continuations @ syntax using token as an argument is syntactic sugar. Example 1: a1 <- m1() @ a2 <- m2( token ); is syntactic sugar for: token t = a1 <- m1(); a2 <- m2( t ); Example 2: a2 <- m2(); a2 <- m2():waitfor( t ); C. Varela

Named Tokens Tokens can be named to enable more loosely-coupled synchronization Example: token t1 = a1 <- m1(); token t2 = a2 <- m2(); token t3 = a3 <- m3( t1 ); token t4 = a4 <- m4( t2 ); a <- m(t1,t2,t3,t4); Sending m(…) to a will be delayed until messages m1()..m4() have been processed. m1() can proceed concurrently with m2(). C. Varela

Causal order in the actor model receive a message with a token bind a token actor A3 x actor A2 create new actor y actor A1 computation step C. Varela

Deterministic Cell Tester Example module cell; behavior TokenCellTester { void act( String[] args ) { Cell c = new Cell(0); standardOutput <- print( ”Initial Value:” ) @ c <- get() @ standardOutput <- println( token ) @ c <- set(2) @ standardOutput <- print( ”New Value:” ) @ standardOutput <- println( token ); } @ syntax enforces a sequential order of message execution. token can be optionally used to get the return value (completion proof) of the previous message. C. Varela

Cell Tester Example with Named Tokens module cell; behavior NamedTokenCellTester { void act(String args[]){ Cell c = new Cell(0); token p0 = standardOutput <- print("Initial Value:"); token t0 = c <- get(); token p1 = standardOutput <- println(t0):waitfor(p0); token t1 = c <- set(2):waitfor(t0); token p2 = standardOutput <- print("New Value:"):waitfor(p1); token t2 = c <- get():waitfor(t1); standardOutput <- println(t2):waitfor(p2); } We use p0, p1, p2 tokens to ensure printing in order. We use t0, t1, t2 tokens to ensure cell messages are processed in order. C. Varela

Join Blocks Provide a mechanism for synchronizing the processing of a set of messages. Set of results is sent along as a token containing an array of results. Example: UniversalActor[] actors = { searcher0, searcher1, searcher2, searcher3 }; join { for (int i=0; i < actors.length; i++){ actors[i] <- find( phrase ); } } @ resultActor <- output( token ); Send the find( phrase ) message to each actor in actors[] then after all have completed send the result to resultActor as the argument of an output( … ) message. C. Varela

Example: Acknowledged Multicast join{ a1 <- m1(); a2 <- m2(); … an <- mn(); } @ cust <- n(token); C. Varela

Lines of Code Comparison 31 100 168 Acknowledged Multicast SALSA Foundry Java C. Varela

First Class Continuations Enable actors to delegate computation to a third party independently of the processing context. For example: int m(…){ b <- n(…) @ currentContinuation; } Ask (delegate) actor b to respond to this message m on behalf of current actor (self) by processing b’s message n. C. Varela

is syntactic sugar for: Delegate Example fib(15) is syntactic sugar for: self <- fib(15) module fibonacci; behavior Calculator { int fib(int n) { Fibonacci f = new Fibonacci(n); f <- compute() @ currentContinuation; } int add(int n1, int n2) {return n1+n2;} void act(String args[]) { fib(15) @ standardOutput <- println(token); fib(5) @ add(token,3) @ standardOutput <- println(token); C. Varela

Fibonacci Example C. Varela module fibonacci; behavior Fibonacci { int n; Fibonacci(int n) { this.n = n; } int add(int x, int y) { return x + y; } int compute() { if (n == 0) return 0; else if (n <= 2) return 1; else { Fibonacci fib1 = new Fibonacci(n-1); Fibonacci fib2 = new Fibonacci(n-2); token x = fib1<-compute(); token y = fib2<-compute(); add(x,y) @ currentContinuation; } void act(String args[]) { n = Integer.parseInt(args[0]); compute() @ standardOutput<-println(token); C. Varela

compute(n-2) is a message to self. Fibonacci Example 2 module fibonacci2; behavior Fibonacci { int add(int x, int y) { return x + y; } int compute(int n) { if (n == 0) return 0; else if (n <= 2) return 1; else { Fibonacci fib = new Fibonacci(); token x = fib <- compute(n-1); compute(n-2) @ add(x,token) @ currentContinuation; } void act(String args[]) { int n = Integer.parseInt(args[0]); compute(n) @ standardOutput<-println(token); compute(n-2) is a message to self. C. Varela

Execution of salsa Fibonacci 6 Create new actor F1 F3 F4 F2 Synchronize on result F2 F5 F3 F1 F2 Non-blocked actor F1 F3 F6 F4 F2 C. Varela

Tree Product Behavior Revisited Notice we use token-passing continuations (@,token), a join block (join), and a first-class continuation (currentContinuation). module treeprod; import tree.Tree; behavior JoinTreeProduct { int multiply(Object[] results){ return (Integer) results[0] * (Integer) results[1]; } int compute(Tree t){ if (t.isLeaf()) return t.value(); else { JoinTreeProduct lp = new JoinTreeProduct(); JoinTreeProduct rp = new JoinTreeProduct(); join { lp <- compute(t.left()); rp <- compute(t.right()); } @ multiply(token) @ currentContinuation; C. Varela

Concurrency control in Erlang Erlang uses a selective receive mechanism to help coordinate concurrent activities: Message patterns and guards To select the next message (from possibly many) to execute. To receive messages from a specific process (actor). To receive messages of a specific kind (pattern). Timeouts To enable default activities to fire in the absence of messages (following certain patterns). To create timers. Zero timeouts (after 0) To implement priority messages, to flush a mailbox. C. Varela

Selective Receive receive MessagePattern1 [when Guard1] -> Actions1 ; MessagePattern2 [when Guard2] -> Actions2 ; … end receive suspends until a message in the actor’s mailbox matches any of the patterns including optional guards. Patterns are tried in order. On a match, the message is removed from the mailbox and the corresponding pattern’s actions are executed. When a message does not match any of the patterns, it is left in the mailbox for future receive actions. C. Varela

Selective Receive Example Example program and mailbox (head at top): receive msg_b -> … end receive tries to match msg_a and fails. msg_b can be matched, so it is processed. Suppose execution continues: msg_c -> … msg_a -> … The next message to be processed is msg_a since it is the next in the mailbox and it matches the 2nd pattern. msg_a msg_b msg_c msg_a msg_c C. Varela

Receiving from a specific actor Actor ! {self(), message} self() is a Built-In-Function (BIF) that returns the current (executing) process id (actor name). Ids can be part of a message. receive {ActorName, Msg} when ActorName == A1 -> … end receive can then select only messages that come from a specific actor, in this example, A1. (Or other actors that know A1’s actor name.) C. Varela

Receiving a specific kind of message counter(Val) -> receive increment -> counter(Val+1); {From,value} -> From ! {self(), Val}, counter(Val); stop -> true; Other -> counter(Val) end. counter is a behavior that can receive increment messages, value request messages, and stop messages. Other message kinds are ignored. increment is an atom whereas Other is a variable (that matches anything!). C. Varela

Order of message patterns matters receive {{Left, Right}, Customer} -> NewCust = spawn(treeprod,join,[Customer]), LP = spawn(treeprod,treeprod,[]), RP = spawn(treeprod,treeprod,[]), LP!{Left,NewCust}, RP!{Right,NewCust}; {Number, Customer} -> Customer ! Number end In this example, a binary tree is represented as a tuple {Left, Right}, or as a Number, e.g., {{{5,6},2},{3,4}} {Left,Right} is a more specific pattern than Number is (which matches anything!). Order of patterns is important. C. Varela

Selective Receive with Timeout MessagePattern1 [when Guard1] -> Actions1 ; MessagePattern2 [when Guard2] -> Actions2 ; … after TimeOutExpr -> ActionsT end TimeOutExpr evaluates to an integer interpreted as milliseconds. If no message has been selected within this time, the timeout occurs and ActionsT are scheduled for evaluation. A timeout of infinity means to wait indefinitely. C. Varela

Timer Example sleep(Time) -> receive after Time -> true end. sleep(Time) suspends the current actor for Time milliseconds. C. Varela

Timeout Example receive click -> double_click after double_click_interval() -> single_click end ... double_click_interval evaluates to the number of milliseconds expected between two consecutive mouse clicks, for the receive to return a double_click. Otherwise, a single_click is returned. C. Varela

Zero Timeout receive MessagePattern1 [when Guard1] -> Actions1 ; MessagePattern2 [when Guard2] -> Actions2 ; … after 0 -> ActionsT end A timeout of 0 means that the timeout will occur immediately, but Erlang tries all messages currently in the mailbox first. C. Varela

Zero Timeout Example flush_buffer() -> receive AnyMessage -> flush_buffer() after 0 -> true end. flush_buffer() completely empties the mailbox of the current actor. C. Varela

Priority Messages priority_receive() -> receive interrupt -> interrupt after 0 -> AnyMessage -> AnyMessage end end. priority_receive() will return the first message in the actor’s mailbox, except if there is an interrupt message, in which case, interrupt will be given priority. C. Varela

Exercises Download and execute the reference cell and tree product examples in SALSA and Erlang. Write a solution to the Flavius Josephus problem in SALSA and Erlang. A description of the problem is at CTM Section 7.8.3 (page 558). PDCS Exercise 9.6.6 (page 204). How would you implement token-passing continuations, join blocks, and first-class continuations in Erlang? How would you implement selective receive in SALSA? C. Varela