Lecture 9. Part I. Overview of Message Passing. Communication Coupling. Decoupling Blackboard. Decoupling Broadcast. Linda and Erlang.

Similar documents
Lecture 8. Linda and Erlang

One process sends a message. We will consider two. dimensions of this. message passing. PPHT07 Linda

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Message Passing. Lecture 7. Shared Variables? Shared Variables? Synchronisation. Overview of Message Passing

Lecture 6. Introduction to Message Passing

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

concurrent programming XXL

Programming Paradigms

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Distributed Systems/Middleware JavaSpaces

Programming Distributed Systems

Inter-process communication (IPC)

Erlang. Joe Armstrong.

Programming Paradigms

Middleware-Konzepte. Tuple Spaces. Dr. Gero Mühl

Erlang 101. Google Doc

Programming Paradigms Written Exam (6 CPs)

Distributed Middleware. Distributed Objects

COSC441. Lecture 8 Introduction to Erlang

Time and Space. Indirect communication. Time and space uncoupling. indirect communication

MapReduce in Erlang. Tom Van Cutsem

Indirect Communication

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

Erlang. Joe Armstrong

Electronic Payment Systems (1) E-cash

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

An Introduction to Erlang

Chapter 1. Fundamentals of Higher Order Programming

Linda, JavaSpaces & Jini

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

Reminder from last time

Indirect Communication

An Introduction to Erlang

Erlang functional programming in a concurrent world

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov

An Introduction to Erlang

Message-passing concurrency in Erlang

Principles of Programming Languages (E)

An Introduction to Erlang

Producer Consumer in Ada

Erlang: distributed programming

Abstract Memory The TupleSpace

Software Architecture

All you need is fun. Cons T Åhs Keeper of The Code

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Models and languages of concurrent computation

LINDA. The eval operation resembles out, except that it creates an active tuple. For example, if fcn is a function, then

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

An introduction introduction to functional functional programming programming using usin Haskell

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Pace University. Fundamental Concepts of CS121 1

Today: Distributed Objects. Distributed Objects

Indirect Communication

Functional programming in Erlang

Using JavaSpaces to create adaptive distributed systems

Two Phase Commit Protocol. Distributed Systems. Remote Procedure Calls (RPC) Network & Distributed Operating Systems. Network OS.

Distributed Programming in Java

Parallel And Distributed Compilers

IV. Process Synchronisation

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Chapter 2 Architectures. Software Architectures

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

SFDV3006 Concurrent Programming

Programming Paradigms Written Exam (6 CPs)

CmpSci 187: Programming with Data Structures Spring 2015

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Concurre Concurr nt n Systems 8L for Part IB Handout 3 Dr. Steven Hand 1

Statements 2. a operator= b a = a operator b

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Outline. Interprocess Communication. Interprocess Communication. Communication Models: Message Passing and shared Memory.

Software Engineering using Formal Methods

CA341 - Comparative Programming Languages

LECTURE 5 Control Structures Part 2

Concurrent Programming (RIO) Lesson 9. Ch 8 [BenA 06] Messages Channels Rendezvous RPC and RMI. X=f(..); send X to B. OS kernel.

LFE - a lisp on the Erlang VM

CSC407: Software Architecture Winter 2007 Middleware

Starting the System & Basic Erlang Exercises

Antonella Di Stefano, Corrado Santoro. (presenting)

Organization of Programming Languages CS3200/5200N. Lecture 11

Formal Specification and Verification

Concurrent Programming TDA381/DIT390

COP4020 Programming Assignment 1 - Spring 2011

An Introduction to Erlang

Functional Programming and Haskell

Advanced OpenMP. Lecture 11: OpenMP 4.0

Software Engineering using Formal Methods

LECTURE 16. Functional Programming

416 Distributed Systems. RPC Day 2 Jan 12, 2018

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages

Distributed Systems. Messaging and JMS Distributed Systems 1. Master of Information System Management

Reminder from last ;me

Typed Racket: Racket with Static Types

Topics in Object-Oriented Design Patterns

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Erlang - functional programming in a concurrent world. Johan Montelius KTH

