-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
258 lines (220 loc) · 7.72 KB
/
Game.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
#include "Headers.hpp"
#include "Game.hpp"
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
Game::Game(game_params params){
m_gen_num = params.n_gen;
m_thread_num = params.n_thread;
interactive_on = params.interactive_on;
print_on = params.print_on;
jobs_num = 0;
pthread_mutex_init(&jobs_lock,NULL);
//pthread_mutex_init(&step_lock,NULL);
pthread_cond_init(&step_cond,NULL);
vector<string> lines = utils::read_lines(params.filename);
vector<string> temp_line;
total_rows_num = lines.size();
temp_line = utils::split(lines[0],' ');
total_cols_num = temp_line.size();
//effective threads number:
m_thread_num = (m_thread_num > total_rows_num) ? total_rows_num : m_thread_num;
//Initializing the vectors with '0':
//allocate with frame size + 2 (=false borders)
curr = new bool_mat(total_rows_num + 2);
next = new bool_mat(total_rows_num + 2);
for(uint i = 0 ; i < total_rows_num + 2; i++) {
for (uint j = 0; j < total_cols_num + 2; j++) {
(*curr)[i].push_back(false);
(*next)[i].push_back(false);
}
}
//Parse the board input:
for(uint i = 1 ; i < total_rows_num + 1 ; i++){
temp_line = utils::split(lines[i-1],' ');
for(uint j = 1; j < total_cols_num + 1 ; j++){
(*curr)[i][j] = (temp_line[j-1] == "1") ;
(*next)[i][j] = (temp_line[j-1] == "1") ;
}
}
}
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);
} // generation loop
print_board("Final Board");
_destroy_game();
}
void Game::_init_game() {
// Create game fields
// Testing of your implementation will presume all threads are started here
// Create threads:
for(uint i = 0; i < m_thread_num; i++){
m_threadpool.push_back(new Worker(i,this));
}
// Start the threads:
for(uint i = 0; i < m_thread_num; i++){
m_threadpool[i]->start();
}
}
void Game::_step(uint curr_gen) {
// Push jobs to queue:
//N jobs would be created by the producer and inserted to the queue
job_t* job;
jobs_num = m_thread_num ;
uint range = total_rows_num / m_thread_num;
uint remainder = total_rows_num % m_thread_num;
for(uint i = 1; i < m_thread_num ; i++){
job = new job_t{1 + range * (i - 1),
1 + range * (i) ,
1 , total_cols_num + 1};
jobs_q.push(job);
}
//Last one gets the remainder:
job = new job_t{1 + range * (m_thread_num - 1),
1 + range * (m_thread_num) + remainder ,
1 , total_cols_num + 1};
jobs_q.push(job);
// Wait for the workers to finish calculating
pthread_mutex_lock(&jobs_lock);
while(jobs_num > 0){
pthread_cond_wait(&step_cond,&jobs_lock);
}
pthread_mutex_unlock(&jobs_lock);
// Swap pointers between current and next field
bool_mat* temp = curr;
curr = next;
next = temp;
}
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
//Inject poison to kill the threads:
job_t* poison_pill;
for(uint i = 0; i < m_threadpool.size(); i++){
poison_pill = new job_t{0,0,0,0};
jobs_q.push(poison_pill);
}
//Wait for them to die
for(uint i = 0; i < m_threadpool.size(); i++){
m_threadpool[i]->join();
}
//Free the memory
for(uint i = 0; i < m_threadpool.size(); i++){
delete m_threadpool[i];
}
delete curr;
delete next;
pthread_mutex_destroy(&jobs_lock);
//pthread_mutex_destroy(&step_lock);
pthread_cond_destroy(&step_cond);
}
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
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 board
cout << u8"╔" << string(u8"═") * (total_cols_num) << u8"╗" << endl;
for (uint i = 1; i < total_rows_num + 1; ++i) {
cout << u8"║";
for (uint j = 1; j < total_cols_num + 1; ++j) {
cout << ((*curr)[i][j] ? u8"█" : u8"░");
}
cout << u8"║" << endl;
}
cout << u8"╚" << string(u8"═") * (total_cols_num)<< u8"╝" << endl;
// Display for GEN_SLEEP_USEC micro-seconds on screen
if(interactive_on)
usleep(GEN_SLEEP_USEC);
}
}
/*
* Thread's Workload Function
* */
void Game::Worker::thread_workload(){
while(true) {
//Start polling the queue:
job_t *job = this->game_ptr->jobs_q.pop();
//Check for poison:
if(job->start_col + job->finish_col == 0) {
delete job;
return;//eat the poison and die
}
auto tile_start = std::chrono::system_clock::now();
//Job acquired. now start working you lazy worker! (ilya's side note: LOL)
//=============:WORK:============//
int alives = 0;
//Loop over the given cells:
for (uint row = job->start_row; row < job->finish_row; row++) {
for (uint col = job->start_col; col < job->finish_col; col++) {
//Check for alive neighbors:
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if ((*this->game_ptr->curr)[row + i][col + j])
alives++;
}
}
if ((*this->game_ptr->curr)[row][col]) alives--;
//===========Rules:==========//
(*this->game_ptr->next)[row][col] = false; //KILL
if ((*this->game_ptr->curr)[row][col]) {
if (alives == 3 || alives == 2)
(*this->game_ptr->next)[row][col] = true; //SURVIVE
} else {
if (alives == 3)
(*this->game_ptr->next)[row][col] = true; //BIRTH
}
//===========::::::===========//
alives = 0;
}
}
//==========:WORK's DONE:=========//
auto tile_done = std::chrono::system_clock::now();
//Now to the next job!
pthread_mutex_lock(&this->game_ptr->jobs_lock);
this->game_ptr->jobs_num--;
this->game_ptr->m_tile_hist.push_back(
(float) std::chrono::duration_cast<std::chrono::microseconds>(
tile_done - tile_start).count());
if(this->game_ptr->jobs_num <= 0){
pthread_cond_signal(&this->game_ptr->step_cond);
}
pthread_mutex_unlock(&this->game_ptr->jobs_lock);
delete job;
}
}
/* Function sketch to use for printing the board. You will need to decide its placement and how exactly
to bring in the field's parameters.
cout << u8"╔" << string(u8"═") * field_width << u8"╗" << endl;
for (uint i = 0; i < field_height ++i) {
cout << u8"║";
for (uint j = 0; j < field_width; ++j) {
cout << (field[i][j] ? u8"█" : u8"░");
}
cout << u8"║" << endl;
}
cout << u8"╚" << string(u8"═") * field_width << u8"╝" << endl;
*/
/* Getters */
const vector<float> Game::gen_hist() const {
return m_gen_hist;
}
const vector<float> Game::tile_hist() const {
return m_tile_hist;
}
uint Game::thread_num() const {
return m_thread_num;
}