-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash_mult.h
More file actions
1489 lines (1349 loc) · 57.7 KB
/
Copy pathhash_mult.h
File metadata and controls
1489 lines (1349 loc) · 57.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <immintrin.h>
#include <algorithm>
#include <fstream>
#include <x86intrin.h>
#include "utility.h"
#include "CSR.h"
#include "BIN.h"
#define VECTORIZE
/* SpGEMM Specific Parameters */
#define HASH_SCAL 107 // Set disjoint number to hash table size (=2^n)
#define SMALL_THRESHOLD 100
#define MIN_HT_S 8 // minimum hash table size per row in symbolic phase
#define MIN_HT_N 8 // minimum hash table size per row in numeric phase
#define VEC_LENGTH 8
#define VEC_LENGTH_BIT 3
#define VEC_LENGTH_LONG 4
#define VEC_LENGTH_LONG_BIT 2
#ifdef PRINT_METADATA_TO_FILE
/*
* Symbolic phase to Calculate NNZ Entries in per-row of SpGEMM output.
*/
template <class IT, class NT>
inline void symbolic_maxnnz_kernel(const IT *arpt, const IT *acol, const IT *brpt, const IT *bcol, BIN<IT, NT> &bin)
{
IT *row_nz_mx = my_malloc<IT>(bin.num_rows);
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.rows_offset[tid];
IT end_row = bin.rows_offset[tid + 1];
IT *check = bin.local_hash_table_id[tid];
// rows of A
for (IT i = start_row; i < end_row; ++i) {
IT nz = 0;
// columns of A
for (IT j = arpt[i]; j < arpt[i + 1]; ++j) {
IT t_acol = acol[j];
// nnz columns in B for row t_acol
nz += (brpt[t_acol + 1] - brpt[t_acol]);
}
row_nz_mx[i] = nz;
}
}
// string filename = "/home/aislam6/passionlab/nnz_frequency_predict.txt";
// std::fstream file(filename, std::ios::out);
// for (IT i = 0; i < bin.num_rows; ++i) {
// file << i << " " << row_nz_mx[i] << endl;
// }
// file.close();
my_free<IT>(row_nz_mx);
}
#endif
/*
* Symbolic phase for Hash SpGEMM.
*/
template <class IT, class NT>
inline void hash_symbolic_kernel(const IT *arpt, const IT *acol, const IT *brpt, const IT *bcol,
BIN<IT, NT> &bin, const string& out_nnz_freq_outfile, bool print_to_file = false) {
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.rows_offset[tid];
IT end_row = bin.rows_offset[tid + 1];
IT *check = bin.local_hash_table_id[tid];
for (IT i = start_row; i < end_row; ++i) {
IT nz = 0;
IT bid = bin.bin_id[i];
if (bid > 0) {
IT ht_size = MIN_HT_S << (bid - 1); // determine hash table size for i-th row
for (IT j = 0; j < ht_size; ++j) { // initialize hash table
check[j] = -1;
}
for (IT j = arpt[i]; j < arpt[i + 1]; ++j) {
IT t_acol = acol[j];
for (IT k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
IT key = bcol[k];
IT hash = (key * HASH_SCAL) & (ht_size - 1);
while (1) { // Loop for hash probing
if (check[hash] == key) { // if the key is already inserted, it's ok
break;
}
else if (check[hash] == -1) { // if the key has not been inserted yet, then it's added.
check[hash] = key;
nz++;
break;
}
else { // linear probing: check next entry
hash = (hash + 1) & (ht_size - 1); //hash = (hash + 1) % ht_size
}
}
}
}
}
bin.row_nz[i] = nz;
}
}
#ifdef PRINT_METADATA_TO_FILE
if(print_to_file) {
std::fstream file(out_nnz_freq_outfile, std::ios::out);
for (IT i = 0; i < bin.num_rows; ++i) {
file << i << " " << bin.row_nz[i] << endl;
}
file.close();
}
#endif
}
/*
* Symbolic phase for Hash Vector SpGEMM
* This function is optimized for 32-bit integer with AVX2.
*/
template <class NT>
inline void hash_symbolic_vec_kernel(const int *arpt, const int *acol, const int *brpt, const int *bcol, BIN<int, NT> &bin)
{
#ifdef VECTORIZE
const __m256i init_m = _mm256_set1_epi32(-1);
const __m256i true_m = _mm256_set1_epi32(0xffffffff);
#endif
#pragma omp parallel
{
int tid = omp_get_thread_num();
int start_row = bin.rows_offset[tid];
int end_row = bin.rows_offset[tid + 1];
int *check = bin.local_hash_table_id[tid];
for (int i = start_row; i < end_row; ++i) {
#ifdef VECTORIZE
__m256i key_m, check_m;
__m256i mask_m;
int mask;
#endif
int nz = 0;
int bid = bin.bin_id[i];
if (bid > 0) {
int table_size = MIN_HT_S << (bid - 1); // the number of entries per table
int ht_size = table_size >> VEC_LENGTH_BIT; // the number of chunks (1 chunk = VEC_LENGTH elments)
for (int j = 0; j < table_size; ++j) {
check[j] = -1; // initialize hash table
}
for (int j = arpt[i]; j < arpt[i + 1]; ++j) {
int t_acol = acol[j];
for (int k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
int key = bcol[k];
int hash = (key * HASH_SCAL) & (ht_size - 1);
#ifdef VECTORIZE
key_m = _mm256_set1_epi32(key);
#endif
while (1) { // Loop for hash probing
// check whether the key is in hash table.
#ifdef VECTORIZE
check_m = _mm256_maskload_epi32(check + (hash << VEC_LENGTH_BIT), true_m);
mask_m = _mm256_cmpeq_epi32(key_m, check_m);
mask = _mm256_movemask_epi8(mask_m);
if (mask != 0) {
break;
}
#else
bool flag = false;
#pragma simd
for (int l = 0; l < VEC_LENGTH; ++l) {
if (check[(hash << VEC_LENGTH_BIT) + l] == key) {
flag = true;
}
}
if (flag) {
break;
}
#endif
else {
// If the entry with same key cannot be found, check whether the chunk is filled or not
int cur_nz;
#ifdef VECTORIZE
mask_m = _mm256_cmpeq_epi32(check_m, init_m);
mask = _mm256_movemask_epi8(mask_m);
cur_nz = (32 - _popcnt32(mask)) >> 2;
#else
cur_nz = VEC_LENGTH;
#pragma simd
for (int l = VEC_LENGTH - 1; l >= 0; --l) {
if (check[(hash << VEC_LENGTH_BIT) + l] == -1) {
cur_nz = l;
}
}
#endif
if (cur_nz < VEC_LENGTH) { //if it is not filled, push the entry to the table
check[(hash << VEC_LENGTH_BIT) + cur_nz] = key;
nz++;
break;
}
else { // if is filled, check next chunk (linear probing)
hash = (hash + 1) & (ht_size - 1);
}
}
}
}
}
}
bin.row_nz[i] = nz;
}
}
}
template <class NT>
inline void hash_symbolic_vec_kernel(const long long int *arpt, const long long int *acol, const long long int *brpt, const long long int *bcol, BIN<long long int, NT> &bin)
{
#ifdef VECTORIZE
const __m256i init_m = _mm256_set1_epi64x(-1);
const __m256i true_m = _mm256_set1_epi64x(0xffffffffffffffff);
#endif
#pragma omp parallel
{
long long int tid = omp_get_thread_num();
long long int start_row = bin.rows_offset[tid];
long long int end_row = bin.rows_offset[tid + 1];
long long int *check = bin.local_hash_table_id[tid];
for (long long int i = start_row; i < end_row; ++i) {
#ifdef VECTORIZE
__m256i key_m, check_m;
__m256i mask_m;
int mask;
#endif
long long int nz = 0;
long long int bid = bin.bin_id[i];
if (bid > 0) {
long long int table_size = MIN_HT_S << (bid - 1);
long long int ht_size = table_size >> VEC_LENGTH_LONG_BIT;
for (long long int j = 0; j < table_size; ++j) {
check[j] = -1;
}
for (long long int j = arpt[i]; j < arpt[i + 1]; ++j) {
long long int t_acol = acol[j];
for (long long int k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
long long int key = bcol[k];
long long int hash = (key * HASH_SCAL) & (ht_size - 1);
#ifdef VECTORIZE
key_m = _mm256_set1_epi64x(key);
#endif
while (1) {
#ifdef VECTORIZE
check_m = _mm256_maskload_epi64(check + (hash << VEC_LENGTH_LONG_BIT), true_m);
mask_m = _mm256_cmpeq_epi64(key_m, check_m);
mask = _mm256_movemask_epi8(mask_m);
if (mask != 0) {
break;
}
#else
bool flag = false;
#pragma simd
for (int l = 0; l < VEC_LENGTH_LONG; ++l) {
if (check[(hash << VEC_LENGTH_LONG_BIT) + l] == key) {
flag = true;
}
}
if (flag) {
break;
}
#endif
else {
long long int cur_nz;
#ifdef VECTORIZE
mask_m = _mm256_cmpeq_epi64(check_m, init_m);
mask = _mm256_movemask_epi8(mask_m);
cur_nz = (32 - _popcnt32(mask)) >> 3;
#else
cur_nz = VEC_LENGTH_LONG;
#pragma simd
for (int l = VEC_LENGTH_LONG - 1; l >= 0; --l) {
if (check[(hash << VEC_LENGTH_LONG_BIT) + l] == -1) {
cur_nz = l;
}
}
#endif
if (cur_nz < VEC_LENGTH_LONG) {
check[(hash << VEC_LENGTH_LONG_BIT) + cur_nz] = key;
nz++;
break;
}
else {
hash = (hash + 1) & (ht_size - 1);
}
}
}
}
}
}
bin.row_nz[i] = nz;
}
}
}
// Reference function for Symbolic phase of Hash SpGEMM
template <bool vectorProbing, class IT, class NT>
inline void hash_symbolic(const IT *arpt, const IT *acol, const IT *brpt, const IT *bcol, IT *crpt, BIN<IT, NT> &bin, const IT nrow, IT *nnz, string out_nnz_freq_outfile, bool first_pass = false)
{
// if (vectorProbing) {
// hash_symbolic_vec_kernel(arpt, acol, brpt, bcol, bin);
// }
// else {
hash_symbolic_kernel(arpt, acol, brpt, bcol, bin, out_nnz_freq_outfile, first_pass);
#ifdef PRINT_METADATA_TO_FILE
// if(first_pass) symbolic_maxnnz_kernel(arpt, acol, brpt, bcol, bin);
#endif
// }
/* Set row pointer of matrix C */
// cout << "bin.row_nz[nrow-1]: " << bin.row_nz[nrow - 1] << ", bin.row_nz[nrow]: " << bin.row_nz[nrow] << endl;
scan(bin.row_nz, crpt, nrow + 1);
// cout << "crpt[nrow]: " << crpt[nrow] << endl;
*nnz = crpt[nrow];
}
// Reference function for Symbolic phase of Hash SpGEMM
template <class IT, class NT>
inline void hash_symbolic_topK(const IT *arpt, const IT *acol,
const IT *brpt, const IT *bcol, IT *crpt,
BIN<IT, NT> &bin, const IT nrow, IT *nnz, IT topK,
string out_nnz_freq_outfile, bool first_pass = false)
{
hash_symbolic_kernel(arpt, acol, brpt, bcol, bin, out_nnz_freq_outfile, first_pass);
/* Set row pointer of matrix C */
// scan(topK, crpt, nrow + 1);
// *nnz = nrow * topK;
scan(bin.row_nz, crpt, nrow + 1);
*nnz = crpt[nrow];
}
/*
* Used for sort function.
* Elements are sorted in ascending order.
*/
template <typename IT, typename NT>
bool sort_less(const pair<IT, NT> &left,const pair<IT, NT> &right)
{
return left.first < right.first;
}
/*
* Numeric phase in Hash SpGEMM.
*/
template <typename IT>
inline void sanity_check(const IT *crpt, IT cnnz, IT num_rows) {
// for(IT i=1; i<num_rows+1; i+=1) {
// if(crpt[i] < 0) {
// cout << i << " " << crpt[i] << endl;
// break;
// }
// }
// cout << "cnnz: " << cnnz << endl;
assert(cnnz > 0 && "NNZ should be a positive number!");
for(IT i=1; i<num_rows; i+=1) {
assert(crpt[i] >= crpt[i - 1] && "C_ptr is wrongly calculated!");
}
assert(crpt[num_rows - 1] < cnnz && "C_ptr should not go beyond C_nnz");
}
/*
* After calculating on each hash table, sort them in ascending order if necessary, and then store them as output matrix
* This function is used in hash_numeric* function.
* the actual indices of colids and values of output matrix are rpt[rowid];
*/
//sort_and_store_table2mat<sortOutput, IT, NT>(ht_check, ht_value, ccol + offset, cval + offset, crpt[i + 1] - offset, ht_size, cnnz - offset);
template <bool sortOutput, typename IT, typename NT>
inline void sort_and_store_table2mat(IT *ht_check, NT *ht_value, IT *colids, NT * values, IT nz, IT ht_size, IT offset, IT row_id = -1)
{
// assert(nz > 0 && "NZ can not be negative!");
IT index = 0;
// Sort elements in ascending order if necessary, and store them as output matrix
if (sortOutput) {
vector<pair<IT, NT>> p_vec(nz);
for (IT j = 0; j < ht_size; ++j) { // accumulate non-zero entry from hash table
if (ht_check[j] != -1) {
p_vec[index++] = make_pair(ht_check[j], ht_value[j]);
}
}
// assert(index <= nz && "Index goes beyond p_vector limit");
// assert(index < offset && "Index goes beyond output limit");
sort(p_vec.begin(), p_vec.end(), sort_less<IT, NT>); // sort only non-zero elements
for (IT j = 0; j < index; ++j) { // store the results
// IT first = p_vec[j].first;
// NT second = p_vec[j].second;
colids[j] = p_vec[j].first;
values[j] = p_vec[j].second;
}
}
else {
for (IT j = 0; j < ht_size; ++j) {
if (ht_check[j] != -1) {
// if(row_id == 0) {
// cout << "j: " << j << ", index: " << index << endl;
// }
colids[index] = ht_check[j];
values[index] = ht_value[j];
index++;
}
}
}
}
/*
* After calculating on each hash table, sort them in ascending order if necessary, and then store them as output matrix
* This function is used in hash_numeric* function.
* the actual indices of colids and values of output matrix are rpt[rowid];
*/
template<bool sortOutput, typename IT, typename NT>
inline void
sort_and_store_table2mat_topK(IT *ht_check, NT *ht_value, IT *colids, NT *values, IT nz, IT ht_size, IT topK) {
// assert(nz > 0 && "NZ can not be negative!");
IT index = 0;
if (sortOutput) {
// Sort elements in ascending order if necessary, and store them as output matrix
vector <pair<NT, IT>> p_vec(nz);
for (IT j = 0; j < ht_size; ++j) { // accumulate non-zero entry from hash table
if (ht_check[j] != -1) {
p_vec[index++] = make_pair(ht_value[j], ht_check[j]);
}
}
// assert(index <= nz && "Index goes beyond p_vector limit");
// assert(index < offset && "Index goes beyond output limit");
sort(p_vec.begin(), p_vec.end(), sort_large<IT, NT>); // sort only non-zero elements
// index = min(index, topK);
for (IT j = 0; j < index; ++j) { // store the results
colids[j] = p_vec[j].second;
values[j] = p_vec[j].first;
}
}
else {
for (IT j = 0; j < ht_size; ++j) {
if (ht_check[j] != -1) {
colids[index] = ht_check[j];
values[index] = ht_value[j];
index++;
}
}
}
}
/*
* After calculating on each hash table, sort them in ascending order if necessary, and then store them as output matrix
* This function is used in hash_numeric* function.
* the actual indices of colids and values of output matrix are rpt[rowid];
*/
template<bool sortOutput, typename IT, typename NT>
inline void
sort_and_store_table2mat_topK_jaccard(IT *ht_check, NT *ht_value, IT *colids, NT *values, IT nz, IT ht_size, IT topK,
const IT *arpt, IT a_row_id) {
// assert(nz > 0 && "NZ can not be negative!");
IT index = 0;
if (sortOutput) {
// Sort elements in ascending order if necessary, and store them as output matrix
vector <pair<NT, IT>> p_vec(nz);
for (IT j = 0; j < ht_size; ++j) { // accumulate non-zero entry from hash table
if (ht_check[j] != -1) {
IT u = (arpt[a_row_id + 1] - arpt[a_row_id]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]]) - ht_value[j];
NT jacc = (u == 0) ? 0.0 : 1.0 * ht_value[j] / u;
p_vec[index++] = make_pair(jacc, ht_check[j]);
// p_vec[index++] = make_pair(ht_value[j], ht_check[j]);
}
}
// assert(index <= nz && "Index goes beyond p_vector limit");
// assert(index < offset && "Index goes beyond output limit");
sort(p_vec.begin(), p_vec.end(), sort_large<IT, NT>); // sort only non-zero elements
// index = min(index, topK);
for (IT j = 0; j < index; ++j) { // store the results
colids[j] = p_vec[j].second;
values[j] = p_vec[j].first;
}
}
else {
IT u;
NT jacc;
for (IT j = 0; j < ht_size; ++j) {
if (ht_check[j] != -1) {
u = (arpt[a_row_id + 1] - arpt[a_row_id]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]]) - ht_value[j];
jacc = (u == 0) ? 0.0 : 1.0 * ht_value[j] / u;
colids[index] = ht_check[j];
values[index] = jacc;
index++;
}
}
}
}
/*
* After calculating on each hash table, sort them in ascending order if necessary, and then store them as output matrix
* This function is used in hash_numeric* function.
* the actual indices of colids and values of output matrix are rpt[rowid];
*/
template<bool sortOutput, typename IT, typename NT>
inline void
sort_and_store_table2mat_topK_cf(IT *ht_check, NT *ht_value, IT *colids, NT *values, IT nz, IT ht_size, IT topK,
const IT *arpt, IT a_row_id) {
// assert(nz > 0 && "NZ can not be negative!");
IT index = 0;
if (sortOutput) {
// Sort elements in ascending order if necessary, and store them as output matrix
vector <pair<NT, IT>> p_vec(nz);
for (IT j = 0; j < ht_size; ++j) { // accumulate non-zero entry from hash table
if (ht_check[j] != -1) {
IT u = (arpt[a_row_id + 1] - arpt[a_row_id]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]]) - ht_value[j];
NT jacc = (u == 0) ? 0.0 : 1.0 * ht_value[j] / u;
NT annz = ((arpt[a_row_id + 1] - arpt[a_row_id]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]])) / 2.0;
NT cf = u / annz / jacc;
p_vec[index++] = make_pair(cf, ht_check[j]);
// p_vec[index++] = make_pair(ht_value[j], ht_check[j]);
}
}
// assert(index <= nz && "Index goes beyond p_vector limit");
// assert(index < offset && "Index goes beyond output limit");
sort(p_vec.begin(), p_vec.end(), sort_large<IT, NT>); // sort only non-zero elements
// index = min(index, topK);
for (IT j = 0; j < index; ++j) { // store the results
colids[j] = p_vec[j].second;
values[j] = p_vec[j].first;
}
}
else {
IT u;
NT jacc, annz, cf;
for (IT j = 0; j < ht_size; ++j) {
if (ht_check[j] != -1) {
u = (arpt[a_row_id + 1] - arpt[a_row_id]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]]) - ht_value[j];
jacc = (u == 0) ? 0.0 : 1.0 * ht_value[j] / u;
annz = ((arpt[a_row_id + 1] - arpt[a_row_id]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]])) / 2.0;
cf = u / annz / jacc;
colids[index] = ht_check[j];
values[index] = cf;
index++;
}
}
}
}
/*
* Numeric phase in Hash SpGEMM.
*/
template <bool sortOutput, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
inline void hash_numeric(const IT *arpt, const IT *acol, const NT *aval, const IT *brpt, const IT *bcol, const NT *bval,
const IT *crpt, IT *ccol, NT *cval, const BIN<IT, NT> &bin,
const MultiplyOperation multop, const AddOperation addop, IT cnnz)
{
// int numThreads;
//#pragma omp parallel
// {
// numThreads = omp_get_num_threads();
// }
// long long int* flops_per_thread = my_malloc<long long int>(numThreads);
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.rows_offset[tid];
IT end_row = bin.rows_offset[tid + 1];
IT *ht_check = bin.local_hash_table_id[tid];
NT *ht_value = bin.local_hash_table_val[tid];
// long long int flops = 0;
// row-id: 1, 2
// [1]: 1, 2, 3
// [2]: 1, 2, 4
// union-of-col-ids: 1, 2, 3, 4
// cluster_size = 2
// // prepare this for every clusters
// vector < map< col-id, vector<NT>> > col_map
// for(int r=0; r<num_rows; r+=cluster_sz) {
// cluster_id = r / 2;
// for(i=r; i< r+cluster_sz; i += 1) {
// for(IT j = arpt[i]; j < arpt[i + 1]; ++j) {
// IT t_acol = acol[j];
// NT t_aval = aval[j];
// if (col_map[cluster_id].find(t_acol) == col_map[cluster_id].end()) {
// col_map[cluster_id][t_acol] = vector<NT>(cluster_size, 0.0);
// }
// col_map[cluster_id][t_acol][i-r] = t_aval;
// }
// }
// }
// col-map
// col 1: <1, val1>, <2, val2>
// col 2: <1, val1>, <2, val2>
// col 3: <1, val1>
// col 4: <2, val2>
//
// col 1: [val1, val2]
// col 2: [val1, val2]
// col 3: [val1, 0]
// col 4: [0, val2]
// create a new BIN class that can process clusters instead of rows
// for each cluster, we will need to calculate flops, nnz for the union of col-ids
// allocate memory for values: nnz * cluster_size
// hash-table:
// col-ids: [1, 2, 3, 4]
// values: [[val1, val2], [val1, val2], [val1, 0], [0, val2]]
//
// A_val: vector< pair<row-id, value> >
// clusters
// for (IT c = start_cluster; c < end_cluster; ++c) {
for (IT i = start_row; i < end_row; ++i) {
// todo: prepare a new BIN class based on the cluster
IT bid = bin.bin_id[i];
if (bid > 0) {
IT offset = crpt[i];
IT ht_size = MIN_HT_N << (bid - 1);
for (IT j = 0; j < ht_size; ++j) {
ht_check[j] = -1;
}
// union of col-ids of cluster
for (IT j = arpt[i]; j < arpt[i + 1]; ++j) { // A.cols
IT t_acol = acol[j];
NT t_aval = aval[j];
// B[i]
for (IT k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
NT t_val = multop(t_aval, bval[k]);
// for(pair<IT, NT> t: col_map[i])
// flops +=1;
IT key = bcol[k];
IT hash = (key * HASH_SCAL) & (ht_size - 1);
while (1) { // Loop for hash probing
if (ht_check[hash] == key) { // key is already inserted
ht_value[hash] = addop(t_val, ht_value[hash]);
// flops += 1;
break;
}
else if (ht_check[hash] == -1) { // insert new entry
ht_check[hash] = key;
ht_value[hash] = t_val;
break;
}
else {
hash = (hash + 1) & (ht_size - 1); // (hash + 1) % ht_size
}
}
}
}
// if(crpt[i + 1] - offset == 0) {
// cout << "i: " << i << ", bid: " << bid << endl;
// }
// if(i == 0) {
// cout << "i: " << i << ", bid: " << bid << ", offset: " << offset << ", next-offset: " << crpt[i + 1] << endl;
// }
sort_and_store_table2mat<sortOutput, IT, NT>(ht_check, ht_value,
ccol + offset, cval + offset,
crpt[i + 1] - offset, ht_size, cnnz - offset, i);
}
}
// flops_per_thread[tid] = flops;
}
// sort(flops_per_thread, flops_per_thread+numThreads);
// cout << "min-flops: " << flops_per_thread[0] << " max-flops: " << flops_per_thread[numThreads - 1] << endl;
}
/*
* Numeric phase in Hash SpGEMM.
*/
template<bool sortOutput, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
inline void
hash_numeric_topk(const IT *arpt, const IT *acol, const NT *aval, const IT *brpt, const IT *bcol, const NT *bval,
const IT *crpt, IT *ccol, NT *cval, const BIN<IT, NT> &bin,
const MultiplyOperation multop, const AddOperation addop, IT topK) {
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.rows_offset[tid];
IT end_row = bin.rows_offset[tid + 1];
IT *ht_check = bin.local_hash_table_id[tid];
NT *ht_value = bin.local_hash_table_val[tid];
for (IT i = start_row; i < end_row; ++i) {
IT bid = bin.bin_id[i];
if (bid > 0) {
IT offset = crpt[i];
IT ht_size = MIN_HT_N << (bid - 1);
for (IT j = 0; j < ht_size; ++j) {
ht_check[j] = -1;
}
// union of col-ids of cluster
for (IT j = arpt[i]; j < arpt[i + 1]; ++j) { // A.cols
IT t_acol = acol[j];
NT t_aval = aval[j];
// B[i]
for (IT k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
NT t_val = multop(t_aval, bval[k]);
IT key = bcol[k];
IT hash = (key * HASH_SCAL) & (ht_size - 1);
while (1) { // Loop for hash probing
if (ht_check[hash] == key) { // key is already inserted
ht_value[hash] = addop(t_val, ht_value[hash]);
break;
} else if (ht_check[hash] == -1) { // insert new entry
ht_check[hash] = key;
ht_value[hash] = t_val;
break;
} else {
hash = (hash + 1) & (ht_size - 1); // (hash + 1) % ht_size
}
}
}
}
// sort_and_store_table2mat_topK<sortOutput, IT, NT>(ht_check, ht_value,
// ccol + offset, cval + offset,
// crpt[i + 1] - offset, ht_size, topK);
sort_and_store_table2mat_topK_jaccard<sortOutput, IT, NT>(ht_check, ht_value,
ccol + offset, cval + offset,
crpt[i + 1] - offset, ht_size, topK,
arpt, i);
// assert(!sortOutput && "can't hande for sortOutput=True");
// IT u, index = 0;
// NT jacc;
// for (IT j = 0; j < ht_size; ++j) {
// if (ht_check[j] != -1) {
// u = (arpt[i + 1] - arpt[i]) + (arpt[ht_check[j] + 1] - arpt[ht_check[j]]) - ht_value[j];
// jacc = (u == 0) ? 0.0 : 1.0 * ht_value[j] / u;
//
// ccol[offset+index] = ht_check[j];
// cval[offset+index] = jacc;
// index++;
// }
// }
// sort_and_store_table2mat_topK_cf<sortOutput, IT, NT>(ht_check, ht_value,
// ccol + offset, cval + offset,
// crpt[i + 1] - offset, ht_size, topK,
// arpt, i);
}
}
}
// sort(flops_per_thread, flops_per_thread+numThreads);
// cout << "min-flops: " << flops_per_thread[0] << " max-flops: " << flops_per_thread[numThreads - 1] << endl;
}
template <bool sortOutput, typename NT, typename MultiplyOperation, typename AddOperation>
inline void hash_numeric_vec(const int *arpt, const int *acol, const NT *aval, const int *brpt, const int *bcol, const NT *bval, const int *crpt, int *ccol, NT *cval, const BIN<int, NT> &bin, MultiplyOperation multop, AddOperation addop)
{
#ifdef VECTORIZE
const __m256i init_m = _mm256_set1_epi32(-1);
const __m256i true_m = _mm256_set1_epi32(0xffffffff);
#endif
#pragma omp parallel
{
int tid = omp_get_thread_num();
int start_row = bin.rows_offset[tid];
int end_row = bin.rows_offset[tid + 1];
int *ht_check = bin.local_hash_table_id[tid];
NT *ht_value = bin.local_hash_table_val[tid];
for (int i = start_row; i < end_row; ++i) {
#ifdef VECTORIZE
__m256i key_m, check_m, mask_m;
int mask;
#endif
int bid = bin.bin_id[i];
if (bid > 0) {
int offset = crpt[i];
int table_size = MIN_HT_N << (bid - 1);
int ht_size = table_size >> VEC_LENGTH_BIT;
for (int j = 0; j < table_size; ++j) {
ht_check[j] = -1;
}
for (int j = arpt[i]; j < arpt[i + 1]; ++j) {
int t_acol = acol[j];
NT t_aval = aval[j];
for (int k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
NT t_val = multop(t_aval, bval[k]);
int key = bcol[k];
int hash = (key * HASH_SCAL) & (ht_size - 1);
#ifdef VECTORIZE
key_m = _mm256_set1_epi32(key);
#endif
while (1) {
#ifdef VECTORIZE
check_m = _mm256_maskload_epi32(ht_check + (hash << VEC_LENGTH_BIT), true_m);
mask_m = _mm256_cmpeq_epi32(key_m, check_m);
mask = _mm256_movemask_epi8(mask_m);
if (mask != 0) {
int target = __builtin_ctz(mask) >> 2;
ht_value[(hash << VEC_LENGTH_BIT) + target] += t_val;
break;
}
#else
int flag = -1;
for (int l = 0; l < VEC_LENGTH; ++l) {
if (ht_check[(hash << VEC_LENGTH_BIT) + l] == key) {
flag = l;
}
}
if (flag >= 0) {
ht_value[(hash << VEC_LENGTH_BIT) + flag] += t_val;
break;
}
#endif
else {
int cur_nz;
#ifdef VECTORIZE
mask_m = _mm256_cmpeq_epi32(check_m, init_m);
mask = _mm256_movemask_epi8(mask_m);
cur_nz = (32 - _popcnt32(mask)) >> 2;
#else
cur_nz = VEC_LENGTH;
for (int l = 0; l < VEC_LENGTH; ++l) {
if (ht_check[(hash << VEC_LENGTH_BIT) + l] == -1) {
cur_nz = l;
break;
}
}
#endif
if (cur_nz < VEC_LENGTH) {
ht_check[(hash << VEC_LENGTH_BIT) + cur_nz] = key;
ht_value[(hash << VEC_LENGTH_BIT) + cur_nz] = t_val;
break;
}
else {
hash = (hash + 1) & (ht_size - 1);
}
}
}
}
}
sort_and_store_table2mat<sortOutput, int, NT>(ht_check, ht_value,
ccol + offset, cval + offset,
crpt[i + 1] - offset, ht_size, crpt[i+1]);
}
}
}
}
template <bool sortOutput, typename NT, typename MultiplyOperation, typename AddOperation>
inline void hash_numeric_vec(const long long int *arpt, const long long int *acol, const NT *aval, const long long int *brpt, const long long int *bcol, const NT *bval, const long long int *crpt, long long int *ccol, NT *cval, const BIN<long long int, NT> &bin, MultiplyOperation multop, AddOperation addop)
{
#ifdef VECTORIZE
const __m256i init_m = _mm256_set1_epi64x(-1);
const __m256i true_m = _mm256_set1_epi64x(0xffffffffffffffff);
#endif
#pragma omp parallel
{
long long int tid = omp_get_thread_num();
long long int start_row = bin.rows_offset[tid];
long long int end_row = bin.rows_offset[tid + 1];
long long int *ht_check = bin.local_hash_table_id[tid];
NT *ht_value = bin.local_hash_table_val[tid];
for (long long int i = start_row; i < end_row; ++i) {
#ifdef VECTORIZE
__m256i key_m, check_m, mask_m;
int mask;
#endif
long long int bid = bin.bin_id[i];
if (bid > 0) {
long long int offset = crpt[i];
long long int table_size = MIN_HT_N << (bid - 1);
long long int ht_size = table_size >> VEC_LENGTH_LONG_BIT;
for (long long int j = 0; j < table_size; ++j) {
ht_check[j] = -1;
}
for (long long int j = arpt[i]; j < arpt[i + 1]; ++j) {
long long int t_acol = acol[j];
NT t_aval = aval[j];
for (long long int k = brpt[t_acol]; k < brpt[t_acol + 1]; ++k) {
NT t_val = multop(t_aval, bval[k]);
long long int key = bcol[k];
long long int hash = (key * HASH_SCAL) & (ht_size - 1);
#ifdef VECTORIZE
key_m = _mm256_set1_epi64x(key);
#endif
while (1) {
#ifdef VECTORIZE
check_m = _mm256_maskload_epi64(ht_check + (hash << VEC_LENGTH_LONG_BIT), true_m);
mask_m = _mm256_cmpeq_epi64(key_m, check_m);
mask = _mm256_movemask_epi8(mask_m);
if (mask != 0) {
int target = __builtin_ctz(mask) >> 3;
ht_value[(hash << VEC_LENGTH_LONG_BIT) + target] += t_val;
break;
}
#else
int flag = -1;
for (int l = 0; l < VEC_LENGTH_LONG; ++l) {
if (ht_check[(hash << VEC_LENGTH_LONG_BIT) + l] == key) {
flag = l;
}
}
if (flag >= 0) {
ht_value[(hash << VEC_LENGTH_LONG_BIT) + flag] += t_val;
break;
}
#endif
else {
int cur_nz;
#ifdef VECTORIZE
mask_m = _mm256_cmpeq_epi64(check_m, init_m);
mask = _mm256_movemask_epi8(mask_m);
cur_nz = (32 - _popcnt32(mask)) >> 3;
#else
cur_nz = VEC_LENGTH_LONG;
for (int l = 0; l < VEC_LENGTH_LONG; ++l) {
if (ht_check[(hash << VEC_LENGTH_LONG_BIT) + l] == -1) {
cur_nz = l;
break;
}
}
#endif
if (cur_nz < VEC_LENGTH_LONG) {
ht_check[(hash << VEC_LENGTH_LONG_BIT) + cur_nz] = key;
ht_value[(hash << VEC_LENGTH_LONG_BIT) + cur_nz] = t_val;
break;
}
else {
hash = (hash + 1) & (ht_size - 1);
}
}
}
}
}
sort_and_store_table2mat<sortOutput, long long int, NT>(ht_check, ht_value,
ccol + offset, cval + offset,
crpt[i + 1] - offset, ht_size, crpt[i+1]);
}
}
}
}
/*
* Numeric phase (with inplace update for SMALL NNZs) in Hash SpGEMM.
*/
template <bool sortOutput, typename IT, typename NT, typename MultiplyOperation, typename AddOperation>
inline void hash_numeric_with_inplace(const IT *arpt, const IT *acol, const NT *aval, const IT *brpt, const IT *bcol, const NT *bval, const IT *crpt, IT *ccol, NT *cval, const BIN<IT, NT> &bin, const MultiplyOperation multop, const AddOperation addop, IT cnnz, int inplace_cutoff)
{
#pragma omp parallel
{
IT tid = omp_get_thread_num();
IT start_row = bin.rows_offset[tid];
IT end_row = bin.rows_offset[tid + 1];
IT row_write_ptr;
IT *ht_check = bin.local_hash_table_id[tid];
NT *ht_value = bin.local_hash_table_val[tid];
for (IT i = start_row; i < end_row; ++i) { //A.rows
// in-place update
if(bin.row_nz[i] > 0 && bin.row_nz[i] <= inplace_cutoff) {
row_write_ptr = crpt[i];
for (size_t j = arpt[i]; j < arpt[i + 1]; ++j) {
size_t rowofb = acol[j];
for (size_t k = brpt[rowofb]; k < brpt[rowofb + 1]; ++k) {
if(sortOutput) {
size_t l = crpt[i];
long pos = (lower_bound(ccol+l, ccol + row_write_ptr, bcol[k])) - ccol;
if(pos == row_write_ptr) {
ccol[row_write_ptr] = bcol[k];
cval[row_write_ptr] = multop(aval[j], bval[k]);
row_write_ptr += 1;