-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimes_cpp_v4.cpp
More file actions
369 lines (323 loc) · 13.7 KB
/
primes_cpp_v4.cpp
File metadata and controls
369 lines (323 loc) · 13.7 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
#include <condition_variable>
#include <iostream>
#include <fstream>
#include <functional>
#include <list>
#include <math.h>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#include <unistd.h>
typedef unsigned long long int llu;
//const llu max_prime(1E11); // smaller than 18E18 18,446,744,073,709,551,616
const unsigned long int step_to_save(1E8); // smaller than 4E9 4,294,967,296
class Worker
{
public:
Worker(bool start) : m_Running(start) { if (start) private_start(); }
Worker() : m_Running(false) { }
~Worker() { stop(); }
template<typename... Args>
void push_task(Args&&... args) {
{
std::lock_guard<std::mutex> lk(m_Mutex);
m_Queue.push_back(std::bind(std::forward<Args>(args)...));
}
m_Condition.notify_all();
}
void start() {
{
std::lock_guard<std::mutex> lk(m_Mutex);
if (m_Running == true) return;
m_Running = true;
}
private_start();
}
void stop() {
{
std::lock_guard<std::mutex> lk(m_Mutex);
if (m_Running == false) return;
m_Running = false;
}
m_Condition.notify_all();
m_Thread.join();
}
private:
void private_start() {
m_Thread = std::thread([this] {
for (;;) {
decltype(m_Queue) local_queue;
{
std::unique_lock<std::mutex> lk(m_Mutex);
m_Condition.wait(lk, [&] { return !m_Queue.empty() + !m_Running; });
if (!m_Running)
{
for (auto& func : m_Queue)
func();
m_Queue.clear();
return;
}
std::swap(m_Queue, local_queue);
}
for (auto& func : local_queue)
func();
}
});
}
private:
std::condition_variable m_Condition;
std::list<std::function<void()>> m_Queue;
std::mutex m_Mutex;
std::thread m_Thread;
bool m_Running = false;
};
class Primes
{
public:
Primes() {}
~Primes() {}
void addElement(llu value, bool save=true) {
if (!m_primes3.empty() || value >= max_lu) m_primes3.push_back(value);
else if (!m_primes2.empty() || value >= max_u) m_primes2.push_back(value);
else m_primes1.push_back(value);
if (save && value >= m_next_save_value) {
save_primes();
}
}
void addVector(const std::vector<llu>& tmp_vector) {
for (const auto& element : tmp_vector) {
addElement(element);
}
}
size_t size() const {
return m_primes1.size() + m_primes2.size() + m_primes3.size();
}
llu getElement(size_t index) const {
if (index < m_primes1.size()) { return m_primes1[index]; }
else if (index - m_primes1.size() < m_primes2.size()) { return m_primes2[index - m_primes1.size()]; }
else if (index - m_primes1.size() - m_primes2.size() < m_primes3.size()) { return m_primes3[index - m_primes1.size() - m_primes2.size()]; }
std::cout << "Problem " << index << " redused " << index - m_primes1.size() - m_primes2.size() << ":" << m_primes1.size() << ":" << m_primes2.size() << ":" << m_primes3.size() << std::endl;
throw std::out_of_range("Index out of range");
}
llu getBack() const {
return getElement(size() - 1);
}
Primes transfer_until(llu number) {
Primes primes_sub;
for (size_t i = 0; i < size(); i++) {
llu prime = getElement(i);
// std::cout << "222 " << i << "of" << number << ":" << prime << std::endl;
if (prime > number) { break; }
primes_sub.addElement(prime, false);
}
//std::cout << "000 " << primes_sub.size() << "-" << size() << std::endl;
return primes_sub;
}
Primes transfer_calc_until(llu number) {
Primes primes_sub=transfer_until(sqrt(number + 1));
// if (primes_sub.size() < size()) {
// // just to be save
// primes_sub.addElement(getElement(primes_sub.size()), false);
// }
return primes_sub;
}
void read_primes() {
std::ifstream file(m_primes_filename);
if (!file.is_open()) {
std::cout << "No stored primes yet." << std::endl;
addElement(2);
addElement(3);
addElement(5);
return;
}
std::cout << "Reading primes ... ";
std::string line;
while (std::getline(file, line)) {
try {
llu num = std::stoull(line);
addElement(num, false);
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid integer: " << line << std::endl;
} catch (const std::out_of_range& e) {
std::cerr << "Integer out of range: " << line << std::endl;
}
}
file.close();
std::cout << "Read " << size() << " Primes until " << getBack() << std::endl;
m_next_save_index = size();
m_next_save_value = getBack() + step_to_save;
}
void save_primes() {
std::ofstream file(m_primes_filename, std::ios::app);
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return;
}
for (size_t i = m_next_save_index; i < size(); i++) {
// to_string + "\n" is much faster then getElement(i) << endl or std::to_string(getElement(i)) << endl
file << std::to_string(getElement(i)) + "\n";
}
file.close();
std::cout << "Saved " << size() - m_next_save_index << " Primes between " << getElement(m_next_save_index) << " and " << getBack();
std::cout << " (at index " << m_next_save_index << " and " << size() - 1 << " (starting from 0))" << std::endl;
m_next_save_index = size();
m_next_save_value = getBack() + step_to_save;
}
private:
// vectors have the disadvantage that things will break as soon as the vector is read in one thread and modified in another
std::vector<unsigned> m_primes1; // only 6542 primes
std::vector<unsigned long int> m_primes2; // 203273679 (2E8) primes
std::vector<llu> m_primes3;
const llu max_lu = pow(2, 32);
const llu max_u = pow(2, 16);
const std::string m_primes_filename="primes.txt";
size_t m_next_save_index = 0;
llu m_next_save_value = step_to_save;
};
void calc_primes(Primes primes_local, llu start, unsigned long int range, std::vector<llu>& primes_tmp, unsigned& done) {
llu max_prime = start + range;
std::cout << start << "-" << max_prime << "-" << primes_local.size() << ":" << primes_local.getBack() << std::endl;
llu max_test_prime = sqrt(max_prime) + 1;
size_t primes_size = primes_local.size();
//bool is_prime; // definition inside or outside of the loop doesn't matter
//llu prime;
for (llu i = start+2; i <= max_prime; i+=2) {
bool is_prime = true;
for (size_t j = 0; j < primes_size; j++) {
llu prime = primes_local.getElement(j);
if (i%prime == 0) {
is_prime = false;
break;
}
if (prime > max_test_prime) {
break;
}
}
/* This is quite a bit slower
unsigned long int j = 0;
while (++j < primes_size && primes_local.getElement(j) < max_test_prime) { // start with the second prime (3)
if (i%primes_local.getElement(j) == 0) {
is_prime = false;
break;
}
} */
//std::cout << start << "-" << i << "-" << is_prime << std::endl;
if (is_prime) {
primes_tmp.push_back(i);
}
}
//std::cout << start << "+" << max_prime << ":" << primes_tmp.size() << "(" << primes_tmp[0] << "-" << primes_tmp.back() << ")" << std::endl;
done = 1;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
std::cout << "Please start with the maximum potential prime to be calculated and the steps to split into" << std::endl;
return 1;
}
llu max_prime = (llu)std::stold(argv[1]);
llu step = (llu)std::stold(argv[2]);
Primes primes;
const unsigned int num_threads = std::thread::hardware_concurrency(); // Get the number of CPU threads
primes.read_primes();
if (primes.size() > 0 && primes.getBack() >= max_prime) {
std::cout << "Nothing to do";
return 0;
}
// Prepare enough primes to run in parallel
while (primes.getBack() < (llu)sqrt(10*num_threads*step)) {
unsigned done(false);
std::vector<llu> primes_in_progress;
llu last = primes.getBack();
calc_primes(primes, last, (primes.getBack() > (int)sqrt(step)) ? step : last*last, primes_in_progress, done);
primes.addVector(primes_in_progress);
}
Worker workers[num_threads];
std::vector<std::vector<llu>> primes_in_progress(num_threads);
std::vector<unsigned> threads_finished(num_threads); // can't use bool because of std::ref
std::vector<int> work_in_progress(num_threads);
llu start = primes.getBack();
for (auto& worker : workers){
worker.start();
}
for (unsigned int i = 0; i < num_threads; ++i) {
// don't run the result code before starting the threads
threads_finished[i] = 2;
}
unsigned long int next_index_to_start = 0;
unsigned long int next_index_to_insert = 0;
std::vector<std::vector<llu>> primes_to_insert;
std::vector<unsigned long int> primes_to_insert_index;
while (next_index_to_insert < next_index_to_start || next_index_to_start == 0) {
//std::this_thread::sleep_for(100);
sleep(0.1);
for (unsigned int i = 0; i < num_threads; ++i) {
//std::cout << i << " ";
bool start_work = false;
if (threads_finished[i] == 1) {
//std::cout << "thread " << i << " finished: " << std::endl;
if (work_in_progress[i] == next_index_to_insert) {
// This was the next bit of work, add to primes directly
//std::cout << "transfer " << primes_in_progress[i].size() << " entries to " << primes.size() << " entries" << std::endl;
primes.addVector(primes_in_progress[i]);
next_index_to_insert++;
// if (!primes_to_insert.empty()) {
// std::cout << "next stored:" << primes_to_insert_index[0] << "-" << next_index_to_insert << std::endl;
// }
while (!primes_to_insert.empty() && primes_to_insert_index[0] == next_index_to_insert) {
// Is the next set of primes already available to insert
//std::cout << "transfer " << primes_to_insert[0].size() << " entries to " << primes.size() << " entries" << std::endl;
primes.addVector(primes_to_insert[0]);
primes_to_insert.erase(primes_to_insert.begin());
primes_to_insert_index.erase(primes_to_insert_index.begin());
next_index_to_insert++;
}
}
else { // We can't add these primes yet, as previous threads have not yet finished -> store them for now
bool added = false;
for (unsigned int j = 0; j < primes_to_insert_index.size(); ++j) {
if (work_in_progress[i] < primes_to_insert_index[j]) {
primes_to_insert_index.insert(primes_to_insert_index.begin() + j, work_in_progress[i]);
primes_to_insert.insert(primes_to_insert.begin() + j, primes_in_progress[i]);
added = true;
break;
}
}
if (!added) {
primes_to_insert_index.insert(primes_to_insert_index.end(), work_in_progress[i]);
primes_to_insert.insert(primes_to_insert.end(), primes_in_progress[i]);
}
// for (auto& index : primes_to_insert_index) {
// std::cout << index << ".";
// }
// std::cout << std::endl;
}
start_work = true;
} else if (threads_finished[i] == 2) {
start_work = true;
}
if (start_work) {
threads_finished[i] = 0;
primes_in_progress[i].clear();
//threads[i] = std::thread(calc_primes, std::ref(primes), start, step, std::ref(primes_in_progress.back()), std::ref(threads_finished[i]));
if (start < max_prime){
Primes primes_part = primes.transfer_calc_until(start + step);
workers[i].push_task(calc_primes, primes_part, start, step, std::ref(primes_in_progress[i]), std::ref(threads_finished[i]));
work_in_progress[i] = next_index_to_start++;
start += step;
} else {
std::cout << "thread " << i << " not started" << std::endl;
}
}
}
}
primes.save_primes();
std::cout << "max: " << primes.getBack() << ", entries: " << primes.size() << ", cores:" << num_threads << std::endl;
return 0; // Return success
}
/*
g++ -o primes_cpp_v4 primes_cpp_v4.cpp && rm primes.txt ; time ./primes_cpp_v4 10E6 5E5
real 0m1.230s 0m1.200s 0m1.285s
user 0m6.443s 0m6.456s 0m6.540s
-> quicker due to multithreading? Step doesn't change result much. Using only primes3 doesn't change result
*/