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