1 chapter 9 spaces with linda. 2 linda linda is an experimental programming concept unlike ada or...
TRANSCRIPT
1
Chapter 9Chapter 9
Spaces with LINDASpaces with LINDA
2
LindaLinda
Linda is an experimental programming Linda is an experimental programming concept unlike ADA or Occam which are concept unlike ADA or Occam which are fully developed production-quality fully developed production-quality languages.languages.
Linda has been experimentally Linda has been experimentally embedded in several languagesembedded in several languages
3
Linda – Ada and OccamLinda – Ada and Occam Ada and Occam are based on the idea of Ada and Occam are based on the idea of
synchronous communication channels synchronous communication channels between processes.between processes.
There is There is couplingcoupling in both in both timetime and and spacespace Time:Time: The synchronous rendezvous means that The synchronous rendezvous means that
both processes must exist simultaneously in order both processes must exist simultaneously in order to communicate (must synchronize to pass data).to communicate (must synchronize to pass data).
Space: Space: Addressing by process identification (task Addressing by process identification (task name in ADA, channel name in Occam) means that name in ADA, channel name in Occam) means that there must be some global scope where processes there must be some global scope where processes are defined (processes must know the identity to are defined (processes must know the identity to pass data).pass data).
4
Linda Linda TupleTuple SSpacepace
Linda provides uncoupling in both time and Linda provides uncoupling in both time and space (unsynchronized and no identity check).space (unsynchronized and no identity check).
This is done by defining a This is done by defining a tupletuple space (TS) space (TS) which is some form of shared memory which is some form of shared memory containing containing tuplestuples
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
TupleTuple Tuples are typed sequences of data like Tuples are typed sequences of data like
parameter lists in procedure calls.parameter lists in procedure calls. The concurrent programming primitives of The concurrent programming primitives of
Linda work by pattern matching on the tuple Linda work by pattern matching on the tuple signaturessignatures
Eg: Eg: (1,’A’) - (integer, character)(1,’A’) - (integer, character) (1,2) - (1,2) - (integer,(integer, integer)integer) (‘A’,1) - (character,(‘A’,1) - (character, integer)integer)
These tuples can not be matched since their These tuples can not be matched since their signatures are differentsignatures are different..
6
The Linda PrimitivesThe Linda Primitives(Based on the Terminology used (Based on the Terminology used
in jBACI)in jBACI) Tuple spaceTuple space is calledis called aa board board and a tupleand a tuple is called ais called a note note Postnote(v1, v2, ...) Postnote(v1, v2, ...) This statement creates a note from This statement creates a note from
the values of the parameters and posts it on the board. If the values of the parameters and posts it on the board. If there are processes blocked waiting for a note matching there are processes blocked waiting for a note matching this parameter signature, an arbitrary one of them is this parameter signature, an arbitrary one of them is unblockedunblocked
Removenote(x1, x2, ...) Removenote(x1, x2, ...) The parameters must be variables. The parameters must be variables. The statement removes a note that matches the The statement removes a note that matches the parameter signature from the board and assigns its values parameter signature from the board and assigns its values to the parameters. If no matching note exists, the process to the parameters. If no matching note exists, the process is blocked. If there is more than one matching note, an is blocked. If there is more than one matching note, an arbitrary one is removedarbitrary one is removed
Readnote(x1, x2, ...) Readnote(x1, x2, ...) Like removenote, but it leaves the Like removenote, but it leaves the note on the boardnote on the board
7
An ExampleAn Example Suppose that the following statements are executedSuppose that the following statements are executed
postnote(‘a’, 10, 20)postnote(‘a’, 10, 20)postnote(‘a’, 30)postnote(‘a’, 30)postnote(‘a’, false, 40)postnote(‘a’, false, 40)postnote(‘a’, true, 50)postnote(‘a’, true, 50)
Execute the statements below
Integer m, integer n, boolean bInteger m, integer n, boolean b
1.1. removenote(‘b’, m, n)removenote(‘b’, m, n) process is blockedprocess is blocked2.2. removenote(‘a’, m, n)removenote(‘a’, m, n) process matches and removes note (‘a’, process matches and removes note (‘a’,
10, 20)10, 20)3.3. readnote(‘a’, m)readnote(‘a’, m) process matches and reads note (‘a’, 30) – no process matches and reads note (‘a’, 30) – no
removalremoval4.4. removenote(‘a’, n)removenote(‘a’, n) note (‘a’, 30) is removednote (‘a’, 30) is removed5.5. removenote(‘a’, m, n)removenote(‘a’, m, n) process is blockedprocess is blocked6.6. removenote(‘a’, b, n)removenote(‘a’, b, n) one of the (‘a’, true, 50) or (‘a’, false, one of the (‘a’, true, 50) or (‘a’, false,
40) is removed40) is removed7.7. removenote(‘a’, b, m)removenote(‘a’, b, m) the other note is removedthe other note is removed8.8. postnote(‘b’, 60, 70)postnote(‘b’, 60, 70) process blocked in (1) is unblocked and process blocked in (1) is unblocked and
the note is removedthe note is removed
The contents of the The contents of the board board
8
Implementing a General Semaphore Implementing a General Semaphore in Lindain Linda
Post k notes as the initial value of a semaphorePost k notes as the initial value of a semaphoredo K times postnote(‘s’)do K times postnote(‘s’)
Removenote is equivalent to Removenote is equivalent to waitwait and postnote and postnote to to signalsignal
9
Formal ParametersFormal Parameters
Client posts a note ‘S’ with process ID, service request Client posts a note ‘S’ with process ID, service request and parameters for the service and waits for a result note and parameters for the service and waits for a result note ‘R’ with matching ID and the result‘R’ with matching ID and the result
The server removes a service note ‘S’ from some client The server removes a service note ‘S’ from some client with service request s and parameter p. When the service with service request s and parameter p. When the service is over it posts a result note ‘R’ to the same clientis over it posts a result note ‘R’ to the same client
Here, a server provides several different service requests Here, a server provides several different service requests at the same timeat the same time
10
Match for a Specific ValueMatch for a Specific Value
The server in the above algorithm waits The server in the above algorithm waits for a specific service using a formal for a specific service using a formal parameter ‘s=‘parameter ‘s=‘
11
Another ExampleAnother Example
The formal parameter ‘count=‘ in the The formal parameter ‘count=‘ in the consumer makes sure that the values are consumer makes sure that the values are consumed in the order they are producedconsumed in the order they are produced
12
Linda in j BACILinda in j BACI A A notenote in jBACI consists of a triple: a character in jBACI consists of a triple: a character
and two integer values.. and two integer values.. The first parameter must be a character The first parameter must be a character valuevalue, ,
while the two optional parameters must be while the two optional parameters must be integer integer variablesvariables..
The operations can be called with one, two or The operations can be called with one, two or three parameters; unused integer parameters three parameters; unused integer parameters are given the default value -32767:are given the default value -32767:
postnote('a'); postnote('a'); { Equivalent to postnote('a', - { Equivalent to postnote('a', -32767, -32767) }32767, -32767) }
postnote('a', 5); { Equivalent to postnote('a', 5, -postnote('a', 5); { Equivalent to postnote('a', 5, -32767) }32767) }
postnote('a', 5, 10);postnote('a', 5, 10);
13
Linda in j BACI (Cont.)Linda in j BACI (Cont.) For historical reasons, there are two syntaxes for matching For historical reasons, there are two syntaxes for matching
the values of a tuple to the current values of the parameters:the values of a tuple to the current values of the parameters: If the equal sign If the equal sign “=“ “=“ appears after a variable, the value of appears after a variable, the value of
the note in that position must match the current value of the note in that position must match the current value of that variable. For example, the following statements will that variable. For example, the following statements will remove any tuple of the form ('a',...,5):remove any tuple of the form ('a',...,5):
i2 := 5; removenote('a',i1,i2=);i2 := 5; removenote('a',i1,i2=); removenoteeq (readnoteeq) is like removenote (readnote), removenoteeq (readnoteeq) is like removenote (readnote),
but the two parameters values of the note must match the but the two parameters values of the note must match the current values of both variables. For example, the current values of both variables. For example, the following statements will remove only the tuple ('a',1,2):following statements will remove only the tuple ('a',1,2):
i1 := 1; i2 := 2; removenoteeq('a',i1,i2);i1 := 1; i2 := 2; removenoteeq('a',i1,i2); Note thatNote that removenoteeq('a',i1,i2) is equivalent to removenoteeq('a',i1,i2) is equivalent to
removenote('a',i1=,i2=).removenote('a',i1=,i2=).
14
Matrix Multiplication Algorithm Matrix Multiplication Algorithm in Lindain Linda
Notes identified as Notes identified as ‘‘E’ contain the elements passed from north to southE’ contain the elements passed from north to south ‘‘S’ contain the partial sums passed from east to westS’ contain the partial sums passed from east to west
Matrix Multiplication in Matrix Multiplication in jBacijBaci
Multiply two matrices Multiply two matrices A A and B to get matrix and B to get matrix CC
AA x B x B == C C
1 2 31 2 3 1 0 11 0 1 7 8 1 7 8 1
4 5 64 5 6 x x 0 1 00 1 0 == 16 17 416 17 4
7 8 97 8 9 2 2 02 2 0 25 26 725 26 7
15
16
Matrix Multiplication in Matrix Multiplication in jBacijBaci
program LindaMultiplication;program LindaMultiplication;{multiply two 3x3 matrices A and B - 9 Workers}{multiply two 3x3 matrices A and B - 9 Workers}
var a,b :array[1..3,1..3] of integer;var a,b :array[1..3,1..3] of integer;
process initandprint; end;process initandprint; end;process worker; end;process worker; end;
beginbegincobegin cobegin
initandprint; initandprint; worker; worker; worker; worker; worker; worker; worker; worker; worker; worker; worker; worker; worker; worker; worker;worker; worker; worker;
coend;coend;end.end.
17
Matrix Multiplication in Matrix Multiplication in jBaci (Cont.)jBaci (Cont.)
process initandprint;process initandprint;varvar c, i, j, k: integer; c, i, j, k: integer; beginbegin {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); 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;writeln('c',k,'=',c); end;end;end;
18
Matrix Multiplication in Matrix Multiplication in jBaci (Cont.)jBaci (Cont.)
process worker;process worker;var element, row, col, r, v :integer;var element, row, col, r, v :integer;beginbegin while true dowhile true do beginbegin 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;end;
19
Producer-Consumer with Producer-Consumer with LindaLinda
One producer and 3 consumers. One producer and 3 consumers. Producer puts a message on the Linda boardProducer puts a message on the Linda board All consumers read the same message and the last one to All consumers read the same message and the last one to
read removes the note foreverread removes the note forever
program producerandconsumers;program producerandconsumers;constconst n=3;n=3; {3 consumers}{3 consumers}
process producer; end;process producer; end;process consumer; end;process consumer; end;
beginbegin cobegin producer; consumer; consumer; consumer; cobegin producer; consumer; consumer; consumer;
coend;coend;end.end.
20
Producer-Consumer with Producer-Consumer with Linda (Cont.)Linda (Cont.)
process producer;process producer;var var i: integer := 34;i: integer := 34; c: char;c: char;beginbegin while (i < 126) do while (i < 126) do {characters 34 to 126 are printable characters}{characters 34 to 126 are printable characters} begin begin
c:= i; c:= i; {convert value to character - first parameter of postnote is a {convert value to character - first parameter of postnote is a character!}character!}i:= i+1; i:= i+1; {increment for the next message}{increment for the next message}postnote(c,n) postnote(c,n) {post a message}{post a message}
end; end; end;end;
process consumer;process consumer;VarVar c :char; c :char; i :integer :=34; i :integer :=34; j :integer;j :integer;beginbegin while (i < 126) dowhile (i < 126) do begin begin
c:= i; removenote(c,j); c:= i; removenote(c,j); {remove the note}{remove the note}j:= j-1; if j <> 0 then postnote(c,j); j:= j-1; if j <> 0 then postnote(c,j); {if not the last process then put note with one {if not the last process then put note with one less count}less count}i:= i+1 i:= i+1 {increment for the next message}{increment for the next message}
end;end;end;end;
21
Linda Simulation of Linda Simulation of RendezvousRendezvous
Ada rendezvous to be simulatedAda rendezvous to be simulatedaccept Proc(I: in integer; C: in character; B: out accept Proc(I: in integer; C: in character; B: out boolean) doboolean) do
..............
end Proc;end Proc;
Proc(65, ‘X’, B); --- call to entryProc(65, ‘X’, B); --- call to entry
22
Linda Simulation of RendezvousLinda Simulation of Rendezvous
integer pid, i; character cinteger pid, i; character c
-- code for accepting process-- code for accepting processreadnote(“Proc”, pid, i, c);readnote(“Proc”, pid, i, c);Proc(I, C, B); -- accept “proc” code Proc(I, C, B); -- accept “proc” code procedureprocedurepostnote(pid, B);postnote(pid, B);
-- code for calling process-- code for calling processreadnote(“Proc”, 24, 65, ‘X’);readnote(“Proc”, 24, 65, ‘X’);postnote(24, B: Boolean);postnote(24, B: Boolean);