Transcription:

Lecture 9 Part I Linda and Erlang Linda Overview of Message Passing Communication Coupling One process sends a message Another process awaits for a message We will consider two dimensions of this approach: What form of synchronisation is required What form of process naming is involved in message passing Time Synchronisation in message passing Processes must usually exist simultaneously to communicate Synchronous/rendezvous y / Asynchronous Space Naming Global scope for the names Processes, or Channels/operations 3 4 Decoupling Broadcast Decoupling Blackboard Communication by broadcast is decoupled In space Sender does not know who the receivers are, or How many receivers there are, or Whether there are any receivers at all But not in time Receivers must exist and act simultaneously with the sender Communication by blackboard is decoupled In space Writer does not know who the readers are, or How many readers there are, or Whether there are any readers at all In time Reader can even be created after the writer dies 5 6 1

The Linda Model The Linda Model Origins 1983: David Gelernter General idea Parallel Programming = Computation + Coordination Based on the blackboard communication Asynchronous communication No naming global blackboard Content matching Linda Not a programming language, but A collection of operations Take your favorite (sequential) host language, Add Linda operations, and Library Extend the language itself You get a parallel/concurrent programming language 7 8 Blackboard Tuple Space Patterns Linda shared memory Blackboard Tuple space Data in the space: tuples Tuples Typed sequences of data Singletons, Pairs, Triples, Quadruples, Examples: (1,true), (1,2), (false,2) differently typed ("list",1,42) Patterns are used to find tuples Tuples with variables Examples ("list",?index,?value) (1,?X) Pattern matching examples (1,?X) matches (1, true) to give X = true (1,?X) fails to match (2, true) (1,?X) fails to match (1, true, 3) 9 10 Communication Blocking Communication Blocking OUT(exp1,,expn) Evaluate the expressions exp1,,expn and produce a tuple T, Atomically add the tuple T to the tuple space. IN(P) Block until a tuple T in the tuple space matches the pattern P, Atomically remove T from the tuple space, Assign the variables in P to values in T according to the match. RD(P) Block until a tuple T in the tuple space matches the pattern P, Assign the variables in P to values in T according to the match. Like IN(P) except that the matched tuple T is left in the tuple space 11 12 2

