Java non transactional version for Em3d
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / javasingle / BiGraph2.java
1 /** 
2  * A class that represents the irregular bipartite graph used in
3  * EM3D.  The graph contains two linked structures that represent the
4  * E nodes and the N nodes in the application.
5  **/
6 public class BiGraph {
7     public BiGraph() {
8     }
9     /**
10      * Nodes that represent the electrical field.
11      **/
12     Node[] eNodes;
13     /**
14      * Nodes that representhe the magnetic field.
15      **/
16     Node[] hNodes;
17     
18     EVector [][] ereversetable;
19     EVector [][] hreversetable;
20     int numNodes;
21
22     /**
23      * Construct the bipartite graph.
24      * @param e the nodes representing the electric fields
25      * @param h the nodes representing the magnetic fields
26      **/ 
27     BiGraph(Node[] e, Node[] h) {
28         eNodes = e;
29         hNodes = h;
30     }
31     
32     /**
33      * Create the bi graph that contains the linked list of
34      * e and h nodes.
35      * @param numNodes the number of nodes to create
36      * @param numDegree the out-degree of each node
37      * @param verbose should we print out runtime messages
38      * @return the bi graph that we've created.
39      **/
40     
41     static BiGraph create(int numNodes, int degree, int numThreads) {
42         // making nodes (we create a table)
43         Node [] eTable = new Node[numNodes];
44         Node [] hTable = new Node[numNodes];
45         BiGraph g = new BiGraph(eTable, hTable);
46         g.numNodes=numNodes;
47         g.ereversetable=new EVector[numThreads][];
48         g.hreversetable=new EVector[numThreads][];
49         return g;
50     }
51     
52     
53     /**
54      * 
55      *
56      * @return 
57      **/
58     public void allocateNodes( int indexBegin, int indexEnd, int threadIndex) { 
59         for(int i = indexBegin; i < indexEnd; i++ ) {
60             eNodes[i]=new Node();
61             hNodes[i]=new Node();
62         }
63         ereversetable[threadIndex]=new EVector[numNodes];
64         hreversetable[threadIndex]=new EVector[numNodes];
65     }
66     
67     public void initializeNodes(Node[] fromnodes, Node[] tonodes, EVector[][] reversetable, int begin, int end, int degree, Random r, int threadIndex) {
68         for(int i = begin; i < end; i++ ) {
69             Node n=fromnodes[i];
70             n.init(degree, r.nextDouble());
71             n.makeUniqueNeighbors(reversetable[threadIndex], tonodes, r, begin, end);
72         }
73     }
74     
75     /**
76      * 
77      *
78      * @return 
79      **/
80     
81     public void makeFromNodes(Node[] nodes, EVector reversetable[][], int indexBegin, int indexEnd, Random r) {
82         // Create the fromNodes and coeff field
83         int numthreads=reversetable.length;
84         for(int i = indexBegin; i < indexEnd; i++) {
85             Node n = nodes[i];
86             int count=0;
87             for(int j=0;j<numthreads;j++) {
88                 EVector v=reversetable[j][i];
89                 if(v!=null)
90                     count+=v.size();
91             }
92             n.fromCount=count;
93             n.fromNodes=new Node[count];
94             n.coeffs=new double[count];
95             count=0;
96             for(int j=0;j<numthreads;j++) {
97                 EVector v=reversetable[j][i];
98                 if(v!=null) {
99                     for(int k=0;k<v.size();k++) {
100                         n.fromNodes[count]=(Node)v.elementAt(k);
101                         n.coeffs[count++]=r.nextDouble();
102                     }
103                 }
104             }
105         }
106     }
107 }