Presentation is loading. Please wait.

Presentation is loading. Please wait.

© P. H. Welch1 Lego-Land ™ Chapter 5. © P. H. Welch2 … some simple networks.

Similar presentations


Presentation on theme: "© P. H. Welch1 Lego-Land ™ Chapter 5. © P. H. Welch2 … some simple networks."— Presentation transcript:

1 © P. H. Welch1 Lego-Land ™ Chapter 5

2 © P. H. Welch2 … some simple networks

3 © P. H. Welch3 ‘Legoland’ Catalog inout succ inout tail in out.0 out.1 black.hole in n n out id in out + + in.0 in.1 out

4 © P. H. Welch4 PROC id (CHAN OF INT in, out) WHILE TRUE WHILE TRUE INT x: INT x: SEQ SEQ in ? x in ? x out ! x out ! x: PROC succ (CHAN OF INT in, out) WHILE TRUE WHILE TRUE INT x: INT x: SEQ SEQ in ? x in ? x out ! x + 1 out ! x + 1: PROC black.hole (CHAN OF INT in) WHILE TRUE WHILE TRUE INT x: INT x: in ? x in ? x: inout succ in id in out

5 © P. H. Welch5 PROC plus (CHAN OF INT in.0, in.1, out) WHILE TRUE WHILE TRUE INT x.0, x.1: INT x.0, x.1: SEQ SEQ PAR PAR in.0 ? x.0 in.0 ? x.0 in.1 ? x.1 in.1 ? x.1 out ! x.0 + x.1 out ! x.0 + x.1: PROC delta (CHAN OF INT in, out.0, out.1) WHILE TRUE WHILE TRUE INT x: INT x: SEQ SEQ in ? x in ? x PAR PAR out.0 ! x out.0 ! x out.1 ! x out.1 ! x: in out.0 out.1 + + in.0 in.1 out

6 © P. H. Welch6 PROC prefix (VAL INT n, CHAN OF INT in, out) SEQ SEQ out ! n out ! n id (in, out) id (in, out): PROC tail (CHAN OF INT in, out) SEQ SEQ INT any: INT any: in ? any in ? any id (in, out) id (in, out): scope of any inout tail n n inout

7 © P. H. Welch7 Theorem n n tail id is a blocking FIFO buffer of capacity 6 Theorem 

8 © P. H. Welch8 succ 0 0 numbers out integrate + + 0 0 in pairs out tail + + in p p + q p + q + r p + q + r + s... 0 1 2 3... p q r s... p q r s... q + p r + q s + r t + s...

9 © P. H. Welch9 PROC numbers (CHAN OF INT out) CHAN OF INT a, b, c: CHAN OF INT a, b, c: PAR PAR delta (a, out, b) delta (a, out, b) succ (b, c) succ (b, c) prefix (0, c, a) prefix (0, c, a): PROC integrate (CHAN OF INT in, out) CHAN OF INT a, b, c: CHAN OF INT a, b, c: PAR PAR delta (a, out, b) delta (a, out, b) prefix (0, b, c) prefix (0, b, c) plus (in, c, a) plus (in, c, a): PROC pairs (CHAN OF INT in, out) CHAN OF INT a, b, c: CHAN OF INT a, b, c: PAR PAR delta (in, a, c) delta (in, a, c) tail (a, b) tail (a, b) plus (b, c, out) plus (b, c, out): succ 0 0 numbers out a b c integrate + + 0 0 in a b c pairs out tail + + in a c b

10 © P. H. Welch10 Sequential Version numbers out PROC numbers (CHAN OF INT out) INT n: INT n: SEQ SEQ n := 0 n := 0 WHILE TRUE WHILE TRUE SEQ SEQ out ! n out ! n n := n + 1 n := n + 1: 0, 1, 2, 3, …

11 © P. H. Welch11 PROC integrate (CHAN OF INT in, out) INT total: SEQ total := 0 WHILE TRUE INT x: SEQ in ? x total := total + x out ! total : Note: each declaration is as local as possible out in integrate p, p + q, p + q + r, p + q + r + s, …p, q, r, s, … Sequential Version

12 © P. H. Welch12 pairs 0 0 fibonacci out 1 1 squares out numbers integrate pairs 0 1 2 3 5 8... 1 4 9 16 25 36... 1 2 3 5 8 13... 0 1 2 3 4 5... 0 1 3 6 10 15...

13 © P. H. Welch13 PROC squares (CHAN OF INT out) CHAN OF INT a, b: PAR numbers (a) integrate (a, b) pairs (b, out) : PROC fibonacci (CHAN OF INT out) CHAN OF INT a, b, c, d: PAR delta (a, b, out) pairs (b, c) prefix (0, d, a) prefix (1, c, d) : pairs 0 0 fibonacci a outbc 1 1 d squares a b numbers pairs integrate

