-
Notifications
You must be signed in to change notification settings - Fork 2
/
SparseReduceMatrix4.cpp
1075 lines (927 loc) · 34 KB
/
SparseReduceMatrix4.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
/******************************************************************/
/*** FILE : SparseReduceMatrix.c ***/
/*** PROGRAMMER: David Lee ***/
/*** DATE WRITTEN: April-August 1992. ***/
/*** PUBLIC ROUTINES: ***/
/*** SparseReduceMatrix() ***/
/*** Get_Matrix_Element() ***/
/*** Insert_Element() ***/
/*** Delete_Element() ***/
/*** Change_Element() ***/
/*** Locate_Node() ***/
/*** PRIVATE ROUTINES: ***/
/*** SparseMultRow() ***/
/*** SparseAddRow() ***/
/*** SparseKnockOut() ***/
/*** SparseInterchange() ***/
/*** Insert_Node() ***/
/*** Delete_Node() ***/
/*** Change_Node() ***/
/*** Row_Empty() ***/
/*** MODULE DESCRIPTION: ***/
/*** This module reduces the sparse matrix ***/
/*** in row canonical form. This code is ***/
/*** similar to the code in ReduceMatrix.c ***/
/******************************************************************/
#include <list>
#include <vector>
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "SparseReduceMatrix4.h"
#include "Build_defs.h"
#include "Scalar_arithmetic.h"
#include "profile.h"
#include "memory_usage.h"
extern bool __record;
extern int __deg;
extern int __nn1;
extern int __nn2;
namespace SparseReduceMatrix4_ns {
using std::max;
using std::min;
using std::list;
using std::vector;
using std::lower_bound;
//using std::random_shuffle;
typedef std::vector<Scalar> DenseRow;
class Row {
public:
Row() : d_start_col(0), d_nz(0), d_fc(0) {}
Row(const Row &r) : s(r.s), d(r.d), d_start_col(r.d_start_col), d_nz(r.d_nz), d_fc(r.d_fc) {};
// Row &operator=(const Row &r) = delete;
inline Row &operator=(const Row &r) {
if (this != &r) {
s = r.s;
d = r.d;
d_start_col = r.d_start_col;
d_nz = r.d_nz;
d_fc = r.d_fc;
}
return *this;
}
SparseRow s;
DenseRow d;
int d_start_col;
int d_nz;
int d_fc;
inline void swap(Row &r) {
s.swap(r.s);
d.swap(r.d);
std::swap(d_start_col, r.d_start_col);
std::swap(d_nz, r.d_nz);
std::swap(d_fc, r.d_fc);
}
inline bool empty() const {
return s.empty() && d.empty();
}
inline size_t capacity() const {
return max(s.capacity(), d.capacity());
}
inline size_t size() const {
return max(s.size(), d.size());
}
inline int firstColumn() const {
if (!s.empty()) {
return s.front().getColumn();
} else if (!d.empty()) {
return d_fc;
// for (auto i = d.cbegin(); i != d.cend(); i++) {
// if (*i != S_zero()) {
// return d_start_col + (i - d.cbegin());
// }
//// DenseRow().swap(d);
//// d_start_col = 0;
// }
}
return 99999999;
}
inline int firstElement() const {
if (!s.empty()) {
return s.front().getElement();
} else if (!d.empty()) {
for (int i = 0; i < d.size(); i++) {
if (d[i] != S_zero()) {
return d[i];
}
}
}
return S_zero();
}
void update_cache() {
if (!d.empty()) {
d_fc = 99999999;
d_nz = 0;
auto i = d.cbegin();
for (; i != d.cend(); i++) {
if (*i != S_zero()) {
d_nz++;
d_fc = d_start_col + (i - d.cbegin());
break;
}
// DenseRow().swap(d);
// d_start_col = 0;
}
for (; i != d.cend(); i++) {
if (*i != S_zero()) {
d_nz++;
}
}
}
}
inline void promote_if_needed(int nCols) {
if (!s.empty()) {
int n = nCols - s.front().getColumn() + 1;
if (n < s.size() * sizeof(Node)) {
promote_to_dense(nCols);
}
} else if (!d.empty()) {
update_cache();
int fc = firstColumn();
if (fc == 99999999) {
DenseRow().swap(d);
d_start_col = 0;
d_nz = 0;
} else if (fc - d_start_col > d.size() / 2) {
int s = fc - d_start_col;
DenseRow tmp(d.begin() + s, d.end());
tmp.swap(d);
d_start_col += s;
}
}
}
void promote_to_dense(int nCols) {
if (!s.empty()) {
d_start_col = s.front().getColumn();
d.clear();
d.resize(nCols - d_start_col + 1, S_zero());
d_nz = 0;
d_fc = s.front().getColumn();
for (auto i = s.cbegin(); i != s.end(); i++) {
assert(i->getColumn() - d_start_col >= 0);
if (i->getColumn() - d_start_col >= d.size()) {
for (auto i2 = s.cbegin(); i2 != s.end(); i2++) {
printf("%d %d %d %d\n", i2->getColumn(), i2->getColumn() - d_start_col, d.size(), nCols);
}
abort();
}
d[i->getColumn() - d_start_col] = i->getElement();
d_nz++;
}
SparseRow().swap(s);
}
}
inline void divide(Scalar x) {
if (x == S_one()) return;
multiply(S_inv(x));
}
inline void multiply(Scalar x) {
if (x == S_one()) return;
if (!s.empty() && !d.empty()) {
abort();
}
if (!s.empty()) {
for (auto ii = s.begin(); ii != s.end(); ii++) {
ii->setElement(S_mul(ii->getElement(), x));
}
} else if (!d.empty()) {
for (auto ii = d.begin(); ii != d.end(); ii++) {
*ii = S_mul(*ii, x);
}
}
}
inline int non_zero_count() const {
if (!s.empty()) {
return s.size();
} else if (!d.empty()) {
return d_nz;
}
return 0;
}
};
inline void swap(Row &lhs, Row &rhs) {
if (&lhs != &rhs) {
lhs.s.swap(rhs.s);
lhs.d.swap(rhs.d);
std::swap(lhs.d_start_col, rhs.d_start_col);
std::swap(lhs.d_nz, rhs.d_nz);
std::swap(lhs.d_fc, rhs.d_fc);
}
}
typedef std::vector<Row> AutoMatrix;
static void promote_to_auto(SparseMatrix &sm, AutoMatrix &am) {
am.resize(sm.size());
for (int i = 0; i < sm.size(); i++) {
am[i].s.swap(sm[i]);
}
}
static void promote_to_sparse(AutoMatrix &am, SparseMatrix &sm) {
sm.resize(am.size());
for (int i = 0; i < am.size(); i++) {
if (!am[i].s.empty() && !am[i].d.empty()) {
abort();
}
if (!am[i].s.empty()) {
sm[i].swap(am[i].s);
} else if (!am[i].d.empty()) {
SparseRow sr;
for (int j = 0; j < am[i].d.size(); j++) {
if (am[i].d[j] != S_zero()) {
sr.push_back(Node(am[i].d[j], am[i].d_start_col + j));
}
}
SparseRow(sr.begin(), sr.end()).swap(sm[i]);
}
}
}
void SparseAddRow(Scalar Factor, const SparseRow &r1, SparseRow &r2);
static void SparseAddRow(AutoMatrix &SM, Scalar Factor, int Row1, int Row2, int nCols);
static void SparseDenseAddRow3(Scalar Factor, const Row &r1, Row &r2);
static void DenseAddRow3(Scalar Factor, const Row &r1, Row &r2);
static void SparseKnockOut(AutoMatrix &SM, int row, int col, int last_row, int nCols);
Scalar Get_Matrix_Element(const AutoMatrix &SM, int i, int j);
#if 0
static void Print_Matrix(MAT_PTR Sparse_Matrix, int r, int c);
static void Print_Rows(int Row1, int Row2, int nCols);
static void Print_SLList(Node *SLHead_Ptr);
static void Print_Node(NODE_PTR Prt_Node);
#endif
struct stats {
//size_t n_zero_elements;
size_t n_elements;
size_t capacity;
size_t n_zero_rows;
size_t n_rows;
size_t n_cols;
int last_nextstairrow;
int prev_col;
int cur_col;
time_t first_update;
time_t prev_update;
time_t cur_update;
stats() : // n_zero_elements(0),
n_elements(0),
capacity(0),
n_zero_rows(0),
n_rows(0),
n_cols(0),
last_nextstairrow(0),
prev_col(0),
cur_col(0),
first_update(0),
prev_update(0),
cur_update(0) {}
void clear() {
//n_zero_elements = 0;
n_elements = 0;
capacity = 0;
n_zero_rows = 0;
n_rows = 0;
n_cols = 0;
last_nextstairrow = 0;
// These are not reset between updates because they are used
// to calculate rates.
//prev_col = 0;
//cur_col = 0;
//first_update = 0;
//prev_update = 0;
//cur_update = 0;
}
static void tp(float t) {
if (t > 3600) {
printf("%.02fh", t / 3600.);
} else if (t > 60) {
printf("%.02fm", t / 60.);
} else {
printf("%.02fs", t);
}
}
void print() const {
printf("\r\t\tne:%lu (%.1fMB)", n_elements, n_elements * sizeof(Node) / 1024. / 1024.);
#if 0
if(n_zero_elements > 0) {
printf(" ze:%lu", n_zero_elements);
}
#endif
if (n_elements != capacity) {
printf(" ce:%lu", capacity);
}
printf(" zr:%lu lr:%d/%lu lc:%d/%lu",
n_zero_rows,
last_nextstairrow, n_rows,
cur_col, n_cols);
{
time_t dt = cur_update - first_update;
if (dt > 0) {
printf(" tt:");
tp(dt);
}
}
if (cur_col > 100) {
int dt = cur_update - prev_update;
if (dt != 0) {
float cps = (cur_col - prev_col + 1) / float(dt);
printf(" cps:%.02f", cps);
float eta = (n_cols - cur_col) / cps;
if (eta > 1) {
printf(" etr:");
tp(eta);
}
}
}
printf(" ");
fflush(nullptr);
}
void update(const AutoMatrix &SM, int nextstairrow_, int last_col_, int nCols_, int timeout = -1,
bool do_print = false) {
time_t t = time(nullptr);
if (timeout != -1 && cur_update != 0 && t - cur_update < timeout) {
return;
}
clear();
if (first_update == 0) {
first_update = t;
}
prev_update = cur_update;
cur_update = t;
n_rows = SM.size();
n_cols = nCols_;
last_nextstairrow = nextstairrow_;
prev_col = cur_col;
cur_col = last_col_;
for (int ii = 0; ii < (int) SM.size(); ii++) {
capacity += SM[ii].capacity();
n_elements += SM[ii].size();
if (SM[ii].empty()) {
n_zero_rows++;
}
#if 0
// There should be no zero elements
for(int jj=0; jj<(int)SM[ii].size(); jj++) {
if(SM[ii][jj].getElement() == S_zero()) {
n_zero_elements++;
}
}
#endif
}
if (do_print) {
print();
}
}
};
static void save_mat_image(int a, int b, int c, const AutoMatrix &SM, int nCols) {
int mh = SM.size();
int mw = nCols;
const int iih = 2160;
const int iiw = 3840;
int ih = min(mh, iih);
int iw = min(mw, iiw);
// if(mh < ih || mw < iw) return;
auto img = new unsigned char[ih * iw]();
for (int r = 0; r < (int) SM.size(); r++) {
for (int j = 0; j < (int) SM[r].size(); j++) {
int col = 0;
Scalar e = S_zero();
if (!SM[r].s.empty()) {
col = SM[r].s[j].getColumn();
e = SM[r].s[j].getElement();
} else if (!SM[r].d.empty()) {
col = SM[r].d_start_col + j;
e = SM[r].d[j];
img[r * iw + 0] = 255;
} else {
continue;
}
if (e != S_zero()) {
int y = r;
int x = col;
if (ih < mh) y = int(y / float(mh) * ih);
if (iw < mw) x = int(x / float(mw) * iw);
//if(x >= iw) abort();
//if(y >= ih) abort();
if (img[y * iw + x] < 255) {
img[y * iw + x]++;
}
}
}
}
#if 0
int mv = 0;
for (int i = 0; i < ih * iw; i++) {
mv = max(int(img[i]), mv);
}
for (int i = 0; i < ih * iw; i++) {
if (img[i] != 0) {
img[i] = int(float(img[i]) / float(mv) * (255 - 63) + 63 + .5);
}
}
#else
for (int i = 0; i < ih * iw; i++) {
if (img[i]) {
img[i] = 255;
}
}
#endif
{
int s = 1;
if (ih >= iw) {
s = iih / ih;
} else {
s = iih / iw;
}
int hoff = (iih - s * ih) / 2;
int woff = (iiw - s * iw) / 2;
auto s_img = new unsigned char[iih * iiw]();
for (int i = 0; i < ih; i++) {
for (int j = 0; j < iw; j++) {
for (int ii = 0; ii < s; ii++) {
for (int jj = 0; jj < s; jj++) {
s_img[((i * s + hoff + ii) * iiw) + woff + j * s + jj] = img[i * iw + j];
}
}
}
}
delete[] img;
img = s_img;
ih = iih;
iw = iiw;
}
char fn[128];
static int ind = 0;
//sprintf(fn, "%d_%d_%d__%d_%d_%d__%d.pgm", __deg, __nn1, __nn2, a, b, c, ind);
sprintf(fn, "%08d_%03d_%03d_%03d_%06d_%06d.pgm", ind, __deg, __nn1, __nn2, c, nCols);
ind++;
FILE *f = fopen(fn, "wb");
fprintf(f, "P5\n%d %d 255\n", iw, ih);
fwrite(img, 1, ih * iw, f);
fclose(f);
delete[] img;
}
static inline bool SM_sort(const SparseRow &r1, const SparseRow &r2) {
if (r1.empty() && r2.empty()) return false;
if (!r1.empty() && r2.empty()) return true;
if (r1.empty() && !r2.empty()) return false;
if (r1.front().getColumn() < r2.front().getColumn()) return true;
if (r1.front().getColumn() > r2.front().getColumn()) return false;
#if 1
// Generally results in greater sparsity
if (r1.size() < r2.size()) return true;
if (r1.size() > r2.size()) return false;
#else
// Generally results in greater density, i.e. more non-zero intermediate entries
if (r1.size() > r2.size()) return true;
if (r1.size() < r2.size()) return false;
#endif
if (r1.front().getElement() < r2.front().getElement()) return true;
if (r1.front().getElement() > r2.front().getElement()) return false;
return false;
}
static inline bool AM_sort(const Row &r1, const Row &r2) {
if (r1.empty() && r2.empty()) return false;
if (!r1.empty() && r2.empty()) return true;
if (r1.empty() && !r2.empty()) return false;
if (r1.firstColumn() < r2.firstColumn()) return true;
if (r1.firstColumn() > r2.firstColumn()) return false;
#if 1
#if 1
// Generally results in greater sparsity
if (r1.non_zero_count() < r2.non_zero_count()) return true;
if (r1.non_zero_count() > r2.non_zero_count()) return false;
#else
// Generally results in greater density, i.e. more non-zero intermediate entries
if (r1.non_zero_count() > r2.non_zero_count()) return true;
if (r1.non_zero_count() < r2.non_zero_count()) return false;
#endif
#endif
if (r1.firstElement() < r2.firstElement()) return true;
if (r1.firstElement() > r2.firstElement()) return false;
return false;
}
void print_matrix(AutoMatrix &SM, int nCols, const char *header) {
if (header) printf(header);
for (int i = 0; i < (int) SM.size(); i++) {
if (!SM[i].s.empty()) {
printf("S%04d ", SM[i].non_zero_count());
} else if (!SM[i].d.empty()) {
printf("D%04d/%04d/%04d", SM[i].non_zero_count(), nCols - SM[i].firstColumn() + 1, SM[i].d.size());
} else {
printf("E ");
}
for (int j = 0; j < (int) nCols; j++) {
Scalar s = Get_Matrix_Element(SM, i, j);
printf(" %3d", s);
}
putchar('\n');
}
}
int SparseReduceMatrix(SparseMatrix &SM_, int nCols, int *Rank) {
memory_usage_init(nCols);
if (SM_.empty() || nCols == 0) {
return OK;
}
AutoMatrix SM;
promote_to_auto(SM_, SM);
bool do_sort = true;
if (do_sort) sort(SM.begin(), SM.end(), AM_sort);
//random_shuffle(SM.begin(), SM.end());
//printf("s:%d c:%d ", (int)SM.size(), (int)SM.capacity());
/* Search for the rightmost nonzero element */
/* Dependent on the current stairrow */
stats s1;
s1.update(SM, 0, 0, nCols, -1, true);
float nper = .1;
if (__record) {
save_mat_image(0, 0, 0, SM, nCols);
}
int nextstairrow = 0;
int last_row = SM.size();
for (int i = 0; i < nCols; i++) {
memory_usage_update(i);
int j;
for (j = nextstairrow; j < (int) SM.size(); j++) {
if (Get_Matrix_Element(SM, j, i) != S_zero()) {
break;
}
}
/* When found interchange and then try to knockout any nonzero
elements in the same column */
#define DEBUG_MATRIX 0
#if DEBUG_MATRIX
printf("\nCol:%d/%d j:%d nextstairrow:%d nRows:%d reducing?:%d\n", i, nCols, j, nextstairrow, SM.size(),
j < (int) SM.size());
print_matrix(SM, nCols, "Start\n");
#endif
if (j < (int) SM.size()) {
SM[nextstairrow].swap(SM[j]);
#if DEBUG_MATRIX
print_matrix(SM, nCols, "After swap\n");
#endif
SparseKnockOut(SM, nextstairrow, i, last_row, nCols);
for (int iii = last_row; iii > 0; iii--) {
if (!SM[iii - 1].empty()) {
last_row = iii;
break;
}
}
// printf("%d %d\n", SM.size(), last_row);
// if (do_sort) sort(SM.begin() + nextstairrow + 1, SM.end(), SM_sort);
if (do_sort) sort(SM.begin() + nextstairrow + 1, SM.begin() + last_row, AM_sort);
// if (__record || 1) {
// printf(">> %d", nextstairrow);
// long aa = 0;
// long bb = 0;
// for(int ii=nextstairrow; ii<SM.size(); ii++) {
//// printf("(%.2f %.2f) ", SM[i].size() / float(nCols - nextstairrow) * 100, SM[ii].size() * 4 / float(nCols - nextstairrow + 4));
// if(!SM[ii].empty()) {
// int a = SM[ii].size() * 4;
// int b = (nCols - SM[ii].front().getColumn() + 1) * 1 + 4;
// printf(" %.2f", a / float(b));
// aa += a;
// bb += b;
// }
// }
// printf("\n %.2f\n", aa / float(bb));
// }
#if DEBUG_MATRIX
print_matrix(SM, nCols, "After reduce\n");
#endif
nextstairrow++;
}
if (__record && (1 || i / float(nCols) > nper)) {
nper += .1;
save_mat_image(0, 1, i, SM, nCols);
}
s1.update(SM, nextstairrow, i, nCols, 60, true);
}
*Rank = nextstairrow;
s1.update(SM, nextstairrow, nCols, nCols, -1, true);
putchar('\n');
if (__record) {
save_mat_image(0, 2, nCols, SM, nCols);
}
#if DEBUG_MATRIX
print_matrix(SM, nCols, "Final\n");
#endif
promote_to_sparse(SM, SM_);
return OK;
}
/*********************************************************************/
/* Three things can happen with the SparseAddRow routine ....
First there is a target row and a row which is multiplied by a factor
and added to the target row.
1. The result is nonzero and there is no column in the target row so add
a new node.
2. The result is nonzero and there is a column in the target row so change
the value in the node.
3. The result is zero and there is a column in the target row so delete
the node.
*/
/*********************************************************************/
void SparseAddRow(Scalar Factor, const SparseRow &r1, SparseRow &r2) {
/* check for zero factor */
if (Factor == S_zero()) {
return;
}
/* get the beginning of the two rows to work with */
SparseRow tmp;
tmp.reserve(r1.size() + r2.size());
auto r1i = r1.cbegin();
auto r2i = r2.cbegin();
for (; r1i != r1.cend() && r2i != r2.cend();) {
if (r1i->getColumn() == r2i->getColumn()) {
Scalar x = S_add(r2i->getElement(), S_mul(Factor, r1i->getElement()));
if (x != S_zero()) {
Node n = *r1i;
n.setElement(x);
tmp.push_back(n);
}
r1i++;
r2i++;
} else if (r1i->getColumn() < r2i->getColumn()) {
Scalar x = S_mul(Factor, r1i->getElement());
//if(x != S_zero()) {
Node n = *r1i;
n.setElement(x);
tmp.push_back(n);
//}
r1i++;
} else { //if(r1i->column > r2i->column) {
tmp.push_back(*r2i);
r2i++;
}
}
// append r2 with remaining r1 nodes
for (; r1i != r1.cend(); r1i++) {
Scalar x = S_mul(Factor, r1i->getElement());
//if(x != S_zero()) {
Node n = *r1i;
n.setElement(x);
tmp.push_back(n);
//}
}
// append r2 with remaining r1 nodes
for (; r2i != r2.cend(); r2i++) {
tmp.push_back(*r2i);
}
SparseRow(tmp.begin(), tmp.end()).swap(r2); // shrink capacity while assigning
//r2 = SparseRow(tmp.begin(), tmp.end());
//shrink_capacity(tmp);
//printf("<%d %d %d %d>", (int)r2.size(), (int)r2.capacity(), (int)tmp.size(), (int)tmp.capacity());
//r2 = tmp;
//r2.swap(tmp);
}
void SparseAddRow(AutoMatrix &SM, Scalar Factor, int Row1, int Row2, int nCols) {
const auto &r1 = SM[Row1];
auto &r2 = SM[Row2];
if (!r1.s.empty() && !r2.s.empty()) {
SparseAddRow(Factor, r1.s, r2.s);
// r2.promote_if_needed(nCols);
} else {
SparseDenseAddRow3(Factor, r1, r2);
// r2.promote_if_needed(nCols);
}
}
void SparseDenseAddRow3(Scalar Factor, const Row &r1, Row &r2) { // }, int nCols) {
if (Factor == S_zero()) return;
if (!r1.s.empty() && !r2.d.empty()) {
if (r2.firstColumn() <= r1.firstColumn()) {
for (auto ii = r1.s.begin(); ii != r1.s.end(); ii++) {
int j = ii->getColumn() - r2.d_start_col;
r2.d[j] = S_add(r2.d[j], S_mul(Factor, ii->getElement()));
}
} else {
abort();
}
} else if (!r1.d.empty() && !r2.s.empty()) {
SparseRow tmp;
tmp.reserve(r1.size() + r2.size());
auto r1i = 0;
auto r2i = r2.s.cbegin();
for (; r1i < r1.d.size() && r2i != r2.s.cend();) {
if (r1i + r1.d_start_col == r2i->getColumn()) {
Scalar x = S_add(r2i->getElement(), S_mul(Factor, r1.d[r1i]));
if (x != S_zero()) {
Node n(x, r2i->getColumn());
tmp.push_back(n);
}
r1i++;
r2i++;
// } else if (r1i + r1.firstColumn() < r2i->getColumn()) {
} else if (r1i + r1.d_start_col < r2i->getColumn()) {
Scalar x = S_mul(Factor, r1.d[r1i]);
if (x != S_zero()) {
Node n(x, r1i + r1.d_start_col);
tmp.push_back(n);
}
r1i++;
} else { //if(r1i->column > r2i->column) {
tmp.push_back(*r2i);
r2i++;
}
}
// append r2 with remaining r1 nodes
for (; r1i < r1.d.size(); r1i++) {
Scalar x = S_mul(Factor, r1.d[r1i]);
if (x != S_zero()) {
Node n(x, r1i + r1.d_start_col);
tmp.push_back(n);
}
}
// append r2 with remaining r1 nodes
for (; r2i != r2.s.cend(); r2i++) {
tmp.push_back(*r2i);
}
SparseRow(tmp.begin(), tmp.end()).swap(r2.s); // shrink capacity while assigning
// r2.promote_if_needed(nCols);
} else if (!r1.d.empty() && !r2.d.empty()) {
DenseAddRow3(Factor, r1, r2);
} else {
abort();
}
}
void DenseAddRow3(Scalar Factor, const Row &r1, Row &r2) { // }, int nCols) {
if (Factor == S_zero()) return;
if (r1.d_start_col < r2.d_start_col) {
for (int i = 0; i < r2.d.size(); i++) {
int j = i + (r2.d_start_col - r1.d_start_col);
r2.d[i] = S_add(r2.d[i], S_mul(Factor, r1.d[j]));
}
} else {
// int n = max(r1.d_start_col + r1.d.size(), r2.d_start_col + r1.d.size());
// for (int i = min(r1.d_start_col, r1.d_start_col); i<n; i++) {
// int c = r1i.getColumn();
// r2[c] = S_add(r2[c], S_mul(Factor, r1i.getElement()));
// }
for (int i = 0; i < r1.d.size(); i++) {
int j = i + (r1.d_start_col - r2.d_start_col);
r2.d[j] = S_add(r2.d[j], S_mul(Factor, r1.d[i]));
}
}
// r2.promote_if_needed(-1);
}
void SparseKnockOut(AutoMatrix &SM, int row, int col, int last_row, int nCols) {
Scalar x = Get_Matrix_Element(SM, row, col);
SM[row].divide(x);
SM[row].promote_if_needed(nCols);
/* try to knockout elements in column in the rows above */
#pragma omp parallel for shared(SM, row, col, last_row, nCols) schedule(dynamic, 10) default(none)
// for (int j = 0; j < (int) SM.size(); j++) {
for (int j = 0; j < last_row; j++) {
if (j != row) {
SparseAddRow(SM, S_minus(Get_Matrix_Element(SM, j, col)), row, j, nCols);
SM[j].promote_if_needed(nCols);
}
}
}
#if 0
void Print_Matrix(MAT_PTR Sparse_Matrix, int r, int c) {
int i, row, col;
Node *Row_Head_Ptr, *Row_Element_Ptr;
if (Sparse_Matrix == NULL) {
return;
}
for (row = 0; row < r; row++) {
Row_Head_Ptr = Sparse_Matrix[row];
Row_Element_Ptr = Row_Head_Ptr;
if (Row_Element_Ptr == NULL) {
printf("EMPTY ROW %d\n", row);
} else {
while (Row_Element_Ptr != NULL) {
printf("%4d", Row_Element_Ptr->element);
Row_Element_Ptr = Row_Element_Ptr->Next_Node;
}
printf("\n");
}
}
printf("\n");
for (row = 0; row < r; row++) {
Row_Head_Ptr = Sparse_Matrix[row];
Row_Element_Ptr = Row_Head_Ptr;
for (col = 0; (col < c); col++) {
if (Row_Element_Ptr != NULL) {
if (Row_Element_Ptr->column != col) {
printf(" 0");
} else {
printf("%4d", Row_Element_Ptr->element);
if (Row_Element_Ptr->Next_Node != NULL)
Row_Element_Ptr = Row_Element_Ptr->Next_Node;
}
} else {
printf(" 0");
}
}
printf("\n");
}
printf("\n");
}
void Print_Rows(int Row1, int Row2, int nCols) {
int i, row, col;
NODE_PTR Row1_Ptr;
NODE_PTR Row2_Ptr;
Row1_Ptr = Matrix_Base_Ptr[Row1];
Row2_Ptr = Matrix_Base_Ptr[Row2];
for (col = 0; (col < nCols); col++) {
if (Row1_Ptr != NULL) {
if (Row1_Ptr->column != col) {
printf(" 0");
} else {
printf("%4d", Row1_Ptr->element);
if (Row1_Ptr->Next_Node != NULL)
Row1_Ptr = Row1_Ptr->Next_Node;
}
} else {
printf(" 0");
}
}
printf("\n");
for (col = 0; (col < nCols); col++) {
if (Row2_Ptr != NULL) {
if (Row2_Ptr->column != col) {
printf(" 0");
} else {
printf("%4d", Row2_Ptr->element);
if (Row2_Ptr->Next_Node != NULL)
Row2_Ptr = Row2_Ptr->Next_Node;
}
} else {
printf(" 0");
}
}
printf("\n");
}
#endif
#if 0
static bool cmp_column(const Node &n1, const Node &n2) { return n1.column < n2.column; }
#if 0
struct A {
bool operator()(const Node &n1, const Node &n2) const { return n1.column < n2.column; }
};
#endif
Scalar Get_Matrix_Element(const SparseMatrix &SM, int i, int j) {
Node n;
n.column = j;
SparseRow::const_iterator ii = lower_bound(SM[i].begin(), SM[i].end(), n, cmp_column);
//SparseRow::const_iterator ii = lower_bound(SM[i].begin(), SM[i].end(), n, A());