-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageRankVbl.java
128 lines (106 loc) · 2.87 KB
/
PageRankVbl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//******************************************************************************
//
// File: PageRankVbl.java
//
//******************************************************************************
import edu.rit.pj2.Vbl;
import java.util.ArrayList;
/**
* Class PageRankVbl provides a reduction variable for PageRank
* shared by many threads.
*
* @author Arjun Nair (an3395)
* @author Aditya Advani (aa5394)
* @version 10-Dec-2015
*/
public class PageRankVbl implements Vbl {
double[] current;
double[] prev;
double a;
double vcalc;
int nodenumber;
int allnodes;
/**
* Constructore to initialize variables and calcuate a value
* @param n [number of nodes]
*/
public PageRankVbl(int n) {
allnodes = n;
current = new double[n];
prev = new double[n];
a = 0.85;
vcalc = (double) (1 / (double) n);
for (int i = 0; i < n; i++) {
current[i] = vcalc;
}
vcalc *= (1 - a);
}
/**
* Create a clone of this shared variable.
*
* @return The cloned object.
*
* @exception CloneNotSupportedException
* Thrown if the Clone is not
* supported.
*/
public Object clone() {
Object vbl = null;
try {
vbl = super.clone();
} catch (CloneNotSupportedException e) {
System.out.println("error in cloning\n" + e);
}
return vbl;
}
/**
* Set these shared variables to the given shared variables.
*
* @param vbl Shared variable.
*
*/
public void set(Vbl vbl) {
this.current = (((PageRankVbl) vbl).current);
this.prev = (((PageRankVbl) vbl).prev);
this.a = (((PageRankVbl) vbl).a);
this.vcalc = (((PageRankVbl) vbl).vcalc);
this.nodenumber = (((PageRankVbl) vbl).nodenumber);
}
/**
* Reduce the shared variables into the shared variables of this class.
* <P>
*
* @param vbl Shared variable.
*
*/
public void reduce(Vbl vbl) {
PageRankVbl a = (PageRankVbl) vbl;
if(this.current[nodenumber] == 0)
this.current[nodenumber] = a.current[nodenumber];
}
/**
* Reinitialize the value to 0
* @param n [number of nodes]
*/
public void clearCurrent(int n) {
for (int i = 0; i < n; i++) {
current[i] = 0;
}
}
/**
* Calculate the page rank
* @param k [thread number]
* @param nodes [Sparse matrix at k]
*/
public void calculate(int k, ArrayList<sparseMatrix> nodes) {
int xTemp=0,yTemp=0;
for (sparseMatrix node : nodes) {
xTemp = node.x;
yTemp = node.y;
double valueTemp = a * node.value;
current[xTemp] += valueTemp * prev[yTemp];
}
nodenumber = xTemp;
current[xTemp] += vcalc;
}
}