Parallel & Concurrent Programming: Occam

15
UNIVERSITY OF NIVERSITY OF MASSACHUSETTS ASSACHUSETTS, A , AMHERST MHERST Department of Computer Science Department of Computer Science Parallel & Concurrent Programming: Occam Vitaliy Lvin University of Massachusetts Amherst

description

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: - PowerPoint PPT Presentation

Transcript of Parallel & Concurrent Programming: Occam

Page 1: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science

Parallel & Concurrent Programming:

Occam

Vitaliy LvinUniversity of Massachusetts

Amherst

Page 2: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 3: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 4: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 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]:

Page 5: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 6: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 7: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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 := 42 -- the variable mice is assigned.. c ! 42 c ? mice -- ..in more than one parallel component

Use channels instead! (similar restrictions apply)

Page 8: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 9: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 10: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 11: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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

Page 12: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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)

Page 13: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 13

Exercise Write a simple program that echoes

user input to stdout and stderr in parallel

Page 14: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science Department of Computer Science 14

SolutionPROC 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

:

Page 15: Parallel & Concurrent Programming: Occam

UUNIVERSITY OF NIVERSITY OF MMASSACHUSETTSASSACHUSETTS, A, AMHERST • MHERST • Department of Computer Science 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)