Scripts and Benchmarks changed to function with 8 machines
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / dsm / Em3d2.java
1 /** 
2  *
3  *
4  * Java implementation of the <tt>em3d</tt> Olden benchmark.  This Olden
5  * benchmark models the propagation of electromagnetic waves through
6  * objects in 3 dimensions. It is a simple computation on an irregular
7  * bipartite graph containing nodes representing electric and magnetic
8  * field values.
9  *
10  * <p><cite>
11  * D. Culler, A. Dusseau, S. Goldstein, A. Krishnamurthy, S. Lumetta, T. von 
12  * Eicken and K. Yelick. "Parallel Programming in Split-C".  Supercomputing
13  * 1993, pages 262-273.
14  * </cite>
15  **/
16 public class Em3d extends Thread {
17
18   /**
19    * The number of nodes (E and H) 
20    **/
21   private int numNodes;
22   /**
23    * The out-degree of each node.
24    **/
25   private int numDegree;
26   /**
27    * The number of compute iterations 
28    **/
29   private int numIter;
30   /**
31    * Should we print the results and other runtime messages
32    **/
33   private boolean printResult;
34     /**
35      * Print information messages?
36      **/
37   private boolean printMsgs;
38
39     int threadindex;
40     int numThreads;
41
42   BiGraph bg;
43   int upperlimit;
44   int lowerlimit;
45     public Em3d() {
46     }
47
48     public Em3d(BiGraph bg, int lowerlimit, int upperlimit, int numIter, int numDegree, int threadindex) {
49     this.bg = bg;
50     this.lowerlimit = lowerlimit;
51     this.upperlimit = upperlimit;
52     this.numIter = numIter;
53     this.numDegree = numDegree;
54     this.threadindex=threadindex;
55   }
56
57   public void run() {
58     int iteration;
59     Barrier barr;
60     int degree;
61     Random random;
62     String hname;
63
64     barr = new Barrier("128.195.175.84");
65     atomic {
66         iteration = numIter;
67         degree = numDegree;
68         random = new Random(lowerlimit);
69     }
70
71     atomic {
72         //This is going to conflict badly...Minimize work here
73         bg.allocateNodes ( lowerlimit, upperlimit, threadindex);
74     }
75     Barrier.enterBarrier(barr);
76     System.clearPrefetchCache();
77
78     atomic {
79         //initialize the eNodes
80         bg.initializeNodes(bg.eNodes, bg.hNodes, bg.hreversetable, lowerlimit, upperlimit, degree, random, threadindex);
81     }
82     Barrier.enterBarrier(barr);
83
84     atomic {
85         //initialize the hNodes
86         bg.initializeNodes(bg.hNodes, bg.eNodes, bg.ereversetable, lowerlimit, upperlimit, degree, random, threadindex);
87     }
88     Barrier.enterBarrier(barr);
89
90     atomic {
91         bg.makeFromNodes(bg.hNodes, bg.hreversetable, lowerlimit, upperlimit, random);
92     }
93     Barrier.enterBarrier(barr);
94
95     atomic {
96         bg.makeFromNodes(bg.eNodes, bg.ereversetable, lowerlimit, upperlimit, random);
97     }
98     Barrier.enterBarrier(barr);
99
100     //Do the computation
101     for (int i = 0; i < iteration; i++) {
102         /* for  eNodes */
103         atomic {
104             for(int j = lowerlimit; j<upperlimit; j++) {
105                 Node n = bg.eNodes[j];
106                 
107                 for (int k = 0; k < n.fromCount; k++) {
108                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
109                 }
110             }
111         }
112         
113         Barrier.enterBarrier(barr);
114         
115         /* for  hNodes */
116         atomic {
117             for(int j = lowerlimit; j<upperlimit; j++) {
118                 Node n = bg.hNodes[j];
119                 for (int k = 0; k < n.fromCount; k++) {
120                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
121                 }
122             }
123         }
124         Barrier.enterBarrier(barr);
125     }
126   }
127
128   /**
129    * The main roitine that creates the irregular, linked data structure
130    * that represents the electric and magnetic fields and propagates the
131    * waves through the graph.
132    * @param args the command line arguments
133    **/
134   public static void main(String args[]) {
135     Em3d em = new Em3d();
136     Em3d.parseCmdLine(args, em);
137     if (em.printMsgs) 
138       System.printString("Initializing em3d random graph...\n");
139     long start0 = System.currentTimeMillis();
140     int numThreads = em.numThreads;
141     int[] mid = new int[8];
142     mid[0] = (128<<24)|(195<<16)|(175<<8)|84;//dw-10
143     mid[1] = (128<<24)|(195<<16)|(175<<8)|85;//dw-11
144     mid[2] = (128<<24)|(195<<16)|(175<<8)|86;//dw-12
145     mid[3] = (128<<24)|(195<<16)|(175<<8)|87;//dw-13
146     mid[4] = (128<<24)|(195<<16)|(175<<8)|88;//dw-14
147     mid[5] = (128<<24)|(195<<16)|(175<<8)|89;//dw-15
148     mid[6] = (128<<24)|(195<<16)|(175<<8)|90;//dw-16
149     mid[7] = (128<<24)|(195<<16)|(175<<8)|91;//dw-17
150
151     System.printString("DEBUG -> numThreads = " + numThreads+"\n");
152     BarrierServer mybarr;
153     BiGraph graph;
154
155     
156     // initialization step 1: allocate BiGraph
157    // System.printString( "Allocating BiGraph.\n" );
158
159     atomic {
160       mybarr = global new BarrierServer(numThreads);
161       graph =  BiGraph.create(em.numNodes, em.numDegree, numThreads);
162     }
163     mybarr.start(mid[0]);
164
165
166     Em3dWrap[] em3d=new Em3dWrap[numThreads];    
167     int increment = em.numNodes/numThreads;
168
169
170     // initialization step 2: divide work of allocating nodes
171     // System.printString( "Launching distributed allocation of nodes.\n" );
172     
173     atomic {
174       int base=0;
175       for(int i=0;i<numThreads;i++) {
176           Em3d tmp;
177           if ((i+1)==numThreads)
178               tmp = global new Em3d(graph, base, em.numNodes, em.numIter, em.numDegree, i);
179           else
180               tmp = global new Em3d(graph, base, base+increment, em.numIter, em.numDegree, i);
181           em3d[i]=new Em3dWrap(tmp);
182           base+=increment;
183       }
184     }
185
186     boolean waitfordone=true;
187     while(waitfordone) {
188         atomic {
189             if (mybarr.done)
190                 waitfordone=false;
191         }
192     }
193
194     //System.printString("Starting Barrier run\n");
195     for(int i = 0; i<numThreads; i++) {
196       em3d[i].em3d.start(mid[i]);
197     }
198     for(int i = 0; i<numThreads; i++) {
199       em3d[i].em3d.join();
200     }
201     System.printString("Done!"+ "\n");
202   }
203
204
205   /**
206    * Parse the command line options.
207    * @param args the command line options.
208    **/
209
210   public static void parseCmdLine(String args[], Em3d em)
211   {
212     int i = 0;
213     String arg;
214
215     while (i < args.length && args[i].startsWith("-")) {
216       arg = args[i++];
217
218       // check for options that require arguments
219       if (arg.equals("-N")) {
220         if (i < args.length) {
221                 em.numNodes = new Integer(args[i++]).intValue();
222         }
223       } else if (arg.equals("-T")) {
224         if (i < args.length) {
225                 em.numThreads = new Integer(args[i++]).intValue();
226         }
227       } else if (arg.equals("-d")) {
228         if (i < args.length) {
229                 em.numDegree = new Integer(args[i++]).intValue();
230         }
231       } else if (arg.equals("-i")) {
232         if (i < args.length) {
233             em.numIter = new Integer(args[i++]).intValue();
234         }
235       } else if (arg.equals("-p")) {
236               em.printResult = true;
237       } else if (arg.equals("-m")) {
238               em.printMsgs = true;
239       } else if (arg.equals("-h")) {
240         em.usage();
241       }
242     }
243
244     if (em.numNodes == 0 || em.numDegree == 0) 
245       em.usage();
246   }
247
248   /**
249    * The usage routine which describes the program options.
250    **/
251   public void usage()
252   {
253     System.printString("usage: java Em3d -T <threads> -N <nodes> -d <degree> [-p] [-m] [-h]\n");
254     System.printString("    -N the number of nodes\n");
255     System.printString("    -T the number of threads\n");
256     System.printString("    -d the out-degree of each node\n");
257     System.printString("    -i the number of iterations\n");
258     System.printString("    -p (print detailed results\n)");
259     System.printString("    -m (print informative messages)\n");
260     System.printString("    -h (this message)\n");
261   }
262
263 }