14 © P. H. Welch14 PROC demo (CHAN OF BYTE out) [4]CHAN OF INT c: PAR numbers (c[0]) squares (c[1]) fibonacci (c[2]) times (c[3]) lay.out (c, out) : Note: use of channel array demo c[0] c[2] c[3] out c[1] numbersfibonacci squares times lay.out

15 © P. H. Welch15... replicators

16 © P. H. Welch16 PAR i = 0 FOR 4 in[i] ? x[i] means PAR in[0] ? x[0] in[1] ? x[1] in[2] ? x[2] in[3] ? x[3] INT declaration number of replications first value x 0 1 2 3 in[0] in[1] in[2] in[3]

17 © P. H. Welch17 The Sort Pump cell out in out c[1] in c[13]c[0]

18 © P. H. Welch18 VAL INT total IS 16: PROC sort (CHAN OF BYTE in, out) [total-2]CHAN OF BYTE c: PAR cell (in, c [0]) PAR p = 1 FOR total-3 cell (c[p-1], c[p]) cell (c[total-3], out) : This is a “pump” of 15 cells – i.e. it can sort groups of up to 16 items

19 © P. H. Welch19 VAL BYTE mark IS 255: PROC cell(CHAN OF BYTE in, out) -- in ? {{data}* mark}* -- ASSUME: mark > data WHILE TRUE BYTE max: SEQ in ? max WHILE max <> mark BYTE n: SEQ in ? n IF -- output smaller and keep larger max >= n out ! n TRUE -- i.e. max < n SEQ out ! max max := n out ! mark : cell out in

20 © P. H. Welch20 sort test rig in out main a b a b out in m[3] probe.out probe.in m[1] m[0] m[2] test.rig keys window (16, 2) lay.out window (48, 2) screen

21 © P. H. Welch21 Test Rig Components Keys:filters user input; rejects erroneous input (and issues error report), compresses/encodes legal inputfor onward transmission. Screen:multiplexes message streams to single output channel. Window:fixed-size delay line, dumps entire contents to some screen “window” on every change of state. Layout:lays out results of previous tests.

22 © P. H. Welch22 Design Guidelines one function = one process multiple functions = parallel processes each function programmed from its own point-of-view (“object-oriented” design)

23 © P. H. Welch23 PROC main (CHAN OF BYTE in, out) CHAN OF BYTE a, b: PAR sort (a, b) test.rig (in, out, b, a) : PROC test.rig (CHAN OF BYTE in, out, probe.in, probe.out) CHAN OF BYTE a, b: [4]CHAN OF BYTE m: PAR keys (in, a, m[0]) window (16, 2, a, probe.out, m[1]) // top-left window (48, 2, probe.in, b, m[2]) // top-right lay.out (b, m[3]) screen (m, out) :

24 © P. H. Welch24 Parallel Implementation of the Sort Cell PROC cell (CHAN OF BYTE in, out) CHAN OF BYTE a, b, c: PAR gt (in, b, c, a) sub (hi, lo, a, b) sub (lo, hi, c, out) : > in out lo hi lo b a cell c.

25 © P. H. Welch25 a b out in PROC sub (VAL BYTE a, b, CHAN OF BYTE in, out) SEQ out ! b WHILE TRUE BYTE x: SEQ in ? x IF x = a out ! b TRUE out ! x :

26 © P. H. Welch26 PROC gt (CHAN OF BYTE in.0, in.1, small, large) WHILE TRUE BYTE x.0, x.1: SEQ PAR in.0 ? x.0 in.1 ? x.1 IF x.0 < x.1 PAR small ! x.0 large ! x.1 TRUE PAR small ! x.1 large ! x.0 : large > in.0 in.1 small.

27 © P. H. Welch27 Non-Deterministic Construct ALT Guarded Process

28 © P. H. Welch28 Guard Attributes A guard may be ready or not-ready. A not-ready guard may change to ready as a result of external activity. A ready guard may be executed. A guard may be simple or pre-conditioned. The following definition assumes all the guards are simple:

29 © P. H. Welch29 Alt Semantics if no guard is ready, the process is suspended until one (or more) is ready if one guard is ready, execute it and then execute the process it was defending (end of Alt process) if more than one guard is ready the system arbitrarily chooses one of them, executes it and then executes the process it was defending (end of Alt process) An ALT process executes as follows: Only one “guarded process” executes.

