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

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

Lecture 8. Linda and Erlang

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

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

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

Lecture 6. Introduction to Message Passing

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

concurrent programming XXL

Erlang. Joe Armstrong.

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

Inter-process communication (IPC)

Distributed Systems/Middleware JavaSpaces

Programming Distributed Systems

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms

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

Programming Paradigms

Indirect Communication

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

MapReduce in Erlang. Tom Van Cutsem

Erlang. Joe Armstrong

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

An Introduction to Erlang

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

Distributed Middleware. Distributed Objects

Indirect Communication

COSC441. Lecture 8 Introduction to Erlang

An Introduction to Erlang

Concurrent Programming TDA381/DIT390

Erlang 101. Google Doc

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

Erlang: distributed programming

Erlang functional programming in a concurrent world

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

Linda, JavaSpaces & Jini

An Introduction to Erlang

Reminder from last time

Programming Paradigms Written Exam (6 CPs)

An introduction introduction to functional functional programming programming using usin Haskell

Producer Consumer in Ada

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

Electronic Payment Systems (1) E-cash

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

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

CIS 120 Midterm II November 8, 2013 SOLUTIONS

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

Chapter 1. Fundamentals of Higher Order Programming

Message-passing concurrency in Erlang

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

Pace University. Fundamental Concepts of CS121 1

Models and languages of concurrent computation

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

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

Parallel And Distributed Compilers

IV. Process Synchronisation

Software Architecture

An Introduction to Erlang

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

SFDV3006 Concurrent Programming

Starting the System & Basic Erlang Exercises

CmpSci 187: Programming with Data Structures Spring 2015

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

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

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

Abstract Memory The TupleSpace

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

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

IMPLEMENTING A FUNCTIONAL LANGUAGE FOR HIGHLY PARALLEL REAL TIME APPLICATIONS

Reminder from last ;me

Today: Distributed Objects. Distributed Objects

Indirect Communication

LECTURE 5 Control Structures Part 2

Antonella Di Stefano, Corrado Santoro. (presenting)

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

Principles of Programming Languages (E)

Advanced OpenMP. Lecture 11: OpenMP 4.0

Using JavaSpaces to create adaptive distributed systems

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

CMSC 330: Organization of Programming Languages

ESC101N: Fundamentals of Computing End-sem st semester

Topics in Object-Oriented Design Patterns

PROGRAMMING IN C AND C++:

Programming Paradigms Written Exam

Organization of Programming Languages CS3200/5200N. Lecture 11

OpenMP 4.0/4.5. Mark Bull, EPCC

OpenMP 4.0. Mark Bull, EPCC

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

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

Parallel Programming with OpenMP. CS240A, T. Yang

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Lecture #23: Conversion and Type Inference

CSE 410 Final Exam 6/09/09. Suppose we have a memory and a direct-mapped cache with the following characteristics.

Functional programming in Erlang

LFE - a lisp on the Erlang VM

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

MPI and OpenMP (Lecture 25, cs262a) Ion Stoica, UC Berkeley November 19, 2016

Transcription:

Lecture 9 Linda and Erlang

Part I Linda

Overview of Mes sage Passing One process sends a message Another process awaits for a message We will consider two approach: dimensions of this What form of synchronisation is required What form of process s naming is involved in message passing 3

Communicatio n Coupling Time Synchronisation in message passing Space Processes must usually exist simultaneously to communicate Naming Synchronous/rendezvous Asynchronous Global scope for the Processes, or Channels/operations names 4

Decoupling Broadcast 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 the sender and act simultaneously with 5

Decoupling Blackboard Communication by blackboard is decoupled d In space Writer does not know who the readers are, or How many readers there are, or Whether there are an y readers at all In time Reader can even be created after the writer dies 6

The Linda Model Origins 1983: David Gelernter General idea Parallel Programmingg = Computation + Coordination Based on the blackboard communication Asynchronous communication No naming global blackboard Content matching 7

The Linda Model 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 8

Blackboard Tuple Space 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) 9

