-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDijkstra-CUDA.cu
348 lines (216 loc) · 5.85 KB
/
Dijkstra-CUDA.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Author: Jose F. Martinez Rivera
// Course: ICOM4036 - 040
// Professor: Wilson Rivera Gallego
// Assignment 2 - CUDA Implementation
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <omp.h>
#include <cuda.h>
#include <cuda_runtime.h>
#define V 8
#define E 11
#define MAX_WEIGHT 1000000
#define TRUE 1
#define FALSE 0
typedef int boolean;
//
//Represents an edge or path between Vertices
typedef struct
{
int u;
int v;
} Edge;
//Represents a Vertex
typedef struct
{
int title;
boolean visited;
} Vertex;
//Finds the weight of the path from vertex u to vertex v
__device__ __host__ int findEdge(Vertex u, Vertex v, Edge *edges, int *weights)
{
int i;
for(i = 0; i < E; i++)
{
if(edges[i].u == u.title && edges[i].v == v.title)
{
return weights[i];
}
}
return MAX_WEIGHT;
}
//Finds the branches of the vertex
__global__ void Find_Vertex(Vertex *vertices, Edge *edges, int *weights, int *length, int *updateLength)
{
int u = threadIdx.x;
if(vertices[u].visited == FALSE)
{
vertices[u].visited = TRUE;
int v;
for(v = 0; v < V; v++)
{
//Find the weight of the edge
int weight = findEdge(vertices[u], vertices[v], edges, weights);
//Checks if the weight is a candidate
if(weight < MAX_WEIGHT)
{
//If the weight is shorter than the current weight, replace it
if(updateLength[v] > length[u] + weight)
{
updateLength[v] = length[u] + weight;
}
}
}
}
}
//Updates the shortest path array (length)
__global__ void Update_Paths(Vertex *vertices, int *length, int *updateLength)
{
int u = threadIdx.x;
if(length[u] > updateLength[u])
{
length[u] = updateLength[u];
vertices[u].visited = FALSE;
}
updateLength[u] = length[u];
}
//Prints the an array of elements
void printArray(int *array)
{
int i;
for(i = 0; i < V; i++)
{
printf("Shortest Path to Vertex: %d is %d\n", i, array[i]);
}
}
//Runs the program
int main(void)
{
//Variables for the Host Device
Vertex *vertices;
Edge *edges;
//Weight of the paths
int *weights;
//Len is the shortest path and updateLength is a special array for modifying updates to the shortest path
int *len, *updateLength;
//Pointers for the CUDA device
Vertex *d_V;
Edge *d_E;
int *d_W;
int *d_L;
int *d_C;
//Sizes used for allocation
int sizeV = sizeof(Vertex) * V;
int sizeE = sizeof(Edge) * E;
int size = V * sizeof(int);
//Timer initialization
float runningTime;
cudaEvent_t timeStart, timeEnd;
//Creates the timers
cudaEventCreate(&timeStart);
cudaEventCreate(&timeEnd);
//Allocates space for the variables
vertices = (Vertex *)malloc(sizeV);
edges = (Edge *)malloc(sizeE);
weights = (int *)malloc(E* sizeof(int));
len = (int *)malloc(size);
updateLength = (int *)malloc(size);
//----------------------------------Graph Base Test-------------------------------------//
Edge ed[E] = {{0, 4}, {0, 6}, {0,2}, {4,6}, {4,7}, {0, 7}, {7, 3}, {3, 1}, {2,5}, {2, 1}, {5,3}};
int w[E] = {10, 90, 30, 20, 20, 50, 10, 20, 10, 10, 10};
int i = 0;
for(i = 0; i < V; i++)
{
Vertex a = { .title =i , .visited=FALSE};
vertices[i] = a;
}
for(i = 0; i < E; i++)
{
edges[i] = ed[i];
weights[i] = w[i];
}
//----------------------------------Graph Base Test-------------------------------------//
//--------------------------------Graph Randomizer-----------------------------------//
// srand(time(NULL));
// int i = 0;
// for(i = 0; i < V; i++)
// {
// Vertex a = { .title =(int) i, .visited=FALSE};
// vertices[i] = a;
// }
// for(i = 0; i < E; i++)
// {
// Edge e = {.u = (int) rand()%V , .v = rand()%V};
// edges[i] = e;
// weights[i] = rand()%100;
// }
//--------------------------------Graph Randomizer-----------------------------------//
//Allocate space on the device
cudaMalloc((void**)&d_V, sizeV);
cudaMalloc((void**)&d_E, sizeE);
cudaMalloc((void**)&d_W, E * sizeof(int));
cudaMalloc((void**)&d_L, size);
cudaMalloc((void**)&d_C, size);
//Initial Node
Vertex root = {0, FALSE};
//--------------------------------------Dijkstra's Algorithm--------------------------------------//
root.visited = TRUE;
len[root.title] = 0;
updateLength[root.title] = 0;
//Copy variables to the Device
cudaMemcpy(d_V, vertices, sizeV, cudaMemcpyHostToDevice);
cudaMemcpy(d_E, edges, sizeE, cudaMemcpyHostToDevice);
cudaMemcpy(d_W, weights, E * sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(d_L, len, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_C, updateLength, size, cudaMemcpyHostToDevice);
int j;
//Loop finds the initial paths from the node 's'
for(i = 0; i < V;i++)
{
if(vertices[i].title != root.title)
{
len[(int)vertices[i].title] = findEdge(root, vertices[i], edges, weights);
updateLength[vertices[i].title] = len[(int)vertices[i].title];
}
else{
vertices[i].visited = TRUE;
}
}
//Start the timer
cudaEventRecord(timeStart, 0);
//Recopy the variables
cudaMemcpy(d_L, len, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_C, updateLength, size, cudaMemcpyHostToDevice);
//Parallelization
for(i = 0; i < V; i++){
Find_Vertex<<<1, V>>>(d_V, d_E, d_W, d_L, d_C);
for(j = 0; j < V; j++)
{
Update_Paths<<<1,V>>>(d_V, d_L, d_C);
}
}
//Timing Events
cudaEventRecord(timeEnd, 0);
cudaEventSynchronize(timeEnd);
cudaEventElapsedTime(&runningTime, timeStart, timeEnd);
//Copies the results back
cudaMemcpy(len, d_L, size, cudaMemcpyDeviceToHost);
printArray(len);
//Running Time
printf("Running Time: %f ms\n", runningTime);
//--------------------------------------Dijkstra's Algorithm--------------------------------------//
//Free up the space
free(vertices);
free(edges);
free(weights);
free(len);
free(updateLength);
cudaFree(d_V);
cudaFree(d_E);
cudaFree(d_W);
cudaFree(d_L);
cudaFree(d_C);
cudaEventDestroy(timeStart);
cudaEventDestroy(timeEnd);
}