1 package edu.berkeley.fleet.dataflow;
4 import edu.berkeley.fleet.loops.*;
5 import edu.berkeley.fleet.api.*;
6 import edu.berkeley.fleet.fpga.*;
7 import edu.berkeley.fleet.interpreter.*;
8 import org.ibex.graphics.*;
10 public class SortingDemo {
12 public static void main(String[] s) throws Exception {
13 //mergeSort(1024*64, 4, "Dvi",
14 mergeSort(1024*128, 4, "Dvi",
21 public static void main0(String[] s) throws Exception {
22 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream("stats.txt")));
24 for(int inflight=1; inflight <= 8; inflight++)
25 for(int count = 32; count < 2097152; count *= 2) {
26 System.out.println("==============================================================================");
27 System.out.println("count="+count);
28 System.out.println("inflight="+inflight);
29 //long time = timeit(count, inflight);
30 long time = mergeSort(count, inflight, "DDR2", 0);
31 pw.println(inflight + ", " + count + ", " + time);
36 public static long timeit(int count, int inflight) throws Exception {
37 Fleet fleet = Fleet.getDefaultImpl();
38 FleetProcess fp = fleet.run(new Instruction[0]);
39 ShipPool pool = new ShipPool(fleet);
40 //Program program = new Program(pool.allocateShip("Memory"));
41 CodeBag cb = new CodeBag(fleet);
43 Ship counter1 = pool.allocateShip("Counter");
44 Ship counter2 = pool.allocateShip("Counter");
46 Ship timer = pool.allocateShip("Timer");
47 Ship debug = pool.allocateShip("Debug");
51 lf = cb.loopFactory(debug.getDock("in"), 2);
56 // First counter //////////////////////////////////////////////////////////////////////////////
58 lf = cb.loopFactory(counter1.getDock("in1"), 1);
63 lf = cb.loopFactory(counter1.getDock("in2"), 1);
67 lf = cb.loopFactory(counter1.getDock("inOp"), 1);
71 lf = cb.loopFactory(counter1.getDock("out"), 0);
74 lf.sendWord(counter2.getDock("in2"));
77 // Second counter //////////////////////////////////////////////////////////////////////////////
79 lf = cb.loopFactory(counter2.getDock("in1"), 1);
87 lf = cb.loopFactory(counter2.getDock("in2"), 1);
88 for(int i=0; i<inflight; i++)
89 lf.sendToken(counter1.getDock("out"));
92 lf.sendToken(counter1.getDock("out"));
95 lf = cb.loopFactory(counter2.getDock("inOp"), 1);
96 lf.literal("DROP_C1_V2");
98 lf.literal("PASS_C1_V1");
101 lf = cb.loopFactory(counter2.getDock("out"), 1);
103 lf.sendToken(timer.getDock("out"));
106 // Timer //////////////////////////////////////////////////////////////////////////////
108 lf = cb.loopFactory(timer.getDock("out"), 1);
109 // collect twice just to be safe
112 lf.sendToken(counter1.getDock("in1"));
113 lf.sendWord(debug.getDock("in"));
115 // collect twice just to be safe
118 lf.sendWord(debug.getDock("in"));
120 Dock out = (Dock)counter1.getDock("out");
121 Dock in = (Dock)counter2.getDock("in2");
122 //System.out.println("distance is " + out.getPathLength((FpgaDestination)in.getDataDestination()));
123 //System.out.println("reverse distance is " + in.getPathLength((FpgaDestination)out.getDataDestination()));
125 for(Instruction i : cb.emit()) System.out.println(i);
126 cb.dispatch(fp, true);
127 long time1 = fp.recvWord().toLong();
128 System.out.println("got " + time1);
129 long time2 = fp.recvWord().toLong();
130 System.out.println("got " + time2);
131 System.out.println("diff=" + (time2-time1));
135 return (time2-time1);
138 public static long mergeSort(int vals_length, int inflight, String shipType, int clearAmount) throws Exception {
139 Node.CAPACITY = inflight;
141 Fleet fleet = Fleet.getDefaultImpl();
142 FleetProcess fp = fleet.run(new Instruction[0]);
143 ShipPool pool = new ShipPool(fleet);
144 Ship mem1 = pool.allocateShip(shipType);
147 randomizeMemory(fp, pool, mem1, 0, clearAmount, false);
149 //randomizeMemory(fp, pool, mem1, 0, vals_length, true);
151 BitVector[] bvs = new BitVector[vals_length];
154 Picture p = new Picture(new FileInputStream("campus.png"));
155 for(int y=0; y<(478/2); y++)
156 for(int x=0; x<544; x++) {
157 if (index >= vals_length) break;
158 int pixel = (x>=p.width) ? 0 : p.data[p.width*y+x];
159 long r = (pixel>>0) & 0xff;
160 long g = (pixel>>8) & 0xff;
161 long b = (pixel>>16) & 0xff;
168 bvs[(int)index] = new BitVector(fleet.getWordWidth()).set( r | (g<<6) | (b<<12) | (index<<18) );
172 for(; index<vals_length; index++) {
173 long tag = index<<18;
174 bvs[(int)index] = new BitVector(fleet.getWordWidth()).set( tag );
177 System.out.println("final index " + index);
179 Random random = new Random(System.currentTimeMillis());
180 for(int i=0; i<bvs.length*10; i++) {
181 int from = Math.abs(random.nextInt()) % bvs.length;
182 int to = Math.abs(random.nextInt()) % bvs.length;
183 BitVector bv = bvs[from];
189 MemoryUtils.writeMem(fp, pool, mem1, 40, bvs);
191 Ship mem = pool.allocateShip("Memory");
192 Program program = new Program(mem);
193 CodeBag cb_ = new MergeSort(fleet, program, pool, 2, mem1, mem1).makeInstance(offset, vals_length);
195 Ship button = pool.allocateShip("Button");
196 CodeBag cb = new CodeBag(fleet, program);
197 LoopFactory lf = cb.loopFactory(button.getDock("out"), 1);
199 lf.literal(cb_.getDescriptor());
200 lf.sendWord(program.getCBDDestination());
203 ShipPool pool2 = new ShipPool(fp.getFleet());
204 pool2.allocateShip(program.memoryShip);
206 long ret = program.run(fp, cb, pool2);
207 pool2.releaseShip(program.memoryShip);
210 // verify the cleanup?
211 //CleanupUtils.verifyClean(fp);
212 //MemoryUtils.readMem(fp, new ShipPool(fp.getFleet()), mem1, 0, bvs);
214 BitVector[] bvx = new BitVector[1024];
215 pool.allocateShip(mem);
216 MemoryUtils.readMem(fp, new ShipPool(fp.getFleet()), mem1, 0, bvx);
217 for(int i=0; i<bvx.length; i++)
218 System.out.println(bvx[i]);
220 System.out.println("results:");
221 for(int i=0; i<vals_length-1; i++)
222 if ( (bvs[i].toLong() & ~(-1L<<18)) != ~(-1L<<18))
223 System.out.println(bvs[i]);
226 for(int i=0; i<vals_length-1; i++) {
227 if (bvs[i].toLong() > bvs[i+1].toLong())
228 System.out.println("sort failure at "+i+":\n "+bvs[i]+"\n "+bvs[i+1]);
235 //static int offset = 32;
236 //static int offset = 544*2;
237 //static int offset = 544;
239 public static void randomizeMemory(FleetProcess fp, ShipPool pool_, Ship memory, long start, long length, boolean randomize) {
240 ShipPool pool = new ShipPool(pool_);
241 Ship mem = pool.allocateShip("Memory");
242 Program prog = new Program(mem);
244 DataFlowGraph dfg = new DataFlowGraph(fp.getFleet(), pool);
245 DownCounterNode dcn = new DownCounterNode(dfg);
246 dcn.start.connectOnce(length);
247 dcn.incr.connectOnce(1);
249 AluNode alu = new AluNode(dfg, "ADD");
250 alu.in1.connectForever(start);
251 alu.in2.connect(dcn.out);
253 MemoryNode mn = new MemoryNode(dfg, memory);
254 mn.inAddrWrite.connect(alu.out);
256 AluNode aluAnd = new AluNode(dfg, "AND");
258 aluAnd.in1.connect(new RandomNode(dfg).out);
260 //aluAnd.in1.connectForever( ~(-1L<<36) );
261 aluAnd.in1.connectForever( 0 );
263 aluAnd.in2.connectForever( ~(-1<<18) );
264 mn.inDataWrite.connect(aluAnd.out);
266 UnPunctuatorNode discard = new UnPunctuatorNode(dfg, true);
267 discard.count.connectOnce(length);
268 discard.val.connect(mn.outWrite);
269 DoneNode done = new DoneNode(dfg, prog);
270 discard.out.connect(done.in);
272 CodeBag cb = new CodeBag(fp.getFleet(), prog);
276 CodeBag cb2 = new CodeBag(fp.getFleet(), prog);
277 Ship button = fp.getFleet().getShip("Button",0);
279 LoopFactory lf = cb2.loopFactory(button.getDock("out"), 1);
281 lf.literal(prog.getEndProgramCodeBag().getDescriptor());
282 lf.sendWord(done.getDestinationToSendNextCodeBagDescriptorTo());
283 lf.literal(cb.getDescriptor());
284 lf.sendWord(prog.getCBDDestination());
287 System.out.println("dispatching randomization codebag...");
288 prog.run(fp, cb2, pool);
289 System.out.println(" randomization done.");