-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.cpp
177 lines (156 loc) · 5.31 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
#include <iostream>
#include <thread>
#include <vector>
#include <typeinfo>
#include <string>
#include <cstdint>
#include <type_traits>
#include "queue_unsafe.h"
#include "queue_locked.h"
#include "queue_spsc.h"
#include "queue_mpmc.h"
#if defined(__GNUC__)
# include <memory>
# include <cxxabi.h> // abi::__cxa_demangle
#endif/*__GNUC__*/
#include "stopwatch.hpp"
template <typename T>
constexpr std::uint64_t calc(T n) {
std::uint64_t r = n;
return (r * (r - 1)) / 2;
}
template <typename T>
std::string type_name() {
#if defined(__GNUC__)
const char* typeid_name = typeid(T).name();
const char* real_name = abi::__cxa_demangle(typeid_name, nullptr, nullptr, nullptr);
std::unique_ptr<void, decltype(::free)*> guard { (void*)real_name, ::free };
if (real_name == nullptr) real_name = typeid_name;
return real_name;
#else
return typeid(T).name();
#endif/*__GNUC__*/
}
enum {
loop_count = 11531520,
rept_count = 1
};
template <int PushN, int PopN, template <typename> class Queue>
void benchmark() {
Queue<int> que;
capo::stopwatch<> sw { true };
int cnt = (loop_count / PushN);
std::thread push_trds[PushN];
for (int i = 0; i < PushN; ++i) {
(push_trds[i] = std::thread {[i, cnt, &que] {
for (int k = 0; k < rept_count; ++k) {
int beg = i * cnt;
for (int n = beg; n < (beg + cnt); ++n) {
while (!que.push(n)) {
std::this_thread::yield();
}
}
}
//std::cout << "end push: " << i << std::endl;
while (!que.push(-1)) {
std::this_thread::yield();
}
}}).detach();
}
std::uint64_t sum[PopN] {};
std::atomic<int> push_end { 0 };
std::thread pop_trds[PopN];
for (int i = 0; i < PopN; ++i) {
pop_trds[i] = std::thread {[i, &que, &sum, &push_end] {
decltype(que.pop()) tp;
while (push_end.load(std::memory_order_acquire) < PushN) {
while (std::get<1>(tp = que.pop())) {
if (std::get<0>(tp) < 0) {
int pop_count = push_end.fetch_add(1, std::memory_order_release) + 1;
//std::cout << "pop count: " << pop_count << std::endl;
if (pop_count >= PushN) {
que.quit();
return;
}
}
else sum[i] += std::get<0>(tp);
}
std::this_thread::yield();
}
}};
}
std::uint64_t ret = 0;
for (int i = 0; i < PopN; ++i) {
pop_trds[i].join();
ret += sum[i];
}
if ((calc(loop_count) * rept_count) != ret) {
std::cout << "fail... " << ret << std::endl;
}
auto t = sw.elapsed<std::chrono::milliseconds>();
std::cout << type_name<decltype(que)>() << " "
<< PushN << ":" << PopN << " - " << t << " ms" << std::endl;
}
template <int PushN, int PopN,
template <typename> class Q1,
template <typename> class Q2,
template <typename> class... Qs>
void benchmark() {
benchmark<PushN, PopN, Q1>();
benchmark<PushN, PopN, Q2, Qs...>();
}
template <typename F, std::size_t...I>
constexpr void static_for(std::index_sequence<I...>, F&& f) {
[[maybe_unused]] auto expand = { (f(std::integral_constant<size_t, I>{}), 0)... };
}
template <int PushN, int PopN, template <typename> class Queue>
void benchmark_batch() {
static_for(std::make_index_sequence<(std::max)(PushN, PopN)>{}, [](auto index) {
benchmark<(PushN <= 1 ? 1 : decltype(index)::value + 1),
(PopN <= 1 ? 1 : decltype(index)::value + 1), Queue>();
});
std::cout << std::endl;
}
template <int PushN, int PopN,
template <typename> class Q1,
template <typename> class Q2,
template <typename> class... Qs>
void benchmark_batch() {
benchmark_batch<PushN, PopN, Q1>();
benchmark_batch<PushN, PopN, Q2, Qs...>();
}
int main() {
// for (int i = 0; i < 100; ++i) {
// std::cout << i << std::endl;
benchmark<1, 1, lock::queue,
cond::queue,
mpmc::queue,
spsc::queue,
mpmc::qlock,
mpmc::qring,
spmc::qring,
spsc::qring,
mpmc::qring2>();
std::cout << std::endl;
benchmark_batch<1, 8, lock::queue,
cond::queue,
mpmc::queue,
mpmc::qlock,
mpmc::qring,
spmc::qring,
mpmc::qring2>();
benchmark_batch<8, 1, lock::queue,
cond::queue,
mpmc::queue,
mpmc::qlock,
mpmc::qring,
mpmc::qring2>();
benchmark_batch<8, 8, lock::queue,
cond::queue,
mpmc::queue,
mpmc::qlock,
mpmc::qring,
mpmc::qring2>();
// }
return 0;
}