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