-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
80 lines (66 loc) · 2.41 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
#include <iostream>
#include <fstream>
#include <thread>
#include <algorithm>
#include <string>
#include <chrono>
#include "generator.h"
void processor(std::string& inputFile, std::string& outputFile, bool& done, uint64_t& processed, uint64_t& found)
{
std::ofstream output;
output.open(outputFile);
std::ifstream input;
input.open(inputFile);
while (input.good()) {
std::string line;
std::getline(input, line);
int64_t seed = std::stoull(line);
if (generator::ChunkGenerator::populate(seed)) {
output << line << "\n";
found++;
}
processed++;
}
done = true;
output.close();
input.close();
}
int main()
{
unsigned int threadCount = std::thread::hardware_concurrency();
std::thread threads[threadCount];
bool done[threadCount];
uint64_t processed[threadCount];
uint64_t found[threadCount];
for (int i = 0; i < threadCount; ++i) {
std::string fileName = "output" + std::to_string(i) + ".txt";
std::string inputName = "seeds" + std::to_string(i) + ".txt";
done[i] = false;
processed[i] = 0;
found[i] = 0;
std::ifstream input;
threads[i] = std::thread(processor, std::ref(inputName), std::ref(fileName), std::ref(done[i]), std::ref(processed[i]), std::ref(found[i]));
}
using namespace std::chrono_literals;
using namespace std::chrono;
milliseconds start = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
while (!std::all_of(done, done + threadCount, [](bool d) { return d; })) {
uint64_t processedCount = 0;
uint64_t foundCount = 0;
for (int i = 0; i < threadCount; i++) {
processedCount += processed[i];
foundCount += found[i];
}
milliseconds current = duration_cast<milliseconds>(system_clock::now().time_since_epoch());
double diff = (double)(current - start).count() / 1000;
if (diff != 0)
std::cout << "Processed " << processedCount << " seeds, found " << foundCount << " correct ones, uptime: "
<< diff << "s, speed: " << (processedCount / diff) / 1000000 << "m seed/sec, percentage of correct seeds: "
<< (double)foundCount / processedCount * 100 << "%" << std::endl;
std::this_thread::sleep_for(0.5s);
}
for (auto& thread : threads) {
thread.join();
}
return 0;
}