some updates to edu.berkeley.fleet.ir
authoradam <adam@megacz.com>
Mon, 10 Nov 2008 05:57:23 +0000 (06:57 +0100)
committeradam <adam@megacz.com>
Mon, 10 Nov 2008 05:57:23 +0000 (06:57 +0100)
src/edu/berkeley/fleet/ir/Context.java
src/edu/berkeley/fleet/ir/Process.java

index c9e6471..170c2a5 100644 (file)
@@ -176,6 +176,21 @@ public class Context {
             pending_path = null;
         }
 
+        public void interruptibleNop() {
+            flush_pending();
+            instructions.add(new Move(dock,
+                                      count!=1,
+                                      predicate,
+                                      true,
+                                      null,
+                                      false,
+                                      false,
+                                      false,
+                                      false,
+                                      false,
+                                      false));
+        }
+
         /** sets the predicate which will be applied to subsequent instructions, or null for the default predicate */
         public void setPredicate(Predicate p) {
             if (p==null) p = Predicate.Default;
@@ -359,7 +374,7 @@ public class Context {
                 }
             }
             if (autoflush && !"Debug".equals(dock.getShip().getType()) && next==null) {
-                if (dock.isInputDock() && !"inAddrRead".equals(dock.getName()))
+                if (dock.isInputDock())
                     ic.add(new Instruction.Flush(dock, true, Predicate.OLCZero));
             }
             if (count!=1) {
index c864931..1c8f706 100644 (file)
@@ -127,40 +127,39 @@ public class Process {
             }
             public void recvToken(Context.LoopFactory lf) { lf.recvToken(); }
             public void sendWord(Context.LoopFactory lf) { lf.sendWord(dock.getDataDestination()); }
-            public void build(Context ctx) {
-                if (peer==null && pattern.length==1 && pattern[0]==null) return;
-                build(ctx, ctx.new LoopFactory(dock, 1));
-            }
+            public void build(Context ctx) { build(ctx, ctx.new LoopFactory(dock, 1)); }
             // number-in-flight is considered a property of the input dock in a pair
             //public int getInflight() { return 4; }
             public int getInflight() { return 1; }
             public int getTokensToAbsorb() { return getInflight(); }
+            private boolean peerUsed() {
+                if (peer==null) return false;
+                for(int i=0; i<pattern.length; i++) if (pattern[i]==null) return true;
+                return false;
+            }
             public void reset(Context ctx, int phase) {
                 if (dock.getShip().getType().equals("Debug")) {
                     return;
                 }
-                if (peer==null && pattern.length==1 && pattern[0]==null) return;
                 switch(phase) {
                     case 0: {
                         torpedoes.add(dock);
                         break;
                     }
-                    case 1: {
-                        // FIXME
+                    case 2: {
                         reset_count++;
                         Context.LoopFactory lf = ctx.new LoopFactory(dock, 1);
                         lf.sendToken(fleet.getShip("Debug",0).getDock("in").getDataDestination());
-                        if (peer != null && peer instanceof DockOutPort) {
-                            DockOutPort dop_peer = (DockOutPort)peer;
+                        if (peerUsed()) {
                             lf = lf.makeNext(0);
                             lf.abortLoopIfTorpedoPresent();
-                            lf.recvToken();
-                            lf.sendToken(dop_peer.dock.getDataDestination());
+                            peer.recvWord(lf);
+                            peer.sendToken(lf);
                         }
                         break;
                     }
-                    case 2: {
-                        if (peer != null && peer instanceof DockOutPort) {
+                    case 3: {
+                        if (peerUsed()) {
                             torpedoes.add(dock);
                         }
                         break;
@@ -168,7 +167,6 @@ public class Process {
                 }
             }
             protected void build(Context ctx, Context.LoopFactory lf) {
-                if (peer==null && pattern.length==1 && pattern[0]==null) return;
                 int inflight = (count != 0 && count < getInflight()) ? count : getInflight();
 
                 if (peer!=null)
@@ -177,10 +175,14 @@ public class Process {
                 lf = lf.makeNext(count);
                 for(int i=0; i<pattern.length; i++) {
                     if (pattern[i]==null) {
-                        lf.abortLoopIfTorpedoPresent();
-                        peer.recvWord(lf);
-                        lf.deliver();
-                        peer.sendToken(lf);
+                        if (peer!=null) {
+                            lf.abortLoopIfTorpedoPresent();
+                            peer.recvWord(lf);
+                            lf.deliver();
+                            peer.sendToken(lf);
+                        } else {
+                            lf.interruptibleNop();
+                        }
                     } else {
                         lf.literal(pattern[i]);
                         lf.abortLoopIfTorpedoPresent();
@@ -206,14 +208,12 @@ public class Process {
             public void build(Context ctx) { build(ctx, ctx.new LoopFactory(dock, 1)); }
             protected void build(Context ctx, Context.LoopFactory lf) {
                 if (peer==null) return;
-                // FIXME: no setup?
                 lf = lf.makeNext(count);
                 lf.abortLoopIfTorpedoPresent();
                 peer.recvToken(lf);
-                //lf.abortLoopIfTorpedoPresent();  // FIXME: do I need this twice?
+                lf.abortLoopIfTorpedoPresent();  // FIXME: do I need this twice?
                 lf.collectWord();
                 peer.sendWord(lf);
-                // FIXME: cleanup
             }
             public void reset(Context ctx, int phase) {
                 switch(phase) {
@@ -222,23 +222,24 @@ public class Process {
                         break;
                     }
                     case 1: {
-                        reset_count++;
                         Context.LoopFactory lf = ctx.new LoopFactory(dock, 1);
-
+                        lf.sendToken(fleet.getShip("Debug",0).getDock("in").getDataDestination());
                         if (peer != null) {
                             for(int i=0; i<peer.getTokensToAbsorb(); i++) {
                                 lf.recvToken();
                             }
                             //lf.sendToken(dip_peer.dock.getInstructionDestination());
                         }
-
-                        lf.sendToken(fleet.getShip("Debug",0).getDock("in").getDataDestination());
                         lf = lf.makeNext(0);
                         lf.abortLoopIfTorpedoPresent();
                         lf.collectWord();
                         break;
                     }
                     case 2: {
+                        reset_count++;
+                        break;
+                    }
+                    case 3: {
                         torpedoes.add(dock);
                         break;
                     }
@@ -247,7 +248,12 @@ public class Process {
         }
     }
 
-    private BitVector bv(long l) { return new BitVector(fleet.getWordWidth()).set(l); }
+    private static BitVector bv(long l) { return new BitVector(/*FIXME fleet.getWordWidth()*/37).set(l); }
+    private static BitVector[] bv(long[] l) {
+        BitVector[] ret = new BitVector[l.length];
+        for(int i=0; i<ret.length; i++) ret[i] = bv(l[i]);
+        return ret;
+    }
 
     /**
      *  For every datum transmitted to in, pass it along to out and
@@ -379,6 +385,7 @@ public class Process {
 
     public class MemoryModule extends Module {
         public final Ship    ship;
+        public final InPort  inCBD;
         public final InPort  inAddrRead1;
         public final InPort  inAddrRead2;
         public final InPort  inAddrWrite;
@@ -388,6 +395,7 @@ public class Process {
         public final OutPort outWrite;
         public MemoryModule(Ship memoryShip) {
             this.ship = memoryShip;
+            this.inCBD        = ship.getType().equals("Memory") ? new DockInPort("inCBD", ship.getDock("inCBD")) : null;
             this.inAddrWrite  = new DockInPort("inAddrWrite", ship.getDock("inAddrWrite"));
             this.inDataWrite  = new DockInPort("inDataWrite", ship.getDock("inDataWrite"));
             this.inAddrRead1  = new InPort("inAddrRead1") {
@@ -396,9 +404,15 @@ public class Process {
                     public void build(Context ctx) { }
                     public int getTokensToAbsorb() { return outRead1.peer.getTokensToAbsorb(); }
                     public void reset(Context ctx, int phase) {
-                        if (phase==2) {
-                            torpedoes.add(ship.getDock("inAddrRead"));
-                            torpedoes.add(ship.getDock("out"));
+                        switch(phase) {
+                            case 0:
+                                torpedoes.add(ship.getDock("inAddrRead"));
+                                break;
+                            case 2:
+                                reset_count++;
+                                Context.LoopFactory lf = ctx.new LoopFactory(ship.getDock("inAddrRead"), 1);
+                                lf.sendToken(fleet.getShip("Debug",0).getDock("in").getDataDestination());
+                                break;
                         }
                     }
                 };
@@ -407,12 +421,7 @@ public class Process {
                     public void sendWord(Context.LoopFactory lf) { lf.sendWord(ship.getDock("inAddrRead").getDataDestination(), new BitVector(1).set(1)); }
                     public void build(Context ctx) { }
                     public int getTokensToAbsorb() { return outRead2.peer.getTokensToAbsorb(); }
-                    public void reset(Context ctx, int phase) {
-                        if (phase==2) {
-                            torpedoes.add(ship.getDock("inAddrRead"));
-                            torpedoes.add(ship.getDock("out"));
-                        }
-                    }
+                    public void reset(Context ctx, int phase) { }
                 };
             this.outRead1 = new OutPort("outRead1") {
                     public void sendToken(Context.LoopFactory lf) { inAddrRead1.peer.sendToken(lf); }
@@ -435,6 +444,8 @@ public class Process {
                         lf.setFlags(FlagFunction.ZERO, FlagFunction.ZERO.add(FlagC));
                         if (this.peer != null) {
                             lf.setPredicate(Predicate.FlagB);
+                            lf.literal(77);
+                            lf.abortLoopIfTorpedoPresent();
                             this.peer.recvToken(lf);
                             this.peer.sendWord(lf);
                         }
@@ -480,26 +491,42 @@ public class Process {
             vals[i] = Math.abs(random.nextInt());
         }
 
-        Ship mem1 = fleet.getShip("Memory", 1);
-        Ship mem2 = fleet.getShip("Memory", 2);
+        Ship mem1 = fleet.getShip("Memory", 0);
+        Ship mem2 = fleet.getShip("Memory", 1);
 
         FleetProcess fp;
         int stride = 1;
         fp = null;
-        while(stride < vals.length) {
-            if (fp==null) fp = fleet.run(new Instruction[0]);
+
+        fp = fleet.run(new Instruction[0]);
+        Gadgets.writeMem(fp, mem1, 0, bv(vals));
+        int vals_length = vals.length;
+        vals = null;
+
+        while(stride < vals_length) {
+            //if (fp==null) fp = fleet.run(new Instruction[0]);
             System.out.println("stride " + stride);
-            vals = mergeSort(fp, fleet, vals, stride, mem1, mem2);
+            vals = mergeSort(fp, fleet, vals, vals_length, stride, mem1, mem2);
+
+            Ship mem = mem1; mem1=mem2; mem2=mem;
+
             stride = stride * 2;
             //fp.terminate(); fp = null;
             System.out.println();
+            /*
             System.out.println("results:");
             for(int i=0; i<vals.length; i++)
                 System.out.println(vals[i]);
+            */
         }
+
+        BitVector[] bvs = new BitVector[vals_length];
+        Gadgets.readMem(fp, mem1, 0, bvs);
+        System.out.println("results:");
+        for(int i=0; i<vals_length; i++)
+            System.out.println(bvs[i].toLong());
     }
 
-    // won't verify that the switch fabric is empty, however
     public static void verifyClean(FleetProcess fp) {
         Ship debug   = fp.getFleet().getShip("Debug", 0);
         Dock debugIn = debug.getDock("in");
@@ -526,14 +553,14 @@ public class Process {
         for(Ship ship : fp.getFleet())
             if (!"Debug".equals(ship.getType()))
                 for (Dock dock : ship) {
-                    System.out.println("checking " + dock);
+                    System.out.print("checking " + dock + "                     ");
 
                     k = (k + 23) % 65535;
                     ctx = new Context(fp.getFleet());
 
                     boolean reverse = (k%2)==0;
 
-                    lf = ctx.new LoopFactory(debugIn, 2);
+                    lf = ctx.new LoopFactory(debugIn, 4);
                     lf.recvToken();
                     lf.setFlags(FlagFunction.ZERO.add(FlagC), FlagFunction.ZERO);
                     lf.setPredicate(Predicate.NotFlagA);
@@ -544,6 +571,8 @@ public class Process {
                     lf.deliver();
 
                     lf = ctx.new LoopFactory(dock, 1);
+                    lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(reverse ? 1 : 0));
+                    lf.sendToken(debugIn.getDataDestination(), new BitVector(1).set(reverse ? 0 : 1));
                     lf.sendToken(dock.getDataDestination(), new BitVector(1).set(reverse ? 1 : 0));
                     lf.sendToken(dock.getDataDestination(), new BitVector(1).set(reverse ? 0 : 1));
                     lf = lf.makeNext(2);
@@ -561,72 +590,66 @@ public class Process {
                     fp.flush();
 
                     long kk;
-                    kk = fp.recvWord().toLong();
-                    if (kk != (reverse ? k+1 : k))
-                        throw new RuntimeException(dock+" not properly initialized (1)");
-                    kk = fp.recvWord().toLong();
-                    if (kk != (reverse ? k   : k+1))
-                        throw new RuntimeException(dock+" not properly initialized (2)");
+                    for(int i=0; i<4; i++) {
+                        kk = fp.recvWord().toLong();
+                        System.out.print("\rchecking " + dock + " (got "+(i+1)+")");
+                        if (kk != ((reverse ^ (i%2!=0)) ? k+1 : k)) {
+                            System.out.println();
+                            throw new RuntimeException(dock+" not properly initialized ("+(i+1)+")");
+                        }
+                    }
+                    System.out.println();
                 }
     }
 
     // FIXME: numbers seem to get duplicated when stride=2
     public static long[] mergeSort(FleetProcess fp, Fleet fleet,
-                                   long[] vals, int stride_length,
+                                   long[] vals, int vals_length, int stride_length,
                                    Ship memoryShip1, Ship memoryShip2) throws Exception {
 
-        BitVector[] mem = new BitVector[vals.length];
-        for(int i=0; i<mem.length; i++) mem[i] = new BitVector(fleet.getWordWidth()).set(vals[i]);
-
-        Gadgets.writeMem(fp, memoryShip1, 0, mem);
+        if (vals != null) {
+            BitVector[] mem = new BitVector[vals_length];
+            for(int i=0; i<mem.length; i++) mem[i] = new BitVector(fleet.getWordWidth()).set(vals[i]);
+            Gadgets.writeMem(fp, memoryShip1, 0, mem);
+        }
 
         //////////////////////////////////////////////////////////////////////////////
 
         Process proc = new Process(fleet);
         DebugModule dm = proc.new DebugModule();
 
-        int end_of_data = vals.length;
+        int end_of_data = vals_length;
         int num_strides = end_of_data / (stride_length * 2);
 
         MemoryModule mm  = proc.new MemoryModule(memoryShip1);
         MemoryModule mm2 = proc.new MemoryModule(memoryShip2);
         SortedMergeModule sm = proc.new SortedMergeModule();
 
-
+        // So far: we have two spare Counter ships; one can be used for resetting
         for(int i=0; i<2; i++) {
 
-            Module.OutPort stride_length_1       = proc.new OnceModule(stride_length).out;
-            Module.OutPort stride_length_2       = proc.new OnceModule(stride_length).out;
-            Module.OutPort twice_stride_length_1 = proc.new OnceModule(stride_length*2).out;
-            Module.OutPort end_of_data_1         = proc.new OnceModule(end_of_data + i*stride_length).out;
-
-            Module.OutPort num_strides_1         = proc.new OnceModule(num_strides).out;
-            Module.OutPort num_strides_2         = proc.new OnceModule(num_strides).out;
-
             RepeatModule r0 = proc.new RepeatModule();
             RepeatModule r1 = proc.new RepeatModule();
-            RepeatModule r2 = proc.new RepeatModule();
             DownCounterModule c0 = proc.new DownCounterModule();
             DownCounterModule c1 = proc.new DownCounterModule();
-            ForeverModule fmm  = proc.new ForeverModule(1);
-            ForeverModule fmm2 = proc.new ForeverModule(2); // 2=ADD
             AluModule alu = proc.new AluModule();
 
-            stride_length_1.connect(r0.val);
-            num_strides_1.connect(r0.count);
+            // FIXME: we should be able to get rid of this!
+            r0.val.connect(proc.new OnceModule(stride_length).out);
+            r0.count.connect(proc.new OnceModule(num_strides).out);
             r0.out.connect(c0.start);
-            fmm.out.connect(c0.incr);
-            c0.out.connect(alu.in2);
 
-            end_of_data_1.connect(c1.start);
-            twice_stride_length_1.connect(c1.incr);
+            c0.incr.connect(proc.new ForeverModule(1).out);
+
+            c1.start.connect(proc.new OnceModule(end_of_data + i*stride_length).out);
+            c1.incr.connect(proc.new OnceModule(stride_length*2).out);
             c1.out.connect(r1.val);
-            stride_length_2.connect(r2.val);
-            num_strides_2.connect(r2.count);
-            r2.out.connect(r1.count);
-            r1.out.connect(alu.in1);
-            fmm2.out.connect(alu.inOp);
 
+            r1.count.connect(proc.new ForeverModule(stride_length).out);
+
+            alu.in1.connect(r1.out);
+            alu.in2.connect(c0.out);
+            alu.inOp.connect(proc.new ForeverModule(2 /* ADD */).out);
             alu.out.connect(i==0 ? mm.inAddrRead1 : mm.inAddrRead2);
 
             ForeverModule fm = proc.new ForeverModule(stride_length);
@@ -641,16 +664,13 @@ public class Process {
         sm.out.connect(unpunc.val);
         fm.out.connect(unpunc.count);
 
-        /*
         DownCounterModule cw = proc.new DownCounterModule();
         proc.new OnceModule(end_of_data).out.connect(cw.start);
         proc.new OnceModule(1).out.connect(cw.incr);
         cw.out.connect(mm2.inAddrWrite);
         unpunc.out.connect(mm2.inDataWrite);
         mm2.outWrite.connect(dm.in);
-        */
-        unpunc.out.connect(dm.in);
-
         //////////////////////////////////////////////////////////////////////////////
 
         Context ctx = new Context(fp.getFleet());
@@ -660,22 +680,16 @@ public class Process {
         proc.build(ctx);
         ctx.emit(ai);
         for(Instruction ins : ai) {
-            System.out.println(ins);
+            //System.out.println(ins);
             fp.sendInstruction(ins);
         }
         fp.flush();
 
-        System.out.println("reading back...");
-        int inc=0;
-        for(int i=0; i<vals.length; i++) {
-            inc++;
-            BitVector bv = fp.recvWord();
-            System.out.println("\r"+bv + " " + bv.toLong() + "          #read="+inc);
-            mem[i] = bv;
+        for(int i=0; i<vals_length; i++) {
+            System.out.print("\rreading back... " + i);
+            fp.recvWord();
         }
         System.out.println("\rdone.                                                                    ");
-        long[] ret = new long[vals.length];
-        for(int i=0; i<ret.length; i++) ret[i] = mem[ret.length-i-1].toLong();
 
         //if (true) return ret;
 
@@ -697,7 +711,7 @@ public class Process {
 
         int count = 0;
 
-        for(int phase=0; phase<=2; phase++) {
+        for(int phase=0; phase<=3; phase++) {
             System.out.println("== phase "+phase+" ==================================================================");
             ctx2 = new Context(fp.getFleet());
             proc.reset(ctx2, phase);
@@ -706,33 +720,26 @@ public class Process {
             for(Instruction ins : ai) fp.sendInstruction(ins);
             fp.flush();
             System.out.println("flushed");
-            for(int ii=0; ii<reset_count; ii++)
-                System.out.print("\r phase "+phase+" ==> " + fp.recvWord().toLong() + " " + (ii+1) + " / " + reset_count);
+            for(int ii=0; ii<reset_count; ii++) {
+                System.out.print("\r phase "+phase+" ==> " + (ii+1) + " / " + reset_count);
+                fp.recvWord();
+            }
             System.out.println();
         }
 
-        /*
-        ctx2 = new Context(fp.getFleet());
-        ai = new ArrayList<Instruction>();
-        for(Ship ship : ctx.allocatedShips)
-            if (!ship.getType().equals("Debug"))
-                for(Dock dock : ship)
-                    if (dock.isInputDock()) {
-                        lf = ctx2.new LoopFactory(dock, 0);
-                        lf.recvWord();
-                        
-                    }
-        */
-
         fp.sendToken(debugIn.getInstructionDestination());
         fp.flush();
 
         System.out.println("verifying cleanup:");
-        verifyClean(fp);
-
-        Gadgets.readMem(fp, memoryShip2, 0, mem);
-        //for(int i=0; i<ret.length; i++) ret[i] = mem[ret.length-i-1].toLong();
-
+        //verifyClean(fp);
+
+        long[] ret = null;
+        if (vals != null) {
+            ret = new long[vals_length];
+            BitVector[] mem = new BitVector[vals_length];
+            Gadgets.readMem(fp, memoryShip2, 0, mem);
+            for(int i=0; i<ret.length; i++) ret[i] = mem[i].toLong();
+        }
         return ret;
     }