-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
287 lines (245 loc) · 8.07 KB
/
Game.cpp
File metadata and controls
287 lines (245 loc) · 8.07 KB
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
#include "utils.hpp"
#include "Game.hpp"
#define NEIGH_IDX(line, col) \
{ \
{(line) , (col + 1) }, \
{(line) , (col - 1) }, \
{(line + 1) , (col + 1) }, \
{(line + 1) , (col) }, \
{(line + 1) , (col - 1) }, \
{(line - 1) , (col + 1) }, \
{(line - 1) , (col) }, \
{(line - 1) , (col - 1) } \
}
#define INIT_XY(neigh, i, x, y, h, w) \
{ \
x = (neigh[i][0] < 0) ? (h - 1) : (neigh[i][0] % h), \
y = (neigh[i][1] < 0) ? (w - 1) : (neigh[i][1] % w); \
}
static const char *colors[7] = {BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN};
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
static uint dominant(vector<int> hist){
int max = 0, max_idx = 0;
for(int i = 1; i <= 7; i++){
if(max < (hist[i] * i)) {
max = hist[i] * i;
max_idx = i;
}
}
return static_cast<uint>(max_idx);
}
static void eval_cell(Game * game, int line_idx, int col_idx){
vector<int> neigh_histo(8,0);
field crr = game->get_crr_fld(), nxt = game->get_nxt_fld();
int x, y, w = game->width, h = game->height, alive = 0,
neigh[8][2] = NEIGH_IDX(line_idx, col_idx);
for(int i = 0; i < 8; i++){
if(neigh[i][0] >=0 && neigh[i][0]<h && neigh[i][1] >= 0 && neigh[i][1] < w) {
x = neigh[i][0];
y = neigh[i][1];
if ((*crr)[x][y] > 0) {
neigh_histo[(*crr)[x][y]]++;
alive++;
}
}
}
if(alive < 4 && alive > 1){
if((*crr)[line_idx][col_idx] > 0){ //Was alive
(*nxt)[line_idx][col_idx] = (*crr)[line_idx][col_idx];
}else if(alive == 3){ //Was dead
/*if(line_idx == 7 && col_idx == 10) {
for(auto x : neigh_histo) cerr << x;
cerr << endl << dominant(neigh_histo) << endl;
}*/
(*nxt)[line_idx][col_idx] = dominant(neigh_histo);
}else{
(*nxt)[line_idx][col_idx] = 0;
}
}else{
(*nxt)[line_idx][col_idx] = 0;
}
}
static void eval_cell_color(Game * game, int line_idx, int col_idx){
field crr = game->get_crr_fld();
field nxt = game->get_nxt_fld();
if((*nxt)[line_idx][col_idx] == 0){
(*crr)[line_idx][col_idx] = 0;
return;
}
int x, y, w = game->width, h = game->height, alive = 1,
neigh[8][2] = NEIGH_IDX(line_idx, col_idx);
uint sum = (*nxt)[line_idx][col_idx];
for(int i = 0; i < 8; i++){
if(neigh[i][0] >= 0 && neigh[i][0]<h && neigh[i][1] >= 0 && neigh[i][1] < w){
x = neigh[i][0];
y = neigh[i][1];
if((*nxt)[x][y] > 0){
sum += (*nxt)[x][y];
alive++;
}
}
}
(*crr)[line_idx][col_idx] = std::round(((double) sum) / ((double) alive));
}
static void set_start_end_bound(uint * start, uint * end, uint tile_id, Game * game){
uint offset = floor(game->height / game->thread_num());
(*start) = offset * tile_id;
(*end) = (tile_id == game->thread_num() - 1) ? game->height - 1 : offset * (tile_id + 1);
}
static void task_phase1(Game * game, uint tile_id, uint start, uint end){
for(uint line = start; line <= end; line++){
for(uint col = 0; col < game->width; col++){
eval_cell(game, static_cast<int>(line), static_cast<int>(col));
}
}
}
static void task_phase2(Game * game, uint tile_id, uint start, uint end){
for(uint line = start; line <= end; line++){
for(uint col = 0; col < game->width; col++){
eval_cell_color(game, static_cast<int>(line), static_cast<int>(col));
}
}
}
static void next_gen(Game * game, uint tile_id){
uint start, end;
set_start_end_bound(&start, &end, tile_id, game);
task_phase1(game,tile_id, start, end);
//Here will be block
game->m_barrier1.block();
task_phase2(game,tile_id, start, end);
}
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
Game::Game(game_params parms):
parms(parms),
m_gen_num(parms.n_gen),
interactive_on(parms.interactive_on),
print_on(parms.print_on),
m_gen(0),
m_tile_hist_sem(1){}
const vector<double> Game::gen_hist() const { return m_gen_hist; }
const vector<double> Game::tile_hist() const { return m_tile_hist; }
void Game::push_tile_time(float time){
m_tile_hist_sem .down();
m_tile_hist.push_back(time);
m_tile_hist_sem.up();
}
uint Game::thread_num() const { return m_thread_num; }
field Game::get_crr_fld() { return crr_fld; }
field Game::get_nxt_fld() { return nxt_fld; }
uint Game::get_crr_gen() { return m_gen; }
uint Game::get_gen_num() { return m_gen_num; }
void Game::run() {
_init_game(); // Starts the threads and all other variables you need
print_board("Initial Board");
for (uint i = 0; i < m_gen_num; ++i) {
auto gen_start = std::chrono::system_clock::now();
_step(i); // Iterates a single generation
auto gen_end = std::chrono::system_clock::now();
m_gen_hist.push_back((float)std::chrono::duration_cast<std::chrono::microseconds>(gen_end - gen_start).count());
print_board(NULL);
m_gen++;
m_barrier1 = Barrier(m_thread_num);
//m_barrier2 = Barrier(m_thread_num+1);
m_sem = Semaphore(-m_thread_num);
} // generation loop
print_board("Final Board");
_destroy_game();
}
void Game::_init_game() {
//Read file
vector<vector<string>> str_field;
vector<string> tmp = utils::read_lines(parms.filename);
for(auto s : tmp){
str_field.push_back(utils::split(s,' '));
}
height = str_field.size();
width = str_field[0].size();
//update m_thread_num
m_thread_num = (height < parms.n_thread) ? height : parms.n_thread;
// Create threads
m_barrier1 = Barrier(m_thread_num);
m_barrier2 = Barrier(m_thread_num+1);
m_sem = Semaphore(-m_thread_num);
m_threadpool = vector<GOLThread*>(m_thread_num, nullptr);
for(uint i = 0; i < m_thread_num; i++){
m_threadpool[i] = new GOLThread(i, this);
}
// Create game fields
crr_fld = new vector<vector<uint>>(height, vector<uint>(width));
nxt_fld = new vector<vector<uint>>(height, vector<uint>(width));
uint count_lines = 0, count_chars;
for(auto str_line : str_field){
count_chars = 0;
for(auto c : str_line){
(*crr_fld)[count_lines][count_chars++] = ((uint) c[0]) - 48;
}count_lines++;
}
// Start the threads
for(auto thread : m_threadpool){
thread->start();
}
// Testing of your implementation will presume all threads are started here
}
void Game::_step(uint curr_gen) {
// Push phase 1 jobs to queue
vector<task_struct> tasks;
for(uint i = 0; i < m_thread_num; i++){
tasks.push_back({i, next_gen});
//t_queue.push({i, next_gen});
}
t_queue.multi_push(tasks);
// Wait for the workers to finish calculating
//m_barrier2.block();
m_sem.up();
m_sem.down();
}
void Game::_destroy_game(){
// Destroys board and frees all threads and resources
// Not implemented in the Game's destructor for testing purposes.
// Testing of your implementation will presume all threads are joined here
for(auto thread : m_threadpool){
thread->join();
}
}
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
static void print_the_board1(field f, uint field_height, uint field_width){
cout << u8"╔" << string(u8"═") * field_width << u8"╗" << endl;
for (auto line : (*f)) {
cout << u8"║";
for (auto x : line) {
if (x > 0){
cout << colors[x % 7] << u8"█" << RESET;
}
else
cout << u8"░";
}
cout << u8"║" << endl;
}
cout << u8"╚" << string(u8"═") * field_width << u8"╝" << endl;
}
/*
static void print_the_board2(field f, uint field_height, uint field_width){
for (auto line : (*f)) {
for (auto x : line) {
cout << "(" << x << ")";
}
cout << endl;
}
}*/
inline void Game::print_board(const char* header) {
if(print_on){
// Clear the screen, to create a running animation
if(interactive_on)
system("clear");
// Print small header if needed
if (header != NULL)
cout << "<------------" << header << "------------>" << endl;
print_the_board1(crr_fld, (*crr_fld).size(), (*crr_fld)[0].size());
// Display for GEN_SLEEP_USEC micro-seconds on screen
if(interactive_on)
usleep(GEN_SLEEP_USEC);
}
}