-
Notifications
You must be signed in to change notification settings - Fork 3
/
SudokuGenerator.cpp
159 lines (121 loc) · 5.33 KB
/
SudokuGenerator.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
#include "SudokuGenerator.hpp"
void generateSudokuAndInsertTask_use_ATOMIC(DatabaseConnection DB,atomic<int> &successTask,atomic<int> &failTask, int number_of_sudoku){
for (int i = 0; i<number_of_sudoku;i++){
Sudoku sudoku;
sudoku.initialize();
DB.insertData("INSERT INTO Sudoku (SudokuValue) Values ('"+sudoku.getFlattenBoard()+"');")==0? successTask.fetch_add(1):failTask.fetch_add(1); // uncomment the deleteData above to avoid duplicates
}
}
void generateSudokuAndInsertTask_use_MUTEX(DatabaseConnection DB, std::mutex &successTask_mutex, std::mutex &failTask_mutex,int &successTask, int &failTask, int number_of_sudoku){
for (int i = 0; i<number_of_sudoku;i++){
Sudoku sudoku;
sudoku.initialize();
if (DB.insertData("INSERT INTO Sudoku (SudokuValue) Values ('"+sudoku.getFlattenBoard()+"');") == 0){
successTask_mutex.lock();
successTask++;
successTask_mutex.unlock();
} else {
failTask_mutex.lock();
failTask++;
failTask_mutex.unlock();
}
}
}
void generateSudokuAndInsertTask_use_INT(int x,DatabaseConnection DB,std::vector<int>&successTask,std::vector<int> &failTask, int number_of_sudoku){
for (int i = 0; i<number_of_sudoku;i++){
Sudoku sudoku;
sudoku.initialize();
DB.insertData("INSERT INTO Sudoku (SudokuValue) Values ('"+sudoku.getFlattenBoard()+"');")==0? successTask[x]++:failTask[x]++; // uncomment the deleteData above to avoid duplicates
}
}
void single_thread_sudoku_generator()
{
//connect to database
const char* dir = R"(../Database/SudokuDatabase.db)";
auto DB = DatabaseConnection(dir);
DB.createTableIfNotExist();
int successTask = 0;
int failTask = 0;
//generate and insert 80 sudoku
for (int i = 0; i<80;i++){
// generate a sudoku
Sudoku sudoku;
sudoku.initialize();
DB.insertData("INSERT INTO Sudoku (SudokuValue) Values ('"+sudoku.getFlattenBoard()+"');")==0?successTask++:failTask++; // uncomment the deleteData above to avoid duplicates
}
//disconnect database
DB.closeConnection();
}
void multi_thread_sudoku_generator_using_ATOMIC()
{
int NUMBER_OF_THREADS = std::thread::hardware_concurrency();
//connect to database
const char* dir = R"(../Database/SudokuDatabase.db)";
auto DB = DatabaseConnection(dir);
DB.createTableIfNotExist();
//create vector to hold thread
vector<std::thread> threadList;
//create thread and push to vector
//generate and insert 25 sudoku each thread, 25 * 4 = 100 sudoku in total
//create atomic int fail and success task to hold total number of fail and success task
std::atomic_int successTask = {0};
std::atomic_int failTask = {0};
for (int i = 0;i<NUMBER_OF_THREADS;i++){
threadList.push_back(std::thread(generateSudokuAndInsertTask_use_ATOMIC,DB, std::ref(successTask), std::ref(failTask), 80/NUMBER_OF_THREADS));
}
for (auto& th : threadList) {
th.join();
}
//disconnect database
DB.closeConnection();
}
void multi_thread_sudoku_generator_using_MUTEX()
{
int NUMBER_OF_THREADS = std::thread::hardware_concurrency();
//connect to database
const char* dir = R"(../Database/SudokuDatabase.db)";
auto DB = DatabaseConnection(dir);
DB.createTableIfNotExist();
//create vector to hold thread
vector<std::thread> threadList;
//create thread and push to vector
//generate and insert 25 sudoku each thread, 25 * 4 = 100 sudoku in total
//create atomic int fail and success task to hold total number of fail and success task
int successTask=0;
int failTask=0;
std::mutex successTask_mutex;
std::mutex failTask_mutex;
for (int i = 0;i<NUMBER_OF_THREADS;i++){
threadList.push_back(std::thread(generateSudokuAndInsertTask_use_MUTEX,DB, std::ref(successTask_mutex), std::ref(failTask_mutex), std::ref(successTask), std::ref(failTask), 80/NUMBER_OF_THREADS));
}
for (auto& th : threadList) {
th.join();
}
//disconnect database
DB.closeConnection();
}
void multi_thread_sudoku_generator_using_INT()
{
int NUMBER_OF_THREADS = std::thread::hardware_concurrency();
//connect to database
const char* dir = R"(../Database/SudokuDatabase.db)";
auto DB = DatabaseConnection(dir);
DB.createTableIfNotExist();
//create vector to hold thread
vector<std::thread> threadList;
//create thread and push to vector
//generate and insert 25 sudoku each thread, 25 * 4 = 100 sudoku in total
//create atomic int fail and success task to hold total number of fail and success task
std::vector<int> successTaskVector(NUMBER_OF_THREADS,0);
std::vector<int> failTaskVector(NUMBER_OF_THREADS,0);
for (int i = 0;i<NUMBER_OF_THREADS;i++){
threadList.push_back(std::thread(generateSudokuAndInsertTask_use_INT,i,DB, std::ref(successTaskVector), std::ref(failTaskVector), 80/NUMBER_OF_THREADS));
}
for (auto& th : threadList) {
th.join();
}
int successTask= std::accumulate(successTaskVector.begin(),successTaskVector.end(),0);
int failTask= std::accumulate(failTaskVector.begin(),failTaskVector.end(),0);
//disconnect database
DB.closeConnection();
}