forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseMachine.cpp
More file actions
198 lines (171 loc) · 4.54 KB
/
Copy pathBaseMachine.cpp
File metadata and controls
198 lines (171 loc) · 4.54 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
/*
* BaseMachine.cpp
*
*/
#include "BaseMachine.h"
#include "OnlineOptions.h"
#include "Math/Setup.h"
#include <iostream>
#include <sodium.h>
using namespace std;
BaseMachine* BaseMachine::singleton = 0;
thread_local int BaseMachine::thread_num;
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 singleton");
}
BaseMachine::BaseMachine() : nthreads(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;
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
load_program(threadname, filename);
}
}
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);
inpf.close();
}
void BaseMachine::print_compiler()
{
if (compiler.size() != 0 and OnlineOptions::singleton.verbose)
cerr << "Compiler: " << compiler << endl;
}
void 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].mb_sent() << " MB)"
<< " after " << timer[n].idle() << endl;
timer[n].start(total_comm());
}
void BaseMachine::stop(int n)
{
timer[n].stop(total_comm());
cout << "Stopped timer " << n << " at " << timer[n].elapsed() << " ("
<< timer[n].mb_sent() << " MB)" << endl;
}
void BaseMachine::print_timers()
{
cerr << "The following timing is ";
if (OnlineOptions::singleton.live_prep)
cerr << "in";
else
cerr << "ex";
cerr << "clusive preprocessing." << endl;
cerr << "Time = " << timer[0].elapsed() << " seconds " << endl;
timer.erase(0);
for (auto it = timer.begin(); it != timer.end(); it++)
cerr << "Time" << it->first << " = " << it->second.elapsed() << " seconds ("
<< it->second.mb_sent() << " MB)" << endl;
}
string BaseMachine::memory_filename(const string& type_short, int my_number)
{
return OnlineOptions::singleton.prep_dir + "/" + "Memory-" + type_short + "-P" + to_string(my_number);
}
string BaseMachine::get_domain(string progname)
{
assert(not singleton);
BaseMachine machine;
singleton = 0;
machine.load_schedule(progname, false);
return machine.domain;
}
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;
}
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;
}
NamedCommStats BaseMachine::total_comm()
{
NamedCommStats res;
for (auto& queue : queues)
res += queue->get_comm_stats();
return res;
}
void BaseMachine::set_thread_comm(const NamedCommStats& stats)
{
auto queue = queues.at(BaseMachine::thread_num);
assert(queue);
queue->set_comm_stats(stats);
}