This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEBGC.cu
270 lines (220 loc) · 10.3 KB
/
EBGC.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
/*
* CUDA C/C++ implementation for Parallel Graph Coloring for Manycore Architectures
* {@link https://ieeexplore.ieee.org/abstract/document/7516086}
*
* @author Ashwin Joisa
* @author Shanthanu Rai
* @author Rohit MP
*/
// Include header files
#include <bits/stdc++.h>
#include <cuda.h>
#define MAX_THREADS 1024
#define CEIL(a, b) ((a - 1) / b + 1)
using namespace std;
float device_time_taken;
// Catch Cuda errors
void catchCudaError(cudaError_t error, const char *function)
{
if (error != cudaSuccess)
{
printf("\n====== Cuda Error Code %i ======\n %s in CUDA %s\n", error, cudaGetErrorString(error), function);
exit(-1);
}
}
// Host Memory
void readGraph(int &nodeCount, int &edgeCount,
int &maxDegree, int **adjacencyList, int **adjacencyListPointers,
int **edgeListX, int **edgeListY) {
int u, v;
cin >> nodeCount >> edgeCount;
// edge (u, v) => edgeListX[i] = u, edgeListY[i] = v
*edgeListX = new int[edgeCount];
*edgeListY = new int[edgeCount];
// Use vector of vectors temporarily to input graph
vector<int> *adj = new vector<int>[nodeCount];
for (int i = 0; i < edgeCount; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
(*edgeListX)[i] = u;
(*edgeListY)[i] = v;
}
// Copy into compressed adjacency List
*adjacencyListPointers = new int[nodeCount +1];
*adjacencyList = new int[2 * edgeCount +1];
int pos = 0;
for(int i=0; i<nodeCount; i++) {
(*adjacencyListPointers)[i] = pos;
for(int node : adj[i])
(*adjacencyList)[pos++] = node;
}
(*adjacencyListPointers)[nodeCount] = pos;
// Calculate max degree
maxDegree = INT_MIN;
for(int i=0; i<nodeCount; i++)
maxDegree = max(maxDegree, (int)adj[i].size());
delete[] adj;
}
__global__ void assignColours(int nodeCount, int edgeCount, int *adjacencyList, int *adjacencyListPointers,
int *edgeListX, int *edgeListY, int *coloured, int *CS, int *colour, int *VForbidden) {
int id = threadIdx.x + blockIdx.x * blockDim.x;
if(id < nodeCount) {
coloured[0] = 0;
}
__syncthreads();
// ################################################ Assign Colours
if(id < nodeCount && colour[id] == 0) {
if(~(VForbidden[id])) {
// Some bit is zero => Some colour available
// colour = First available bit (zero bit)
int temp = (~(VForbidden[id]));
colour[id] = (temp & -temp);
}
else {
CS[id]++;
VForbidden[id] = 0;
}
}
}
__global__ void detectConflicts(int nodeCount, int edgeCount, int *adjacencyList, int *adjacencyListPointers,
int *edgeListX, int *edgeListY, int *coloured, int *CS, int *colour, int *VForbidden) {
int id = threadIdx.x + blockIdx.x * blockDim.x;
// ################################################ Detect Conflicts
if(id < edgeCount) {
int u = edgeListX[id], v = edgeListY[id];
if(CS[u] == CS[v]) {
if(u < v) {
atomicCAS(&colour[u], colour[v], 0);
if(colour[u] == 0) {
*coloured = 1;
}
}
else {
atomicCAS(&colour[v], colour[u], 0);
if(colour[v] == 0) {
*coloured = 1;
}
}
__syncthreads();
// ################################################ VForbidden
if(colour[u] && colour[v] == 0)
atomicOr(&VForbidden[v], colour[u]);
if(colour[v] && colour[u] == 0)
atomicOr(&VForbidden[u], colour[v]);
}
}
}
__global__ void convertColour(int nodeCount, int edgeCount, int *adjacencyList, int *adjacencyListPointers,
int *edgeListX, int *edgeListY, int *coloured, int *CS, int *colour, int *VForbidden) {
int id = threadIdx.x + blockIdx.x * blockDim.x;
if(id < nodeCount) {
int cnt = 0, c = colour[id];
while(c) {
c >>= 1;
cnt++;
}
colour[id] = CS[id] * 32 + cnt;
}
}
int *graphColouring(int nodeCount, int edgeCount, int *device_adjacencyList, int *device_adjacencyListPointers,
int *device_edgeListX, int *device_edgeListY, int maxDegree) {
int *host_colours = new int[nodeCount], *done = new int;
int *device_colours, *device_coloured;
int *device_CS, *device_VForbidden;
catchCudaError(cudaMalloc((void **)&device_colours, sizeof(int) * nodeCount), "Malloc - Colours");
catchCudaError(cudaMalloc((void **)&device_CS, sizeof(int) * nodeCount), "Malloc - CS");
catchCudaError(cudaMalloc((void **)&device_VForbidden, sizeof(int) * nodeCount), "Malloc - VForbidden");
catchCudaError(cudaMalloc((void **)&device_coloured, sizeof(int)), "Malloc - Coloured");
// Timer
cudaEvent_t device_start, device_end;
catchCudaError(cudaEventCreate(&device_start), "Event Create");
catchCudaError(cudaEventCreate(&device_end), "Event Create");
catchCudaError(cudaEventRecord(device_start), "Event Record");
do {
assignColours <<< CEIL(max(nodeCount, edgeCount), MAX_THREADS), MAX_THREADS >>> (nodeCount, edgeCount, device_adjacencyList,
device_adjacencyListPointers, device_edgeListX, device_edgeListY, device_coloured, device_CS, device_colours, device_VForbidden);
detectConflicts <<< CEIL(max(nodeCount, edgeCount), MAX_THREADS), MAX_THREADS >>> (nodeCount, edgeCount, device_adjacencyList,
device_adjacencyListPointers, device_edgeListX, device_edgeListY, device_coloured, device_CS, device_colours, device_VForbidden);
catchCudaError(cudaMemcpy(&done, device_coloured, sizeof(int), cudaMemcpyDeviceToHost), "Memcpy00");
}while(done);
convertColour <<< CEIL(max(nodeCount, edgeCount), MAX_THREADS), MAX_THREADS >>> (nodeCount, edgeCount, device_adjacencyList,
device_adjacencyListPointers, device_edgeListX, device_edgeListY, device_coloured, device_CS, device_colours, device_VForbidden);
cudaThreadSynchronize();
// Timer
catchCudaError(cudaEventRecord(device_end), "Event Record");
catchCudaError(cudaEventSynchronize(device_end), "Event Synchronize");
catchCudaError(cudaEventElapsedTime(&device_time_taken, device_start, device_end), "Elapsed time");
// Copy colours to host and return
catchCudaError(cudaMemcpy(host_colours, device_colours, sizeof(int) * nodeCount, cudaMemcpyDeviceToHost), "Memcpy3");
delete[] host_conflicts;
catchCudaError(cudaFree(device_colours), "Free");
catchCudaError(cudaFree(device_coloured), "Free");
catchCudaError(cudaFree(device_CS), "Free");
catchCudaError(cudaFree(device_VForbidden), "Free");
catchCudaError(cudaFree(device_conflicts), "Free");
return host_colours;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
cout << "Usage: " << argv[0] << " <graph_input_file> [output_file]\n";
return 0;
}
int nodeCount, edgeCount, maxDegree;
int *adjacencyList = NULL, *adjacencyListPointers = NULL, *edgeListX = NULL, *edgeListY = NULL;
int *device_adjacencyList, *device_adjacencyListPointers, *device_edgeListX, *device_edgeListY;
char choice = 'n';
cout << "Would you like to print the colouring of the graph? (y/n) ";
cin >> choice;
freopen(argv[1], "r", stdin);
readGraph(nodeCount, edgeCount, maxDegree, &adjacencyList, &adjacencyListPointers, &edgeListX, &edgeListY);
// Alocate device memory and copy - Adjacency List
catchCudaError(cudaMalloc((void **)&device_adjacencyList, sizeof(int) * (2 * edgeCount + 1)), "Malloc5");
catchCudaError(cudaMemcpy(device_adjacencyList, adjacencyList, sizeof(int) * (2 * edgeCount + 1), cudaMemcpyHostToDevice), "Memcpy11");
// Alocate device memory and copy - Adjacency List Pointers
catchCudaError(cudaMalloc((void **)&device_adjacencyListPointers, sizeof(int) * (nodeCount + 1)), "Malloc6");
catchCudaError(cudaMemcpy(device_adjacencyListPointers, adjacencyListPointers, sizeof(int) * (nodeCount + 1), cudaMemcpyHostToDevice), "Memcpy12");
// Alocate device memory and copy - Edge List X
catchCudaError(cudaMalloc((void **)&device_edgeListX, sizeof(int) * (edgeCount)), "Malloc - Edge List X");
catchCudaError(cudaMemcpy(device_edgeListX, edgeListX, sizeof(int) * (edgeCount), cudaMemcpyHostToDevice), "Memcpy - Edge List X");
// Alocate device memory and copy - Edge List Y
catchCudaError(cudaMalloc((void **)&device_edgeListY, sizeof(int) * (edgeCount)), "Malloc - Edge List Y");
catchCudaError(cudaMemcpy(device_edgeListY, edgeListY, sizeof(int) * (edgeCount), cudaMemcpyHostToDevice), "Memcpy - Edge List Y");
int *colouring = graphColouring(nodeCount, edgeCount, device_adjacencyList, device_adjacencyListPointers,
device_edgeListX, device_edgeListY, maxDegree);
// calculating number of colours
int chromaticNumber = INT_MIN;
for (int i = 0; i < nodeCount; i++) {
chromaticNumber = max(chromaticNumber, colouring[i]);
if(choice == 'y' || choice == 'Y')
printf("Node %d => Colour %d\n", i, colouring[i]);
}
cout << endl;
printf("\nNumber of colours used (chromatic number) ==> %d\n", chromaticNumber);
printf("Time Taken (Parallel) = %f ms\n", device_time_taken);
if (argc == 3) {
freopen(argv[2], "w", stdout);
for (int i = 0; i < nodeCount; i++)
cout << colouring[i] << " ";
cout << endl;
}
// Check for correctness
int count = 0;
for(int u = 0; u < nodeCount; u++){
for(int j = adjacencyListPointers[u]; j < adjacencyListPointers[u+1]; j++){
int v = adjacencyList[j];
if(colouring[u] == colouring[v] && u <= v ){
if(count <= 10)
cout << "Conflicting colour found at nodes " << u << " and " << v << endl;
count++;
}
}
}
cout << "Found total " << count << " conflicts!" << endl;
// Free all memory
delete[] colouring, adjacencyList, adjacencyListPointers, edgeListX, edgeListY;
catchCudaError(cudaFree(device_adjacencyList), "Free");
catchCudaError(cudaFree(device_adjacencyListPointers), "Free");
catchCudaError(cudaFree(device_edgeListX), "Free");
catchCudaError(cudaFree(device_edgeListY), "Free");
}