start of new file
[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             /* for  eNodes */
47             Node prev = bg.eNodes;
48             Node curr = null;
49             for(int j = 0; j<lowerlimit; j++){
50                 curr = prev;
51                 prev = prev.next;
52             }
53             for(int j = lowerlimit; j<=upperlimit; j++) {
54                 Node n = curr;
55                 for (int k = 0; k < n.fromCount; k++) {
56                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
57                 }
58                 curr = curr.next;
59             }
60             Barrier.enterBarrier(mybarr);
61
62             /* for  hNodes */
63             prev = bg.hNodes;
64             curr = null;
65             for(int j = 0; j<lowerlimit; j++){
66                 curr = prev;
67                 prev = prev.next;
68             }
69             for(int j = lowerlimit; j<=upperlimit; j++) {
70                 Node n = curr;
71                 for (int k = 0; k < n.fromCount; k++) {
72                     n.value -= n.coeffs[k] * n.fromNodes[k].value;
73                 }
74                 curr = curr.next;
75             }
76             Barrier.enterBarrier(mybarr);
77         }
78     }
79         
80     /**
81      * The number of nodes (E and H) 
82      **/
83     private int numNodes;
84     /**
85      * The out-degree of each node.
86      **/
87     private int numDegree;
88     /**
89      * The number of compute iterations 
90      **/
91     private int numIter;
92     /**
93      * Should we print the results and other runtime messages
94      **/
95     private boolean printResult;
96     /**
97      * Print information messages?
98      **/
99     private boolean printMsgs;
100
101     /**
102      * The main roitine that creates the irregular, linked data structure
103      * that represents the electric and magnetic fields and propagates the
104      * waves through the graph.
105      * @param args the command line arguments
106      **/
107     public static void main(String args[])
108     {
109         Random rand = new Random(783);
110         Em3d em = new Em3d();
111         em.parseCmdLine(args, em);
112         if (em.printMsgs) 
113             System.out.println("Initializing em3d random graph...");
114         long start0 = System.currentTimeMillis();
115         BiGraph graph1 = new BiGraph();
116         int num_threads = 2;
117         Barrier mybarr = new Barrier(num_threads);
118         BiGraph graph = graph1.create(em.numNodes, em.numDegree, em.printResult, rand);
119         
120         long end0 = System.currentTimeMillis();
121
122         // compute a single iteration of electro-magnetic propagation
123         if (em.printMsgs) 
124             System.out.println("Propagating field values for " + em.numIter + 
125                     " iteration(s)...");
126         long start1 = System.currentTimeMillis();
127         Em3d[] em3d = new Em3d[num_threads];
128         //em3d[0] = new Em3d(graph, 1, em.numNodes, em.numIter, mybarr);
129         em3d[0] = new Em3d(graph, 1, em.numNodes/2, em.numIter, mybarr);
130         em3d[1] = new Em3d(graph, (em.numNodes/2)+1, em.numNodes, em.numIter, mybarr);
131         for(int i = 0; i<num_threads; i++) {
132             em3d[i].start();
133         }
134         for(int i = 0; i<num_threads; i++) {
135             try {
136                 em3d[i].join();
137             } catch (InterruptedException e) {}
138         }
139         long end1 = System.currentTimeMillis();
140
141         // print current field values
142         if (em.printResult) {
143             System.out.println(graph);
144         }
145
146         if (em.printMsgs) {
147             System.out.println("EM3D build time "+ (end0 - start0)/1000.0);
148             System.out.println("EM3D compute time " + (end1 - start1)/1000.0);
149             System.out.println("EM3D total time " + (end1 - start0)/1000.0);
150         }
151         System.out.println("Done!");
152     }
153
154
155     /**
156      * Parse the command line options.
157      * @param args the command line options.
158      **/
159
160     public void parseCmdLine(String args[], Em3d em)
161     {
162         int i = 0;
163         String arg;
164
165         while (i < args.length && args[i].startsWith("-")) {
166             arg = args[i++];
167
168             // check for options that require arguments
169             if (arg.equals("-n")) {
170                 if (i < args.length) {
171                     em.numNodes = new Integer(args[i++]).intValue();
172                 }
173             } else if (arg.equals("-d")) {
174                 if (i < args.length) {
175                     em.numDegree = new Integer(args[i++]).intValue();
176                 }
177             } else if (arg.equals("-i")) {
178                 if (i < args.length) {
179                     em.numIter = new Integer(args[i++]).intValue();
180                 }
181             } else if (arg.equals("-p")) {
182                 em.printResult = true;
183             } else if (arg.equals("-m")) {
184                 em.printMsgs = true;
185             } else if (arg.equals("-h")) {
186                 em.usage();
187             }
188         }
189         if (em.numNodes == 0 || em.numDegree == 0) 
190             em.usage();
191     }
192
193     /**
194      * The usage routine which describes the program options.
195      **/
196     public void usage()
197     {
198         System.out.println("usage: java Em3d -n <nodes> -d <degree> [-p] [-m] [-h]");
199         System.out.println("    -n the number of nodes");
200         System.out.println("    -d the out-degree of each node");
201         System.out.println("    -i the number of iterations");
202         System.out.println("    -p (print detailed results)");
203         System.out.println("    -m (print informative messages)");
204         System.out.println("    -h (this message)");
205     }
206
207 }