Outline 0024 Spring 2010 14 :: 2 CSP: communicating sequential - - PowerPoint PPT Presentation

outline
SMART_READER_LITE
LIVE PREVIEW

Outline 0024 Spring 2010 14 :: 2 CSP: communicating sequential - - PowerPoint PPT Presentation

Outline 0024 Spring 2010 14 :: 2 CSP: communicating sequential processes Synchronous communication with global channels Channels are the only state that is shared between threads No shared objects, shared


slide-1
SLIDE 1
slide-2
SLIDE 2

– 14 :: 2 – 0024 Spring 2010

Outline

slide-3
SLIDE 3

– 14 :: 3 – 0024 Spring 2010

CSP: communicating sequential processes

Synchronous communication with global channels Channels are the only state that is shared between threads No shared objects, shared references, or shared arrays Only data can be transmitted -- no point to send a reference

We use only integers No loss of generality but simplifies setup

slide-4
SLIDE 4

– 14 :: 4 – 0024 Spring 2010

Guarded commands

Used in a select clause select {

  • guard1 -> {block1};
  • guard2 -> {block2};

…. } Any guard that evaluates to true may trigger execution

  • f its block.

Only one block (of those with a guard that evaluates to true) is executed.

Select fails if no guard evaluates to true A guard that waits (e.g., for input) cannot be evaluated *yet*

slide-5
SLIDE 5

– 14 :: 5 – 0024 Spring 2010

CSP and Java

CSP is a (powerful) model

Ignores many problems of a real programming language

CSP often provides us with (additional) ideas on how to write a parallel program We want to practice this style of parallel programming

Two options: introduce new language based on CSP

E.g., occam

Use CSP within the context of Java

Programming language remains the same Must make a few compromises

» E.g., “?” operator already defined

slide-6
SLIDE 6

– 14 :: 6 – 0024 Spring 2010

JCSP

We pursue the second option JCSP: Communicating Sequential Processes for Java

Developed by Peter Welch and his colleagues at the

University of Kent at Canterbury (UK)

slide-7
SLIDE 7

– 14 :: 7 – 0024 Spring 2010

Java packages

You can use previously defined classes by including an “import” statement in your .java files

A package defines a space for classes to be imported by

  • thers

import org.jcsp.lang.OneClass

Allows you to use OneClass as if defined in your project

OneClass is contained in package org.jcsp.lang

Compiler finds information (about method signatures) in a

class file in org/jcsp/lang or in some archive

import org.jcsp.lang.*

Same as above, imports all classes in the package

slide-8
SLIDE 8

– 14 :: 8 – 0024 Spring 2010

JCSP terminology

JCSP based (to some extend) on work for other programming language (occam) CSProcess -- unit of computation

  • Run inside a thread
  • Think “thread” -- but without access to global data

Channels are the only kind of global object that

should be accessed

Well, a program that uses JCSP is a legal Java program.

You *can* share object references -- but we do not want to do

this!

slide-9
SLIDE 9

– 14 :: 9 – 0024 Spring 2010

CSProcess

slide-10
SLIDE 10

– 14 :: 10 – 0024 Spring 2010

Channel

Channels allow CSProcesses to communicate Different kinds of channel to capture common usage models

One2OneChannel: connects two processes

One can send (write), the other can receive (read)

One2AnyChannel: one process can write to an arbitrary

number of processes

Any2OneChannel: one process can read from any number of

processes

Any2AnyChannel: arbitrary number or readers and writers

slide-11
SLIDE 11

– 14 :: 11 – 0024 Spring 2010

Channel ends

CSProcesses interact with channel ends

E.g., a process can write to a channel

ChannelOutput out;

  • ut.write(new Integer(1));

Another process can read from a channel

ChannelInput in; Integer d = (Integer)in.read();

When we put the processes together we must make sure that the local references “in” (in reader) and “out” in writer are mapped to appropriate ends of

  • ne channel
slide-12
SLIDE 12

– 14 :: 12 – 0024 Spring 2010

JCSP process structure

slide-13
SLIDE 13

– 14 :: 13 – 0024 Spring 2010

CSProcess example

import org.jcsp.lang.*; public class SendEvenIntsProcess implements CSProcess { private ChannelOutput out; public SendEvenIntsProcess(ChannelOutput out) { this.out = out; } …… run() }

slide-14
SLIDE 14

– 14 :: 14 – 0024 Spring 2010

Example, continued

public void run() { for (int i = 2; i <= 100; i = i + 2) {

  • ut.write (new Integer (i));

} }

slide-15
SLIDE 15

– 14 :: 15 – 0024 Spring 2010

Example, continued

import org.jcsp.lang.*; public class ReadEvenIntsProcess implements CSProcess { private ChannelInput in; public ReadEvenIntsProcess(ChannelInput in){ this.in = in; } … run }

slide-16
SLIDE 16

– 14 :: 16 – 0024 Spring 2010

Example, continued

public void run() { while (true) { Integer d = (Integer)in.read(); System.out.println("Read: " + d.intValue()); } }

slide-17
SLIDE 17

– 14 :: 17 – 0024 Spring 2010

Parallel processes

We want to run the sender and the receiver process in parallel. Heres how to do this in JCSP:

