[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ComsTime with full PAR in Delta



Hi

Thanks for a most intersting time, Gerald, Andy and Peter.
There is so much nice code, and I'm learning!

1. As far as i have seen, no implementations address the problem of a 
   broken channel/pipe. I know that this is not occam CHAN sematics, but to
   be true, I have missed it. The Java Piped... library handles broken
   'pipes'. Could this be handled with non-busy polling?
   Since the idea of a synchronous and broken CHAN is contradictory -  
   will it have to be called PIPE_OF_INT? This could be inherited
   from CHAN_OF_INT?

2. This solution in Gerald's Delta is not easy to read:
   do
   {
     if (!done1) done1 = chanout1.swrite(a[0]);
     if (!done2) done2 = chanout2.swrite(a[0]);
     if (!(done1 & done2)) Thread.yield();
   } while (!(done1 & done2));

3. In Peter's last code, I have modified Delta to have outputs in
   parallel through a Kicker class. The problem is that it does
   not work. (This seems to follow me.)
   The same solution worked 90% of the time when used
   with Piped.. library. Code is below. I may have missed something,
   or Peter may have. Could it be the warm-up phase? I haven't
   studied in detail, but why have a warm-up phase? My processor
   is warm enough, no problem :-)

4. During a phase in the code (this morning at 6:15 at home), the
   compiler issued a compiler exception. This was when the 
   Kicker fold was placed after the Delta fold, and the call
   to Kicker in Delta had not been renamed from JumpBuffer to
   Kicker yet. 

5. I must terminate ComsTime with Ctrl C!
   And ComsTimeFullPAR doesn't work properly.
   I'm running Java 1.0 an a Win95 on a 486Dx2 66Mhz. 


import java.util.*;

//{{{  PROC Prefix (VAL INT n, CHAN OF INT in, out)
// Oyvind Teig 20.3.96
// modified Welch 8.5.96

class Prefix extends Thread {
  private CHAN_OF_INT in;
  private CHAN_OF_INT out;
  private int n;

  //{{{  constructor
  Prefix (int n, CHAN_OF_INT in, CHAN_OF_INT out) {
    // setName ("Prefix");
    this.n = n;
    this.in = in;
    this.out = out;
    start ();
  }
  //}}}  
  //{{{  run
  public void run () {
    // System.out.println ("    " + getName() + " " + toString());
    try {
      //{{{  main code in here
      out.out (n);                      //          SEQ
      while (true)                      //            out ! n
      {                                 //            WHILE TRUE
        int value;                      //              INT value:
        value = in.in ();               //              SEQ
        out.out (value);                //                in ? value
      }                                 //                out ! value
      //}}}  
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + getName() + " method run exception");
    }
  }
  //}}}  
}
//}}}  
//{{{  PROC Kicker (VAL INT value, CHAN OF INT out)
// Oyvind Teig 14.5.96

class Kicker extends Thread {
  private CHAN_OF_INT out;
  private int value;
  //{{{  constructor
  Kicker (int value, CHAN_OF_INT out) {
    // setName ("Buffer");
    this.value = value;
    this.out = out;
    start ();
  }
  //}}}  
  //{{{  run
  public void  run ()
  {
    // System.out.println (currentThread().toString());
    try
    {
      out.out (value);
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + getName() + " method run exception");
    }
  }
  //}}}  
}
//}}}  
//{{{  PROC Delta (CHAN OF INT in, CHAN OF INT out.0, out.1)
// Oyvind Teig 20.3.96
// modified Welch 8.5.96

class Delta extends Thread {
  private CHAN_OF_INT in;
  private CHAN_OF_INT out_0;
  private CHAN_OF_INT out_1;

  //{{{  constructor
  Delta (CHAN_OF_INT in, CHAN_OF_INT out_0, CHAN_OF_INT out_1) {
    // setName ("Delta");
    this.in = in;
    this.out_0 = out_0;
    this.out_1 = out_1;
    start ();
  }
  //}}}  
  //{{{  run
  public void  run () {
    try {
      //{{{  main code
      // System.out.println ("    " + getName() + " " + toString());
      while (true)                      //          WHILE TRUE
      {                                 //            INT value:
        int value;                      //            SEQ
        value = in.in ();               //              in ? value
        // out_0.out (value);           //              out.0 ! value  -- should be
        // out_1.out (value);           //              out.1 ! value  -- in PAR !!
        //{{{  Start new threads          //              Here is the PAR
        Kicker PAR_0 = new Kicker (value, out_0);
        Kicker PAR_1 = new Kicker (value, out_1);

        PAR_0.join ();
        PAR_1.join ();
        //}}}  
      }
      //}}}  
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + getName() + " method run exception");
    }
  }
  //}}}  
}
//}}}  
//{{{  PROC Succ (CHAN OF INT in, CHAN OF INT out)
// Oyvind Teig 20.3.96
// modified Welch 8.5.96

class Succ extends Thread {
  private CHAN_OF_INT in;
  private CHAN_OF_INT out;

