-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagerank_alt.c
367 lines (304 loc) · 8.46 KB
/
pagerank_alt.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <immintrin.h>
#include "pagerank.h"
#define DEBUG 0
static ssize_t g_nthreads = 1;
size_t g_chunk;
size_t g_chunk_elements;
size_t npages;
size_t nelements;
double inv_damp;
double inv_n;
typedef struct {
size_t id;
double* result;
const double* a;
const double* b;
const size_t extra;
} worker_args;
typedef struct {
size_t id;
double* result;
double* M;
double dampener;
} worker_dampen_args;
double* matrix_mul(double* matrix_a, double* vector, size_t npages, double* result);
void dampen(double* Mhat, double *M, double dampener);
void pagerank(node* list, size_t pages, size_t nedges, size_t nthreads, double dampener) {
npages = pages;
// set limit on number of threads
if (nthreads > npages) {
nthreads = npages;
}
g_nthreads = nthreads;
nelements = npages * npages;
g_chunk = npages/g_nthreads;
g_chunk_elements = nelements/g_nthreads;
// set constants for calculation
inv_n = 1.0/npages;
inv_damp = (1.0-dampener)/npages;
// TODO: Calculate IN sets
// int nIn[npages];
// node **in = (node**) malloc(nelements * sizeof(node*));
// Construct Probability matrix (M)
//probMatrix(M, list, npages);
char **names = (char**) malloc(npages * sizeof(char*));
double *M = (double*) malloc(nelements * sizeof(double));
node *current_j = list;
for (int j = 0; j < npages; j++) {
node *current = list;
for (int i = 0; i < npages; i++) {
// if OUT[j] is 0;
// DEBUG printf("OUT[%d] = %zu\n", i, current->page->noutlinks);
if (current_j->page->noutlinks == 0) {
// DEBUG printf("OUT[%d] = 0: [%d][%d]\n", j, i, j);
for (i = 0; i < npages; i++) {
M[j * npages + i] = inv_n;
}
break;
}
// Use the IN sets to see if there are any links
else {
bool link = false;
// iterate through in list, and try and see if j is
// in i's in-list
node* current_in = current->page->inlinks;
while (true) {
// set nIn[i] to counter and break if end of LL is reached.
if (!current_in) {
// printf("----NULL----\n");
break;
}
if (current_in->page->name == current_j->page->name) {
link = true;
break;
}
current_in = current_in->next;
}
if (link == true) {
// if j links to i
// DEBUG printf("found a link: [%d][%d]\n", i, j);
M[j * npages + i] = 1.0/current_j->page->noutlinks;
}
else {
// DEBUG printf("No links: [%d][%d]\n", i, j);
// No links to page
M[j * npages + i] = 0;
}
}
//printf("[%d][%d] %lf\n", i, j, M[i * npages + j]);
current = current->next;
}
current_j = current_j->next;
}
// print M
if(DEBUG) {
for (int i = 0; i < npages; i++) {
printf("%s ", names[i]);
}
printf("\n----------\n");
for (int i = 0; i < npages; i++) {
for (int j = 0; j < npages; j++) {
printf("%lf ", M[j * npages + i]);
}
printf("\n");
}
printf("-------- \n");
}
// TODO: Construct pagerank score matrix and initialise to 1/N
double P[npages];
for (int i = 0; i < npages; i++) {
P[i] = inv_n;
}
// ------------ Calculate Mhat ----------------------------------------
//---------------------------------------------------------------------
double *Mhat = (double*) malloc(nelements * sizeof(double));
if(npages >0) {
for (int x = 0; x < npages; x++) { // y = old rows
for (int y= 0; y < npages; y++) { // x = old columns
// insert in row i, column j
Mhat[npages*x + y] = (M[npages*y + x]*dampener) + inv_damp;
}
}
} else {
dampen(Mhat, M, dampener);
}
// print M(hat)
if(DEBUG) {
for (int i = 0; i < npages; i++) {
printf("%s ", names[i]);
}
printf("\n-----Mhat-----\n");
for (int i = 0; i < npages; i++) {
for (int j = 0; j < npages; j++) {
printf("%lf ", Mhat[i * npages + j]);
}
printf("\n");
}
printf("-------- \n");
}
// ---------------- Main iteration step: ------------------------------
//---------------------------------------------------------------------
int count = 0;
double newP[npages];
while(true) {
count ++;
double vector_norm_sum = 0.0;
if(npages > 5) {
matrix_mul(Mhat, P, npages, newP);
for (int i = 0; i < npages; i++) {
double diff = (newP[i] - P[i]);
vector_norm_sum += diff*diff;
}
memcpy(P, newP, sizeof(P));
}
else {
for (int i = 0; i < npages; i++) {
newP[i] = 0;
// iterate through ith row of M and multiply with P[j]
for (int j = 0; j < npages; j++) {
if(DEBUG) {
printf("%lf * %lf + \n", P[j], Mhat[i * npages + j]);
}
newP[i] += P[j] * Mhat[i * npages + j];
}
double diff = (newP[i] - P[i]);
vector_norm_sum += diff*diff;
}
memcpy(P, newP, sizeof(P));
}
// DEBUG print results of each iteration
if(DEBUG) {
printf("-------\niteration %d: vector normal = %lf\n", count, sqrt(vector_norm_sum));
node* current = list;
for (size_t i = 0; i < npages; i++) {
printf("%s %.8lf\n", current->page->name, P[i]);
current = current->next;
}
}
if (sqrt(vector_norm_sum) < EPSILON) {
break;
}
//printf("%d\n", count);
}
// Print scores for each page
node* current = list;
for (size_t i = 0; i < npages; i++) {
printf("%s %.8lf\n", current->page->name, P[i]);
current = current->next;
}
free(names);
free(M);
free(Mhat);
}
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------
/**
* Returns the product of the column vector and matrix provided
*/
void* worker_mul(void* args) {
worker_args* wargs = (worker_args*) args;
size_t id = wargs->id;
size_t npages = wargs->extra;
const size_t start = id * g_chunk;
const size_t end = id == g_nthreads - 1 ? npages : (id + 1) * g_chunk;
const double* Mhat = wargs->a;
const double* P = wargs->b;
double* result = wargs->result;
for (int i = start; i < end; i++) {
result[i] = 0;
// iterate through ith row of M and multiply with P[j]
for (int j = 0; j < npages; j++) {
if(DEBUG) {
printf("%lf * %lf + \n", P[j], Mhat[i * npages + j]);
}
result[i] += P[j] * Mhat[i * npages + j];
}
}
return NULL;
}
double* matrix_mul(double* matrix_a, double* vector, size_t npages, double* result) {
worker_args args[g_nthreads];
pthread_t thread_ids[g_nthreads];
for (size_t i = 0; i < g_nthreads; i++) {
args[i] = (worker_args) {
.a = matrix_a,
.b = vector,
.id = i,
.result = result,
.extra = npages,
};
}
// Launch threads
for (size_t i = 0; i < g_nthreads; i++) {
pthread_create(thread_ids + i, NULL, worker_mul, args + i);
}
// Wait for threads to finish
for (size_t i = 0; i < g_nthreads; i++) {
pthread_join(thread_ids[i], NULL);
}
return result;
}
void* worker_dampen(void* args) {
worker_dampen_args* wargs = (worker_dampen_args*) args;
size_t id = wargs->id;
const size_t start = id * g_chunk_elements;
const size_t end = id == g_nthreads - 1 ? nelements : (id + 1) * g_chunk_elements;
double* Mhat = wargs->result;
const double* M = wargs-> M;
const double dampener = wargs->dampener;
for (int x = start; x < end; x++) { // y = old rows
for (int y = 0; y < npages; y++) { // x = old columns
Mhat[npages*x + y] = (M[npages*y + x]*dampener) + inv_damp;
}
}
return NULL;
}
void dampen(double* Mhat, double* M, double dampener) {
worker_dampen_args args[g_nthreads];
pthread_t thread_ids[g_nthreads];
for (size_t i = 0; i < g_nthreads; i++) {
args[i] = (worker_dampen_args) {
.id = i,
.result = Mhat,
.dampener = dampener,
.M = M,
};
}
// Launch threads
for (size_t i = 0; i < g_nthreads; i++) {
pthread_create(thread_ids + i, NULL, worker_dampen, args + i);
}
// Wait for threads to finish
for (size_t i = 0; i < g_nthreads; i++) {
pthread_join(thread_ids[i], NULL);
}
}
/*
######################################
### DO NOT MODIFY BELOW THIS POINT ###
######################################
*/
int main(int argc, char** argv) {
/*
######################################################
### DO NOT MODIFY THE MAIN FUNCTION OR HEADER FILE ###
######################################################
*/
config conf;
init(&conf, argc, argv);
node* list = conf.list;
size_t npages = conf.npages;
size_t nedges = conf.nedges;
size_t nthreads = conf.nthreads;
double dampener = conf.dampener;
pagerank(list, npages, nedges, nthreads, dampener);
release(list);
return 0;
}