30 © P. H. Welch30 Simple Guard Input Guard in ? x An input guard is ready if a parallel process has output to that channel and is waiting acknowledgement of its data. An input guard executes like an input process.

31 © P. H. Welch31 crude.plex in.0 in.2 in.1 out PROC crude.plex (CHAN OF INT in.0, in.1, in.2, out) WHILE TRUE INT x: ALT in.0 ? x out ! x in.1 ? x out ! x in.2 ? x out ! x :

32 © P. H. Welch32 Simple Guard Timeout Guard tim ? AFTER t A timeout guard is ready if the value on the tim channel (always available and increasing) is AFTER the (fixed) value of t. It has a null execution.

33 © P. H. Welch33 PROC watch (VAL INT max.gap, CHAN OF INT in, out, CHAN OF BOOL panic) WHILE TRUE TIMER tim: INT x, t: SEQ tim ? t ALT in ? x out ! x tim ? AFTER t PLUS max.gap panic ! TRUE : in out panic watch (max.gap)

34 © P. H. Welch34 Simple Guard Skip Guard TRUE & SKIP A skip guard is always ready. It has a null execution. Its main application is for polling input channels to see if data is pending (but only in conjunction with PRI ALT – see later). However: ALT TRUE & SKIP x := 42 TRUE & SKIP x := 43 Do either assignment – you choose!

35 © P. H. Welch35 The skip guard always has to be written with a pre-condition. Pre-conditioned guards will be explained later. For all simple guards, the pre-condition is TRUE. This is why the simple skip guard is written in such a strange way. There is no good reason why a skip guard must have a pre-condition. It is just the cautious nature of the language. Skip guards should not be used too freely!

36 © P. H. Welch36 Deterministic Choice PRI ALT “Guarded” Process If more than one guard is ready, the first one in the order of listing is chosen for execution.

37 © P. H. Welch37 Polling To poll a channel to see (& take) if there is data pending: PRI ALT in ? x... data was present - execute this code TRUE & SKIP... data was not present - execute this code WARNING: polling is a rather expensive and dangerous technique! Be vary sparing in its use. Only use when absolutely essential.

38 © P. H. Welch38 Simple Multiplexor De-Multiplexor plex...... in[] out de.plex...... in out[]

39 © P. H. Welch39 PROC plex ([]CHAN OF INT in, CHAN OF INT out) WHILE TRUE INT x: ALT i = 0 FOR SIZE in in[i] ? x SEQ out ! i out ! x : PROC de.plex (CHAN OF INT in, []CHAN OF INT out) WHILE TRUE INT i, x: SEQ in ? i in ? x out[i] ! x : Note the replicated ALT

40 © P. H. Welch40 Channel Protocols It is a good idea to document the structure of information flowing through the channels: plex...... n n de.plex...... item* (channel number, item)* occam2 supports much of this in the language Use comments to document the rest

41 © P. H. Welch41 Sequential Protocol PROC plex ([]CHAN OF INT in, CHAN OF CHAN.INT out) WHILE TRUE INT x: ALT i = 0 FOR SIZE in in[i] ? x out ! i; x : PROC de.plex (CHAN OF CHAN.INT in, []CHAN OF INT out) WHILE TRUE INT i, x: SEQ in ? i; x out[i] ! x : PROTOCOL CHAN.INT IS INT; INT:

42 © P. H. Welch42 Sequential Protocol PROC plex ([]CHAN OF BYTE in, CHAN OF CHAN.BYTE out) WHILE TRUE BYTE x: ALT i = 0 FOR SIZE in in[i] ? x out ! i; x : PROC de.plex (CHAN OF CHAN.BYTE in, []CHAN OF BYTE out) WHILE TRUE INT i: BYTE x: SEQ in ? i; x out[i] ! x : PROTOCOL CHAN.BYTE IS INT; BYTE:

43 © P. H. Welch43 multi.plex...... in[] out de.multi.plex...... in out[] Channel number followed by a variable length packet Variable length packets of data: count a “length” count followed by that number of items5-77212499 items (e.g., 5, -77, 2, 12, 4, 99)

44 © P. H. Welch44 Packet Multiplexer (raw) multi.plex...... in[] out PROC multi.plex ([]CHAN OF INT in, CHAN OF INT out) WHILE TRUE ALT i = 0 FOR SIZE in INT length: in[i] ? length SEQ out ! i out ! length SEQ j = 0 FOR length INT x: SEQ in[i] ? x out ! x : Replicated ALT Declared only for the guarded process Guarded process Guard

45 © P. H. Welch45 Packet De-Multiplexer (raw) de.m.plex...... in out[] PROC de.m.plex (CHAN OF INT in, []CHAN OF INT out) WHILE TRUE INT i, length: SEQ in ? i in ? length out[i] ! length SEQ j = 0 FOR length INT x: SEQ in ? x out[i] ! x :

