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