-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverflow.cpp
More file actions
1058 lines (895 loc) · 36.3 KB
/
Copy pathoverflow.cpp
File metadata and controls
1058 lines (895 loc) · 36.3 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2026 Seth Troisi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "overflow.h"
#include <atomic>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <exception>
#include <fstream>
#include <iostream>
#include <mutex>
#include <unistd.h>
#include <utility>
// pthread_setname_np
#include <pthread.h>
#include <gmp.h>
#include <primesieve.hpp>
#include "gap_common.h"
#include "gpu_testing.h"
using std::cout;
using std::cerr;
using std::endl;
using namespace std::chrono;
/** Extern globals */
OverflowQueue overflow;
/** Tuning Parameters */
const uint32_t OVERFLOW_SIEVE_LIMIT = 150'000;
const bool EXTRA_CHECKS = false;
/** Globals for this class */
std::mutex record_mtx;
class TestingStats {
public:
std::atomic<uint64_t> tested = 0;
std::atomic<uint64_t> tested_cpu = 0;
std::atomic<uint64_t> tested_gpu = 0;
std::atomic<uint64_t> gpu_total_tests = 0;
// Set to the largest m computed during execution
std::atomic<uint64_t> max_m = 0;
std::atomic<uint64_t> skipped_prev = 0;
std::atomic<uint64_t> tested_prev = 0;
std::atomic<uint64_t> greater_than_min_merit = 0;
// GAP MISMATCH where next_prime had pow(2, n-1, n) == 1
std::atomic<uint64_t> pseudoprimes = 0;
// GAP MISMATCH where ^ is NOT TRUE.
std::atomic<uint64_t> mismatches = 0;
std::atomic<uint64_t> spot_checked = 0;
std::atomic<double> d_sieve{0.0};
std::atomic<double> d_next_prime_cpu{0.0};
std::atomic<double> d_next_prime_gpu{0.0};
std::atomic<double> d_next_prime_gpu_misc{0.0};
std::atomic<double> d_prev_prime_cpu{0.0};
std::atomic<double> d_spot_check{0.0};
};
class OverflowMisc {
public:
OverflowMisc(
vector<uint16_t> &cX,
vector<uint16_t> &cin,
vector<std::pair<uint32_t, uint32_t>> &panrs,
vector<std::pair<uint32_t, uint32_t>> &panr,
uint64_t d,
uint64_t kmd,
vector<uint16_t> &dw, vector<uint16_t> &dwn) :
coprime_X(cX), coprime_index_next(cin),
p_and_neg_r_small(panrs), p_and_neg_r(panr),
D(d), K_mod_d(kmd),
d_wheel(dw), d_wheel_next(dwn) {};
OverflowMisc() {};
vector<uint16_t> coprime_X;
// coprime_X[coprime_index_next[t]] >= t
vector<uint16_t> coprime_index_next;
vector<std::pair<uint32_t, uint32_t>> p_and_neg_r_small;
vector<std::pair<uint32_t, uint32_t>> p_and_neg_r;
uint64_t D;
uint64_t K_mod_d;
vector<uint16_t> d_wheel;
// d_wheel[d_wheel_next[t]] >= t
vector<uint16_t> d_wheel_next;
} ofs;
void setup_overflow(const struct Config config) {
vector<uint16_t> coprime_X;
vector<uint16_t> coprime_index_next;
vector<std::pair<uint32_t, uint32_t>> p_and_neg_r_small;
vector<std::pair<uint32_t, uint32_t>> p_and_neg_r;
mpz_t K;
init_K(config, K);
// 10 -> 3% overflow, 21K sieves / second
// 11 -> 1% overflow, 20K sieves / second
// 15 -> .5% overflow, 18K sieves / second
uint32_t stop_x = 11 * config.p;
{
auto X = get_coprime_X(config, stop_x);
coprime_X.reserve(X.size());
for (const auto x : X) {
coprime_X.push_back(x);
}
coprime_index_next.resize(coprime_X.back() + 1, 0xFFFF);
uint16_t i = 0;
for (uint16_t x_i = 0; x_i < coprime_X.size(); x_i++) {
for ( ; i <= coprime_X[x_i]; i++ ) {
coprime_index_next[i] = x_i;
}
}
for (i = 0; i < coprime_index_next.size(); i++) {
assert( coprime_X[coprime_index_next[i]] >= i );
}
}
uint64_t D = config.d;
uint64_t K_mod_d = mpz_fdiv_ui(K, D);
assert( 1 <= K_mod_d && K_mod_d < D );
vector<uint16_t> d_wheel;
vector<uint16_t> d_wheel_next;
{
assert(D < 65000); // Not as useful otherwise
for (uint32_t i = 1; i < D; i++) {
if (gcd(i, D) != 1)
d_wheel.push_back(i);
}
// Default value is d_wheel.size();
// Include one extra value so mod+1 is possible
d_wheel_next.resize(D+1, d_wheel.size());
uint16_t i = 0;
for (uint16_t d_i = 0; d_i < d_wheel.size(); d_i++) {
for ( ; i <= d_wheel[d_i]; i++ ) {
d_wheel_next[i] = d_i;
}
}
for (i = 0; i < d_wheel_next.size(); i++) {
uint16_t d_i = d_wheel_next[i];
assert( d_i == d_wheel.size() || d_wheel[d_wheel_next[i]] >= i );
}
}
{
primesieve::iterator iter;
uint64_t prime = iter.next_prime();
assert (prime == 2); // we skip 2 which is the oddest prime.
for (prime = iter.next_prime(); prime < OVERFLOW_SIEVE_LIMIT; prime = iter.next_prime()) {
// factors of D handled by d_wheel
if (prime <= config.p)
continue;
const uint32_t base_r = mpz_fdiv_ui(K, prime);
assert( 0 < base_r && base_r < prime );
const uint32_t neg_base_r = prime - base_r;
if (prime < stop_x) {
p_and_neg_r_small.emplace_back((uint32_t) prime, neg_base_r);
} else {
p_and_neg_r.emplace_back((uint32_t) prime, neg_base_r);
}
}
}
ofs = OverflowMisc{
coprime_X, coprime_index_next,
p_and_neg_r_small, p_and_neg_r,
D, K_mod_d, d_wheel, d_wheel_next};
mpz_clear(K);
}
static
void process_result(
const float min_merit,
const double K_log, const uint32_t P, const uint32_t D,
const mpz_t &K, mpz_t ¢er,
const uint64_t m,
double merit, uint64_t gap, uint64_t prev_gap,
mpz_t &next_p, mpz_t &prev_p,
mpz_t &tmp, mpz_t &tmp2,
TestingStats &stats,
std::ofstream &record_stream) {
if (merit > min_merit) {
stats.greater_than_min_merit++;
// Double check, we only performed a single round of rabin miller on many numbers.
mpz_mul_ui(center, K, m);
mpz_sub_ui(prev_p, center, prev_gap);
mpz_nextprime(next_p, prev_p);
mpz_sub(tmp, next_p, prev_p);
uint64_t test_gap = mpz_get_ui(tmp);
if (test_gap != gap) {
// These numbers are marked "prime" by GPU because we only do 1 round.
mpz_sub_ui(tmp, next_p, 1);
mpz_set_ui(tmp2, 2);
// Check if mismatch is Fermat pseudoprime base 2 <=> 2^(np-1) % np = 1
mpz_powm(tmp, tmp2, tmp, next_p);
if ( mpz_cmp_ui(tmp, 1) == 0) {
stats.pseudoprimes++;
printf("\tFermat Pseuodprime: %lu * %u# / %u + %lu\n",
m, P, D, test_gap - prev_gap);
} else {
stats.mismatches++;
gmp_printf("%Zd\n", next_p);
printf("\tGAP MISMATCH! %lu vs %lu at %lu * %u# / %u + %lu\n",
test_gap, gap, m, P, D, test_gap - prev_gap);
}
gap = test_gap;
merit = test_gap / (K_log + log(m));
}
if (merit > min_merit) {
std::string record = std::format(
"{} {:.3f} {} * {}# / {} - {}",
gap, merit, m, P, D, prev_gap);
cout << record << endl;
record_mtx.lock();
record_stream << record << endl;
record_stream.flush();
record_mtx.unlock();
}
}
}
/** Expects center to be correctly set */
static
void handle_next_prime_result(
const uint32_t MIN_GAP_TO_CONTINUE,
const float min_merit,
const double K_log, const uint32_t P, const uint32_t D,
const mpz_t &K, mpz_t ¢er,
const uint64_t m, const uint32_t next_gap,
mpz_t &next_p, mpz_t &prev_p,
mpz_t &tmp, mpz_t &tmp2,
TestingStats &stats,
std::ofstream &record_stream) {
if (next_gap < MIN_GAP_TO_CONTINUE) {
stats.skipped_prev++;
return;
}
stats.tested_prev++;
if (EXTRA_CHECKS) {
mpz_mul_ui(tmp2, K, m);
assert( mpz_cmp(tmp2, center) == 0);
}
auto s_start_t = high_resolution_clock::now();
mpz_prevprime(prev_p, center);
mpz_sub(prev_p, center, prev_p);
uint64_t prev_gap = mpz_get_ui(prev_p);
uint64_t gap = prev_gap + next_gap;
double merit = gap / (K_log + log(m));
stats.d_prev_prime_cpu += duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.tested_cpu += 1;
process_result(
min_merit, K_log, P, D, K, center,
m, merit, gap, prev_gap,
next_p, prev_p, tmp, tmp2, stats,
record_stream);
}
/**
* [sieve_start, sieve_start + sieve_length)
* If not is_positive [-sieve_start, -sieve_start - sieve_length)
*/
static
void sieve_interval_cpu(const uint64_t m,
const bool is_positive,
const uint32_t sieve_start,
const uint32_t sieve_length,
vector<uint8_t> &composite,
TestingStats &stats,
mpz_t &tmp, const mpz_t &K
) {
auto s_start_t = high_resolution_clock::now();
// TODO stop storing evens.
uint16_t bytes = (sieve_length + 7) / 8 + 1;
composite.resize(bytes, 0);
std::fill(composite.begin(), composite.end(), 0);
// only interested in even i
assert(sieve_start % 2 == 0);
// otherwise m * neg_r < sieve_start
assert(m > ofs.p_and_neg_r.back().first);
// Otherwise I need to do something different here
// Technically should check m * K_mod_d < 60 bits
assert(std::log2(m) + std::log2(ofs.p_and_neg_r.back().first) < 60);
uint64_t D = ofs.D;
// Tile d_wheel into composite with a possible offset
if (is_positive) {
uint64_t wheel_start = (m * ofs.K_mod_d + sieve_start) % D;
uint32_t w_n = ofs.d_wheel.size();
uint32_t w_i = ofs.d_wheel_next[wheel_start];
assert( w_i == w_n || ofs.d_wheel[w_i] >= wheel_start);
assert( w_i == 0 || ofs.d_wheel[w_i-1] < wheel_start);
// Technically wheel_start is always odd, but we only care about even t
// could only use odd valued d_wheel.
for (int32_t j = -wheel_start; j < (signed) sieve_length; ) {
for ( ; w_i < w_n; w_i++) {
int32_t t = j + ofs.d_wheel[w_i];
assert( t >= 0 );
if (t > (signed) sieve_length)
break;
composite[t >> 3] |= 1 << (t & 7);
}
j += D;
w_i = 0;
}
} else {
assert( sieve_start == 0);
uint64_t wheel_start = m * ofs.K_mod_d % D;
uint32_t w_n = ofs.d_wheel.size();
// Looking for first number <= wheel_start
int32_t w_i = ofs.d_wheel_next[wheel_start+1];
assert( w_i == (signed) w_n || ofs.d_wheel[w_i+1] > wheel_start);
w_i -= 1;
assert( w_i < 0 || ofs.d_wheel[w_i] <= wheel_start);
// composite[0] is wheel_start
// composite[1] is wheel_start - 1
for (int32_t j = wheel_start; j < (signed) sieve_length; ) {
for ( ; w_i >= 0; w_i--) {
int32_t t = j - ofs.d_wheel[w_i];
assert( t >= 0 );
if (t > (signed) sieve_length)
break;
composite[t >> 3] |= 1 << (t & 7);
if (EXTRA_CHECKS) {
mpz_mul_ui(tmp, K, m);
mpz_sub_ui(tmp, tmp, t);
if ( mpz_gcd_ui(NULL, tmp, D) == 1) {
printf("(%lu*K - %u, D) = 1 | %lu -> %d - %u\n",
m, t, wheel_start, j, ofs.d_wheel[w_i]);
}
}
}
j += D;
w_i = w_n - 1;
}
}
if (is_positive) {
for (const auto& [p, neg_r] : ofs.p_and_neg_r_small) {
// -(m * K + sieve_start) % r
uint64_t temp = m * neg_r - sieve_start;
uint64_t center_mod = temp % ((uint64_t) p);
center_mod += (center_mod & 1) ? p : 0;
uint32_t two_p = p << 1;
for (uint32_t i = center_mod; i < sieve_length; i += two_p) {
composite[i >> 3] |= 1 << (i & 7);
}
}
for (const auto& [p, neg_r] : ofs.p_and_neg_r) {
// -(m * K + sieve_start) % r
uint64_t temp = m * neg_r - sieve_start;
uint64_t center_mod = temp % ((uint64_t) p);
if (center_mod < sieve_length && (center_mod & 1) == 0) {
composite[center_mod >> 3] |= 1 << (center_mod & 7);
}
}
} else {
for (const auto& [p, neg_r] : ofs.p_and_neg_r_small) {
// (m * K - sieve_start) % r
uint64_t temp = m * (p - neg_r) + sieve_start;
uint64_t center_mod = temp % ((uint64_t) p);
center_mod += (center_mod & 1) ? p : 0;
uint32_t two_p = p << 1;
for (uint32_t i = center_mod; i < sieve_length; i += two_p) {
composite[i >> 3] |= 1 << (i & 7);
}
}
for (const auto& [p, neg_r] : ofs.p_and_neg_r) {
// (m * K - sieve_start) % r
uint64_t temp = m * (p - neg_r) + sieve_start;
uint64_t center_mod = temp % ((uint64_t) p);
if (center_mod < sieve_length && (center_mod & 1) == 0) {
composite[center_mod >> 3] |= 1 << (center_mod & 7);
}
}
}
double total_s = duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.d_sieve += total_s;
}
static
void run_tests_on_cpu(
const uint32_t MIN_GAP_TO_CONTINUE, const float min_merit,
const double K_log, const uint32_t P, const uint32_t D,
const uint64_t m, const uint64_t min_x,
const mpz_t &K, mpz_t ¢er,
mpz_t &next_p, mpz_t &prev_p,
mpz_t &tmp, mpz_t &tmp2,
vector<uint8_t> &composite_tmp,
TestingStats &stats,
std::ofstream &record_stream) {
auto s_start_t = high_resolution_clock::now();
uint64_t next_gap = 0;
mpz_mul_ui(center, K, m);
mpz_add_ui(tmp, center, min_x);
if (min_x + 500 < ofs.coprime_X.back()) {
uint32_t min_x_i = ofs.coprime_index_next[min_x];
uint32_t next_possible_x = ofs.coprime_X[min_x_i];
assert( 1 <= min_x_i && min_x_i < ofs.coprime_X.size() );
assert( min_x <= next_possible_x );
assert( ofs.coprime_X[min_x_i-1] < min_x );
sieve_interval_cpu(
m, true, next_possible_x, ofs.coprime_X.back() - next_possible_x + 1,
composite_tmp, stats, tmp, K);
const uint32_t N = ofs.coprime_X.size();
for (uint32_t x_i = min_x_i; x_i < N; x_i++) {
uint16_t x = ofs.coprime_X[x_i];
uint16_t j = x - next_possible_x;
if ((composite_tmp[j >> 3] & (1 << (j & 7))) == 0) {
mpz_add_ui(tmp, center, x);
if (mpz_probab_prime_p(tmp, 20)) {
next_gap = x;
break;
}
}
}
mpz_add_ui(tmp, center, ofs.coprime_X.back());
}
if (next_gap == 0) {
// Fallback to mpz_nextprime if very large
mpz_nextprime(tmp, tmp);
mpz_sub(tmp, tmp, center);
next_gap = mpz_get_ui(tmp);
}
double total_s = duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.d_next_prime_cpu += total_s;
stats.tested_cpu += 1;
handle_next_prime_result(
MIN_GAP_TO_CONTINUE, min_merit, K_log, P, D,
K, center,
m, next_gap,
next_p, prev_p, tmp, tmp2, stats, record_stream);
}
class OverflowBatch {
public:
const uint32_t N = 4096;
// TODO parametrize this number.
GPUBatch gpu_batch{N};
// Start looking for a non-active entry here
size_t i = 0;
size_t added = 0;
// m, current coprime_X index, sieve_start, next_gap
// if next_gap == 0, finding next_prime, if > 0 finding prev_prime
vector<std::tuple<uint64_t, uint16_t, uint16_t, uint16_t>> data;
// Optimized for less handling, could be 10x smaller by changing to bitset over coprime_x.
vector<vector<uint8_t>> composite_tmp;
OverflowBatch() {
size_t n = gpu_batch.m_i.size();
composite_tmp.resize(n);
data.resize(n);
}
void remove_entry(size_t j) {
gpu_batch.active[j] = 0;
added--;
if (j < i) {
i = j;
}
}
OverflowBatch(const OverflowBatch&) = delete;
OverflowBatch& operator=(const OverflowBatch&) = delete;
};
static
void push_to_overflow_batch(
OverflowBatch &overflow_batch,
const uint64_t m, const uint32_t min_x_i, const uint32_t sieve_start,
const mpz_t &K, mpz_t ¢er,
vector<uint8_t> &composite_tmp,
TestingStats &stats) {
auto s_start_t = high_resolution_clock::now();
GPUBatch &gpu_batch = overflow_batch.gpu_batch;
uint32_t i = overflow_batch.i; // start search here.
for (; i < overflow_batch.N; i++) {
if (gpu_batch.active[i] == 0) {
break;
}
}
mpz_mul_ui(center, K, m);
overflow_batch.i = i+1;
overflow_batch.added++;
assert(i < overflow_batch.N);
assert(gpu_batch.active[i] == 0);
overflow_batch.gpu_batch.active[i] = true;
mpz_add_ui(*overflow_batch.gpu_batch.z[i], center, sieve_start);
assert( sieve_start == ofs.coprime_X[min_x_i] );
overflow_batch.data[i] = {m, min_x_i, sieve_start, 0};
overflow_batch.composite_tmp[i].swap(composite_tmp);
double total_s = duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.d_next_prime_gpu_misc += total_s;
}
static
uint32_t run_overflow_batch(
GPURunner &runner,
OverflowBatch &overflow_batch,
const uint32_t MIN_GAP_TO_CONTINUE,
const float MIN_MERIT, const float K_log, const uint32_t P, const uint32_t D,
const mpz_t &K, mpz_t ¢er, mpz_t &next_p, mpz_t &prev_p,
mpz_t &tmp, mpz_t& tmp2,
TestingStats &stats, std::ofstream &record_stream) {
GPUBatch &gpu_batch = overflow_batch.gpu_batch;
for (uint32_t i = 0; i < overflow_batch.N; i++) {
// May need to disable for last batch
assert( gpu_batch.active[i] == 1 );
}
gpu_batch.i = overflow_batch.added;
std::fill(gpu_batch.result.begin(), gpu_batch.result.end(), -1);
auto s_start_t = high_resolution_clock::now();
// Run gpu_batch on GPU.
runner.run( gpu_batch );
stats.d_next_prime_gpu += duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.gpu_total_tests += overflow_batch.N;
// Process results.
s_start_t = high_resolution_clock::now();
uint32_t finished_items = 0;
for (uint32_t i = 0; i < overflow_batch.N; i++) {
assert( gpu_batch.active[i] );
assert (gpu_batch.result[i] == 0 || gpu_batch.result[i] == 1);
auto [m, x_i, sieve_start, next_gap] = overflow_batch.data[i];
uint32_t prev_gap = 0;
auto is_next_prime = (m > 0);
m = is_next_prime ? m : -m;
bool remove = false;
bool change_to_prev = false;
bool process = false;
if (gpu_batch.result[i] == 1) {
// Found prime for m!
stats.tested_gpu++;
if (next_gap == 0) {
change_to_prev = true;
next_gap = ofs.coprime_X[x_i];
if (EXTRA_CHECKS) {
mpz_mul_ui(center, K, m);
mpz_add_ui(tmp, center, next_gap);
assert( mpz_cmp(tmp, *gpu_batch.z[i]) == 0 );
}
} else {
remove = true;
process = true;
prev_gap = ofs.coprime_X[x_i];
if (EXTRA_CHECKS) {
mpz_mul_ui(center, K, m);
mpz_sub_ui(tmp, center, prev_gap);
assert( mpz_cmp(tmp, *gpu_batch.z[i]) == 0 );
}
}
} else {
// Advance to next test see `next_prime_distance`
uint32_t last_x = ofs.coprime_X[x_i];
uint32_t M = ofs.coprime_X.size();
x_i++;
for (; x_i < M; x_i++) {
uint16_t x = ofs.coprime_X[x_i];
uint16_t j = x - sieve_start;
if ((overflow_batch.composite_tmp[i][j >> 3] & (1 << (j & 7))) == 0) {
if (next_gap == 0) {
mpz_add_ui(*gpu_batch.z[i], *gpu_batch.z[i], x - last_x);
} else {
mpz_sub_ui(*gpu_batch.z[i], *gpu_batch.z[i], x - last_x);
}
// Write back updated x_i;
std::get<1>(overflow_batch.data[i]) = x_i;
break;
}
}
if (x_i >= M) {
uint32_t min_x = ofs.coprime_X.back() + 1;
if (next_gap == 0) {
change_to_prev = true;
auto s_start_t = high_resolution_clock::now();
// Fallback to mpz_nextprime when > coprime_X[-1]
mpz_mul_ui(center, K, m);
mpz_add_ui(tmp, center, min_x);
mpz_nextprime(tmp, tmp);
mpz_sub(tmp, tmp, center);
next_gap = mpz_get_ui(tmp);
double total_s = duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.d_next_prime_cpu += total_s;
stats.tested_cpu += 1;
} else {
process = true;
remove = true;
auto s_start_t = high_resolution_clock::now();
// Fallback to mpz_prevprime when > coprime_X[-1]
mpz_mul_ui(center, K, m);
mpz_sub_ui(tmp, center, min_x);
mpz_prevprime(tmp, tmp);
mpz_sub(tmp, center, tmp);
prev_gap = mpz_get_ui(tmp);
double total_s = duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.d_next_prime_cpu += total_s;
stats.tested_cpu += 1;
}
}
}
if (process) {
assert( remove );
assert( next_gap > 0 && prev_gap > 0 );
uint64_t gap = prev_gap + next_gap;
double merit = gap / (K_log + log(m));
if (EXTRA_CHECKS) {
mpz_mul_ui(center, K, m);
mpz_sub_ui(tmp, center, prev_gap);
assert( mpz_probab_prime_p(tmp, 20) );
mpz_add_ui(tmp, center, next_gap);
assert( mpz_probab_prime_p(tmp, 20) );
}
stats.tested_prev++;
process_result(
MIN_MERIT, K_log, P, D, K, center,
m, merit, gap, prev_gap,
next_p, prev_p, tmp, tmp2, stats, record_stream);
}
if (change_to_prev) {
assert( !remove );
if (next_gap < MIN_GAP_TO_CONTINUE) {
stats.skipped_prev++;
remove = true;
} else {
uint32_t x_0 = ofs.coprime_X.front();
sieve_interval_cpu(
m, false, 0, ofs.coprime_X.back() + 1,
overflow_batch.composite_tmp[i], stats, tmp, K);
// Reset this range to be prev_prime search
overflow_batch.data[i] = {m, 0, 0, next_gap};
mpz_mul_ui(center, K, m);
mpz_sub_ui(*gpu_batch.z[i], center, x_0);
}
}
if (remove) {
overflow_batch.remove_entry(i);
finished_items++;
}
}
stats.d_next_prime_gpu_misc += duration<double>(high_resolution_clock::now() - s_start_t).count();
//printf("\tRan overflow on GPU found: %lu primes\n", overflow_batch.N - overflow_batch.added);
return finished_items;
}
void run_cpu_overflow_worker(const int thread_index,
const struct Config og_config,
TestingStats &stats) {
{
std::string name = std::format("CPU_WORKER_{}", thread_index);
pthread_setname_np(pthread_self(), name.c_str());
std::ignore = nice(+10);
}
mpz_t K, center, next_p, prev_p, tmp, tmp2;
mpz_inits(center, next_p, prev_p, tmp, tmp2, NULL);
const uint32_t P = og_config.p;
const uint32_t D = og_config.d;
if (thread_index == 0) {
prob_prime_and_stats(og_config, K);
} else {
init_K(og_config, K);
}
double K_log = _log(K);
const float MIN_MERIT = og_config.min_merit;
// 2-5x what comes in per batch
const uint64_t overflow_too_much = og_config.m_inc * og_config.cpu_fraction;
// See THEORY.md! +1 is optimal-ish +1.XX is small preference for doing less prev_p.
const float MIN_MERIT_TO_CONTINUE = 1.95 + std::log2(MIN_MERIT * std::log(2) + 1);
const float m_log = log(og_config.m_start + og_config.m_inc);
const uint32_t MIN_GAP_TO_CONTINUE = MIN_MERIT_TO_CONTINUE * (K_log + m_log);
if (thread_index == 0 && og_config.verbose >= 1) {
setlocale(LC_NUMERIC, "");
// ----- Merit / Sieve stats
float m_log = log(og_config.m_inc);
printf("Min Gap ~= %'d (for merit > %.1f)\n",
(int) (MIN_MERIT * (K_log + m_log)), MIN_MERIT);
printf("Min Gap to continue ~= %'d (merit = %.1f)\n",
MIN_GAP_TO_CONTINUE, MIN_MERIT_TO_CONTINUE);
setlocale(LC_NUMERIC, "C");
}
std::ofstream record_stream(std::format("records_{}.txt", P), std::ios_base::app);
GPURunner runner{};
OverflowBatch overflow_batch{};
vector<uint8_t> composite_tmp;
uint64_t max_m = 0;
while (is_running) {
// Wait till size is not zero
overflow.size.wait(0);
if (!is_running) {
break;
}
overflow.lock();
if (overflow.size == 0) {
overflow.unlock();
continue; // Might have been removed while locking.
}
if (stats.tested % 100'000 == 0 && overflow.size > overflow_too_much) {
printf("\tCPU Sieve Queue is behind: %u open, %lu processed\n",
overflow.size.load(), stats.tested.load());
}
// Maybe move this to the helper?
if (stop_queue > 0) {
uint32_t rem = overflow.size;
bool is_power_print = false;
for (uint64_t p = 1000; p <= rem; p *= 10) {
is_power_print |= (rem == p) || (rem == 2*p) || (rem == 5*p);
}
if (is_power_print) {
printf("\tFinalizing(stage %d): %u open, %lu processed\n",
stop_queue.load(), rem,
stats.tested.load());
}
}
assert( overflow.queue.size() == overflow.size );
auto [m, d, type] = overflow.queue.front(); overflow.queue.pop_front();
overflow.size--;
if ( type == Overflow::Type::NEXT_PRIME ) {
// Do this immediatly to prevent prints (above) with same tested value.
stats.tested++;
}
overflow.unlock();
if ( type == Overflow::Type::STOP_WORKER ) {
break;
}
// Handle SPOT_CHECK DIRECTLY
if ( type == Overflow::Type::SPOT_CHECK ) {
uint32_t x = d;
stats.spot_checked++;
mpz_mul_ui(center, K, m);
mpz_add_ui(next_p, center, x);
auto s_start_t = high_resolution_clock::now();
if (!mpz_probab_prime_p(next_p, 20)) {
printf("\n\n");
printf("%lu'th SPOT CHECK FAILED!\n", stats.spot_checked.load());
printf("%lu * %u# / %u + %u is not prime!\n",
m, P, D, x);
printf("\n\n");
exit(1);
}
double total_s = duration<double>(high_resolution_clock::now() - s_start_t).count();
stats.d_spot_check += total_s;
continue;
}
if (m > max_m) max_m = m;
assert( type == Overflow::Type::NEXT_PRIME );
// stats.tested++ moved above.
auto min_x = d;
// This would mean that stop_x was poorly tuned.
assert(min_x + 500 < ofs.coprime_X.back());
{ // Sieve and push that to overflow batch
uint32_t min_x_i = ofs.coprime_index_next[min_x];
uint32_t sieve_start = ofs.coprime_X[min_x_i];
assert( min_x <= sieve_start);
sieve_interval_cpu(
m, true, sieve_start, ofs.coprime_X.back() - sieve_start + 1,
composite_tmp, stats, tmp, K);
push_to_overflow_batch(
overflow_batch,
m, min_x_i, sieve_start,
K, center,
composite_tmp, stats);
if (overflow_batch.added == overflow_batch.N) {
for (size_t i = 0; i < 30; i++) {
auto finished = run_overflow_batch(
runner,
overflow_batch,
MIN_GAP_TO_CONTINUE, MIN_MERIT,
K_log, P, D,
K, center, next_p, prev_p, tmp, tmp2,
stats, record_stream);
if (finished) break;
printf("GPUBatch didn't fully process any numbers?\n");
}
}
}
}
if (is_running) {
assert( (signed) overflow.size < og_config.cpu_threads ); // should contain only STOP_WORKER items
if (overflow_batch.added) {
// Clear out any remaining items queued in GPUBatch
if (og_config.verbose >= 1) {
printf("\tCPU overflow finishing %lu remaining items in GPUBatch\n", overflow_batch.added);
}
// Slightly akward to run partial batches so handle on CPU.
GPUBatch &gpu_batch = overflow_batch.gpu_batch;
for (uint32_t i = 0; i < overflow_batch.N; i++) {
if (!gpu_batch.active[i])
continue;
{ // Run each remaining row of GPUBatch manually
auto& [m, x_i, sieve_start, next_gap] = overflow_batch.data[i];
if (next_gap == 0) {
uint32_t next_x = ofs.coprime_X[x_i];
uint32_t min_x = next_x;
run_tests_on_cpu(
MIN_GAP_TO_CONTINUE, MIN_MERIT,
K_log, P, D,
m, min_x,
K, center, next_p, prev_p, tmp, tmp2, composite_tmp, stats, record_stream);
} else {
// Ignores partially computed prev_prime.
mpz_mul_ui(center, K, m);
handle_next_prime_result(
MIN_GAP_TO_CONTINUE, MIN_MERIT,
K_log, P, D,
K, center,
m, next_gap,
next_p, prev_p, tmp, tmp2, stats, record_stream);
}
}
overflow_batch.remove_entry(i);
}
}
}
overflow.lock();
stats.max_m = std::max(stats.max_m.load(), max_m);
overflow.unlock();
mpz_clears(K, center, next_p, prev_p, tmp, tmp2, NULL);
if (og_config.verbose >= 3) {
usleep(thread_index * 10'000); // X0ms
printf("\tCPU overflow(%u) done\n", thread_index);
}
}
uint32_t USE_GPU_FOR_OVERFLOW = true;
void run_overflow_coordinator_thread(const struct Config og_config) {
try {
{
pthread_setname_np(pthread_self(), "CPU_OVERFLOW");
std::ignore = nice(+1); // Lower priority a tiny bit
// Helpers run at much lower
}
setup_overflow(og_config);
TestingStats stats;
std::vector<std::thread> worker_threads;
for (int i = 0; i < og_config.cpu_threads; i++) {
worker_threads.emplace_back(
run_cpu_overflow_worker,
i, std::ref(og_config), std::ref(stats)
);
}
for (auto &worker : worker_threads) {
worker.join();
}
if (is_running)
assert( overflow.size == 0 ); // Should be empty now
// How to get access to final m?
uint64_t processed_m = (stats.max_m <= og_config.m_start) ?
0 : count_num_m(og_config.m_start, stats.max_m - og_config.m_start, og_config.d);
if (og_config.verbose >= 4) {
printf("Processed M: %'lu [%lu, %lu]\n",
processed_m, og_config.m_start, stats.max_m.load());
}
uint64_t T = stats.tested;
uint64_t total_primes = T + stats.tested_prev;
if (og_config.verbose >= 1 and T > 0) {
printf("\nCPU OVERFLOW Timing:\n");