46 © P. H. Welch46 Array Protocol PROTOCOL PACKET IS INT::[]REAL64: UNFORTUNATELY: occam2 does not allow us to re-use user-named protocols in these definitions. Instead, we have to expand them down to their primitive names: PROTOCOL CHAN.PACKET IS INT; INT::[]REAL64: count array PROTOCOL CHAN.PACKET IS INT; PACKET:

47 © P. H. Welch47 User supplies multi.plex with a buffer sufficiently large for each application instance of this process.User supplies multi.plex with a buffer sufficiently large for each application instance of this process. With PROTOCOL s, channels securely carry structured messages (with mixed types).With PROTOCOL s, channels securely carry structured messages (with mixed types). multi.plex (buffer)...... in[] out PROC multi.plex ([]REAL64 buffer, []CHAN OF PACKET in, []CHAN OF CHAN.PACKET out) WHILE TRUE ALT i = 0 FOR SIZE in INT length: in[i] ? length::buffer out ! i; length::buffer : Higher-level expression (implicit inner loop) Faster execution

48 © P. H. Welch48 User supplies de.multi.plex with a buffer sufficiently large for each instance of this process.User supplies de.multi.plex with a buffer sufficiently large for each instance of this process. de.multi.plex (buffer)...... in out[] PROC de.multi.plex ([]REAL64 buffer, CHAN OF CHAN.PACKET in, []CHAN OF PACKET out) WHILE TRUE INT i, length: SEQ in ? i; length::buffer out[i] ! length::buffer : Higher-level expression (implicit inner loop) Faster execution

49 © P. H. Welch49 Three Monitors mon.0...... in[] alarm mon.1...... in[] alarm limits mon.2...... in[] alarm control limits

50 © P. H. Welch50 Protocols for the Monitors PROTOCOL MY.DATA IS INT: PROTOCOL ALARM.MESSAGE IS INT; INT: offending channel number offending value

51 © P. H. Welch51 PROC mon.0 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm) -- Sends an “alarm” if any input is out of the -- range 30..60. An “alarm” is the offending input -- range 30..60. An “alarm” is the offending input -- channel number, followed by the offending value. -- channel number, followed by the offending value. : mon.0...... in[] alarm PROC mon.0 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm) -- Sends an “alarm” if any input is out of the -- range 30..60. An “alarm” is the offending input -- range 30..60. An “alarm” is the offending input -- channel number, followed by the offending value. -- channel number, followed by the offending value. WHILE TRUE ALT i = 0 FOR SIZE in INT x: in[i] ? x IF (x 60) alarm ! i; x alarm ! i; x TRUE SKIP :

52 © P. H. Welch52 Protocols for the Monitors PROTOCOL MY.DATA IS INT: PROTOCOL ALARM.MESSAGE IS INT; INT: offending channel number offending value PROTOCOL LIMITS.MESSAGE IS INT; INT: lower limit upper limit

53 © P. H. Welch53 PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) : PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) WHILE TRUE ALT i = 0 FOR SIZE in INT x: INT x: in[i] ? x in[i] ? x IF IF (x hi) (x hi) alarm ! i; x alarm ! i; x TRUE TRUE SKIP SKIP : mon.1...... in[] alarm limits PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) INT lo, hi: SEQ SEQ limits ? lo; hi limits ? lo; hi WHILE TRUE PRI ALT limits ? lo; hi SKIP SKIP ALT i = 0 FOR SIZE in INT x: INT x: in[i] ? x in[i] ? x IF IF (x hi) (x hi) alarm ! i; x alarm ! i; x TRUE TRUE SKIP SKIP : PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) INT lo, hi: SEQ SEQ limits ? lo; hi limits ? lo; hi ALT is This ALT is nested!

54 © P. H. Welch54 ALTPRIALT Nested ALT s and PRI ALT sALT ALT  ALT ALT The inner ALT disappears!

55 © P. H. Welch55 ALTPRIALT Nested ALT s and PRI ALT sALT ALT i = 0 FOR n ALT ALT ALT... 

56 © P. H. Welch56 ALTPRIALT Nested ALT s and PRI ALT s ALT We could not do this without nested ALTs! ALT ALT i = 0 FOR n ALT... 

