-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cu
363 lines (295 loc) · 10.6 KB
/
kernel.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys\timeb.h>
#include "main.h"
#include "lodepng.h"
#include <string.h>
// For an M (rows) by N (columns) maze
# define M 1001
# define N 1001
void postProcessing(char* inputImage);
__shared__ Point* new_points;
__device__ bool insertPoint(Point array[2 * (N < M ? N : M)], Point point);
__global__ void checkPoint(Point* d_points, Point* d_new_points, unsigned char* image, unsigned nb_threads, int* insert_index, int run_num) //bool* maze
{
for (int index = (blockIdx.x * nb_threads + threadIdx.x); index < N; index += nb_threads) {
int pos[4] = { -1 };
// If Point is empty (ie, still at initial Point(-1,-1) value), skip process but still copy back results
if (d_points[index].getR() >= 0) {
// Get row and col for current point
int row = d_points[index].getR();
int col = d_points[index].getC();
// If point above is in bounds and not a wall
if (row - 1 >= 0 && image[(row - 1) * 4 * N + col * 4 + 2] > 250) {
// Insert in shared array and get insertion index
if (insertPoint(new_points, Point((row - 1), col))) {
pos[0] = atomicAdd(&insert_index[0], 1);
new_points[pos[0]] = Point((row - 1), col);
image[(row - 1) * 4 * N + col * 4] = 255 - run_num;
image[(row - 1) * 4 * N + col * 4 + 1] = 0;
image[(row - 1) * 4 * N + col * 4 + 2] = 0;
}
}
// If point to the left is in bounds and not a wall
if (col - 1 >= 0 && image[row * 4 * N + (col - 1) * 4 + 2] > 250) {
// Insert in shared array and get insertion index
if (insertPoint(new_points, Point(row, (col - 1)))) {
pos[1] = atomicAdd(&insert_index[0], 1);
new_points[pos[1]] = Point(row, (col - 1));
image[row * 4 * N + (col - 1) * 4] = 255 - run_num;
image[row * 4 * N + (col - 1) * 4 + 1] = 0;
image[row * 4 * N + (col - 1) * 4 + 2] = 0;
}
}
// If point below is in bounds and not a wall
if (row + 1 < M && image[(row + 1) * 4 * N + col * 4 + 2] > 250) {
// Insert in shared array and get insertion index
if (insertPoint(new_points, Point((row + 1), col))) {
pos[2] = atomicAdd(&insert_index[0], 1);
new_points[pos[2]] = Point((row + 1), col);
image[(row + 1) * 4 * N + col * 4] = 255 - run_num;
image[(row + 1) * 4 * N + col * 4 + 1] = 0;
image[(row + 1) * 4 * N + col * 4 + 2] = 0;
}
}
// If point to the right is in bounds and not a wall
if (col + 1 < N && image[row * 4 * N + (col + 1) * 4 + 2] > 250) {
// Insert in shared array and get insertion index
if (insertPoint(new_points, Point(row, (col + 1)))) {
pos[3] = atomicAdd(&insert_index[0], 1);
new_points[pos[3]] = Point(row, (col + 1));
image[row * 4 * N + (col + 1) * 4] = 255 - run_num;
image[row * 4 * N + (col + 1) * 4 + 1] = 0;
image[row * 4 * N + (col + 1) * 4 + 2] = 0;
}
}
}
for (int k = 0; k < 4; k++) {
if (pos[k] != -1) {
d_new_points[pos[k]] = new_points[pos[k]];
}
}
}
}
__global__ void shared_initialize() {
__shared__ Point i[2 * (N < M ? N : M)];
for (int j = 0; j < 2 * (N < M ? N : M); j++) {
i[j] = Point(-1, -1);
}
new_points = (Point*)&i;
}
// Insert given point at the first available position in the given array (avoiding duplicate points)
__device__ bool insertPoint(Point array[2 * (N < M ? N : M)], Point point) {
int i;
// Cycle through array points until the end or an empty point (ie, still at initial Point(-1,-1) value) is reached
for (i = 0; i < 2 * (N < M ? N : M) && array[i].getR() >= 0; i++) {
// If duplicate point found (ie, point we want to insert is already in the array) do nothing and return
if (point.getR() == array[i].getR() && point.getC() == array[i].getC()) {
return false;
}
}
return true;
}
int main(int argc, char* argv[])
{
struct timeb start_time, end_time, cuda_start, cuda_end;
double cuda_total = 0, total_time = 0;
ftime(&start_time);
const int diagonalSize = 2 * (N < M ? N : M);
if (argc < 2) {
printf("Invalid arguments! Usage: ./ParallelMazeSolver <name of input png> (optional)<number of threads>\n");
return -1;
}
char* input_filename = argv[1];
unsigned total_threads = diagonalSize;
if (argc == 3) total_threads = atoi(argv[2]);
unsigned nb_threads = total_threads;
unsigned nb_blocks = 1;
bool pathFound = false;
// Max threads per block is 1024
while (nb_threads > 1024) {
nb_blocks++;
nb_threads = total_threads / nb_blocks;
}
char* output_filename = (char*)malloc(strlen(input_filename));
for (int i = 0; i < strlen(input_filename) - 7; i++) {
output_filename[i] = input_filename[i];
}
output_filename[strlen(input_filename) - 7] = 'p';
output_filename[strlen(input_filename) - 6] = '.';
output_filename[strlen(input_filename) - 5] = 'p';
output_filename[strlen(input_filename) - 4] = 'n';
output_filename[strlen(input_filename) - 3] = 'g';
output_filename[strlen(input_filename) - 2] = '\0';
unsigned error;
unsigned char* image = (unsigned char*)malloc(N * M * sizeof(unsigned char) * 4);
unsigned char* image_copy;
unsigned image_width, image_height;
// Decode image
error = lodepng_decode32_file(&image, &image_width, &image_height, input_filename);
if (error) printf("error %u: %s\n", error, lodepng_error_text(error));
printf("Input file: %s, maze width: %d, maze height: %d\n", input_filename, image_width, image_height);
printf("Number of blocks: %d, number of threads: %d\n", nb_blocks, nb_threads);
// Check that maze is non-empty
if (image_width * image_height != 0) {
// Array of points to be visited at each iteration, initialized with all Point(-1,-1) entries
Point points[diagonalSize];
// Set first point to be visited - the arrival point (because we are backtracking), the last point of the maze (assuming square maze)
points[0] = Point(M - 1, N - 1);
cudaMallocManaged((void**)& image_copy, image_width * image_height * 4 * sizeof(unsigned char));
cudaMemcpy(image_copy, image, image_width * image_height * 4 * sizeof(unsigned char), cudaMemcpyHostToDevice);
// Cuda copies of the points to be visited and resulting new points to visit for next iteration
Point* d_points, * d_new_points;
cudaMallocManaged((void**)& d_points, diagonalSize * sizeof(Point));
cudaMallocManaged((void**)& d_new_points, diagonalSize * sizeof(Point));
for (int a = 0; a < diagonalSize; a++) {
d_new_points[a] = Point(-1, -1);
}
int* d_data;
cudaMalloc((void**)& d_data, 1 * sizeof(int));
cudaMemset(d_data, 0, 1 * sizeof(int));
int run_num = 0;
//Initialize shared memory
shared_initialize <<<1,1>>>();
// While there are still points to visit
while (points[0].getR() != -1 && !pathFound) {
for (int k = 0; k < diagonalSize; k++) {
if (points[k].getR() == 0 && points[k].getC() == 0) {
pathFound = true;
break;
}
if (points[k].getR() == -1) {
break;
}
}
cudaMemcpy(d_points, &points, diagonalSize * sizeof(Point), cudaMemcpyHostToDevice);
cudaMemset(d_data, 0, 1 * sizeof(int));
ftime(&cuda_start);
// Call to device function with N threads (at most N points), points to be visited, and array to hold resulting new points to visit for next iteration
checkPoint << <nb_blocks, nb_threads >> > (d_points, d_new_points, image_copy, nb_threads, d_data, run_num); //d_maze
cudaDeviceSynchronize();
ftime(&cuda_end);
cuda_total += 1000 * (cuda_end.time - cuda_start.time) + (cuda_end.millitm - cuda_start.millitm);
// Copy the resulting new points to visit for next iteration into the points to be visited array
cudaMemcpy(&points, d_new_points, diagonalSize * sizeof(Point), cudaMemcpyDeviceToHost);
run_num++;
if (run_num % 10 == 0) run_num = 0;
}
lodepng_encode32_file(output_filename, image_copy, image_width, image_height);
// Free cuda copies memory
cudaFree(image_copy);
cudaFree(d_points);
cudaFree(d_new_points);
}
// Check that path has been found
if (pathFound) printf("Path found!\n");
else printf("Path not found!\n");
ftime(&end_time);
total_time = 1000 * (end_time.time - start_time.time) + (end_time.millitm - start_time.millitm);
printf("Total execution time: %d, Parallel execution time: %d\n", (int)total_time, (int)cuda_total);
struct timeb s_time, e_time;
ftime(&s_time);
postProcessing(output_filename);
ftime(&e_time);
float post_time = 1000 * (e_time.time - s_time.time) + (e_time.millitm - s_time.millitm);
printf("Post processing time: %d", (int)post_time);
return 0;
}
int findNextIndex(unsigned char* image, int index, int indexValue, int h, int w) {
int nextIndex = -1;
if (indexValue == 255) indexValue = 245;
int r = index / (w * 4);
int c = index % (w * 4) / 4;
unsigned current = r * 4 * w + c * 4;
unsigned right = current + 4;
unsigned down = current + 4 * w;
unsigned left = current - 4;
unsigned up = current - 4 * w;
if (r == 0 && c == 0) {
if (image[right] == indexValue + 1) {
nextIndex = right;
}
if (image[down] == indexValue + 1) {
nextIndex = down;
}
}
else if (r == 0) {
if (image[right] == indexValue + 1) {
nextIndex = right;
}
if (image[down] == indexValue + 1) {
nextIndex = down;
}
if (image[left] == indexValue + 1) {
nextIndex = left;
}
}
else if (r == h - 1) {
if (image[right] == indexValue + 1) {
nextIndex = right;
}
if (image[up] == indexValue + 1) {
nextIndex = up;
}
if (image[left] == indexValue + 1) {
nextIndex = left;
}
}
else if (c == 0) {
if (image[up] == indexValue + 1) {
nextIndex = up;
}
if (image[down] == indexValue + 1) {
nextIndex = down;
}
if (image[right] == indexValue + 1) {
nextIndex = right;
}
}
else if (c == w - 1) {
if (image[up] == indexValue + 1) {
nextIndex = up;
}
if (image[down] == indexValue + 1) {
nextIndex = down;
}
if (image[left] == indexValue + 1) {
nextIndex = left;
}
}
else {
if (image[up] == indexValue + 1) {
nextIndex = up;
}
if (image[down] == indexValue + 1) {
nextIndex = down;
}
if (image[left] == indexValue + 1) {
nextIndex = left;
}
if (image[right] == indexValue + 1) {
nextIndex = right;
}
}
return nextIndex;
}
void postProcessing(char* inputImage) {
unsigned error;
unsigned char* image;
unsigned image_width, image_height;
error = lodepng_decode32_file(&image, &image_width, &image_height, inputImage);
if (error) printf("error %u: %s\n", error, lodepng_error_text(error));
int index = 0;
while (index != image_width * image_height - 1) {
int indexValue = image[index];
image[index] = (char)0;
image[index + 1] = (char)255;
index = findNextIndex(image, index, indexValue, image_width, image_height);
if (index == -1) {
lodepng_encode32_file(inputImage, image, image_width, image_height);
return;
}
}
}