-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_vertex_parallel-serial.cu
272 lines (211 loc) · 7.87 KB
/
main_vertex_parallel-serial.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
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
/*
Authors
- Dibyadarshan Hota 16CO154
- Omkar Prabhu 16CO233
*/
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <string.h>
#include <cuda.h>
#define ll long long
using namespace std;
// ============== Kernel for betweenness calculation ========================
__global__
void betweenness_centrality_kernel (int nodes, int *C, int *R, int *d, int *sigma, float *delta, float *bc, int *reverse_stack) {
// Used to store the position where nodes are pushed as a stack
__shared__ int position;
// Used to store the source vertex
__shared__ int s;
//__shared__ int end_pos;
int idx = threadIdx.x;
if (idx == 0) {
// Initializing source
s = 0;
//end_pos = 1;
//reverse_bfs_limit[0] = 0;
}
__syncthreads();
while (s < nodes) {
__syncthreads();
// ============== Vertex parallel method for BFS ========================
//Initialize d and sigma
for(int v=idx; v<nodes; v+=blockDim.x) {
if(v == s) {
d[v] = 0;
sigma[v] = 1;
}
else {
d[v] = INT_MAX;
sigma[v] = 0;
}
delta[v] = 0;
}
__syncthreads();
__shared__ int current_depth;
__shared__ bool done;
// ============== INIT ========================
if(idx == 0) {
done = false;
current_depth = 0;
position = 0;
}
__syncthreads();
// SP Calc
while(!done)
{
__syncthreads();
done = true;
__syncthreads();
for(int v=idx; v<nodes; v+=blockDim.x) {
if(d[v] == current_depth) {
// ============== Storing nodes for reverse BFS ========================
int t = atomicAdd(&position,1);
reverse_stack[t] = v;
// ============== Relaxation step to find minimum distance ========================
for(int r=R[v]; r<R[v+1]; r++) {
int w = C[r];
if(d[w] == INT_MAX) {
d[w] = d[v] + 1;
done = false;
}
if(d[w] == (d[v] + 1)) {
atomicAdd(&sigma[w],sigma[v]);
}
}
}
}
__syncthreads();
if(idx == 0){
current_depth++;
//reverse_bfs_limit[end_pos] = position;
//++end_pos;
}
}
// Parallel Vertex Parallel implementation (uncomment the following lines and comment the ones below)
__syncthreads();
// atomicSub(&end_pos,2);
// for(int itr1 = end_pos; itr1 >= 0; --itr1){
// for(int itr2 = reverse_bfs_limit[itr1] + idx; itr2 < reverse_bfs_limit[itr1+1]; itr2+=blockDim.x){
// // reverse_stack[itr2] is one node
// for(int itr3 = R[reverse_stack[itr2]]; itr3 < R[reverse_stack[itr2] + 1]; ++itr3){
// int consider = C[itr3];
// // C[itr3] other node
// if(d[consider] == d[reverse_stack[itr2]]-1){
// delta[consider] += ( ((float)sigma[consider]/sigma[reverse_stack[itr2]]) * ((float)1 + delta[reverse_stack[itr2]]) );
// }
// }
// if(reverse_stack[itr2] != s){
// bc[reverse_stack[itr2]] += delta[reverse_stack[itr2]];
// }
// }
// __syncthreads();
// }
// Serialized Vertex Parallel implementation. Comment the following for parallel implementation
if(idx == 0){
for(int itr1 = nodes - 1; itr1 >= 0; --itr1){
for(int itr2 = R[reverse_stack[itr1]]; itr2 < R[reverse_stack[itr1] + 1]; ++itr2){
int consider = C[itr2];
if(d[consider] == d[reverse_stack[itr1]]-1){
delta[consider] += ( ((float)sigma[consider]/sigma[reverse_stack[itr1]]) * ((float)1 + delta[reverse_stack[itr1]]) );
}
}
if(reverse_stack[itr1] != s){
bc[reverse_stack[itr1]] += delta[reverse_stack[itr1]];
}
}
}
// ============== Incrementing source ========================
__syncthreads();
if (idx == 0) {
s += 1;
}
}
}
int main () {
// Uncomment for reading files in stdin
// freopen("graph", "r", stdin);
// ============== INIT ========================
// nodes and edges
int nodes, edges;
cin>>nodes>>edges;
// compressed adjancency list
int * V = new int[nodes + 1];
int * E = new int[2 * edges];
// ============== Formation of compressed adjacency for CSR ========================
string line;
int node = 0;
int counter = 0;
getline(cin, line);
for (int i = 0; i < nodes; ++i) {
getline(cin, line);
V[node] = counter;
istringstream is(line);
int tmp;
while (is >> tmp) {
E[counter] = tmp;
counter += 1;
}
++node;
}
V[node] = counter;
// Uncomment for printing compressed adjacency list
// cout<<"\n";
// for (int i = 0; i <= nodes; i++) {
// cout<<V[i]<<" ";
// }
// cout<<"\n";
// for (int i = 0; i < 2 * edges; ++i) {
// cout<<E[i]<<" ";
// }
// cout<<"\n";
// Initializations
int *d = new int[nodes];
int *sigma = new int[nodes];
float *delta = new float[nodes];
float *bc = new float[nodes];
memset(bc,0,sizeof(bc));
int *d_d, *d_sigma, *d_V, *d_E, *d_reverse_stack;
float *d_delta, *d_bc;
// Allocating memory via cudamalloc
cudaMalloc((void**)&d_d, sizeof(int) * nodes);
// cudaMalloc((void**)&d_end_point, sizeof(int) * (nodes + 1));
cudaMalloc((void**)&d_sigma, sizeof(int) * nodes);
cudaMalloc((void**)&d_reverse_stack, sizeof(int) * nodes);
cudaMalloc((void**)&d_V, sizeof(int) * (nodes + 1));
cudaMalloc((void**)&d_E, sizeof(int) * (2*edges));
cudaMalloc((void**)&d_delta, sizeof(float) * nodes);
cudaMalloc((void**)&d_bc, sizeof(float) * nodes);
cudaMemcpy(d_V, V, sizeof(int) * (nodes+1), cudaMemcpyHostToDevice);
cudaMemcpy(d_E, E, sizeof(int) * (2*edges), cudaMemcpyHostToDevice);
cudaMemcpy(d_bc, bc, sizeof(float) * (nodes), cudaMemcpyHostToDevice);
// cudaMemcpy(d_delta, delta, sizeof(float) * (nodes), cudaMemcpyHostToDevice);
// ============== Kernel call ========================
betweenness_centrality_kernel <<<1, 256>>> (nodes, d_E, d_V, d_d, d_sigma, d_delta, d_bc, d_reverse_stack);
// cudaMemcpy(d, d_d, sizeof(float) * nodes, cudaMemcpyDeviceToHost);
// cudaMemcpy(sigma, d_sigma, sizeof(float) * nodes, cudaMemcpyDeviceToHost);
cudaMemcpy(bc, d_bc, sizeof(float) * nodes, cudaMemcpyDeviceToHost);
// cudaMemcpy(delta, d_delta, sizeof(float) * nodes, cudaMemcpyDeviceToHost);
cout<<"Res: \n";
for (int i = 0; i < nodes; i++) {
printf("%f ", bc[i]/2.0);
// cout<<bc[i];
}
cout<<endl;
// ============== Deallocating memory ========================
cudaFree(d_sigma);
cudaFree(d_d);
cudaFree(d_V);
cudaFree(d_E);
cudaFree(d_delta);
cudaFree(d_bc);
cudaFree(d_reverse_stack);
// cudaFree(d_end_point);
free(E);
free(V);
free(d);
free(sigma);
free(delta);
free(bc);
return 0;
}