-
Notifications
You must be signed in to change notification settings - Fork 4
/
parallel-work-coarse.cu
300 lines (245 loc) · 10.5 KB
/
parallel-work-coarse.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* CUDA C/C++ implementation for Accelerating Graph Betweenness Centrality for Sparse Graphs
*
* @author Ashwin Joisa
* @author Praveen Gupta
**/
//=============================================================================================//
// Include header files
#include <iostream>
#include <cuda.h>
// Include custom header file for implementation of Graphs
#include "Graph.h"
//=============================================================================================//
#define MAX_THREAD_COUNT 1024
#define CEIL(a, b) ((a - 1) / b + 1)
// Max device memory : 4 GB
#define MAX_MEMORY ((long long)4e9)
//=============================================================================================//
using namespace std;
//=============================================================================================//
#define catchCudaError(error) { gpuAssert((error), __FILE__, __LINE__); }
float device_time_taken;
void printTime(float ms) {
int h = ms / (1000*3600);
int m = (((int)ms) / (1000*60)) % 60;
int s = (((int)ms) / 1000) % 60;
int intMS = ms;
intMS %= 1000;
printf("Time Taken (Parallel) = %dh %dm %ds %dms\n", h, m, s, intMS);
printf("Time Taken in milliseconds : %d\n", (int)ms);
}
// Catch Cuda errors
inline void gpuAssert(cudaError_t error, const char *file, int line, bool abort = false)
{
if (error != cudaSuccess)
{
printf("\n====== Cuda Error Code %i ======\n %s in CUDA %s\n", error, cudaGetErrorString(error));
printf("\nIn file :%s\nOn line: %d", file, line);
if(abort)
exit(-1);
}
}
//=============================================================================================//
__global__ void betweennessCentralityKernel(Graph *graph, float *bwCentrality, int nodeCount,
int *sigma, int *distance, float *dependency, int *Q, int *Qpointers) {
int idx = threadIdx.x;
if(idx >= nodeCount)
return;
__shared__ int s;
__shared__ int Q_len;
__shared__ int Qpointers_len;
__shared__ int noOfBlocks;
if(idx == 0) {
s = blockIdx.x - gridDim.x;
noOfBlocks = gridDim.x;
// printf("Progress... %3d%%", 0);
}
__syncthreads();
while(s < nodeCount - noOfBlocks)
{
if(idx == 0)
{
s += noOfBlocks;
// printf("\rProgress... %5.2f%%", (s+1)*100.0/nodeCount);
// printf("Node %d\n", s);
Q[0 + (blockIdx.x * nodeCount)] = s;
Q_len = 1;
Qpointers[0 + (blockIdx.x * nodeCount)] = 0;
Qpointers[1 + (blockIdx.x * nodeCount)] = 1;
Qpointers_len = 1;
}
__syncthreads();
for(int v=idx; v<nodeCount; v+=blockDim.x)
{
if(v == s)
{
distance[v + (blockIdx.x * nodeCount)] = 0;
sigma[v + (blockIdx.x * nodeCount)] = 1;
}
else
{
distance[v + (blockIdx.x * nodeCount)] = INT_MAX;
sigma[v + (blockIdx.x * nodeCount)] = 0;
}
dependency[v + (blockIdx.x * nodeCount)] = 0.0;
}
__syncthreads();
// BFS
while(true)
{
__syncthreads();
for(int k=idx; k<Qpointers[Qpointers_len + (blockIdx.x * nodeCount)]; k+=blockDim.x)
{
if(k < Qpointers[Qpointers_len -1 + (blockIdx.x * nodeCount)])
continue;
int v = Q[k + (blockIdx.x * nodeCount)];
for(int r = graph->adjacencyListPointers[v]; r < graph->adjacencyListPointers[v + 1]; r++)
{
int w = graph->adjacencyList[r];
if(atomicCAS(&distance[w + (blockIdx.x * nodeCount)], INT_MAX, distance[v + (blockIdx.x * nodeCount)] +1) == INT_MAX)
{
int t = atomicAdd(&Q_len, 1);
Q[t + (blockIdx.x * nodeCount)] = w;
}
if(distance[w + (blockIdx.x * nodeCount)] == (distance[v + (blockIdx.x * nodeCount)]+1))
{
atomicAdd(&sigma[w + (blockIdx.x * nodeCount)], sigma[v + (blockIdx.x * nodeCount)]);
}
}
}
__syncthreads();
if(Q_len == Qpointers[Qpointers_len + (blockIdx.x * nodeCount)])
break;
if(idx == 0)
{
Qpointers_len++;
Qpointers[Qpointers_len + (blockIdx.x * nodeCount)] = Q_len;
}
__syncthreads();
}
__syncthreads();
// Reverse BFS
while(Qpointers_len > 0)
{
for(int k=idx; k < Qpointers[Qpointers_len + (blockIdx.x * nodeCount)]; k+=blockDim.x)
{
if(k < Qpointers[Qpointers_len -1 + (blockIdx.x * nodeCount)])
continue;
int v = Q[k + (blockIdx.x * nodeCount)];
for(int r = graph->adjacencyListPointers[v]; r < graph->adjacencyListPointers[v + 1]; r++)
{
int w = graph->adjacencyList[r];
if(distance[w + (blockIdx.x * nodeCount)] == (distance[v + (blockIdx.x * nodeCount)] + 1))
{
if (sigma[w + (blockIdx.x * nodeCount)] != 0)
dependency[v + (blockIdx.x * nodeCount)] += (sigma[v + (blockIdx.x * nodeCount)] * 1.0 / sigma[w + (blockIdx.x * nodeCount)]) * (1 + dependency[w + (blockIdx.x * nodeCount)]);
}
}
if (v != s)
{
// Each shortest path is counted twice. So, each partial shortest path dependency is halved.
atomicAdd(bwCentrality + v, dependency[v + (blockIdx.x * nodeCount)] / 2);
}
}
__syncthreads();
if(idx == 0)
Qpointers_len--;
__syncthreads();
}
}
}
float *betweennessCentrality(Graph *graph, int nodeCount)
{
float *bwCentrality = new float[nodeCount]();
float *device_bwCentrality, *dependency;
int *sigma, *distance, *Q, *Qpointers;
const int BLOCK_COUNT = MAX_MEMORY / (4 * 5 * nodeCount);
// pritnf(">> %d\n", BLOCK_COUNT);
//TODO: Allocate device memory for bwCentrality
catchCudaError(cudaMalloc((void **)&device_bwCentrality, sizeof(float) * nodeCount));
catchCudaError(cudaMalloc((void **)&sigma, sizeof(int) * nodeCount * BLOCK_COUNT));
catchCudaError(cudaMalloc((void **)&distance, sizeof(int) * nodeCount * BLOCK_COUNT));
catchCudaError(cudaMalloc((void **)&Q, sizeof(int) * (nodeCount) * BLOCK_COUNT));
catchCudaError(cudaMalloc((void **)&Qpointers, sizeof(int) * (nodeCount) * BLOCK_COUNT));
catchCudaError(cudaMalloc((void **)&dependency, sizeof(float) * nodeCount * BLOCK_COUNT));
catchCudaError(cudaMemcpy(device_bwCentrality, bwCentrality, sizeof(float) * nodeCount, cudaMemcpyHostToDevice));
// Timer
cudaEvent_t device_start, device_end;
catchCudaError(cudaEventCreate(&device_start));
catchCudaError(cudaEventCreate(&device_end));
catchCudaError(cudaEventRecord(device_start));
betweennessCentralityKernel<<<BLOCK_COUNT, MAX_THREAD_COUNT>>>(graph, device_bwCentrality, nodeCount, sigma, distance, dependency, Q, Qpointers);
cudaDeviceSynchronize();
//End of progress bar
cout << endl;
// Timer
catchCudaError(cudaEventRecord(device_end));
catchCudaError(cudaEventSynchronize(device_end));
cudaEventElapsedTime(&device_time_taken, device_start, device_end);
// Copy back and free memory
catchCudaError(cudaMemcpy(bwCentrality, device_bwCentrality, sizeof(float) * nodeCount, cudaMemcpyDeviceToHost));
catchCudaError(cudaFree(device_bwCentrality));
catchCudaError(cudaFree(sigma));
catchCudaError(cudaFree(dependency));
catchCudaError(cudaFree(distance));
catchCudaError(cudaFree(Q));
catchCudaError(cudaFree(Qpointers));
return bwCentrality;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "Usage: " << argv[0] << " <graph_input_file> [output_file]\n";
return 0;
}
char choice;
cout << "Would you like to print the Graph Betweenness Centrality for all nodes? (y/n) ";
cin >> choice;
freopen(argv[1], "r", stdin);
Graph *host_graph = new Graph();
Graph *device_graph;
catchCudaError(cudaMalloc((void **)&device_graph, sizeof(Graph)));
host_graph->readGraph();
int nodeCount = host_graph->getNodeCount();
int edgeCount = host_graph->getEdgeCount();
catchCudaError(cudaMemcpy(device_graph, host_graph, sizeof(Graph), cudaMemcpyHostToDevice));
// Copy Adjancency List to device
int *adjacencyList;
// Alocate device memory and copy
catchCudaError(cudaMalloc((void **)&adjacencyList, sizeof(int) * (2 * edgeCount + 1)));
catchCudaError(cudaMemcpy(adjacencyList, host_graph->adjacencyList, sizeof(int) * (2 * edgeCount + 1), cudaMemcpyHostToDevice));
// Update the pointer to this, in device_graph
catchCudaError(cudaMemcpy(&(device_graph->adjacencyList), &adjacencyList, sizeof(int *), cudaMemcpyHostToDevice));
// Copy Adjancency List Pointers to device
int *adjacencyListPointers;
// Alocate device memory and copy
catchCudaError(cudaMalloc((void **)&adjacencyListPointers, sizeof(int) * (nodeCount + 1)));
catchCudaError(cudaMemcpy(adjacencyListPointers, host_graph->adjacencyListPointers, sizeof(int) * (nodeCount + 1), cudaMemcpyHostToDevice));
// Update the pointer to this, in device_graph
catchCudaError(cudaMemcpy(&(device_graph->adjacencyListPointers), &adjacencyListPointers, sizeof(int *), cudaMemcpyHostToDevice));
float *bwCentrality = betweennessCentrality(device_graph, nodeCount);
float maxBetweenness = -1;
for (int i = 0; i < nodeCount; i++)
{
maxBetweenness = max(maxBetweenness, bwCentrality[i]);
if (choice == 'y' || choice == 'Y')
printf("Node %d => Betweeness Centrality %0.2lf\n", i, bwCentrality[i]);
}
cout << endl;
printf("\nMaximum Betweenness Centrality ==> %0.2lf\n", maxBetweenness);
printTime(device_time_taken);
if (argc == 3)
{
freopen(argv[2], "w", stdout);
for (int i = 0; i < nodeCount; i++)
cout << bwCentrality[i] << " ";
cout << endl;
}
// Free all memory
delete[] bwCentrality;
catchCudaError(cudaFree(adjacencyList));
catchCudaError(cudaFree(adjacencyListPointers));
catchCudaError(cudaFree(device_graph));
}