java version of Em3d benchmark
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / java / BiGraph.java
1 import java.util.Random;
2 /** 
3  * A class that represents the irregular bipartite graph used in
4  * EM3D.  The graph contains two linked structures that represent the
5  * E nodes and the N nodes in the application.
6  **/
7 public class BiGraph
8 {
9   public BiGraph() {
10   }
11   /**
12    * Nodes that represent the electrical field.
13    **/
14   Node eNodes;
15   /**
16    * Nodes that representhe the magnetic field.
17    **/
18   Node hNodes;
19
20   /**
21    * Construct the bipartite graph.
22    * @param e the nodes representing the electric fields
23    * @param h the nodes representing the magnetic fields
24    **/ 
25   BiGraph(Node e, Node h)
26   {
27       eNodes = e;
28       hNodes = h;
29   }
30
31   /**
32    * Create the bi graph that contains the linked list of
33    * e and h nodes.
34    * @param numNodes the number of nodes to create
35    * @param numDegree the out-degree of each node
36    * @param verbose should we print out runtime messages
37    * @return the bi graph that we've created.
38    **/
39
40   BiGraph create(int numNodes, int numDegree, boolean verbose, Random r)
41   {
42     Node newnode = new Node();
43
44     // making nodes (we create a table)
45     if (verbose) System.out.println("making nodes (tables in orig. version)");
46     Node[] hTable = newnode.fillTable(numNodes, numDegree, r);
47     Node[] eTable = newnode.fillTable(numNodes, numDegree, r);
48
49     // making neighbors
50     if (verbose) System.out.println("updating from and coeffs");
51     for(int i = 0; i< numNodes; i++) {
52       Node n = hTable[i];
53       n.makeUniqueNeighbors(eTable, r);
54     }
55
56     for (int i = 0; i < numNodes; i++) {
57       Node n = eTable[i];
58       n.makeUniqueNeighbors(hTable, r);
59     }
60
61     // Create the fromNodes and coeff field
62     if (verbose) System.out.println("filling from fields");
63     for(int i = 0; i< numNodes; i++) {
64       Node n = hTable[i];
65       n.makeFromNodes();
66     }
67
68     for (int i = 0; i < numNodes; i++) {
69       Node n = eTable[i];
70       n.makeFromNodes();
71     }
72
73     // Update the fromNodes
74     for (int i = 0; i < numNodes; i++) {
75       Node n = hTable[i];
76       n.updateFromNodes(r);
77     }
78     for (int i = 0; i < numNodes; i++) {
79       Node n = eTable[i];
80       n.updateFromNodes(r);
81     }
82
83     BiGraph g = new BiGraph(eTable[0], hTable[0]);
84     return g;
85   }
86
87   /** 
88    * Update the field values of e-nodes based on the values of
89    * neighboring h-nodes and vice-versa.
90    **/
91   /*
92   void compute()
93   {
94       Node tmp = eNodes;
95       while(tmp!= null) {
96           Node n = tmp;
97           n.computeNewValue();
98           tmp = tmp.next;
99       }
100       tmp = hNodes;
101       while(tmp!=null) {
102           Node n = tmp;
103           n.computeNewValue();
104           tmp = tmp.next;
105       }
106   }
107   */
108
109   /**
110    * Override the toString method to print out the values of the e and h nodes.
111    * @return a string contain the values of the e and h nodes.
112    **/
113   public String toString()
114   {
115       StringBuffer retval = new StringBuffer();
116       Node tmp = eNodes;
117       while(tmp!=null) {
118           Node n = tmp;
119           retval.append("E: " + n + "\n");
120           tmp = tmp.next;
121       }
122       tmp = hNodes;
123       while(tmp!=null) {
124           Node n = tmp;
125           retval.append("H: " + n + "\n");
126           tmp = tmp.next;
127       }
128       return retval.toString();
129   }
130
131 }
132