-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_vertex_parallel.cu
284 lines (248 loc) · 9.33 KB
/
main_vertex_parallel.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
273
274
275
276
277
278
279
280
281
282
283
284
/*
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 computing Betweenness Centrality
* res: Stored in global memory variable bc
*/
__global__
void betweenness_centrality_kernel (int nodes, int *C, int *R, int *d, int *sigma, float *delta, float *bc, int *reverse_stack, int *finish_limit) {
// ================================== VARIABLES INIT ============================================
// initial variables
__shared__ int position;
__shared__ int s;
__shared__ int finish_limit_position;
// source variable initially 0
int idx = threadIdx.x;
if (idx == 0) {
s = 0;
}
__syncthreads();
// move through all nodes
while (s < nodes) {
__syncthreads();
// ============================== distance, delta and sigma INIT ============================================
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;
if(idx == 0) {
done = false;
current_depth = 0;
position = 0;
finish_limit_position = 1;
finish_limit[0] = 0;
}
__syncthreads();
// ============================== Shortest Path Calculation using curr source ======================
// ============================== Using Vertex Parallel ============================================
while(!done)
{ // wait
__syncthreads();
done = true;
__syncthreads();
// move through modes
for(int v=idx; v<nodes; v+=blockDim.x) {
if(d[v] == current_depth) {
// add to reverse_stack
int t = atomicAdd(&position,1);
reverse_stack[t] = v;
// move through neighbours
for(int r=R[v]; r<R[v+1]; r++) {
int w = C[r];
// if not visited
if(d[w] == INT_MAX) {
d[w] = d[v] + 1;
done = false;
}
// add number of paths
if(d[w] == (d[v] + 1)) {
atomicAdd(&sigma[w],sigma[v]);
}
}
}
}
__syncthreads();
// increment variables
if(idx == 0){
current_depth++;
finish_limit[finish_limit_position] = position;
++finish_limit_position;
}
}
// ============================== BC calculation using Brande's Algorithm ============================================
// Parallel Vertex Parallel implementation
// __syncthreads();
if(idx == 0){
finish_limit_position-=2;
// printf("%d %d %d<--", finish_limit_position, finish_limit[finish_limit_position], finish_limit[finish_limit_position+1]);
// for(int a1=0;a1<=finish_limit_position+1;++a1) printf("%d-", finish_limit[a1]);
// printf("\n");
// for(int a1=0;a1<nodes;++a1) printf("%d<", reverse_stack[a1]);
// cout<<"\n";
// printf("\n");
}
__syncthreads();
//atomicSub(&finish_limit_position,2);
for(int itr1 = finish_limit_position; itr1 >= 0; --itr1){
// __syncthreads();
for(int itr2 = finish_limit[itr1] + idx; itr2 < finish_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){
//atomicAdd(&delta[consider], ( ((float)sigma[consider]/sigma[reverse_stack[itr2]]) * ((float)1 + delta[reverse_stack[itr2]]) ));
delta[reverse_stack[itr2]] += ( ((float)sigma[reverse_stack[itr2]]/sigma[consider]) * ((float)1 + delta[consider]) );
}
}
if(reverse_stack[itr2] != s){
bc[reverse_stack[itr2]] += delta[reverse_stack[itr2]];
}
}
__syncthreads();
}
// Serialized Vertex 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]];
// }
// }
// }
// increment
__syncthreads();
if (idx == 0) {
s += 1;
}
}
}
/**
* Main function
*/
int main () {
// ================================ READ INPUT AND MAKE Compressed Adjancency List ====================================
// freopen("graph", "r", stdin);
// nodes and edges
int nodes, edges;
cin>>nodes>>edges;
// compressed adjancency list
int * V = new int[nodes + 1];
int * E = new int[2 * edges];
// read graph data in CSR format
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;
// 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";
// ================================ DECLARE AND INIT VARIABLES ====================================
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, *d_end_point;
float *d_delta, *d_bc;
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 PARAMS AND CALL ====================================
float elapsed_time;
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
// kernel call
betweenness_centrality_kernel <<<1, 1024>>> (nodes, d_E, d_V, d_d, d_sigma, d_delta, d_bc, d_reverse_stack, d_end_point);
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&elapsed_time, start, stop);
// ================================ RESULT ====================================
// 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<<"Result: \n";
// for (int i = 0; i < nodes; i++) {
// cout<<"Node: "<<i<<" BC: "<<fixed<<setprecision(6)<<bc[i]/2.0<<"\n";
// }
cout<<"\n";
// Print the time for execution
cout<<"Execution time: "<<elapsed_time/1000.0<<endl;
// Maximum BC value
float max_bc = 0.0;
for (int i = 0; i < nodes; ++i) {
max_bc = (bc[i] > max_bc) ? bc[i] : max_bc;
}
cout<<"Max BC value: "<<max_bc/2.0<<endl;
// ================================ MEMORY RELEASE ====================================
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;
}