-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPageRank.cu
147 lines (116 loc) · 4.05 KB
/
PageRank.cu
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
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yichao Cheng
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Distributed memory implementation using Olive framework.
*
* Author: Yichao Cheng ([email protected])
* Created on: 2015-01-13
* Last Modified: 2015-01-13
*/
#include "oliver.h"
FILE * outputFile;
struct PR_Vertex {
double rank;
double delta;
void print() {
fprintf(outputFile, "%f\n", rank);
}
void reduce(double &r) {
r += fabs(delta);
}
};
struct PR_edge_F {
__device__
inline double gather(PR_Vertex srcValue, EdgeId outdegree, Dump_Edge edge) {
return srcValue.rank / outdegree;
}
__device__
inline void reduce(double &accumulator, double accum) {
atomicAdd(&accumulator, accum);
}
}; // edgeMa
struct PR_vertex_F {
double damping, addConstant;
PR_vertex_F(double _damping, double _oneOverN) : damping(_damping),
addConstant( (1-_damping) * _oneOverN ) {}
__device__
inline void operator() (PR_Vertex &v, double accum) {
double new_rank = damping * accum + addConstant;
v.delta = new_rank - v.rank;
v.rank = new_rank;
}
}; // vertexMap
struct PR_init_F {
double rank;
PR_init_F(double _rank): rank(_rank) {}
__device__
inline void operator() (PR_Vertex &v, double accum) {
v.rank = rank;
v.delta = rank;
}
}; // vertexMap
int main(int argc, char **argv) {
CommandLine cl(argc, argv, "<inFile> [-dimacs] [-verbose] [-round 100]");
char * inFile = cl.getArgument(0);
bool dimacs = cl.getOption("-dimacs");
bool verbose = cl.getOption("-verbose");
int max_rounds = cl.getOptionIntValue("-round", 100);
// Read the graph file.
CsrGraph<int, int> graph;
if (dimacs) {
graph.fromDimacsFile(inFile);
} else {
graph.fromEdgeListFile(inFile);
}
// Algorithm specific parameters
const double damping = 0.85;
const double oneOverN = 1.0 / graph.vertexCount;
const double epsilon = 0.0000001;
Oliver<PR_Vertex, Dump_Edge, double> ol;
ol.readGraph(graph);
// Universal vertex set in sparse representation
VertexSubset all(graph.vertexCount, true);
ol.vertexMap<PR_init_F>(all, PR_init_F(oneOverN));
double start = getTimeMillis();
Stopwatch w;
w.start();
int iterations = 0;
while (1) {
ol.edgeMap<PR_edge_F>(all, PR_edge_F());
ol.vertexMap<PR_vertex_F>(all, PR_vertex_F(damping, oneOverN));
double err = ol.vertexReduce();
if (err < epsilon || iterations == max_rounds) break;
if (verbose)
LOG(INFO) << "PR iterations: " << iterations << ", err: " << err
<<", time: " << w.getElapsedMillis() << "ms";
iterations++;
}
double totalTime = getTimeMillis() - start;
LOG(INFO) << "iterations: " << iterations <<", time: " << totalTime << "ms";
// Log the vertex value into a file
outputFile = fopen("PageRank.txt", "w");
ol.printVertices();
all.del();
return 0;
}