-
Notifications
You must be signed in to change notification settings - Fork 1
/
bicsb.cpp
1375 lines (1246 loc) · 45.7 KB
/
bicsb.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
#include <cassert>
#include "bicsb.h"
#include "utility.h"
// Choose block size as big as possible given the following constraints
// 1) The bot array is addressible by IT
// 2) The parts of x & y vectors that a block touches fits into L2 cache [assuming a saxpy() operation]
// 3) There's enough parallel slackness for block rows (at least SLACKNESS * CILK_NPROC)
template <class NT, class IT>
void BiCsb<NT, IT>::Init(int workers, IT forcelogbeta)
{
ispar = (workers > 1);
IT roundrowup = nextpoweroftwo(m);
IT roundcolup = nextpoweroftwo(n);
// if indices are negative, highestbitset returns -1,
// but that will be caught by the sizereq below
IT rowbits = highestbitset(roundrowup);
IT colbits = highestbitset(roundcolup);
bool sizereq;
if (ispar)
{
sizereq = ((IntPower<2>(rowbits) > SLACKNESS * workers)
&& (IntPower<2>(colbits) > SLACKNESS * workers));
}
else
{
sizereq = ((rowbits > 1) && (colbits > 1));
}
if(!sizereq)
{
cerr << "Matrix too small for this library" << endl;
return;
}
rowlowbits = rowbits-1;
collowbits = colbits-1;
IT inf = numeric_limits<IT>::max();
IT maxbits = highestbitset(inf);
rowhighbits = rowbits-rowlowbits; // # higher order bits for rows (has at least one bit)
colhighbits = colbits-collowbits; // # higher order bits for cols (has at least one bit)
if(ispar)
{
while(IntPower<2>(rowhighbits) < SLACKNESS * workers)
{
rowhighbits++;
rowlowbits--;
}
}
// calculate the space that suby occupies in L2 cache
IT yL2 = IntPower<2>(rowlowbits) * sizeof(NT);
while(yL2 > L2SIZE)
{
yL2 /= 2;
rowhighbits++;
rowlowbits--;
}
// calculate the space that subx occupies in L2 cache
IT xL2 = IntPower<2>(collowbits) * sizeof(NT);
while(xL2 > L2SIZE)
{
xL2 /= 2;
colhighbits++;
collowbits--;
}
// blocks need to be square for correctness (maybe generalize this later?)
while(rowlowbits+collowbits > maxbits)
{
if(rowlowbits > collowbits)
{
rowhighbits++;
rowlowbits--;
}
else
{
colhighbits++;
collowbits--;
}
}
while(rowlowbits > collowbits)
{
rowhighbits++;
rowlowbits--;
}
while(rowlowbits < collowbits)
{
colhighbits++;
collowbits--;
}
assert (collowbits == rowlowbits);
lowrowmask = IntPower<2>(rowlowbits) - 1;
lowcolmask = IntPower<2>(collowbits) - 1;
if(forcelogbeta != 0)
{
IT candlowmask = IntPower<2>(forcelogbeta) -1;
cout << "Forcing beta to "<< (candlowmask+1) << " instead of the chosen " << (lowrowmask+1) << endl;
cout << "Warning : No checks are performed on the beta you have forced, anything can happen !" << endl;
lowrowmask = lowcolmask = candlowmask;
rowlowbits = collowbits = forcelogbeta;
rowhighbits = rowbits-rowlowbits;
colhighbits = colbits-collowbits;
}
else
{
double sqrtn = sqrt(sqrt(static_cast<double>(m) * static_cast<double>(n)));
IT logbeta = static_cast<IT>(ceil(log2(sqrtn))) + 2;
if(rowlowbits > logbeta)
{
rowlowbits = collowbits = logbeta;
lowrowmask = lowcolmask = IntPower<2>(logbeta) -1;
rowhighbits = rowbits-rowlowbits;
colhighbits = colbits-collowbits;
}
cout << "Beta chosen to be "<< (lowrowmask+1) << endl;
}
highrowmask = ((roundrowup - 1) ^ lowrowmask);
highcolmask = ((roundcolup - 1) ^ lowcolmask);
// nbc = #{block columns} = #{blocks in any block row}, nbr = #{block rows)
IT blcdimrow = lowrowmask + 1;
IT blcdimcol = lowcolmask + 1;
nbr = static_cast<IT>(ceil(static_cast<double>(m) / static_cast<double>(blcdimrow)));
nbc = static_cast<IT>(ceil(static_cast<double>(n) / static_cast<double>(blcdimcol)));
blcrange = (lowrowmask+1) * (lowcolmask+1); // range indexed by one block
mortoncmp = MortonCompare<IT>(rowlowbits, collowbits, lowrowmask, lowcolmask);
}
// Partial template specialization for booleans
// Does not check cache considerations as this is mostly likely
// to be used for gaxpy() with multiple rhs vectors (we don't know how many and what type at this point)
template <class IT>
void BiCsb<bool,IT>::Init(int workers, IT forcelogbeta)
{
ispar = (workers > 1);
IT roundrowup = nextpoweroftwo(m);
IT roundcolup = nextpoweroftwo(n);
// if indices are negative, highestbitset returns -1,
// but that will be caught by the sizereq below
IT rowbits = highestbitset(roundrowup);
IT colbits = highestbitset(roundcolup);
bool sizereq;
if (ispar)
{
sizereq = ((IntPower<2>(rowbits) > SLACKNESS * workers)
&& (IntPower<2>(colbits) > SLACKNESS * workers));
}
else
{
sizereq = ((rowbits > 1) && (colbits > 1));
}
if(!sizereq)
{
cerr << "Matrix too small for this library" << endl;
return;
}
rowlowbits = rowbits-1;
collowbits = colbits-1;
IT inf = numeric_limits<IT>::max();
IT maxbits = highestbitset(inf);
rowhighbits = rowbits-rowlowbits; // # higher order bits for rows (has at least one bit)
colhighbits = colbits-collowbits; // # higher order bits for cols (has at least one bit)
if(ispar)
{
while(IntPower<2>(rowhighbits) < SLACKNESS * workers)
{
rowhighbits++;
rowlowbits--;
}
}
// blocks need to be square for correctness (maybe generalize this later?)
while(rowlowbits+collowbits > maxbits)
{
if(rowlowbits > collowbits)
{
rowhighbits++;
rowlowbits--;
}
else
{
colhighbits++;
collowbits--;
}
}
while(rowlowbits > collowbits)
{
rowhighbits++;
rowlowbits--;
}
while(rowlowbits < collowbits)
{
colhighbits++;
collowbits--;
}
assert (collowbits == rowlowbits);
lowrowmask = IntPower<2>(rowlowbits) - 1;
lowcolmask = IntPower<2>(collowbits) - 1;
if(forcelogbeta != 0)
{
IT candlowmask = IntPower<2>(forcelogbeta) -1;
cout << "Forcing beta to "<< (candlowmask+1) << " instead of the chosen " << (lowrowmask+1) << endl;
cout << "Warning : No checks are performed on the beta you have forced, anything can happen !" << endl;
lowrowmask = lowcolmask = candlowmask;
rowlowbits = collowbits = forcelogbeta;
rowhighbits = rowbits-rowlowbits;
colhighbits = colbits-collowbits;
}
else
{
double sqrtn = sqrt(sqrt(static_cast<double>(m) * static_cast<double>(n)));
IT logbeta = static_cast<IT>(ceil(log2(sqrtn))) + 2;
if(rowlowbits > logbeta)
{
rowlowbits = collowbits = logbeta;
lowrowmask = lowcolmask = IntPower<2>(logbeta) -1;
rowhighbits = rowbits-rowlowbits;
colhighbits = colbits-collowbits;
}
cout << "Beta chosen to be "<< (lowrowmask+1) << endl;
}
highrowmask = ((roundrowup - 1) ^ lowrowmask);
highcolmask = ((roundcolup - 1) ^ lowcolmask);
// nbc = #{block columns} = #{blocks in any block row}, nbr = #{block rows)
IT blcdimrow = lowrowmask + 1;
IT blcdimcol = lowcolmask + 1;
nbr = static_cast<IT>(ceil(static_cast<double>(m) / static_cast<double>(blcdimrow)));
nbc = static_cast<IT>(ceil(static_cast<double>(n) / static_cast<double>(blcdimcol)));
blcrange = (lowrowmask+1) * (lowcolmask+1); // range indexed by one block
mortoncmp = MortonCompare<IT>(rowlowbits, collowbits, lowrowmask, lowcolmask);
}
// Constructing empty BiCsb objects (size = 0) are not allowed.
template <class NT, class IT>
BiCsb<NT, IT>::BiCsb (IT size, IT rows, IT cols, int workers): nz(size),m(rows),n(cols)
{
assert(nz != 0 && n != 0 && m != 0);
Init(workers);
num = (NT*) aligned_malloc( nz * sizeof(NT));
bot = (IT*) aligned_malloc( nz * sizeof(IT));
top = allocate2D<IT>(nbr, nbc+1);
}
// Partial template specialization for booleans
template <class IT>
BiCsb<bool, IT>::BiCsb (IT size, IT rows, IT cols, int workers): nz(size),m(rows),n(cols)
{
assert(nz != 0 && n != 0 && m != 0);
Init(workers);
bot = (IT*) aligned_malloc( nz * sizeof(IT));
top = allocate2D<IT>(nbr, nbc+1);
}
// copy constructor
template <class NT, class IT>
BiCsb<NT, IT>::BiCsb (const BiCsb<NT,IT> & rhs)
: nz(rhs.nz), m(rhs.m), n(rhs.n), blcrange(rhs.blcrange), nbr(rhs.nbr), nbc(rhs.nbc),
rowhighbits(rhs.rowhighbits), rowlowbits(rhs.rowlowbits), highrowmask(rhs.highrowmask), lowrowmask(rhs.lowrowmask),
colhighbits(rhs.colhighbits), collowbits(rhs.collowbits), highcolmask(rhs.highcolmask), lowcolmask(rhs.lowcolmask),
mortoncmp(rhs.mortoncmp), ispar(rhs.ispar)
{
if(nz > 0)
{
num = (NT*) aligned_malloc( nz * sizeof(NT));
bot = (IT*) aligned_malloc( nz * sizeof(IT));
copy (rhs.num, rhs.num + nz, num);
copy (rhs.bot, rhs.bot + nz, bot);
}
if ( nbr > 0)
{
top = allocate2D<IT>(nbr, nbc+1);
for(IT i=0; i<nbr; ++i)
copy (rhs.top[i], rhs.top[i] + nbc + 1, top[i]);
}
}
// copy constructor for partial NT=boolean specialization
template <class IT>
BiCsb<bool, IT>::BiCsb (const BiCsb<bool,IT> & rhs)
: nz(rhs.nz), m(rhs.m), n(rhs.n), blcrange(rhs.blcrange), nbr(rhs.nbr), nbc(rhs.nbc),
rowhighbits(rhs.rowhighbits), rowlowbits(rhs.rowlowbits), highrowmask(rhs.highrowmask), lowrowmask(rhs.lowrowmask),
colhighbits(rhs.colhighbits), collowbits(rhs.collowbits), highcolmask(rhs.highcolmask), lowcolmask(rhs.lowcolmask),
mortoncmp(rhs.mortoncmp), ispar(rhs.ispar)
{
if(nz > 0)
{
bot = (IT*) aligned_malloc( nz * sizeof(IT));
copy (rhs.bot, rhs.bot + nz, bot);
}
if ( nbr > 0)
{
top = allocate2D<IT>(nbr, nbc+1);
for(IT i=0; i<nbr; ++i)
copy (rhs.top[i], rhs.top[i] + nbc + 1, top[i]);
}
}
template <class NT, class IT>
BiCsb<NT, IT> & BiCsb<NT, IT>::operator= (const BiCsb<NT, IT> & rhs)
{
if(this != &rhs)
{
if(nz > 0) // if the existing object is not empty, make it empty
{
aligned_free(bot);
aligned_free(num);
}
if(nbr > 0)
{
deallocate2D(top, nbr);
}
ispar = rhs.ispar;
nz = rhs.nz;
n = rhs.n;
m = rhs.m;
nbr = rhs.nbr;
nbc = rhs.nbc;
blcrange = rhs.blcrange;
rowhighbits = rhs.rowhighbits;
rowlowbits = rhs.rowlowbits;
highrowmask = rhs.highrowmask;
lowrowmask = rhs.lowrowmask;
colhighbits = rhs.colhighbits;
collowbits = rhs.collowbits;
highcolmask = rhs.highcolmask;
lowcolmask= rhs.lowcolmask;
mortoncmp = rhs.mortoncmp;
if(nz > 0) // if the copied object is not empty
{
num = (NT*) aligned_malloc( nz * sizeof(NT));
bot = (IT*) aligned_malloc( nz * sizeof(IT));
copy (rhs.num, rhs.num + nz, num);
copy (rhs.bot, rhs.bot + nz, bot);
}
if ( nbr > 0)
{
top = allocate2D<IT>(nbr, nbc+1);
for(IT i=0; i<nbr; ++i)
copy (rhs.top[i], rhs.top[i] + nbc + 1, top[i]);
}
}
return *this;
}
template <class IT>
BiCsb<bool, IT> & BiCsb<bool, IT>::operator= (const BiCsb<bool, IT> & rhs)
{
if(this != &rhs)
{
if(nz > 0) // if the existing object is not empty, make it empty
{
aligned_free(bot);
}
if(nbr > 0)
{
deallocate2D(top, nbr);
}
ispar = rhs.ispar;
nz = rhs.nz;
n = rhs.n;
m = rhs.m;
nbr = rhs.nbr;
nbc = rhs.nbc;
blcrange = rhs.blcrange;
rowhighbits = rhs.rowhighbits;
rowlowbits = rhs.rowlowbits;
highrowmask = rhs.highrowmask;
lowrowmask = rhs.lowrowmask;
colhighbits = rhs.colhighbits;
collowbits = rhs.collowbits;
highcolmask = rhs.highcolmask;
lowcolmask= rhs.lowcolmask;
mortoncmp = rhs.mortoncmp;
if(nz > 0) // if the copied object is not empty
{
bot = (IT*) aligned_malloc( nz * sizeof(IT));
copy (rhs.bot, rhs.bot + nz, bot);
}
if ( nbr > 0)
{
top = allocate2D<IT>(nbr, nbc+1);
for(IT i=0; i<nbr; ++i)
copy (rhs.top[i], rhs.top[i] + nbc + 1, top[i]);
}
}
return *this;
}
template <class NT, class IT>
BiCsb<NT, IT>::~BiCsb()
{
if( nz > 0)
{
aligned_free((unsigned char*) num);
aligned_free((unsigned char*) bot);
}
if ( nbr > 0)
{
deallocate2D(top, nbr);
}
}
template <class IT>
BiCsb<bool, IT>::~BiCsb()
{
if( nz > 0)
{
aligned_free((unsigned char*) bot);
}
if ( nbr > 0)
{
deallocate2D(top, nbr);
}
}
template <class NT, class IT>
BiCsb<NT, IT>::BiCsb (Csc<NT, IT> & csc, int workers, IT forcelogbeta):nz(csc.nz),m(csc.m),n(csc.n)
{
typedef std::pair<IT, IT> ipair;
typedef std::pair<IT, ipair> mypair;
assert(nz != 0 && n != 0 && m != 0);
if(forcelogbeta == 0)
Init(workers);
else
Init(workers, forcelogbeta);
num = (NT*) aligned_malloc( nz * sizeof(NT));
bot = (IT*) aligned_malloc( nz * sizeof(IT));
top = allocate2D<IT>(nbr, nbc+1);
mypair * pairarray = new mypair[nz];
IT k = 0;
for(IT j = 0; j < n; ++j)
{
for (IT i = csc.jc [j] ; i < csc.jc[j+1] ; ++i) // scan the jth column
{
// concatenate the higher/lower order half of both row (first) index and col (second) index bits
IT hindex = (((highrowmask & csc.ir[i] ) >> rowlowbits) << colhighbits)
| ((highcolmask & j) >> collowbits);
IT lindex = ((lowrowmask & csc.ir[i]) << collowbits) | (lowcolmask & j) ;
// i => location of that nonzero in csc.ir and csc.num arrays
pairarray[k++] = mypair(hindex, ipair(lindex,i));
}
}
sort(pairarray, pairarray+nz); // sort according to hindex
SortBlocks(pairarray, csc.num);
delete [] pairarray;
}
template <class IT>
template <typename NT> // to provide conversion from arbitrary Csc<> to specialized BiCsb<bool>
BiCsb<bool, IT>::BiCsb (Csc<NT, IT> & csc, int workers):nz(csc.nz),m(csc.m),n(csc.n)
{
typedef std::pair<IT, IT> ipair;
typedef std::pair<IT, ipair> mypair;
assert(nz != 0 && n != 0 && m != 0);
Init(workers);
bot = (IT*) aligned_malloc( nz * sizeof(IT));
top = allocate2D<IT>(nbr, nbc+1);
mypair * pairarray = new mypair[nz];
IT k = 0;
for(IT j = 0; j < n; ++j)
{
for (IT i = csc.jc [j] ; i < csc.jc[j+1] ; ++i) // scan the jth column
{
// concatenate the higher/lower order half of both row (first) index and col (second) index bits
IT hindex = (((highrowmask & csc.ir[i] ) >> rowlowbits) << colhighbits)
| ((highcolmask & j) >> collowbits);
IT lindex = ((lowrowmask & csc.ir[i]) << collowbits) | (lowcolmask & j) ;
// i => location of that nonzero in csc.ir and csc.num arrays
pairarray[k++] = mypair(hindex, ipair(lindex,i));
}
}
sort(pairarray, pairarray+nz); // sort according to hindex
SortBlocks(pairarray);
delete [] pairarray;
}
// Assumption: rowindices (ri) and colindices(ci) are "parallel arrays" sorted w.r.t. column index values
template <class NT, class IT>
BiCsb<NT, IT>::BiCsb (IT size, IT rows, IT cols, IT * ri, IT * ci, NT * val, int workers, IT forcelogbeta)
:nz(size),m(rows),n(cols)
{
typedef std::pair<IT, IT> ipair;
typedef std::pair<IT, ipair> mypair;
assert(nz != 0 && n != 0 && m != 0);
Init(workers, forcelogbeta);
num = (NT*) aligned_malloc( nz * sizeof(NT));
bot = (IT*) aligned_malloc( nz * sizeof(IT));
top = allocate2D<IT>(nbr, nbc+1);
mypair * pairarray = new mypair[nz];
for(IT k = 0; k < nz; ++k)
{
// concatenate the higher/lower order half of both row (first) index and col (second) index bits
IT hindex = (((highrowmask & ri[k] ) >> rowlowbits) << colhighbits) | ((highcolmask & ci[k]) >> collowbits);
IT lindex = ((lowrowmask & ri[k]) << collowbits) | (lowcolmask & ci[k]) ;
// k is stored in order to retrieve the location of this nonzero in val array
pairarray[k] = mypair(hindex, ipair(lindex, k));
}
sort(pairarray, pairarray+nz); // sort according to hindex
SortBlocks(pairarray, val);
delete [] pairarray;
}
template <class IT>
BiCsb<bool, IT>::BiCsb (IT size, IT rows, IT cols, IT * ri, IT * ci, int workers, IT forcelogbeta)
:nz(size),m(rows),n(cols)
{
typedef std::pair<IT, IT> ipair;
typedef std::pair<IT, ipair> mypair;
assert(nz != 0 && n != 0 && m != 0);
Init(workers, forcelogbeta);
bot = (IT*) aligned_malloc( nz * sizeof(IT));
top = allocate2D<IT>(nbr, nbc+1);
mypair * pairarray = new mypair[nz];
for(IT k = 0; k < nz; ++k)
{
// concatenate the higher/lower order half of both row (first) index and col (second) index bits
IT hindex = (((highrowmask & ri[k] ) >> rowlowbits) << colhighbits) | ((highcolmask & ci[k]) >> collowbits);
IT lindex = ((lowrowmask & ri[k]) << collowbits) | (lowcolmask & ci[k]) ;
// k is stored in order to retrieve the location of this nonzero in val array
pairarray[k] = mypair(hindex, ipair(lindex, k));
}
sort(pairarray, pairarray+nz); // sort according to hindex
SortBlocks(pairarray);
delete [] pairarray;
}
template <class NT, class IT>
void BiCsb<NT, IT>::SortBlocks(pair<IT, pair<IT,IT> > * pairarray, NT * val)
{
typedef typename std::pair<IT, std::pair<IT, IT> > mypair;
IT cnz = 0;
IT ldim = IntPower<2>(colhighbits); // leading dimension (not always equal to nbc)
for(IT i = 0; i < nbr; ++i)
{
for(IT j = 0; j < nbc; ++j)
{
top[i][j] = cnz;
IT prevcnz = cnz;
vector< mypair > blocknz;
while(cnz < nz && pairarray[cnz].first == ((i*ldim)+j) ) // as long as we're in this block
{
IT lowbits = pairarray[cnz].second.first;
IT rlowbits = ((lowbits >> collowbits) & lowrowmask);
IT clowbits = (lowbits & lowcolmask);
IT bikey = BitInterleaveLow(rlowbits, clowbits);
blocknz.push_back(mypair(bikey, pairarray[cnz++].second));
}
// sort the block into bitinterleaved order
sort(blocknz.begin(), blocknz.end());
for(IT k=prevcnz; k<cnz ; ++k)
{
bot[k] = blocknz[k-prevcnz].second.first;
num[k] = val[blocknz[k-prevcnz].second.second];
}
}
top[i][nbc] = cnz; // hence equal to top[i+1][0] if i+1 < nbr
}
assert(cnz == nz);
}
template <class IT>
void BiCsb<bool, IT>::SortBlocks(pair<IT, pair<IT,IT> > * pairarray)
{
typedef pair<IT, pair<IT, IT> > mypair;
IT cnz = 0;
IT ldim = IntPower<2>(colhighbits); // leading dimension (not always equal to nbc)
for(IT i = 0; i < nbr; ++i)
{
for(IT j = 0; j < nbc; ++j)
{
top[i][j] = cnz;
IT prevcnz = cnz;
std::vector<mypair> blocknz;
while(cnz < nz && pairarray[cnz].first == ((i*ldim)+j) ) // as long as we're in this block
{
IT lowbits = pairarray[cnz].second.first;
IT rlowbits = ((lowbits >> collowbits) & lowrowmask);
IT clowbits = (lowbits & lowcolmask);
IT bikey = BitInterleaveLow(rlowbits, clowbits);
blocknz.push_back(mypair(bikey, pairarray[cnz++].second));
}
// sort the block into bitinterleaved order
sort(blocknz.begin(), blocknz.end());
for(IT k=prevcnz; k<cnz ; ++k)
bot[k] = blocknz[k-prevcnz].second.first;
}
top[i][nbc] = cnz;
}
assert(cnz == nz);
}
/**
* @param[IT**] chunks {an array of pointers, ith entry is an address pointing to the top array }
* That address belongs to the the first block in that chunk
* chunks[i] is valid for i = {start,start+1,...,end}
* chunks[0] = btop
**/
template <class NT, class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<NT, IT>::BMult(IT** chunks, IT start, IT end, const RHS * __restrict x, LHS * __restrict y, IT ysize) const
{
assert(end-start > 0); // there should be at least one chunk
if (end-start == 1) // single chunk
{
if((chunks[end] - chunks[start]) == 1) // chunk consists of a single (normally dense) block
{
IT chi = ( (chunks[start] - chunks[0]) << collowbits);
// m-chi > lowcolmask for all blocks except the last skinny tall one.
// if the last one is regular too, then it has m-chi = lowcolmask+1
if(ysize == (lowrowmask+1) && (m-chi) > lowcolmask ) // parallelize if it is a regular/complete block
{
const RHS * __restrict subx = &x[chi];
BlockPar<SR>( *(chunks[start]) , *(chunks[end]), subx, y, 0, blcrange, BREAKEVEN * ysize);
}
else // otherwise block parallelization will fail
{
SubSpMV<SR>(chunks[0], chunks[start]-chunks[0], chunks[end]-chunks[0], x, y);
}
}
else // a number of sparse blocks with a total of at most O(\beta) nonzeros
{
SubSpMV<SR>(chunks[0], chunks[start]-chunks[0], chunks[end]-chunks[0], x, y);
}
}
else
{
// divide chunks into half
IT mid = (start+end)/2;
cilk_spawn BMult<SR>(chunks, start, mid, x, y, ysize);
if(SYNCHED)
{
BMult<SR>(chunks, mid, end, x, y, ysize);
}
else
{
LHS * temp = new LHS[ysize]();
// not the empty set of parantheses as the initializer, therefore
// even if LHS is a built-in type (such as double,int) it will be default-constructed
// The C++ standard says that: A default constructed POD type is zero-initialized,
// for non-POD types (such as std::array), the caller should make sure default constructs to zero
BMult<SR>(chunks, mid, end, x, temp, ysize);
cilk_sync;
#pragma simd
for(IT i=0; i<ysize; ++i)
SR::axpy(temp[i], y[i]);
delete [] temp;
}
}
}
// partial template specialization for NT=bool
template <class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<bool, IT>::BMult(IT** chunks, IT start, IT end, const RHS * __restrict x, LHS * __restrict y, IT ysize) const
{
assert(end-start > 0); // there should be at least one chunk
if (end-start == 1) // single chunk
{
if((chunks[end] - chunks[start]) == 1) // chunk consists of a single (normally dense) block
{
IT chi = ( (chunks[start] - chunks[0]) << collowbits);
// m-chi > lowcolmask for all blocks except the last skinny tall one.
// if the last one is regular too, then it has m-chi = lowcolmask+1
if(ysize == (lowrowmask+1) && (m-chi) > lowcolmask ) // parallelize if it is a regular/complete block
{
const RHS * __restrict subx = &x[chi];
BlockPar<SR>( *(chunks[start]) , *(chunks[end]), subx, y, 0, blcrange, BREAKEVEN * ysize);
}
else // otherwise block parallelization will fail
{
SubSpMV<SR>(chunks[0], chunks[start]-chunks[0], chunks[end]-chunks[0], x, y);
}
}
else // a number of sparse blocks with a total of at most O(\beta) nonzeros
{
SubSpMV<SR>(chunks[0], chunks[start]-chunks[0], chunks[end]-chunks[0], x, y);
}
}
else
{
// divide chunks into half
IT mid = (start+end)/2;
cilk_spawn BMult<SR>(chunks, start, mid, x, y, ysize);
if(SYNCHED)
{
BMult<SR>(chunks, mid, end, x, y, ysize);
}
else
{
LHS * temp = new LHS[ysize]();
// not the empty set of parantheses as the initializer, therefore
// even if LHS is a built-in type (such as double,int) it will be default-constructed
// The C++ standard says that: A default constructed POD type is zero-initialized,
// for non-POD types (such as std::array), the caller should make sure default constructs to zero
BMult<SR>(chunks, mid, end, x, temp, ysize);
cilk_sync;
#pragma simd
for(IT i=0; i<ysize; ++i)
SR::axpy(temp[i], y[i]);
delete [] temp;
}
}
}
/**
* Improved non-zero dividing version of BTransMult (as opposed to spatially dividing)
* @warning {difference from BMult is that while the top array pointed by chunks is still contiguous...
* the nonzeros pointed by two consecutive top locations - top[i] and top[i+1] are NOT}
* @param[vector< vector< pair<IT,IT> > * >] chunks {a vector of pointers to vectors of pairs}
* Each vector of pairs is a chunk and each pair is a block within that chunk
* chunks[i] is valid for i = {start,start+1,...,end-1}
**/
template <class NT, class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<NT, IT>::BTransMult(vector< vector< tuple<IT,IT,IT> > * > & chunks, IT start, IT end, const RHS * __restrict x, LHS * __restrict y, IT ysize) const
{
#ifdef STATS
blockparcalls += 1;
#endif
assert(end-start > 0); // there should be at least one chunk
if (end-start == 1) // single chunk (note that single chunk does not mean single block)
{
if(chunks[start]->size() == 1) // chunk consists of a single (normally dense) block
{
// get the block row id higher order bits to index x (because this is A'x)
auto block = chunks[start]->front(); // get the tuple representing this compressed sparse block
IT chi = ( get<2>(block) << rowlowbits);
// m-chi > lowrowmask for all blocks except the last skinny tall one.
// if the last one is regular too, then it has m-chi = lowcolmask+1
// parallelize if it is a regular/complete block (and it it is worth it)
if(ysize == (lowrowmask+1) && (m-chi) > lowrowmask && (get<1>(block)-get<0>(block)) > BREAKEVEN * ysize)
{
const RHS * __restrict subx = &x[chi];
BlockParT<SR>( get<0>(block) , get<1>(block), subx, y, 0, blcrange, BREAKEVEN * ysize);
}
else // otherwise block parallelization will fail
{
SubSpMVTrans<SR>(*(chunks[start]), x, y);
}
}
else // a number of sparse blocks with a total of at most O(\beta) nonzeros
{
SubSpMVTrans<SR>(*(chunks[start]), x, y);
}
}
else // multiple chunks
{
IT mid = (start+end)/2;
cilk_spawn BTransMult<SR>(chunks, start, mid, x, y, ysize);
if(SYNCHED)
{
BTransMult<SR>(chunks, mid, end, x, y, ysize);
}
else
{
LHS * temp = new LHS[ysize]();
BTransMult<SR>(chunks, mid, end, x, temp, ysize);
cilk_sync;
#pragma simd
for(IT i=0; i<ysize; ++i)
SR::axpy(temp[i], y[i]);
delete [] temp;
}
}
}
// Partial template specialization on NT=bool
template <class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<bool, IT>::BTransMult(vector< vector< tuple<IT,IT,IT> > * > & chunks, IT start, IT end, const RHS * __restrict x, LHS * __restrict y, IT ysize) const
{
assert(end-start > 0); // there should be at least one chunk
if (end-start == 1) // single chunk (note that single chunk does not mean single block)
{
if(chunks[start]->size() == 1) // chunk consists of a single (normally dense) block
{
// get the block row id higher order bits to index x (because this is A'x)
auto block = chunks[start]->front(); // get the tuple representing this compressed sparse block
IT chi = ( get<2>(block) << rowlowbits);
// m-chi > lowrowmask for all blocks except the last skinny tall one.
// if the last one is regular too, then it has m-chi = lowcolmask+1
if(ysize == (lowrowmask+1) && (m-chi) > lowrowmask ) // parallelize if it is a regular/complete block
{
const RHS * __restrict subx = &x[chi];
BlockParT<SR>( get<0>(block) , get<1>(block), subx, y, 0, blcrange, BREAKEVEN * ysize);
}
else // otherwise block parallelization will fail
{
SubSpMVTrans<SR>(*(chunks[start]), x, y);
}
}
else // a number of sparse blocks with a total of at most O(\beta) nonzeros
{
SubSpMVTrans<SR>(*(chunks[start]), x, y);
}
}
else // multiple chunks
{
IT mid = (start+end)/2;
cilk_spawn BTransMult<SR>(chunks, start, mid, x, y, ysize);
if(SYNCHED)
{
BTransMult<SR>(chunks, mid, end, x, y, ysize);
}
else
{
LHS * temp = new LHS[ysize]();
BTransMult<SR>(chunks, mid, end, x, temp, ysize);
cilk_sync;
#pragma simd
for(IT i=0; i<ysize; ++i)
SR::axpy(temp[i], y[i]);
delete [] temp;
}
}
}
// double* restrict a; --> No aliases for a[0], a[1], ...
// bstart/bend: block start/end index (to the top array)
template <class NT, class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<NT, IT>::SubSpMV(IT * __restrict btop, IT bstart, IT bend, const RHS * __restrict x, LHS * __restrict suby) const
{
IT * __restrict r_bot = bot;
NT * __restrict r_num = num;
__m128i lcms = _mm_set1_epi32 (lowcolmask);
__m128i lrms = _mm_set1_epi32 (lowrowmask);
for (IT j = bstart ; j < bend ; ++j) // for all blocks inside that block row
{
// get higher order bits for column indices
IT chi = (j << collowbits);
const RHS * __restrict subx = &x[chi];
#ifdef SIMDUNROLL
IT start = btop[j];
IT range = (btop[j+1]-btop[j]) >> 2;
if(range > ROLLING)
{
for (IT k = 0 ; k < range ; ++k) // for all nonzeros within ith block (expected =~ nnz/n = c)
{
// ABAB: how to ensure alignment on the stack?
// float a[4] __attribute__((aligned(0x1000)));
#define ALIGN16 __attribute__((aligned(16)))
IT ALIGN16 rli4[4]; IT ALIGN16 cli4[4];
NT ALIGN16 x4[4]; NT ALIGN16 y4[4];
// _mm_srli_epi32: Shifts the 4 signed or unsigned 32-bit integers to right by shifting in zeros.
IT pin = start + (k << 2);
__m128i bots = _mm_loadu_si128((__m128i*) &r_bot[pin]); // load 4 consecutive r_bot elements
__m128i clis = _mm_and_si128( bots, lcms);
__m128i rlis = _mm_and_si128( _mm_srli_epi32(bots, collowbits), lrms);
_mm_store_si128 ((__m128i*) cli4, clis);
_mm_store_si128 ((__m128i*) rli4, rlis);
x4[0] = subx[cli4[0]];
x4[1] = subx[cli4[1]];
x4[2] = subx[cli4[2]];
x4[3] = subx[cli4[3]];
__m128d Y01QW = _mm_mul_pd((__m128d)_mm_loadu_pd(&r_num[pin]), (__m128d)_mm_load_pd(&x4[0]));
__m128d Y23QW = _mm_mul_pd((__m128d)_mm_loadu_pd(&r_num[pin+2]), (__m128d)_mm_load_pd(&x4[2]));
_mm_store_pd(&y4[0],Y01QW);
_mm_store_pd(&y4[2],Y23QW);
suby[rli4[0]] += y4[0];
suby[rli4[1]] += y4[1];
suby[rli4[2]] += y4[2];
suby[rli4[3]] += y4[3];
}
for(IT k=start+4*range; k<btop[j+1]; ++k)
{
IT rli = ((r_bot[k] >> collowbits) & lowrowmask);
IT cli = (r_bot[k] & lowcolmask);
SR::axpy(r_num[k], subx[cli], suby[rli]);
}
}
else
{
#endif
for(IT k=btop[j]; k<btop[j+1]; ++k)
{
IT rli = ((r_bot[k] >> collowbits) & lowrowmask);
IT cli = (r_bot[k] & lowcolmask);
SR::axpy(r_num[k], subx[cli], suby[rli]);
}
#ifdef SIMDUNROLL
}
#endif
}
}
// Partial boolean specialization on NT=bool
template <class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<bool, IT>::SubSpMV(IT * __restrict btop, IT bstart, IT bend, const RHS * __restrict x, LHS * __restrict suby) const
{
IT * __restrict r_bot = bot;
for (IT j = bstart ; j < bend ; ++j) // for all blocks inside that block row or chunk
{
// get higher order bits for column indices
IT chi = (j << collowbits);
const RHS * __restrict subx = &x[chi];
for (IT k = btop[j] ; k < btop[j+1] ; ++k) // for all nonzeros within ith block (expected =~ nnz/n = c)
{
IT rli = ((r_bot[k] >> collowbits) & lowrowmask);
IT cli = (r_bot[k] & lowcolmask);
SR::axpy(subx[cli], suby[rli]); // suby [rli] += subx [cli] where subx and suby are vectors.
}
}
}
//! SubSpMVTrans's chunked version
template <class NT, class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<NT, IT>::SubSpMVTrans(const vector< tuple<IT,IT,IT> > & chunk, const RHS * __restrict x, LHS * __restrict suby) const
{
IT * __restrict r_bot = bot;
NT * __restrict r_num = num;
for(auto itr = chunk.begin(); itr != chunk.end(); ++itr) // over all blocks within this chunk
{
// get the starting point for accessing x
IT chi = ( get<2>(*itr) << rowlowbits);
const RHS * __restrict subx = &x[chi];
IT nzbeg = get<0>(*itr);
IT nzend = get<1>(*itr);
for (IT k = nzbeg ; k < nzend ; ++k)
{
// Note the swap in cli/rli
IT cli = ((r_bot[k] >> collowbits) & lowrowmask);
IT rli = (r_bot[k] & lowcolmask);
SR::axpy(r_num[k], subx[cli], suby[rli]); // suby [rli] += r_num[k] * subx [cli] where subx and suby are vectors.
}
}
}
//! SubSpMVTrans's chunked version with boolean specialization
template <class IT>
template <typename SR, typename RHS, typename LHS>
void BiCsb<bool, IT>::SubSpMVTrans(const vector< tuple<IT,IT,IT> > & chunk, const RHS * __restrict x, LHS * __restrict suby) const
{
IT * __restrict r_bot = bot;
for(auto itr = chunk.begin(); itr != chunk.end(); ++itr)
{
// get the starting point for accessing x
IT chi = ( get<2>(*itr) << rowlowbits);
const RHS * __restrict subx = &x[chi];