Patter rns 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) 10

Communicationn 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. 11

Communicationn Blocking 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 tthth hat the matched tuple T is left in the tuple space 12

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 13

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

Distributed Da ta Structures To promote concurrency it is natural to use a distributed ib t d epresentation ti of data structures The tuple space is logically shared, but May be physically y distributed. What do we mean by distributed representations of data-structures? 15

Example: Lis st/vector Create a three element list/vector OUT("list",0,value0); OUT("list",1,value1); OUT("list",2,value2); OUT("list","tail",3 ); Add? void add( (E element) { int Index; IN ("l list","tail",?index); "t il" OUT("list","tail",Index+1); OUT("l list",index,element); } 16

Example: Unbou unded Buffer Create an empty buffer called buff OUT("head", OUT("tail", "buff", 0); "buff", 0); Buffer operation put void put(string bufname, E element) { int Index; IN ("tail", bufname e,?index); OUT("tail", bufname, Index+1); OUT("element", bufname, Index, element); } 17

Example: Unbou unded Buffer Buffer operation get void E get(string bufname) { int Index; E Element; } IN ("head", bufname,?index); OUT("head", bufname, Index+1); IN ("element", bufname, Index,?Element); return Element; 18

Expressive e Power 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 19

Semaph hores Semaphore sem s = N Linda for(int x=0;x<n;x++) OUT("s"); P(s); IN( "s"); V(s); OUT("s"); 20

Expressive e Power Semaphores as Linda singletons Assignment 3 is for you to implement Linda tuple space using asynchronous message passing in Erlang The same expressive e power Can implement any await statement Important t theoretical ti result 21

Client-Server Interaction Common asynchronous communication pattern For example: a web server handles requests for web pages from clients (web browsers) client request client result ser ver client client 22

Linda s Clien nt-server Communication by message content server client requ uest result client Tuple space client client 23

Replicated Wo rker Pattern A typical programming style used in the Linda model: Break a problem into a collection of tasks Start a team of (identical) Worker processes Each Worker takes tasks and solves them A Master process coordinates the activities 24

Example: NxN Matr rix 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 25

Master Mu ultiplier 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) } 26

Worker Mu ultiplier 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,?vecto or1); RD("B", j,?vector2); int x = innerproduct(vector1, Vector2); out("c", i, j, x) ); IN("Next",?Element); OUT("Next", Element+1); }} 27

JavaSpacesaces 28

JavaSpa aces Why tuple spaces in a Java context? Simplified approach to distributed computation Alternative ti middlewa re to the standard d stuff Remote Method Invocation (RMI) Enterprise Java Bean ns (EJB) Object Request Broker Architecture (CORBA) 29

Tuples = Entries The Tuple is replaced by the concept of an Entry in JavaSpaces An entry is an object which Implements net.jini Has objects as fields i.space.entry Pattern matching uses templates A template is a entry Fields set to specified values, or Wildcards (?X): fieldssettonull set to 30

Operations on a JavaSpace 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 31

Transact tions 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",I ndex,element); } 32

Conclusionss Linda Tuple spaces provide a very simple mechanism for paral lel l and distributed ib t d computation Loose coupling Flexibility Extendibility Modern instances (examples) JavaSpaces (Sun), and for example Blitz open-source project TSpaces (IBM) 33

Conclusionss Linda Negatives? Less structured communication Data and structure intermixed Implementation efficiency Distributed space Distributed pattern ma atching 34

Part II Erlang

Erlang La anguage Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open PPHT07 Erlang 36

Erlang Typical Applications Telecoms Switches (POTS, ATM, IP, ) GPRS/3G/4G-LTE SMS applications Banks Credit card clearing 3D modelling (Wings s3d) PPHT07 Erlang 37

History Le esson 1981 Ericsson CSLab formed 1986 Prolog games 1987 Erlang name appears 1989 Prototypes show 9 22 fold increase in design ef fficiency 1995 AXE-N fails after 8 years 1998 AXD301 deliv vered 1998 Erlang banned; goes open source PPHT07 Erlang 38

