bug fixes
[IRC.git] / Robust / src / Benchmarks / Prefetch / Em3d / dsm / 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 = global 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         for (int filled = 0; filled < toNodes.length; filled++) {
54             int k;
55             Node otherNode;
56             int index;
57             do {
58                 boolean isBreak = false;
59                 // generate a random number in the correct range
60                 index = rand.nextInt();
61                 if (index < 0) index = -index;
62                 //local vs remote from em3d benchmark
63                 if ((rand.nextInt()%2)==0)
64                     index=index%nodeTable.length;
65                 else
66                     index=begin+(index%(end-begin));
67                 
68                 // find a node with the random index in the given table
69                 otherNode = nodeTable[index];
70                 
71                 for (k = 0; (k < filled) && (isBreak==false); k++) {
72                     if (otherNode == toNodes[k]) 
73                         isBreak = true;
74                 }
75             } while (k < filled);
76             
77             // other node is definitely unique among "filled" toNodes
78             toNodes[filled] = otherNode;
79             
80             // update fromCount for the other node
81             if (reversetable[index]==null)
82                 reversetable[index]=global new EVector();
83             reversetable[index].addElement(this);
84         }
85     }
86
87     /** 
88      * Allocate the right number of FromNodes for this node. This
89      * step can only happen once we know the right number of from nodes
90      * to allocate. Can be done after unique neighbors are created and known.
91      *
92      * It also initializes random coefficients on the edges.
93      **/
94
95     public void makeFromNodes() {
96         fromNodes = global new Node[fromCount]; // nodes fill be filled in later
97         coeffs = global new double[fromCount];
98     }
99
100     /**
101      * Fill in the fromNode field in "other" nodes which are pointed to
102      * by this node.
103      **/
104     public void updateFromNodes(Random rand) { 
105         for (int i = 0; i < toNodes.length; i++) {
106             Node otherNode = toNodes[i];
107             int count = otherNode.fromLength++;
108             otherNode.fromNodes[count] = this;
109             otherNode.coeffs[count] = rand.nextDouble();
110         }
111     }
112     
113     /**
114      * Override the toString method to return the value of the node.
115      * @return the value of the node.
116      **/
117     public String toString() {
118         String returnString;
119         returnString = "value " + (long)value + ", from_count " + fromCount;
120     }
121 }