-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoi_cuda_baseline.cu
474 lines (409 loc) · 13.8 KB
/
goi_cuda_baseline.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include "exporter.h"
#include "settings.h"
// including the "dead faction": 0
#define MAX_FACTIONS 10
// this macro is here to make the code slightly more readable, not because it can be safely changed to
// any integer value; changing this to a non-zero value may break the code
#define DEAD_FACTION 0
// death toll due to fighting
__device__ __managed__ int deathToll;
void check_cuda_errors()
{
cudaError_t rc;
rc = cudaGetLastError();
if (rc != cudaSuccess)
{
printf("Last CUDA error %s\n", cudaGetErrorString(rc));
}
}
/**
* Specifies the number(s) of live neighbors of the same faction required for a dead cell to become alive.
*/
__device__ bool isBirthable(int n)
{
return n == 3;
}
/**
* Specifies the number(s) of live neighbors of the same faction required for a live cell to remain alive.
*/
__device__ bool isSurvivable(int n)
{
return n == 2 || n == 3;
}
/**
* Specifies the number of live neighbors of a different faction required for a live cell to die due to fighting.
*/
__device__ bool willFight(int n) {
return n > 0;
}
/**
* returns the value at the input row and col of the input grid, if valid.
*
* -1 is returned if row or col is out of bounds (as specified by nRows and nCols).
*/
__device__ int getValueAtCuda(const int *grid, int nRows, int nCols, int row, int col)
{
if (row < 0 || row >= nRows || col < 0 || col >= nCols)
{
return -1;
}
return *(grid + (row * nCols) + col);
}
/**
* sets the value at the input row and col of the input grid to val.
*
* Does nothing if row or col is out of bounds (as specified by nRows and nCols).
*/
__device__ void setValueAtCuda(int *grid, int nRows, int nCols, int row, int col, int val)
{
if (row < 0 || row >= nRows || col < 0 || col >= nCols)
{
return;
}
*(grid + (row * nCols) + col) = val;
}
/**
* returns the value at the input row and col of the input grid, if valid.
*
* -1 is returned if row or col is out of bounds (as specified by nRows and nCols).
*/
int getValueAt(const int *grid, int nRows, int nCols, int row, int col)
{
if (row < 0 || row >= nRows || col < 0 || col >= nCols)
{
return -1;
}
return *(grid + (row * nCols) + col);
}
/**
* sets the value at the input row and col of the input grid to val.
*
* Does nothing if row or col is out of bounds (as specified by nRows and nCols).
*/
void setValueAt(int *grid, int nRows, int nCols, int row, int col, int val)
{
if (row < 0 || row >= nRows || col < 0 || col >= nCols)
{
return;
}
*(grid + (row * nCols) + col) = val;
}
/**
* Writes the input world to stdout.
*/
void printWorld(const int *world, int nRows, int nCols)
{
for (int row = 0; row < nRows; row++)
{
for (int col = 0; col < nCols; col++)
{
printf("%d ", *(world + (row * nCols) + col));
}
printf("\n");
}
}
/**
* Computes and returns the next state of the cell specified by row and col based on currWorld and invaders. Sets *diedDueToFighting to
* true if this cell should count towards the death toll due to fighting.
*
* invaders can be NULL if there are no invaders.
*/
__device__ int getNextState(const int *currWorld, const int *invaders, int nRows, int nCols, int row, int col, bool *diedDueToFighting)
{
// we'll explicitly set if it was death due to fighting
*diedDueToFighting = false;
// faction of this cell
int cellFaction = getValueAtCuda(currWorld, nRows, nCols, row, col);
// did someone just get landed on?
if (invaders != NULL && getValueAtCuda(invaders, nRows, nCols, row, col) != DEAD_FACTION)
{
*diedDueToFighting = cellFaction != DEAD_FACTION;
return getValueAtCuda(invaders, nRows, nCols, row, col);
}
// tracks count of each faction adjacent to this cell
int neighborCounts[MAX_FACTIONS];
memset(neighborCounts, 0, MAX_FACTIONS * sizeof(int));
// count neighbors (and self)
for (int dy = -1; dy <= 1; dy++)
{
for (int dx = -1; dx <= 1; dx++)
{
int faction = getValueAtCuda(currWorld, nRows, nCols, row + dy, col + dx);
if (faction >= DEAD_FACTION)
{
neighborCounts[faction]++;
}
}
}
// we counted this cell as its "neighbor"; adjust for this
neighborCounts[cellFaction]--;
if (cellFaction == DEAD_FACTION)
{
// this is a dead cell; we need to see if a birth is possible:
// need exactly 3 of a single faction; we don't care about other factions
// by default, no birth
int newFaction = DEAD_FACTION;
// start at 1 because we ignore dead neighbors
for (int faction = DEAD_FACTION + 1; faction < MAX_FACTIONS; faction++)
{
int count = neighborCounts[faction];
if (isBirthable(count))
{
newFaction = faction;
}
}
return newFaction;
}
else
{
/**
* this is a live cell; we follow the usual rules:
* Death (fighting): > 0 hostile neighbor
* Death (underpopulation): < 2 friendly neighbors and 0 hostile neighbors
* Death (overpopulation): > 3 friendly neighbors and 0 hostile neighbors
* Survival: 2 or 3 friendly neighbors and 0 hostile neighbors
*/
int hostileCount = 0;
for (int faction = DEAD_FACTION + 1; faction < MAX_FACTIONS; faction++)
{
if (faction == cellFaction)
{
continue;
}
hostileCount += neighborCounts[faction];
}
if (willFight(hostileCount))
{
*diedDueToFighting = true;
return DEAD_FACTION;
}
int friendlyCount = neighborCounts[cellFaction];
if (!isSurvivable(friendlyCount))
{
return DEAD_FACTION;
}
return cellFaction;
}
}
__global__ void simulate(int *wholeNewWorld, const int *world, const int * inv, int nRows, int nCols, int elementPerThread)
{
// printf("Currently on block %d %d %d, thread %d %d %d\n", blockIdx.x, blockIdx.y, blockIdx.z, threadIdx.x, threadIdx.y, threadIdx.z);
int blockId = blockIdx.x + blockIdx.y * gridDim.x + gridDim.x * gridDim.y * blockIdx.z;
int threadId = blockId * (blockDim.x * blockDim.y * blockDim.z) + (threadIdx.z * (blockDim.x * blockDim.y)) + (threadIdx.y * blockDim.x) + threadIdx.x;
// calculate the starting and ending position of the world to be computed by the current thread
int firstElement = threadId * elementPerThread;
int lastElement = ((threadId + 1) * elementPerThread) - 1;
// handle cases where nRows * nCols are not divisible by nThreads.
int max = nRows * nCols - 1;
if (lastElement > max) {
lastElement = max;
}
int currentElement = firstElement;
int row, col;
while (currentElement <= lastElement) {
row = currentElement / nCols;
col = currentElement % nCols;
// printf("[thread %d] row:%d, col:%d\n", threadId, row, col);
bool diedDueToFighting;
int nextState = getNextState(world, inv, nRows, nCols, row, col, &diedDueToFighting);
setValueAtCuda(wholeNewWorld, nRows, nCols, row, col, nextState);
if (diedDueToFighting) {
// critical region
atomicAdd(&deathToll, 1);
}
currentElement++;
}
}
/**
* The main simulation logic.
*
* goi does not own startWorld, invasionTimes or invasionPlans and should not modify or attempt to free them.
* nThreads is the number of threads to simulate with. It is ignored by the sequential implementation.
*/
int goi(int nGenerations, const int *startWorld, int nRows, int nCols, int nInvasions, const int *invasionTimes, int **invasionPlans,
int gridX, int gridY, int gridZ, int blockX, int blockY, int blockZ)
{
cudaError_t rc;
size_t worldSizeInBytes = nRows * nCols * sizeof(int);
// grid and block dimensions
dim3 gridDim(gridX, gridY, gridZ);
dim3 blockDim(blockX, blockY, blockZ);
int nThreads = blockX * blockY * blockZ * gridX * gridY * gridZ;
// rounding up the division to the nearest integer
// further assume that nRows * nCols >= nThreads
int elementPerThread = 1 +((nRows * nCols - 1) / nThreads);
// init deathtoll
deathToll = 0;
// init the world!
// we make a copy because we do not own startWorld (and will perform free() on hostWorld)
int* world = (int*) malloc(worldSizeInBytes);
if (world == NULL)
{
return -1;
}
// set value of the host world
// TODO (maybe can just use the startWorld since we are not modifying)
for (int row = 0; row < nRows; row++)
{
for (int col = 0; col < nCols; col++)
{
setValueAt(world, nRows, nCols, row, col, getValueAt(startWorld, nRows, nCols, row, col));
}
}
#if PRINT_GENERATIONS
printf("\n=== WORLD 0 ===\n");
printWorld(world, nRows, nCols);
#endif
#if EXPORT_GENERATIONS
exportWorld(world, nRows, nCols);
#endif
// Begin simulating
int invasionIndex = 0;
for (int i = 1; i <= nGenerations; i++)
{
// is there an invasion this generation?
int *inv = NULL;
int *inv_cuda = NULL;
if (invasionIndex < nInvasions && i == invasionTimes[invasionIndex])
{
// we make a copy because we do not own invasionPlans
inv = (int*) malloc(worldSizeInBytes);
if (inv == NULL)
{
free(world);
return -1;
}
rc = cudaMalloc((void**) &inv_cuda, worldSizeInBytes);
if (rc != cudaSuccess) {
free(world);
free(inv);
return -1;
}
for (int row = 0; row < nRows; row++)
{
for (int col = 0; col < nCols; col++)
{
setValueAt(inv, nRows, nCols, row, col, getValueAt(invasionPlans[invasionIndex], nRows, nCols, row, col));
}
}
invasionIndex++;
cudaMemcpy(inv_cuda, inv, worldSizeInBytes, cudaMemcpyHostToDevice);
}
// Copy world to CUDA
int* world_cuda;
rc = cudaMalloc((void **)&world_cuda, sizeof(int) * nRows * nCols);
if (rc != cudaSuccess) {
printf("Could not cudaMalloc world_cuda. Reason: %s\n", cudaGetErrorString(rc));
free(world);
cudaFree(world_cuda);
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
return -1;
}
rc = cudaMemcpy(world_cuda, world, sizeof(int) * nRows * nCols, cudaMemcpyHostToDevice);
if (rc != cudaSuccess)
{
printf("Could not copy world_cuda to device. Reason: %s\n", cudaGetErrorString(rc));
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
free(world);
cudaFree(world_cuda);
return -1;
}
// create the next world state
int *wholeNewWorld = (int*) malloc(sizeof(int) * nRows * nCols);
if (wholeNewWorld == NULL)
{
printf("Could not malloc wholeNewWorld.\n");
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
free(world);
cudaFree(world_cuda);
return -1;
}
// create newWorld state for cuda
int *wholeNewWorld_cuda;
rc = cudaMalloc((void **)&wholeNewWorld_cuda, sizeof(int) * nRows * nCols);
if (rc != cudaSuccess) {
printf("Could not cudaMalloc wholeNewWorld_cuda. Reason: %s\n", cudaGetErrorString(rc));
free(world);
cudaFree(world_cuda);
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
free(wholeNewWorld);
return -1;
}
// Simulate next state
simulate<<<gridDim, blockDim>>>(wholeNewWorld_cuda, world_cuda, inv_cuda, nRows, nCols, elementPerThread);
check_cuda_errors();
cudaError_t cudaerr = cudaDeviceSynchronize();
if (cudaerr != cudaSuccess) {
printf("kernel launch failed with error \"%s\".\n", cudaGetErrorString(cudaerr));
free(world);
cudaFree(world_cuda);
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
free(wholeNewWorld);
cudaFree(wholeNewWorld_cuda);
return -1;
}
// retrive data
rc = cudaMemcpy(wholeNewWorld, wholeNewWorld_cuda, sizeof(int) * nRows * nCols, cudaMemcpyDeviceToHost);
if (rc != cudaSuccess)
{
printf("Could not copy from device. Reason: %s\n", cudaGetErrorString(rc));
free(world);
cudaFree(world_cuda);
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
free(wholeNewWorld);
cudaFree(wholeNewWorld_cuda);
return -1;
}
// clean up
cudaFree(wholeNewWorld_cuda);
cudaFree(world_cuda);
// free inv and inv_cuda if needed
if (inv != NULL)
{
free(inv);
cudaFree(inv_cuda);
}
// swap worlds
free(world);
world = wholeNewWorld;
#if PRINT_GENERATIONS
printf("\n=== WORLD %d ===\n", i);
printWorld(world, nRows, nCols);
#endif
#if EXPORT_GENERATIONS
exportWorld(world, nRows, nCols);
#endif
}
free(world);
return deathToll;
}