Outline 0024 Spring 2010 14 :: 2 CSP: communicating sequential - - PowerPoint PPT Presentation
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
– 14 :: 2 – 0024 Spring 2010
Outline
– 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
– 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*
– 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
– 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)
– 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
– 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!
– 14 :: 9 – 0024 Spring 2010
CSProcess
– 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
– 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
– 14 :: 12 – 0024 Spring 2010
JCSP process structure
– 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() }
– 14 :: 14 – 0024 Spring 2010
Example, continued
public void run() { for (int i = 2; i <= 100; i = i + 2) {
- ut.write (new Integer (i));
} }
– 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 }
– 14 :: 16 – 0024 Spring 2010
Example, continued
public void run() { while (true) { Integer d = (Integer)in.read(); System.out.println("Read: " + d.intValue()); } }
– 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
– 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 (); } }
– 14 :: 19 – 0024 Spring 2010
Channel manufacture
– 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 (); } }
– 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 (); } }
– 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; }
}
– 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.
– 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
– 14 :: 25 – 0024 Spring 2010
Select mechanics
– 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
– 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; }
– 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()) { } }
– 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;
– 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 }
– 14 :: 31 – 0024 Spring 2010
Recall
Order of channels in guard determines mapping to cases
– 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
– 14 :: 33 – 0024 Spring 2010