-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlife3d.cpp
568 lines (488 loc) · 16.1 KB
/
life3d.cpp
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include <omp.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <tuple>
#include <algorithm>
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
typedef std::tuple<int, int, int> Vector3;
// Node representing elements of the sparse matrix
struct node {
short z; // The z value
short num_neighbours; // The # of neighbours (not always right, only when we need it)
bool is_dead; // Is it a dead or an alive node?
bool operator<(const struct node& rhs) const
{
return z < rhs.z;
}
};
void print_node(struct node* n)
{
printf("{z: %hd, num_nei: %hd, is_dead: %s}\n", n->z, n->num_neighbours, n->is_dead ? "true" : "false");
}
typedef struct da_struct {
size_t initial_size;
void* data;
size_t used;
size_t size;
size_t step; // Size of an element
} dynamic_array;
void da_init(dynamic_array* da, size_t initial_size)
{
da->initial_size = initial_size;
da->step = sizeof(struct node);
da->used = 0;
da->size = da->initial_size;
da->data = realloc(NULL, da->initial_size * da->step);
}
dynamic_array da_make(size_t initial_size)
{
dynamic_array da;
da_init(&da, MAX(4, initial_size));
return da;
}
dynamic_array* da_make_ptr(size_t initial_size)
{
dynamic_array* da = (dynamic_array*) malloc(sizeof(dynamic_array));
da_init(da, MAX(4, initial_size));
return da;
}
void da_resize(dynamic_array* da, size_t new_size)
{
assert(da);
da->size = new_size;
da->data = realloc(da->data, new_size * da->step);
// printf("Reallocating to size %d\n", da->size);
}
int da_find_closest_higher(dynamic_array* da, short test_z)
{
assert(da);
// printf("\n\nStarting loop with da '%p'. Used = %lu; Data: %p\n", da, da->used, da->data);
for (size_t i = 0; i < da->used; i++) {
struct node* ptr = ((struct node*)da->data) + i;
if (ptr->z > test_z) {
return i;
}
}
return da->used;
}
void da_insert(dynamic_array* da, struct node* to_insert)
{
assert(da);
//printf("Inserting in da '%p', %hd\n", da, to_insert->z);
if (da->used == da->size) {
da_resize(da, da->size * 2);
}
/* Ordered array
struct node* data = ((struct node*)da->data);
int index = da_find_closest_higher(da, to_insert->z);
memmove(data+index+1, data+index, da->step*(da->used - index));
memcpy(data+index, to_insert, da->step);
*/
struct node* dest = ((struct node*)da->data) + da->used;
memcpy(dest, to_insert, da->step);
da->used++;
}
void da_delete_at(dynamic_array* da, size_t i)
{
assert(da);
// printf("Deleting in da '%p' at: %lu\n", da, i);
if (i >= da->used) { // i < 0 is always false, cause unsigned
printf("Invalid delete: index smaller or larger than the array size!\n");
return;
}
da->used--;
/* Ordered array
struct node* dest = ((struct node*)da->data) + i;
struct node* src = ((struct node*)da->data) + i + 1;
memmove(dest, src, da->step*(da->used - i));
*/
struct node* dest = ((struct node*)da->data) + i;
struct node* src = ((struct node*)da->data) + da->used;
memcpy(dest, src, da->step);
if (da->size > da->initial_size && da->used <= da->size / 4) {
da_resize(da, da->size / 2);
}
}
void da_free(dynamic_array* da)
{
assert(da);
if (da->data) {
free(da->data);
da->data = NULL;
da->used = 0;
da->size = 0;
}
}
void da_print(dynamic_array* da)
{
assert(da);
printf("************************\n");
printf("size: %lu\n", da->size);
printf("used: %lu\n", da->used);
printf("data:\n");
size_t i;
for (i = 0; i < da->used; i++) {
struct node* ptr = ((struct node*)da->data) + i;
printf(" ");
print_node(ptr);
}
printf("************************\n");
}
int da_empty(dynamic_array* da)
{
assert(da);
return da->used;
}
void da_clear(dynamic_array* da)
{
assert(da);
da->used = 0;
da_resize(da, da->initial_size);
}
int da_find_z(dynamic_array* da, short test_z)
{
assert(da);
// printf("\n\nStarting loop with da '%p'. Used = %lu; Data: %p\n", da, da->used, da->data);
for (size_t i = 0; i < da->used; i++) {
struct node* ptr = ((struct node*)da->data) + i;
if (ptr->z == test_z) {
return i;
}
}
return -1;
}
// ############################################################
// ######################### MATRIX ###########################
// ############################################################
struct matrix_struct {
short side;
dynamic_array** data;
};
typedef matrix_struct Matrix;
void matrix_print(Matrix* m);
// Returns a initialized matrix
inline Matrix make_matrix(short side)
{
Matrix m;
m.side = side;
m.data = (dynamic_array**) calloc(side * side, sizeof(dynamic_array*));
return m;
}
// Returns the head of the matrix in the pos\ition x and y. Can be NULL.
inline dynamic_array* matrix_get(Matrix* m, short x, short y)
{
return m->data[x + (y * m->side)];
}
// Returns the element with the x, y, z passed. NULL if it doesn't exist.
inline struct node* matrix_get_ele(Matrix* m, short x, short y, short z)
{
dynamic_array* da = matrix_get(m, x, y);
if (!da) {
return NULL;
}
int pos = da_find_z(da, z);
if (pos == -1) {
return NULL;
} else {
return ((struct node*)da->data) + pos;
}
}
// Insert a new element in the matrix (ordered)
inline void matrix_insert(Matrix* m, short x, short y, short z, bool is_dead, short num_nei)
{
struct node new_el = { z, num_nei, is_dead };
dynamic_array* da = matrix_get(m, x, y);
if (da == NULL) {
da = da_make_ptr(4);
m->data[x + (y * m->side)] = da;
}
da_insert(da, &new_el);
}
// Remove from the matrix. We need to search for the z in the linked list
inline void matrix_remove(Matrix* m, short x, short y, short z)
{
dynamic_array* da = matrix_get(m, x, y);
if(!da) {
return;
}
int pos = da_find_z(da, z);
//printf("Trying to delete %hd %hd %hd\n", x, y, z);
if (pos != -1) {
da_delete_at(da, pos);
}
}
// Print the live nodes in the matrix (the matrix only contains alive nodes at this point, so print all nodes)
void matrix_print_live(Matrix* m) {
short SIZE = m->side;
for (short i = 0; i < SIZE; i++) {
for (short j = 0; j < SIZE; j++) {
dynamic_array* da = matrix_get(m, i, j);
if (da != NULL) {
struct node* ptr = ((struct node*)da->data);
// Kinda sucks to sort here, but oh well
std::sort(ptr, (ptr + da->used));
for (size_t k = 0; k < da->used; k++) {
printf("%hd %hd %hd\n", i, j, ptr->z);
ptr++;
}
}
}
}
}
void matrix_print(Matrix* m) {
short SIZE = m->side;
for (short i = 0; i < SIZE; i++) {
for (short j = 0; j < SIZE; j++) {
dynamic_array* da = matrix_get(m, i, j);
if (da != NULL) {
struct node* ptr = ((struct node*)da->data);
printf("(%hd, %hd): [", i, j);
for (size_t k = 0; k < da->used; k++) {
printf("%hd%c", ptr->z, k == da->used-1 ? '\x07': ',');
ptr++;
}
printf("] (size: %lu; used: %lu)\n", da->size, da->used);
} else {
printf("(%hd, %hd): []\n", i, j);
}
}
}
}
inline short pos_mod(short val, short mod)
{
if (val >= mod)
return val - mod;
else if (val < 0)
return val + mod;
else
return val;
// The method above is faster!
// return ((val % mod) + mod) % mod;
// return (val % mod) + (mod * (val < 0));
}
int main(int argc, char* argv[])
{
double start, end, init_time, process_time;
start = omp_get_wtime();
if (argc != 3) {
printf("[ERROR] Incorrect usage!\n");
printf("[Usage] ./life3d <input_file> <nr_generations>\n");
return -1;
}
char* input_file = argv[1];
int generations = atoi(argv[2]);
if (generations <= 0) {
printf("[ERROR] Number of generations must be bigger that 0. Got: '%d'\n", generations);
return -1;
}
FILE* fp = fopen(input_file, "r");
if (fp == NULL) {
printf("[ERROR] Unable to read the input file.\n");
perror("[ERROR]");
return -1;
}
short SIZE;
if (fscanf(fp, "%hd", &SIZE) == EOF) {
printf("[ERROR] Unable to read the size.\n");
return -1;
}
// Finished parsing metadata. Now only need to parse the actual positions
Matrix m = make_matrix(SIZE);
short x, y, z;
while (fscanf(fp, "%hd %hd %hd", &x, &y, &z) != EOF) {
matrix_insert(&m, x, y, z, false, -1);
}
// Finished parsing!
fclose(fp);
end = omp_get_wtime();
init_time = end - start;
start = omp_get_wtime();
/*dynamic_array* T = da_make_ptr(8);
struct node n = {0, 0, false};
for (int i = 0; i < 12; i += 2) {
n.z = i;
n.num_neighbours = i;
da_insert(T, &n);
}
da_print(T);
for (int i = 1; i < 12; i += 2) {
n.z = i;
n.num_neighbours = i;
da_insert(T, &n);
}
da_print(T);
da_delete_at(T, 1);
da_print(T);
da_delete_at(T, 0);
da_print(T);
da_delete_at(T, T->used -1);
da_print(T);*/
//-----------------
//--- MAIN LOOP ---
//-----------------
for (int gen = 0; gen < generations; gen++) {
dynamic_array* da;
struct node *ptr, *to_test;
Vector3 t;
int32_t i, j;
short z, _z, _y, _x, y, x;
// printf("==============================================================\n");
// printf("==================== BEFORE INSERTING ========================\n");
// printf("==============================================================\n");
// matrix_print(&m);
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
da = matrix_get(&m, i, j);
if (!da) {
continue;
}
size_t limit = da->used;
// Iterate over every existing z for x and y
for (size_t k = 0; k < limit; k++) {
ptr = ((struct node*)da->data) + k;
// If its dead skip
if (ptr->is_dead) {
continue;
}
// PROCESS AN ALIVE NODE
x = i;
y = j;
z = ptr->z;
ptr->num_neighbours = 0;
_z = pos_mod(z + 1, SIZE);
to_test = matrix_get_ele(&m, x, y, _z);
if (to_test) {
if (to_test->is_dead == true) {
to_test->num_neighbours++;
} else {
ptr->num_neighbours++;
}
} else {
matrix_insert(&m, x, y, _z, true, 1);
ptr = ((struct node*)da->data) + k;
}
_z = pos_mod(z - 1, SIZE);
to_test = matrix_get_ele(&m, x, y, _z);
if (to_test) {
if (to_test->is_dead == true) {
to_test->num_neighbours++;
} else {
ptr->num_neighbours++;
}
} else {
matrix_insert(&m, x, y, _z, true, 1);
ptr = ((struct node*)da->data) + k;
}
_x = pos_mod(x + 1, SIZE);
to_test = matrix_get_ele(&m, _x, y, z);
if (to_test) {
if (to_test->is_dead == true) {
to_test->num_neighbours++;
} else {
ptr->num_neighbours++;
}
} else {
matrix_insert(&m, _x, y, z, true, 1);
ptr = ((struct node*)da->data) + k;
}
_x = pos_mod(x - 1, SIZE);
to_test = matrix_get_ele(&m, _x, y, z);
if (to_test) {
if (to_test->is_dead == true) {
to_test->num_neighbours++;
} else {
ptr->num_neighbours++;
}
} else {
matrix_insert(&m, _x, y, z, true, 1);
ptr = ((struct node*)da->data) + k;
}
_y = pos_mod(y + 1, SIZE);
to_test = matrix_get_ele(&m, x, _y, z);
if (to_test) {
if (to_test->is_dead == true) {
to_test->num_neighbours++;
} else {
ptr->num_neighbours++;
}
} else {
matrix_insert(&m, x, _y, z, true, 1);
ptr = ((struct node*)da->data) + k;
}
_y = pos_mod(y - 1, SIZE);
to_test = matrix_get_ele(&m, x, _y, z);
if (to_test) {
if (to_test->is_dead == true) {
to_test->num_neighbours++;
} else {
ptr->num_neighbours++;
}
} else {
matrix_insert(&m, x, _y, z, true, 1);
ptr = ((struct node*)da->data) + k;
}
}
}
}
// printf("=============================================================\n");
// printf("==================== BEFORE DELETING ========================\n");
// printf("=============================================================\n");
// matrix_print(&m);
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
da = matrix_get(&m, i, j);
if (!da) {
continue;
}
// Iterate over every existing z for x and y
for (int k = (int) da->used - 1; k >= 0; k--) {
ptr = ((struct node*)da->data) + k;
if (ptr->is_dead) {
if (ptr->num_neighbours == 2 || ptr->num_neighbours == 3) {
ptr->is_dead = false;
} else {
matrix_remove(&m, i, j, ptr->z);
ptr = ((struct node*)da->data) + k;
}
} else {
if (ptr->num_neighbours < 2 || ptr->num_neighbours > 4) {
matrix_remove(&m, i, j, ptr->z);
ptr = ((struct node*)da->data) + k;
}
}
}
}
}
// printf("=============================================================\n");
// printf("==================== AFTER DELETING ========================\n");
// printf("=============================================================\n");
// matrix_print(&m);
}
end = omp_get_wtime();
process_time = end - start;
//-----------
//--- END ---
//-----------
// Output the result
// matrix_print(&m);
matrix_print_live(&m);
// Free all (uneccessary)
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
dynamic_array *da = matrix_get(&m, i, j);
if (!da) {
continue;
}
da_free(da);
free(da);
da = NULL;
}
}
free(m.data);
// Write the time log to a file
FILE* out_fp = fopen("time.log", "w");
char out_str[80];
sprintf(out_str, "OMP %s: \ninit_time: %lf \nproc_time: %lf\n", input_file, init_time, process_time);
fwrite(out_str, strlen(out_str), 1, out_fp);
}