-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_priv.c
124 lines (110 loc) · 3.17 KB
/
world_priv.c
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
#ifdef _OPENMP
#include <omp.h>
#endif
#include "world_priv.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
static size_t world_initial_population(const world_parameters_t *p)
{
return world_world_size(p) * p->populationPercent / 100;
}
size_t world_world_size(const world_parameters_t *p)
{
return p->worldWidth * p->worldHeight;
}
int world_init_common(world_t *world, const world_parameters_t *p)
{
if (!world) {
return -1;
}
const size_t world_size = world_world_size(p);
const size_t people_to_spawn = world_initial_population(p);
if (!world_size) {
return -1;
}
if (!(people_to_spawn >= p->initialImmune + p->initialInfected)) {
return -1;
}
world->grid = (state_t *)malloc(world_size * sizeof(*world->grid));
world->infectionDurationGrid = (uint8_t *)malloc(
world_size * sizeof(*world->infectionDurationGrid));
if (!world->grid || !world->infectionDurationGrid) {
return -1;
}
memset(world->grid, EMPTY, sizeof(*world->grid) * world_size);
memset(world->infectionDurationGrid, p->infectionDuration,
sizeof(*world->infectionDurationGrid) * world_size);
memcpy(&world->params, p, sizeof(*p));
srand(time(NULL));
#ifdef _OPENMP
const size_t max_threads = omp_get_max_threads();
const size_t people_to_infect_per_thread =
p->initialInfected / max_threads;
const size_t people_to_infect_on_last_thread =
p->initialInfected % max_threads;
const size_t people_to_immunize_per_thread =
p->initialImmune / max_threads;
const size_t people_to_immunize_on_last_thread =
p->initialImmune % max_threads;
const size_t people_to_spawn_per_thread = people_to_spawn / max_threads;
const size_t people_to_spawn_on_last_thread =
people_to_spawn % max_threads;
const size_t chunk_per_thread = world_size / max_threads;
#pragma omp parallel
{
const size_t start_index =
omp_get_thread_num() * chunk_per_thread;
const bool is_last_thread = omp_get_thread_num() ==
max_threads - 1;
const size_t people_to_spawn =
is_last_thread ? people_to_spawn_on_last_thread :
people_to_spawn_per_thread;
const size_t infected_people_to_spawn =
is_last_thread ? people_to_infect_on_last_thread :
people_to_infect_per_thread;
const size_t immune_people_to_spawn =
is_last_thread ? people_to_immunize_on_last_thread :
people_to_immunize_per_thread;
#else
const size_t infected_people_to_spawn = p->initialInfected;
const size_t immune_people_to_spawn = p->initialImmune;
#endif
size_t people = 0;
size_t people_infected = 0;
size_t people_immune = 0;
while (people < people_to_spawn) {
#ifdef _OPENMP
const size_t i =
start_index + (rand() % chunk_per_thread);
#else
const size_t i = rand() % world_size;
#endif
if (world->grid[i] != EMPTY) {
continue;
}
++people;
if (people_infected < infected_people_to_spawn) {
world->grid[i] = INFECTED;
++people_infected;
continue;
}
if (people_immune < immune_people_to_spawn) {
world->grid[i] = IMMUNE;
++people_immune;
continue;
}
world->grid[i] = HEALTHY;
}
#ifdef _OPENMP
}
#endif
return 0;
}
void world_destroy_common(world_t *w)
{
free(w->grid);
free(w->infectionDurationGrid);
}