revamp demos
[slipway.git] / src / edu / berkeley / slipway / demos / MicropipelineFifoDemo.java
1 package edu.berkeley.slipway.demos;
2
3 import java.io.*;
4 import java.util.*;
5 import java.awt.*;
6 import com.atmel.fpslic.*;
7 import edu.berkeley.slipway.*;
8 import edu.berkeley.slipway.gui.*;
9 import static com.atmel.fpslic.FpslicConstants.*;
10
11
12 /**
13  *  This demo runs the asynchronous micropipeline fifo experiment from
14  *  the FCCM paper.
15  *
16  *  Output is placed in misc/data/async/ as a collection of .csv
17  *  files.  Each file is named sizeXXX.csv, where XXX is the capacity
18  *  of the fifo.  Each line of each file is of the form
19  *  occupancy,tokenrate where occupancy is the proportion of the fifo
20  *  which is occupied (a number between 0 and 1) and tokenrate is the
21  *  number of millions of tokens per second observed at a fixed point
22  *  on the ring.  All files should be concatenated in order to
23  *  reproduce the graphs in the paper.
24  */
25 public abstract class MicropipelineFifoDemo {
26
27     public SlipwayBoard slipway;
28     public FpslicDevice fpslic;
29     public FpslicDevice.Cell start;
30
31     // Abstract methods to implement //////////////////////////////////////////////////////////////////////////////
32
33     protected abstract int  numDivisors();
34     protected abstract void forceMasterSuccessor(boolean high);
35     protected abstract void unForceMasterSuccessor(boolean state);
36     protected abstract void unPauseMaster();
37     protected abstract void unPauseSlaves();
38     protected abstract void pauseMaster();
39     protected abstract void pauseSlaves();
40     protected abstract void resetAll();
41     protected abstract int  init(int size);
42     protected abstract int  init(int size, FpslicDevice.Cell start);
43
44     // Constructors //////////////////////////////////////////////////////////////////////////////
45
46     public MicropipelineFifoDemo() throws Exception {
47         System.err.println("MicropipelineFifoDemo: initializing board...");
48         slipway = new SlipwayBoard();
49         fpslic = slipway.getFpslicDevice();
50     }
51
52     public void mainx(String[] s) throws Exception {
53         System.err.println("MicropipelineFifoDemo: setting up scan cell...");
54         
55         //if (s.length > 0 && s[0].equals("-g")) {
56
57         /*
58         Gui vis = new Gui(fpslic, slipway, fpslic.getWidth(), fpslic.getHeight());
59             Frame fr = new Frame();
60             fr.setTitle("SLIPWAY Live Fabric Debugger");
61             fr.addKeyListener(vis);
62             fr.setLayout(new BorderLayout());
63             fr.add(vis, BorderLayout.CENTER);
64             fr.pack();
65             fr.setSize(900, 900);
66             vis.repaint();
67             fr.repaint();
68             fr.show();
69         */
70             //}
71     
72         ExperimentUtils.setupScanCell(fpslic);
73
74         for(int i=0; i<255; i++) {
75             slipway.readInterruptCount();
76             System.err.print("\rMicropipelineFifoDemo: paranoia -- flushing interrupt count: " + i + "/254 ");
77         }
78         System.err.println();
79
80         for(int i=1; i<402; i+=2) go(i);
81         System.err.println("MicropipelineFifoDemo: experiment is finished");
82     }
83
84
85     // Experiment Logic //////////////////////////////////////////////////////////////////////////////
86
87     /** drain the fifo */
88     protected void drain() {
89         while(true){
90             pauseMaster();
91             pauseSlaves();
92             resetAll();
93             unPauseMaster();
94             unPauseSlaves();
95             slipway.readInterruptCount();
96             try { Thread.sleep(100); } catch (Exception e) { }
97             int rc = slipway.readInterruptCount();
98             if (rc!=0) {
99                 System.err.println("flush() failed => " + rc);
100                 try { Thread.sleep(1000); } catch (Exception e) { }
101                 continue;
102             }
103             break;
104         }
105     }
106
107     /** fill the fifo with "count" tokens */
108     protected void fill(int count) {
109         boolean yes = false;
110         for(int i=0; i<count; i++) {
111             pauseSlaves();
112             forceMasterSuccessor(yes);
113             unPauseSlaves();
114             yes = !yes;
115         }
116         if (count>0)
117             unForceMasterSuccessor(!yes);
118     }
119
120     public void go(int size) throws Exception {
121         int rsize = init(size, fpslic.cell(20, 20));
122
123         String sizes = rsize+"";
124         while(sizes.length()<3) sizes = "0"+sizes;
125         String fname = "misc/data/async/size"+sizes+".csv";
126         if (!new File(fname).exists()) {
127             System.err.println();
128             System.err.println("MicropipelineFifoDemo:   fifo size is "+rsize+"...");
129             PrintWriter outfile = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fname)));
130             for(int i=rsize; i>=0; i-=2)
131                 test(i, rsize, outfile);
132             outfile.flush();
133             outfile.close();
134         } else {
135             System.out.println("MicropipelineFifoDemo: file " + fname + " already exists; skipping");
136         }
137     }
138
139     public void test(int count, int size, PrintWriter outfile) throws Exception {
140         double[] results = new double[numtrials];
141
142       
143         int clockdivisor = 64;
144         double occupancy = ((double)count)/((double)size);
145         int clockrate = 24; // (in mhz)
146
147         for(int i=0; i<results.length; i++) {
148             init(size);
149             fpslic.flush();
150             drain();
151             fpslic.flush();
152             fill(count);
153             fpslic.flush();
154
155             unPauseMaster();
156             unPauseSlaves();
157             fpslic.flush();
158
159             slipway.readInterruptCount();
160             Thread.sleep(1000);
161             int tokens = slipway.readInterruptCount();
162             
163             double elapsed = (double)(slipway.readInterruptCountTime()/clockrate);
164             
165             int multiplier = 1;
166             for(int j=0; j<numDivisors(); j++) multiplier *= 2;
167             multiplier /= clockdivisor;
168
169             double result = (tokens*multiplier)/elapsed; // in millions
170             results[i] = result;
171         }
172
173         double max = 0;
174         double min = Double.MAX_VALUE;
175         double total = 0;
176         for(int i=0; i<numtrials; i++) {
177             max = Math.max(max, results[i]);
178             min = Math.min(min, results[i]);
179             total += results[i];
180         }
181         total -= max;
182         total -= min;
183         total /= (numtrials-2);
184
185         // result is transitions/sec
186         outfile.println(size + ", " + occupancy + ", " + total);
187         outfile.flush();
188         System.out.println("num_tokens/capacity: "+count+"/"+size+
189                            "  occupancy="+((int)(occupancy*100.0)) +"%"+
190                            "  tokenrate=" + total);
191     }
192     private static final int numtrials = 5;
193
194     protected FpslicDevice.Cell topLeft() { return start.north().north(); }
195
196
197
198 }
199
200