-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpe_dpe
505 lines (434 loc) · 13.3 KB
/
pe_dpe
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#ifndef PE_DPE_
#define PE_DPE_
#include "pe_base"
namespace pe {
#if ENABLE_ZMQ
struct DpeCommand {
std::string name;
std::vector<std::vector<int64>> args;
std::string ToString() const {
std::stringstream ss;
ss << name;
ss << "(";
int arg_idx = 0;
for (const auto &arg : args) {
if (arg_idx > 0) ss << ",";
if (std::size(arg) == 1) {
ss << arg[0];
} else {
ss << "{";
int val_idx = 0;
for (const int64 val : arg) {
if (val_idx > 0) ss << ",";
ss << val;
++val_idx;
}
ss << "}";
}
++arg_idx;
}
ss << ")";
return ss.str();
}
};
SL std::ostream &operator<<(std::ostream &o, const DpeCommand &cmd) {
return o << cmd.ToString();
}
namespace internal {
SL std::optional<std::string> ParseDpeToken(std::string_view cmd, int &i) {
while (i < std::size(cmd) && std::isspace(cmd[i])) ++i;
if (i == std::size(cmd)) return "$";
if (cmd[i] == '(' || cmd[i] == ')' || cmd[i] == '{' || cmd[i] == '}' ||
cmd[i] == ',') {
return std::string(cmd.substr(i++, 1));
} else if (std::isalpha(cmd[i])) {
int j = i;
while (i < std::size(cmd) && std::isalpha(cmd[i])) {
++i;
}
return std::string(cmd.substr(j, i - j));
} else if (std::isdigit(cmd[i]) || cmd[i] == '+' || cmd[i] == '-') {
int j = i;
while (i < std::size(cmd) && (cmd[i] == '+' || cmd[i] == '-')) {
++i;
}
if (i == std::size(cmd) || !std::isdigit(cmd[i])) {
return std::nullopt;
}
while (i < std::size(cmd) && std::isdigit(cmd[i])) {
++i;
}
return std::string(cmd.substr(j, i - j));
}
return std::nullopt;
}
SL std::optional<int64> ParseNumberToken(std::optional<std::string> s) {
if (!s.has_value() || std::empty(s.value())) {
return std::nullopt;
}
if (std::isdigit(s.value()[0]) || s.value()[0] == '+' ||
s.value()[0] == '-') {
std::string t = *s;
const int size = std::size(t);
int sgn = 1;
int j = 0;
while (j < size && (t[j] == '+' || t[j] == '-')) {
if (t[j] == '-') {
sgn = -sgn;
}
++j;
}
int64 value = 0;
while (j < size && std::isdigit(t[j])) {
value = value * 10 + t[j] - '0';
++j;
}
return sgn < 0 ? -value : value;
}
return std::nullopt;
}
SL std::optional<std::vector<int64>> ParseDpeNumberList(std::string_view cmd,
int &i) {
std::optional<std::string> token = ParseDpeToken(cmd, i);
if (!token.has_value()) {
return std::nullopt;
}
if (token == "{") {
std::vector<int64> ret;
for (;;) {
std::optional<std::string> token = ParseDpeToken(cmd, i);
if (!token.has_value()) {
return std::nullopt;
}
if (token == "}") {
break;
}
if (token == ",") {
continue;
}
if (std::optional<int64> number = ParseNumberToken(token);
number.has_value()) {
ret.push_back(*number);
} else {
return std::nullopt;
}
}
return ret;
} else if (std::optional<int64> number = ParseNumberToken(token);
number.has_value()) {
return std::vector<int64>{*number};
}
return std::nullopt;
}
SL std::optional<std::vector<std::vector<int64>>> ParseDpeArgs(
std::string_view cmd, int &i) {
std::optional<std::string> start = ParseDpeToken(cmd, i);
if (!start.has_value()) {
return std::nullopt;
}
if (start == "$") {
return std::vector<std::vector<int64>>{};
}
if (start != "(") {
return std::nullopt;
}
std::vector<std::vector<int64>> ret;
for (;;) {
std::optional<std::string> token = ParseDpeToken(cmd, i);
if (!token.has_value()) {
return std::nullopt;
}
if (token == ")") {
break;
}
if (token == ",") {
continue;
}
std::string s = *std::move(token);
if (s[0] == '{' || s[0] == '+' || s[0] == '-' || std::isdigit(s[0])) {
i -= std::size(s);
std::optional<std::vector<int64>> number_list =
ParseDpeNumberList(cmd, i);
if (!number_list.has_value()) {
return std::nullopt;
}
ret.push_back(*std::move(number_list));
} else {
return std::nullopt;
}
}
return ret;
}
SL std::optional<DpeCommand> ParseDpeCommand(std::string_view cmd, int &i) {
std::optional<std::string> name = ParseDpeToken(cmd, i);
if (!name.has_value() || std::empty(name.value()) ||
!std::isalpha(name.value()[0])) {
return std::nullopt;
}
std::optional<std::vector<std::vector<int64>>> args = ParseDpeArgs(cmd, i);
if (!args.has_value()) {
return std::nullopt;
}
return DpeCommand{.name = *std::move(name), .args = *std::move(args)};
}
SL std::optional<DpeCommand> ParseDpeCommand(std::string_view cmd) {
int i = 0;
return ParseDpeCommand(cmd, i);
}
} // namespace internal
// Distributred PE
template <typename Derived>
class DpeServer {
public:
DpeServer(std::string_view server_address, std::string_view cache_path)
: server_address(server_address), cache_path(cache_path) {}
void RunReducer() {
Initialize();
void *context = zmq_ctx_new();
void *responder = zmq_socket(context, ZMQ_REP);
int rc = zmq_bind(responder, std::data(server_address));
assert(rc == 0);
constexpr int buffer_length = 1 << 20;
char *buffer = new char[buffer_length + 1];
for (; solved < total;) {
int received = zmq_recv(responder, buffer, buffer_length, 0);
buffer[received] = 0;
std::string response = HandleRequest(std::string_view(buffer, received));
zmq_send(responder, std::data(response), std::size(response), 0);
}
LogProgress();
delete[] buffer;
zmq_close(responder);
zmq_ctx_destroy(context);
}
private:
void Initialize() {
std::vector<int64> tasks = AsDerived().GetTasks();
cache.clear();
const int64 size = std::size(tasks);
for (int i = 0; i < size; ++i) {
cache[tasks[i]] = std::nullopt;
}
answer = AsDerived().GetInit();
total = std::size(cache);
solved = 0;
LoadCache();
unsolved = std::queue<int64>();
for (const auto [key, value] : cache) {
if (value.has_value()) {
++solved;
answer = AsDerived().Reduce(answer, key, *value);
} else {
unsolved.push(key);
}
}
LogProgress();
}
void SaveCache() {
if (std::empty(cache_path)) return;
FILE *fp = fopen(std::data(cache_path), "w");
if (!fp) return;
for (const auto [key, value] : cache) {
if (value.has_value()) {
fprintf(fp, "%I64d %I64d\n", key, *value);
}
}
fclose(fp);
}
void LoadCache() {
if (std::empty(cache_path)) return;
FILE *fp = fopen(std::data(cache_path), "r");
if (!fp) return;
int64 k, v;
while (fscanf(fp, "%I64d %I64d", &k, &v) == 2) {
auto where = cache.find(k);
if (where != cache.end()) {
where->second = v;
}
}
fclose(fp);
}
std::string HandleRequest(std::string_view request) {
std::optional<DpeCommand> command = internal::ParseDpeCommand(request);
if (!command.has_value()) {
return "error()";
}
if (command->name == "GetTasks") {
if (std::size(command->args) != 1 || std::size(command->args[0]) != 1) {
return "error()";
}
return AsDerived().HandleGetTasks(command->args[0][0]);
}
if (command->name == "SetResults") {
std::vector<std::pair<int64, int64>> answers;
for (const auto &iter : command->args) {
if (std::size(iter) != 2) {
return "error()";
}
answers.push_back({iter[0], iter[1]});
}
return AsDerived().HandleSetResults(answers);
}
return "error()";
}
std::string HandleGetTasks(int64 max_task_count) {
std::vector<int64> tasks;
for (int64 i = 0; i < max_task_count; ++i) {
if (std::empty(unsolved)) {
break;
}
tasks.push_back(unsolved.front());
unsolved.pop();
}
DpeCommand raw_command;
raw_command.name = "Tasks";
raw_command.args.push_back(tasks);
return raw_command.ToString();
}
std::string HandleSetResults(
const std::vector<std::pair<int64, int64>> &answers) {
for (const auto [key, value] : answers) {
auto where = cache.find(key);
if (where != cache.end()) {
where->second = value;
++solved;
}
answer = AsDerived().Reduce(answer, key, value);
if (solved % 1000 == 0) {
SaveCache();
}
if (solved % 100 == 0) {
LogProgress();
}
}
return "ok()";
}
void LogProgress() {
TimeDelta te = tr.Elapsed();
char buff[32];
sprintf(buff, "%2.2f%%", 100. * solved / total);
std::string eta;
while (std::size(history_progress) > 100) history_progress.pop();
if (std::empty(history_progress) ||
history_progress.front().second == solved) {
eta = "n/a";
} else {
auto his = history_progress.front();
// (total - solved) / eta = (solved - his.second) / (te - his.first)
// eta = (total - solved) / (solved - his.second) * (te - his.first)
eta = TimeDelta(duration_t(static_cast<int64>(
1. * (total - solved) / (solved - his.second) *
(te.NativeTime() - his.first.NativeTime()))))
.Format();
}
history_progress.push({te, solved});
std::cerr << std::setw(6) << buff << " " << solved << " " << total << " "
<< answer << " " << te.Format() << " " << eta << std::endl;
}
int64 GetInit() { return 0; }
Derived &AsDerived() { return static_cast<Derived &>(*this); }
private:
std::string server_address;
std::string cache_path;
int64 solved = 0;
std::map<int64, std::optional<int64>> cache;
std::queue<int64> unsolved;
int64 answer = 0;
int64 total = 0;
TimeRecorder tr;
std::queue<std::pair<TimeDelta, int64>> history_progress;
};
template <typename Derived>
class DpeClient {
public:
DpeClient(std::string_view target)
: target(target), buffer_length(1 << 20), buffer(nullptr) {}
~DpeClient() {
if (buffer != nullptr) delete[] buffer;
}
std::vector<int64> GetTasks(int64 task_count) {
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, std::data(target));
DpeCommand raw_command;
raw_command.name = "GetTasks";
raw_command.args.push_back({task_count});
std::string cmd = raw_command.ToString();
zmq_send(requester, std::data(cmd), std::size(cmd), 0);
int cnt = zmq_recv(requester, buffer, buffer_length, 0);
zmq_close(requester);
zmq_ctx_destroy(context);
buffer[cnt] = 0;
std::optional<DpeCommand> result =
internal::ParseDpeCommand(std::string_view(buffer, cnt));
if (!result.has_value() || result->name != "Tasks") {
return {};
}
if (std::size(result->args) != 1) {
return {};
}
return result->args[0];
}
int64 SetResults(const std::vector<std::pair<int64, int64>> &answers) {
void *context = zmq_ctx_new();
void *requester = zmq_socket(context, ZMQ_REQ);
zmq_connect(requester, std::data(target));
DpeCommand raw_command;
raw_command.name = "SetResults";
for (auto [key, value] : answers) {
raw_command.args.push_back({key, value});
}
std::string cmd = raw_command.ToString();
zmq_send(requester, std::data(cmd), std::size(cmd), 0);
zmq_recv(requester, buffer, buffer_length, 0);
zmq_close(requester);
zmq_ctx_destroy(context);
return 0;
}
void RunWorker(int thread_count, int task_count) {
if (buffer == nullptr) {
buffer = new char[buffer_length];
}
#if ENABLE_OPENMP
OmpLock lock;
#endif
for (;;) {
std::vector<int64> tasks = GetTasks(task_count);
std::cerr << std::endl;
std::cerr << "Received " << std::size(tasks) << " tasks" << std::endl;
if (std::empty(tasks)) {
break;
}
std::cerr << "First task: " << tasks[0] << std::endl;
std::cerr << "Last task: " << tasks.back() << std::endl;
TimeRecorder tr;
std::vector<std::pair<int64, int64>> answers;
#if ENABLE_OPENMP
#pragma omp parallel for schedule(dynamic, 1) num_threads(thread_count)
#endif
for (auto key : tasks) {
int64 value = static_cast<Derived &>(*this).Cal(key);
#if ENABLE_OPENMP
OmpGuard guard(lock);
#endif
answers.push_back({key, value});
}
std::cerr << tr.Elapsed().Format() << std::endl;
SetResults(answers);
}
}
private:
std::string target;
const int buffer_length;
char *buffer;
};
template <typename Derived>
class DpeSolver : public DpeServer<Derived>, public DpeClient<Derived> {
public:
DpeSolver(std::string_view server_address, std::string_view cache_path = "")
: DpeServer<Derived>(server_address, cache_path),
DpeClient<Derived>(server_address) {}
};
#endif
} // namespace pe
#endif