forked from Cryolite/kanachan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.cpp
326 lines (291 loc) · 11 KB
/
simulator.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "simulation/simulator.hpp"
#include "simulation/game.hpp"
#include "simulation/paishan.hpp"
#include "simulation/decision_maker.hpp"
#include "simulation/utility.hpp"
#include "simulation/gil.hpp"
#include "common/thread.hpp"
#include "common/assert.hpp"
#include "common/throw.hpp"
#include <boost/python/errors.hpp>
#include <boost/python/dict.hpp>
#include <boost/python/list.hpp>
#include <boost/python/object.hpp>
#include <mutex>
#include <thread>
#include <stop_token>
#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
#include <array>
#include <functional>
#include <utility>
#include <memory>
#include <stdexcept>
#include <limits>
#include <cmath>
#include <cstdint>
#include <cstddef>
namespace{
using std::placeholders::_1;
namespace python = boost::python;
} // namespace `anonymous`
namespace Kanachan{
class Simulator::Impl_
{
private:
using Seat_ = std::pair<std::uint_fast8_t, std::shared_ptr<Kanachan::DecisionMaker>>;
using Seats_ = std::array<Seat_, 4u>;
public:
Impl_(
std::string const &device, python::object dtype,
std::uint_fast8_t baseline_grade, python::object baseline_model,
std::uint_fast8_t proposed_grade, python::object proposed_model,
unsigned long simulation_mode, std::size_t num_simulation_sets,
std::size_t batch_size, std::size_t concurrency);
Impl_(Impl_ const &) = delete;
Impl_ &operator=(Impl_ const &) = delete;
private:
void threadMain_(std::stop_token stop_token);
public:
python::list run();
private:
bool dong_feng_zhan_;
std::uint_fast8_t room_;
std::shared_ptr<Kanachan::DecisionMaker> p_baseline_decision_maker_;
std::shared_ptr<Kanachan::DecisionMaker> p_proposed_decision_maker_;
std::vector<std::jthread> threads_;
std::vector<std::vector<std::uint_least32_t>> seeds_;
std::vector<Seats_> seats_list_;
std::vector<python::dict> results_;
std::size_t num_alive_threads_;
std::mutex mtx_;
}; // class Simulator::Impl_
Simulator::Simulator(
std::string const &device, python::object dtype,
std::uint_fast8_t baseline_grade, python::object baseline_model,
std::uint_fast8_t proposed_grade, python::object proposed_model,
unsigned long simulation_mode, std::size_t num_simulation_sets,
std::size_t batch_size, std::size_t concurrency)
: p_impl_(std::make_shared<Impl_>(
device, dtype, baseline_grade, baseline_model, proposed_grade, proposed_model,
simulation_mode, num_simulation_sets, batch_size, concurrency))
{}
python::list Simulator::run()
{
KANACHAN_ASSERT((!!p_impl_));
return p_impl_->run();
}
Simulator::Impl_::Impl_(
std::string const &device, python::object dtype,
std::uint_fast8_t baseline_grade, python::object baseline_model,
std::uint_fast8_t proposed_grade, python::object proposed_model,
unsigned long simulation_mode, std::size_t num_simulation_sets,
std::size_t batch_size, std::size_t concurrency)
: dong_feng_zhan_(simulation_mode & 2u)
, room_(std::numeric_limits<std::uint_fast8_t>::max())
, p_baseline_decision_maker_(
std::make_shared<Kanachan::DecisionMaker>(device, dtype, baseline_model, batch_size))
, p_proposed_decision_maker_(
std::make_shared<Kanachan::DecisionMaker>(device, dtype, proposed_model, batch_size))
, threads_()
, seeds_()
, seats_list_()
, results_()
, num_alive_threads_(concurrency)
, mtx_()
{
if (num_simulation_sets == 0u) {
KANACHAN_THROW<std::invalid_argument>("`num_simulation_sets` must be a positive integer.");
}
bool const no_duplicate = (simulation_mode & 1u);
bool const one_versus_three = (simulation_mode & 4u);
if ((simulation_mode & 8u) != 0u) {
room_ = 0u;
}
if ((simulation_mode & 16u) != 0u) {
if (room_ != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>("simulation_mode: Multiple rooms specified.");
}
room_ = 1u;
}
if ((simulation_mode & 32u) != 0u) {
if (room_ != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>("simulation_mode: Multiple rooms specified.");
}
room_ = 2u;
}
if ((simulation_mode & 64u) != 0u) {
if (room_ != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>("simulation_mode: Multiple rooms specified.");
}
room_ = 3u;
}
if ((simulation_mode & 128u) != 0u) {
if (room_ != std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>("simulation_mode: Multiple rooms specified.");
}
room_ = 4u;
}
if (room_ == std::numeric_limits<std::uint_fast8_t>::max()) {
KANACHAN_THROW<std::invalid_argument>("simulation_mode: Room not specified.");
}
if (baseline_grade < 0 || 16 <= baseline_grade) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< baseline_grade << ": An invalid baseline grade.";
}
if (proposed_grade < 0 || 16 <= proposed_grade) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< proposed_grade << ": An invalid proposed grade.";
}
if (concurrency == 0u) {
KANACHAN_THROW<std::invalid_argument>("`concurrency` must be a positive integer.");
}
if (concurrency < batch_size * 2u - 1u) {
KANACHAN_THROW<std::invalid_argument>(_1)
<< "`concurrency` (= " << concurrency
<< ") must be at least twice as large as `batch_size` (= " << batch_size << ").";
}
{
using Seed = std::vector<std::uint_least32_t>;
using Flags = std::array<bool, 4u>;
auto const append = [&](Seed const &seed, Flags const flags) {
seeds_.push_back(seed);
Seat_ const baseline_seat(baseline_grade, p_baseline_decision_maker_);
Seat_ const proposed_seat(proposed_grade, p_proposed_decision_maker_);
Seats_ seats = {
flags[0u] ? proposed_seat : baseline_seat,
flags[1u] ? proposed_seat : baseline_seat,
flags[2u] ? proposed_seat : baseline_seat,
flags[3u] ? proposed_seat : baseline_seat
};
seats_list_.push_back(seats);
};
if (no_duplicate) {
std::vector<std::uint_least32_t> seed = Kanachan::getRandomSeed();
std::seed_seq ss(seed.cbegin(), seed.cend());
std::mt19937 urng(ss);
for (std::size_t i = 0u; i < num_simulation_sets; ++i) {
std::array<bool, 4u> flags = { false, false, !one_versus_three, true };
std::shuffle(flags.begin(), flags.end(), urng);
append(Kanachan::getRandomSeed(), flags);
}
}
else {
for (std::size_t i = 0u; i < num_simulation_sets; ++i) {
if (one_versus_three) {
Seed seed = Kanachan::getRandomSeed();
append(seed, { false, false, false, true });
append(seed, { false, false, true, false });
append(seed, { false, true, false, false });
append(seed, { true, false, false, false });
}
else {
Seed seed = Kanachan::getRandomSeed();
append(seed, { false, false, true, true });
append(seed, { false, true, false, true });
append(seed, { false, true, true, false });
append(seed, { true, false, false, true });
append(seed, { true, false, true, false });
append(seed, { true, true, false, false });
}
}
}
}
for (std::size_t i = 0u; i < concurrency; ++i) {
threads_.emplace_back(&Impl_::threadMain_, this);
}
{
Kanachan::GIL::RecursiveRelease gil_release;
for (std::jthread &thread : threads_) {
thread.join();
}
p_baseline_decision_maker_->join();
p_proposed_decision_maker_->join();
}
p_baseline_decision_maker_.reset();
p_proposed_decision_maker_.reset();
}
void Simulator::Impl_::threadMain_(std::stop_token stop_token)
try {
for (;;) {
std::vector<std::uint_least32_t> seed;
Seats_ seats;
{
std::unique_lock lock(mtx_);
if (seeds_.empty()) {
std::size_t const num_alive_threads = --num_alive_threads_;
lock.unlock();
p_baseline_decision_maker_->shrinkBatchSizeToFitNumThreads(num_alive_threads);
p_proposed_decision_maker_->shrinkBatchSizeToFitNumThreads(num_alive_threads);
return;
}
KANACHAN_ASSERT((!seats_list_.empty()));
seed = std::move(seeds_.back());
seeds_.pop_back();
seats = std::move(seats_list_.back());
seats_list_.pop_back();
}
python::dict result = [&]() {
std::vector<Kanachan::Paishan> dummy_paishan_list;
python::dict result = Kanachan::simulateGame(
seed, room_, dong_feng_zhan_, seats, dummy_paishan_list, stop_token);
{
Kanachan::GIL::RecursiveLock gil_lock;
result["proposed"] = python::list();
result["proposed"].attr("append")(
seats[0u].second.get() == p_proposed_decision_maker_.get() ? 1 : 0);
result["proposed"].attr("append")(
seats[1u].second.get() == p_proposed_decision_maker_.get() ? 1 : 0);
result["proposed"].attr("append")(
seats[2u].second.get() == p_proposed_decision_maker_.get() ? 1 : 0);
result["proposed"].attr("append")(
seats[3u].second.get() == p_proposed_decision_maker_.get() ? 1 : 0);
}
return result;
}();
{
std::scoped_lock lock(mtx_);
results_.push_back(result);
std::cout << results_.size() << '/'
<< seeds_.size() + num_alive_threads_ + results_.size() - 1u << std::endl;
}
}
}
catch (Kanachan::ThreadTermination const &) {
// This is graceful termination, so there is nothing to do.
}
catch (python::error_already_set const &) {
for (std::jthread &thread : threads_) {
thread.request_stop();
}
try {
Kanachan::translatePythonException();
} catch (std::exception const &e) {
std::cerr << e.what() << std::endl;
throw;
}
}
catch (std::exception const &e) {
for (std::jthread &thread : threads_) {
thread.request_stop();
}
std::cerr << e.what() << std::endl;
throw;
}
python::list Simulator::Impl_::run()
{
{
Kanachan::GIL::RecursiveRelease gil_release;
for (auto &thread : threads_) {
KANACHAN_ASSERT((!thread.joinable()));
}
}
python::list results;
for (python::dict result : results_) {
results.append(result);
}
return results;
}
} // namespace Kanachan