-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.hpp
98 lines (75 loc) · 3.15 KB
/
Game.hpp
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
#ifndef __GAMERUN_H
#define __GAMERUN_H
#include "Headers.hpp"
#include "Thread.hpp"
#include "PCQueue.hpp"
#include "utils.hpp"
/*--------------------------------------------------------------------------------
Auxiliary Structures
--------------------------------------------------------------------------------*/
struct game_params {
// All here are derived from ARGV, the program's input parameters.
uint n_gen;
uint n_thread;
string filename;
bool interactive_on;
bool print_on;
};
/* Every thread needs to know his job definition */
struct job_t {
uint start_row;
uint finish_row;
uint start_col;
uint finish_col;
};
/*--------------------------------------------------------------------------------
Class Declaration
--------------------------------------------------------------------------------*/
class Game {
public:
Game(game_params);
~Game() = default; //TODO: join all threads. Insert a sign to the queue that lets the threads know they should stop
void run(); // Runs the game
const vector<float> gen_hist() const; // Returns the generation timing histogram
const vector<float> tile_hist() const; // Returns the tile timing histogram
uint thread_num() const; //Returns the effective number of running threads = min(thread_num, field_height)
protected: // All members here are protected, instead of private for testing purposes
// See Game.cpp for details on these three functions
void _init_game();
void _step(uint curr_gen);
void _destroy_game();
inline void print_board(const char* header);
uint m_gen_num; // The number of generations to run
uint m_thread_num; // Effective number of threads = min(thread_num, field_height)
vector<float> m_tile_hist; /// Shared Timing history for tiles: First m_thread_num cells are the calculation durations for tiles in generation 1 and so on.
// Note: In your implementation, all m_thread_num threads must write to this structure.
vector<float> m_gen_hist; // Timing history for generations: x=m_gen_hist[t] iff generation t was calculated in x microseconds
vector<Thread*> m_threadpool; // A storage container for your threads. This acts as the threadpool.
bool interactive_on; // Controls interactive mode - that means, prints the board as an animation instead of a simple dump to STDOUT
bool print_on; // Allows the printing of the board. Turn this off when you are checking performance (Dry 3, last question)
// TODO: Add in your variables and synchronization primitives
uint total_rows_num;
uint total_cols_num;
bool_mat *curr;
bool_mat *next;
pthread_mutex_t jobs_lock;
//pthread_mutex_t step_lock;
pthread_cond_t step_cond;
PCQueue<job_t*> jobs_q;
int jobs_num;
private:
/*
* Worker class - extends the abstract class Thread
* and implements the thread_workload function.
* */
class Worker : public Thread {
private:
Game* game_ptr;
public:
Worker(uint thread_id, Game* game):
Thread::Thread(thread_id), game_ptr(game){};
protected:
void thread_workload() override;
};
};
#endif