U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Parallel & Concurrent Programming: Occam Vitaliy Lvin University of Massachusetts Amherst
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 2 Today Before: languages that hid control of parallelism from the application programmer (ZPL, Flux) Extensions of existing languages to add control (Cilk, OpenMP) Libraries (MPI) Today: Occam – an explicitly parallel imperative language
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 Introduction to Occam The idea: from Communicating Sequential Processes, C.A.R. Hoare Parallel processes communicate through channels, not shared memory Channels can be read (?) and written to (!) Occam was designed in conjunction with the Transputer processor architecture
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 The First Example Hello, World! but with a parallel twist PROC hello.world (CHAN OF BYTE keyboard, screen, error) VAL []BYTE greeting1 IS “Hello ” : VAL []BYTE greeting2 IS “World ” : VAL []BYTE endl IS “*c*n” : SEQ PAR SEQ i = 0 FOR SIZE greeting1 screen ! greeting1[i] SEQ i = 0 FOR SIZE greeting2 screen ! greeting1[i] SEQ i = 0 FOR SIZE endl screen ! endl[i] :
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 5 Occam and processes In Occam, every instruction is a process: Primitive processes: assignment, channel input-output, STOP, SKIP Constructed processes: SEQ – sequential execution IF, CASE – branching WHILE – looping ALT, PRI ALT – alternation PAR – parallel execution repetition
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Occam language constructs Part I SEQ straightforwardly combines a number of processes to be executed in order into one process IF & CASE have familiar semantics, except if no condition is matched, they behave like STOP and not SKIP WHILE behaves normally
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 7 Occam language constructs Part II PAR: combines a number of processes to be executed in parallel into one process Important restriction: no write-shared memory PAR -- this parallel is INVALID! SEQ mice := the variable mice is assigned.. c ! 42 c ? mice --..in more than one parallel component Use channels instead! (similar restrictions apply)
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 8 Occam language constructs Part III ALT combines a number of processes, only one of which will be executed Which one gets executed is determined by a guard – usually a channel input A process whose input is ready – proceeds (SELECT) PRI ALT adds a notion of priority
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 9 ALT Example -- Regulator: -- regulate flow of work into a networked farm SEQ idle := processors WHILE running ALT from.workers ? result SEQ reg.to.gen ! result idle := idle + 1 (idle >= 1) & gen.to.reg ? packet SEQ to.workers ! packet idle := idle - 1
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 Channel input-output: To read from channel: channel ? variable(s) To write to it: channel ! expression(s) Channels have protocols associated with them which describe units of communication along them Replication: SEQ, IF, PAR & ALT can be replicated (for loop) Occam language constructs Part IV
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 11 FUNCTION and PROCEDURE are also processes VALOF creates a process that returns a value RESULT defines the return value INT FUNCTION sum (VAL []INT values) INT accumulator : VALOF SEQ accumulator := 0 SEQ i = 0 FOR SIZE values accumulator := accumulator + values[i] RESULT accumulator : Occam language constructs Part V
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 12 Occam Data Types Primitive types: BOOL, BYTE, INT(16/32/64), REAL32/64 Arrays Records (structs) Types can be: Aliased Converted (changes underlying representation) Reshaped (leaves underlying representation intact)
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 13 Exercise Write a simple program that echoes user input to stdout and stderr in parallel
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 14 Solution PROC double.echo (CHAN OF BYTE keyboard, screen, error) BYTE ch: SEQ ch := ' ' WHILE TRUE SEQ keyboard ? ch PAR SEQ screen ! ch screen ! FLUSH SEQ error ! ch error ! FLUSH :
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 15 Conclusion Very clean language tailored for a particular hardware architecture Very low-level at the same time Allows for significant amount of mathematical analysis of programs Current work – Occam-pi (CSP + Pi calculus)