1 / 15

Parallel & Concurrent Programming: Occam

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:

mab
Télécharger la présentation

Parallel & Concurrent Programming: Occam

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. Vitaliy Lvin University of Massachusetts Amherst Parallel & Concurrent Programming:Occam

  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

  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

  4. 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]:

  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

  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

  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 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)

  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

  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

  10. 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)

  11. 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:

  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)

  13. Exercise Write a simple program that echoes user input to stdout and stderr in parallel

  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 :

  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)

More Related