1 / 42

Programming in Occam-pi: A Tutorial

Programming in Occam-pi: A Tutorial. By: Zain-ul-Abdin Zain-ul-Abdin@hh.se. Outline. Occam-pi basics Networks and communication Types, channels, processes ... Primitive Processes Structured Processes Examples: Integration, Matrix Multiplication Mobility Mobile Semantics

ann-house
Download Presentation

Programming in Occam-pi: A Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming in Occam-pi:A Tutorial By: Zain-ul-Abdin Zain-ul-Abdin@hh.se

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

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

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

  7. Synchronized Communication • Output valuedown 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

  8. Synchronized Communication-cont’d • Input the next piece of information from channel ininto 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

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

  11. Precision must be matched Types must match Operators • +, -, *, /, \ PLUS, MINUS, TIMES • +, -, *, /, \ • <, <=, >=, > • =, <> • /\(and), \/(or), >< (xor), >>(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 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

  12. Expressions • No auto-coercionhappens between types: if xis a REAL32and iis 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

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

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

  17. Processes can run in any order, No data race hazards 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) "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 "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!) : "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 "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 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

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

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

  24. Mobility in occam-pi

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

  26. { <x , v >, <x , v>, … }x0 := x1{ <x , v >, <x , v >, … } 0 0 1 1 0 11 1 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. • Inoccam, because of its zero-tolerance of aliasing, assignment semantics is what we expect: • [Inall other languages with assignment, the semantics are much more complex - since the variablex0 may be aliased to other variables … and the values associated with those aliases will also have changed tov.] "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

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

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

  29. B A c c ! x c ? y Reference Semantics HEAP after x y "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. "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

  31. { <x , v >, <x , v>, … }x0 := x1{ <x , v >, <x , ??>, … } 0 0 1 1 0 11 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. "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

  32. CHAN OF FOO c: equals PAR c ! x1 c ? x0 Mobile Communication • The semantics for assignment and communication are directly related. Inoccam, 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, ifx0andx1were of typeFOO, we have the semantic equivalence: x0 := x1 "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: "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 "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 "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 "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 P c ? a c a Generator b d Q d ? b "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 ... "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 "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 "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

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

  42. Client-Server Application • Fault-tolerance by Replication response S C request P B S "Programming in Occam-pi: A Tutorial", Zain-ul-Abdin

More Related