  //{{{  Succ
  Succ (CHAN_OF_INT in, CHAN_OF_INT out) {
    // setName ("Succ");
    this.in = in;
    this.out = out;
    start ();
  }
  //}}}  
  //{{{  run
  public void  run () {
    // System.out.println ("    " + getName() + " " + toString());
    try {
      //{{{  main code in here
      while (true)                      //          WHILE TRUE
      {                                 //            INT value:
        int value;                      //            SEQ
        value = in.in ();               //              in ? value
        out.out (value + 1);            //              out ! value + 1
      }
      //}}}  
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + getName() + " method run exception");
    }
  }
  //}}}  
}
//}}}  
//{{{  PROC Consume (INT nLoops, CHAN OF INT in, out)
// Oyvind Teig 20.3.96
// modified Welch 8.5.96

class Consume extends Thread {
  private CHAN_OF_INT in;
  private CHAN_OF_INT out;
  private int nLoops;

  //{{{  constructor
  Consume (int nLoops, CHAN_OF_INT in, CHAN_OF_INT out) {
    // setName ("Consume");
    this.in = in;
    this.out = out;
    this.nLoops = nLoops;
    start ();
  }
  //}}}  
  //{{{  run
  public void  run () {
    // System.out.println ("    " + getName() + " " + toString());
    try {
      //{{{  main code in here
      //{{{  warm-up loop
      int warm_up = 16;                          //          VAL INT warm.up IS 16:
      for (int i = 0; i < warm_up; i++) {        //          SEQ i = 0 FOR warm.up
        int value;                               //            INT value:
        value = in.in ();                        //            in ? value
        //{{{  COMMENT System.out.println (value);
        //System.out.println (value);
        //}}}  
      }
      //}}}  
      //{{{  Time tag
      Date date1 = new Date();
      //}}}  
      //{{{  bench-mark loop
      for (int i = 0; i < nLoops; i++) {         //          SEQ i = 0 FOR nLoops
        int value;                               //            INT value:
        value = in.in ();                        //            in ? value
        //{{{  COMMENT System.out.println (value);
        //System.out.println (value);
        //}}}  
      }
      //}}}  
      //{{{  Time tag
      Date date2 = new Date();
      //}}}  
      //{{{  Report
      long microseconds   = ((date2.getTime() - date1.getTime()) * 1000);
      long timePerLoop_us = (microseconds / ((long) nLoops));
      System.out.println ("   " + timePerLoop_us + " microseconds / iteration");
      //}}}  
      //{{{  signal main thread that we're done
      out.out (0);
      //}}}  
      //}}}  
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + getName() + " method run exception");
    }
  }
  //}}}  
}
//}}}  

//{{{  ComsTimeFullPAR main program thread
// modified Teig 14.5.96
// modified Welch 8.5.96

class ComsTimeFullPAR {
  //{{{  public static void  main (String argv [])
  public static void  main (String argv []) {
    //{{{  Banner
    System.out.println ("");
    System.out.println ("Test of communication between Java threads");
    System.out.println ("Based on occam ComsTime.occ by Peter Welch, University of 
Kent at Canterbury");
    System.out.println ("Ported into Java by Oyvind Teig");
    System.out.println ("Now using CHAN_OF_INT (phw) and parallel output in Delta 
(OT)");
    System.out.println ("");
    //}}}  
    //{{{  nLoops
    int nLoops = 1000;
    System.out.println (nLoops + " loops:");
    //}}}  
    //{{{  CHAN OF INT a, b, c, d, e:
    CHAN_OF_INT a = new CHAN_OF_INT ();
    CHAN_OF_INT b = new CHAN_OF_INT ();
    CHAN_OF_INT c = new CHAN_OF_INT ();
    CHAN_OF_INT d = new CHAN_OF_INT ();
    CHAN_OF_INT e = new CHAN_OF_INT ();
    //}}}  
    //{{{  PAR (prefix, delta, succ, consume)         //    PAR
    Prefix prefix = new Prefix (0, b, a);           //      prefix (0, b, a)
    Delta delta = new Delta (a, c, d);              //      delta (a, c, d)
    Succ succ = new Succ (c, b);                    //      succ (c, b)
    Consume consume = new Consume (nLoops, d, e);   //      consume (nLoops, d, e)
    //}}}  
    //{{{  wait for the all done signal
    try {
      int done = e.in ();
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + "ComsTime" + " method run exception");
    }
    //}}}  
    //{{{  Stop threads
    prefix.stop ();
    delta.stop ();
    succ.stop ();
    consume.stop ();
    //}}}  
    //{{{  join
    try {
       prefix.join ();
       delta.join ();
       succ.join ();
       consume.join ();
    }
    catch (InterruptedException caught) {
      System.out.println ("    " + "ComsTime" + " final join interrupted");
    }
    //}}}  
  }
  //}}}  
}
//}}}  

Cheers,   |   0yvind Teig, Autronica, Trondheim, Norway       |
Oyvind    |   Oyvind.Teig@xxxxxxxxxxxxxxxxxxxx                |
          |   Presently obsolete address: teig@xxxxxxxxxxxx   |
          |   47 73 58 12 68 (Tel) 47 73 91 93 20 (Fax)       |