-
Notifications
You must be signed in to change notification settings - Fork 19
/
Stella.hpp
453 lines (418 loc) · 47.6 KB
/
Stella.hpp
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
// (c) 2017-present Pttn and contributors (https://riecoin.xyz/rieMiner)
#ifndef HEADER_Stella_hpp
#define HEADER_Stella_hpp
#include <array>
#include <atomic>
#include <cassert>
#include <cmath>
#include <condition_variable>
#include <deque>
#include <fstream>
#include <gmpxx.h>
#include <iomanip>
#include <iostream>
#include <memory>
#include <mutex>
#include <numeric>
#include <optional>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#ifdef __SSE2__
#include <immintrin.h>
#ifdef __AVX2__
#include "ispc/fermat.h"
#endif
#endif
using namespace std::chrono_literals;
using namespace std::string_literals;
#define leading0s(x) std::setw(x) << std::setfill('0')
#define FIXED(x) std::fixed << std::setprecision(x)
#define primeTableFile "PrimeTable64.bin"
namespace Stella {
class SysInfo {
std::string _os, _cpuArchitecture, _cpuBrand;
uint64_t _physicalMemory;
bool _avx, _avx2, _avx512;
public:
SysInfo();
std::string getOs() const {return _os;}
uint64_t getPhysicalMemory() const {return _physicalMemory;}
std::string getCpuArchitecture() const {return _cpuArchitecture;}
std::string getCpuBrand() const {return _cpuBrand;}
bool hasAVX() const {return _avx;}
bool hasAVX2() const {return _avx2;}
bool hasAVX512() const {return _avx512;}
};
inline SysInfo sysInfo;
static const std::vector<std::pair<std::vector<uint64_t>, std::vector<uint64_t>>> defaultConstellationData = {
// 1-tuples
{{0}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645 (12-tuples)
// 2-tuples
{{0, 2}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645 (12-tuples)
// 3-tuples
{{0, 2, 4}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645 (12-tuples)
{{0, 4, 2}, {1418575498573ULL, 27899359258003ULL, 34460918582323ULL, 76075560855373ULL, 186460616596333ULL, 218021188549243ULL, 234280497145543ULL, 282854319391723ULL, 345120905374093ULL, 346117552180633ULL, 604439135284063ULL, 727417501795063ULL, 1041814617748753ULL, 1090754719898923ULL, 1539765965257753ULL, 3152045700948223ULL, 3323127757029313ULL, 3449427485143873ULL, 4422879865247923ULL, 4525595253334003ULL, 4730773080017833ULL, 5462875671033013ULL, 6147764065076713ULL, 6205707895751443ULL, 6308411019731053ULL, 7582919852522863ULL, 7791180222409663ULL, 9162887985581563ULL, 9305359177794913ULL, 10096106139749863ULL, 10349085616714693ULL, 10744789916260633ULL, 10932016019429353ULL, 11140102475962693ULL, 12448240792011103ULL, 14727257011031413ULL, 16892267700442213ULL, 17963729763800053ULL, 18908121647739403ULL, 19028992697498863ULL, 19756696515786463ULL, 20252223877980943ULL, 20429666791949263ULL, 21680774776901473ULL, 21682173462980263ULL, 23076668788453513ULL, 24036602580170413ULL, 24101684579532793ULL, 25053289894907353ULL, 25309078073142943ULL, 25662701041982083ULL, 25777719656829373ULL, 26056424604564433ULL, 26315911419972253ULL, 26866456999592443ULL, 26887571851660753ULL, 27303559129791793ULL, 27839080743588193ULL, 28595465291933773ULL, 29137316070747733ULL, 30824439453812083ULL, 31395828815154883ULL, 31979851757518513ULL, 32897714831936803ULL}}, // OEIS A213601 (12-tuples) + 6
// 4-tuples
{{0, 2, 4, 2}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645
// 5-tuples
{{0, 2, 4, 2, 4}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645 (12-tuples)
{{0, 4, 2, 4, 2}, {1418575498597ULL, 27899359258027ULL, 34460918582347ULL, 76075560855397ULL, 186460616596357ULL, 218021188549267ULL, 234280497145567ULL, 282854319391747ULL, 345120905374117ULL, 346117552180657ULL, 604439135284087ULL, 727417501795087ULL, 1041814617748777ULL, 1090754719898947ULL, 1539765965257777ULL, 3152045700948247ULL, 3323127757029337ULL, 3449427485143897ULL, 4422879865247947ULL, 4525595253334027ULL, 4730773080017857ULL, 5462875671033037ULL, 6147764065076737ULL, 6205707895751467ULL, 6308411019731077ULL, 7582919852522887ULL, 7791180222409687ULL, 9162887985581587ULL, 9305359177794937ULL, 10096106139749887ULL, 10349085616714717ULL, 10744789916260657ULL, 10932016019429377ULL, 11140102475962717ULL, 12448240792011127ULL, 14727257011031437ULL, 16892267700442237ULL, 17963729763800077ULL, 18908121647739427ULL, 19028992697498887ULL, 19756696515786487ULL, 20252223877980967ULL, 20429666791949287ULL, 21680774776901497ULL, 21682173462980287ULL, 23076668788453537ULL, 24036602580170437ULL, 24101684579532817ULL, 25053289894907377ULL, 25309078073142967ULL, 25662701041982107ULL, 25777719656829397ULL, 26056424604564457ULL, 26315911419972277ULL, 26866456999592467ULL, 26887571851660777ULL, 27303559129791817ULL, 27839080743588217ULL, 28595465291933797ULL, 29137316070747757ULL, 30824439453812107ULL, 31395828815154907ULL, 31979851757518537ULL, 32897714831936827ULL}}, // OEIS A213601 (12-tuples) + 30
// 6-tuples
{{0, 4, 2, 4, 2, 4}, {1091257ULL, 1615837ULL, 1954357ULL, 2822707ULL, 2839927ULL, 3243337ULL, 3400207ULL, 6005887ULL, 6503587ULL, 7187767ULL, 7641367ULL, 8061997ULL, 8741137ULL, 10526557ULL, 11086837ULL, 11664547ULL, 14520547ULL, 14812867ULL, 14834707ULL, 14856757ULL, 16025827ULL, 16094707ULL, 18916477ULL, 19197247ULL, 19634047ULL, 19800367ULL, 20112217ULL, 20247037ULL, 21321187ULL, 21850177ULL, 22587277ULL, 24786397ULL, 25009417ULL, 25524127ULL, 27305557ULL, 29153557ULL, 31563937ULL, 31875577ULL, 32836747ULL, 33575947ULL, 36319177ULL, 36985297ULL, 37055647ULL, 40660717ULL, 41214067ULL, 41763427ULL, 41927437ULL, 44842867ULL, 45974557ULL, 47204737ULL, 48660247ULL, 49157737ULL, 50685907ULL, 50943787ULL, 51255637ULL, 53204857ULL, 53266387ULL, 55057897ULL, 56431927ULL, 57812467ULL, 59877397ULL, 61052347ULL, 62757967ULL, 63655717ULL}}, // OEIS A022013
// 7-tuples
{{0, 2, 4, 2, 4, 6, 2}, {114023297140211ULL, 114189340938131ULL, 114355384736051ULL, 114521428533971ULL, 145799289681161ULL, 145844141558231ULL, 145888993435301ULL, 145933845312371ULL, 220452326319761ULL, 220469307413891ULL, 220486288508021ULL, 220503269602151ULL, 235290683530361ULL, 235367427477641ULL, 235444171424921ULL, 235520915372201ULL, 294911989246991ULL, 295129811972111ULL, 295347634697231ULL, 295565457422351ULL, 345279548696891ULL, 345390797864921ULL, 345502047032951ULL, 345613296200981ULL, 353247262660751ULL, 353395658218061ULL, 353544053775371ULL, 353692449332681ULL, 393391977659951ULL, 393554263173341ULL, 393716548686731ULL, 393878834200121ULL, 437171043550511ULL, 437359375794611ULL, 437547708038711ULL, 437736040282811ULL, 547374026909441ULL, 547566452933231ULL, 547758878957021ULL, 547951304980811ULL, 624475220164331ULL, 624728375797061ULL, 624981531429791ULL, 625234687062521ULL, 727343907387941ULL, 727564067888501ULL, 727784228389061ULL, 728004388889621ULL, 770217042177041ULL, 770410293094901ULL, 770603544012761ULL, 770796794930621ULL, 942173182708601ULL, 942393875821241ULL, 942614568933881ULL, 942835262046521ULL, 987033700014821ULL, 987192352739051ULL, 987351005463281ULL, 987509658187511ULL, 992287041033131ULL, 992305282156031ULL, 992323523278931ULL, 992341764401831ULL, 1196266900300931ULL, 1196331655791731ULL, 1196396411282531ULL, 1196461166773331ULL, 1363806213708551ULL, 1363923696714191ULL, 1364041179719831ULL, 1364158662725471ULL, 1504145477032811ULL, 1504390303845041ULL, 1504635130657271ULL, 1504879957469501ULL, 1506606424567241ULL, 1506633383579321ULL, 1506660342591401ULL, 1506687301603481ULL, 1547277513006761ULL, 1547538621604511ULL, 1547799730202261ULL, 1548060838800011ULL, 1551543396451661ULL, 1551708809259221ULL, 1551874222066781ULL, 1552039634874341ULL, 1554535163864141ULL, 1554630942026621ULL, 1554726720189101ULL, 1554822498351581ULL}}, // 10-tuples in groups of 4, valid up to primorial max 1400, difficulty around 2190.
{{0, 2, 6, 4, 2, 4, 2}, {226193845148639ULL, 226374233346629ULL, 226554621544619ULL, 226735009742609ULL, 301570107719129ULL, 301713410008259ULL, 301856712297389ULL, 302000014586519ULL, 443216221842839ULL, 443355092154419ULL, 443493962465999ULL, 443632832777579ULL, 594353915119409ULL, 594554524107989ULL, 594755133096569ULL, 594955742085149ULL, 602484187049189ULL, 602527797215789ULL, 602571407382389ULL, 602615017548989ULL, 620850037212269ULL, 621267059407859ULL, 621684081603449ULL, 622101103799039ULL, 631774346860829ULL, 632202123099719ULL, 632629899338609ULL, 633057675577499ULL, 638409170551289ULL, 638417212765469ULL, 638425254979649ULL, 638433297193829ULL, 652691407502999ULL, 652768901389469ULL, 652846395275939ULL, 652923889162409ULL, 715627356082349ULL, 715866860827829ULL, 716106365573309ULL, 716345870318789ULL, 751355932060829ULL, 751594040050949ULL, 751832148041069ULL, 752070256031189ULL, 847107141137429ULL, 847125608236079ULL, 847144075334729ULL, 847162542433379ULL, 850067287283309ULL, 850306665932819ULL, 850546044582329ULL, 850785423231839ULL, 963140918896139ULL, 963391264321469ULL, 963641609746799ULL, 963891955172129ULL, 1322395947238349ULL, 1322624640823469ULL, 1322853334408589ULL, 1323082027993709ULL, 1426366146255299ULL, 1426606782441089ULL, 1426847418626879ULL, 1427088054812669ULL, 1450867151696099ULL, 1450935595771799ULL, 1451004039847499ULL, 1451072483923199ULL}}, // 10-tuples + 12 in groups of 4, valid up to primorial max 1400, difficulty around 2190.
// 8-tuples
{{0, 2, 4, 2, 4, 6, 2, 6}, {114023297140211ULL, 114189340938131ULL, 114355384736051ULL, 114521428533971ULL, 145799289681161ULL, 145844141558231ULL, 145888993435301ULL, 145933845312371ULL, 220452326319761ULL, 220469307413891ULL, 220486288508021ULL, 220503269602151ULL, 235290683530361ULL, 235367427477641ULL, 235444171424921ULL, 235520915372201ULL, 294911989246991ULL, 295129811972111ULL, 295347634697231ULL, 295565457422351ULL, 345279548696891ULL, 345390797864921ULL, 345502047032951ULL, 345613296200981ULL, 353247262660751ULL, 353395658218061ULL, 353544053775371ULL, 353692449332681ULL, 393391977659951ULL, 393554263173341ULL, 393716548686731ULL, 393878834200121ULL, 437171043550511ULL, 437359375794611ULL, 437547708038711ULL, 437736040282811ULL, 547374026909441ULL, 547566452933231ULL, 547758878957021ULL, 547951304980811ULL, 624475220164331ULL, 624728375797061ULL, 624981531429791ULL, 625234687062521ULL, 727343907387941ULL, 727564067888501ULL, 727784228389061ULL, 728004388889621ULL, 770217042177041ULL, 770410293094901ULL, 770603544012761ULL, 770796794930621ULL, 942173182708601ULL, 942393875821241ULL, 942614568933881ULL, 942835262046521ULL, 987033700014821ULL, 987192352739051ULL, 987351005463281ULL, 987509658187511ULL, 992287041033131ULL, 992305282156031ULL, 992323523278931ULL, 992341764401831ULL, 1196266900300931ULL, 1196331655791731ULL, 1196396411282531ULL, 1196461166773331ULL, 1363806213708551ULL, 1363923696714191ULL, 1364041179719831ULL, 1364158662725471ULL, 1504145477032811ULL, 1504390303845041ULL, 1504635130657271ULL, 1504879957469501ULL, 1506606424567241ULL, 1506633383579321ULL, 1506660342591401ULL, 1506687301603481ULL, 1547277513006761ULL, 1547538621604511ULL, 1547799730202261ULL, 1548060838800011ULL, 1551543396451661ULL, 1551708809259221ULL, 1551874222066781ULL, 1552039634874341ULL, 1554535163864141ULL, 1554630942026621ULL, 1554726720189101ULL, 1554822498351581ULL}}, // 10-tuples in groups of 4, valid up to primorial max 1400, difficulty around 2190.
{{0, 2, 4, 6, 2, 6, 4, 2}, {1418575498577ULL, 27899359258007ULL, 34460918582327ULL, 76075560855377ULL, 186460616596337ULL, 218021188549247ULL, 234280497145547ULL, 282854319391727ULL, 345120905374097ULL, 346117552180637ULL, 604439135284067ULL, 727417501795067ULL, 1041814617748757ULL, 1090754719898927ULL, 1539765965257757ULL, 3152045700948227ULL, 3323127757029317ULL, 3449427485143877ULL, 4422879865247927ULL, 4525595253334007ULL, 4730773080017837ULL, 5462875671033017ULL, 6147764065076717ULL, 6205707895751447ULL, 6308411019731057ULL, 7582919852522867ULL, 7791180222409667ULL, 9162887985581567ULL, 9305359177794917ULL, 10096106139749867ULL, 10349085616714697ULL, 10744789916260637ULL, 10932016019429357ULL, 11140102475962697ULL, 12448240792011107ULL, 14727257011031417ULL, 16892267700442217ULL, 17963729763800057ULL, 18908121647739407ULL, 19028992697498867ULL, 19756696515786467ULL, 20252223877980947ULL, 20429666791949267ULL, 21680774776901477ULL, 21682173462980267ULL, 23076668788453517ULL, 24036602580170417ULL, 24101684579532797ULL, 25053289894907357ULL, 25309078073142947ULL, 25662701041982087ULL, 25777719656829377ULL, 26056424604564437ULL, 26315911419972257ULL, 26866456999592447ULL, 26887571851660757ULL, 27303559129791797ULL, 27839080743588197ULL, 28595465291933777ULL, 29137316070747737ULL, 30824439453812087ULL, 31395828815154887ULL, 31979851757518517ULL, 32897714831936807ULL}}, // OEIS A213601 (12-tuples) + 10
{{0, 6, 2, 6, 4, 2, 4, 2}, {226193845148633ULL, 226374233346623ULL, 226554621544613ULL, 226735009742603ULL, 301570107719123ULL, 301713410008253ULL, 301856712297383ULL, 302000014586513ULL, 443216221842833ULL, 443355092154413ULL, 443493962465993ULL, 443632832777573ULL, 594353915119403ULL, 594554524107983ULL, 594755133096563ULL, 594955742085143ULL, 602484187049183ULL, 602527797215783ULL, 602571407382383ULL, 602615017548983ULL, 620850037212263ULL, 621267059407853ULL, 621684081603443ULL, 622101103799033ULL, 631774346860823ULL, 632202123099713ULL, 632629899338603ULL, 633057675577493ULL, 638409170551283ULL, 638417212765463ULL, 638425254979643ULL, 638433297193823ULL, 652691407502993ULL, 652768901389463ULL, 652846395275933ULL, 652923889162403ULL, 715627356082343ULL, 715866860827823ULL, 716106365573303ULL, 716345870318783ULL, 751355932060823ULL, 751594040050943ULL, 751832148041063ULL, 752070256031183ULL, 847107141137423ULL, 847125608236073ULL, 847144075334723ULL, 847162542433373ULL, 850067287283303ULL, 850306665932813ULL, 850546044582323ULL, 850785423231833ULL, 963140918896133ULL, 963391264321463ULL, 963641609746793ULL, 963891955172123ULL, 1322395947238343ULL, 1322624640823463ULL, 1322853334408583ULL, 1323082027993703ULL, 1426366146255293ULL, 1426606782441083ULL, 1426847418626873ULL, 1427088054812663ULL, 1450867151696093ULL, 1450935595771793ULL, 1451004039847493ULL, 1451072483923193ULL}}, // 10-tuples + 6 in groups of 4, valid up to primorial max 1400, difficulty around 2190.
// 9-tuples
{{0, 2, 4, 2, 4, 6, 2, 6, 4}, {114023297140211ULL, 114189340938131ULL, 114355384736051ULL, 114521428533971ULL, 145799289681161ULL, 145844141558231ULL, 145888993435301ULL, 145933845312371ULL, 220452326319761ULL, 220469307413891ULL, 220486288508021ULL, 220503269602151ULL, 235290683530361ULL, 235367427477641ULL, 235444171424921ULL, 235520915372201ULL, 294911989246991ULL, 295129811972111ULL, 295347634697231ULL, 295565457422351ULL, 345279548696891ULL, 345390797864921ULL, 345502047032951ULL, 345613296200981ULL, 353247262660751ULL, 353395658218061ULL, 353544053775371ULL, 353692449332681ULL, 393391977659951ULL, 393554263173341ULL, 393716548686731ULL, 393878834200121ULL, 437171043550511ULL, 437359375794611ULL, 437547708038711ULL, 437736040282811ULL, 547374026909441ULL, 547566452933231ULL, 547758878957021ULL, 547951304980811ULL, 624475220164331ULL, 624728375797061ULL, 624981531429791ULL, 625234687062521ULL, 727343907387941ULL, 727564067888501ULL, 727784228389061ULL, 728004388889621ULL, 770217042177041ULL, 770410293094901ULL, 770603544012761ULL, 770796794930621ULL, 942173182708601ULL, 942393875821241ULL, 942614568933881ULL, 942835262046521ULL, 987033700014821ULL, 987192352739051ULL, 987351005463281ULL, 987509658187511ULL, 992287041033131ULL, 992305282156031ULL, 992323523278931ULL, 992341764401831ULL, 1196266900300931ULL, 1196331655791731ULL, 1196396411282531ULL, 1196461166773331ULL, 1363806213708551ULL, 1363923696714191ULL, 1364041179719831ULL, 1364158662725471ULL, 1504145477032811ULL, 1504390303845041ULL, 1504635130657271ULL, 1504879957469501ULL, 1506606424567241ULL, 1506633383579321ULL, 1506660342591401ULL, 1506687301603481ULL, 1547277513006761ULL, 1547538621604511ULL, 1547799730202261ULL, 1548060838800011ULL, 1551543396451661ULL, 1551708809259221ULL, 1551874222066781ULL, 1552039634874341ULL, 1554535163864141ULL, 1554630942026621ULL, 1554726720189101ULL, 1554822498351581ULL}}, // 10-tuples in groups of 4, valid up to primorial max 1400, difficulty around 2190.
{{0, 2, 4, 6, 2, 6, 4, 2, 4}, {1418575498577ULL, 27899359258007ULL, 34460918582327ULL, 76075560855377ULL, 186460616596337ULL, 218021188549247ULL, 234280497145547ULL, 282854319391727ULL, 345120905374097ULL, 346117552180637ULL, 604439135284067ULL, 727417501795067ULL, 1041814617748757ULL, 1090754719898927ULL, 1539765965257757ULL, 3152045700948227ULL, 3323127757029317ULL, 3449427485143877ULL, 4422879865247927ULL, 4525595253334007ULL, 4730773080017837ULL, 5462875671033017ULL, 6147764065076717ULL, 6205707895751447ULL, 6308411019731057ULL, 7582919852522867ULL, 7791180222409667ULL, 9162887985581567ULL, 9305359177794917ULL, 10096106139749867ULL, 10349085616714697ULL, 10744789916260637ULL, 10932016019429357ULL, 11140102475962697ULL, 12448240792011107ULL, 14727257011031417ULL, 16892267700442217ULL, 17963729763800057ULL, 18908121647739407ULL, 19028992697498867ULL, 19756696515786467ULL, 20252223877980947ULL, 20429666791949267ULL, 21680774776901477ULL, 21682173462980267ULL, 23076668788453517ULL, 24036602580170417ULL, 24101684579532797ULL, 25053289894907357ULL, 25309078073142947ULL, 25662701041982087ULL, 25777719656829377ULL, 26056424604564437ULL, 26315911419972257ULL, 26866456999592447ULL, 26887571851660757ULL, 27303559129791797ULL, 27839080743588197ULL, 28595465291933777ULL, 29137316070747737ULL, 30824439453812087ULL, 31395828815154887ULL, 31979851757518517ULL, 32897714831936807ULL}}, // OEIS A213601 (12-tuples) + 10
{{0, 4, 2, 4, 6, 2, 6, 4, 2}, {1418575498573ULL, 27899359258003ULL, 34460918582323ULL, 76075560855373ULL, 186460616596333ULL, 218021188549243ULL, 234280497145543ULL, 282854319391723ULL, 345120905374093ULL, 346117552180633ULL, 604439135284063ULL, 727417501795063ULL, 1041814617748753ULL, 1090754719898923ULL, 1539765965257753ULL, 3152045700948223ULL, 3323127757029313ULL, 3449427485143873ULL, 4422879865247923ULL, 4525595253334003ULL, 4730773080017833ULL, 5462875671033013ULL, 6147764065076713ULL, 6205707895751443ULL, 6308411019731053ULL, 7582919852522863ULL, 7791180222409663ULL, 9162887985581563ULL, 9305359177794913ULL, 10096106139749863ULL, 10349085616714693ULL, 10744789916260633ULL, 10932016019429353ULL, 11140102475962693ULL, 12448240792011103ULL, 14727257011031413ULL, 16892267700442213ULL, 17963729763800053ULL, 18908121647739403ULL, 19028992697498863ULL, 19756696515786463ULL, 20252223877980943ULL, 20429666791949263ULL, 21680774776901473ULL, 21682173462980263ULL, 23076668788453513ULL, 24036602580170413ULL, 24101684579532793ULL, 25053289894907353ULL, 25309078073142943ULL, 25662701041982083ULL, 25777719656829373ULL, 26056424604564433ULL, 26315911419972253ULL, 26866456999592443ULL, 26887571851660753ULL, 27303559129791793ULL, 27839080743588193ULL, 28595465291933773ULL, 29137316070747733ULL, 30824439453812083ULL, 31395828815154883ULL, 31979851757518513ULL, 32897714831936803ULL}}, // OEIS A213601 (12-tuples) + 6
{{0, 4, 6, 2, 6, 4, 2, 4, 2}, {226193845148629ULL, 226374233346619ULL, 226554621544609ULL, 226735009742599ULL, 301570107719119ULL, 301713410008249ULL, 301856712297379ULL, 302000014586509ULL, 443216221842829ULL, 443355092154409ULL, 443493962465989ULL, 443632832777569ULL, 594353915119399ULL, 594554524107979ULL, 594755133096559ULL, 594955742085139ULL, 602484187049179ULL, 602527797215779ULL, 602571407382379ULL, 602615017548979ULL, 620850037212259ULL, 621267059407849ULL, 621684081603439ULL, 622101103799029ULL, 631774346860819ULL, 632202123099709ULL, 632629899338599ULL, 633057675577489ULL, 638409170551279ULL, 638417212765459ULL, 638425254979639ULL, 638433297193819ULL, 652691407502989ULL, 652768901389459ULL, 652846395275929ULL, 652923889162399ULL, 715627356082339ULL, 715866860827819ULL, 716106365573299ULL, 716345870318779ULL, 751355932060819ULL, 751594040050939ULL, 751832148041059ULL, 752070256031179ULL, 847107141137419ULL, 847125608236069ULL, 847144075334719ULL, 847162542433369ULL, 850067287283299ULL, 850306665932809ULL, 850546044582319ULL, 850785423231829ULL, 963140918896129ULL, 963391264321459ULL, 963641609746789ULL, 963891955172119ULL, 1322395947238339ULL, 1322624640823459ULL, 1322853334408579ULL, 1323082027993699ULL, 1426366146255289ULL, 1426606782441079ULL, 1426847418626869ULL, 1427088054812659ULL, 1450867151696089ULL, 1450935595771789ULL, 1451004039847489ULL, 1451072483923189ULL}}, // 10-tuples + 2 in groups of 4, valid up to primorial max 1400, difficulty around 2190.
// 10-tuples
{{0, 2, 4, 2, 4, 6, 2, 6, 4, 2}, {114023297140211ULL, 114189340938131ULL, 114355384736051ULL, 114521428533971ULL, 145799289681161ULL, 145844141558231ULL, 145888993435301ULL, 145933845312371ULL, 220452326319761ULL, 220469307413891ULL, 220486288508021ULL, 220503269602151ULL, 235290683530361ULL, 235367427477641ULL, 235444171424921ULL, 235520915372201ULL, 294911989246991ULL, 295129811972111ULL, 295347634697231ULL, 295565457422351ULL, 345279548696891ULL, 345390797864921ULL, 345502047032951ULL, 345613296200981ULL, 353247262660751ULL, 353395658218061ULL, 353544053775371ULL, 353692449332681ULL, 393391977659951ULL, 393554263173341ULL, 393716548686731ULL, 393878834200121ULL, 437171043550511ULL, 437359375794611ULL, 437547708038711ULL, 437736040282811ULL, 547374026909441ULL, 547566452933231ULL, 547758878957021ULL, 547951304980811ULL, 624475220164331ULL, 624728375797061ULL, 624981531429791ULL, 625234687062521ULL, 727343907387941ULL, 727564067888501ULL, 727784228389061ULL, 728004388889621ULL, 770217042177041ULL, 770410293094901ULL, 770603544012761ULL, 770796794930621ULL, 942173182708601ULL, 942393875821241ULL, 942614568933881ULL, 942835262046521ULL, 987033700014821ULL, 987192352739051ULL, 987351005463281ULL, 987509658187511ULL, 992287041033131ULL, 992305282156031ULL, 992323523278931ULL, 992341764401831ULL, 1196266900300931ULL, 1196331655791731ULL, 1196396411282531ULL, 1196461166773331ULL, 1363806213708551ULL, 1363923696714191ULL, 1364041179719831ULL, 1364158662725471ULL, 1504145477032811ULL, 1504390303845041ULL, 1504635130657271ULL, 1504879957469501ULL, 1506606424567241ULL, 1506633383579321ULL, 1506660342591401ULL, 1506687301603481ULL, 1547277513006761ULL, 1547538621604511ULL, 1547799730202261ULL, 1548060838800011ULL, 1551543396451661ULL, 1551708809259221ULL, 1551874222066781ULL, 1552039634874341ULL, 1554535163864141ULL, 1554630942026621ULL, 1554726720189101ULL, 1554822498351581ULL}}, // 10-tuples in groups of 4, valid up to primorial max 1400, difficulty around 2190.
{{0, 2, 4, 6, 2, 6, 4, 2, 4, 2}, {226193845148627ULL, 226374233346617ULL, 226554621544607ULL, 226735009742597ULL, 301570107719117ULL, 301713410008247ULL, 301856712297377ULL, 302000014586507ULL, 443216221842827ULL, 443355092154407ULL, 443493962465987ULL, 443632832777567ULL, 594353915119397ULL, 594554524107977ULL, 594755133096557ULL, 594955742085137ULL, 602484187049177ULL, 602527797215777ULL, 602571407382377ULL, 602615017548977ULL, 620850037212257ULL, 621267059407847ULL, 621684081603437ULL, 622101103799027ULL, 631774346860817ULL, 632202123099707ULL, 632629899338597ULL, 633057675577487ULL, 638409170551277ULL, 638417212765457ULL, 638425254979637ULL, 638433297193817ULL, 652691407502987ULL, 652768901389457ULL, 652846395275927ULL, 652923889162397ULL, 715627356082337ULL, 715866860827817ULL, 716106365573297ULL, 716345870318777ULL, 751355932060817ULL, 751594040050937ULL, 751832148041057ULL, 752070256031177ULL, 847107141137417ULL, 847125608236067ULL, 847144075334717ULL, 847162542433367ULL, 850067287283297ULL, 850306665932807ULL, 850546044582317ULL, 850785423231827ULL, 963140918896127ULL, 963391264321457ULL, 963641609746787ULL, 963891955172117ULL, 1322395947238337ULL, 1322624640823457ULL, 1322853334408577ULL, 1323082027993697ULL, 1426366146255287ULL, 1426606782441077ULL, 1426847418626867ULL, 1427088054812657ULL, 1450867151696087ULL, 1450935595771787ULL, 1451004039847487ULL, 1451072483923187ULL}}, // 10-tuples in groups of 4, valid up to primorial max 1400, difficulty around 2190.
// 11-tuples
{{0, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645 (12-tuples)
{{0, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2}, {1418575498573ULL, 27899359258003ULL, 34460918582323ULL, 76075560855373ULL, 186460616596333ULL, 218021188549243ULL, 234280497145543ULL, 282854319391723ULL, 345120905374093ULL, 346117552180633ULL, 604439135284063ULL, 727417501795063ULL, 1041814617748753ULL, 1090754719898923ULL, 1539765965257753ULL, 3152045700948223ULL, 3323127757029313ULL, 3449427485143873ULL, 4422879865247923ULL, 4525595253334003ULL, 4730773080017833ULL, 5462875671033013ULL, 6147764065076713ULL, 6205707895751443ULL, 6308411019731053ULL, 7582919852522863ULL, 7791180222409663ULL, 9162887985581563ULL, 9305359177794913ULL, 10096106139749863ULL, 10349085616714693ULL, 10744789916260633ULL, 10932016019429353ULL, 11140102475962693ULL, 12448240792011103ULL, 14727257011031413ULL, 16892267700442213ULL, 17963729763800053ULL, 18908121647739403ULL, 19028992697498863ULL, 19756696515786463ULL, 20252223877980943ULL, 20429666791949263ULL, 21680774776901473ULL, 21682173462980263ULL, 23076668788453513ULL, 24036602580170413ULL, 24101684579532793ULL, 25053289894907353ULL, 25309078073142943ULL, 25662701041982083ULL, 25777719656829373ULL, 26056424604564433ULL, 26315911419972253ULL, 26866456999592443ULL, 26887571851660753ULL, 27303559129791793ULL, 27839080743588193ULL, 28595465291933773ULL, 29137316070747733ULL, 30824439453812083ULL, 31395828815154883ULL, 31979851757518513ULL, 32897714831936803ULL}}, // OEIS A213601 (12-tuples) + 6
// 12-tuples
{{0, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 6}, {380284918609481ULL, 437163765888581ULL, 701889794782061ULL, 980125031081081ULL, 1277156391416021ULL, 1487854607298791ULL, 1833994713165731ULL, 2115067287743141ULL, 2325810733931801ULL, 3056805353932061ULL, 3252606350489381ULL, 3360877662097841ULL, 3501482688249431ULL, 3595802556731501ULL, 3843547642594391ULL, 5000014653723821ULL, 5861268883004651ULL, 7486645325734691ULL, 7933248530182091ULL, 8760935349271991ULL, 8816939536219931ULL, 8871465225933041ULL, 9354490866900911ULL, 11096062107205961ULL, 11096434547374061ULL, 13764730155211151ULL, 13884748604026031ULL, 17438667992681051ULL, 20362378935668501ULL, 20471700514990841ULL, 20475715985020181ULL, 20614750499829371ULL, 21465425387541251ULL, 21628360938574121ULL, 21817283854511261ULL, 22238558064758921ULL, 22318056296221571ULL, 22733842556089781ULL, 22849881428489231ULL, 23382987892499351ULL, 23417442472403711ULL, 25964083184094941ULL, 26515897161980111ULL, 29512383574028471ULL, 30074756036270831ULL, 30310618347929651ULL, 30402250951007051ULL, 30413977411117031ULL, 33502273017038711ULL, 33508988966488151ULL, 33976718302847051ULL, 34783522781262371ULL, 37564605737538611ULL, 37606024583356961ULL, 39138758504100371ULL, 40205947750578341ULL, 40257009922154141ULL, 40392614725338761ULL, 40504121267225501ULL, 41099072498143391ULL, 41289201480321911ULL, 41543933848913381ULL, 42218492028808211ULL, 43938526447515431ULL}}, // OEIS A213645
{{0, 6, 4, 2, 4, 6, 2, 6, 4, 2, 4, 2}, {1418575498567ULL, 27899359257997ULL, 34460918582317ULL, 76075560855367ULL, 186460616596327ULL, 218021188549237ULL, 234280497145537ULL, 282854319391717ULL, 345120905374087ULL, 346117552180627ULL, 604439135284057ULL, 727417501795057ULL, 1041814617748747ULL, 1090754719898917ULL, 1539765965257747ULL, 3152045700948217ULL, 3323127757029307ULL, 3449427485143867ULL, 4422879865247917ULL, 4525595253333997ULL, 4730773080017827ULL, 5462875671033007ULL, 6147764065076707ULL, 6205707895751437ULL, 6308411019731047ULL, 7582919852522857ULL, 7791180222409657ULL, 9162887985581557ULL, 9305359177794907ULL, 10096106139749857ULL, 10349085616714687ULL, 10744789916260627ULL, 10932016019429347ULL, 11140102475962687ULL, 12448240792011097ULL, 14727257011031407ULL, 16892267700442207ULL, 17963729763800047ULL, 18908121647739397ULL, 19028992697498857ULL, 19756696515786457ULL, 20252223877980937ULL, 20429666791949257ULL, 21680774776901467ULL, 21682173462980257ULL, 23076668788453507ULL, 24036602580170407ULL, 24101684579532787ULL, 25053289894907347ULL, 25309078073142937ULL, 25662701041982077ULL, 25777719656829367ULL, 26056424604564427ULL, 26315911419972247ULL, 26866456999592437ULL, 26887571851660747ULL, 27303559129791787ULL, 27839080743588187ULL, 28595465291933767ULL, 29137316070747727ULL, 30824439453812077ULL, 31395828815154877ULL, 31979851757518507ULL, 32897714831936797ULL}} // OEIS A213601
};
template<class T> class TsQueue {
std::deque<T> _q;
std::mutex _m;
std::condition_variable _cv;
public:
void push_back(T item) {
std::unique_lock<std::mutex> lock(_m);
_q.push_back(item);
_cv.notify_one();
}
void push_front(T item) {
std::unique_lock<std::mutex> lock(_m);
_q.push_front(item);
_cv.notify_one();
}
T blocking_pop_front() { // Blocks until an item is available to pop
std::unique_lock<std::mutex> lock(_m);
while (_q.empty())
_cv.wait(lock);
auto r(_q.front());
_q.pop_front();
return r;
}
bool try_pop_front(T& item) { // Pops the front and returns true if the queue isn't empty else returns false.
std::lock_guard<std::mutex> lock(_m);
if (_q.empty()) return false;
item = _q.front();
_q.pop_front();
return true;
}
typename std::deque<T>::size_type clear() { // Nonblocking - clears queue, returns number of items removed
std::unique_lock<std::mutex> lock(_m);
auto s(_q.size());
_q.clear();
return s;
}
uint32_t size() {
std::unique_lock<std::mutex> lock(_m);
return _q.size();
}
};
template <class C> std::string formatContainer(const C& container) {
std::ostringstream oss;
for (auto it(container.begin()) ; it < container.end() ; it++) {
oss << *it;
if (it != container.end() - 1) oss << ", ";
}
return oss.str();
}
// Parameters for the Stella Instance, auto tuned if left as below.
struct Configuration {
uint16_t threads{0}, sieveWorkers{0};
uint64_t primeTableLimit{0}, sieveBits{0}, sieveSize{0}, sieveWords{0}, sieveIterations{0};
std::vector<uint64_t> pattern{}, primorialOffsets{};
std::vector<bool> patternMin{};
uint32_t primeCountTarget{0}, primeCountMin{0};
uint64_t initialBits{0}, initialTargetBits{0};
};
// Stella Job, containing just the Target. To alter other parameters like the Pattern or Max Offset, the Instance must be reintialized.
// The flag allows to signal that current Jobs must be stopped (for example, a new Block found by the network obsoletes all previous Jobs).
struct Job {
bool clearPreviousJobs{false};
uint32_t id;
mpz_class target;
};
// Output produced by the Stella Instance with a Prime Number satisfying the desired properties.
struct Result {
uint32_t jobId;
uint16_t threadId;
mpz_class result;
uint32_t primeCount;
uint16_t primorialNumber;
uint64_t primorialFactor, primorialOffset;
};
inline mpz_class u64ToMpz(const uint64_t u64) {
mpz_class mpz;
mpz_import(mpz.get_mpz_t(), 1, 1, 8, 0, 0, &u64);
return mpz;
}
inline std::vector<mpz_class> v64ToVMpz(const std::vector<uint64_t> &v64) {
std::vector<mpz_class> vMpz;
for (const auto & n : v64)
vMpz.push_back(u64ToMpz(n));
return vMpz;
}
constexpr uint32_t sieveCacheSize(32);
constexpr uint32_t nWorks(2);
constexpr uint32_t maxCandidatesPerCheckTask(64);
// Create an Instance to generate Prime Constellations. It must be initialized with parameters appropriate for the Stella Jobs that will be worked on by the Instance.
// Altering the initial parameters require a reinitialization of the Instance (or making a new one).
class Instance {
#ifdef __SSE2__
union xmmreg_t {
uint32_t v[4];
uint64_t v64[2];
__m128i m128;
};
#ifdef __AVX2__
union ymmreg_t {
uint32_t v[8];
uint64_t v64[4];
__m256i m256;
};
#endif
#endif
struct Sieve {
uint32_t id;
std::mutex presieveLock;
uint64_t *factorsTable = nullptr; // Booleans corresponding to whether a primorial factor is eliminated
uint32_t *factorsToEliminate = nullptr; // One entry for each constellation offset, for each prime number p < factorMax (the factors are in the form of indexes of the factorsTable)
uint32_t **additionalFactorsToEliminate = nullptr; // Factors for p >= factorMax (they are eliminated only once and treated separately), arranged by Sieve Iteration (also in the form of indexes of the factorsTable)
std::atomic<uint64_t> *additionalFactorsToEliminateCounts = nullptr; // Counts for each Sieve Iteration
};
struct MinerWork {
bool current{true};
Stella::Job job;
mpz_class primorialMultipleStart{0}; // Smallest multiple of the primorial >= target.
std::atomic<uint64_t> nRemainingCheckTasks{0};
void clear() {
primorialMultipleStart = 0;
nRemainingCheckTasks = 0;
}
};
struct Task {
enum Type {Dummy, Presieve, Sieve, Check};
Type type;
uint64_t workIndex;
union {
struct {} dummy;
struct {
uint64_t start;
uint64_t end;
} presieve;
struct {
uint32_t id;
uint64_t iteration;
} sieve;
struct {
uint32_t offsetId;
uint32_t nCandidates;
uint32_t factorStart; // The form of a candidate is firstCandidate + primorial*f, with f = factorStart + factorOffset
std::array<uint32_t, maxCandidatesPerCheckTask> factorOffsets;
} check;
};
static Task PresieveTask(uint64_t workIndex, uint64_t start, uint64_t end) {
Task task;
task.type = Presieve;
task.workIndex = workIndex;
task.presieve.start = start;
task.presieve.end = end;
return task;
}
static Task SieveTask(uint64_t workIndex, uint32_t id, uint64_t iteration) {
Task task;
task.type = Sieve;
task.workIndex = workIndex;
task.sieve.id = id;
task.sieve.iteration = iteration;
return task;
}
};
// Used for task synchronization and balancing purposes like ensuring that all Presieve Task finished before generating Sieve Tasks.
struct TaskDoneInfo {
Task::Type type;
union {
uint64_t workIndex;
uint64_t firstPrimeIndex;
};
};
uint16_t _threads, _sieveWorkers;
std::thread _masterThread;
std::vector<std::thread> _workerThreads;
// Miner data (generated in init)
mpz_class _primorial;
uint64_t _primorialNumber, _primeTableLimit, _nPrimes, _nPrimes32, _factorMax, _primesIndexThreshold;
uint64_t _sieveBits, _sieveSize, _sieveWords, _sieveIterations;
std::vector<uint32_t> _primes32, _modularInverses32; // Use 32 bits Ints to save Memory
std::vector<uint64_t> _primes64, _modularInverses64;
#ifdef __SSE2__
std::vector<uint64_t> _modPrecompute;
#endif
std::vector<mpz_class> _primorialOffsets;
std::vector<uint64_t> _pattern, _patternCumulative, _halfPattern, _primorialOffsetsU64, _primorialOffsetDiff;
std::vector<bool> _patternMin;
uint16_t _primeCountTarget, _primeCountMin;
// Miner state variables
bool _inited, _running, _keepStats;
TsQueue<Job> _availableJobs;
TsQueue<Task> _presieveTasks, _tasks;
TsQueue<TaskDoneInfo> _tasksDoneInfos;
std::vector<Sieve> _sieves;
std::array<MinerWork, nWorks> _works; // Alternating work for better efficiency when there is a new block
uint32_t _nRemainingCheckTasksTarget, _currentWorkIndex;
std::chrono::microseconds _presieveTime, _sieveTime, _verifyTime;
// Stats and Results
bool _primeTableExtracted;
double _primeTableGenerationTime, _modularInversesGenerationTime;
std::vector<uint64_t> _tupleCounts;
std::vector<Result> _results;
std::mutex _countsLock, _resultMutex;
std::vector<std::string> _initMessages;
void _addResult(const Result &result) {
std::lock_guard<std::mutex> lock(_resultMutex);
_results.push_back(result);
}
void _addToSieveCache(uint64_t *sieve, std::array<uint32_t, sieveCacheSize> &sieveCache, uint64_t &pos, uint32_t ent) {
__builtin_prefetch(&(sieve[ent >> 6U]));
uint32_t old(sieveCache[pos]);
if (old != 0)
sieve[old >> 6U] |= (1ULL << (old & 63U));
sieveCache[pos] = ent;
pos++;
pos &= sieveCacheSize - 1;
}
void _endSieveCache(uint64_t *sieve, std::array<uint32_t, sieveCacheSize> &sieveCache) {
for (uint64_t i(0) ; i < sieveCacheSize ; i++) {
const uint32_t old(sieveCache[i]);
if (old != 0)
sieve[old >> 6U] |= (1ULL << (old & 63U));
}
}
void _addCachedAdditionalFactorsToEliminate(Sieve&, uint64_t*, uint64_t*, const int);
void _doPresieveTask(const Task&);
void _processSieve(uint64_t*, uint32_t*, const uint64_t, const uint64_t);
#ifdef __SSE2__
void _processSieve6(uint64_t*, uint32_t*, uint64_t, const uint64_t);
void _processSieve7(uint64_t*, uint32_t*, uint64_t, const uint64_t);
void _processSieve8(uint64_t*, uint32_t*, uint64_t, const uint64_t);
#ifdef __AVX2__
void _processSieve7_avx2(uint64_t*, uint32_t*, uint64_t, const uint64_t);
void _processSieve8_avx2(uint64_t*, uint32_t*, uint64_t, const uint64_t);
bool _testPrimesIspc(const std::array<uint32_t, maxCandidatesPerCheckTask>&, uint32_t[maxCandidatesPerCheckTask], const mpz_class&, mpz_class&);
#endif
#endif
void _doSieveTask(Task);
void _doCheckTask(Task);
void _doTasks(uint16_t);
void _manageTasks();
uint64_t _getPrime(uint64_t i) const {
if (i < _nPrimes32) return _primes32[i];
else return _primes64[i - _nPrimes32];
}
uint64_t _getModularInverse(uint64_t i) const {
if (i < _nPrimes32) return _modularInverses32[i];
else return _modularInverses64[i - _nPrimes32];
}
public:
Instance() :
_inited(false), _running(false), _keepStats(false) {
_nPrimes = 0;
_primesIndexThreshold = 0;
}
bool hasAcceptedPatterns(const std::vector<std::vector<uint64_t>>&) const;
void start(const Configuration &minerParameters) {
init(minerParameters);
startThreads();
}
void init(const Configuration&);
void addJob(const Job& job) {
if (!job.clearPreviousJobs)
_availableJobs.push_back(job);
else {
_availableJobs.clear();
_availableJobs.push_back(job);
invalidateWork();
}
}
uint64_t availableJobs() {return _availableJobs.size();}
void startThreads();
void invalidateWork();
void stop(const bool keepStats = true) {
_keepStats = keepStats;
if (_running) stopThreads();
if (_inited) clear();
}
void stopThreads();
void clear();
bool inited() {return _inited;}
bool running() {return _running;}
std::vector<Result> getResults() {
std::lock_guard<std::mutex> lock(_resultMutex);
auto results(_results);
_results.clear();
return results;
}
std::vector<uint64_t> getTupleCounts() {
std::lock_guard<std::mutex> lock(_countsLock);
return _tupleCounts;
}
std::string paramsStr() const {
if (_inited) {
std::string str;
str += "\tThreads: "s + std::to_string(_threads) + " ("s + std::to_string(_sieveWorkers) + " Sieve Worker(s))\n"s;
str += "\tConstellation pattern: n + ("s + formatContainer(_patternCumulative) + "), length "s + std::to_string(_pattern.size()) + "\n"s;
str += "\tPrime Table: "s + std::to_string(_nPrimes) + " entries, largest " + std::to_string(_getPrime(_nPrimes - 1ULL)) + "\n"s;
if (_primeTableExtracted)
str += "\t\tExtracted in "s + std::to_string(_primeTableGenerationTime) + " s\n"s;
else
str += "\t\tGenerated in "s + std::to_string(_primeTableGenerationTime) + " s\n"s;
str += "\t\tModular Inverses/Division Data generated in "s + std::to_string(_modularInversesGenerationTime) + " s\n"s;
str += "\tPrimorial: p"s + std::to_string(_primorialNumber) + "# = "s + std::to_string(_primes32[_primorialNumber - 1]) + "# = "s;
if (mpz_sizeinbase(_primorial.get_mpz_t(), 10) < 18)
str += _primorial.get_str();
else
str += "~"s + _primorial.get_str()[0] + "."s + _primorial.get_str().substr(1, 12) + "*10^"s + std::to_string(_primorial.get_str().size() - 1);
str += " ("s + std::to_string(mpz_sizeinbase(_primorial.get_mpz_t(), 2)) + " bits)\n"s;
str += "\tSieve Size: 2^"s + std::to_string(_sieveBits) + " = "s + std::to_string(_sieveSize) + " ("s + std::to_string(_sieveWords) + " words)\n"s;
str += "\tSieve Iterations: "s + std::to_string(_sieveIterations) + "\n"s;
str += "\tPrimorial Factor Max: "s + std::to_string(_sieveIterations*_sieveSize) + "\n"s;
str += "\tPrimorial Offsets: "s + std::to_string(_primorialOffsets.size()) + " entries\n"s;
str += "\tPrime index threshold: "s + std::to_string(_primesIndexThreshold) + "\n"s;
return str;
}
else
return "Instance was not initialized!"s;
}
std::vector<std::string> initMessages() const {
return _initMessages;
}
};
inline double timeSince(const std::chrono::time_point<std::chrono::steady_clock> &t0) {
const std::chrono::time_point<std::chrono::steady_clock> t(std::chrono::steady_clock::now());
const std::chrono::duration<double> dt(t - t0);
return dt.count();
}
std::vector<uint64_t> generatePrimeTable(const uint64_t);
std::string doubleToString(const double, const uint16_t = 0U);
std::string formattedCounts(const std::vector<uint64_t>&, const uint64_t = 0ULL);
std::string formattedTime(const double&);
std::string formattedClockTimeNow();
std::string formattedDuration(const double&);
}
#endif