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 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 � 0024 Spring 2010 � – 14 :: 3 – �
Guarded commands � Used in a select clause � select { � � guard1 -> {block1}; � � guard2 -> {block2}; � …. � } � Any guard that evaluates to true may trigger execution of 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* � 0024 Spring 2010 � – 14 :: 4 – �
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 � 0024 Spring 2010 � – 14 :: 5 – �
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) � 0024 Spring 2010 � – 14 :: 6 – �
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 others � 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 � 0024 Spring 2010 � – 14 :: 7 – �
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! � 0024 Spring 2010 � – 14 :: 8 – �
CSProcess � 0024 Spring 2010 � – 14 :: 9 – �
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 � 0024 Spring 2010 � – 14 :: 10 – �
Channel ends � CSProcesses interact with channel ends � � � E.g., a process can write to a channel � ChannelOutput out; out.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 one channel � 0024 Spring 2010 � – 14 :: 11 – �
JCSP process structure � 0024 Spring 2010 � – 14 :: 12 – �
CSProcess example � import org.jcsp.lang.*; public class SendEvenIntsProcess implements CSProcess { private ChannelOutput out; public SendEvenIntsProcess(ChannelOutput out) { this.out = out; } …… run() } 0024 Spring 2010 � – 14 :: 13 – �
Example, continued � public void run() { for (int i = 2; i <= 100; i = i + 2) { out.write (new Integer (i)); } } 0024 Spring 2010 � – 14 :: 14 – �
Example, continued � import org.jcsp.lang.*; public class ReadEvenIntsProcess implements CSProcess { private ChannelInput in; public ReadEvenIntsProcess(ChannelInput in){ this.in = in; } … run 0024 Spring 2010 � – 14 :: 15 – � }
Example, continued � public void run() { while (true) { Integer d = (Integer)in.read(); System.out.println("Read: " + d.intValue()); } } 0024 Spring 2010 � – 14 :: 16 – �
Parallel processes � We want to run the sender and the receiver process in parallel. � Here � s 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 � 0024 Spring 2010 � – 14 :: 17 – � � � defined by the run() methods of the parallel sections �
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 (); } 0024 Spring 2010 � – 14 :: 18 – � }
Channel manufacture � 0024 Spring 2010 � – 14 :: 19 – �
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 (); } 0024 Spring 2010 � – 14 :: 20 – � }
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 (); } 0024 Spring 2010 � – 14 :: 21 – � }
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; } } 0024 Spring 2010 � – 14 :: 22 – �
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 that � s 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. � 0024 Spring 2010 � – 14 :: 23 – �
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 � 0024 Spring 2010 � – 14 :: 24 – �
Select mechanics � 0024 Spring 2010 � – 14 :: 25 – �
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 � 0024 Spring 2010 � – 14 :: 26 – �
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; 0024 Spring 2010 � – 14 :: 27 – � }
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()) { } 0024 Spring 2010 � – 14 :: 28 – � }
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(); out.write(xx); } else { out.write (buffer[rptr]); rptr++; } 0024 Spring 2010 � – 14 :: 29 – � break;
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 } 0024 Spring 2010 � – 14 :: 30 – �
Recommend
More recommend