changes for the new build
[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
63     barr = new Barrier("128.195.136.162");
64     atomic {
65         iteration = numIter;
66         degree = numDegree;
67         random = new Random(lowerlimit);
68     }
69
70     atomic {
71         //This is going to conflict badly...Minimize work here
72         bg.allocateNodes ( lowerlimit, upperlimit, threadindex);
73     }
74     Barrier.enterBarrier(barr);
75     System.clearPrefetchCache();
76
77     atomic {
78         //initialize the eNodes
79         bg.initializeNodes(bg.eNodes, bg.hNodes, bg.hreversetable, lowerlimit, upperlimit, degree, random, threadindex);
80     }
81     Barrier.enterBarrier(barr);
82
83     atomic {
84         //initialize the hNodes
85         bg.initializeNodes(bg.hNodes, bg.eNodes, bg.ereversetable, lowerlimit, upperlimit, degree, random, threadindex);
86     }
87     Barrier.enterBarrier(barr);
88
89     atomic {
90         bg.makeFromNodes(bg.hNodes, bg.hreversetable, lowerlimit, upperlimit, random);
91     }
92     Barrier.enterBarrier(barr);
93
94     atomic {
95         bg.makeFromNodes(bg.eNodes, bg.ereversetable, lowerlimit, upperlimit, random);
96     }
97     Barrier.enterBarrier(barr);
98
99     //Do the computation
100     for (int i = 0; i < iteration; i++) {
101         /* for  eNodes */
102         atomic {
103             for(int j = lowerlimit; j<upperlimit; j++) {
104                 Node n = bg.eNodes[j];
105                 
106                 for (int k = 0; k < n.fromCount; k++) {
107                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
108                 }
109             }
110         }
111         
112         Barrier.enterBarrier(barr);
113         
114         /* for  hNodes */
115         atomic {
116             for(int j = lowerlimit; j<upperlimit; j++) {
117                 Node n = bg.hNodes[j];
118                 for (int k = 0; k < n.fromCount; k++) {
119                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
120                 }
121             }
122         }
123         Barrier.enterBarrier(barr);
124     }
125   }
126
127   /**
128    * The main roitine that creates the irregular, linked data structure
129    * that represents the electric and magnetic fields and propagates the
130    * waves through the graph.
131    * @param args the command line arguments
132    **/
133   public static void main(String args[]) {
134     Em3d em = new Em3d();
135     Em3d.parseCmdLine(args, em);
136     if (em.printMsgs) 
137       System.printString("Initializing em3d random graph...\n");
138     long start0 = System.currentTimeMillis();
139     int numThreads = em.numThreads;
140     int[] mid = new int[8];
141     mid[0] = (128<<24)|(195<<16)|(136<<8)|162; //dw-10
142     mid[1] = (128<<24)|(195<<16)|(136<<8)|163; //dw-11
143     mid[2] = (128<<24)|(195<<16)|(136<<8)|164; //dw-12
144     mid[3] = (128<<24)|(195<<16)|(136<<8)|165; //dw-13
145     mid[4] = (128<<24)|(195<<16)|(136<<8)|166; //dw-14
146     mid[5] = (128<<24)|(195<<16)|(136<<8)|167; //dw-15
147     mid[6] = (128<<24)|(195<<16)|(136<<8)|168; //dw-16
148     mid[7] = (128<<24)|(195<<16)|(136<<8)|169; //dw-17
149
150     System.printString("DEBUG -> numThreads = " + numThreads+"\n");
151     BarrierServer mybarr;
152     BiGraph graph;
153
154     
155     // initialization step 1: allocate BiGraph
156    // System.printString( "Allocating BiGraph.\n" );
157
158     atomic {
159       mybarr = global new BarrierServer(numThreads);
160       graph =  BiGraph.create(em.numNodes, em.numDegree, numThreads);
161     }
162     mybarr.start(mid[0]);
163
164
165     Em3dWrap[] em3d=new Em3dWrap[numThreads];    
166     int increment = em.numNodes/numThreads;
167
168
169     // initialization step 2: divide work of allocating nodes
170     // System.printString( "Launching distributed allocation of nodes.\n" );
171     
172     atomic {
173       int base=0;
174       for(int i=0;i<numThreads;i++) {
175           Em3d tmp;
176           if ((i+1)==numThreads)
177               tmp = global new Em3d(graph, base, em.numNodes, em.numIter, em.numDegree, i);
178           else
179               tmp = global new Em3d(graph, base, base+increment, em.numIter, em.numDegree, i);
180           em3d[i]=new Em3dWrap(tmp);
181           base+=increment;
182       }
183     }
184
185     boolean waitfordone=true;
186     while(waitfordone) {
187         atomic {
188             if (mybarr.done)
189                 waitfordone=false;
190         }
191     }
192
193     //System.printString("Starting Barrier run\n");
194     for(int i = 0; i<numThreads; i++) {
195       em3d[i].em3d.start(mid[i]);
196     }
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 }