Programming in occam-pi -

advertisement
Programming in Occam-pi:
A Tutorial
By: Zain-ul-Abdin
[email protected]
Outline
• Occam-pi basics
–
–
–
–
–
Networks and communication
Types, channels, processes ...
Primitive Processes
Structured Processes
Examples: Integration, Matrix Multiplication
• Mobility
–
–
–
–
–
Mobile Semantics
Mobile Assignment
Mobile Communication
FORKing Processes
Example: Dynamic Process Creation
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
2
CSP
• CSP deals with processes, networks of
processes and various forms of
synchronisation / communication between
processes.
• A network of processes is also a process - so
CSP naturally accommodates layered network
structures (networks of networks).
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
3
Occam-pi Introduction
• Named after Occam Razor ”If you have two equally
likely solutions to a problem, choose the simplest”
• Combines the concepts of CSP with pi-calculus
• Parallel processing language that simplifies the
description of systems
• Allows composing simple components to build
complex systems
Semantics [A+B] = Semantics [A] + Semantics [B]
• Built in semantics for concurrency
– Processes
– Channels (Unbuffered message passing)
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
4
Processes
myProcess
• A process is a component that encapsulates some data
structures and algorithms for manipulating that data.
• Both its data and algorithms are private. The outside
world can neither see that data nor execute those
algorithms! [It is not an object.]
• The algorithms are executed by the process in its own
thread (or threads) of control.
• So, how does one process interact with another?
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
5
Processes
myProcess
• The simplest form of interaction is synchronised
message-passing along channels.
• The simplest forms of channel are zero-buffered and
point-to-point (i.e. wires).
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
6
Synchronized Communication
• Output value down the channel out
• This operation does not complete until the
process at the other end of the channel inputs
the information
• Until that happens, the outputting process waits
out ! value
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
7
Synchronized Communicationcont’d
• Input the next piece of information from channel
in into the variable x
• This operation does not complete until the
process at the other end of the channel outputs
the information
• Until that happens, the inputting process waits
in ? x
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
8
Synchronized Communicationcont’d
• A may write on c at any time, but has to wait for a read.
• B may read from c at any time, but has to wait for a
write.
y
x
A
c!x
c
B
c?y
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
9
Types
• Primitive types
– INT, BYTE, BOOL
– INT16, INT32, INT64
– REAL32, REAL64
• Arrays types (indexed from 0)
– [100]INT
– [32][32][8]BYTE
– [50]REAL64
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
10
Operators
• +, -, *, /, \
PLUS, MINUS, TIMES
INTxx, INTxx → INTxx
BYTE, BYTE → BYTE
• +, -, *, /, \
• <, <=, >=, >
REALxx, REALxx → REALxx
INTxx, INTxx → BOOL
BYTE, BYTE → BOOL
REALxx, REALxx → BOOL
• =, <>
*, * → BOOL
• /\(and), \/(or),
>< (xor), >>(rshift),
<<(lshift)
INTxx, INTxx → INTxx
BYTE, BYTE → BYTE
INTxx → INTxx
BYTE → BYTE
• ~ (not)
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
Precision
must be
matched
Types must
match
11
Expressions
• No auto-coercion happens between types: if x
is a REAL32 and i is an INT, then x + i is illegal
...
• Where necessary, explicit casting between types
must be programmed: e.g. x + (REAL32 i) ...
• No precedence is defined between operators,
we must use brackets: e.g. a + (b*c) ...
• The operators +, -, * and / trigger run-time errors
if their results overflow.
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
12
Declarations
• All declarations end in a colon
• A declaration cannot be used before it is made
...
• Declarations are aligned at the same level of
indentation ...
• Variable declarations
INT a, b:
[max]INT c:
[max]BYTE d:
• Channel declarations
CHAN BYTE p:
[max<<2]CHAN INT q:
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
13
An Occam-pi Process
• Syntactically, an occam-pi process consists of:
– an optional sequence of declarations (e.g. values,
variables, channels)
– a single executable process
• All the declarations – and the executable – are
aligned at the same level of indentation.
• The executable process is either primitive or
structured.
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
14
Process Abstractions
in
PROC foo (VAL []BYTE s,
VAL BOOL mode,
INT result,
CHAN INT in?, out!,
CHAN BYTE pause?)
...
:
foo(s, mode, result)
pause
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
out
15
Primitive Processes
• Assignment
a := c[2] + b
• Input (synchronising)
in ? a
• Output (synchronising)
out ! a + (2*b)
• Null (do nothing)
SKIP
• Timer (increments every millisec.)
TIMER tim
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
16
Structured ProcessesSEQ and PAR
SEQ
in ? sum
in ? x
sum := sum + x
out ! Sum
Processes can
run in any order,
No data race
hazards
PAR
in0 ? a
in1 ? b
out ! a + b
c := a + (2*b)
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
17
Structured ProcessesIF and WHILE
IF
x > 0
screen ! 'p‘
x < 0
screen ! 'n'
TRUE
screen ! 'z'
WHILE TRUE
INT x:
SEQ
in ? x
out ! 2*x
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
18
Structured ProcessesPROC instance
PROC octople (CHAN INT in?,
out!)
CHAN INT a, b:
PAR
double (in?, a!)
double (a?, b!)
double (b?, out!)
:
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
19
Example - Integrate
• Sequential implementation
PROC integrate (CHAN INT in?, out!)
INT total:
SEQ
total := 0
WHILE TRUE
INT x:
SEQ
in ? x
total := total + x
out ! total
:
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
20
Example - Integrate
• Parallel implementation
PROC integrate (CHAN INT in?, out!)
CHAN INT a, b, c:
PAR
delta (a?, out!, b!)
prefix (0, b?, c!)
plus (in?, c?, a!)
:
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
21
Example – Matrix Multiplication
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
22
Tool Set
• KROC- Kent Retargetable Occam
Compiler
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
23
Mobility in occam-pi
Copy Semantics
• Classical occam: data is copied from the workspace of
A into the workspace of B. Subsequent work by A on its
x variable and B on its y variable causes no mutual
interference.
y
x
A
c
c!x
B
c?y
SAFE but SLOW
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
25
Copy Assignment
• As assignment changes the state of its environment,
which we can represent by a set of ordered pairs
mapping variables into data values.
• In occam, because of its zero-tolerance of aliasing,
assignment semantics is what we expect:
{ <x 0 , v0 >, <x 1 , v1 >, … } x0 := x1 { <x 0, v 1 >, <x 1, v 1 >, … }
• [In all other languages with assignment, the semantics
are much more complex - since the variable x0 may be
aliased to other variables … and the values associated
with those aliases will also have changed to v .]
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
26
Reference Semantics
• Java/JCSP: references to data (objects) are copied from
the workspace of A into B. Subsequent work by A on its
x variable and B on its y variable causes mutual
interference (race hazard).
y
x
A
c
c!x
B
c?y
UNSAFE but FAST
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
27
Reference Semantics
HEAP
before
x
y
A
c!x
c
B
c?y
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
28
Reference Semantics
HEAP
after
x
y
A
c!x
c
B
c?y
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
29
Movement Semantics
Consider copy and move operations on files …
• copy duplicates the file, placing the copy in the
target directory under a (possibly) new name.
• move moves the file to the target directory,
possibly renaming it:
– the original file can no longer be found at the source
address;
– it has moved.
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
30
Mobile Assignment
Mobile semantics differs in one crucial respect:
{ <x 0 , v0 >, <x 1 , v1 >, … } x0 := x1 { <x 0, v 1 >, <x 1, ?? >, … }
• The value of the variable at the source of the
assignment has become undefined - its value
has moved to the target variable.
• It must be noted: mobile semantics is
strictly weaker than copy semantics.
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
31
Mobile Communication
• The semantics for assignment and communication are
directly related. In occam, communication is just a
distributed form of assignment - a value computed in the
sending process is assigned to a variable in the
receiving one (after the two processes have
synchronised).
• For example, if x0 and x1 were of type FOO, we have
the semantic equivalence:
x0 := x1
equals
CHAN OF FOO c:
PAR
c ! x1
c ? x0
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
32
Mobile data Syntax
• Occam-pi has a new keyword –
a MOBILE qualifier for data types/variables
• The MOBILE qualifier doesn’t change the semantics of
types as types. For example, MOBILE types are
compatible with ordinary types in expressions.
• But it imposes the mobile semantics on assignment
and communication between MOBILE variables.
• The MOBILE qualifier need not be burnt into the type
declaration - it can be associated just with particular
variables.
MOBILE INT x0, x1:
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
33
Mobile data – cont’d
• Mobiles are safe references
• Assignment and communication with
reference semantics
• Only one process may hold a given mobile
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
34
Mobile Channel Type
• In occam programs, channels are fixed in place
at compile time
• What if we want to reconnect the process
network at runtime?
• A channel type is a bundle of one or more
related channels: for example, the set of
channels connecting a client and a server
• Note this has to be a CHAN TYPE, else you can’t
put channels in it
• Channel direction specifiers are mandatory
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
35
Mobile Channel Bundles
• Defined as ‘client’ and ‘server’ ends of a “mobile
channel-type”, e.g.:
• Directions specified in the type are server-relative
– Channel ends inside channel type ends can be used
like regular channels
CHAN TYPE INT.IO
MOBILE RECORD
CHAN INT in?:
CHAN INT out!:
:
INT.IO! cli:
SEQ
cli[in] ! 42
cli[out] ? r
INT.IO? svr:
SEQ
svr[in] ? x
svr[out] ! x+1
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
36
Communicating Mobile
Channels
• The main use of mobile channel bundles is to support
the run-time reconfiguration of process networks
• P and Q are now directly connected
• Process networks may be arbitrarily reconfigured using
mobile channels
– dynamic process creation makes this much more interesting
c
Generator
d
c ? a
P
a
b
d ? b
Q
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
37
Dynamic Process Creation
• N-replicated PAR, where the replicator count is a
constant
• The creating process has to wait for them all to
terminate before creating process has to wait for
them all to terminate before it can do anything
else.
VAL N IS 10:
SEQ
PAR i = 1 FOR N
...
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
38
Dynamic Process Creation
• Asynchronous process invocation using FORK
• Essentially a procedure instance that runs in parallel with
the invoking process
• Parameter passing is strictly uni-directional and uses a
communication semantics
• occam-pi introduces two new keywords – FORKING and
FORK
• Inside a FORKING block, you can use FORK at any time
to spawn a new process
• When the FORKING block exits, it’ll wait for all the
spawned processes to finish
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
39
Dynamic Process Creation
PROC A ()
... local state
SEQ
......
VAL data is
copied into a
FORKING
FORKed process
SEQ
......
FORK P(n, svr, cli)
...
...
:
MOBILE data and
channel-ends are
moved into a
FORKed process
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
40
Client-Server Application
S
response
request
C
P
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
41
Client-Server Application
• Fault-tolerance by Replication
S
response
request
C
P
B
S
"Programming in Occam-pi: A Tutorial", Zain-ul-Abdin
42
Download