Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 9 Spaces with LINDA. 2 Linda Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages.

Similar presentations


Presentation on theme: "1 Chapter 9 Spaces with LINDA. 2 Linda Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages."— Presentation transcript:

1 1 Chapter 9 Spaces with LINDA

2 2 Linda Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages. Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages. Linda has been experimentally embedded in several languages Linda has been experimentally embedded in several languages

3 3 Linda – Ada and Occam Ada and Occam are based on the idea of synchronous communication channels between processes. Ada and Occam are based on the idea of synchronous communication channels between processes. There is coupling in both time and space There is coupling in both time and space Time: The synchronous rendezvous means that both processes must exist simultaneously in order to communicate (must synchronize to pass data). Time: The synchronous rendezvous means that both processes must exist simultaneously in order to communicate (must synchronize to pass data). Space: Addressing by process identification (task name in ADA, channel name in Occam) means that there must be some global scope where processes are defined (processes must know the identity to pass data). Space: Addressing by process identification (task name in ADA, channel name in Occam) means that there must be some global scope where processes are defined (processes must know the identity to pass data).

4 4 Linda Tuple Space Linda provides uncoupling in both time and space (unsynchronized and no identity check). Linda provides uncoupling in both time and space (unsynchronized and no identity check). This is done by defining a tuple space (TS) which is some form of shared memory containing tuples This is done by defining a tuple space (TS) which is some form of shared memory containing tuples The shared data (tuples) is persistent, meaning that the data can exist after the termination of the process that created them and used by processes that are activated later

5 5 Tuple Tuples are typed sequences of data like parameter lists in procedure calls. Tuples are typed sequences of data like parameter lists in procedure calls. The concurrent programming primitives of Linda work by pattern matching on the tuple signatures The concurrent programming primitives of Linda work by pattern matching on the tuple signatures Eg: (1,’A’) - (integer, character) Eg: (1,’A’) - (integer, character) (1,2) - (integer, integer) (1,2) - (integer, integer) (‘A’,1) - (character, integer) (‘A’,1) - (character, integer) These tuples can not be matched since their signatures are different.

6 6 The Linda Primitives (Based on the Terminology used in jBACI) Tuple space is called a board and a tuple is called a note Tuple space is called a board and a tuple is called a note Postnote(v1, v2,...) This statement creates a note from the values of the parameters and posts it on the board. If there are processes blocked waiting for a note matching this parameter signature, an arbitrary one of them is unblocked Postnote(v1, v2,...) This statement creates a note from the values of the parameters and posts it on the board. If there are processes blocked waiting for a note matching this parameter signature, an arbitrary one of them is unblocked Removenote(x1, x2,...) The parameters must be variables. The statement removes a note that matches the parameter signature from the board and assigns its values to the parameters. If no matching note exists, the process is blocked. If there is more than one matching note, an arbitrary one is removed Removenote(x1, x2,...) The parameters must be variables. The statement removes a note that matches the parameter signature from the board and assigns its values to the parameters. If no matching note exists, the process is blocked. If there is more than one matching note, an arbitrary one is removed Readnote(x1, x2,...) Like removenote, but it leaves the note on the board Readnote(x1, x2,...) Like removenote, but it leaves the note on the board

7 7 An Example Suppose that the following statements are executed Suppose that the following statements are executed postnote(‘a’, 10, 20) postnote(‘a’, 30) postnote(‘a’, false, 40) postnote(‘a’, true, 50)  Execute the statements below Integer m, integer n, boolean b 1. removenote(‘b’, m, n)process is blocked 2. removenote(‘a’, m, n)process matches and removes note (‘a’, 10, 20) 3. readnote(‘a’, m)process matches and reads note (‘a’, 30) – no removal 4. removenote(‘a’, n)note (‘a’, 30) is removed 5. removenote(‘a’, m, n)process is blocked 6. removenote(‘a’, b, n)one of the (‘a’, true, 50) or (‘a’, false, 40) is removed 7. removenote(‘a’, b, m)the other note is removed 8. postnote(‘b’, 60, 70)process blocked in (1) is unblocked and the note is removed  The contents of the board

