-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.cu
272 lines (194 loc) · 6.22 KB
/
solver.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
/*
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <cuda_runtime.h>
#include "board.h"
/* Parallel version of moveAllWalls.
2D thread array.
Input: walls, moves, opponentIdx
*/
__global__ void
CUDA_solveForAllWalls(wall *d_walls, nextMove *d_moves, int oppPos) {
int tidx = threadIdx.x; // X-Dim = Wall
int tidy = threadIdx.y; // Y-Dim = Direction
int idx = blockIdx.x; // Space #
//printf("Thread: (%d, %d)\n", tidx, tidy);
// - - - - -
// Coalesced Load d_walls Global --> Shared for this Block
// Only threads (0-15, 0)
__shared__ wall sharedWalls[NUM_WALLS];
if (tidy == 0) {
sharedWalls[tidx] = d_walls[tidx];
}
// - - - - -
// Create a blank board template --> Shared for this block
// Only threads (0-15, 1)
__shared__ space sharedBoardTemplate[NUM_SPACES];
// Spaces 0-15 First 16 spaces
if (tidy == 1) {
CUDA_boardInitParallel(sharedBoardTemplate, tidx);
}
// Spaces 16-29
if (tidy == 2 && (tidx + 16) < NUM_SPACES) {
//printf("tidx: %d; NUM_SPACES: %d\n", (tidx + 16), NUM_SPACES);
CUDA_boardInitParallel(sharedBoardTemplate, (tidx + 16));
}
// - - - - -
// Create shared move, global --> shared
// Threads (0-4, 3)
__shared__ nextMove move;
if (tidy == 3) {
switch(tidx) { // Thread []
case 0:
move.space = d_moves[idx].space;
break;
case 1:
move.playerScore = d_moves[idx].playerScore;
break;
case 2:
move.oppScore = d_moves[idx].oppScore;
break;
case 3:
move.wallIdx = d_moves[idx].wallIdx;
break;
case 4:
move.newDir = d_moves[idx].newDir;
break;
}
}
__syncthreads();
// Each thread makes local copy of walls
wall l_walls[NUM_WALLS];
for (int i = 0; i < NUM_WALLS; i++) {
l_walls[i] = sharedWalls[i];
}
// Check for wall collisions && if it's the same direction
wall oldDir = l_walls[tidx];
bool sameDir = (oldDir == (wall)tidy);
l_walls[tidx] = (wall) tidy;
bool collision = CUDA_checkWallCollisions(&l_walls[0], tidx);
if (sameDir || collision) {
// Do nothing and return
return;
}
// If no collision, contune
// Create local copy of new board
space l_board[NUM_SPACES];
for (int i = 0; i < NUM_SPACES; i++) {
l_board[i] = sharedBoardTemplate[i];
}
// Generate the board from the walls
CUDA_generateBoard(&l_board[0], l_walls);
// Calculate shortest path for player & opponent
int playerScore = CUDA_shortestPath(&l_board[0], move.space);
int oppScore = CUDA_shortestPath(&l_board[0], oppPos);
printf("PlayerPos: %d -- Wall Idx: %d -- Wall Pos: %d -- PlayerScore: %d -- OppScore: %d\n", move.space, tidx, tidy, playerScore, oppScore);
if (playerScore < move.playerScore || oppScore > move.oppScore) {
move.playerScore = playerScore;
move.oppScore = oppScore;
move.wallIdx = tidx;
move.newDir = (wall) tidy;
}
d_moves[idx] = move;
}
// CUDA Error Check
void checkCudaError(cudaError_t e, char const *in) {
if (e != cudaSuccess) {
printf("CUDA Error: %s, %s \n", in, cudaGetErrorString(e));
exit(EXIT_FAILURE);
}
}
int main(int argc, char const *argv[])
{
int playerPos = 0;
int oppPos = 0;
int numSpaces = SPACE_LENGTH * SPACE_WIDTH;
size_t spaceSize = sizeof(space) * numSpaces;
int numWalls = WALL_LENGTH * WALL_WIDTH;
size_t wallSize = sizeof(wall) * numWalls;
// Malloc the array of wall / board
wall *walls = (wall *)malloc(wallSize);
space *board = (space *)malloc(spaceSize);
// Initialize and setup the current board state
boardInit(board);
generateWalls(walls);
generateBoard(board, walls);
//display board
outputBoard(board); //display by numbers
displayBoard(board); //display visually
// Find nearest neighbors to player
int *neighbors = findNeighbors(board, playerPos);
// Determine the number of spaces around the player
// Count the number of possible spaces = # of blocks
int possibleSpaces = 0;
for (int i = 0; i < 12; i++) {
if (neighbors[i] != -1) {
possibleSpaces++;
}
}
// Malloc an array nextMove[ # of neighbors ]
nextMove *moves = (nextMove *)malloc( sizeof(nextMove) * possibleSpaces );
// Zero-out the results array and set each move.space ot the neighbor space
int j = 0;
for (int i = 0; i < 12 && j < possibleSpaces; i++) {
if (neighbors[i] != -1) {
printf("Init results array. Moves[%d], Space: %d\n", j, neighbors[i]);
moves[j].space = neighbors[i];
moves[j].playerScore = 100; // Intentionally high preset
moves[j].oppScore = -1;
moves[j].wallIdx = -1;
moves[j].newDir = (wall) 0;
j++;
}
}
// Malloc space on device, copy to device
wall *d_walls = NULL;
nextMove *d_moves = NULL;
checkCudaError( cudaMalloc((void**) &d_walls, wallSize),
"Malloc d_walls");
checkCudaError( cudaMalloc((void**) &d_moves, (sizeof(nextMove) * possibleSpaces) ),
"Malloc d_walls");
// cudaMemcpy(target, source, size, function)
checkCudaError( cudaMemcpy(d_walls, walls, wallSize, cudaMemcpyHostToDevice),
"Copy walls to device");
checkCudaError( cudaMemcpy(d_moves, moves, (sizeof(nextMove) * possibleSpaces), cudaMemcpyHostToDevice),
"Copy moves to device");
// Setup: Measure Runtime
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
/* Kernel Call
Blocks = possible spaces
Threads = #walls * #possible directions
*/
dim3 grid(16,4);
CUDA_solveForAllWalls <<<possibleSpaces, grid>>> (d_walls, d_moves, oppPos);
checkCudaError(cudaGetLastError(), "Checking Last Error, Kernel Launch");
// Report kernel runtime
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
float elapsedTime;
cudaEventElapsedTime(&elapsedTime, start, stop);
printf("Time to generate: %0.5f seconds\n", elapsedTime/1000);
cudaEventDestroy(start);
cudaEventDestroy(stop);
// Copy Device --> Host
// cudaMemcpy(target, source, size, function)
checkCudaError( cudaMemcpy(moves, d_moves, (sizeof(nextMove) * possibleSpaces), cudaMemcpyDeviceToHost),
"Copy moves to host");
outputResults(moves, possibleSpaces);
// PICK THE BEST MOVE
nextMove bestMove = pickBestMove(moves, possibleSpaces);
printf("Best Move: %d\n", bestMove.space);
// Free Memory
checkCudaError(cudaFree(d_walls), "Free device histogram");
checkCudaError(cudaFree(d_moves), "Free device atom_list");
free(board);
free(walls);
free(moves);
return 0;
}