-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheap_mult_generic.h
827 lines (667 loc) · 30.4 KB
/
heap_mult_generic.h
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
#ifndef MASKED_SPGEMM_HEAP_MULT_GENERIC_H
#define MASKED_SPGEMM_HEAP_MULT_GENERIC_H
#include <algorithm>
#include "CSR.h"
// TODO: move to a separate file
namespace tmp {
/**
** Count flop of SpGEMM between A and B in CSR format
**/
template<typename IT, typename NT>
long long int getFlop(const CSR<IT, NT> &A, const CSR<IT, NT> &B, IT *maxnnzc) {
long long int flop = 0; // total flop (multiplication) needed to generate C
#pragma omp parallel for reduction(+:flop)
for (IT i = 0; i < A.rows; ++i) {
long long int locmax = 0;
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; ++j) {
IT inner = A.colids[j];
IT npins = B.rowptr[inner + 1] - B.rowptr[inner];
locmax += npins;
}
maxnnzc[i] = locmax;
flop += locmax;
}
return flop * 2;
}
}
namespace heap {
template<class RandomAccessIterator, class SizeT>
[[gnu::always_inline]]
inline void make(RandomAccessIterator heap, SizeT size) {
std::make_heap(heap, heap + size);
}
template<class RandomAccessIterator, class SizeT>
[[gnu::always_inline]]
inline void pop(RandomAccessIterator heap, SizeT &size) {
std::pop_heap(heap, heap + size);
size--;
}
template<class RandomAccessIterator, class SizeT>
[[gnu::always_inline]]
inline void sinkRoot(RandomAccessIterator heap, SizeT size) {
std::pop_heap(heap, heap + size);
std::push_heap(heap, heap + size);
}
}
namespace rowAlg {
struct HeapBase {
const static bool masked = false;
template<class IT, class NT>
static IT
estimateResultSize(IT rowBeginIdx, IT rowEndIdx, IT *maxnnzc,
const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M) {
return std::accumulate(maxnnzc + rowBeginIdx, maxnnzc + rowEndIdx, 0);
}
template<class IT, class NT>
static HeapEntry<IT, void> *allocateAuxiliaryMemory(IT rowBeginIdx, IT rowEndIdx, IT *maxnnzc,
const CSR<IT, NT> &A, const CSR<IT, NT> &B,
const CSR<IT, NT> &M) {
IT threadHeapSize = 0;
for (IT i = rowBeginIdx; i < rowEndIdx; ++i) {
IT rownnz = A.rowptr[i + 1] - A.rowptr[i];
if (rownnz > threadHeapSize) { threadHeapSize = rownnz; }
}
return my_malloc<HeapEntry<IT, void>>(threadHeapSize);
};
};
struct BasicHeap : HeapBase {
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, HeapEntry<IT, void> *mergeheap,
IT &threadNvals) {
// Make initial heap for the row
IT currRowNvals = 0;
IT hsize = 0;
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; ++j) { // For all the nonzeros of the ith column
IT inner = A.colids[j]; // get the col id of A (or row id of B)
IT npins = B.rowptr[inner + 1] - B.rowptr[inner]; // get the number of nzs in B's row
if (npins == 0) { continue; }
mergeheap[hsize].loc = B.rowptr[inner];
mergeheap[hsize].runr = j; // the pointer to A.colid's is the run-rank
mergeheap[hsize++].key = B.colids[B.rowptr[inner]]; // B's first colid is the first key
}
heap::make(mergeheap, hsize);
// Traverse the heaps
while (hsize > 0) {
auto &hentry = mergeheap[0];
NT value = multop(A.values[hentry.runr], B.values[hentry.loc]);
// Use short circuiting
if ((currRowNvals > 0) && *prevColIdC == hentry.key) {
*prevValueC = addop(value, *prevValueC);
} else {
*(++prevValueC) = value;
*(++prevColIdC) = hentry.key;
currRowNvals++;
}
IT inner = A.colids[hentry.runr];
// If still unused nonzeros exists in A(:,colind), insert the next nonzero to the heap
if (++hentry.loc < B.rowptr[inner + 1]) {
hentry.key = B.colids[hentry.loc];
heap::sinkRoot(mergeheap, hsize);
} else {
heap::pop(mergeheap, hsize);
}
}
rowNvals[i] = currRowNvals;
threadNvals += currRowNvals;
}
};
template<size_t threshold>
struct HeapLinear : HeapBase {
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, HeapEntry<IT, void> *mergeheap,
IT &threadNvals) {
// Make initial heap for the row
IT currRowNvals = 0;
IT hsize = 0;
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; ++j) { // For all the nonzeros of the ith column
IT inner = A.colids[j]; // get the col id of A (or row id of B)
IT npins = B.rowptr[inner + 1] - B.rowptr[inner]; // get the number of nzs in B's row
if (npins == 0) { continue; }
mergeheap[hsize].loc = B.rowptr[inner];
mergeheap[hsize].runr = j; // the pointer to A.colid's is the run-rank
mergeheap[hsize++].key = B.colids[B.rowptr[inner]]; // B's first colid is the first key
}
if (hsize > threshold) { heap::make(mergeheap, hsize); }
// Traverse the heaps
while (hsize > 0) {
IT idx = hsize > threshold ? 0 : std::max_element(mergeheap, mergeheap + hsize) - mergeheap;
auto &hentry = mergeheap[idx];
NT value = multop(A.values[hentry.runr], B.values[hentry.loc]);
// Use short circuiting
if ((currRowNvals > 0) && *prevColIdC == hentry.key) {
*prevValueC = addop(value, *prevValueC);
} else {
*(++prevValueC) = value;
*(++prevColIdC) = hentry.key;
currRowNvals++;
}
IT inner = A.colids[hentry.runr];
// If still unused nonzeros exists in A(:,colind), insert the next nonzero to the heap
if (++hentry.loc < B.rowptr[inner + 1]) {
hentry.key = B.colids[hentry.loc];
if (hsize > threshold) {
heap::sinkRoot(mergeheap, hsize);
}
} else {
if (hsize > threshold) {
heap::pop(mergeheap, hsize);
} else {
*(mergeheap + idx) = *(mergeheap + --hsize);
}
}
}
rowNvals[i] = currRowNvals;
threadNvals += currRowNvals;
}
};
struct MaskedHeapBase {
const static bool masked = true;
template<class IT, class NT>
static IT estimateResultSize(IT rowBeginIdx, IT rowEndIdx, IT *maxnnzc,
const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M) {
IT size = 0;
for (IT row = rowBeginIdx; row < rowEndIdx; row++) {
size += std::min(maxnnzc[row], M.rowptr[row + 1] - M.rowptr[row]);
}
return size;
}
template<class IT, class NT>
static HeapEntry<IT, void> *allocateAuxiliaryMemory(IT rowBeginIdx, IT rowEndIdx, IT *maxnnzc,
const CSR<IT, NT> &A, const CSR<IT, NT> &B,
const CSR<IT, NT> &M) {
IT threadHeapSize = 0;
for (IT i = rowBeginIdx; i < rowEndIdx; ++i) {
IT rownnz = A.rowptr[i + 1] - A.rowptr[i];
if (rownnz > threadHeapSize) { threadHeapSize = rownnz; }
}
return my_malloc<HeapEntry<IT, void>>(threadHeapSize);
};
};
struct MaskedHeap_v0 : MaskedHeapBase {
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, HeapEntry<IT, void> *mergeheap,
IT &threadNvals) {
IT maskIdx = M.rowptr[i];
IT maskEnd = M.rowptr[i + 1];
// Make initial heap for the row
IT currRowNvals = 0;
IT hsize = 0;
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; ++j) { // For all the nonzeros of the ith column
IT inner = A.colids[j]; // get the col id of A (or row id of B)
IT npins = B.rowptr[inner + 1] - B.rowptr[inner]; // get the number of nzs in B's row
if (npins == 0) { continue; }
mergeheap[hsize].loc = B.rowptr[inner];
mergeheap[hsize].runr = j; // the pointer to A.colid's is the run-rank
mergeheap[hsize++].key = B.colids[B.rowptr[inner]]; // B's first colid is the first key
}
heap::make(mergeheap, hsize);
// Traverse the heaps
while (hsize > 0) {
auto &hentry = mergeheap[0];
while (maskIdx < maskEnd && hentry.key > M.colids[maskIdx]) { ++maskIdx; }
if (maskIdx >= maskEnd) { break; }
if (hentry.key == M.colids[maskIdx]) {
NT value = multop(A.values[hentry.runr], B.values[hentry.loc]);
// Use short circuiting
if ((currRowNvals > 0) && *prevColIdC == hentry.key) {
*prevValueC = addop(value, *prevValueC);
} else {
*(++prevValueC) = value;
*(++prevColIdC) = hentry.key;
currRowNvals++;
}
}
IT inner = A.colids[hentry.runr];
// If still unused nonzeros exists in A(:,colind), insert the next nonzero to the heap
if (++hentry.loc < B.rowptr[inner + 1]) {
hentry.key = B.colids[hentry.loc];
heap::sinkRoot(mergeheap, hsize);
} else {
heap::pop(mergeheap, hsize);
}
}
rowNvals[i] = currRowNvals;
threadNvals += currRowNvals;
}
};
struct MaskedHeap_v1 : MaskedHeapBase {
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, HeapEntry<IT, void> *mergeheap,
IT &threadNvals) {
IT maskIdx = M.rowptr[i];
IT maskEnd = M.rowptr[i + 1];
if (maskIdx == maskEnd) { return; }
// Make initial heap for the row
IT currRowNvals = 0;
IT hsize = 0;
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; ++j) { // For all the nonzeros of the ith column
IT inner = A.colids[j]; // get the col id of A (or row id of B)
IT npins = B.rowptr[inner + 1] - B.rowptr[inner]; // get the number of nzs in B's row
if (npins == 0) { continue; }
mergeheap[hsize].loc = B.rowptr[inner];
mergeheap[hsize].runr = j; // the pointer to A.colid's is the run-rank
mergeheap[hsize].key = B.colids[B.rowptr[inner]]; // B's first colid is the first key
while (mergeheap[hsize].key < M.colids[maskIdx] && (mergeheap[hsize].loc + 1 < B.rowptr[inner + 1])) {
mergeheap[hsize].loc++;
mergeheap[hsize].key = B.colids[mergeheap[hsize].loc];
}
// If we did not reach the end of B's row, add it to the heap
if (mergeheap[hsize].loc < B.rowptr[inner + 1]) { hsize++; }
}
heap::make(mergeheap, hsize);
// Traverse the heaps
while (hsize > 0) {
auto &hentry = mergeheap[0];
while (maskIdx < maskEnd && hentry.key > M.colids[maskIdx]) { ++maskIdx; }
if (maskIdx >= maskEnd) { break; }
if (hentry.key == M.colids[maskIdx]) {
NT value = multop(A.values[hentry.runr], B.values[hentry.loc]);
// Use short circuiting
if ((currRowNvals > 0) && *prevColIdC == hentry.key) {
*prevValueC = addop(value, *prevValueC);
} else {
*(++prevValueC) = value;
*(++prevColIdC) = hentry.key;
currRowNvals++;
}
}
IT inner = A.colids[hentry.runr];
// Before pushing the entry back to the queue, remove elements that are < than current mask element
while (++hentry.loc < B.rowptr[inner + 1]) {
hentry.key = B.colids[hentry.loc];
if (hentry.key >= M.colids[maskIdx]) { break; }
}
if (hentry.loc < B.rowptr[inner + 1]) {
heap::sinkRoot(mergeheap, hsize);
} else {
heap::pop(mergeheap, hsize);
}
}
rowNvals[i] = currRowNvals;
threadNvals += currRowNvals;
}
};
struct MaskedHeap_v2 : MaskedHeapBase {
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, HeapEntry<IT, void> *mergeheap,
IT &threadNvals) {
IT maskIdx = M.rowptr[i];
IT maskEnd = M.rowptr[i + 1];
if (maskIdx == maskEnd) { return; }
// Make initial heap for the row
IT currRowNvals = 0;
IT hsize = 0;
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; ++j) { // For all the nonzeros of the ith column
IT inner = A.colids[j]; // get the col id of A (or row id of B)
IT npins = B.rowptr[inner + 1] - B.rowptr[inner]; // get the number of nzs in B's row
if (npins == 0) { continue; }
mergeheap[hsize].loc = B.rowptr[inner];
mergeheap[hsize].runr = j; // the pointer to A.colid's is the run-rank
mergeheap[hsize].key = B.colids[B.rowptr[inner]]; // B's first colid is the first key
// Find the first match in the intersection of the mask column and the A column
IT maskIdxCopy = maskIdx;
while (true) {
if (mergeheap[hsize].key < M.colids[maskIdx]) {
if (++mergeheap[hsize].loc < B.rowptr[inner + 1]) {
mergeheap[hsize].key = B.colids[mergeheap[hsize].loc];
} else {
break;
}
} else if (mergeheap[hsize].key > M.colids[maskIdx]) {
if (++maskIdx == maskEnd) {
break;
}
} else {
hsize++;
break;
}
}
maskIdx = maskIdxCopy;
}
heap::make(mergeheap, hsize);
// Traverse the heaps
while (hsize > 0) {
auto &hentry = mergeheap[0];
while (maskIdx < maskEnd && hentry.key > M.colids[maskIdx]) { ++maskIdx; }
if (maskIdx >= maskEnd) { break; }
if (hentry.key == M.colids[maskIdx]) {
NT value = multop(A.values[hentry.runr], B.values[hentry.loc]);
// Use short circuiting
if ((currRowNvals > 0) && *prevColIdC == hentry.key) {
*prevValueC = addop(value, *prevValueC);
} else {
*(++prevValueC) = value;
*(++prevColIdC) = hentry.key;
currRowNvals++;
}
}
IT inner = A.colids[hentry.runr];
// Check if we are done with the current row from B, and if we are not move to the next element.
if (++hentry.loc >= B.rowptr[inner + 1]) {
heap::pop(mergeheap, hsize);
continue;
}
hentry.key = B.colids[hentry.loc];
// Find the first match in the intersection of
// the mask column (starting with maskIdx) and the A column (starting with hentry.loc)
IT maskIdxCopy = maskIdx;
while (true) {
if (hentry.key < M.colids[maskIdx]) {
if (++hentry.loc < B.rowptr[inner + 1]) {
hentry.key = B.colids[hentry.loc];
} else {
heap::pop(mergeheap, hsize);
break;
}
} else if (hentry.key > M.colids[maskIdx]) {
if (++maskIdx == maskEnd) {
heap::pop(mergeheap, hsize);
break;
}
} else {
// put the merge heap in the valid state again
heap::sinkRoot(mergeheap, hsize);
break;
}
}
maskIdx = maskIdxCopy;
}
rowNvals[i] = currRowNvals;
threadNvals += currRowNvals;
}
};
struct MCABase {
const static bool masked = true;
template<class IT, class NT>
static IT estimateResultSize(IT rowBeginIdx, IT rowEndIdx, IT *maxnnzc,
const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M) {
IT size = 0;
for (IT row = rowBeginIdx; row < rowEndIdx; row++) {
size += std::min(maxnnzc[row], M.rowptr[row + 1] - M.rowptr[row]);
}
return size;
}
template<class IT, class NT>
static bool *allocateAuxiliaryMemory(IT rowBeginIdx, IT rowEndIdx, IT *maxnnzc,
const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M) {
IT flagsSize = 0;
for (IT i = rowBeginIdx; i < rowEndIdx; ++i) {
IT maxMRow = M.rowptr[i + 1] - M.rowptr[i];
if (maxMRow > flagsSize) { flagsSize = maxMRow; }
}
return my_malloc<bool>(flagsSize);
};
};
struct MCA_v1 : MCABase {
const static bool masked = true;
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, bool *flags,
IT &threadNvals) {
IT maskBegin = M.rowptr[i];
const IT maskEnd = M.rowptr[i + 1];
const IT maskSize = maskEnd - maskBegin;
prevColIdC++;
prevValueC++;
std::fill(flags, flags + maskSize, false);
// Iterate though nonzeros in the A's current row
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; j++) {
const IT inner = A.colids[j];
IT loc = B.rowptr[inner];
IT key = A.colids[loc];
if (loc == B.rowptr[inner + 1]) { continue; }
IT maskIdx = maskBegin;
// Find the intersection between the mask's row and the A's row
while (true) {
if (key < M.colids[maskIdx]) {
if (++loc < B.rowptr[inner + 1]) { key = B.colids[loc]; } else { break; }
} else if (key > M.colids[maskIdx]) {
if (++maskIdx == maskEnd) { break; }
} else {
// colid is found in both arrays
const auto idx = maskIdx - maskBegin;
const NT value = multop(A.values[j], B.values[loc]);
if (!flags[idx]) {
prevValueC[idx] = value;
flags[idx] = true;
} else {
prevValueC[idx] = addop(prevValueC[idx], value);
}
if (++loc < B.rowptr[inner + 1]) { key = B.colids[loc]; } else { break; }
if (++maskIdx == maskEnd) { break; }
}
}
}
/* Remove empty values the destination arrays and set row IDs */
size_t dst = 0;
for (size_t src = 0; src < maskSize; src++) {
if (flags[src]) {
prevColIdC[dst] = M.colids[maskBegin + src];
prevValueC[dst] = prevValueC[src];
dst++;
}
}
prevColIdC += dst - 1;
prevValueC += dst - 1;
rowNvals[i] = dst;
threadNvals += dst;
}
};
struct MCA_v2 : MCABase {
const static bool masked = true;
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, bool *flags,
IT &threadNvals) {
IT maskBegin = M.rowptr[i];
const IT maskEnd = M.rowptr[i + 1];
const IT maskSize = maskEnd - maskBegin;
// Since prev***C point to the previous element, increment them
prevColIdC++;
prevValueC++;
std::fill(flags, flags + maskSize, false);
// Iterate though nonzeros in the A's current row
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; j++) {
const IT inner = A.colids[j];
IT loc = B.rowptr[inner];
IT key = A.colids[loc];
if (loc == B.rowptr[inner + 1]) { continue; }
// Find the intersection between the mask's row and the A's row
for (IT maskIdx = maskBegin; maskIdx < maskEnd; maskIdx++) {
while (key < M.colids[maskIdx]) {
if (++loc < B.rowptr[inner + 1]) { key = B.colids[loc]; } else { goto outerLoopBreak; }
}
if (key == M.colids[maskIdx]) {
// colid is found in both arrays
const auto idx = maskIdx - maskBegin;
const NT value = multop(A.values[j], B.values[loc]);
if (!flags[idx]) {
prevValueC[idx] = value;
flags[idx] = true;
} else {
prevValueC[idx] = addop(prevValueC[idx], value);
}
if (++loc < B.rowptr[inner + 1]) { key = B.colids[loc]; } else { break; }
}
}
outerLoopBreak:
continue;
}
/* Remove empty values the destination arrays and set row IDs */
size_t dst = 0;
for (size_t src = 0; src < maskSize; src++) {
if (flags[src]) {
prevColIdC[dst] = M.colids[maskBegin + src];
prevValueC[dst] = prevValueC[src];
dst++;
}
}
prevColIdC += dst - 1;
prevValueC += dst - 1;
rowNvals[i] = dst;
threadNvals += dst;
}
};
struct MCA_v3 : MCABase {
const static bool masked = true;
template<typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
[[gnu::always_inline]]
static void row(const CSR<IT, NT> &A, const CSR<IT, NT> &B, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, IT i,
IT *rowNvals, IT *&prevColIdC, NT *&prevValueC, bool *flags,
IT &threadNvals) {
const auto maskBegin = &M.colids[M.rowptr[i]];
const auto maskEnd = &M.colids[M.rowptr[i + 1]];
const auto maskSize = maskEnd - maskBegin;
prevColIdC++;
prevValueC++;
std::fill(flags, flags + maskSize, false);
// Iterate though nonzeros in the A's current row
for (IT j = A.rowptr[i]; j < A.rowptr[i + 1]; j++) {
const IT inner = A.colids[j];
auto colIdsIt = &B.colids[B.rowptr[inner]];
const auto colIdsBegin = &B.colids[B.rowptr[inner]];
const auto colIdsEnd = &B.colids[B.rowptr[inner + 1]];
auto maskIt = maskBegin;
if (colIdsIt == colIdsEnd) { continue; }
// Find the intersection between the mask's row and the A's row
while (true) {
if (*colIdsIt < *maskIt) {
if (++colIdsIt == colIdsEnd) { break; }
} else if (*colIdsIt > *maskIt) {
if (++maskIt == maskEnd) { break; }
} else {
// colid is found in both arrays
const auto idx = maskIt - maskBegin;
const NT value = multop(A.values[j], B.values[B.rowptr[inner] + colIdsIt - colIdsBegin]);
if (!flags[idx]) {
prevValueC[idx] = value;
flags[idx] = true;
} else {
prevValueC[idx] = addop(prevValueC[idx], value);
}
if (++colIdsIt >= colIdsEnd) { break; }
if (++maskIt == maskEnd) { break; }
}
}
}
/* Remove empty values the destination arrays and set row IDs */
size_t dst = 0;
for (size_t src = 0; src < maskSize; src++) {
if (flags[src]) {
prevColIdC[dst] = maskBegin[src];
prevValueC[dst] = prevValueC[src];
dst++;
}
}
prevColIdC += dst - 1;
prevValueC += dst - 1;
rowNvals[i] = dst;
threadNvals += dst;
}
};
}
template<bool masked, class RowAlgorithm, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
void HeapSpGEMMImpl(const CSR<IT, NT> &A, const CSR<IT, NT> &B, CSR<IT, NT> &C, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, unsigned numThreads) {
static_assert(masked == RowAlgorithm::masked || masked, "Row algorithm does not support mask.");
static_assert(masked == RowAlgorithm::masked || !masked, "Row algorithm is used for masked computation.");
if (numThreads == 0) {
#pragma omp parallel
#pragma omp single
numThreads = omp_get_num_threads();
}
if (!C.isEmpty()) { C.make_empty(); }
C.rows = A.rows;
C.cols = B.cols;
// Load-balancing Thread Scheduling
IT *maxnnzc = my_malloc<IT>(A.rows);
long long int flops = tmp::getFlop(A, B, maxnnzc) / 2;
IT flopsPerThread = flops / numThreads; // amount of work that will be assigned to each thread
IT *rowStart = my_malloc<IT>(A.rows); //start index in the global array for storing ith column of C
IT *rowNvals = my_malloc<IT>(A.rows); // number of nonzeros in each each column in C
rowStart[0] = 0;
// Global space used to store result
IT *threadsNvals = my_malloc<IT>(numThreads);
// Parallelized version
scan(maxnnzc, rowStart, A.rows);
// ************************ Numeric Phase *************************************
#pragma omp parallel num_threads(numThreads)
{
int thisThread = omp_get_thread_num();
// @formatter:off
IT rowBegin = thisThread != 0 ? (lower_bound(rowStart, rowStart + A.rows, flopsPerThread * thisThread)) - rowStart : 0;
IT rowEnd = thisThread != numThreads - 1 ? (lower_bound(rowStart, rowStart + A.rows, flopsPerThread * (thisThread + 1))) - rowStart : A.rows;
// @formatter:on
IT localsum = RowAlgorithm::estimateResultSize(rowBegin, rowEnd, maxnnzc, A, B, M);
// We need +1 even though the first element of the array is never accessed.
// However, the first element may be prefetched so we have to allocate it together with the rest of the array.
IT *colIdsLocalMem = my_malloc<IT>(localsum + 1);
NT *valuesLocalMem = my_malloc<NT>(localsum + 1);
IT *prevColIdC = colIdsLocalMem;
NT *prevValueC = valuesLocalMem;
auto auxMemory = RowAlgorithm::allocateAuxiliaryMemory(rowBegin, rowEnd, maxnnzc, A, B, M);
IT threadNvals = 0;
// Iterate through all rows in A
for (IT i = rowBegin; i < rowEnd; ++i) {
RowAlgorithm::row(A, B, M, multop, addop, i, rowNvals, prevColIdC, prevValueC, auxMemory, threadNvals);
}
threadsNvals[thisThread] = threadNvals;
my_free(auxMemory);
#pragma omp barrier
#pragma omp master
{
C.rowptr = my_malloc<IT>(C.rows + 1);
C.rowptr[0] = 0;
C.nnz = std::accumulate(threadsNvals, threadsNvals + numThreads, IT(0));;
C.colids = my_malloc<IT>(C.nnz);
C.values = my_malloc<NT>(C.nnz);
}
IT rowPtrOffset = std::accumulate(threadsNvals, threadsNvals + thisThread, IT(0));
#pragma omp barrier
// set rowptr in C for local rows
for (IT i = rowBegin; i < rowEnd; ++i) {
C.rowptr[i] = rowPtrOffset;
rowPtrOffset += rowNvals[i];
}
if (thisThread == numThreads - 1) { C.rowptr[C.rows] = rowPtrOffset; }
// copy local values to C
copy(colIdsLocalMem + 1, colIdsLocalMem + threadNvals + 1, C.colids + C.rowptr[rowBegin]);
copy(valuesLocalMem + 1, valuesLocalMem + threadNvals + 1, C.values + C.rowptr[rowBegin]);
my_free<IT>(colIdsLocalMem);
my_free<NT>(valuesLocalMem);
}
my_free<IT>(maxnnzc);
my_free<IT>(rowStart);
my_free<IT>(rowNvals);
}
template<class RowAlgorithm, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
void HeapSpGEMM(const CSR<IT, NT> &A, const CSR<IT, NT> &B, CSR<IT, NT> &C,
MultiplyOperation multop, AddOperation addop, unsigned numThreads = 0) {
HeapSpGEMMImpl<false, RowAlgorithm>(A, B, C, CSR<IT, NT>{}, multop, addop, numThreads);
}
template<class RowAlgorithm, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
void HeapSpGEMM(const CSR<IT, NT> &A, const CSR<IT, NT> &B, CSR<IT, NT> &C, const CSR<IT, NT> &M,
MultiplyOperation multop, AddOperation addop, unsigned numThreads = 0) {
HeapSpGEMMImpl<true, RowAlgorithm>(A, B, C, M, multop, addop, numThreads);
}
#endif //MASKED_SPGEMM_HEAP_MULT_GENERIC_H