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