8 8 Implementing a General Semaphore in Linda Post k notes as the initial value of a semaphore Post k notes as the initial value of a semaphore do K times postnote(‘s’) Removenote is equivalent to wait and postnote to signal Removenote is equivalent to wait and postnote to signal

9 9 Formal Parameters Client posts a note ‘S’ with process ID, service request and parameters for the service and waits for a result note ‘R’ with matching ID and the result Client posts a note ‘S’ with process ID, service request and parameters for the service and waits for a result note ‘R’ with matching ID and the result The server removes a service note ‘S’ from some client with service request s and parameter p. When the service is over it posts a result note ‘R’ to the same client The server removes a service note ‘S’ from some client with service request s and parameter p. When the service is over it posts a result note ‘R’ to the same client Here, a server provides several different service requests at the same time Here, a server provides several different service requests at the same time

10 10 Match for a Specific Value The server in the above algorithm waits for a specific service using a formal parameter ‘s=‘ The server in the above algorithm waits for a specific service using a formal parameter ‘s=‘

11 11 Another Example The formal parameter ‘count=‘ in the consumer makes sure that the values are consumed in the order they are produced The formal parameter ‘count=‘ in the consumer makes sure that the values are consumed in the order they are produced

12 12 Linda in j BACI A note in jBACI consists of a triple: a character and two integer values.. A note in jBACI consists of a triple: a character and two integer values.. The first parameter must be a character value, while the two optional parameters must be integer variables. The first parameter must be a character value, while the two optional parameters must be integer variables. The operations can be called with one, two or three parameters; unused integer parameters are given the default value -32767: The operations can be called with one, two or three parameters; unused integer parameters are given the default value -32767: postnote('a'); { Equivalent to postnote('a', -32767, -32767) } postnote('a', 5); { Equivalent to postnote('a', 5, -32767) } postnote('a', 5, 10);

13 13 Linda in j BACI (Cont.) For historical reasons, there are two syntaxes for matching the values of a tuple to the current values of the parameters: For historical reasons, there are two syntaxes for matching the values of a tuple to the current values of the parameters: If the equal sign “=“ appears after a variable, the value of the note in that position must match the current value of that variable. For example, the following statements will remove any tuple of the form ('a',...,5): If the equal sign “=“ appears after a variable, the value of the note in that position must match the current value of that variable. For example, the following statements will remove any tuple of the form ('a',...,5): i2 := 5; removenote('a',i1,i2=); removenoteeq (readnoteeq) is like removenote (readnote), but the two parameters values of the note must match the current values of both variables. For example, the following statements will remove only the tuple ('a',1,2): removenoteeq (readnoteeq) is like removenote (readnote), but the two parameters values of the note must match the current values of both variables. For example, the following statements will remove only the tuple ('a',1,2): i1 := 1; i2 := 2; removenoteeq('a',i1,i2); Note that removenoteeq('a',i1,i2) is equivalent to removenote('a',i1=,i2=). Note that removenoteeq('a',i1,i2) is equivalent to removenote('a',i1=,i2=).

14 14 Matrix Multiplication Algorithm in Linda Notes identified as Notes identified as ‘E’ contain the elements passed from north to south ‘E’ contain the elements passed from north to south ‘S’ contain the partial sums passed from east to west ‘S’ contain the partial sums passed from east to west

15 Matrix Multiplication in jBaci Multiply two matrices A and B to get matrix C Multiply two matrices A and B to get matrix C A x B= C A x B= C 1 2 31 0 1 7 8 1 4 5 6 x 0 1 0=16 17 4 7 8 92 2 025 26 7 15

16 16 Matrix Multiplication in jBaci program LindaMultiplication; {multiply two 3x3 matrices A and B - 9 Workers} var a,b :array[1..3,1..3] of integer; process initandprint; end; process worker; end; begincobegininitandprint; worker; worker; worker; coend;end.

