Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in Occam-pi: A Tutorial By: Zain-ul-Abdin

Similar presentations


Presentation on theme: "Programming in Occam-pi: A Tutorial By: Zain-ul-Abdin"— Presentation transcript:

1 Programming in Occam-pi: A Tutorial By: Zain-ul-Abdin

2 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 2 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

3 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 3 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).

4 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 4 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)

5 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 5 Processes 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? myProcess

6 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 6 Processes 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). myProcess

7 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 7 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

8 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 8 Synchronized Communication- cont’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

9 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 9 Synchronized Communication- cont’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. c A A B B c ! x x c ? y y

10 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 10 Types Primitive types –INT, BYTE, BOOL –INT16, INT32, INT64 –REAL32, REAL64 Arrays types (indexed from 0) –[100]INT –[32][32][8]BYTE –[50]REAL64

11 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 11 Operators +, -, *, /, \ PLUS, MINUS, TIMES +, -, *, /, \ =, > =, <> /\(and), \/(or), > >(rshift), <<(lshift) ~ (not) INTxx, INTxx → INTxx BYTE, BYTE → BYTE REALxx, REALxx → REALxx INTxx, INTxx → BOOL BYTE, BYTE → BOOL REALxx, REALxx → BOOL *, * → BOOL INTxx, INTxx → INTxx BYTE, BYTE → BYTE INTxx → INTxx BYTE → BYTE Precision must be matched Types must match

12 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 12 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.

13 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 13 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 Channel declarations INT a, b: [max]INT c: [max]BYTE d: CHAN BYTE p: [max<<2]CHAN INT q:

14 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 14 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.

15 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 15 Process Abstractions PROC foo (VAL []BYTE s, VAL BOOL mode, INT result, CHAN INT in?, out!, CHAN BYTE pause?)... : foo(s, mode, result) pauseout in

16 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 16 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

17 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 17 Structured Processes- SEQ and PAR SEQ in ? sum in ? x sum := sum + x out ! Sum PAR in0 ? a in1 ? b out ! a + b c := a + (2*b) Processes can run in any order, No data race hazards

18 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 18 Structured Processes- IF and WHILE IF x > 0 screen ! 'p‘ x < 0 screen ! 'n' TRUE screen ! 'z' WHILE TRUE INT x: SEQ in ? x out ! 2*x

19 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 19 Structured Processes- PROC instance PROC octople (CHAN INT in?, out!) CHAN INT a, b: PAR double (in?, a!) double (a?, b!) double (b?, out!) :

20 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 20 Example - Integrate PROC integrate (CHAN INT in?, out!) INT total: SEQ total := 0 WHILE TRUE INT x: SEQ in ? x total := total + x out ! total : Sequential implementation

21 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 21 Example - Integrate PROC integrate (CHAN INT in?, out!) CHAN INT a, b, c: PAR delta (a?, out!, b!) prefix (0, b?, c!) plus (in?, c?, a!) : Parallel implementation

22 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 22 Example – Matrix Multiplication

23 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 23 Tool Set KROC- Kent Retargetable Occam Compiler

24 Mobility in occam-pi

25 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 25 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. c A A B B c ! x x c ? y y SAFE but SLOW

26 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 26 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: [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.] {,, … } x0 := x1 {,, … }

27 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 27 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). c A A B B c ! x x c ? y y UNSAFE but FAST

28 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 28 Reference Semantics c A A B B xy c ! xc ? y HEAP before

29 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 29 Reference Semantics c A A B B xy c ! xc ? y HEAP after

30 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 30 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.

31 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 31 Mobile Assignment Mobile semantics differs in one crucial respect: 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. {,, … } x0 := x1 {,, … }

32 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 32 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 CHAN OF FOO c: PAR c ! x1 c ? x0 equals

33 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 33 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:

34 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 34 Mobile data – cont’d Mobiles are safe references Assignment and communication with reference semantics Only one process may hold a given mobile

35 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 35 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

36 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 36 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

37 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 37 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 ? a d ? b Generator P Q c d a b

38 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 38 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...

39 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 39 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

40 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 40 Dynamic Process Creation PROC A ()... local state SEQ FORKING SEQ FORK P(n, svr, cli)... : VAL data is copied into a FORKed process MOBILE data and channel-ends are moved into a FORKed process

41 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 41 Client-Server Application CS response request P

42 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin 42 Client-Server Application Fault-tolerance by Replication CS response request P BS


Download ppt "Programming in Occam-pi: A Tutorial By: Zain-ul-Abdin"

Similar presentations


Ads by Google