Essence of Erlang A simple functional language Direct asynchronouss message passing Open Telecom Platform libraries Practically proven programming patterns Utilities t PPHT07 Erlang 39

Data types Constant Numbers Integers (arbitrarily big) Floats Atoms start_with_a_low wer_case_letter _ Anything_inside_quotes\n\09 PPHT07 Erlang 40

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! PPHT07 Erlang 41

Data types Compound Records -record(person, {name =, phone = [], address}). X = #person{name e = Joe, phone = [1,1,2], address= here } X#person.namename X#person{phone = [0,3,1,1,2,3,4]} PPHT07 Erlang 42

Modelling Ha askell data data Tree a = Leaf a Node (Tree a ) (Tree a) edoc style types : tree(a) = {leaf, A} {node, tree(a), tree(a)} PPHT07 Erlang 43

Variab les 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 PPHT07 Erlang 44

Functions and Modules 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 l/1]). factorial(0) -> 1; factorial(n) -> N * factorial(n-1). PPHT07 Erlang 45

Evaluat tion 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 * factoria al(1) matches N = 1 in clause 2 == 3 * 2 * 1 * fact torial(1-1) == 3 * 2 * 1 * factorial(0) == 3 * 2 * 1 * 1 (clause 1) == 6 PPHT07 Erlang 46

Pattern Ma atching area({square,sid de}) -> Side*Side Side. a pattern {square, Side} matches {square, 4} and binds 4 to variable Side PPHT07 Erlang 47

More Pattern Matching {B, C, D} = {10, foo, bar} Succeeds - binds B to 10, C to foo and D to bar {A, A, B} = {ab c, 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 PPHT07 Erlang 48

Even More Patte ern Matching [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 PPHT07 Erlang 49

List Exam mples -module(list_stuff). -export([average e/1, average/2]). average(x) -> su um(x) / len(x). average(a, B) -> (A+B)/2. sum([h T]) -> H + sum(t); sum([]) -> 0. len([h T]) -> 1 + len(t); len([]) -> 0. PPHT07 Erlang 50

Loops Tail Recursion Inefficient recursive definition len([h T]) -> 1 + len(t); len([]) -> 0. Fast tail recursive version = while loop len(list) -> len_a(list, 0). len_a([_ T], Acc) len_ a([], Acc) -> len_a(t, Acc+1); -> Acc. PPHT07 Erlang 51

Concurrent Pro ogramming Based on Message Passing: Q. What form of synchronisation? A. Asynchronous Q. What form of process naming? A. Direct, asymmetric A send MSG to B B receive anything PPHT07 Erlang 52

Proces sses Process = a function separate thread evaluated in a -module(process). -export([start/0]). start() -> Pid = spawn(fu un run/0), io:format("spawned ~p~n",[pid]). run() -> io:format("hello!~n",[]). PPHT07 Erlang 53

BIFs Built-in functions spawn, spawn_link k, atom_to_list Don t need importing Mostly in module called erlang Some have guarante eed termination These can be used in guards PPHT07 Erlang 54

Process Creation -module(echo). -export([start/0]). start() -> Pid = spawn(fun( ) -> loop(42) end), Pid! {self(), hello}, io:format("sent hello.~n",[]). %%continues PPHT07 Erlang 55

Message Passing %%continuation loop(number) -> receive {From, Msg} -> From! {self(), Number}, io:format( "Recei ived ~p, N: ~p~n n", [Msg, Number]), loop(numb ber+1); stop -> true end. PPHT07 Erlang 56

Message Passing S Pid = spawn(fun() -> loop(42) end) PPHT07 Erlang 57

Message Passing S {S, hello} L io:format( ). receive {From, Msg} stop end. PPHT07 Erlang 58

Message Passing {< >, 42} S L S! {self(), 42}, loop(42+1); PPHT07 Erlang 59

Conclusions Erlang Functional Concurrent Basics Next time More Erlang concurrency PPHT07 Erlang 60