-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprime.cpp
455 lines (387 loc) · 13.7 KB
/
prime.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <iostream>
#include <cmath>
#include <cassert>
#include "prime.h"
#include "networkcontroller.h"
#include "asyncPrimeSearching.h"
#include "primeSearchingUtilities.h"
#include "fileio.h"
inline size_t GetReservedSize(unsigned int BatchSize, mpz_ptr Candidate)
{
// Approximates ln(2).
typedef std::ratio<34657, 50000> ln2;
// ceil()s to the nearest 1000
return std::ceil(((BatchSize) / ((mpz_sizeinbase(Candidate, 2) * ln2::num) / ln2::den)) / 1000.0) * 1000;
}
// Yeah this is kind of ugly, Old-C style.
// Feel free to send a PR with a better way to do this.
// Maybe a Generator?
std::pair<mpz_class, int> Primebot::GenerateRandomOdd(unsigned int Bits, unsigned int Seed)
{
static gmp_randstate_t Rand = { 0 };
static unsigned int LastSeed = 0;
static std::atomic<int> RandomIterations(0);
int PreviousIteration;
if (Rand[0]._mp_algdata._mp_lc == nullptr)
{
gmp_randinit_mt(Rand);
}
if (LastSeed != Seed)
{
gmp_randseed_ui(Rand, Seed);
LastSeed = Seed;
RandomIterations = 0;
}
mpz_class Work;
mpz_urandomb(Work.get_mpz_t(), Rand, Bits);
PreviousIteration = RandomIterations.fetch_add(1);
if (mpz_even_p(Work.get_mpz_t()))
{
Work += 1;
}
return std::make_pair(std::move(Work), PreviousIteration + 1);
}
mpz_class Primebot::GetInitialWorkitem(const AllPrimebotSettings& Settings)
{
if (Settings.PrimeSettings.StartValue.empty())
{
auto RandomWork = GenerateRandomOdd(Settings.PrimeSettings.Bitsize, Settings.PrimeSettings.RngSeed);
return RandomWork.first;
}
else
{
return mpz_class(Settings.PrimeSettings.StartValue, Settings.PrimeSettings.StartValueBase);
}
}
std::vector<int> Primebot::DecomposeToPowersOfTwo(mpz_class Input)
{
// Convert the prime into a nicer list of powers of two?
mpz_class Candidate(Input);
std::vector<int> Powers;
unsigned int Base = 2;
while (mpz_cmp_ui(Candidate.get_mpz_t(), 1) >= 0)
{
unsigned int Exp = mpz_sizeinbase(Candidate.get_mpz_t(), Base);
mpz_class Test(Base);
mpz_pow_ui(Test.get_mpz_t(), Test.get_mpz_t(), Exp);
while (mpz_cmp(Test.get_mpz_t(), Candidate.get_mpz_t()) > 0)
{
// if the test power is greater, decrement Exp
// and regenerate Test;
Test = Base;
Exp--;
mpz_pow_ui(Test.get_mpz_t(), Test.get_mpz_t(), Exp);
}
Candidate -= Test;
Powers.push_back(Exp);
}
return Powers;
}
void Primebot::FindPrimeStandalone(mpz_class && workitem, int id, unsigned int BatchSize)
{
unsigned int Step = 2 * BatchSize * (Settings.PrimeSettings.ThreadCount - 1);
for (unsigned long long j = 0; j < Settings.PrimeSettings.BatchCount && !Quit; j++)
{
assert(mpz_odd_p(workitem.get_mpz_t()));
mpz_list Batch;
// This wastes some memory, but we'll never have to do a resize operation.
Batch.reserve(GetReservedSize(2 * BatchSize, workitem.get_mpz_t()));
// Miller-Rabin has 4^-n (or (1/4)^n if you prefer) probability that a
// composite number will pass the nth iteration of the test.
// The below selects the bit-length of the candidate prime, divided by 2
// as the number of iterations to perform.
// This has the property of putting the probability of a composite passing
// the test to be less than the count of possible numbers for a given bit-length.
// Or so I think...
int MillerRabinIterations = (mpz_sizeinbase(workitem.get_mpz_t(), 2) / 2);
// The increment of workitem in the last iteration is essential to making
// sure there are no gaps.
for (unsigned int i = 0; i < BatchSize && !Quit; i++, workitem += 2)
{
// Skip numbers ending in 5, since they cannot be prime, even if they are odd.
if (mpz_get_ui(workitem.get_mpz_t()) & 5 == 5) continue;
if (mpz_probab_prime_p(workitem.get_mpz_t(), MillerRabinIterations))
{
// this will invoke the copy constructor
Batch.emplace_back(workitem);
}
}
// If this thread has reported any results already
// wait until the last thread cleans up the results before inserting
// TODO: this could be optimized somehow...
while (!Results.load()->data()[id].empty())
{
std::this_thread::yield();
}
// Store results in shared array
// After this, Batch should be empty
Results.load()->data()[id].swap(Batch);
// At this point, Batch should be empty
assert(Batch.empty());
// Create new Results list in case we're the last thread and have to
// send results; this'll be cleaned up if unused.
std::unique_ptr<mpz_list_list> MyResults(new mpz_list_list(Settings.PrimeSettings.ThreadCount));
if (++ResultsCount % Settings.PrimeSettings.ThreadCount == 0)
{
// Swap the shared Results vector with the temp one
MyResults.reset(Results.exchange(MyResults.release()));
IoPool.EnqueueWorkItem(std::move(MyResults));
}
MyResults.reset(nullptr);
// move to the next batch
workitem += Step;
}
}
void Primebot::FindPrimesClient()
{
for (unsigned long long j = 0; j < Settings.PrimeSettings.BatchCount && !Quit; j++)
{
// Get workitem from server
ClientWorkitem Workitem = Controller->RequestWork(1);
while ((Workitem.Start.get_ui() == 0 || Workitem.Offset == 0 || Workitem.Id == 0) & !Quit)
{
// Failed to get work item, try again.
std::this_thread::sleep_for(std::chrono::seconds(1));
Workitem = Controller->RequestWork(1);
}
if ((Workitem.Start.get_ui() == 0 || Workitem.Offset == 0 || Workitem.Id == 0) && Quit)
{
// This lets clients that have just downloaded a workitem to
// complete it and prevent the server from hanging.
break;
}
mpz_list Batch;
// This wastes some memory, but we'll never have to do a resize operation.
Batch.reserve(GetReservedSize(Workitem.Offset, Workitem.Start.get_mpz_t()));
// Miller-Rabin has 4^-n (or (1/4)^n if you prefer) probability that a
// composite number will pass the nth iteration of the test.
// The below selects the bit-length of the candidate prime, divided by 2
// as the number of iterations to perform.
// This has the property of putting the probability of a composite passing
// the test to be less than the count of possible numbers for a given bit-length.
// Or so I think...
int MillerRabinIterations = (mpz_sizeinbase(Workitem.Start.get_mpz_t(), 2) / 2);
// The increment of workitem in the last iteration is essential to making
// sure there are no gaps.
// Note: this loop wont be canceled by Quit because it's guaranteed to
// always reach a terminal state, so it'll always return a full batch.
for (unsigned int i = 0; i < Workitem.Offset / 2; i++, Workitem.Start += 2)
{
// Skip numbers ending in 5, since they cannot be prime, even if they are odd.
if (mpz_get_ui(Workitem.Start.get_mpz_t()) & 5 == 5) continue;
if (mpz_probab_prime_p(Workitem.Start.get_mpz_t(), MillerRabinIterations))
{
Batch.emplace_back(Workitem.Start);
}
}
Controller->ReportWork(Workitem.Id, Batch);
}
// Abusing ResultsCount here to synchronize between completed threads.
if (++ResultsCount == Settings.PrimeSettings.ThreadCount)
{
// If the batch count is reached, call stop()
std::thread(&Primebot::Stop, this).detach();
}
}
void Primebot::ProcessOrReportResults(std::vector<mpz_class>& Results)
{
// Don't use network to report results
if (Controller == nullptr)
{
if (Settings.FileSettings.Path.empty())
{
for (auto& res : Results)
{
// print results to console
gmp_fprintf(stdout, "%Zd\n", res.get_mpz_t());
}
}
else
{
// Write out to disk here if file path present
FileIo.WritePrimes(Results);
}
}
}
// This will contend with the worker threads, but the OS thread
// scheduler should be smart enough to schedule the worker threads
// when this thread is waiting for I/O.
void Primebot::ProcessIo(std::unique_ptr<mpz_list_list> && data)
{
for (mpz_list& l : *data)
{
ProcessOrReportResults(l);
}
}
void Primebot::StartClient()
{
// Start listening for events from the server
Controller->Start();
while (!Controller->RegisterClient() & !Quit)
{
// Failed to register client, try again
std::this_thread::sleep_for(std::chrono::seconds(1));
}
// I/O pool unused in client mode.
IoPool.Stop();
if (Settings.PrimeSettings.UseAsync)
{
RunAsyncClient();
}
else
{
RunThreadsClient();
}
}
void Primebot::StartStandalone()
{
if (Settings.PrimeSettings.UseAsync)
{
RunAsyncStandalone();
}
else
{
RunThreadsStandalone();
}
}
void Primebot::RunAsyncClient()
{
for (unsigned long long i = 0; (i < Settings.PrimeSettings.BatchCount) & !Quit; i++)
{
// Get workitem from server
ClientWorkitem Workitem = Controller->RequestWork(Settings.PrimeSettings.ThreadCount);
while ((Workitem.Start.get_ui() == 0 || Workitem.Offset == 0 || Workitem.Id == 0) & !Quit)
{
// Failed to get work item, try again.
std::this_thread::sleep_for(std::chrono::seconds(1));
Workitem = Controller->RequestWork(Settings.PrimeSettings.ThreadCount);
}
if ((Workitem.Start.get_ui() == 0 || Workitem.Offset == 0 || Workitem.Id == 0) && Quit)
{
break;
}
// Initialize start and end for asynchronous work
mpz_class AsyncStart(Workitem.Start);
mpz_class AsyncEnd(AsyncStart);
AsyncEnd += Workitem.Offset;
Controller->ReportWork(Workitem.Id, findPrimes(Settings.PrimeSettings.ThreadCount, AsyncStart, AsyncEnd));
}
}
void Primebot::RunAsyncStandalone()
{
// Stand-alone mode, generate starting prime
mpz_class Start = GetInitialWorkitem(Settings);
// Async implementation
mpz_class AsyncStart(Start);
mpz_class AsyncEnd(AsyncStart);
// Make the range grow proportionally to the size of numbers being searched.
unsigned int RangeSize =
Settings.PrimeSettings.BatchSize
* Settings.PrimeSettings.ThreadCount
* mpz_sizeinbase(AsyncStart.get_mpz_t(), 2);
for (unsigned long long i = 0; (i < Settings.PrimeSettings.BatchCount) & !Quit; i++)
{
AsyncEnd += RangeSize;
std::unique_ptr<mpz_list_list> IoContainer(new mpz_list_list());
IoContainer->push_back(
findPrimes(Settings.PrimeSettings.ThreadCount, AsyncStart, AsyncEnd));
IoPool.EnqueueWorkItem(std::move(IoContainer));
// Move the start to the end of the range
AsyncStart = AsyncEnd;
}
}
void Primebot::RunThreadsClient()
{
for (unsigned int i = 0; i < Threads.size(); i++)
{
Threads[i] = std::move(std::thread(&Primebot::FindPrimesClient, this));
}
}
void Primebot::RunThreadsStandalone()
{
// Stand-alone mode, generate starting prime
mpz_class Start = GetInitialWorkitem(Settings);
// Make BatchSize grow proportionally to the size of the numbers being searched.
unsigned int BatchSize = Settings.PrimeSettings.BatchSize * mpz_sizeinbase(Start.get_mpz_t(), 2);
for (unsigned int i = 0; i < Threads.size(); i++)
{
// ((2 * i) + Start)
mpz_class work(Start);
work += (2 * i * BatchSize);
Threads[i] = std::move(std::thread(&Primebot::FindPrimeStandalone, this, std::move(work), i, BatchSize));
}
}
Primebot::Primebot(const AllPrimebotSettings& Config, NetworkController* NetController) :
Controller(NetController),
Settings(Config),
FileIo(Settings),
Threads(Config.PrimeSettings.ThreadCount),
IoPool(1,
std::bind(&Primebot::ProcessIo, this, std::placeholders::_1)),
Quit(false),
Results(new std::vector<mpz_list>(Settings.PrimeSettings.ThreadCount)),
ResultsCount(0)
{}
Primebot::~Primebot()
{
Stop();
}
void Primebot::Start()
{
mpz_class Start;
if (Settings.NetworkSettings.Client)
{
StartClient();
}
else
{
StartStandalone();
}
}
void Primebot::Stop()
{
if (!Quit)
{
// Stop running loops
Quit = true;
// Wait for threads to finish
for (auto& t : Threads)
{
if (t.joinable())
{
t.join();
}
}
Threads.clear();
// Drain remaining I/Os
IoPool.Stop();
// Signal to server that we're done
if (Controller != nullptr)
{
Controller->UnregisterClient();
}
// Notify main() that it's time to quit
{
std::unique_lock<std::mutex> lock(DoneLock);
Done.notify_all();
}
}
}
void Primebot::WaitForStop()
{
if (Settings.PrimeSettings.UseAsync)
{
//int dummy = 0;
//std::cout << "Type anything and press enter to exit.";
//std::cin >> dummy;
// Nothing to wait for, since the Async path runs in a loop that doesn't
// exit until Quit is set from CTRL+C/server shutdown message.
Stop();
}
else
{
std::unique_lock<std::mutex> lock(DoneLock);
// Wait to exit
Done.wait(lock);
}
}