-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_server.hpp
550 lines (505 loc) · 24.2 KB
/
eval_server.hpp
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include "common/common.h"
#include "computing-server/server.hpp"
#include "secret-share/secret_share.hpp"
const int kTotalDetectLength = 100;
const int kShuffleSeed = 1234;
class EvalServer : public NetServer {
public:
int fd = 0;
EvalServer(SERVER_ID server_id_, uint32_t id_range_, int bit_len_,
int max_degree_, int detect_length_k_, int total_detect_length_,
int local_compute_parallel_num_, int neighbor_pass_parallel_num_,
int sort_parallel_num_, int filter_parallel_num_,
int extract_path_parallel_num_, uint64_t sort_elem_threshold_,
int num_elem_to_find_median_, int seed12_permute_,
int seed23_permute_, int seed31_permute_, int seedA_, int seedB_,
/*ip address of the three computing server*/
string ip_1_, string ip_2_, string ip_3_,
/*num of tuples processed in each batch*/
uint64_t filter_batch_num_, uint64_t neighbor_pass_batch_num_,
uint64_t extract_batch_num_, uint64_t partition_batch_num_,
int tie_breaker_bit_)
: NetServer(server_id_, id_range_, bit_len_, max_degree_,
detect_length_k_, total_detect_length_,
/*local_compute_parallel_num=*/local_compute_parallel_num_,
/*neighbor_pass_parallel_num=*/neighbor_pass_parallel_num_,
/*sort_parallel_num=*/sort_parallel_num_,
/*filter_parallel_num=*/filter_parallel_num_,
/*extract_path_parallel_num=*/extract_path_parallel_num_,
/*sort_elem_threshold=*/sort_elem_threshold_,
/*num_to_find_median=*/num_elem_to_find_median_,
/*seed12_permute=*/seed12_permute_,
/*seed23_permute=*/seed23_permute_,
/*seed31_permute=*/seed31_permute_, /*seedA=*/seedA_,
/*seedB=*/seedB_, ip_1_, ip_2_, ip_3_,
/*filter_batch_num=*/filter_batch_num_,
/*neighbor_pass_batch_num=*/neighbor_pass_batch_num_,
/*extract_batch_num=*/extract_batch_num_,
/*partition_batch_num=*/partition_batch_num_,
/*tie_breaker_bit=*/kTieBreakerBit) {}
void createRandomTuplesForSortWorker(int max_degree, int detect_length_k,
uint64_t rand_nodes_num,
uint64_t rand_path_num,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
int parallel_num, int pid) {
SecretShareTupleHelper helper((1 << kBitLen), kTieBreakerRange);
uint32_t node_per_thread = rand_nodes_num / parallel_num;
uint32_t begin_node_id = node_per_thread * pid + 1;
uint32_t end_node_id = node_per_thread * (pid + 1) + 1;
if (pid == parallel_num - 1) {
end_node_id = rand_nodes_num + 1;
}
MYASSERT(end_node_id <= rand_nodes_num + 1);
for (uint32_t node_id = begin_node_id; node_id < end_node_id;
node_id++) {
vector<uint32_t> edges;
int rand_edge_num = rand() % (max_degree + 1);
for (int i = 0; i < rand_edge_num; i++) {
uint32_t rand_neighbor = rand() % rand_nodes_num + 1;
MYASSERT(rand_neighbor > 0);
edges.push_back(rand_neighbor);
}
unique_ptr<SecretShareTuple> et = helper.formatEdgeTuple(
node_id, edges, max_degree, detect_length_k);
et->set_tie_breaker_id((node_id - 1) * rand_path_num);
vector<unique_ptr<SecretShareTuple>> ets =
helper.splitTupleIntoTwo(et);
s1->insert(ets[0]);
s2->insert(ets[1]);
for (uint64_t i = 0; i < rand_path_num; i++) {
vector<uint32_t> path;
for (int i = 0; i < detect_length_k - 1; i++) {
uint32_t rand_neighbor = rand() % rand_nodes_num;
if (rand_neighbor == 0) {
rand_neighbor = 1;
}
path.push_back(rand_neighbor);
}
path.push_back(node_id);
unique_ptr<SecretShareTuple> t =
helper.formatPathTuple(path, max_degree, detect_length_k);
t->set_tie_breaker_id((node_id - 1) * rand_path_num + i);
vector<unique_ptr<SecretShareTuple>> ts =
helper.splitTupleIntoTwo(t);
s1->insert(ts[0]);
s2->insert(ts[1]);
}
}
}
void createRandomTuplesForSort(int max_degree, int detect_length_k,
uint64_t rand_nodes_num,
uint64_t rand_path_num,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
int parallel_num) {
srand(time(0));
cout << "using " << rand_nodes_num << " nodes in total\n";
vector<unique_ptr<ServerSecretShares>> s1_vec;
vector<unique_ptr<ServerSecretShares>> s2_vec;
vector<thread> t_vec;
for (int i = 0; i < parallel_num; i++) {
unique_ptr<ServerSecretShares> s1_p =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s2_p =
make_unique<ServerSecretShares>();
s1_vec.push_back(move(s1_p));
s2_vec.push_back(move(s2_p));
}
for (int i = 0; i < parallel_num; i++) {
thread t(&EvalServer::createRandomTuplesForSortWorker, this,
max_degree, detect_length_k, rand_nodes_num, rand_path_num,
ref(s1_vec[i]), ref(s2_vec[i]), parallel_num, i);
t_vec.push_back(move(t));
}
for (auto &t : t_vec) {
t.join();
}
s1->mergeFromSSVec(s1_vec);
s2->mergeFromSSVec(s2_vec);
MYASSERT(s1->size() == rand_nodes_num * (rand_path_num + 1));
MYASSERT(s2->size() == rand_nodes_num * (rand_path_num + 1));
cout << "finish creating data\n";
}
void createRandomTuplesForExtract(int max_degree, int detect_length_k,
uint64_t rand_nodes_num,
uint64_t rand_path_num,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
int parallel_num) {
createRandomTuplesForSort(max_degree, detect_length_k, rand_nodes_num,
rand_path_num, s1, s2, parallel_num);
}
void createRandomTuplesForNeighborPassingWorker(
int max_degree, int detect_length_k, uint64_t nodes_num,
uint64_t path_tuple_num, unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2, int parallel_num, int pid) {
SecretShareTupleHelper helper((1 << kBitLen), kTieBreakerRange);
MYASSERT(s1->size() == 0);
MYASSERT(s2->size() == 0);
uint64_t nodes_per_thread = nodes_num / parallel_num;
uint64_t begin_nodes = nodes_per_thread * pid;
uint64_t end_nodes = nodes_per_thread * (pid + 1);
if (pid == parallel_num - 1) {
end_nodes = nodes_num;
}
for (uint64_t i = begin_nodes; i < end_nodes; i++) {
// node id cannot be zero so set to i+1
uint32_t node_id = (i + 1) % (1 << kBitLen);
if (node_id == 0) {
node_id = 1;
}
vector<uint32_t> current_neighbors;
// create neighbors for the node_id.
int rand_degree = rand() % max_degree;
for (int j = 0; j < rand_degree; j++) {
uint32_t neighbor_val = rand() % (1 << kBitLen);
current_neighbors.push_back(neighbor_val);
}
unique_ptr<SecretShareTuple> et = helper.formatEdgeTuple(
node_id, current_neighbors, max_degree, detect_length_k);
// assign tie_breaker_id
et->set_tie_breaker_id(i);
vector<unique_ptr<SecretShareTuple>> ets =
helper.splitTupleIntoTwo(et);
s1->insert(ets[0]);
s2->insert(ets[1]);
// create rand_tuple_num of the current node id.
for (uint64_t j = 0; j < path_tuple_num; j++) {
vector<uint32_t> path;
for (int k = 0; k < detect_length_k; k++) {
path.push_back(rand() % (1 << kBitLen));
}
unique_ptr<SecretShareTuple> pt =
helper.formatPathTuple(path, max_degree, detect_length_k);
vector<unique_ptr<SecretShareTuple>> pts =
helper.splitTupleIntoTwo(pt);
s1->insert(pts[0]);
s2->insert(pts[1]);
}
}
}
void createRandomTuplesForNeighborPassing(
int max_degree, int detect_length_k, uint64_t nodes_num,
uint64_t path_tuple_num, unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2, int parallel_num) {
vector<unique_ptr<ServerSecretShares>> s1_vec;
vector<unique_ptr<ServerSecretShares>> s2_vec;
vector<thread> t_vec;
for (int i = 0; i < parallel_num; i++) {
unique_ptr<ServerSecretShares> s1_p =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s2_p =
make_unique<ServerSecretShares>();
s1_vec.push_back(move(s1_p));
s2_vec.push_back(move(s2_p));
}
for (int i = 0; i < parallel_num; i++) {
thread t(&EvalServer::createRandomTuplesForNeighborPassingWorker,
this, max_degree, detect_length_k, nodes_num,
path_tuple_num, ref(s1_vec[i]), ref(s2_vec[i]),
parallel_num, i);
t_vec.push_back(move(t));
}
for (auto &t : t_vec) {
t.join();
}
s1->mergeFromSSVec(s1_vec);
s2->mergeFromSSVec(s2_vec);
MYASSERT(s1->size() == nodes_num * (path_tuple_num + 1));
MYASSERT(s2->size() == nodes_num * (path_tuple_num + 1));
cout << "finish creating tuples for filter\n";
}
void createRandomTuplesForFilterWorker(int max_degree, int detect_length_k,
uint64_t path_num,
double valid_ratio,
double cycle_ratio,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
int parallel_num, int pid) {
SecretShareTupleHelper helper((1 << kBitLen), kTieBreakerRange);
uint64_t valid_path_num = static_cast<uint64_t>(path_num * valid_ratio);
uint64_t cycle_num = static_cast<uint64_t>(path_num * cycle_ratio);
uint64_t tuple_per_thread = path_num / parallel_num;
uint64_t begin_index = tuple_per_thread * pid;
uint64_t end_index = tuple_per_thread * (pid + 1);
if (pid == parallel_num - 1) {
end_index = path_num;
}
for (uint64_t i = begin_index; i < end_index; i++) {
vector<uint32_t> path;
for (int i = 0; i < detect_length_k; i++) {
uint32_t node = rand() % ((1 << kBitLen) - 1) + 1;
while (find(path.begin(), path.end(), node) != path.end()) {
node = rand() % ((1 << kBitLen) - 1) + 1;
}
path.push_back(node);
}
bool is_valid = false;
bool is_cycle = false;
if (i <= valid_path_num) {
is_valid = true;
}
if (i <= cycle_num) {
is_cycle = true;
}
if (!is_valid) {
int repeat_node_id = rand() % (detect_length_k) + 1;
if (repeat_node_id == detect_length_k) {
path.push_back(0);
} else {
path.push_back(path[repeat_node_id]);
}
} else if (is_cycle) {
path.push_back(path[0]);
} else {
// valid but non-cycle
uint32_t last_node = rand() % ((1 << kBitLen) - 1) + 1;
// if last node repeats with existing nodes
while (find(path.begin(), path.end(), last_node) !=
path.end()) {
last_node = rand() % ((1 << kBitLen) - 1) + 1;
}
path.push_back(last_node);
}
unique_ptr<SecretShareTuple> pt =
helper.formatFilledPathTuple(path, max_degree, detect_length_k);
pt->set_tie_breaker_id(i);
vector<unique_ptr<SecretShareTuple>> pts =
helper.splitTupleIntoTwo(pt);
s1->insert(pts[0]);
s2->insert(pts[1]);
}
}
void createRandomTuplesForFilter(int max_degree, int detect_length_k,
uint64_t path_num, double valid_ratio,
double cycle_ratio,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
int parallel_num) {
uint64_t valid_path_num = static_cast<uint64_t>(path_num * valid_ratio);
uint64_t cycle_num = static_cast<uint64_t>(path_num * cycle_ratio);
cout << "creating " << path_num << " tuples, " << valid_path_num
<< " tuples, cycle tuples: " << cycle_num << endl;
vector<unique_ptr<ServerSecretShares>> s1_vec;
vector<unique_ptr<ServerSecretShares>> s2_vec;
vector<thread> t_vec;
for (int i = 0; i < parallel_num; i++) {
unique_ptr<ServerSecretShares> s1_p =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s2_p =
make_unique<ServerSecretShares>();
s1_vec.push_back(move(s1_p));
s2_vec.push_back(move(s2_p));
}
for (int i = 0; i < parallel_num; i++) {
thread t(&EvalServer::createRandomTuplesForFilterWorker, this,
max_degree, detect_length_k, path_num, valid_ratio,
cycle_ratio, ref(s1_vec[i]), ref(s2_vec[i]), parallel_num,
i);
t_vec.push_back(move(t));
}
for (auto &t : t_vec) {
t.join();
}
s1->mergeFromSSVec(s1_vec);
s2->mergeFromSSVec(s2_vec);
MYASSERT(s1->size() == path_num);
MYASSERT(s2->size() == path_num);
cout << "finish creating tuples for filter\n";
}
void createRandomTuplesForShuffleWorker(int max_degree, int detect_length_k,
uint64_t tuple_num,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
unique_ptr<ServerSecretShares> &s3,
int parallel_num, int pid) {
SecretShareTupleHelper helper((1 << kBitLen), kTieBreakerRange);
uint64_t tuple_per_thread = tuple_num / parallel_num;
uint64_t begin_index = tuple_per_thread * pid;
uint64_t end_index = tuple_per_thread * (pid + 1);
if (pid == parallel_num - 1) {
end_index = tuple_num;
}
for (uint64_t i = begin_index; i < end_index; i++) {
vector<uint32_t> e = {
static_cast<uint32_t>(rand() % (1 << kBitLen)),
static_cast<uint32_t>(rand() % (1 << kBitLen))};
unique_ptr<SecretShareTuple> t =
helper.formatEdgeTuple(0, e, max_degree, detect_length_k);
t->set_tie_breaker_id(i);
vector<unique_ptr<SecretShareTuple>> ts = helper.splitTuple(t);
s1->insert(ts[0]);
s2->insert(ts[1]);
s3->insert(ts[2]);
}
}
// generated tuples are all edges, but this does not affect performance of
// shuffle.
void createRandomTuplesForShuffle(int max_degree, int detect_length_k,
uint64_t tuple_num,
unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2,
unique_ptr<ServerSecretShares> &s3,
int parallel_num) {
cout << "creating " << tuple_num << " tuples\n";
vector<unique_ptr<ServerSecretShares>> s1_vec;
vector<unique_ptr<ServerSecretShares>> s2_vec;
vector<unique_ptr<ServerSecretShares>> s3_vec;
vector<thread> t_vec;
for (int i = 0; i < parallel_num; i++) {
unique_ptr<ServerSecretShares> s1_p =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s2_p =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s3_p =
make_unique<ServerSecretShares>();
s1_vec.push_back(move(s1_p));
s2_vec.push_back(move(s2_p));
s3_vec.push_back(move(s3_p));
}
for (int i = 0; i < parallel_num; i++) {
thread t(&EvalServer::createRandomTuplesForShuffleWorker, this,
max_degree, detect_length_k, tuple_num, ref(s1_vec[i]),
ref(s2_vec[i]), ref(s3_vec[i]), parallel_num, i);
t_vec.push_back(move(t));
}
for (auto &t : t_vec) {
t.join();
}
s1->mergeFromSSVec(s1_vec);
s2->mergeFromSSVec(s2_vec);
s3->mergeFromSSVec(s3_vec);
MYASSERT(s1->size() == tuple_num);
MYASSERT(s2->size() == tuple_num);
MYASSERT(s3->size() == tuple_num);
cout << "finish creating shuffle tuples\n";
}
void establishConnection() {
// establish server connection
if (server_id == SERVER_ID::S1) {
listenThread(ip_1, &fd);
} else {
connectThread(ip_1, &fd);
}
}
void closeConnection() {
// close connection
close(fd);
}
unique_ptr<ServerSecretShares> prepareSecretSharesForSort(
int max_degree, int detect_length_k, uint64_t nodes_num,
uint64_t path_num, int parallel_num) {
// generate random data, and exchange shares
unique_ptr<ServerSecretShares> local_share =
make_unique<ServerSecretShares>();
if (server_id == SERVER_ID::S1) {
unique_ptr<ServerSecretShares> s2 =
make_unique<ServerSecretShares>();
createRandomTuplesForSort(max_degree, detect_length_k, nodes_num,
path_num, local_share, s2, parallel_num);
compute_server->shuffleSecretShares(*local_share.get(),
kShuffleSeed);
compute_server->shuffleSecretShares(*s2.get(), kShuffleSeed);
sendServerSecretShareWorker(s2.get(), fd);
} else {
recvServerSecretShareWorker(local_share, fd, max_degree,
detect_length_k);
}
return local_share;
}
unique_ptr<ServerSecretShares> prepareSecretSharesForExtract(
int max_degree, int detect_length_k, uint64_t nodes_num,
uint64_t path_num, int parallel_num) {
// generate random data, and exchange shares
unique_ptr<ServerSecretShares> local_share =
make_unique<ServerSecretShares>();
if (server_id == SERVER_ID::S1) {
unique_ptr<ServerSecretShares> s2 =
make_unique<ServerSecretShares>();
createRandomTuplesForExtract(max_degree, detect_length_k, nodes_num,
path_num, local_share, s2,
parallel_num);
sendServerSecretShareWorker(s2.get(), fd);
} else {
recvServerSecretShareWorker(local_share, fd, max_degree,
detect_length_k);
}
return local_share;
}
unique_ptr<ServerSecretShares> prepareSecretSharesForNeighborPass(
int max_degree, int detect_length_k, uint64_t nodes_num,
uint64_t path_num, int parallel_num) {
// generate random data, and exchange shares
unique_ptr<ServerSecretShares> local_share =
make_unique<ServerSecretShares>();
if (server_id == SERVER_ID::S1) {
unique_ptr<ServerSecretShares> s2 =
make_unique<ServerSecretShares>();
createRandomTuplesForNeighborPassing(max_degree, detect_length_k,
nodes_num, path_num,
local_share, s2, parallel_num);
sendServerSecretShareWorker(s2.get(), fd);
} else {
recvServerSecretShareWorker(local_share, fd, max_degree,
detect_length_k);
}
return local_share;
}
unique_ptr<ServerSecretShares> prepareSecretSharesForFilter(
int max_degree, int detect_length_k, uint64_t path_num,
double valid_ratio, double cycle_ratio, int parallel_num) {
// generate random data, and exchange shares
unique_ptr<ServerSecretShares> local_share =
make_unique<ServerSecretShares>();
if (server_id == SERVER_ID::S1) {
unique_ptr<ServerSecretShares> s2 =
make_unique<ServerSecretShares>();
createRandomTuplesForFilter(max_degree, detect_length_k, path_num,
valid_ratio, cycle_ratio, local_share,
s2, parallel_num);
sendServerSecretShareWorker(s2.get(), fd);
} else {
recvServerSecretShareWorker(local_share, fd, max_degree,
detect_length_k);
}
return local_share;
}
void prepareSecretSharesForShuffle(int max_degree, int detect_length_k,
uint64_t path_num,
unique_ptr<ServerSecretShares> &gen_s1,
unique_ptr<ServerSecretShares> &gen_s2,
int parallel_num) {
// generate random data, and exchange shares
if (server_id == SERVER_ID::S1) {
unique_ptr<ServerSecretShares> s1 =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s2 =
make_unique<ServerSecretShares>();
unique_ptr<ServerSecretShares> s3 =
make_unique<ServerSecretShares>();
createRandomTuplesForShuffle(max_degree, detect_length_k, path_num,
s1, s2, s3, parallel_num);
sendServerSecretShareWorker(s2.get(), this->fd_2);
sendServerSecretShareWorker(s3.get(), this->fd_2);
sendServerSecretShareWorker(s3.get(), this->fd_3);
sendServerSecretShareWorker(s1.get(), this->fd_3);
gen_s1->copyFromServerSecretShares(s1);
gen_s2->copyFromServerSecretShares(s2);
} else {
recvServerSecretShareWorker(gen_s1, this->fd_1, max_degree,
detect_length_k);
recvServerSecretShareWorker(gen_s2, this->fd_1, max_degree,
detect_length_k);
}
}
void assignToSSForShuffle(unique_ptr<ServerSecretShares> &s1,
unique_ptr<ServerSecretShares> &s2) {
compute_server->getFirstServerSecretShares()->clear();
compute_server->getSecondServerSecretShares()->clear();
compute_server->getFirstServerSecretShares()
->copyFromServerSecretShares(s1);
compute_server->getSecondServerSecretShares()
->copyFromServerSecretShares(s2);
}
};