-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
283 lines (255 loc) · 9.82 KB
/
main.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
#include "file.hpp"
#include "root_subs.hpp"
#include "smart_dist.hpp"
#include "snapshot.hpp"
#include "subs_dist.hpp"
#include "worker.hpp"
#include <atomic>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <vector>
const float TEMPLATE_PERC_THRESHOLD = 95.0;
std::vector<std::string> read_ranking(std::string ranking_path) {
std::vector<std::string> ranking;
std::ifstream ranking_file(ranking_path);
std::string user;
while (ranking_file >> user) {
ranking.push_back(user);
}
return ranking;
}
bool ensure_snap_same_task(const partial_t &partial, std::string soldir) {
if (!partial.empty()) {
auto &[perc, f1, f2] = *partial.begin();
if (f1.rfind(soldir, 0) != 0) {
std::cerr << "\033[41;37;1mWARNING!\033[0m The snapshot is using a "
"different directory with the solutions!"
<< std::endl;
std::cerr
<< "The snapshot is using: "
<< std::filesystem::path(f1).parent_path().parent_path().string()
<< std::endl;
std::cerr << "You are using: " << soldir << std::endl;
std::cerr << "Are you sure to continue? [yn] " << std::flush;
std::string prompt;
std::cin >> prompt;
if (prompt != "y") {
std::cerr << "quitting..." << std::endl;
return false;
}
}
}
return true;
}
std::vector<file_t> read_templates(std::string templatedir) {
std::vector<file_t> templates;
for (auto &p : std::filesystem::directory_iterator(templatedir)) {
templates.emplace_back(p.path().string());
}
return templates;
}
template <typename T>
std::tuple<int, int, int> compute_eta(T start, size_t progress, size_t total) {
int delta = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now() - start)
.count() /
1.0e9 * (total - progress) / progress;
int h = delta / 3600;
int m = (delta % 3600) / 60;
int s = delta % 60;
return {h, m, s};
}
file_list_t read_files(std::string soldir,
const std::vector<std::string> &ranking,
const std::vector<file_t> &templates,
size_t resume_index) {
file_list_t files(ranking.size());
size_t num_files = 0;
for (const auto &entry : std::filesystem::directory_iterator(soldir)) {
std::string group_name = entry.path().filename();
for (size_t u = resume_index; u < ranking.size(); u++) {
std::string dir = soldir + "/" + group_name + "/" + ranking[u];
if (!std::filesystem::exists(dir)) {
continue;
}
for (const auto &path : std::filesystem::directory_iterator(dir)) {
const size_t THRESHOLD = 32 * 1024;
auto size = std::filesystem::file_size(path.path());
if (size <= THRESHOLD) {
file_t file(path.path());
file.group = group_name;
files[u].emplace_back(file, 0);
num_files++;
} else {
std::cerr << "Ignoring too big file " << path.path().string() << ": "
<< size << " > " << THRESHOLD << std::endl;
}
}
}
}
std::cerr << "Comparing files with templates..." << std::endl;
std::atomic<size_t> pos(resume_index), files_done(0), files_ignored(0);
size_t nthreads = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
for (size_t t = 0; t < nthreads; t++) {
threads.emplace_back(
[&files, &ranking, &templates, &pos, &files_done, &files_ignored]() {
for (size_t u = pos++; u < ranking.size(); u = pos++) {
std::vector<size_t> to_remove;
for (size_t i = 0; i < files[u].size(); i++) {
auto &[file, perc] = files[u][i];
for (const file_t &templ : templates) {
perc = std::max(perc, smart_dist(file, templ, 0.0));
}
if (perc > TEMPLATE_PERC_THRESHOLD) {
to_remove.push_back(i);
}
}
files_done += files[u].size();
for (ssize_t i = to_remove.size() - 1; i >= 0; i--) {
std::swap(files[u][to_remove[i]], files[u].back());
files[u].pop_back();
}
files_ignored += to_remove.size();
}
});
}
auto start = std::chrono::high_resolution_clock::now();
while (files_done < num_files) {
if (files_done) {
auto [h, m, s] = compute_eta(start, files_done.load(), num_files);
fprintf(stderr,
"\033[J files %6ld / %6ld (%6.2f%%) | user %4ld / %4ld | ETA "
"%4d:%02d:%02d\r",
files_done.load(), num_files,
100.0 * files_done.load() / num_files, pos.load(), ranking.size(),
h, m, s);
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
}
for (auto &thread : threads) {
thread.join();
}
fprintf(stderr, "\033[J files %6ld / %6ld (%6.2f%%) | user %4ld / %4ld\n",
num_files, num_files, 100.0, ranking.size(), ranking.size());
std::cerr << "Ignored " << files_ignored << " files" << std::endl;
return files;
}
int main(int argc, char **argv) {
if (argc != 6) {
std::cerr << "Usage: " << argv[0]
<< " soldir templatedir ranking.txt cutoff target" << std::endl;
return 1;
}
std::string soldir = argv[1];
std::string templatedir = argv[2];
std::string ranking_path = argv[3];
int cutoff = std::atoi(argv[4]);
std::string target_path = argv[5];
std::filesystem::create_directories(target_path);
std::cerr << "Reading the ranking file..." << std::endl;
std::vector<std::string> ranking = read_ranking(ranking_path);
std::cerr << "Checking local snapshots..." << std::endl;
partial_t partial_hi, partial_lo;
size_t resume_index = std::numeric_limits<size_t>::max();
for (auto &f : std::filesystem::directory_iterator(target_path)) {
auto path = f.path();
auto name = path.filename().string();
if (name.find("_temp", name.size() - 5) != std::string::npos) {
std::cerr << "Partial snapshot found... ignoring " << path << std::endl;
} else if (name.rfind("snap", 0) == 0) {
std::cerr << "Loading snapshot from " << path << std::endl;
read_snap(path.string(), false, resume_index, partial_hi, partial_lo);
}
}
if (std::filesystem::exists(target_path + "/partial")) {
std::cerr << "Loading partial results from " << target_path + "/partial"
<< std::endl;
read_snap(target_path + "/partial", true, resume_index, partial_hi,
partial_lo);
}
if (resume_index == std::numeric_limits<size_t>::max())
resume_index = 0;
// prune partial results
prune_extra_results(partial_hi);
prune_extra_results(partial_lo);
// make sure the snapshot used the same solution directory
if (!ensure_snap_same_task(partial_hi, soldir)) {
return 1;
}
// save partial results
save_snap(resume_index, partial_hi, partial_lo, target_path + "/partial");
std::cerr << "Reading template files..." << std::endl;
std::vector<file_t> templates = read_templates(templatedir);
std::cerr << "Reading solution files..." << std::endl;
file_list_t files = read_files(soldir, ranking, templates, resume_index);
// files are read, since we don't want to print them this mapping is useless
mapping.clear();
std::cerr << "Starting from " << resume_index << std::endl;
int nthreads = std::thread::hardware_concurrency();
std::cerr << "Using " << nthreads << " threads" << std::endl;
std::vector<std::pair<queue_t, queue_t>> results(nthreads);
std::vector<std::thread> threads;
std::vector<size_t> current_index(nthreads);
std::atomic<size_t> progress(0), global_pos(resume_index);
auto start = std::chrono::high_resolution_clock::now();
// spawn all the workers
for (int i = 0; i < nthreads; i++) {
threads.emplace_back(worker, &files, cutoff, &global_pos, i, &results[i],
&progress, ¤t_index[i], target_path);
}
// meanwhile compute the total number of pairs to process
size_t num_pairs = 0;
size_t tot = 0;
for (size_t i = resume_index; i < files.size(); i++) {
tot += files[i].size();
}
for (size_t i = resume_index; i < files.size(); i++) {
num_pairs += files[i].size() * (tot - files[i].size());
}
num_pairs /= 2;
printf(" pairs %8ld / %ld (%6.2f%%) | user %4ld / %4ld\r", progress.load(),
num_pairs, 0.0, global_pos.load() - nthreads, ranking.size());
// UI loop, print the progress in one line using `\r` for going back to the
// start of the line
while (progress < num_pairs) {
if (progress) {
auto [h, m, s] = compute_eta(start, progress.load(), num_pairs);
printf("\033[J pairs %8ld / %ld (%6.2f%%) | user %4ld / %4ld | ETA "
"%4d:%02d:%02d | cur",
progress.load(), num_pairs, progress * 100.0 / num_pairs,
global_pos.load() - nthreads, ranking.size(), h, m, s);
for (auto index : current_index)
printf(" %ld", index);
printf("\r");
fflush(stdout);
}
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
}
printf(" pairs %8ld / %ld (%6.2f%%) | user %4ld / %4ld\033[J\n",
progress.load(), num_pairs, 0.0, global_pos.load() - nthreads,
ranking.size());
// since progress >= num_pairs we know that all threads have finished
for (auto &thread : threads) {
thread.join();
}
// merge the partial results of each thread with the previous partial results
for (auto &[hi, lo] : results) {
partial_hi.insert(hi.begin(), hi.end());
partial_lo.insert(lo.begin(), lo.end());
}
prune_extra_results(partial_hi);
prune_extra_results(partial_lo);
save_snap(global_pos.load(), partial_hi, partial_lo,
target_path + "/partial");
save_snap(global_pos.load(), partial_hi, partial_lo, target_path + "/total");
}