new Parallel ( … an array of CSProcess ).run ();

Like an anonymous thread

Create parallel construct

run() methods of CSProcesses execute in parallel

Execute run() method

defined by the run() methods of the parallel sections

slide-18
SLIDE 18

– 14 :: 18 – 0024 Spring 2010

Putting the processes together

import org.jcsp.lang.*;

public class DriverProgram { public static void main(String[] args) { One2OneChannel chan = Channel.one2one(); new Parallel ( new CSProcess[] { new SendEvenIntsProcess (chan.out() ), new ReadEvenIntsProcess (chan.in() ) } ).run (); } }

slide-19
SLIDE 19

– 14 :: 19 – 0024 Spring 2010

Channel manufacture

slide-20
SLIDE 20

– 14 :: 20 – 0024 Spring 2010

Slow motion -- channel

import org.jcsp.lang.*;

public class DriverProgram { public static void main(String[] args) { One2OneChannel chan = Channel.one2one(); new Parallel ( new CSProcess[] { new SendEvenIntsProcess (chan.out() ), new ReadEvenIntsProcess (chan.in() ) } ).run (); } }

slide-21
SLIDE 21

– 14 :: 21 – 0024 Spring 2010

Slow motion: channel setup

import org.jcsp.lang.*;

public class DriverProgram { public static void main(String[] args) { One2OneChannel chan = Channel.one2one(); new Parallel ( new CSProcess[] { new SendEvenIntsProcess (chan.out() ), new ReadEvenIntsProcess (chan.in() ) } ).run (); } }

slide-22
SLIDE 22

– 14 :: 22 – 0024 Spring 2010

Slow motion - constructors

public class ReadEvenIntsProcess implements CSProcess {

private ChannelInput in; public ReadEvenIntsProcess(ChannelInput in){ this.in = in; }

}

public class SendEvenIntsProcess implements CSProcess {

private ChannelOutput out; public SendEvenIntsProcess(ChannelOutput out) { this.out = out; }

}

slide-23
SLIDE 23

– 14 :: 23 – 0024 Spring 2010

Choosing between inputs

Need to realize guards in the Java context

Guard[] guards = {in, request, skip}; Sets up guards for input channels “in” and “request”

Skip is a channel thats always ready The guards object (!) [of class Guard[]) will be used in a “select” statement. For this to work, the system needs to know that the relevant channel (inputs) are special.

slide-24
SLIDE 24

– 14 :: 24 – 0024 Spring 2010

Guard setup

private AltingChannelInput in; private ChannelOutput out; //as before private AltingChannelInput request; An “AltingChannelInput” is an input that can be used in a guard (that will be used to make a selection)

The name refers to the CSP “ALT” command – think

alternative

slide-25
SLIDE 25

– 14 :: 25 – 0024 Spring 2010

Select mechanics

slide-26
SLIDE 26

– 14 :: 26 – 0024 Spring 2010

Select flavors

Select -- as introduced in CSP discussion PriSelect – priority select – order matters FairSelect – as introduced in CSP discussion, but provides fairness

No channel selected a second time before all other channels

that are ready have been read at least once

slide-27
SLIDE 27

– 14 :: 27 – 0024 Spring 2010

Example

class BufferP implements CSProcess { private AltingChannelInput in; private ChannelOutput out; private AltingChannelInput request; public BufferP(AltingChannelInput in, ChannelOutput out, AltingChannelInput request) { this.in = in; this.out = out; this.request = request; }

slide-28
SLIDE 28

– 14 :: 28 – 0024 Spring 2010

public void run() { Skip skip = new Skip (); Guard[] guards = {in, request, skip}; Alternative alt = new Alternative (guards); Integer [] buffer = new Integer[100]; int wptr = 0; int rptr = 0; while (true) { switch (alt.priSelect()) { } }

slide-29
SLIDE 29

– 14 :: 29 – 0024 Spring 2010

switch (alt.priSelect()) { case 0: Integer x = (Integer)in.read(); buffer[wptr] = x; wptr++; break; case 1: Integer signal = (Integer)request.read(); if (rptr == wptr) { Integer xx = (Integer)in.read();

  • ut.write(xx);

} else {

  • ut.write (buffer[rptr]);

rptr++; } break;

slide-30
SLIDE 30

– 14 :: 30 – 0024 Spring 2010

switch (alt.priSelect()) { case 0: case 1: case 2: // ... nothing available for the above ... // ... so get on with something else for a while ... // ... then loop around and poll again ... try {Thread.sleep (400);} catch (InterruptedException e) {} System.out.println("... so getting on with something else for a while ..."); break; } // end switch }

slide-31
SLIDE 31

– 14 :: 31 – 0024 Spring 2010

Recall

Order of channels in guard determines mapping to cases

slide-32
SLIDE 32

– 14 :: 32 – 0024 Spring 2010

Comments

JCSP contains a lot more

Large number of additional classes Dont become a JCSP expert

Focus on principles Significant changes announced

Look at this style of programming for inspiration

Do you really need to share all objects Structure thread/process interactions

slide-33
SLIDE 33

– 14 :: 33 – 0024 Spring 2010

JCSP home page: http://www.cs.kent.ac.uk/projects/ofa/jcsp/ Proceed with caution.