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