Communication Non-blocking Two non-blocking input/read variants INP(P) and RDP(P) Predicates that return: true and assign variables in P if there is a matching tuple false otherwise Communication Non-blocking EVAL(exp1,,expn) Concurrently evaluate the expressions exp1,,expn and produce a tuple T, Atomically add the tuple T to the tuple space. The caller does not wait private op void eval() { OUT("value", f(i)); for(int i=0;i<n;i++) send eval(); for(int i=0;i<n;i++) EVAL("value", f(i)); 13 14 Distributed Data Structures Example: List/Vector To promote concurrency it is natural to use a distributed representation of data structures The tuple space is logically shared, but May be physically distributed. What do we mean by distributed representations of data-structures? Create a three element list/vector Add? OUT("list",0,value0); OUT("list",1,value1); OUT("list",2,value2); OUT("list","tail",3 ); void add(e element) { int Index; IN ("list","tail",?index); OUT("list","tail",Index+1); OUT("list",Index,element); 15 16 Example: Unbounded Buffer Example: Unbounded Buffer Create an empty buffer called buff Buffer operation get OUT("head", "buff", 0); OUT("tail", "buff", 0); Buffer operation put void E get(string bufname) { int Index; E Element; void put(string bufname, E element) { int Index; IN ("tail", bufname,?index); OUT("tail", bufname, Index+1); OUT("element", bufname, Index, element); IN ("head", bufname,?index); OUT("head", bufname, Index+1); IN ("element", bufname, Index,?Element); return Element; 17 18 3

Expressive Power Semaphores Semaphores, monitors, message passing Equally expressive Any synchronisation with await statement Linda vs. XXX Can we implement XXX? Can XXX implement Linda? Important theoretical question An illustrative example, but not normal practice Semaphore sem s = N P(s); V(s); Linda for(int x=0;x<n;x++) OUT("s"); IN("s"); OUT("s"); 19 20 Expressive Power Client-Server Interaction Semaphores as Linda singletons Assignment 3 is for you to implement Linda tuple space using asynchronous message passing in Erlang Common asynchronous communication pattern For example: a web server handles requests for web pages from s (web browsers) request The same expressive power Can implement any await statement Important theoretical result result server 21 22 Linda s Client-Server Replicated Worker Pattern Communication by message content server A typical programming style used in the Linda model: Break a problem into a collection of tasks Start a team of (identical) Worker processes request result Each Worker takes tasks and solves them A Master process coordinates the activities Tuple space 23 24 4

Example: NxN Matrix Multiplication Master process Initialises the tuple space with rows of matrix A and columns of B, Adds a next job'' counter, and Collects the results. Worker processes Grab a job number from 0 to N N-1 Get corresponding row + column to multiply Place the resulting number in the tuple space Master Multiplier process master { final int N = 3; OUT("A", 0, {1,2,3);OUT("B", 0, {1,0,1); OUT("A", 1, {4,5,6);OUT("B", 1, {0,1,0); OUT("A", 2, {7,8,9);OUT("B", 2, {2,2,0); OUT("Next", 0); for(int i=0; i<n; i++) for(int j=0; j<n; j++) IN("C", i, j,?c); //Do something with C(i,j) 25 26 Worker Multiplier JavaSpaces process worker((int w=0; w<nworkers; w++)) { int Element; int[] Vector1, Vector2; IN("Next",?Element); OUT("Next", Element+1); while(element < N*N) { int i = Element / N; int j = Element % N; RD("A", i,?vector1); RD("B", j,?vector2); int x = innerproduct(vector1, Vector2); out("c", i, j, x); IN("Next",?Element); OUT("Next", Element+1); 27 28 JavaSpaces Tuples = Entries Why tuple spaces in a Java context? Simplified approach to distributed computation Alternative middleware to the standard stuff Remote Method Invocation (RMI) Enterprise Java Beans (EJB) Object Request Broker Architecture (CORBA) The Tuple is replaced by the concept of an Entry in JavaSpaces An entry is an object which Implements net.jini.space.entry Has objects as fields Pattern matching uses templates A template is a entry Fields set to specified values, or Wildcards (?X): fields set to null 29 30 5

Operations on a JavaSpace Transactions Standard operations write: like OUT read/readifexists: like RD/RDP take/takeifexists: like IN/INP New operations notify: requests a notification that an object matching a template is added to the space It will cause the requester s notify method to be called when a matching tuple appears Transactions A standard database concept Collections of operations execute atomically, Or not at all Example: adding to a list Either complete the whole add or roll-back void add(e element) { int Index; IN ("list","tail",?index); OUT("list","tail",Index+1); OUT("list",Index,element); 31 32 Conclusions Linda Conclusions Linda Tuple spaces provide a very simple mechanism for parallel and distributed computation Loose coupling Negatives? Less structured communication Data and structure intermixed Implementation efficiency Flexibility Extendibility Modern instances (examples) JavaSpaces (Sun), and for example Blitz open-source project TSpaces (IBM) Distributed ib space Distributed pattern matching 33 34 Erlang Language Part II Functional Concurrent Distributed Erlang Soft real-time OTP (fault-tolerance, hot code update ) Open 36 6

Erlang Typical Applications History Lesson Telecoms Switches (POTS, ATM, IP, ) GPRS/3G/4G-LTE SMS applications Banks Credit card clearing 3D modelling (Wings3D) 1981 Ericsson CSLab formed 1986 Prolog games 1987 Erlang name appears 1989 Prototypes show 9 22 fold increase in design efficiency 1995 AXE-N fails after 8 years 1998 AXD301 delivered 1998 Erlang banned; goes open source 37 38 Essence of Erlang Data types Constant A simple functional language Direct asynchronous message passing so-called actor model Open Telecom Platform libraries Practically proven programming patterns Utilities Numbers Integers (arbitrarily big) Floats Atoms start_with_a_lower_case_letter Anything_inside_quotes\n\09 39 40 Data types Compound Data types Compound Tuples { {atom, another_atom, PPxT {atom, {tup, 2, {{tup, element Lists [] [65,66,67,68] = ABCD [1, true] and [a b] Allowed but not a good programming practice! Records -record(person, {name =, phone = [], address). X = #person{name = Joe, phone = [1,1,2], address= here X#person.name X#person{phone = [0,3,1,1,2,3,4] 41 42 7

Modelling Haskell data Variables data Tree a = Leaf a Node (Tree a) (Tree a) edoc style types : tree(a) = {leaf, A {node, tree(a), tree(a) Identifires A_long_variable_name Must start with an Upper Case Letter Can store values Can be bound only once! Bound variables cannot change values 43 44 Functions and Modules Evaluation Basic compilation unit is a module Module name = file name (.erl) Modules contain function definitions Some functions can be exported usable from outside of the module -module(math_stuff). -export([factorial/1]). factorial(0) -> 1; factorial(n) -> N * factorial(n-1). factorial(3) matches N = 3 in clause 2 == 3 * factorial(3-1) == 3 * factorial(2) matches N =2 in clause 2 == 3 * 2 * factorial(2-1) == 3 * 2 * factorial(1) matches N = 1 in clause 2 == 3 * 2 * 1 * factorial(1-1) == 3 * 2 * 1 * factorial(0) == 3 * 2 * 1 * 1 (clause 1) == 6 45 46 Pattern Matching More Pattern Matching area({square,side) -> Side*Side. a pattern {square, Side matches {square, 4 and binds 4 to variable Side {B, C, D = {10, foo, bar Succeeds - binds B to 10, C to foo and D to bar {A, A, B = {abc, abc, foo Succeeds - binds A to abc, B to foo {A, A, B = {abc, def, 123 Fails [A,B,C,D] = [1,2,3] Fails 47 48 8

Even More Pattern Matching List Examples [H T]= [1,2,3,4] Succeeds - binds H to 1, T to [2,3,4] [H T] = [abc] Succeeds - binds H to abc, T to [] [H T] = [] Fails {A, _, [B _],{B = {abc,23,[22,x],{22 Succeeds - binds A = abc, B = 22 -module(list_stuff). -export([average/1, average/2]). average(x) -> sum(x) / len(x). average(a, age( B) -> (A+B)/2. sum([h T]) -> H + sum(t); sum([]) -> 0. len([h T]) -> 1 + len(t); len([]) -> 0. 49 50 Loops Tail Recursion Concurrent Programming Inefficient recursive definition len([h T]) -> 1 + len(t); len([]) -> 0. Based on Message Passing: Q. What form of synchronisation? A. Asynchronous Fast tail recursive version = while loop len(list) -> len_a(list, 0). Q. What form of process naming? A. Direct, asymmetric len_a([_ T], Acc) -> len_a(t, Acc+1); len_a([], Acc) -> Acc. A send MSG to B B receive anything 51 52 Processes Process = a function evaluated in a separate thread -module(process). -export([start/0]). start() -> Pid = spawn(fun run/0), io:format("spawned ~p~n",[pid]). Built-in functions BIFs spawn, spawn_link, atom_to_list Don t need importing Mostly in module called erlang Some have guaranteed termination These can be used in guards run() -> io:format("hello!~n",[]). 53 54 9

Process Creation Message Passing -module(echo). -export([start/0]). start() -> Pid = spawn(fun() -> loop(42) end), Pid! {self(), hello, io:format("sent hello.~n",[]). %%continues %%continuation loop(number) -> receive {From, Msg -> From! {self(), Number, io:format( "Received ~p, N: ~p~n", [Msg, Number]), loop(number+1); stop -> true end. 55 56 Message Passing Message Passing S Pid = spawn(fun() -> loop(42) end) S {S, hello Pid io:format( ).! {self(), hello receive {From, Msg stop end. L 57 58 Message Passing Conclusions Erlang Functional Concurrent Basics {< >, 42 S L S! {self(), loop(42+1); 42, loop(42+1); Next time More Erlang concurrency 59 60 10