updates to get some of the shutdown code to execute via Program
[fleet.git] / src / edu / berkeley / fleet / dataflow / MergeSort.java
index e39e1b7..9a8c70e 100644 (file)
@@ -1,11 +1,12 @@
 package edu.berkeley.fleet.dataflow;
+import java.util.*;
 import edu.berkeley.fleet.loops.*;
 import edu.berkeley.fleet.api.*;
 import edu.berkeley.fleet.fpga.*;
 import java.util.*;
 
 public class MergeSort {
-    public static long[] mergeSort(FleetProcess fp, Fleet fleet,
+    public static long[] mergeSort(FleetProcess fp, Fleet fleet, ShipPool pool,
                                    long[] vals, int vals_length, int stride_length,
                                    Ship memoryShip1, Ship memoryShip2) throws Exception {
 
@@ -17,7 +18,7 @@ public class MergeSort {
 
         //////////////////////////////////////////////////////////////////////////////
 
-        DataFlowGraph proc = new DataFlowGraph(fleet);
+        DataFlowGraph proc = new DataFlowGraph(fleet, pool);
         DebugNode dm = new DebugNode(proc);
 
         int end_of_data = vals_length;
@@ -69,17 +70,12 @@ public class MergeSort {
 
         //////////////////////////////////////////////////////////////////////////////
 
+        Ship codeMemoryShip = proc.pool.allocateShip("Memory");
+
         Context ctx = new Context(fp.getFleet());
         ctx.setAutoflush(true);
-
-        ArrayList<Instruction> ai = new ArrayList<Instruction>();
         proc.build(ctx);
-        ctx.emit(ai);
-        for(Instruction ins : ai) {
-            //System.out.println(ins);
-            fp.sendInstruction(ins);
-        }
-        fp.flush();
+        ctx.dispatch(fp, true);
 
         for(int i=0; i<vals_length; i++) {
             System.out.print("\rreading back... " + i+"/"+vals_length+"  ");
@@ -88,67 +84,71 @@ public class MergeSort {
         }
         System.out.println("\rdone.                                                                    ");
 
-        //if (true) return ret;
-
-        Context ctx2 = new Context(fp.getFleet());
         Dock debugIn = fleet.getShip("Debug",0).getDock("in");
-        Dock fred = debugIn;
         fp.sendToken(debugIn.getInstructionDestination());
         fp.flush();
 
-        LoopFactory lf = new LoopFactory(ctx2, debugIn, 0);
-        lf.literal(0);
-        lf.abortLoopIfTorpedoPresent();
-        lf.recvToken();
-        lf.deliver();
-
-        ctx2.dispatch(fp);
-        fp.flush();
-
         int count = 0;
-
         Ship counter = proc.pool.allocateShip("Counter");
 
         for(int phase=0; phase<=3; phase++) {
             System.out.println("== phase "+phase+" ==================================================================");
-            ctx2 = new Context(fp.getFleet());
+
+            LoopFactory lf;
 
             Destination ackDestination = counter.getDock("in2").getDataDestination();
-            int expected_tokens = proc.reset(ctx2, phase, ackDestination);
+            HashSet<Dock> sendTorpedoesTo = new HashSet<Dock>();
+            Context ctx_reset = new Context(fp.getFleet());
+            int expected_tokens = proc.reset(ctx_reset, phase, ackDestination, sendTorpedoesTo);
+
+            Context ctx_debug = new Context(fp.getFleet());
+            lf = new LoopFactory(ctx_debug, debugIn, 0);
+            lf.literal(0);
+            lf.abortLoopIfTorpedoPresent();
+            lf.recvToken();
+            lf.deliver();
 
-            Context ctx3 = new Context(fp.getFleet());
-            lf = new LoopFactory(ctx3, counter.getDock("inOp"), 1);
+            Context ctx_count = new Context(fp.getFleet());
+            lf = new LoopFactory(ctx_count, counter.getDock("inOp"), 1);
             lf.literal("DROP_C1_V2");
             lf.deliver();
             lf.literal(5);
             lf.deliver();
-            lf = new LoopFactory(ctx3, counter.getDock("in1"), 1);
+            lf = new LoopFactory(ctx_count, counter.getDock("in1"), 1);
             lf.literal(expected_tokens-1);
             lf.deliver();
             lf.literal(1);
             lf.deliver();
-            lf = new LoopFactory(ctx3, counter.getDock("in2"), 0);
+            lf = new LoopFactory(ctx_count, counter.getDock("in2"), 0);
             lf.abortLoopIfTorpedoPresent();
             lf.recvWord();
             lf.deliver();
-            lf = new LoopFactory(ctx3, counter.getDock("out"), 1);
+            lf = new LoopFactory(ctx_count, counter.getDock("out"), 1);
             lf.collectWord();
             lf.sendToken(counter.getDock("in2").getInstructionDestination());  // HACK: we don't check to make sure this hits
             lf.sendToken(debugIn.getDataDestination());
-            ctx3.dispatch(fp);  // HACK: we don't check to make sure that this is "firmly in place"
 
-            for(Dock dock : DataFlowGraph.torpedoes) fp.sendToken(dock.getInstructionDestination());
-            ctx2.dispatch(fp);
+            Program program = new Program(codeMemoryShip);
+            CodeBag cb = program.makeCodeBag(ctx_count);
+            program.install(fp);
+            MemoryUtils.putMemoryShipInDispatchMode(fp, codeMemoryShip);
+            program.run(fp, cb);
             fp.flush();
+
+            ctx_debug.dispatch(fp, true);
+            //ctx_count.dispatch(fp, true);  // HACK: we don't check to make sure that this is "firmly in place"
+            for(Dock dock : sendTorpedoesTo) fp.sendToken(dock.getInstructionDestination());
+            ctx_reset.dispatch(fp, true);
             System.out.println("flushed");
 
             fp.recvWord();
+            fp.sendToken(debugIn.getInstructionDestination());
             System.out.println("phase done");
 
+            MemoryUtils.removeMemoryShipFromDispatchMode(fp, codeMemoryShip);
             System.out.println();
         }
 
-        fp.sendToken(debugIn.getInstructionDestination());
         fp.flush();
 
         //System.out.println("verifying cleanup:");
@@ -201,8 +201,12 @@ public class MergeSort {
             // if we reset the FleetProcess, restart it
             if (fp==null) fp = fleet.run(new Instruction[0]);
 
+            ShipPool pool = new ShipPool(fleet);
+            pool.allocateShip(mem1);
+            pool.allocateShip(mem2);
+
             // do the mergeSort
-            vals = MergeSort.mergeSort(fp, fleet, vals, vals_length, stride, mem1, mem2);
+            vals = MergeSort.mergeSort(fp, fleet, pool, vals, vals_length, stride, mem1, mem2);
 
             // verify the cleanup
             //CleanupUtils.verifyClean(fp);