57 © P. H. Welch57 PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) : PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) WHILE TRUE ALT i = 0 FOR SIZE in INT x: INT x: in[i] ? x in[i] ? x IF IF (x hi) (x hi) alarm ! i; x alarm ! i; x TRUE TRUE SKIP SKIP : mon.1...... in[] alarm limits PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) INT lo, hi: SEQ limits ? lo; hi WHILE TRUE PRI ALT limits ? lo; hi SKIP SKIP ALT i = 0 FOR SIZE in INT x: INT x: in[i] ? x in[i] ? x IF IF (x hi) (x hi) alarm ! i; x alarm ! i; x TRUE TRUE SKIP SKIP : PROC mon.1 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits) INT lo, hi: SEQ limits ? lo; hi ALT is This ALT is nested!

58 © P. H. Welch58 Guards Pre-Conditioned Guards Any simple guard may be prefixed by a Boolean expression: & ALTPRI ALT When the ALT (or PRI ALT ) starts execution, any pre-conditions on the guards are evaluated. FALSE If a pre-condition turns out to be FALSE, that guarded process is not chosen for execution – even if the (simple) guard becomes ready.

59 © P. H. Welch59 in.0 out in.1 in.2 INT a, b: SEQ... set a and b TIMER tim: INT time.out, x: SEQ tim ? time.out time.out := time.out PLUS 1000 ALT in.0 ? x out ! x in.1 ? x out ! x (a > 0) & in.2 ? x out ! x (b > 0) & tim ? AFTER time.out out ! –1 RUN-TIME DECISION do we listen to the in.2 channel? do we set the time-out?

60 © P. H. Welch60 Protocols for the Monitors PROTOCOL MY.DATA IS INT: PROTOCOL ALARM.MESSAGE IS INT; INT: offending channel number offending value PROTOCOL LIMITS.MESSAGE IS INT; INT: lower limit upper limit PROTOCOL CONTROL.MESSAGE IS INT; BOOL: channel number “on” or “off”

61 © P. H. Welch61 mon.2...... in[] alarm control limits PROC mon.2 ([]CHAN OF MY.DATA in, CHAN OF ALARM.MESSAGE alarm, CHAN OF LIMITS.MESSAGE limits, CHAN OF CONTROL.MESSAGE control, []BOOL ok) -- assume: (SIZE in) = (SIZE ok) INT lo, hi: SEQ... initialise... main cycle... main cycle :

62 © P. H. Welch62 --{{{ main cycle WHILE TRUE PRI ALT limits ? lo; hi SKIP INT line: control ? line; ok[line] control ? line; ok[line] SKIP SKIP--}}} --{{{ main cycle WHILE TRUE PRI ALT--}}} ALT i = 0 FOR SIZE in INT x: INT x: ok[i] & in[i] ? x ok[i] & in[i] ? x IF IF (x hi) (x hi) alarm ! i; x alarm ! i; x TRUE TRUE SKIP SKIP--}}} --{{{ main cycle WHILE TRUE PRI ALT limits ? lo; hi SKIP SKIP--}}} --{{{ initialise SEQ limits ? lo; hi SEQ i = 0 FOR SIZE ok ok[i] := TRUE–– initially listen to all inputs ok[i] := TRUE –– initially listen to all inputs--}}} … in[] alarm control limits mon.2 guard is This guard is pre-conditioned!

63 © P. H. Welch63 INT declaration number of replications first value PAR PAR i = start FOR count Replicators (Summary) INT declaration ALT ALT i = start FOR count number of replications first value

64 © P. H. Welch64 INT declaration number of replications first value SEQ SEQ i = start FOR count Replicators (Summary) INT declaration IF IF i = start FOR count number of replications first value

65 © P. H. Welch65 INT declaration number of replications first value SEQ SEQ i = start FOR count Replicators (Summary) for for (int i = start; i < (start + count); i++) { } In Java or C: Must not change the value of Must not change the value of i

66 © P. H. Welch66 INT declaration IF IF i = start FOR count number of replications first value Replicators (Summary) nested IF -replication This performs a ‘bounded sequential search’ – but needs a nested IF -replication in case the search fails …

67 © P. H. Welch67 IF IF IF i = 0 FOR SIZE a some.condition (a[i])... you found it at index i TRUE... you didn’t find it   ... you didn’t find it    Bounded Sequential Search (occam) a Find the first element of an array, a, that matches some condition …

68 © P. H. Welch68 int i = 0; bool bool found = false; for for (i = 0; i < a.length; i++) { if if (some.condition (a[i])) { found = true; break; } if if (found) {... you found it at index i... you found it at index i } else {... you didn’t find it   ... you didn’t find it    } Bounded Sequential Search (Java/C) a Find the first element of an array, a, that matches some condition …


Download ppt "© P. H. Welch1 Lego-Land ™ Chapter 5. © P. H. Welch2 … some simple networks."

Similar presentations


Ads by Google