150 likes | 324 Views
Vitaliy Lvin University of Massachusetts Amherst. Parallel & Concurrent Programming: Occam. 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:
E N D
Vitaliy Lvin University of Massachusetts Amherst Parallel & Concurrent Programming:Occam
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
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
The First Example Hello, World! but with a parallel twistPROC 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]:
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
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
Occam language constructs Part II PAR: combines a number of processes to be executed in parallel into one process Important restriction: no write-shared memoryPAR -- this parallel is INVALID!SEQ mice := 42 -- the variable mice is assigned.. c ! 42 c ? mice -- ..in more than one parallel component Use channels instead! (similar restrictions apply)
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
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
Occam language constructs Part IV 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 V FUNCTION and PROCEDURE are also processes VALOF creates a process that returns a value RESULT defines the return valueINT FUNCTION sum (VAL []INT values)INT accumulator : VALOF SEQ accumulator := 0 SEQ i = 0 FOR SIZE values accumulator := accumulator + values[i] RESULT accumulator:
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)
Exercise Write a simple program that echoes user input to stdout and stderr in parallel
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 :
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)