-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
278 lines (258 loc) · 6.78 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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cmath>
#include <thread>
// Macro for quick debugging
#define logval(varname) std::cout << #varname": " << varname << "\n"
// Definitions of functions
void trd(const std::vector<uint64_t>& primes, bool& runthread, bool log, const std::string& ifilename);
inline bool fileExists(const std::string& name);
inline uint64_t fast_sqrt(uint64_t num);
enum class readOperation {read_zero_count, read_number};
int main(int argc, char** argv) {
// n = amount of primes to be calculated
uint64_t n;
// Filename
std::string ifilename;
// Options
bool log = false;
bool binin = false;
bool binout = false;
bool decout = true;
bool convert = false;
// IsDefined
bool defn = false;
bool defifilename = false;
// Parse arguments
for (uint64_t i = 1; i < argc; i++) {
if (strcmp(argv[i], "-log") == 0)
log = true;
else if (strcmp(argv[i], "-binin")) {
binin = true;
}
else if (strcmp(argv[i], "-binout")) {
binout = true;
decout = false;
}
else if (strcmp(argv[i], "-allout")) {
binout = true;
decout = true;
}
else if (strcmp(argv[i], "-tobin")) {
binin = false;
binout = true;
decout = false;
convert = true;
}
else if (strcmp(argv[i], "-todec")) {
binin = true;
binout = false;
decout = true;
convert = true;
}
else if (strcmp(argv[i], "-help") || strcmp(argv[i], "--help") || strcmp(argv[i], "--?") || strcmp(argv[i], "-?") || strcmp(argv[i], "/?") || strcmp(argv[i], "/help")) {
std::cout << "Follow this link for help: https://github.com/Antosser/prime-number-calculator#readme\n";
exit(0);
}
else if (!defifilename) {
ifilename = argv[i];
defifilename = true;
}
else if (!defn) {
n = atoi(argv[i]);
defn = true;
}
else {
std::cout << "Unexpected argument: " << argv[i] << std::endl;
exit(1);
}
}
// If n is not defined, ask
if (!defn && !convert) {
std::cout << "Numbers: ";
std::cin >> n;
}
// If ifilename is not defined, ask
if (!defifilename) {
std::cout << "File: ";
std::cin >> ifilename;
}
// Current calculated
uint64_t i = 2;
// Vector with result
std::vector<uint64_t> primes = {};
if (!binin) {
// Parse decimal file
if (fileExists(ifilename)) {
// File exists
std::cout << "Restoring vector from decimal...\n";
std::ifstream ifile(ifilename);
if (ifile.is_open()) {
std::string line;
uint64_t j = 0;
uint64_t total = 0;
// Parse each line
while (std::getline(ifile, line)) {
++j;
total += line.length() + 1;
// Print state
if (!(j & 0xfffff))
std::cout << primes.size() / 1000000 << "m\n";
primes.push_back(std::stoi(line));
}
// Set current calculated to last number in file + 1
i = primes[primes.size() - 1] + 1;
ifile.close();
}
else {
std::cout << "Unable to open file";
exit(1);
}
}
}
else {
// Parse binary file
if (fileExists(ifilename + ".prime")) {
// File exists
std::cout << "Restoring vector from binary...\n";
std::ifstream ifile(ifilename + ".prime", std::ios::binary);
if (ifile.is_open()) {
// number = parsed number
uint64_t number = 0;
// How many bytes to read next
int readbytes = 1;
// Same
int length = 0;
// Buffer
char* buffer = new char[8];
// State: read_zero_count OR read_number
readOperation readOperation = readOperation::read_zero_count;
// Cycle trough file
while (ifile.read(buffer, readbytes)) {
if (readOperation == readOperation::read_zero_count) {
length = *buffer;
// Switch operation
readOperation = readOperation::read_number;
readbytes = length;
// Clear read
memset(buffer, 0x00, 8);
}
else if (readOperation == readOperation::read_number) {
// Cast buffer (char*) to number (uint64_t)
number = *((uint64_t*)buffer);
// Switch operation
readOperation = readOperation::read_zero_count;
readbytes = 1;
// Add number to list
primes.push_back(number);
}
}
// Set current calculated to last number in file + 1
i = primes[primes.size() - 1] + 1;
}
else {
std::cout << "Unable to open file";
exit(1);
}
}
}
if (!convert)
std::cout << "Calculating...\n";
bool runthread = true;
// Timer
auto starttime = std::chrono::high_resolution_clock::now();
// Create log thread
std::thread* t1;
if (!convert) {
// Init thread
t1 = new std::thread(trd, std::ref(primes), std::ref(runthread), log, std::ref(ifilename));
// Explained in GitHub
for (; primes.size() < n; i++) {
uint64_t root = fast_sqrt(i);
for (uint64_t ci : primes) {
if (i % ci == 0)
goto brk;
if (ci > root)
break;
}
primes.push_back(i);
brk:;
}
}
runthread = false;
if (decout) {
std::cout << "Writing decimal...\n";
{
std::ofstream file(ifilename);
std::string data;
uint64_t count = 0;
for (uint64_t i = 0; i < primes.size(); i++) {
data += std::to_string(primes[i]) + '\n';
if (count & 0x1000000) {
std::cout << float(i) / 0x100000 << "m\n";
file.write(data.data(), data.size());
count = 0;
data.erase();
}
count++;
}
file.write(data.data(), data.size());
file.close();
}
}
if (binout) {
std::cout << "Writing binary...\n";
{
std::ofstream file(ifilename + ".prime", std::ios::binary);
// Loop trough primes
for (uint64_t j : primes) {
// Get length of current number
int length = 8;
const char* jchar = (const char*)&j;
for (int i = 7; i >= 0; i--) {
if (jchar[i] == 0) length--;
else break;
}
// Write length and number to file
file.write((char*)&length, 1);
file.write((char*)&j, length);
}
file.close();
}
}
// Print clock
auto duration = std::chrono::high_resolution_clock::now() - starttime;
std::cout << "Done in " << double(duration.count()) / 1000000000 << "s\n";
}
void trd(const std::vector<uint64_t>& primes, bool& runthread, bool log, const std::string& ifilename) {
std::ofstream logfile;
if (log) {
logfile.open(std::string(ifilename) + ".log.csv", std::ios::app);
}
long long before = 0;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
while (runthread) {
std::cout << floor(primes.size() / 1000) / 1000 << "m | " << float(primes.size() - before) / 50 << "k" << '\n';
if (log) {
logfile << primes.size() << "," << float(primes.size() - before) * 20 << '\n';
}
before = primes.size();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
inline bool fileExists(const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}
inline uint64_t fast_sqrt(uint64_t num) {
uint64_t root = 0;
for (int32_t i = 15; i >= 0; i--) {
uint64_t temp = root | ((uint64_t)1 << i);
if (temp * temp <= num) {
root = temp;
}
}
return root;
}