8ca449d1d0916226a8ac366224e8fefc625cf726
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / java / Em3d.java
1 import java.io.*;
2 import java.util.Random;
3
4 /** 
5  *
6  *
7  * Java implementation of the <tt>em3d</tt> Olden benchmark.  This Olden
8  * benchmark models the propagation of electromagnetic waves through
9  * objects in 3 dimensions. It is a simple computation on an irregular
10  * bipartite graph containing nodes representing electric and magnetic
11  * field values.
12  *
13  * <p><cite>
14  * D. Culler, A. Dusseau, S. Goldstein, A. Krishnamurthy, S. Lumetta, T. von 
15  * Eicken and K. Yelick. "Parallel Programming in Split-C".  Supercomputing
16  * 1993, pages 262-273.
17  * </cite>
18  **/
19 public class Em3d extends Thread
20 {
21
22     BiGraph bg;
23     int upperlimit;
24     int lowerlimit;
25     Random rand;
26     Barrier mybarr;
27
28     public Em3d() {
29         numNodes = 0;
30         numDegree = 0;
31         numIter = 1;
32         printResult = false;
33         printMsgs = false;
34     }
35
36     public Em3d(BiGraph bg, int lowerlimit, int upperlimit, int numIter, Barrier mybarr) {
37         this.bg = bg;
38         this.lowerlimit = lowerlimit;
39         this.upperlimit = upperlimit;
40         this.numIter = numIter;
41         this.mybarr = mybarr;
42     }
43
44     public void run() {
45         for (int i = 0; i < numIter; i++) {
46             Barrier runBarrier = new Barrier();
47             /* for  eNodes */
48             Node prev = bg.eNodes;
49             Node curr = null;
50             for(int j = 0; j<lowerlimit; j++){
51                 curr = prev;
52                 prev = prev.next;
53             }
54             for(int j = lowerlimit; j<=upperlimit; j++) {
55                 Node n = curr;
56                 for (int k = 0; k < n.fromCount; k++) {
57                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
58                 }
59                 curr = curr.next;
60             }
61             runBarrier.enterBarrier(mybarr);
62             //mybarr.reset();
63
64             /* for  hNodes */
65             prev = bg.hNodes;
66             curr = null;
67             for(int j = 0; j<lowerlimit; j++){
68                 curr = prev;
69                 prev = prev.next;
70             }
71             for(int j = lowerlimit; j<=upperlimit; j++) {
72                 Node n = curr;
73                 for (int k = 0; k < n.fromCount; k++) {
74                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
75                 }
76                 curr = curr.next;
77             }
78             runBarrier.enterBarrier(mybarr);
79             //mybarr.reset();
80         }
81     }
82         
83     /**
84      * The number of nodes (E and H) 
85      **/
86     private int numNodes;
87     /**
88      * The out-degree of each node.
89      **/
90     private int numDegree;
91     /**
92      * The number of compute iterations 
93      **/
94     private int numIter;
95     /**
96      * Should we print the results and other runtime messages
97      **/
98     private boolean printResult;
99     /**
100      * Print information messages?
101      **/
102     private boolean printMsgs;
103
104     /**
105      * The main roitine that creates the irregular, linked data structure
106      * that represents the electric and magnetic fields and propagates the
107      * waves through the graph.
108      * @param args the command line arguments
109      **/
110     public static void main(String args[])
111     {
112         Random rand = new Random(783);
113         Em3d em = new Em3d();
114         em.parseCmdLine(args, em);
115         if (em.printMsgs) 
116             System.out.println("Initializing em3d random graph...");
117         long start0 = System.currentTimeMillis();
118         BiGraph graph1 = new BiGraph();
119         int num_threads = 2;
120         Barrier mybarr = new Barrier(num_threads);
121         System.out.println("DEBUG -> num_threads = " + num_threads);
122         BiGraph graph = graph1.create(em.numNodes, em.numDegree, em.printResult, rand);
123         
124         long end0 = System.currentTimeMillis();
125
126         // compute a single iteration of electro-magnetic propagation
127         if (em.printMsgs) 
128             System.out.println("Propagating field values for " + em.numIter + 
129                     " iteration(s)...");
130         long start1 = System.currentTimeMillis();
131         Em3d[] em3d = new Em3d[num_threads];
132         //em3d[0] = new Em3d(graph, 1, em.numNodes, em.numIter, mybarr);
133         em3d[0] = new Em3d(graph, 1, em.numNodes/2, em.numIter, mybarr);
134         em3d[1] = new Em3d(graph, (em.numNodes/2)+1, em.numNodes, em.numIter, mybarr);
135         for(int i = 0; i<num_threads; i++) {
136             em3d[i].start();
137         }
138         for(int i = 0; i<num_threads; i++) {
139             try {
140                 em3d[i].join();
141             } catch (InterruptedException e) {}
142         }
143         long end1 = System.currentTimeMillis();
144
145         // print current field values
146         if (em.printResult) {
147             System.out.println(graph);
148         }
149
150         if (em.printMsgs) {
151             System.out.println("EM3D build time "+ (end0 - start0)/1000.0);
152             System.out.println("EM3D compute time " + (end1 - start1)/1000.0);
153             System.out.println("EM3D total time " + (end1 - start0)/1000.0);
154         }
155         System.out.println("Done!");
156     }
157
158
159     /**
160      * Parse the command line options.
161      * @param args the command line options.
162      **/
163
164     public void parseCmdLine(String args[], Em3d em)
165     {
166         int i = 0;
167         String arg;
168
169         while (i < args.length && args[i].startsWith("-")) {
170             arg = args[i++];
171
172             // check for options that require arguments
173             if (arg.equals("-n")) {
174                 if (i < args.length) {
175                     em.numNodes = new Integer(args[i++]).intValue();
176                 }
177             } else if (arg.equals("-d")) {
178                 if (i < args.length) {
179                     em.numDegree = new Integer(args[i++]).intValue();
180                 }
181             } else if (arg.equals("-i")) {
182                 if (i < args.length) {
183                     em.numIter = new Integer(args[i++]).intValue();
184                 }
185             } else if (arg.equals("-p")) {
186                 em.printResult = true;
187             } else if (arg.equals("-m")) {
188                 em.printMsgs = true;
189             } else if (arg.equals("-h")) {
190                 em.usage();
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.out.println("usage: java Em3d -n <nodes> -d <degree> [-p] [-m] [-h]");
203         System.out.println("    -n the number of nodes");
204         System.out.println("    -d the out-degree of each node");
205         System.out.println("    -i the number of iterations");
206         System.out.println("    -p (print detailed results)");
207         System.out.println("    -m (print informative messages)");
208         System.out.println("    -h (this message)");
209     }
210
211 }