17 17 Matrix Multiplication in jBaci (Cont.) process initandprint; varc, i, j, k: integer; begin {array A} {array A} a[1,1]:=1; a[1,2]:=2; a[1,3]:=3; a[1,1]:=1; a[1,2]:=2; a[1,3]:=3; a[2,1]:=4; a[2,2]:=5; a[2,3]:=6; a[2,1]:=4; a[2,2]:=5; a[2,3]:=6; a[3,1]:=7; a[3,2]:=8; a[3,3]:=9; a[3,1]:=7; a[3,2]:=8; a[3,3]:=9; {array B} {array B} b[1,1]:=1; b[1,2]:=0; b[1,3]:=2; b[1,1]:=1; b[1,2]:=0; b[1,3]:=2; b[2,1]:=1; b[2,2]:=2; b[2,3]:=1; b[2,1]:=1; b[2,2]:=2; b[2,3]:=1; b[3,1]:=1; b[3,2]:=1; b[3,3]:=1; b[3,1]:=1; b[3,2]:=1; b[3,3]:=1; {post worker notes – column and row numbers} {post worker notes – column and row numbers} postnote('w',1,1); postnote('w',1,2); postnote('w',1,3); postnote('w',1,1); postnote('w',1,2); postnote('w',1,3); postnote('w',2,1); postnote('w',2,2); postnote('w',2,3); postnote('w',2,1); postnote('w',2,2); postnote('w',2,3); postnote('w',3,1); postnote('w',3,2); postnote('w',3,3); postnote('w',3,1); postnote('w',3,2); postnote('w',3,3); postnote('N',1); {Next job counter} postnote('N',1); {Next job counter} { write the resulting matrix when computations are over} { write the resulting matrix when computations are over} for i:= 1 to 3 do for j:=1 to 3 do begin k:=i*10+j; removenote('c',k,c); writeln('c',k,'=',c); end; for i:= 1 to 3 do for j:=1 to 3 do begin k:=i*10+j; removenote('c',k,c); writeln('c',k,'=',c); end;end;

18 18 Matrix Multiplication in jBaci (Cont.) process worker; var element, row, col, r, v :integer; begin while true do while true do begin begin removenote('N',element); removenote('N',element); postnote('N',element+1); postnote('N',element+1); if element > 3*3 then suspend; if element > 3*3 then suspend; removenote('w',row,col); removenote('w',row,col); v:=0; v:=0; for r:=1 to 3 do v:=v+a[row,r]*b[r,col]; for r:=1 to 3 do v:=v+a[row,r]*b[r,col]; r:= row*10+col; postnote('c',r,v); r:= row*10+col; postnote('c',r,v); end; end;end;

19 19 Producer-Consumer with Linda One producer and 3 consumers. One producer and 3 consumers. Producer puts a message on the Linda board Producer puts a message on the Linda board All consumers read the same message and the last one to read removes the note forever All consumers read the same message and the last one to read removes the note forever program producerandconsumers; constn=3;{3 consumers} process producer; end; process consumer; end; begin cobegin producer; consumer; consumer; consumer; coend; cobegin producer; consumer; consumer; consumer; coend;end.

20 20 Producer-Consumer with Linda (Cont.) process producer; var i: integer := 34; c: char; c: char;begin while (i < 126) do {characters 34 to 126 are printable characters} while (i < 126) do {characters 34 to 126 are printable characters} begin begin c:= i; {convert value to character - first parameter of postnote is a character!} i:= i+1; {increment for the next message} postnote(c,n) {post a message} end; end;end; process consumer; Varc :char; i :integer :=34; i :integer :=34; j :integer; j :integer;begin while (i < 126) do while (i < 126) do begin begin c:= i; removenote(c,j); {remove the note} j:= j-1; if j <> 0 then postnote(c,j); {if not the last process then put note with one less count} i:= i+1 {increment for the next message} end; end;end;

21 21 Linda Simulation of Rendezvous Ada rendezvous to be simulated Ada rendezvous to be simulated accept Proc(I: in integer; C: in character; B: out boolean) do....... end Proc; Proc(65, ‘X’, B); --- call to entry

22 22 Linda Simulation of Rendezvous integer pid, i; character c -- code for accepting process readnote(“Proc”, pid, i, c); Proc(I, C, B); -- accept “proc” code procedure postnote(pid, B); -- code for calling process readnote(“Proc”, 24, 65, ‘X’); postnote(24, B: Boolean);


Download ppt "1 Chapter 9 Spaces with LINDA. 2 Linda Linda is an experimental programming concept unlike ADA or Occam which are fully developed production-quality languages."

Similar presentations


Ads by Google