-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
334 lines (294 loc) · 9.16 KB
/
main.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
/**
* NOTE TO STUDENTS: you should not need to modify this file.
* You can if you wish to, but incorrectly modifying it may lead to an incorrect program which you will be penalized for.
*
* Also, if you modify this file (e.g. for some optimization) and wish it to be taken into consideration for grading,
* please make a note of it in your report. Otherwise, we will NOT consider it when grading.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include "exporter.h"
#include "settings.h"
#include "goi_cuda.h"
int readParam(FILE *fp, char **line, size_t *len, int *param);
int readWorldLayout(FILE *fp, char **line, size_t *len, int *world, int nRows, int nCols);
/**
* Handles input, output and file open/close operations. Delegates simulation to goi.
*/
int main(int argc, char *argv[])
{
int nGenerations;
int nRows;
int nCols;
int *startWorld;
int nInvasions;
int *invasionTimes;
int **invasionPlans;
// Grid and block params
int gridX;
int gridY;
int gridZ;
int blockX;
int blockY;
int blockZ;
FILE *outputFile;
FILE *inputFile;
char *line = NULL;
size_t len = 0;
if (argc < 8)
{
#if EXPORT_GENERATIONS
fprintf(stderr, "Usage: %s <INPUT_PATH> <OUTPUT_PATH> <NUM_THREADS> [<OPT_EXPORT_PATH>]\n", argv[0]);
#else
fprintf(stderr, "Usage: %s <INPUT_PATH> <OUTPUT_PATH> <GRID_X> <GRID_Y> <GRID_Z> <BLOCK_X> <BLOCK_Y> <BLOCK_Z>\n", argv[0]);
#endif
exit(EXIT_FAILURE);
}
printf("<INPUT_PATH>: %s\n", argv[1]);
printf("<OUTPUT_PATH>: %s\n", argv[2]);
printf("<GRID_X>: %s\n", argv[3]);
printf("<GRID_Y>: %s\n", argv[4]);
printf("<GRID_Z>: %s\n", argv[5]);
printf("<BLOCK_X>: %s\n", argv[6]);
printf("<BLOCK_Y>: %s\n", argv[7]);
printf("<BLOCK_Z>: %s\n", argv[8]);
#if EXPORT_GENERATIONS
FILE *exportFile = NULL;
if (argc >= 5)
{
printf("<OPT_EXPORT_PATH>: %s\n", argv[4]);
exportFile = fopen(argv[4], "w");
initWorldExporter(exportFile);
}
#endif
inputFile = fopen(argv[1], "r");
if (inputFile == NULL)
{
fprintf(stderr, "Failed to open %s for reading. Aborting...\n", argv[1]);
exit(EXIT_FAILURE);
}
outputFile = fopen(argv[2], "w");
if (outputFile == NULL)
{
fprintf(stderr, "Failed to open %s for writing. Aborting...\n", argv[2]);
exit(EXIT_FAILURE);
}
// Parse gridX
if (sscanf(argv[3], "%d", &gridX) != 1 )
{
fprintf(stderr, "Failed to parse <GRID_X> as positive integer. Got '%s'. Aborting...\n", argv[3]);
exit(EXIT_FAILURE);
}
if (gridX < 1)
{
fprintf(stderr, "<GRID_X> has invalid value: %d. Aborting...\n", gridX);
exit(EXIT_FAILURE);
}
// Parse gridY
if (sscanf(argv[4], "%d", &gridY) != 1 )
{
fprintf(stderr, "Failed to parse <GRID_Y> as positive integer. Got '%s'. Aborting...\n", argv[4]);
exit(EXIT_FAILURE);
}
if (gridY < 1)
{
fprintf(stderr, "<GRID_Y> has invalid value: %d. Aborting...\n", gridY);
exit(EXIT_FAILURE);
}
// Parse gridZ
if (sscanf(argv[5], "%d", &gridZ) != 1 )
{
fprintf(stderr, "Failed to parse <GRID_Z> as positive integer. Got '%s'. Aborting...\n", argv[5]);
exit(EXIT_FAILURE);
}
if (gridZ < 1)
{
fprintf(stderr, "<GRID_Z> has invalid value: %d. Aborting...\n", gridZ);
exit(EXIT_FAILURE);
}
// Parse blockX
if (sscanf(argv[6], "%d", &blockX) != 1 )
{
fprintf(stderr, "Failed to parse <BLOCK_X> as positive integer. Got '%s'. Aborting...\n", argv[6]);
exit(EXIT_FAILURE);
}
if (blockX < 1)
{
fprintf(stderr, "<BLOCK_X> has invalid value: %d. Aborting...\n", blockX);
exit(EXIT_FAILURE);
}
// Parse blockY
if (sscanf(argv[7], "%d", &blockY) != 1 )
{
fprintf(stderr, "Failed to parse <BLOCK_Y> as positive integer. Got '%s'. Aborting...\n", argv[7]);
exit(EXIT_FAILURE);
}
if (blockY < 1)
{
fprintf(stderr, "<BLOCK_Y> has invalid value: %d. Aborting...\n", blockY);
exit(EXIT_FAILURE);
}
// Parse blockZ
if (sscanf(argv[8], "%d", &blockZ) != 1 )
{
fprintf(stderr, "Failed to parse <BLOCK_Z> as positive integer. Got '%s'. Aborting...\n", argv[8]);
exit(EXIT_FAILURE);
}
if (blockZ < 1)
{
fprintf(stderr, "<BLOCK_Z> has invalid value: %d. Aborting...\n", blockZ);
exit(EXIT_FAILURE);
}
// Read nGenerations
if (readParam(inputFile, &line, &len, &nGenerations) == -1)
{
fprintf(stderr, "Failed to read N_GENERATIONS. Aborting...\n");
exit(EXIT_FAILURE);
}
// Read nRows
if (readParam(inputFile, &line, &len, &nRows) == -1)
{
fprintf(stderr, "Failed to read N_ROWS. Aborting...\n");
exit(EXIT_FAILURE);
}
// Read nCols
if (readParam(inputFile, &line, &len, &nCols) == -1)
{
fprintf(stderr, "Failed to read N_COLS. Aborting...\n");
exit(EXIT_FAILURE);
}
if (nRows == 0 || nCols == 0)
{
fprintf(stderr, "N_ROWS or N_COLS is 0. Aborting...\n");
exit(EXIT_FAILURE);
}
// Read start world
startWorld = (int*) malloc(sizeof(int) * nRows * nCols);
if (startWorld == NULL || readWorldLayout(inputFile, &line, &len, startWorld, nRows, nCols) == -1)
{
fprintf(stderr, "Failed to read STARTING_WORLD. Aborting...\n");
exit(EXIT_FAILURE);
}
// Read nInvasions
if (readParam(inputFile, &line, &len, &nInvasions) == -1)
{
fprintf(stderr, "Failed to read N_INVASIONS. Aborting...\n");
exit(EXIT_FAILURE);
}
// Read invasions
invasionTimes = (int*) malloc(sizeof(int) * nInvasions);
invasionPlans = (int**) malloc(sizeof(int *) * nInvasions);
if (invasionTimes == NULL || invasionPlans == NULL)
{
fprintf(stderr, "No memory for invasions. Aborting...\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < nInvasions; i++)
{
if (invasionTimes == NULL || readParam(inputFile, &line, &len, invasionTimes + i))
{
fprintf(stderr, "Failed to read INVASION_TIME. Aborting...\n");
exit(EXIT_FAILURE);
}
invasionPlans[i] = (int*) malloc(sizeof(int) * nRows * nCols);
if (invasionPlans[i] == NULL || readWorldLayout(inputFile, &line, &len, invasionPlans[i], nRows, nCols))
{
fprintf(stderr, "Failed to read INVASION_PLAN. Aborting...\n");
exit(EXIT_FAILURE);
}
}
#if PRINT_GENERATIONS
printf("N_GENERATIONS: %d, N_ROWS: %d, N_COLS: %d, N_INVASIONS: %d\n", nGenerations, nRows, nCols, nInvasions);
printf("\n== STARTING_WORLD ==\n");
printWorld(startWorld, nRows, nCols);
for (int i = 0; i < nInvasions; i++)
{
printf("\n== invasion %d at time: %d ==\n", i, invasionTimes[i]);
printWorld(invasionPlans[i], nRows, nCols);
}
#endif
// we're done with the file
fclose(inputFile);
if (line)
{
free(line);
}
// run the simulation
int warDeathToll = goi(nGenerations, startWorld, nRows, nCols, nInvasions, invasionTimes, invasionPlans, gridX, gridY, gridZ, blockX, blockY, blockZ);
// output the result
fprintf(outputFile, "%d", warDeathToll);
fclose(outputFile);
#if EXPORT_GENERATIONS
if (exportFile != NULL)
{
fclose(exportFile);
}
#endif
// free everything!
for (int i = 0; i < nInvasions; i++)
{
free(invasionPlans[i]);
}
free(invasionTimes);
free(invasionPlans);
free(startWorld);
}
// readParam reads one integer from a line into param, advancing the read head to the next line.
// -1 is returned on error.
int readParam(FILE *fp, char **line, size_t *len, int *param)
{
if (getline(line, len, fp) == -1 ||
sscanf(*line, "%d", param) != 1)
{
return -1;
}
return 0;
}
/**
* 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 setValue(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;
}
// readWorldLayout reads a world layout specified by nRows and nCols, advancing the read head by
// nRows number of lines. -1 is returned on error.
int readWorldLayout(FILE *fp, char **line, size_t *len, int *world, int nRows, int nCols)
{
for (int row = 0; row < nRows; row++)
{
if (getline(line, len, fp) == -1)
{
return -1;
}
char *p = *line;
for (int col = 0; col < nCols; col++)
{
char *end;
int cell = strtol(p, &end, 10);
// unexpected end
if (cell == 0 && end == p)
{
return -1;
}
// other errors
if (errno == EINVAL || errno == ERANGE)
{
return -1;
}
setValue(world, nRows, nCols, row, col, cell);
p = end;
}
}
return 0;
}