© p. h. welch1 applying... chapter 6. © p. h. welch2 integrator component with reset line int.2 in...
TRANSCRIPT
© P. H. Welch 2
Integrator Component with Reset Line
int.2in out
reset
PROC int.2 (CHAN OF REAL64 in, reset, out)PROC int.2 (CHAN OF REAL64 in, reset, out) -- WARNING: reset before use!!!-- WARNING: reset before use!!! REAL64 total:REAL64 total: WHILE TRUEWHILE TRUE PRI ALTPRI ALT reset ? totalreset ? total SKIPSKIP REAL64 x:REAL64 x: in ? xin ? x SEQSEQ total := total + xtotal := total + x out ! totalout ! total::
© P. H. Welch 3
Inertial Navigation Component
in
pos
vel
acc
p.resetv.reset
PROC nav.comp (CHAN OF REAL64 in, v.reset, p.reset,PROC nav.comp (CHAN OF REAL64 in, v.reset, p.reset, acc, vel, pos)acc, vel, pos)
::
© P. H. Welch 4
Inertial Navigation Component
PROC nav.comp (CHAN OF REAL64 in, v.reset, p.reset,PROC nav.comp (CHAN OF REAL64 in, v.reset, p.reset, acc, vel, pos)acc, vel, pos) CHAN OF REAL64 a, b, c:CHAN OF REAL64 a, b, c: PARPAR delta (in, acc, a)delta (in, acc, a) delta (b, vel, c)delta (b, vel, c) int2 (a, v.reset, b)int2 (a, v.reset, b) int.2 (c, p.reset, pos)int.2 (c, p.reset, pos)::
in
pos
vel
acc
p.resetv.reset
a
b cint.2
int.2
© P. H. Welch 5
Memory Cell
write
read
read.req
PROC mem.cell (CHAN OF INT write, PROC mem.cell (CHAN OF INT write, CHAN OF BOOL read.req,CHAN OF BOOL read.req, CHAN OF INT read)CHAN OF INT read) -- WARNING: write before reading!-- WARNING: write before reading! INT x:INT x: WHILE TRUEWHILE TRUE ALTALT write ? x write ? x SKIPSKIP BOOL any:BOOL any: read.req ? anyread.req ? any read ! xread ! x::
© P. H. Welch 6
Asynchronous Communication
• A sends information to B
• A can send at any time (it will never be blocked by B not being ready to receive)
• B can receive data at any time but, first, it has to “request” some (it will never be blocked by A not being able to send)
• The memory cell acts as a common “pool” of information
A B
© P. H. Welch 7
req
in
outprompt
PROC prompt (CHAN OF INT in,PROC prompt (CHAN OF INT in, CHAN OF BOOL req,CHAN OF BOOL req, CHAN OF INT out)CHAN OF INT out) WHILE TRUEWHILE TRUE INT x:INT x: SEQSEQ req ! TRUEreq ! TRUE in ? xin ? x out ! xout ! x::
PROC async (CHAN OF INT in, out)PROC async (CHAN OF INT in, out) CHAN OF INT b:CHAN OF INT b: CHAN OF BOOL a:CHAN OF BOOL a: PARPAR mem.cell (in, a, b)mem.cell (in, a, b) prompt (b, a, out)prompt (b, a, out)::
in
b
aprompt
out
async
© P. H. Welch 8
• A sends information to B asynchronously
• B does not now have to be adapted to request data
A async B
• These issues will be crucial for the proper management of a real-time environment
WARNINGWARNING:: async async buffers buffers two items of data. One of thesetwo items of data. One of these
(that held inside the prompt) may(that held inside the prompt) mayget rather stale!get rather stale!
WARNINGWARNING:: async async buffers buffers two items of data. One of thesetwo items of data. One of these
(that held inside the prompt) may(that held inside the prompt) mayget rather stale!get rather stale!
© P. H. Welch 9
Regular Events
clock (cycle)tick
PROC clock (VAL INT cycle, CHAN OF BOOL tick)PROC clock (VAL INT cycle, CHAN OF BOOL tick) TIMER tim:TIMER tim: INT t:INT t: SEQSEQ tim ? ttim ? t WHILE TRUEWHILE TRUE SEQSEQ t := t PLUS cyclet := t PLUS cycle tim ? AFTER ttim ? AFTER t tick ! TRUEtick ! TRUE::
Run this at Run this at highhigh priority!!priority!!
Run this at Run this at highhigh priority!!priority!!
© P. H. Welch 10
clock (cycle)
regularregulardata flowdata flow
irregularirregulardata flowdata flow
Run all these at high priority
© P. H. Welch 11
Traditional “Ring” Buffer
• buffer has a capacity of max (say). A process may send its data into the buffer until it is full. If it then tries to send more, the source process will get blocked until the buffer gets emptier.
• A process may extract data (by first making a request) until the buffer is empty. If it then request more, the sink gets blocked until the buffer gets some data.
req
in outbuffer
© P. H. Welch 13
req
in outbuffer
PROC buffer (CHAN OF INT in, PROC buffer (CHAN OF INT in, CHAN OF BOOL req,CHAN OF BOOL req, CHAN OF INT out)CHAN OF INT out) [max]INT buff:[max]INT buff: INT lo, hi, size : INT lo, hi, size : -- size = hi – lo-- size = hi – lo SEQSEQ lo, hi, size := 0, 0, 0lo, hi, size := 0, 0, 0 WHILE TRUEWHILE TRUE ALTALT (size < max) & in ? buff[hi](size < max) & in ? buff[hi] SEQSEQ hi := (hi + 1)\maxhi := (hi + 1)\max size := size + 1size := size + 1 BOOL any:BOOL any: (size > 0) & req ? any(size > 0) & req ? any SEQSEQ out ! buff[lo]out ! buff[lo] lo := (lo + 1)\maxlo := (lo + 1)\max size := size – 1size := size – 1::
© P. H. Welch 14
Note:Note: We have to give the reading process the responsibility for making a We have to give the reading process the responsibility for making arequest to the buffer for output. Output guards are not allowed (forrequest to the buffer for output. Output guards are not allowed (forimplementation reasons), despite their semantic power – e.g...implementation reasons), despite their semantic power – e.g...
..
..
..WHILE TRUEWHILE TRUE
ALTALT
(size < max) & in ? buff[hi](size < max) & in ? buff[hi] SEQSEQ hi := (hi + 1)\maxhi := (hi + 1)\max size := size + 1size := size + 1 (size > 0) & out ! buff[lo] (size > 0) & out ! buff[lo] -- not allowed -- not allowed SEQSEQ lo := (lo + 1)\maxlo := (lo + 1)\max size := size – 1size := size – 1......
© P. H. Welch 15
Output guards impose an excessive overhead on the run-time system in orderto manage secure synchronization:-
aa
bb
ALTALT a ? xa ? x b ! nb ! n
ALTALT a ! ma ! m b ? yb ? y
How do we arrange for both processes to agree which communication toperform? It can be carried out, but it’s expensive in execution time.
Ada has (the equivalent to) output guards!
© P. H. Welch 16
NOTE:NOTE:The capacity of The capacity of new.buffernew.buffer
isis (max + 1) (max + 1)
We can always provide a small process to relieve the actual reading processfrom having to request output:
a
in bbuffer prompt
out
new.buffer
PROC new.buffer (CHAN OF INT in, out)PROC new.buffer (CHAN OF INT in, out) CHAN OF BOOL a:CHAN OF BOOL a: CHAN OF INT b:CHAN OF INT b: PARPAR buffer (in, a, b)buffer (in, a, b) prompt(b, a, out)prompt(b, a, out)::
© P. H. Welch 17
NOTE: a parallel implementation is symmetric and much simpler:
id ididnew.buffer
in out
(max + 1)
PROC new.buffer (CHAN OF INT in, out)PROC new.buffer (CHAN OF INT in, out) [max]CHAN OF INT c:[max]CHAN OF INT c: PARPAR id (in, c[0])id (in, c[0]) id (c[max – 1], out)id (c[max – 1], out) PAR i = 0 FOR max – 1PAR i = 0 FOR max – 1 id(c[i], c[i+1])id(c[i], c[i+1])::
c[0] c[1]
© P. H. Welch 18
Exercise:
req
in outoverflow.buffer
error
This is the same as buffer, except that it does not block the sourcewhen it is full. Instead, it outputs a signal on the (BOOL) errorline and discards the incoming datum.
(This type of buffer is of use in a real-time system where it isimportant not to delay the source process if the sink is slow andit is not crucial if we miss some items, so long as we know aboutit!)
© P. H. Welch 19
Exercise (cont.):
Demonstrate by using it asa “type ahead” buffer:
plexoverflow.buffer
slow.process
slow.process translates lower to upper case (and leaves others unchanged).slow.process waits at least one second after inputting a character beforeoutputting. plex rings a bell if it is signalled by overflow.buffer.
© P. H. Welch 20
req
in outoverwriting.buffer
Exercise:
This is also similar to buffer – but it also does not block the source when it is full. It differs from the overflow.buffer in that new incoming data will overwrite the oldest data that has not been output.
(This type of buffer is of use in a real-time system where it isimportant not to delay the source process if the sink is slow andit is not crucial if we miss some items, so long as we know thelatest value.)
© P. H. Welch 21
More Parallel Design Down to “Stateless” Components
• Earlier implementations of int.2int.2 and mem.cellmem.cell retained state information with conventional sequential techniques (i.e. state variables)
• The following implementations retain state information just by the topology (feedback loops) of the connections. The internal components do not themselves retain state. They give a design for hardware implementation.
© P. H. Welch 22
in
c b
outa
d
+
any
reset
int.2
PROC int.2 (CHAN OF REAL64 in, reset, out)PROC int.2 (CHAN OF REAL64 in, reset, out) -- WARNING: reset before use!!!-- WARNING: reset before use!!! CHAN OF REAL64 a, b, c, d:CHAN OF REAL64 a, b, c, d: PARPAR plus(in, d, a)plus(in, d, a) delta (a, out, b)delta (a, out, b) replace (b, reset, c)replace (b, reset, c) REAL64 any:REAL64 any: prefix (any, c, d)prefix (any, c, d)::
scope of anyscope of any
© P. H. Welch 23
in
reset
out
PROC replace (CHAN OF REAL64 in, reset, out)PROC replace (CHAN OF REAL64 in, reset, out) WHILE TRUEWHILE TRUE PRI ALTPRI ALT REAL64 x, any:REAL64 x, any: reset ? x reset ? x -- replace the-- replace the PAR PAR -- next ‘in’-- next ‘in’ in ? any in ? any -- with the-- with the out ! x out ! x -- ‘reset’ value-- ‘reset’ value REAL64 x:REAL64 x: in ? x in ? x -- normally-- normally out ! xout ! x -- just copy through-- just copy through
© P. H. Welch 24
PROC mem.cell (CHAN OF INT write, CHAN OF BOOL read.req,PROC mem.cell (CHAN OF INT write, CHAN OF BOOL read.req, CHAN OF INT read)CHAN OF INT read) -- WARNING: write before reading!!!-- WARNING: write before reading!!! CHAN OF INT a, b, c:CHAN OF INT a, b, c: PARPAR replace (c, write, a)replace (c, write, a) sample (a, b, read.req, read)sample (a, b, read.req, read) INT any:INT any: prefix (any, b, c)prefix (any, b, c)::
anyc b
a
read
read.req
write
scope of anyscope of any
© P. H. Welch 25
ansreq
outin
PROC sample (CHAN OF INT in, out, CHAN OF BOOL req,PROC sample (CHAN OF INT in, out, CHAN OF BOOL req, CHAN OF INT ans)CHAN OF INT ans) WHILE TRUEWHILE TRUE PRI ALTPRI ALT BOOL any:BOOL any: req ? anyreq ? any INT x:INT x: SEQSEQ in ? xin ? x PARPAR ans ! x ans ! x -- duplicate-- duplicate out ! x out ! x -- output-- output INT x:INT x: in ? x in ? x -- normal-- normal out ! x out ! x -- copy-- copy::
© P. H. Welch 26
anyd c
a
read
read.req
writeb
video.out
PROC video.ram.cell (CHAN OF INT write, video.out,PROC video.ram.cell (CHAN OF INT write, video.out, CHAN OF BOOL read.req,CHAN OF BOOL read.req, CHAN OF INT read)CHAN OF INT read) -- WARNING: write before viewing!!!-- WARNING: write before viewing!!! CHAN OF INT a, b, c, d:CHAN OF INT a, b, c, d: PARPAR replace (d, write, a)replace (d, write, a) delta (a, video.out, b)delta (a, video.out, b) sample (b, c, read.req, read)sample (b, c, read.req, read) INT any:INT any: prefix (any, c, d)prefix (any, c, d)::
© P. H. Welch 27
Some More Examples…
• Compiler
• Programming support environment
• Real-time inference engines
• Fast fourier transform
• Shared memory
• Neural nets
© P. H. Welch 28
lex
line.parse
structure.parse
source
tokens
l.tokens
s.tokens.0
s.tokens.1
code.generate
semantic.consistency
t.ident
ident
q.token
mess
name.table
error.reporter
a.ident
compiler
errors
object
© P. H. Welch 29
PROC compiler (CHAN OF BYTE source, errors,PROC compiler (CHAN OF BYTE source, errors, CHAN OF CODE object)CHAN OF CODE object) CHAN OF INT tokens, t.ident:CHAN OF INT tokens, t.ident: CHAN OF SYNTAX s.tokens.0, s.tokens.1:CHAN OF SYNTAX s.tokens.0, s.tokens.1: CHAN OF INT q.token:CHAN OF INT q.token: CHAN OF STRING ident, a.ident:CHAN OF STRING ident, a.ident: CHAN OF ERROR mess:CHAN OF ERROR mess:
PARPAR lex (source, tokens, ident, t.ident)lex (source, tokens, ident, t.ident) parse (tokens, s.tokens.0)parse (tokens, s.tokens.0) semantic.consistency (s.tokens.0, s.tokens.1, mess)semantic.consistency (s.tokens.0, s.tokens.1, mess) code.generate (s.tokens.1, object)code.generate (s.tokens.1, object) name.table (ident, t.ident, q.token, a.ident)name.table (ident, t.ident, q.token, a.ident) error.reporter (mess, q.token, a.ident, errors)error.reporter (mess, q.token, a.ident, errors)::
© P. H. Welch 32
Bayesian Logic
chokes integrator
influences(lower)opinions
(higher)opinion
F0
F1
F2
© P. H. Welch 33
The car is difficult to start
It is difficult to rev the engine
There is a grey deposit on the
spark plugs
There is a lack of power
The fuel consumption is heavy
The exhaust is smoky
The car is backfiring
The car has done a very high mileage
Fuel jets were cleaned with wire
The engine overheats
Fuel is not reaching the engine
Fuel is reaching the float chamber
Fuel is reaching carburettor inlet
Fuel is reaching the fuel pump
Carburettor start jet is blocked
Carburettor main jets are blocked
Weak mix due to incorrect
adjustment
Rich mix due to incorrect adjustment
Fuel pump is faulty
Fuel jets have become enlarged
Pipe from pump to float chamber is blocked
Pipe from pump to tank is blocked
Needle valve in float chamber is faulty
Carburettor fuel mix is too weak
Carburettor fuel mix
is too rich
High fuel level in float chamber
© P. H. Welch 34
PROTOCOL COMPLEX64 IS REAL64; REAL64:PROTOCOL COMPLEX64 IS REAL64; REAL64:
PROC bfly (VAL REAL64 wr, wi, CHAN OF COMPLEX64 a, b, x, y)PROC bfly (VAL REAL64 wr, wi, CHAN OF COMPLEX64 a, b, x, y) WHILE TRUEWHILE TRUE REAL64 ar, ai, br, bi:REAL64 ar, ai, br, bi: SEQSEQ PARPAR a ? ar; aia ? ar; ai b ? br; bib ? br; bi PARPAR x ! ar + br; ai + bix ! ar + br; ai + bi y ! (wr * (ar-br)) – (wi * (ai–bi));y ! (wr * (ar-br)) – (wi * (ai–bi)); (wr * (ai-bi)) – (wi * (ar–br))(wr * (ai-bi)) – (wi * (ar–br))::
a
b
x
y
© P. H. Welch 36
8-Point FFT (Constant Geometry)
00
10
20
30
40
50
60
70
01
11
21
31
41
51
61
71
02
12
22
32
42
52
62
72
03
13
23
33
43
53
63
73
© P. H. Welch 37
VAL REAL64 zero IS 0.0:VAL REAL64 zero IS 0.0:
VAL [4][3]REAL64 real.weight IS [[zero, zero, zero],VAL [4][3]REAL64 real.weight IS [[zero, zero, zero], [1.0, zero, zero],[1.0, zero, zero], [2.0, 2.0, zero],[2.0, 2.0, zero], [3.0, 2.0, zero]]:[3.0, 2.0, zero]]:
[8][4]CHAN OF COMPLEX64 c:[8][4]CHAN OF COMPLEX64 c:
PAR i = 0 FOR 4PAR i = 0 FOR 4 PAR j = 0 FOR 3PAR j = 0 FOR 3 bfly (real.weight[i][j], zero, c[i][j],bfly (real.weight[i][j], zero, c[i][j], c[i + 4][j],c[i + 4][j], c[2 * i][j + 1],c[2 * i][j + 1], c[(2 * i) + 1][j + 1])c[(2 * i) + 1][j + 1])
8-Point FFT (Constant Geometry)
© P. H. Welch 38
Parallel Computation on Global Data Structures
Shared Memory!
X = (X0, X1, X2,..., Xi, ..., Xn-1)
X’ = (X’0, X’1, X’2,..., X’i, ..., X’n-1)
Each component can be updated independently, but requires knowledgeof the whole structure:
X’i = Pi(X0,..., Xn-1)
• modelling (e.g. “n-body”)• iterative numerics (e.g. Gauss-Jacobi)
© P. H. Welch 41
Global DataX X’
...
X’1
XP1
X’n-1
XPn-1
X X
...
•broadcast global data•return updated components
X’0
XP0
X’n-1X’1X’0
X ...
© P. H. Welch 42
This scheme isnot directly
implementable!
For the correct mechanism,see the “n-body” problem
and “geometric distribution”in Chapter 2
© P. H. Welch 44
Neural Networks
• In the neural net example, each node computes a weighted sum of the data held by all nodes in the layer above.
• In each layer, compute these “global” sums for each node and then drop the answers down to the next layer.
• Use a ring mechanism in each layer to broadcast and compute on this data in parallel.
© P. H. Welch 47
The Dining Philosophers
Once upon a time, five philosophers lived in the same college. They were proud, independent philosophers who though independent thoughts and never communicated with each other what these thoughts might have been.
From time to time, each philosopher would get hungry. He/she would then stop thinking and go to the single dining room in the college – this had to be shared by all the philosophers.
© P. H. Welch 48
The dining room contained one circular table, around which were symmetrically placed five chairs. Each chair was labelled with the name of one of the philosophers and each philosopher was only allowed to sit in her/his own chair.
Opposite each chair was a plate and, on the left, was laid a golden fork. In the centre of the table was a large bowl of spaghetti, which was constantly replenished.
F4
F3 F2
F1
F0
P0P1
P2
P3
P4
© P. H. Welch 49
The philosophers never managed to master the art of serving, or indeed, eating the spaghetti with a single fork.
Consequently, they always tried to pick up both forks on either side of their plates.
If a fork was being used by a neighbouring philosopher, the hungry philosopher politely waited for the neighbour to finish eating.
This was the only occasion when the existence of one philosopher had an impact on another!
© P. H. Welch 50
The philosophers lived like this for years and years until, one day, something most unfortunate happened.
By chance, all the philosophers got hungry at the same time, went to the dining room, sat down and reached for the forks.
By further chance, each philosopher picked up the fork on his/her left. Noticing that the other fork was being used, each philosopher waited for the neighbour to finish.
And waited ... and waited … ... and starved to death!
MORAL: philosophersought to talk to each other
a bit more
© P. H. Welch 51
philosopherright left
PROC philosopher (CHAN OF BOOL left, right)PROC philosopher (CHAN OF BOOL left, right) WHILE TRUEWHILE TRUE SEQSEQ ... think... think PAR PAR -- pick up forks-- pick up forks left ! TRUEleft ! TRUE right ! TRUEright ! TRUE ... eat... eat PAR PAR -- put down forks-- put down forks left ! TRUEleft ! TRUE right ! TRUEright ! TRUE::
The philosopher’s only point of contact with the rest of the world is when picking up and replacing the forks … he/she might find one or both not there … from which the presence of others may be deduced!
© P. H. Welch 52
forkleft right
PROC fork (CHAN OF BOOL left, right)PROC fork (CHAN OF BOOL left, right) WHILE TRUEWHILE TRUE BOOL any:BOOL any: ALTALT left ? any left ? any -- p.left picks up -- p.left picks up left ? any left ? any -- p.left puts down -- p.left puts down right ? any right ? any -- p.right picks up -- p.right picks up right ? any right ? any -- p.right puts down -- p.right puts down ::
Once a fork has been “picked up” by a philosopher (say on itsleft), it waits to be “put down” by that philosopher (i.e., on itsleft) and cannot be “picked up” by the other philosopher inthe meantime.
Note: fork is a binary semaphore
© P. H. Welch 54
PROC college ()PROC college () [5] CHAN OF BOOL left, right:[5] CHAN OF BOOL left, right: PAR i = 0 FOR 5PAR i = 0 FOR 5 PARPAR philosopher (left[i], right[i])philosopher (left[i], right[i]) fork (left[i], right[(i+1)\5])fork (left[i], right[(i+1)\5])::
© P. H. Welch 55
1.1. Buy one extra fork:Buy one extra fork:
The philosophers are very jealous and would not tolerate one of their number having more resources (e.g., a private fork) than the others!
2.2. Buy five extra forks:Buy five extra forks:
Too expensive! The college is suffering from government cut-backs and the forks are made of gold!!
3.3. One of the philosophers picks up the right fork first:One of the philosophers picks up the right fork first:
Asymmetric solution. The philosophers do not talk to each other so it is difficult to arrange! Cannot impose decision because of jealously problem!!
Ways to avoid this deadlock ...
© P. H. Welch 56
4.4. External authority:External authority:
College hires a securitysecurity guard to whom each philosopher has to report when she wants to sit down at or stand up from the table.
The securitysecurity guard has instructions not to allow more than four philosophers at a time to sit down.
This solution is acceptable because it is symmetric (the philosophers still have equal, though reduced, rights) and is cheap (salaries are peanuts compared with the cost of extra forks).
Ways to avoid this deadlock...
© P. H. Welch 57
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
© P. H. Welch 58
philosopherright left
down up
PROC philosopher (CHAN OF BOOL left, rightPROC philosopher (CHAN OF BOOL left, right, down, up, down, up)) WHILE TRUEWHILE TRUE SEQSEQ ... think... think down ! TRUEdown ! TRUE -- get permission to sit-- get permission to sit PAR PAR -- pick up forks-- pick up forks left ! TRUEleft ! TRUE right ! TRUEright ! TRUE ... eat... eat PAR PAR -- put down forks-- put down forks left ! TRUEleft ! TRUE right ! TRUEright ! TRUE up ! TRUEup ! TRUE -- notify security that-- notify security that -- you have finished-- you have finished::
© P. H. Welch 59
PROC security ([]CHAN OF BOOL down, up)PROC security ([]CHAN OF BOOL down, up) VAL INT max IS 4:VAL INT max IS 4: INT n.sat.down:INT n.sat.down: SEQSEQ n.sat.down := 0n.sat.down := 0 WHILE TRUEWHILE TRUE BOOL any:BOOL any: ALT i = 0 FOR 5ALT i = 0 FOR 5 ALTALT (n.sat.down < max) & down[i] ? any(n.sat.down < max) & down[i] ? any n.sat.down := n.sat.down + 1n.sat.down := n.sat.down + 1 up[i] ? anyup[i] ? any n.sat.down := n.sat.down – 1n.sat.down := n.sat.down – 1::
securitysecurity
u[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]
© P. H. Welch 60
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
© P. H. Welch 61
PROC secure.college ()PROC secure.college () [5]CHAN OF BOOL left, right:[5]CHAN OF BOOL left, right: [5]CHAN OF BOOL up, down:[5]CHAN OF BOOL up, down: PARPAR security (down, up)security (down, up) PAR i = 0 FOR 5PAR i = 0 FOR 5 PARPAR philosopher (left[i], right[i]philosopher (left[i], right[i],, down[i], up[i]down[i], up[i])) fork (left[i], right [(i+1)\5])fork (left[i], right [(i+1)\5])::
© P. H. Welch 62
The potential for deadlock in collegecollege was not obvious to its designers.
The claim that there is no such potential withinsecure.collegesecure.college should not be acceptedlightly.
VERIFYVERIFY
We must provide a formal proof of We must provide a formal proof of the absence of deadlock in any safety-the absence of deadlock in any safety-
critical application. Systematic critical application. Systematic validation through “exhaustive” validation through “exhaustive”
testing in unacceptable!testing in unacceptable!
© P. H. Welch 63
Lemma:Lemma: a deadlocked network will contain a cycle of a deadlocked network will contain a cycle of processes with each process in the cycle blocked trying to processes with each process in the cycle blocked trying to communicate with the next node in the cycle.communicate with the next node in the cycle.
Lemma:Lemma: a deadlocked network will contain a cycle of a deadlocked network will contain a cycle of processes with each process in the cycle blocked trying to processes with each process in the cycle blocked trying to communicate with the next node in the cycle.communicate with the next node in the cycle.
DEF (informal): DEADLOCKDEF (informal): DEADLOCK
A network of processes is deadlocked when every process is A network of processes is deadlocked when every process is blocked trying to communicate with other processes blocked trying to communicate with other processes within within that networkthat network..
If any process within the network is blocked If any process within the network is blocked on an external on an external communicationcommunication, it is willing to accept that external , it is willing to accept that external communication – and the network is not deadlocked.communication – and the network is not deadlocked.
If any process within the network is blocked If any process within the network is blocked on an external on an external communicationcommunication, it is willing to accept that external , it is willing to accept that external communication – and the network is not deadlocked.communication – and the network is not deadlocked.
A deadlocked network A deadlocked network refusesrefuses all external communications. all external communications.A deadlocked network A deadlocked network refusesrefuses all external communications. all external communications.
© P. H. Welch 64
If any process within the network is blocked on a timeout, If any process within the network is blocked on a timeout, that process will eventually continue – and the network is that process will eventually continue – and the network is not deadlocked.not deadlocked.
If any process within the network is blocked on a timeout, If any process within the network is blocked on a timeout, that process will eventually continue – and the network is that process will eventually continue – and the network is not deadlocked.not deadlocked.
A network of processes is deadlocked when every process is A network of processes is deadlocked when every process is blocked trying to communicate with other processes blocked trying to communicate with other processes within within that networkthat network..
DEF (informal): DEADLOCKDEF (informal): DEADLOCK
© P. H. Welch 65
collegecollege r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F F
!!!!
!!!!
!!!!
!!!!!!!!
????
????????
????
????
This college This college may deadlock may deadlock
This college This college may deadlock may deadlock
Note the cycle of blocked Note the cycle of blocked communicationscommunications
Note the cycle of blocked Note the cycle of blocked communicationscommunications
© P. H. Welch 66
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
What about What about this one?this one?
What about What about this one?this one?
© P. H. Welch 67
The claim that there is no such potential withinsecure.collegesecure.college should not be acceptedlightly.
ASSUME: ASSUME: secure.collegesecure.college is deadlocked … is deadlocked …
In that case, all its processes – each In that case, all its processes – each philosopherphilosopher, each , each forkfork and and the the securitysecurity guard are blocked. Where might they be? guard are blocked. Where might they be?In that case, all its processes – each In that case, all its processes – each philosopherphilosopher, each , each forkfork and and the the securitysecurity guard are blocked. Where might they be? guard are blocked. Where might they be?
The The securitysecurity guard can only be in one place – blocked on its guard can only be in one place – blocked on its ALTALT, waiting for a , waiting for a philosopherphilosopher to enter/leave the dining room. to enter/leave the dining room. The The securitysecurity guard can only be in one place – blocked on its guard can only be in one place – blocked on its ALTALT, waiting for a , waiting for a philosopherphilosopher to enter/leave the dining room. to enter/leave the dining room.
Each Each forkfork is either on the table or in the hands of one of its is either on the table or in the hands of one of its neighbouring philosophers.neighbouring philosophers.Each Each forkfork is either on the table or in the hands of one of its is either on the table or in the hands of one of its neighbouring philosophers.neighbouring philosophers.
© P. H. Welch 68
The claim that there is no such potential withinsecure.collegesecure.college should not be acceptedlightly.
ASSUME: ASSUME: secure.collegesecure.college is deadlocked … is deadlocked …
In that case, all its processes – each In that case, all its processes – each philosopherphilosopher, each , each forkfork and and the the securitysecurity guard are blocked. Where might they be? guard are blocked. Where might they be?In that case, all its processes – each In that case, all its processes – each philosopherphilosopher, each , each forkfork and and the the securitysecurity guard are blocked. Where might they be? guard are blocked. Where might they be?
EachEach philosopherphilosopher could be in one of several places – thinking, could be in one of several places – thinking, trying to get past trying to get past securitysecurity, trying to pick up its , trying to pick up its forkforks, eating, s, eating, trying to put down its forks or trying to leave the dining room trying to put down its forks or trying to leave the dining room (i.e. telling (i.e. telling securitysecurity that it’s leaving). that it’s leaving).
EachEach philosopherphilosopher could be in one of several places – thinking, could be in one of several places – thinking, trying to get past trying to get past securitysecurity, trying to pick up its , trying to pick up its forkforks, eating, s, eating, trying to put down its forks or trying to leave the dining room trying to put down its forks or trying to leave the dining room (i.e. telling (i.e. telling securitysecurity that it’s leaving). that it’s leaving).
© P. H. Welch 69
PROC philosopher (CHAN OF BOOL left, rightPROC philosopher (CHAN OF BOOL left, right, down, up, down, up)) WHILE TRUEWHILE TRUE SEQSEQ ... think... think down ! TRUEdown ! TRUE -- get permission to sit-- get permission to sit PAR PAR -- pick up forks-- pick up forks left ! TRUEleft ! TRUE right ! TRUEright ! TRUE ... eat... eat PAR PAR -- put down forks-- put down forks left ! TRUEleft ! TRUE right ! TRUEright ! TRUE up ! TRUEup ! TRUE -- notify security that-- notify security that -- you have finished-- you have finished::
Can’t get stuck here!Can’t get stuck here!Can’t get stuck here!Can’t get stuck here!
philosopherright left
down up
Four must get past here …Four must get past here …
© P. H. Welch 70
The claim that there is no such potential withinsecure.collegesecure.college should not be acceptedlightly.
ASSUME: ASSUME: secure.collegesecure.college is deadlocked … is deadlocked …
In that case, all its processes – each In that case, all its processes – each philosopherphilosopher, each , each forkfork and and the the securitysecurity guard are blocked. Where might they be? guard are blocked. Where might they be?In that case, all its processes – each In that case, all its processes – each philosopherphilosopher, each , each forkfork and and the the securitysecurity guard are blocked. Where might they be? guard are blocked. Where might they be?
Therefore, oneTherefore, one philosopherphilosopher must be stuck trying to get past must be stuck trying to get past securitysecurity. The other four must be in the dining room, trying to . The other four must be in the dining room, trying to pick up their pick up their forkforks. No s. No philosopherphilosopher can have picked up can have picked up bothboth forkforks.s.
Therefore, oneTherefore, one philosopherphilosopher must be stuck trying to get past must be stuck trying to get past securitysecurity. The other four must be in the dining room, trying to . The other four must be in the dining room, trying to pick up their pick up their forkforks. No s. No philosopherphilosopher can have picked up can have picked up bothboth forkforks.s.
© P. H. Welch 71
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
Suppose the top Suppose the top philosopher is philosopher is not at the tablenot at the table
Suppose the top Suppose the top philosopher is philosopher is not at the tablenot at the table
P P
PP
© P. H. Welch 72
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
P P
PP
Philosophers 1 Philosophers 1 and 4 must get and 4 must get the top forksthe top forks
Philosophers 1 Philosophers 1 and 4 must get and 4 must get the top forksthe top forks
forkforkforkfork
Philosophers 1 Philosophers 1 and 4 can’t get and 4 can’t get
their other forkstheir other forks
Philosophers 1 Philosophers 1 and 4 can’t get and 4 can’t get
their other forkstheir other forks
© P. H. Welch 73
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
P P
PP
Philosophers 2 Philosophers 2 and 3 must have and 3 must have
them …them …
Philosophers 2 Philosophers 2 and 3 must have and 3 must have
them …them …
forkforkforkfork
Philosophers 2 Philosophers 2 and 3 can’t get and 3 can’t get
their other forkstheir other forks
Philosophers 2 Philosophers 2 and 3 can’t get and 3 can’t get
their other forkstheir other forks
forkforkforkfork
© P. H. Welch 74
secure.secure.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]
P P
PP
P
F
F
F
F Fu[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]security
P P
PP
But one of them But one of them will …will …
But one of them But one of them will …will …
forkforkforkfork
Philosophers 2 Philosophers 2 and 3 can’t get and 3 can’t get
their other forkstheir other forks
Philosophers 2 Philosophers 2 and 3 can’t get and 3 can’t get
their other forkstheir other forks
forkforkforkfork
CONTRADICTION: CONTRADICTION: the initial assumption the initial assumption
(of deadlock) is (of deadlock) is impossible !!!impossible !!!
Q.E.DQ.E.D
CONTRADICTION: CONTRADICTION: the initial assumption the initial assumption
(of deadlock) is (of deadlock) is impossible !!!impossible !!!
Q.E.DQ.E.D
forkfork
© P. H. Welch 75
Exercise:
Provide some links from secure.collegesecure.college to the outside world and animate aninteractive demonstration of life inside.
Modify the forkfork process so that it guarantees service on each input – even if the other one is perpetually busy. No philosopher must starve because of greedy colleagues!
© P. H. Welch 76
reporting.reporting.collegecollege
r[0] l[0]
l[4]
l[3]
l[2]
l[1]r[4]
r[3]
r[2]
r[1]u[0]u[0] d[0]d[0]
u[1]u[1]
d[1]d[1]
u[2]u[2]d[2]d[2]u[3]u[3]
d[3]d[3]
d[4]d[4]
u[4]u[4]
P
PP
P
P
F F
F
F
F
security
© P. H. Welch 77
Systolic Arrays
Multiplication of two arrays is a common operation incomputational engineering:
Assume we have:
an array B which is m n
and:
an array C which is n l
and we want: A = B*C.
Then, A[i][j] is equal to the sum for k = 0 to n-1 of
B[i][k] * C[k][j]
© P. H. Welch 78
PROC node (VAL INT n, PROC node (VAL INT n, CHAN OF REAL64 hl, hr, vu, vdCHAN OF REAL64 hl, hr, vu, vd )) REAL64 sum:REAL64 sum: SEQSEQ sum := 0.0sum := 0.0 SEQ k = 0 FOR nSEQ k = 0 FOR n REAL64 x, y:REAL64 x, y: SEQSEQ PARPAR hl ? xhl ? x vu ? yvu ? y PARPAR sum := sum + (x * y)sum := sum + (x * y) hr ! xhr ! x vr ! yvr ! y::
The array multiplication can be carried out rapidly with multiple copies of the following module:
hl
vd
vu
hrnode
© P. H. Welch 79
Systolic Arrays
We create a systolic array of the nodes, and pump theelements of B and C through:
...
...
...
...
... a03 a02 a01 a00
... a12 a11 a10 0
... a21 a20 0 0
... a30 0 0 0
...b30
b20
b10
b00
...b21
b11
b01
0
...b12
b02
00
...b03
000
© P. H. Welch 80
Systolic Arrays
If the array of nodes is implemented in a VLSI device,all nodes can be executing at the same time. After aboutn steps, each node (i, j) will contain Ai,j; this is considerably faster than the m n l steps thatwould be required if you did each operation sequentially.
Exercise: Develop a complete program, using the basic node, to carry out a matrix multiplication.
Options:• leave the computed elements in the nodes• move the complete elements to the edge of the array