-
Notifications
You must be signed in to change notification settings - Fork 0
/
f4_dynamic_structured.cpp
1912 lines (1780 loc) · 62 KB
/
f4_dynamic_structured.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
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
#ifndef __F4_REDUCTION_CPP__
#define __F4_REDUCTION_CPP__
/*****************************************************************************\
* This file is part of DynGB. *
* *
* DynGB is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* DynGB is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
\*****************************************************************************/
#include "f4_dynamic.hpp"
#include "algorithm_buchberger_basic.hpp"
#define USE_DOMINATORS 1
using std::cerr;
#include <fstream>
using std::ofstream;
#include <future>
using std::future; using std::async;
#include <thread>
using std::thread;
#include <mutex>
using std::mutex;
#include <bitset>
using std::bitset;
#include <string>
using std::to_string;
#include <algorithm>
using std::fill;
#include <cstdlib>
using std::srand; using std::rand;
#include "lp_solver.hpp"
using LP_Solvers::LP_Solver;
#include "skeleton.hpp"
using LP_Solvers::Skeleton;
#include "glpk_solver.hpp"
using LP_Solvers::GLPK_Solver;
#include "ppl_solver.hpp"
using LP_Solvers::PPL_Solver;
using Dynamic_Engine::less_by_hilbert_then_degree;
using Dynamic_Engine::less_by_hilbert;
using Dynamic_Engine::less_by_degree_then_hilbert;
using Dynamic_Engine::less_by_grad_hilbert_then_degree;
using Dynamic_Engine::less_by_smoothest_degrees;
using Dynamic_Engine::less_by_largest_max_component;
using Dynamic_Engine::less_by_num_crit_pairs;
using Dynamic_Engine::less_by_betti;
using Dynamic_Engine::less_by_grad_betti;
using Dynamic_Engine::less_by_degree_then_grad_hilbert;
using Dynamic_Engine::less_by_random;
using Dynamic_Engine::verify_and_modify_if_necessary;
extern list<Abstract_Polynomial *> reduce_basis(list<Abstract_Polynomial *>G);
extern void report_front_pair(Critical_Pair_Basic *p, StrategyFlags strategy);
extern void gm_update_dynamic(
list<Critical_Pair_Dynamic *> & P,
list<Abstract_Polynomial *> & G,
Abstract_Polynomial * r,
StrategyFlags strategy,
ORDERING_TYPE * ordering
);
extern template void report_critical_pairs<Critical_Pair_Dynamic>(
const list<Critical_Pair_Dynamic *>, bool
);
extern template void sort_pairs_by_strategy<Critical_Pair_Basic>(
list<Critical_Pair_Basic *> &
);
F4_Reduction_Data::F4_Reduction_Data(
WGrevlex * curr_ord,
const list<Critical_Pair_Dynamic *> & P,
const list<Abstract_Polynomial *> & B,
Dynamic_Heuristic method
) :
G(B), Rx(P.front()->first()->base_ring()), heur(method),
M_table(P.front()->first()->base_ring().number_of_variables()),
mord(curr_ord)
{
static double overall_time = 0;
static double adding_time = 0;
static double initializing_time = 0;
time_t ostart = time(nullptr);
auto mod = Rx.ground_field().modulus();
num_cols = 0;
// set up the heuristic
switch(heur) {
case Dynamic_Heuristic::ORD_HILBERT_THEN_DEG:
heuristic_judges_smaller = less_by_hilbert_then_degree; break;
case Dynamic_Heuristic::ORD_HILBERT_THEN_LEX:
heuristic_judges_smaller = less_by_hilbert; break;
case Dynamic_Heuristic::DEG_THEN_ORD_HILBERT:
heuristic_judges_smaller = less_by_degree_then_hilbert; break;
case Dynamic_Heuristic::GRAD_HILB_THEN_DEG:
heuristic_judges_smaller = less_by_grad_hilbert_then_degree; break;
case Dynamic_Heuristic::SMOOTHEST_DEGREES:
heuristic_judges_smaller = less_by_smoothest_degrees; break;
case Dynamic_Heuristic::LARGEST_MAX_COMPONENT:
heuristic_judges_smaller = less_by_largest_max_component; break;
case Dynamic_Heuristic::MIN_CRIT_PAIRS:
heuristic_judges_smaller = less_by_num_crit_pairs; break;
case Dynamic_Heuristic::BETTI_HILBERT_DEG:
heuristic_judges_smaller = less_by_betti; break;
case Dynamic_Heuristic::GRAD_BETTI_HILBERT_DEG:
heuristic_judges_smaller = less_by_grad_betti; break;
default:
heuristic_judges_smaller = less_by_hilbert; break;
}
// set up the matrix
A.clear();
strategies.clear();
NVAR_TYPE n = Rx.number_of_variables();
for (auto p : P) {
//
if (p->first()->length() == 1 and p->second() != nullptr) p->swap();
//
Poly_Sugar_Data * new_sugar = new Poly_Sugar_Data(p->first());
strategies.push_back(new_sugar);
if (strategies.back() != nullptr) {
strategies.back()->at_generation_tasks(p->first_multiplier());
if (p->second() != nullptr) {
auto p2log = p->second_multiplier().log();
strategies.back()->pre_reduction_tasks(p2log, *(p->second()));
delete [] p2log;
}
}
add_monomials(curr_ord, p->first(), p->first_multiplier(), true);
if (p->second() != nullptr) {
add_monomials(curr_ord, p->second(), p->second_multiplier());
M_builder[const_cast<Monomial *>(&(p->lcm()))] = const_cast<Abstract_Polynomial *>(p->second());
}
}
// for each monomial, find an appropriate reducer
for (auto mi = M_builder.rbegin(); mi != M_builder.rend(); ++mi) {
auto g = G.begin();
bool found = mi->second != nullptr;
//cout << "for " << *mi->first;
while (not found and g != G.end()) {
if (mi->first->divisible_by((*g)->leading_monomial())) {
found = true;
//cout << " selected " << (*g)->leading_monomial() << endl;
mi->second = *g;
Monomial u(*(mi->first), (*g)->leading_monomial(), false);
time_t astart = time(nullptr);
add_monomials(curr_ord, *g, u);
time_t aend = time(nullptr);
adding_time += difftime(aend, astart);
g = G.end();
}
++g;
}
//if (not found) cout << " no reducer found\n";
}
cout << "adding monomials time " << adding_time << endl;
time_t istart = time(nullptr);
initialize_many(P);
time_t iend = time(nullptr);
initializing_time += difftime(iend, istart);
cout << "initializing time " << initializing_time << endl;
unsigned els = 0;
for (auto Ak : A) els += Ak.size();
cout << "saved " << (num_rows*num_cols - els)*100 / (num_rows*num_cols) << "%\n";
time_t oend = time(nullptr);
overall_time += difftime(oend, ostart);
cout << "overall time in setup " << overall_time << endl;
}
mutex print_lock;
bool sort_by_first(pair<unsigned, COEF_TYPE> a, pair<unsigned, COEF_TYPE> b) {
return a.first < b.first;
}
void F4_Reduction_Data::initialize_some_rows(
const list<Critical_Pair_Dynamic *> & P, unsigned row
) {
const unsigned num_cols = M_builder.size();
const COEF_TYPE F0 = 0;
for (auto cp : P) {
auto p = cp->first();
nonzero_entries[row] = p->length();
//print_lock.lock();
//cout << "initializing row " << row << " for " << cp->lcm() << " with poly " << *p << " and poly ";
//if (cp->second() == nullptr) cout << "0\n"; else cout << *cp->second() << endl;
//print_lock.unlock();
const Monomial & t = cp->first_multiplier();
unsigned j = 0;
auto & Arow = A[row];
Arow.resize(p->length());
auto pi = p->new_iterator();
for (/* */; not pi->fellOff(); pi->moveRight()) {
auto i = M_table.lookup_product(pi->currMonomial(), t);
Arow[j].first = i;
Arow[j].second = pi->currCoeff().value();
++j;
}
sort(Arow.begin(), Arow.end(), sort_by_first);
delete pi;
delete cp;
++row;
}
}
void F4_Reduction_Data::initialize_many(const list<Critical_Pair_Dynamic *> & P) {
num_cols = M_builder.size();
num_rows = P.size();
cout << "Initializing " << num_rows << " x " << num_cols << " basic matrix\n";
dirty.resize(num_rows, true);
last_compatible_ordering.resize(num_rows, nullptr);
strategies.resize(num_rows);
nonzero_entries.resize(num_rows);
R_built.resize(num_cols);
for (auto m : M) delete m;
M.clear(); M.resize(M_builder.size());
R.clear(); R.resize(M_builder.size());
vector<mutex> new_red_mutex(M_builder.size());
red_mutex.swap(new_red_mutex);
size_t m = 0;
for (auto mi = M_builder.rbegin(); mi != M_builder.rend(); ++mi) {
M[m] = mi->first;
R[m] = mi->second;
M_table.update_location(mi->first, m);
++m;
}
A.resize(P.size());
pref_head.resize(P.size());
pp_weights.resize(M.size());
compatible_pps.resize(P.size());
potential_ideals.resize(P.size());
dominators.resize(M.size());
unsigned cores = std::thread::hardware_concurrency();
unsigned num_threads = (cores < num_rows) ? cores : num_rows;
list<Critical_Pair_Dynamic *> * thread_rows
= new list<Critical_Pair_Dynamic *>[num_threads];
// loop through num_rows
unsigned k = 0, rows_added = 0;
srand(time(NULL));
for (auto pi : P) {
thread_rows[k].push_back(pi);
++rows_added;
if (rows_added > P.size() / num_threads + 1) { ++k; rows_added = 0; }
//thread_rows[rand() % num_threads].push_back(pi);
}
unsigned start_work[num_threads];
unsigned start_row = 0;
for (unsigned i = 0; i < num_threads; ++i) {
start_work[i] = start_row;
cout << "thread " << i << " starts at " << start_row << endl;
start_row += thread_rows[i].size();
}
thread * workers = new thread[num_threads];
for (unsigned c = 0; c < num_threads; ++c) {
workers[c] = thread(
&F4_Reduction_Data::initialize_some_rows, this, std::cref(thread_rows[c]),
start_work[c]
);
}
for (unsigned c = 0; c < num_threads; ++c)
workers[c].join();
delete [] workers;
delete [] thread_rows;
}
void F4_Reduction_Data::print_builder() {
cout << "[ ";
for (auto m : M_builder)
cout << "( " << *(m.first) << ", " << m.second << " ) ";
cout << "]\n";
}
double emplace_time;
void F4_Reduction_Data::add_monomials(
const WGrevlex * curr_ord,
const Abstract_Polynomial *g,
const Monomial & u,
bool new_row
) {
Polynomial_Iterator * pi = g->new_iterator();
if (not new_row) pi->moveRight();
while (not (pi->fellOff())) {
bool already_there = M_table.contains_product(pi->currMonomial(), u);
if (not already_there) {
Monomial * t = new Monomial(pi->currMonomial(), u);
t->set_monomial_ordering(curr_ord);
M_table.add_monomial(t);
M_builder.emplace(t, nullptr);
}
pi->moveRight();
}
//cout << "processed " << monomials_processed << " monomials\n";
delete pi;
}
F4_Reduction_Data::~F4_Reduction_Data() {
for (auto strat : strategies) {
if (strat != nullptr)
delete strat;
}
for (auto t : M_builder) delete t.first;
cout << "there were at most " << M_table.max_size << " monomials in any list of hash table\n";
cout << "we spend " << emplace_time << " seconds emplacing\n";
}
void F4_Reduction_Data::print_row(unsigned i, bool as_poly) {
auto & Ai = A[i];
for (unsigned j = 0; j < Ai.size(); ++j) {
if (as_poly) {
cout << " + " << Ai[j].second << " " << *M[Ai[j].first];
} else {
cout << Ai[j].second << " (" << Ai[j].first << "), ";
}
}
cout << endl;
}
void F4_Reduction_Data::print_matrix(bool show_data) {
if (show_data) { // print monomials
for (auto m : M)
cout << *m << ", ";
cout << endl;
}
for (unsigned i = 0; i < num_rows; ++i) { // print entries
cout << "A[" << i << "]: ( ";
unsigned j = 0;
auto & Ai = A[i];
for (unsigned l = 0; l < Ai.size(); ++l) {
for (/* */; j < Ai[l].first; ++j) cout << "0, ";
cout << Ai[l].second << ", ";
++j;
}
for (/* */; j < num_cols; ++j)
cout << "0, ";
cout << ")\n";
}
}
void F4_Reduction_Data::list_reducers() {
for (unsigned i = 0; i < num_cols; ++i) {
cout << *(M[i]) << " to be reduced by ";
if (R[i] == nullptr)
cout << "none\n";
else
cout << R[i]->leading_monomial() << endl;
}
}
bool F4_Reduction_Data::is_zero() {
bool is_zero_so_far = true;
for (unsigned i = 0; is_zero_so_far and i < num_rows; ++i)
is_zero_so_far = is_zero_so_far and (nonzero_entries[i] == 0);
return is_zero_so_far;
}
void F4_Reduction_Data::build_reducer(unsigned mi) {
const auto g = R[mi];
Polynomial_Iterator * gi = g->new_iterator();
auto & r = R_built[mi];
r.resize(g->length());
Monomial u(*M[mi], g->leading_monomial(), false);
unsigned k = 0;
while (not gi->fellOff()) {
const Monomial & t = gi->currMonomial();
r[k].first = M_table.lookup_product(u, t);;
r[k].second = gi->currCoeff().value();
++k;
gi->moveRight();
}
delete gi;
sort(r.begin(), r.end(), sort_by_first);
}
void expand(
vector< pair< unsigned, COEF_TYPE > > & row,
vector< COEF_TYPE > & B,
vector< unsigned > & prev,
vector< unsigned > & next
) {
auto k = row[0].first;
B[k] = row[0].second;
prev[k] = B.size();
next[k] = row[1].first;
unsigned i = 1, n = row.size() - 1;
while (i < n) {
k = row[i].first;
B[k] = row[i].second;
next[k] = row[i+1].first;
prev[k] = row[i-1].first;
++i;
}
if (i < row.size()) {
B[row[i].first] = row[i].second;
prev[row[i].first] = k;
next[row[i].first] = B.size();
} else { // happens only if B.size() == 1
next[k] = B.size();
}
}
COEF_TYPE max_entry_value = 0;
// always reduces the monomial at start
unsigned reduce_monomial(
vector<COEF_TYPE> & B,
const vector< pair< unsigned, COEF_TYPE> > & r,
COEF_TYPE a,
COEF_TYPE mod,
unsigned start, unsigned & head,
vector<unsigned> & prev, vector<unsigned> & next,
unsigned & nonzero_entries
) {
auto num_cols = B.size();
unsigned i = head, j = 0;
start = next[start];
while (i < r[j].first) i = next[i];
unsigned prev_i = prev[i];
auto mod_square = mod * mod;
// add until we run out of monomials in reductee
while (j < r.size()) {
auto k = r[j].first;
if (i < k) {
prev_i = i;
i = next[i];
} else if (i == k) {
B[i] -= a*r[j].second;
//if (B[i] >= max_entry_value) max_entry_value = B[i];
//B[i] %= mod;
B[i] += ( B[i] >> 31 ) & ( mod_square );
//if (B[i] < 0) B[i] += mod;
if (B[i] != 0)
prev_i = i;
else {
if ((i == start) and (start < num_cols)) start = next[start];
--nonzero_entries;
if (i == head) head = next[i];
if (next[i] < num_cols) prev[next[i]] = prev[i];
if (prev[i] < num_cols) next[prev[i]] = next[i];
prev_i = prev[i];
}
i = next[i]; ++j;
} else {
++nonzero_entries;
if (k < start) start = k;
//B[k] = (- r[j].second * a) % mod;
B[k] = - r[j].second * a;
B[k] += ( B[k] >> 31 ) & ( mod_square );
if (-B[k] > max_entry_value) max_entry_value = -B[k];
//B[k] += mod;
if (head > k) { // inserting new head
prev[k] = num_cols;
next[k] = head;
if (head < num_cols) prev[head] = k;
i = head;
head = k;
} else { // head < k < i
if (i < num_cols) {
prev[k] = prev[i];
if (prev[i] < num_cols) next[prev[i]] = k;
prev[i] = k;
} else {
prev[k] = prev_i;
if (prev_i < num_cols) next[prev_i] = k;
}
next[k] = i;
}
prev_i = k;
++j;
}
}
return start;
}
void condense(
vector< pair< unsigned, COEF_TYPE > > & row,
unsigned head,
const vector< COEF_TYPE > & B, const vector< unsigned > & next,
unsigned & nonzero_entries,
UCOEF_TYPE mod
) {
if (row.size() != nonzero_entries) row.resize(nonzero_entries);
unsigned i = 0;
unsigned n = B.size();
for (unsigned k = head; k < n; k = next[k]) {
if (B[k] % mod != 0) {
row[i].first = k;
row[i].second = B[k] % mod;
++i;
} else --nonzero_entries;
}
row.resize(nonzero_entries);
}
unsigned long long reduced_by_old = 0;
void F4_Reduction_Data::reduce_my_rows(
const vector<int> & my_rows, vector<COEF_TYPE> & B,
vector<unsigned> & prev, vector<unsigned> & next
) {
NVAR_TYPE n = Rx.number_of_variables();
const Prime_Field & F = Rx.ground_field();
auto mod = F.modulus();
Monomial u(n);
B.resize(num_cols);
prev.resize(num_cols);
next.resize(num_cols);
// expand, reduce, condense each row
for (unsigned i : my_rows) {
expand(A[i], B, prev, next);
unsigned head = A[i][0].first;
for (unsigned j = head; j < num_cols; /* */) {
const Abstract_Polynomial * g = R[j];
auto a = B[j] % mod;
if ( (g == nullptr) || (a == 0) )
j = next[j];
else {
//auto a = B[j] % mod;
red_mutex[j].lock();
auto & r = R_built[j];
if (r.size() == 0) build_reducer(j);
red_mutex[j].unlock();
auto si = strategies[i];
si->pre_reduction_tasks(u, *g);
j = reduce_monomial(
B, r, a, mod, j, head, prev, next, nonzero_entries[i]
);
reduced_by_old += 1;
}
}
condense(A[i], head, B, next, nonzero_entries[i], mod);
print_lock.lock(); cout << "row " << i << " completed\n"; print_lock.unlock();
}
}
void F4_Reduction_Data::reduce_by_old() {
/*cout << "before reduction\n";
for (unsigned k = 0; k < num_rows; ++k)
check_consistency(k);*/
if (red_lock.size() < num_cols) {
vector<mutex> new_list(3*num_cols / 2);
red_lock.swap(new_list);
}
unsigned cores = std::thread::hardware_concurrency();
unsigned num_threads = (cores < num_rows) ? cores : num_rows;
vector<COEF_TYPE> buffer[num_threads];
vector<unsigned> prev[num_threads], next[num_threads];
vector<int> * thread_rows = new vector<int>[num_threads];
// loop through num_rows
for (unsigned k = 0; k < num_rows; ++k)
thread_rows[k % num_threads].push_back(k);
thread * workers = new thread[num_threads];
for (unsigned c = 0; c < num_threads; ++c) {
workers[c] = thread(
&F4_Reduction_Data::reduce_my_rows, this, std::cref(thread_rows[c]),
std::ref(buffer[c]), std::ref(prev[c]), std::ref(next[c])
);
}
for (unsigned c = 0; c < num_threads; ++c)
workers[c].join();
delete [] workers;
delete [] thread_rows;
/*cout << "after reduction\n";
for (unsigned k = 0; k < num_rows; ++k)
check_consistency(k);*/
}
unsigned location_of_monomial_index(
vector< pair< unsigned, COEF_TYPE > > & row, unsigned i
) {
if (i < row[0].first or i > row[row.size() - 1].first) return row.size();
unsigned j = 0, k = row.size() / 2, l = row.size();
while (row[k].first != i and j != k and k != l) {
auto tmp = k;
if (row[k].first < i) {
k = (k + l) / 2;
j = tmp;
} else {
k = (j + k) / 2;
l = tmp;
}
}
if (row[k].first == i) return k;
if (row[l].first == i) return l;
return row.size();
}
unsigned long long reduced_by_new = 0;
void F4_Reduction_Data::reduce_my_new_rows(
unsigned i,
unsigned lhead_i,
vector< COEF_TYPE > & B,
vector< unsigned > & prev, vector< unsigned > & next,
const set<unsigned> & to_reduce,
unsigned mod
) {
auto & Ai = A[i];
B.resize(num_cols);
prev.resize(num_cols);
next.resize(num_cols);
for (auto j : to_reduce) {
auto & Aj = A[j];
expand(Aj, B, prev, next);
unsigned k = location_of_monomial_index(Aj, lhead_i);
COEF_TYPE a = Aj[k].second % mod;
unsigned head = Aj[0].first;
if (a != 0) {
auto start = Ai[0].first;
if (B[start] == 0) {
auto new_start = head;
while (next[new_start] < start) new_start = next[new_start];
start = new_start;
}
reduce_monomial(B, Ai, a, mod, start, head, prev, next, nonzero_entries[j]);
reduced_by_new += 1;
}
condense(Aj, head, B, next, nonzero_entries[j], mod);
}
}
void F4_Reduction_Data::reduce_by_new(
unsigned i, unsigned lhead_i, const set<unsigned> & unprocessed
) {
//cout << "pre reduction:\n";
fill(dirty.begin(), dirty.end(), false);
//for (auto b: dirty) cout << b << ' '; cout << endl;
//print_matrix(false);
auto & F = Rx.ground_field();
auto mod = F.modulus();
unsigned cores = std::thread::hardware_concurrency();
unsigned num_threads = (cores < num_rows) ? cores : num_rows;
set<unsigned> * thread_rows = new set<unsigned>[num_threads];
vector<COEF_TYPE> B[num_threads];
vector<unsigned> prev[num_threads], next[num_threads];
thread * workers = new thread[num_threads];
for (unsigned j = 0; j < num_threads; ++j)
thread_rows[j].clear();
unsigned k = 0;
unsigned num_to_reduce = 0;
for (unsigned j = 0; j < num_rows; ++j) {
if (j != i and nonzero_entries[j] != 0
and location_of_monomial_index(A[j], lhead_i) != A[j].size()
) {
dirty[j] = true;
thread_rows[k].insert(j);
++k; k %= num_threads;
++num_to_reduce;
}
}
cout << "row " << i << " reduces " << num_to_reduce << " rows\n";
for (unsigned c = 0; c < num_threads; ++c)
workers[c] = thread(
&F4_Reduction_Data::reduce_my_new_rows, this,
i, lhead_i,
std::ref(B[c]), std::ref(prev[c]), std::ref(next[c]),
std::cref(thread_rows[c]), mod
);
for (unsigned c = 0; c < num_threads; ++c)
workers[c].join();
delete [] workers;
delete [] thread_rows;
//cout << "post reduction:\n";
//for (auto b: dirty) cout << b << ' '; cout << endl;
//print_matrix(false);
}
Polynomial_Hashed * F4_Reduction_Data::finalize(
unsigned i, vector< Monomial * > & final_monomials, F4_Hash & final_hash
) {
Polynomial_Hashed * result;
const Prime_Field & F = Rx.ground_field();
UCOEF_TYPE mod = F.modulus();
NVAR_TYPE n = M[0]->num_vars();
result = new Polynomial_Hashed(Rx, final_monomials, final_hash, A[i], M, mord);
result->set_strategy(strategies[i]);
strategies[i] = nullptr;
return result;
}
void F4_Reduction_Data::monomials_in_row(unsigned i, list<unsigned> & result) const {
unsigned processed = 0;
auto & Ai = A[i];
for (unsigned k = 0; k < Ai.size(); ++k)
result.push_back(Ai[k].first);
}
void F4_Reduction_Data::simplify_identical_rows(set<unsigned> & in_use) {
const Prime_Field & F = Rx.ground_field();
UCOEF_TYPE mod = F.modulus();
set<unsigned> removed;
// identify redundants
for (auto i : in_use) {
auto & Ai = A[i];
if (nonzero_entries[i] != 0) {
for (unsigned j = i + 1; j < number_of_rows(); ++j) {
auto & Aj = A[j];
if (
nonzero_entries[j] == nonzero_entries[i]
and Ai[0].first == Aj[0].first
) {
auto a = Aj[0].second * F.inverse(Ai[0].second) % mod;
unsigned k = 1;
for (
/* already initialized */ ;
k < Ai.size() and Ai[k].first == Aj[k].first
and ((a*Ai[k].second % mod) == Aj[k].second) ;
++k
) {
/* already handled */
}
if (k == Ai.size()) {
nonzero_entries[j] = 0;
removed.insert(j);
}
}
}
}
}
cout << "identified " << removed.size() << " redundant rows\n";
// now remove
for (auto i : removed) in_use.erase(i);
}
unsigned divisible_incompatible = 0;
unsigned old_divisibile_incompatible = 0;
void divisibility_tests(
list<int> & allPPs,
F4_Reduction_Data & F4,
bool & stop
) {
bool verbose = false;
if (verbose) {
cout << "checking divisibility criterion for ";
for (auto i : allPPs) cout << *F4.M[i] << ", ";
cout << endl;
}
auto it = allPPs.begin();
auto n = F4.M.front()->num_vars();
while ((not stop) and it != allPPs.end()) {
bool incompatible = false, new_incompatible = false;
auto & u = F4.M[*it];
set<Monomial *> T;
Monomial Tt(n, F4.M.front()->monomial_ordering());
for (auto j : allPPs) {
if (stop or incompatible) break;
if (j != *it) {
auto & t = F4.M[j];
if (t->divisible_by(*u)) {
incompatible = true;
++old_divisibile_incompatible;
if (verbose)
cout << "detected incompatible monomial " << *u
<< " through divisibility by " << *t << "\n";
}
else if (not t->is_coprime(*u)) {
T.insert(t);
Tt *= *t;
if (Tt.divisible_by_power(*u, T.size())) {
new_incompatible = incompatible = true;
if (verbose)
cout << "detected incompatible monomial " << *u
<< " through divisibility by " << Tt << "(" << T.size() << ")\n";
}
}
}
}
while ((not incompatible) and T.size() > 1) {
int j = 0, d = (*u)[0]*T.size() - Tt[0];
for (unsigned k = 1; k < n; ++k)
if ((*u)[k]*T.size() > Tt[k] + d) j = k;
auto ti = T.begin();
d = u->gcd_degree(**ti);
auto remove_me = ti; ++ti;
for (/* */; ti != T.end(); ++ti) {
if (((**ti)[j] < (**remove_me)[j]) or u->gcd_degree(**ti) < d)
remove_me = ti;
}
Tt /= **remove_me;
T.erase(remove_me);
if (Tt.divisible_by_power(*u, T.size())) {
if (verbose) {
cout << "detected incompatible monomial " << *u
<< " through divisibility by " << Tt << " (" << T.size() << ")\n";
cout << "\t[ "; for (auto t : T) cout << *t << ", "; cout << " ]\n";
}
new_incompatible = incompatible = true;
}
}
auto next_it(it); ++next_it;
if (incompatible) {
++divisible_incompatible;
allPPs.erase(it);
}
it = next_it;
}
}
void divisibility_tests_new(
list<int> & allPPs,
F4_Reduction_Data & F4,
bool & stop
) {
bool verbose = false;
bool very_verbose = false;
if (very_verbose) {
for (auto i : allPPs) cout << *F4.M[i] << ", ";
cout << endl;
}
// build monomial for multi-divisibility criterion
auto n = F4.M.front()->num_vars();
Monomial Ttall(n, F4.M.front()->monomial_ordering());
for (auto j : allPPs) {
if (stop) break;
auto & t = F4.M[j];
Ttall *= *t;
}
auto it = allPPs.begin();
while ((not stop) and it != allPPs.end()) {
// check for simple divisibility
bool incompatible = false, new_incompatible = false;
auto & u = F4.M[*it];
for (auto j : allPPs) {
if (stop or incompatible) break;
if (j != *it) {
auto & t = F4.M[j];
if (t->divisible_by(*u)) {
incompatible = true;
if (verbose)
cout << "detected incompatible monomial " << *u
<< " through old divisibility by " << *t << "\n";
}
}
}
// now check for multi-divisibility
if (not (stop or incompatible)) {
set<Monomial *> T;
for (auto set_it = allPPs.begin(); set_it != allPPs.end(); ++set_it) {
if (it != set_it) {
T.insert(F4.M[*set_it]);
}
}
Monomial Tt(Ttall); Tt /= *u;
for (auto new_it = T.begin(); new_it != T.end(); ) {
if (not u->is_coprime(**new_it)) {
++new_it;
} else {
Tt /= **new_it;
auto next_it = new_it; ++next_it; T.erase(new_it); new_it = next_it;
}
}
if (Tt.divisible_by_power(*u, T.size())) {
if (verbose) {
cout << "detected incompatible monomial " << *u
<< " through divisibility by " << Tt << " (" << T.size() << ")\n";
cout << "\t[ "; for (auto t : T) cout << *t << ", "; cout << " ]\n";
}
new_incompatible = incompatible = true;
} else {
if (very_verbose)
cout << "( " << *u << " )^" << T.size() << " does not divide " << Tt << endl;
}
while ((not stop) and (not incompatible) and T.size() > 2) {
int j = 0, d = (*u)[0]*T.size() - Tt[0];
for (unsigned k = 1; k < n; ++k) {
if ((*u)[k]*T.size() > Tt[k] + d) j = k;
}
auto ti = T.begin();
auto remove_me = T.end();
for (/* */; ti != T.end(); ++ti) {
if (remove_me == T.end()) remove_me = ti;
else if ((**ti)[j] < (**remove_me)[j] /*or (u->gcd(**ti).total_degree() < u->gcd(**remove_me).total_degree())*/)
remove_me = ti;
}
if (very_verbose) cout << "divide " << Tt << " by " << **remove_me << " to get ";
Tt /= **remove_me;
T.erase(remove_me);
if (very_verbose) cout << Tt << endl;
if (Tt.divisible_by_power(*u, T.size())) {
if (verbose) {
cout << "detected incompatible monomial " << *u
<< " through divisibility by " << Tt << " (" << T.size() << ")\n";
cout << "\t[ "; for (auto t : T) cout << *t << ", "; cout << " ]\n";
}
new_incompatible = incompatible = true;
} else {
if (very_verbose)
cout << "( " << *u << " )^" << T.size() << " does not divide " << Tt << endl;
}
}
}
auto next_it(it); ++next_it;
if (incompatible) {
++divisible_incompatible;
Ttall /= *F4.M[*it];
allPPs.erase(it);
}
it = next_it;
}
}
void F4_Reduction_Data::prune_dominators(list< unsigned > & all_pps, unsigned i) {
/*cout << "dominators: ";
for (auto i = 1; i < dominators.size(); ++i)
if (dominators[i] != dominators.size())
cout << i << ":" << dominators[i] << " ";
cout << endl;
cout << "pruning ";
print_row(i, true);
print_row(i, false);*/
auto & Ai = A[i];
auto Ai_size = Ai.size();
unsigned pruned = 0, original_number = all_pps.size();
for (auto li = all_pps.begin(); li != all_pps.end(); ) {
auto k = *li;
//cout << k << ": " << dominators[k] << " " << "( " << location_of_monomial_index(A[i], dominators[k]) << " ) ";
unsigned l = Ai_size;
while (dominators[k] != M.size()) {
l = location_of_monomial_index(Ai, dominators[k]);
//cout << k << " ( " << l << " ) ";
if (l == Ai_size) k = dominators[ k ];
else break;
}
//cout << endl;
if (l == Ai_size) ++li;
else { // dominator found
//cout << "pruning " << *li << " because of column " << k << endl;
auto tmp(li); ++tmp;
all_pps.erase(li);
li = tmp;
++pruned;
}
}
cout << "pruned " << pruned << " monomials of " << original_number
<< " from row " << i << " leaving " << all_pps.size() << endl;
}
unsigned total_terms_considered = 0;
/**
@ingroup GBComputation
@author John Perry
@date 2019
@brief Compute the compatible leading monomials of a polynomial.
@details This differs from the more general case in that monomials are
indexed by @c M, rather than making copies of monomials.
In addition, we keep a cache of the monomials for each row,
so that we don't have to recompute the compatible monomials on each pass.
@param my_row row to process
@param F4 matrix structure
@param skel existing skeleton that defines currently-compatible orderings
@param stop signal bit whether to stop processing early (indeterminism may result)
@param completed (out only) whether we complete our given rows
*/
void compatible_pp(
int my_row,
F4_Reduction_Data & F4,
WGrevlex * curr_ord,
const LP_Solver * skel,
bool & stop,
vector<bool> & completed
)
{
F4.last_compatible_ordering[my_row] = curr_ord;
int currentLPP_index = F4.A[my_row][0].first;
// get the exponent vector of the current LPP, insert it
const Monomial & currentLPP(*F4.M[currentLPP_index]);
NVAR_TYPE n = currentLPP.num_vars();
list<int> initial_candidates;
initial_candidates.push_back(currentLPP_index);
// compare other monomials with LPP
list<unsigned> allPPs;
if (not stop) {
F4.monomials_in_row(my_row, allPPs);
if (USE_DOMINATORS) F4.prune_dominators(allPPs, my_row);
total_terms_considered += allPPs.size();
//divisibility_tests(allPPs, F4, stop);