Java non transactional version for Em3d
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / javasingle / Node2.java
1 /** 
2  * This class implements nodes (both E- and H-nodes) of the EM graph. Sets
3  * up random neighbors and propagates field values among neighbors.
4  */
5 public class Node {
6     /**
7      * The value of the node.
8      **/
9     double value;
10     /**
11      * Array of nodes to which we send our value.
12      **/
13     Node[] toNodes;
14     /**
15      * Array of nodes from which we receive values.
16      **/
17     Node[] fromNodes;
18     /**
19      * Coefficients on the fromNodes edges
20      **/
21     double[] coeffs;
22     /**
23      * The number of fromNodes edges
24      **/
25     int fromCount;
26     /**
27      * Used to create the fromEdges - keeps track of the number of edges that have
28      * been added
29      **/
30     int fromLength;
31     
32     /** 
33      * Constructor for a node with given `degree'.   The value of the
34      * node is initialized to a random value.
35      **/
36     public Node() {
37     }
38     
39     public void init(int degree, double val) {
40         this.value=val;
41         // create empty array for holding toNodes
42         toNodes = new Node[degree];
43     }
44     
45     /** 
46      * Create unique `degree' neighbors from the nodes given in nodeTable.
47      * We do this by selecting a random node from the give nodeTable to
48      * be neighbor. If this neighbor has been previously selected, then
49      * a different random neighbor is chosen.
50      * @param nodeTable the list of nodes to choose from.
51      **/
52     public void makeUniqueNeighbors(EVector[] reversetable,Node[] nodeTable, Random rand, int begin, int end) {
53         int len=toNodes.length;
54         for (int filled = 0; filled < len; filled++) {
55             int k;
56             Node otherNode;
57             int index;
58             do {
59                 boolean isBreak = false;
60                 // generate a random number in the correct range
61                 index = rand.nextInt();
62                 if (index < 0) index = -index;
63                 //local vs remote from em3d benchmark
64                 if (filled<(len/4))
65                     index=index%nodeTable.length;
66                 else
67                     index=begin+(index%(end-begin));
68                 
69                 // find a node with the random index in the given table
70                 otherNode = nodeTable[index];
71                 
72                 for (k = 0; (k < filled) && (isBreak==false); k++) {
73                     if (otherNode == toNodes[k]) 
74                         isBreak = true;
75                 }
76             } while (k < filled);
77             
78             // other node is definitely unique among "filled" toNodes
79             toNodes[filled] = otherNode;
80             
81             // update fromCount for the other node
82             if (reversetable[index]==null)
83                 reversetable[index]=new EVector();
84             reversetable[index].addElement(this);
85         }
86     }
87
88     /** 
89      * Allocate the right number of FromNodes for this node. This
90      * step can only happen once we know the right number of from nodes
91      * to allocate. Can be done after unique neighbors are created and known.
92      *
93      * It also initializes random coefficients on the edges.
94      **/
95
96     public void makeFromNodes() {
97         fromNodes = new Node[fromCount]; // nodes fill be filled in later
98         coeffs = new double[fromCount];
99     }
100
101     /**
102      * Fill in the fromNode field in "other" nodes which are pointed to
103      * by this node.
104      **/
105     public void updateFromNodes(Random rand) { 
106         for (int i = 0; i < toNodes.length; i++) {
107             Node otherNode = toNodes[i];
108             int count = otherNode.fromLength++;
109             otherNode.fromNodes[count] = this;
110             otherNode.coeffs[count] = rand.nextDouble();
111         }
112     }
113     
114     /**
115      * Override the toString method to return the value of the node.
116      * @return the value of the node.
117      **/
118     public String toString() {
119         String returnString;
120         returnString = "value " + (long)value + ", from_count " + fromCount;
121     }
122 }