still have some compile errors . Use Em3d with new Barrier class
[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  //Barrier barr;
46  String hostname;
47     public Em3d() {
48     }
49
50     public Em3d(BiGraph bg, int lowerlimit, int upperlimit, int numIter, int numDegree, int threadindex, String name) {
51     this.bg = bg;
52     this.lowerlimit = lowerlimit;
53     this.upperlimit = upperlimit;
54     this.numIter = numIter;
55     this.hostname = name;
56     this.numDegree = numDegree;
57     this.threadindex=threadindex;
58   }
59
60   public void run() {
61     int iteration;
62     Barrier barr;
63     int degree;
64     Random random;
65
66     barr = new Barrier(hostname);
67     atomic {
68         iteration = numIter;
69         degree = numDegree;
70         random = new Random(lowerlimit);
71     }
72
73     atomic {
74         //This is going to conflict badly...Minimize work here
75         bg.allocateNodes ( lowerlimit, upperlimit, threadindex);
76     }
77     Barrier.enterBarrier(barr);
78     System.clearPrefetchCache();
79
80     atomic {
81         //initialize the eNodes
82         bg.initializeNodes(bg.eNodes, bg.hNodes, lowerlimit, upperlimit, degree, random, threadindex);
83     }
84     Barrier.enterBarrier(barr);
85
86     atomic {
87         //initialize the hNodes
88         bg.initializeNodes(bg.hNodes, bg.eNodes, lowerlimit, upperlimit, degree, random, threadindex);
89     }
90     Barrier.enterBarrier(barr);
91
92     atomic {
93         bg.makeFromNodes(bg.hNodes, lowerlimit, upperlimit, random);
94     }
95     Barrier.enterBarrier(barr);
96
97     atomic {
98         bg.makeFromNodes(bg.eNodes, lowerlimit, upperlimit, random);
99     }
100     Barrier.enterBarrier(barr);
101
102     //Do the computation
103     for (int i = 0; i < iteration; i++) {
104         /* for  eNodes */
105         atomic {
106             for(int j = lowerlimit; j<upperlimit; j++) {
107                 Node n = bg.eNodes[j];
108                 
109                 for (int k = 0; k < n.fromCount; k++) {
110                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
111                 }
112             }
113         }
114         
115         Barrier.enterBarrier(barr);
116         
117         /* for  hNodes */
118         atomic {
119             for(int j = lowerlimit; j<upperlimit; j++) {
120                 Node n = bg.hNodes[j];
121                 for (int k = 0; k < n.fromCount; k++) {
122                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
123                 }
124             }
125         }
126         Barrier.enterBarrier(barr);
127     }
128   }
129
130   /**
131    * The main roitine that creates the irregular, linked data structure
132    * that represents the electric and magnetic fields and propagates the
133    * waves through the graph.
134    * @param args the command line arguments
135    **/
136   public static void main(String args[]) {
137     Em3d em = new Em3d();
138     Em3d.parseCmdLine(args, em);
139     if (em.printMsgs) 
140       System.printString("Initializing em3d random graph...\n");
141     long start0 = System.currentTimeMillis();
142     int numThreads = em.numThreads;
143     int[] mid = new int[4];
144     String[] hostname;
145     atomic {
146       hostname = global new String[4];
147       hostname[0] = global new String("128.195.175.79");
148       hostname[1] =  global new String("128.195.175.73");
149       hostname[2] = global new String("128.195.175.78");
150       hostname[3] = global new String("128.195.175.69");
151     }
152     mid[0] = (128<<24)|(195<<16)|(175<<8)|79;//dw-1
153     mid[1] = (128<<24)|(195<<16)|(175<<8)|73;//dw-2
154     mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
155     mid[3] = (128<<24)|(195<<16)|(175<<8)|69;
156
157     System.printString("DEBUG -> numThreads = " + numThreads+"\n");
158     BarrierServer mybarr;
159     BiGraph graph;
160
161     
162     // initialization step 1: allocate BiGraph
163    // System.printString( "Allocating BiGraph.\n" );
164
165     atomic {
166       mybarr = global new BarrierServer(numThreads);
167       graph =  BiGraph.create(em.numNodes, em.numDegree, numThreads);
168     }
169
170     Em3dWrap[] em3d=new Em3dWrap[numThreads];    
171     int increment = em.numNodes/numThreads;
172
173
174     // initialization step 2: divide work of allocating nodes
175     // System.printString( "Launching distributed allocation of nodes.\n" );
176     
177     atomic {
178       int base=0;
179       for(int i=0;i<numThreads;i++) {
180           Em3d tmp;
181           if ((i+1)==numThreads)
182               tmp = global new Em3d(graph, base, em.numNodes, em.numIter, em.numDegree, i, hostname[0]);
183           else
184               tmp = global new Em3d(graph, base, base+increment, em.numIter, em.numDegree, i, hostname[0]);
185           em3d[i]=new Em3dWrap(tmp);
186           base+=increment;
187       }
188     }
189
190     //TODO check if correct
191     mybarr.start(mid[0]);
192     for(int i = 0; i<numThreads; i++) {
193       em3d[i].em3d.start(mid[i]);
194     }
195
196     mybarr.join();
197     for(int i = 0; i<numThreads; i++) {
198       em3d[i].em3d.join();
199     }
200     System.printString("Done!"+ "\n");
201   }
202
203
204   /**
205    * Parse the command line options.
206    * @param args the command line options.
207    **/
208
209   public static void parseCmdLine(String args[], Em3d em)
210   {
211     int i = 0;
212     String arg;
213
214     while (i < args.length && args[i].startsWith("-")) {
215       arg = args[i++];
216
217       // check for options that require arguments
218       if (arg.equals("-N")) {
219         if (i < args.length) {
220                 em.numNodes = new Integer(args[i++]).intValue();
221         }
222       } else if (arg.equals("-T")) {
223         if (i < args.length) {
224                 em.numThreads = new Integer(args[i++]).intValue();
225         }
226       } else if (arg.equals("-d")) {
227         if (i < args.length) {
228                 em.numDegree = new Integer(args[i++]).intValue();
229         }
230       } else if (arg.equals("-i")) {
231         if (i < args.length) {
232             em.numIter = new Integer(args[i++]).intValue();
233         }
234       } else if (arg.equals("-p")) {
235               em.printResult = true;
236       } else if (arg.equals("-m")) {
237               em.printMsgs = true;
238       } else if (arg.equals("-h")) {
239         em.usage();
240       }
241     }
242
243     if (em.numNodes == 0 || em.numDegree == 0) 
244       em.usage();
245   }
246
247   /**
248    * The usage routine which describes the program options.
249    **/
250   public void usage()
251   {
252     System.printString("usage: java Em3d -T <threads> -N <nodes> -d <degree> [-p] [-m] [-h]\n");
253     System.printString("    -N the number of nodes\n");
254     System.printString("    -T the number of threads\n");
255     System.printString("    -d the out-degree of each node\n");
256     System.printString("    -i the number of iterations\n");
257     System.printString("    -p (print detailed results\n)");
258     System.printString("    -m (print informative messages)\n");
259     System.printString("    -h (this message)\n");
260   }
261
262 }