/
pagerank-serial.c
66 lines (56 loc) · 2.01 KB
/
pagerank-serial.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
#include <stdio.h>
#include "pagerank-util.h"
#include <mpi.h>
// pagerank-straw provides straw_mvpSM(), scale(), solve().
// some subset of these functions (definitely including straw_mvpSM) will be modified for performance.
#include "pagerank-straw.h"
/////////////////////////////////////////////////////////////
void serial_mvpSM(double * w, SparseMatrix S, double * z) {
strawman_mvpSM(w, S, z);
}
/////////////////////////////////////////////////////////////
void uniform(double *x, int n) {
// Vector x has length n, is already allocated. All entries are set to 1/n.
int i;
double ninv = 1.0/n;
for (i = 0; i < n; ++i) x[i] = ninv;
}
int main(int argc, char* argv[]){
// hello
int n = 100;
double d = 0.85; // damping variable
double eps = 0.000000001;
if (argc <= 1 || argc > 4) {
printf("usage: %s num-pages damping-factor(0.85) epsilon(0.0001)\n", argv[0]);
return 0;
}
if (argc > 1) n = atoi(argv[1]);
if (argc > 2) d = atof(argv[2]);
if (argc > 3) eps = atof(argv[3]);
// build vectors
double *y0 = (double *) malloc(n*sizeof(double)); // holds initial page probabilities
double *y = (double *) malloc(n*sizeof(double)); // holds intermediate and ultimate probs.
uniform(y0, n);
printf("initial probabilities\n");
printvec(y0, n);
// build matrix
struct SparseMatrixHandle SH;
SparseMatrix S = &SH;
randomLM(S, n); // The link matrix
if (n <= 100) writeSM(S, 50);
printf("dimension is %d, nnz is %d, damper is %f, epsilon is %g\n\n", S->coldim, S->nnz, d, eps);
double *C = (double *) malloc(S->coldim*sizeof(double));
scale(C, S); // convert link matrix to stochastic matrix (col sums are 1).
MPI_Init(0,0);
double start, elapsed, elapsed2;
int iters;
// compute page rank
start = MPI_Wtime();
iters = solve(S, C, d, y0, y, eps, strawman_mvpSM);
elapsed = MPI_Wtime() - start;
printf("final (page rank) probabilities, %d iterations in time %f\n", iters, elapsed);
printvec(y, n); printf("\n");
// good bye
free(C); free(y); free(y0); free(SH.row); free(SH.col); free(SH.val);
return 0;
}