-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
228 lines (193 loc) · 6.25 KB
/
main.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
//
// Created by valco1994 on 13.02.20.
//
#include <gtest/gtest.h>
#include <cmath>
#include <random>
#include "thread_pool.h"
#include "timer.h"
using namespace thread_pool;
int lis(std::vector<int> const& a) {
size_t n = a.size();
std::vector<int> d(n, 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (a[j] < a[i])
d[i] = std::max(d[i], d[j] + 1);
}
}
int ans = d[0];
for (int i = 1; i < n; i++) {
ans = std::max(ans, d[i]);
}
return ans;
}
class Callable {
public:
Callable() {
std::cout << "Callable constructor\n";
}
Callable(Callable const &other) {
std::cout << "Callable copy constructor\n";
}
Callable(Callable &&other) {
std::cout << "Callable move constructor\n";
}
~Callable() {
std::cout << "Callable destructor\n";
}
void operator ()() & {
std::cout << "Callable called by lvalue\n";
}
void operator ()() && {
std::cout << "Callable called by rvalue\n";
}
};
void test_function_by_value(Callable c) {}
void test_function_by_lvalue(Callable &c) {}
void test_function_by_rvalue(Callable &&c) {}
class ThreadPoolTests: public ::testing::Test {};
TEST_F(ThreadPoolTests, SimpleFunctions) {
Timer timer;
std::future<double> future1;
std::future<int> future2;
std::future<int> future3;
std::future<void> future4;
timer.start();
{
ThreadPool pool(2);
future1 = pool.execute([](int a, int b) { return sqrt(a*a + b*b); }, 4, 3);
future2 = pool.execute([](int a, int b) { return static_cast<int>(log2(a*a - b)); }, 9, 17);
future3 = pool.execute([]() { return 5; });
future4 = pool.execute([]() { std::cout << "Void function is being processed!\n"; });
pool.poll();
EXPECT_EQ(future2.get(), 6);
EXPECT_EQ(future3.get(), 5);
}
EXPECT_EQ(future1.get(), 5);
future4.wait();
auto elapsed = timer.stop();
std::cout << elapsed.count() << " us elapsed" << std::endl;
}
TEST_F(ThreadPoolTests, SlightlyMoreDifficult) {
Timer timer;
std::vector<int> v { 13, 22, 88, 323, 324, 1, 42, -4, 3, 89, 123, 3333, 8943, 999 };
std::future<int> future;
{
ThreadPool pool(2);
printf("Is task queue empty? %s\n", pool.empty() ? "Yes" : "No");
pool.execute(lis, std::cref(v));
pool.execute(lis, std::cref(v));
pool.execute(lis, std::cref(v));
pool.execute(lis, std::cref(v));
pool.poll();
printf("Is task queue empty? %s\n", pool.empty() ? "Yes" : "No");
pool.poll();
}
auto elapsed = timer.stop();
std::cout << elapsed.count() << " us elapsed" << std::endl;
}
TEST_F(ThreadPoolTests, GeneratingFunctions) {
Timer timer;
timer.start();
{
ThreadPool pool(2);
auto future1 = pool.execute([&pool](int a, int b) {
auto future2 = pool.execute([&pool](int a, int b) {
auto future3 = pool.execute([&pool]() {
return 5;
});
//future3.wait(); // If you uncomment this line, then program will freeze, because all
// threads from the pool will be blocked.
return static_cast<int>(log2(a*a - b));
}, 9, 17);
future2.wait();
return sqrt(a*a + b*b);
}, 4, 3);
future1.wait();
}
auto elapsed = timer.stop();
std::cout << elapsed.count() << " us elapsed" << std::endl;
}
TEST_F(ThreadPoolTests, GeneratingFunctionsCorrected) {
Timer timer;
timer.start();
{
std::vector<std::future<int>> futures;
ThreadPool pool(2);
auto future1 = pool.execute([&pool](int a, int b) {
auto future2 = pool.execute([&pool](int a, int b) {
auto future3 = pool.execute([&pool]() {
return 5;
});
pool.poll();
future3.wait();
return static_cast<int>(log2(a*a - b));
}, 9, 17);
pool.poll();
future2.wait();
return sqrt(a*a + b*b);
}, 4, 3);
pool.poll();
future1.wait();
}
auto elapsed = timer.stop();
std::cout << elapsed.count() << " us elapsed" << std::endl;
}
TEST_F(ThreadPoolTests, LoadTests) {
std::random_device r;
// Choose a random mean between 1 and 6
std::default_random_engine engine(r());
std::mt19937 gen(r());
std::poisson_distribution<int> dist(10);
std::vector<int> v;
size_t N = 500; //10000;
v.reserve(N);
for (size_t i = 0; i < N; ++i) {
v.push_back(dist(gen));
}
Timer timer;
std::cout << "Count of tasks: 100, pool size is being varied" << std::endl;
for (size_t pool_size = 2; pool_size < 21; ++pool_size) {
timer.start();
{
ThreadPool pool(pool_size);
for (size_t i = 0; i < 100; ++i) {
pool.execute(lis, std::cref(v));
}
}
auto elapsed = timer.stop();
std::cout << "Pool size is " << pool_size << ", count of tasks is 100, " <<
elapsed.count() << " us elapsed" << std::endl;
}
std::cout << "\nCount of tasks is being varied, pool size is 4" << std::endl;
for (size_t n_tasks = 10; n_tasks < 201; n_tasks += 10) {
timer.start();
{
ThreadPool pool(4);
for (size_t i = 0; i < n_tasks; ++i) {
pool.execute(lis, std::cref(v));
}
}
auto elapsed = timer.stop();
std::cout << "Pool size is 4, count of tasks is " << n_tasks << ", " <<
elapsed.count() << " us elapsed" << std::endl;
}
}
TEST_F(ThreadPoolTests, too_much_threads) {
EXPECT_THROW(ThreadPool(100000), std::system_error);
}
TEST_F(ThreadPoolTests, callable_lvalue) {
ThreadPool pool(2);
Callable c;
pool.execute([](Callable){}, c);
}
TEST_F(ThreadPoolTests, callable_rvalue) {
ThreadPool pool(2);
pool.execute([](Callable){}, Callable());
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
auto code = RUN_ALL_TESTS();
return code;
}