Latest version of Em3d
[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         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, lowerlimit, upperlimit, degree, random, threadindex);
81     }
82     Barrier.enterBarrier(barr);
83
84     atomic {
85         //initialize the hNodes
86         bg.initializeNodes(bg.hNodes, bg.eNodes, lowerlimit, upperlimit, degree, random, threadindex);
87     }
88     Barrier.enterBarrier(barr);
89
90     atomic {
91         bg.makeFromNodes(bg.hNodes, lowerlimit, upperlimit, random);
92     }
93     Barrier.enterBarrier(barr);
94
95     atomic {
96         bg.makeFromNodes(bg.eNodes, 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[4];
142     mid[0] = (128<<24)|(195<<16)|(175<<8)|79;//dw-1
143     mid[1] = (128<<24)|(195<<16)|(175<<8)|73;//dw-2
144     mid[2] = (128<<24)|(195<<16)|(175<<8)|78;
145     mid[3] = (128<<24)|(195<<16)|(175<<8)|69;
146
147     System.printString("DEBUG -> numThreads = " + numThreads+"\n");
148     BarrierServer mybarr;
149     BiGraph graph;
150
151     
152     // initialization step 1: allocate BiGraph
153    // System.printString( "Allocating BiGraph.\n" );
154
155     atomic {
156       mybarr = global new BarrierServer(numThreads);
157       graph =  BiGraph.create(em.numNodes, em.numDegree, numThreads);
158     }
159     mybarr.start(mid[0]);
160
161
162     Em3dWrap[] em3d=new Em3dWrap[numThreads];    
163     int increment = em.numNodes/numThreads;
164
165
166     // initialization step 2: divide work of allocating nodes
167     // System.printString( "Launching distributed allocation of nodes.\n" );
168     
169     atomic {
170       int base=0;
171       for(int i=0;i<numThreads;i++) {
172           Em3d tmp;
173           if ((i+1)==numThreads)
174               tmp = global new Em3d(graph, base, em.numNodes, em.numIter, em.numDegree, i);
175           else
176               tmp = global new Em3d(graph, base, base+increment, em.numIter, em.numDegree, i);
177           em3d[i]=new Em3dWrap(tmp);
178           base+=increment;
179       }
180     }
181
182     boolean waitfordone=true;
183     while(waitfordone) {
184         atomic {
185             if (mybarr.done)
186                 waitfordone=false;
187         }
188     }
189
190     //System.printString("Starting Barrier run\n");
191     for(int i = 0; i<numThreads; i++) {
192       em3d[i].em3d.start(mid[i]);
193     }
194     for(int i = 0; i<numThreads; i++) {
195       em3d[i].em3d.join();
196     }
197     System.printString("Done!"+ "\n");
198   }
199
200
201   /**
202    * Parse the command line options.
203    * @param args the command line options.
204    **/
205
206   public static void parseCmdLine(String args[], Em3d em)
207   {
208     int i = 0;
209     String arg;
210
211     while (i < args.length && args[i].startsWith("-")) {
212       arg = args[i++];
213
214       // check for options that require arguments
215       if (arg.equals("-N")) {
216         if (i < args.length) {
217                 em.numNodes = new Integer(args[i++]).intValue();
218         }
219       } else if (arg.equals("-T")) {
220         if (i < args.length) {
221                 em.numThreads = new Integer(args[i++]).intValue();
222         }
223       } else if (arg.equals("-d")) {
224         if (i < args.length) {
225                 em.numDegree = new Integer(args[i++]).intValue();
226         }
227       } else if (arg.equals("-i")) {
228         if (i < args.length) {
229             em.numIter = new Integer(args[i++]).intValue();
230         }
231       } else if (arg.equals("-p")) {
232               em.printResult = true;
233       } else if (arg.equals("-m")) {
234               em.printMsgs = true;
235       } else if (arg.equals("-h")) {
236         em.usage();
237       }
238     }
239
240     if (em.numNodes == 0 || em.numDegree == 0) 
241       em.usage();
242   }
243
244   /**
245    * The usage routine which describes the program options.
246    **/
247   public void usage()
248   {
249     System.printString("usage: java Em3d -T <threads> -N <nodes> -d <degree> [-p] [-m] [-h]\n");
250     System.printString("    -N the number of nodes\n");
251     System.printString("    -T the number of threads\n");
252     System.printString("    -d the out-degree of each node\n");
253     System.printString("    -i the number of iterations\n");
254     System.printString("    -p (print detailed results\n)");
255     System.printString("    -m (print informative messages)\n");
256     System.printString("    -h (this message)\n");
257   }
258
259 }