-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathBaseMachine.cpp
More file actions
418 lines (366 loc) · 10.6 KB
/
Copy pathBaseMachine.cpp
File metadata and controls
418 lines (366 loc) · 10.6 KB
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
/*
* BaseMachine.cpp
*
*/
#include "BaseMachine.h"
#include "OnlineOptions.h"
#include "Math/Setup.h"
#include "Tools/Bundle.h"
#include "Instruction.hpp"
#include "Protocols/ShuffleSacrifice.hpp"
#include <iostream>
#include <sodium.h>
#include <regex>
using namespace std;
BaseMachine* BaseMachine::singleton = 0;
thread_local int BaseMachine::thread_num;
thread_local OnDemandOTTripleSetup BaseMachine::ot_setup;
thread_local const Program* BaseMachine::program = 0;
void print_usage(ostream& o, const char* name, size_t capacity)
{
if (capacity)
o << name << "=" << capacity << " ";
}
BaseMachine& BaseMachine::s()
{
if (singleton)
return *singleton;
else
throw runtime_error("no BaseMachine singleton");
}
bool BaseMachine::has_program()
{
return has_singleton() and not s().progs.empty();
}
DataPositions BaseMachine::get_offline_data_used()
{
if (program)
return program->get_offline_data_used();
else
return s().progs[0].get_offline_data_used();
}
int BaseMachine::edabit_bucket_size(int n_bits)
{
size_t usage = 0;
if (has_program())
usage = get_offline_data_used().total_edabits(n_bits);
return bucket_size(usage);
}
int BaseMachine::triple_bucket_size(DataFieldType type)
{
size_t usage = 0;
if (has_program())
usage = get_offline_data_used().files[type][DATA_TRIPLE];
return bucket_size(usage);
}
int BaseMachine::bucket_size(size_t usage)
{
int res = OnlineOptions::singleton.bucket_size;
int min = res;
if (usage)
{
res = 5;
for (int B = res; B >= min; B--)
if (ShuffleSacrifice(B).minimum_n_outputs() > usage * 1.1)
break;
else
res = B;
}
if (OnlineOptions::singleton.has_option("debug_batch_size"))
fprintf(stderr, "bucket_size=%d usage=%zu\n", res, usage);
return res;
}
int BaseMachine::matrix_batch_size(int n_rows, int n_inner, int n_cols)
{
int limit = max(1., 1e6 / (max(n_rows * n_inner, n_inner * n_cols)));
unsigned res = min(limit, OnlineOptions::singleton.batch_size);
if (has_program())
res = min(res, (unsigned) matrix_requirement(n_rows, n_inner, n_cols));
return res;
}
int BaseMachine::matrix_requirement(int n_rows, int n_inner, int n_cols)
{
if (has_program())
{
auto res = get_offline_data_used().matmuls[
{n_rows, n_inner, n_cols}];
if (res)
return res;
else
return -1;
}
else
return -1;
}
bool BaseMachine::allow_mulm()
{
return singleton and singleton->relevant_opts.find("no_mulm") != string::npos;
}
BaseMachine::BaseMachine() :
nthreads(0), multithread(false), nan_warning(0), mini_warning(0)
{
if (sodium_init() == -1)
throw runtime_error("couldn't initialize libsodium");
if (not singleton)
singleton = this;
}
void BaseMachine::load_schedule(const string& progname, bool load_bytecode)
{
this->progname = progname;
string fname = "Programs/Schedules/" + progname + ".sch";
#ifdef DEBUG_FILES
cerr << "Opening file " << fname << endl;
#endif
ifstream inpf;
inpf.open(fname);
if (inpf.fail()) { throw file_error("Missing '" + fname + "'. Did you compile '" + progname + "'?"); }
int nprogs;
inpf >> nthreads;
inpf >> nprogs;
if (inpf.fail())
throw file_error("Error reading " + fname);
#ifdef DEBUG_FILES
cerr << "Number of threads I will run in parallel = " << nthreads << endl;
cerr << "Number of program sequences I need to load = " << nprogs << endl;
#endif
bc_filenames.clear();
// Load in the programs
string threadname;
for (int i=0; i<nprogs; i++)
{ inpf >> threadname;
size_t split = threadname.find_last_of(":");
long expected = -1;
if (split != string::npos)
{
expected = atoi(threadname.substr(split + 1).c_str());
threadname = threadname.substr(0, split);
}
string filename = "Programs/Bytecode/" + threadname + ".bc";
bc_filenames.push_back(filename);
if (load_bytecode)
{
#ifdef DEBUG_FILES
cerr << "Loading program " << i << " from " << filename << endl;
#endif
long size = load_program(threadname, filename);
if (expected >= 0 and expected != size)
{
stringstream os;
os << "broken bytecode file, found " << size
<< " instructions, expected " << expected;
throw runtime_error(os.str());
}
}
}
for (auto i : {1, 0, 0})
{
int n;
inpf >> n;
if (n != i)
throw runtime_error("old schedule format not supported");
}
inpf.get();
getline(inpf, compiler);
getline(inpf, domain);
getline(inpf, relevant_opts);
getline(inpf, security);
getline(inpf, gf2n);
getline(inpf, expected_communication);
inpf.close();
}
void BaseMachine::print_compiler()
{
if (compiler.size() != 0 and OnlineOptions::singleton.verbose)
cerr << "Compiler: " << compiler << endl;
}
size_t BaseMachine::load_program(const string& threadname,
const string& filename)
{
(void)threadname;
(void)filename;
throw not_implemented();
}
void BaseMachine::time()
{
cout << "Elapsed time: " << timer[0].elapsed() << endl;
}
void BaseMachine::start(int n)
{
cout << "Starting timer " << n << " at " << timer[n].elapsed()
<< " (" << timer[n] << ")"
<< " after " << timer[n].idle() << endl;
timer[n].start(total_stats());
}
void BaseMachine::stop(int n)
{
timer[n].stop(total_stats());
cout << "Stopped timer " << n << " at " << timer[n].elapsed() << " ("
<< timer[n] << ")" << endl;
}
void BaseMachine::print_timers()
{
cerr << "The following benchmarks are ";
if (OnlineOptions::singleton.live_prep)
cerr << "in";
else
cerr << "ex";
cerr << "cluding preprocessing (offline phase)." << endl;
cerr << "Time = " << timer[0].elapsed() << " seconds " << endl;
for (auto it = timer.begin(); it != timer.end(); it++)
if (it->first)
{
cerr << "Time" << it->first << " = " << it->second.elapsed()
<< " seconds (" << it->second << ")" << endl;
if (not it->second.exe_stats().empty()
and OnlineOptions::singleton.has_option("part_exe"))
it->second.exe_stats().print();
}
}
string BaseMachine::memory_filename(const string& type_short, int my_number)
{
return PREP_DIR "Memory-" + type_short + "-P" + to_string(my_number);
}
string BaseMachine::get_domain(string progname)
{
return get_basics(progname).domain;
}
BaseMachine BaseMachine::get_basics(string progname)
{
if (singleton and s().progname == progname)
return s();
auto backup = singleton;
BaseMachine machine;
singleton = backup;
machine.load_schedule(progname, false);
return machine;
}
int BaseMachine::ring_size_from_schedule(string progname)
{
string domain = get_domain(progname);
if (domain.substr(0, 2).compare("R:") == 0)
{
return stoi(domain.substr(2));
}
else
return 0;
}
int BaseMachine::prime_length_from_schedule(string progname)
{
string domain = get_domain(progname);
if (domain.substr(0, 4).compare("lgp:") == 0)
return stoi(domain.substr(4));
else
return 0;
}
int BaseMachine::gf2n_length_from_schedule(string progname)
{
string domain = get_basics(progname).gf2n;
if (domain.substr(0, 4).compare("lg2:") == 0)
return stoi(domain.substr(4));
else
return 0;
}
bigint BaseMachine::prime_from_schedule(string progname)
{
string domain = get_domain(progname);
if (domain.substr(0, 2).compare("p:") == 0)
return bigint(domain.substr(2));
else
return 0;
}
int BaseMachine::security_from_schedule(string progname)
{
string sec = get_basics(progname).security;
if (sec.substr(0, 4).compare("sec:") == 0)
return stoi(sec.substr(4));
else
return 0;
}
ThreadQueues* BaseMachine::maybe_get_queues()
{
if (singleton and thread_num == 0)
return &s().queues;
else
return 0;
}
ThreadStats BaseMachine::total_stats()
{
return queues.total_stats();
}
void BaseMachine::set_thread_stats(const ThreadStats& stats)
{
auto queue = queues.at(BaseMachine::thread_num);
assert(queue);
queue->set_stats(stats);
}
void BaseMachine::print_global_comm(Player& P, const NamedCommStats& stats)
{
Bundle<octetStream> bundle(P);
bundle.mine.store(stats.sent);
P.Broadcast_Receive_no_stats(bundle);
long long global = 0;
for (auto& os : bundle)
global += os.get_int(8);
cerr << "Global data sent = " << global / 1e6 << " MB (all parties)" << endl;
double total_time;
if (multithread)
total_time = max_comm.total_time();
else
total_time = comm_stats.total_time();
if (total_time > .95 * timer[0].elapsed())
cerr
<< "Communication time accounts for more than 95 percent of total time ("
<< total_time << " seconds). "
<< "This might be because of the computation and the network setting, "
<< "but it could also be product of the optimization trade-off during compilation. "
<< "See https://mp-spdz.readthedocs.io/en/latest/troubleshooting.html#high-number-of-rounds-or-slow-wan-execution"
<< endl;
smatch what;
regex comm_regexp("online:([0-9]*) offline:([0-9]*) n_parties:([0-9]*)");
if (regex_search(expected_communication, what, comm_regexp))
{
long long expected = stoll(what[1]) + stoll(what[2]);
int n_parties = stoi(what[3]);
if (expected and n_parties != P.num_players())
{
cerr << "Wrong number of parties in compiler's expectation: "
<< n_parties << endl;
}
else if (expected)
{
double over = round(100. * (global - expected) / expected);
if (over >= 5)
cerr
<< "Actual communication exceeds the compiler's expectation by "
<< over << " percent." << endl;
if (over < 0)
{
if (OnlineOptions::singleton.has_option("overestimate"))
cerr << "Actual communication is below the compiler's "
"expectation by " << -over << " percent." << endl;
else
cerr << "The compiler overestimated the communication." << endl;
}
}
}
}
void BaseMachine::print_comm(Player& P, const NamedCommStats& comm_stats)
{
size_t rounds = 0;
for (auto& x : comm_stats)
if (x.first.find("transmission") == string::npos)
rounds += x.second.rounds;
cerr << "Data sent = " << comm_stats.sent / 1e6 << " MB in ~" << rounds
<< " rounds (party " << P.my_num() << " only";
if (multithread)
cerr << "; rounds counted double due to multi-threading";
if (not OnlineOptions::singleton.verbose)
cerr << "; use '-v' for more details";
cerr << ")" << endl;
print_global_comm(P, comm_stats);
}
void BaseMachine::add_one_off(const NamedCommStats& comm)
{
if (has_singleton())
s().one_off_comm += comm;
}