-
Notifications
You must be signed in to change notification settings - Fork 0
/
sptrans.h
966 lines (873 loc) · 34.9 KB
/
sptrans.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
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
/*
* (c) 2017 Virginia Polytechnic Institute & State University (Virginia Tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version 2.1.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* LICENSE in the root of the repository for details.
*
*/
#ifndef _SPTRANS_H
#define _SPTRANS_H
#include <omp.h>
#include <algorithm>
#include <numeric>
#include <immintrin.h>
#define NBLOCKS(_n,_bsize) (1 + ((_n)-1)/(_bsize))
#define _SCAN_BSIZE 2048
void scan_kernel_vec_horn(int *in, int *out, int N, bool inclusive, int base) {
#ifdef __AVX2__
// shrink n to its nearest power of 2
int log_n = N & 0xfffffff8;
__m256i param0 = _mm256_set1_epi32(0);
__m256i param1 = _mm256_set1_epi32(base);
__m256i param2 = _mm256_set_epi32(6,5,4,3,2,1,0,7);
__m256i param3 = _mm256_set_epi32(5,4,3,2,1,0,7,6);
__m256i param4 = _mm256_set_epi32(3,2,1,0,7,6,5,4);
__m256i param5 = _mm256_set_epi32(7,7,7,7,7,7,7,7);
if (inclusive) {
for (int i = 0; i < log_n; i += 8) {
__m256i va = _mm256_loadu_si256((__m256i *)(&in[i]));
__m256i vb = _mm256_permutevar8x32_epi32(va, param2);
__m256i vc = _mm256_insert_epi32(vb, 0, 0);
__m256i vd = _mm256_add_epi32(va, vc);
__m256i ve = _mm256_permutevar8x32_epi32(vd, param3);
__m256i vf = _mm256_blend_epi32(ve, param0, 0x03);
__m256i vg = _mm256_add_epi32(vd, vf);
__m256i vh = _mm256_permutevar8x32_epi32(vg, param4);
__m256i vi = _mm256_blend_epi32(vh, param0, 0x0f);
__m256i vj = _mm256_add_epi32(vg, vi);
__m256i vk = _mm256_add_epi32(vj, param1);
_mm256_storeu_si256((__m256i *)(&out[i]), vk);
param1 = _mm256_permutevar8x32_epi32(vk, param5);
}
if (log_n < N) {
out[log_n] = in[log_n] + out[log_n-1];
for (int i = log_n+1; i < N; i++) {
out[i] = out[i-1] + in[i];
}
}
} else {
int sum = in[log_n-1];
for (int i = 0; i < log_n; i += 8) {
__m256i va = _mm256_loadu_si256((__m256i *)(&in[i]));
__m256i vb = _mm256_permutevar8x32_epi32(va, param2);
__m256i vc = _mm256_insert_epi32(vb, 0, 0);
__m256i vd = _mm256_add_epi32(va, vc);
__m256i ve = _mm256_permutevar8x32_epi32(vd, param3);
__m256i vf = _mm256_blend_epi32(ve, param0, 0x03);
__m256i vg = _mm256_add_epi32(vd, vf);
__m256i vh = _mm256_permutevar8x32_epi32(vg, param4);
__m256i vi = _mm256_blend_epi32(vh, param0, 0x0f);
__m256i vj = _mm256_add_epi32(vg, vi);
__m256i vk = _mm256_add_epi32(vj, param1);
__m256i vl = _mm256_permutevar8x32_epi32(vk, param2);
__m256i vm = _mm256_blend_epi32(vl, param1, 0x01);
_mm256_storeu_si256((__m256i *)(&out[i]), vm);
param1 = _mm256_permutevar8x32_epi32(vk, param5);
}
sum += out[log_n-1];
for (int i = log_n; i < N; i++) {
int tmp = in[i];
out[i] = sum;
sum += tmp;
}
}
#elif defined __MIC__
// shrink n to its nearest power of 2
int log_n = N & 0xfffffff0;
__m512i param0 = _mm512_set1_epi32(0);
__m512i param1 = _mm512_set1_epi32(base);
__m512i param2 = _mm512_set_epi32(14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,15);
__m512i param3 = _mm512_set_epi32(13,12,11,10,9,8,7,6,5,4,3,2,1,0,15,14);
__m512i param4 = _mm512_set_epi32(15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15);
if (inclusive) {
for (int i = 0; i < log_n; i += 16) {
__m512i va = _mm512_loadunpacklo_epi32(va, in+i);
va = _mm512_loadunpackhi_epi32(va, in+i+16);
__m512i vb = _mm512_mask_permutevar_epi32(param0, 0xfffe, param2, va);
__m512i vc = _mm512_add_epi32(va,vb);
__m512i vd = _mm512_mask_permutevar_epi32(param0, 0xfffc, param3, vc);
__m512i ve = _mm512_add_epi32(vc,vd);
__m512i vf = _mm512_mask_permute4f128_epi32(param0, 0xfff0, ve, _MM_PERM_CBAD);
__m512i vg = _mm512_add_epi32(ve,vf);
__m512i vh = _mm512_mask_permute4f128_epi32(param0, 0xff00, vg, _MM_PERM_BADC);
__m512i vi = _mm512_add_epi32(vg,vh);
__m512i vj = _mm512_add_epi32(vi, param1);
_mm512_packstorelo_epi32(out+i, vj);
_mm512_packstorehi_epi32(out+i+16, vj);
param1 = _mm512_permutevar_epi32(param4, vj);
}
if (log_n < N) {
out[log_n] = in[log_n] + out[log_n-1];
for (int i = log_n+1; i < N; i++) {
out[i] = out[i-1] + in[i];
}
}
} else {
int sum = in[log_n-1];
for (int i = 0; i < log_n; i += 16) {
__m512i va = _mm512_loadunpacklo_epi32(va, in+i);
va = _mm512_loadunpackhi_epi32(va, in+i+16);
__m512i vb = _mm512_mask_permutevar_epi32(param0, 0xfffe, param2, va);
__m512i vc = _mm512_add_epi32(va,vb);
__m512i vd = _mm512_mask_permutevar_epi32(param0, 0xfffc, param3, vc);
__m512i ve = _mm512_add_epi32(vc,vd);
__m512i vf = _mm512_mask_permute4f128_epi32(param0, 0xfff0, ve, _MM_PERM_CBAD);
__m512i vg = _mm512_add_epi32(ve,vf);
__m512i vh = _mm512_mask_permute4f128_epi32(param0, 0xff00, vg, _MM_PERM_BADC);
__m512i vi = _mm512_add_epi32(vg,vh);
__m512i vj = _mm512_add_epi32(vi, param1);
__m512i vk = _mm512_mask_permutevar_epi32(param1, 0xfffe, param2, vj);
_mm512_packstorelo_epi32(out+i, vk);
_mm512_packstorehi_epi32(out+i+16, vk);
param1 = _mm512_permutevar_epi32(param4, vj);
}
sum += out[log_n-1];
for (int i = log_n; i < N; i++) {
int tmp = in[i];
out[i] = sum;
sum += tmp;
}
}
#endif
}
void scan_kernel(int *in, int *out, int N, bool inclusive, int base) {
#if defined(__AVX2__) || defined(__MIC__)
scan_kernel_vec_horn(in, out, N, inclusive, base);
#else
if (inclusive) {
out[0] = in[0] + base;
for(int i = 1; i < N; i++) {
out[i] = out[i-1] + in[i];
}
} else {
int sum = base;
for(int i = 0; i < N; i++) {
int tmp = in[i];
out[i] = sum;
sum += tmp;
}
}
return;
#endif
}
void scan_omp(int *in, int *out, int N, bool inclusive, int base) {
int p = 1;
#pragma omp parallel
p = omp_get_num_threads();
int nblocks = NBLOCKS(N, _SCAN_BSIZE);
if (nblocks <= 2) {
scan_kernel(in, out, N, inclusive, base);
return;
}
int *sums = new int[nblocks];
#pragma omp parallel for
for (int i = 0; i < nblocks; i++) {
int st = i * _SCAN_BSIZE;
int ed = std::min(st + _SCAN_BSIZE, N);
int sum = std::accumulate(in+st, in+ed, 0);
sums[i] = sum;
}
scan_omp(sums, sums, nblocks, false, base);
#pragma omp parallel for
for(int i = 0; i < nblocks; i++) {
int st = i * _SCAN_BSIZE;
int ed = std::min(st + _SCAN_BSIZE, N);
scan_kernel(in+st, out+st, ed-st, inclusive, sums[i]);
}
delete []sums;
}
template<typename iT, typename vT>
void sptrans_scanTrans(int m,
int n,
int nnz,
iT *csrRowPtr,
iT *csrColIdx,
vT *csrVal,
iT *cscRowIdx,
iT *cscColPtr,
vT *cscVal)
{
int i, j, k, ii, jj, procs;
#pragma omp parallel
procs = omp_get_num_threads();
int size = nnz / procs;
int left = nnz % procs;
int *csrRowUnroll = (int *)malloc(nnz * sizeof(int));
int *intra = (int *)malloc(nnz * sizeof(int));
int *wb_index = (int *)malloc(nnz * sizeof(int));
int *inter = (int *)malloc((procs + 1) * n * sizeof(int));
memset(inter, 0, (procs + 1) * n * sizeof(int));
#pragma omp parallel default(shared) private(i)
{
int index = 0;
int tid = omp_get_thread_num();
int inter_start = n + tid * n;
int intra_start = size * tid;
int len = size;
if (left != 0) {
if (tid < left) {
len += 1;
intra_start = tid * size + tid;
} else {
intra_start = tid * size + left;
}
}
for (i = 0; i < len; i++) {
index = inter_start + csrColIdx[intra_start + i];
intra[intra_start + i] = inter[index];
inter[index]++;
}
}
#pragma omp parallel for default(shared) private(i, j) //schedule(dynamic)
for (i = 0; i < n; i++) {
for (j = 2; j < procs + 1; j++) {
inter[i + n * j] += inter[i + n * (j - 1)];
}
}
#pragma omp parallel for default(shared) private(i) //schedule(dynamic)
#pragma ivdep
for (i = 0; i < n; i++) {
cscColPtr[i + 1] = inter[n * procs + i];
}
scan_omp(cscColPtr,cscColPtr,n+1,true,0);
#pragma omp parallel for default(shared) private(i, j, ii, jj) //schedule(dynamic)
for (i = 0 ; i < m; i++) {
ii = csrRowPtr[i + 1] - csrRowPtr[i];
jj = csrRowPtr[i];
for (j = 0; j < ii; j++) {
csrRowUnroll[jj + j] = i;
}
}
#pragma omp parallel default(shared) private(i, j)
{
int tid = omp_get_thread_num();
int inter_start = tid * n;
int intra_start = size * tid;
int len = size;
int colIdx, offset;
if (left != 0) {
if (tid < left) {
len += 1;
intra_start = tid * size + tid;
} else {
intra_start = tid * size + left;
}
}
for (i = 0; i < len; i++) {
colIdx = csrColIdx[intra_start + i];
offset = cscColPtr[colIdx] + inter[inter_start + colIdx] + intra[intra_start + i];
wb_index[intra_start + i] = offset;
}
}
if (csrVal != NULL) {
#pragma omp parallel default(shared) private(i, j)
{
int offset = 0;
int tid = omp_get_thread_num();
int inter_start = tid * n;
int intra_start = size * tid;
int len = size;
if (left != 0) {
if (tid < left) {
len += 1;
intra_start = tid * size + tid;
} else {
intra_start = tid * size + left;
}
}
for (i = 0; i < len; i++) {
offset = wb_index[intra_start + i];
cscVal[offset] = csrVal[intra_start + i];
cscRowIdx[offset] = csrRowUnroll[intra_start + i];
}
}
} else {
#pragma omp parallel default(shared) private(i, j)
{
int offset = 0;
int tid = omp_get_thread_num();
int inter_start = tid * n;
int intra_start = size * tid;
int len = size;
if (left != 0) {
if (tid < left) {
len += 1;
intra_start = tid * size + tid;
} else {
intra_start = tid * size + left;
}
}
for(i = 0; i < len; i++) {
offset = wb_index[intra_start + i];
cscRowIdx[offset] = csrRowUnroll[intra_start + i];
}
}
}
free(csrRowUnroll);
free(inter);
free(intra);
free(wb_index);
}
// merge two csr by one thread
template<typename iT, typename vT>
void segment_merge_csr(int len,
iT *csrRowPtrA,
iT *csrColIdxA,
vT *csrValA,
iT *csrRowPtrB,
iT *csrColIdxB,
vT *csrValB,
iT *csrRowPtrC,
iT *csrColIdxC,
vT *csrValC)
{
int i, j, k;
for (i = 0; i < len; i++) {
csrRowPtrC[i] = csrRowPtrA[i] + csrRowPtrB[i];
}
for (i = 0; i < len - 1; i++) {
int starta = csrRowPtrA[i];
int startb = csrRowPtrB[i];
int startc = csrRowPtrC[i];
int lena = csrRowPtrA[i + 1] - csrRowPtrA[i];
int lenb = csrRowPtrB[i + 1] - csrRowPtrB[i];
int lenc = csrRowPtrC[i + 1] - csrRowPtrC[i];
for (j = 0; j < lena; j++) {
csrColIdxC[startc + j] = csrColIdxA[starta + j];
csrValC[startc + j] = csrValA[starta + j];
}
for (k = 0; k < lenb; k++) {
csrColIdxC[startc + lena + k] = csrColIdxB[startb + k];
csrValC[startc + lena + k] = csrValB[startb + k];
}
}
}
// merge multiple csr by one thread
template<typename iT, typename vT>
void segment_merge_multiple_csr(int ncsr,
int len,
iT *csrRowPtrA,
iT *csrColIdxA,
vT *csrValA,
iT *csrRowPtrB,
iT *csrColIdxB,
vT *csrValB)
{
int i, j, k;
for (i = 0; i < len; i++) {
for (j = 0; j < ncsr; j++) {
csrRowPtrB[i] += csrRowPtrA[i + len * j];
}
}
for (i = 0; i < len - 1; i++) {
int startb = csrRowPtrB[i];
int starta = 0;
int lena = 0;
int offseta = 0;
int offsetb = 0;
for (j = 0; j < ncsr; j++) {
starta = csrRowPtrA[i + j * len];
lena = csrRowPtrA[i + 1 + j * len] - csrRowPtrA[i + j * len];
offseta += csrRowPtrA[j * (len - 1)];
for (k = 0; k < lena; k++) {
csrColIdxB[startb + offsetb + k] = csrColIdxA[offseta + starta + k];
csrValB[startb + offsetb + k] = csrValA[offseta + starta + k];
}
offsetb += lena;
}
}
}
// merge two csr by multiple threads
template<typename iT, typename vT>
void segment_merge_csr_multithreads(int begin,
int end,
iT *csrRowPtrA,
iT *csrColIdxA,
vT *csrValA,
iT *csrRowPtrB,
iT *csrColIdxB,
vT *csrValB,
iT *csrRowPtrC,
iT *csrColIdxC,
vT *csrValC)
{
int i, j, k, tid, procs;
tid = omp_get_thread_num();
procs = omp_get_num_threads();
for (i = begin; i <= end; i++) {
csrRowPtrC[i] = csrRowPtrA[i] + csrRowPtrB[i];
}
for (i = begin; i < end; i++) {
int starta = csrRowPtrA[i];
int startb = csrRowPtrB[i];
int startc = csrRowPtrC[i];
int lena = csrRowPtrA[i + 1] - csrRowPtrA[i];
int lenb = csrRowPtrB[i + 1] - csrRowPtrB[i];
int lenc = csrRowPtrC[i + 1] - csrRowPtrC[i];
for (j = 0; j < lena; j++) {
csrColIdxC[startc + j] = csrColIdxA[starta + j];
csrValC[startc + j] = csrValA[starta + j];
}
for (k = 0; k < lenb; k++) {
csrColIdxC[startc + lena + k] = csrColIdxB[startb + k];
csrValC[startc + lena + k] = csrValB[startb + k];
}
}
if (tid != procs - 1) {
int starta = csrRowPtrA[end];
int startb = csrRowPtrB[end];
int startc = csrRowPtrA[end] + csrRowPtrB[end];
int lena = csrRowPtrA[end + 1] - csrRowPtrA[end];
int lenb = csrRowPtrB[end + 1] - csrRowPtrB[end];
int lenc = lena + lenb;
for (j = 0; j < lena; j++) {
csrColIdxC[startc + j] = csrColIdxA[starta + j];
csrValC[startc + j] = csrValA[starta + j];
}
for (k = 0; k < lenb; k++) {
csrColIdxC[startc + lena + k] = csrColIdxB[startb + k];
csrValC[startc + lena + k] = csrValB[startb + k];
}
}
}
template<typename iT, typename vT>
void sptrans_mergeTrans(int m,
int n,
int nnz,
iT *csrRowPtr,
iT *csrColIdx,
vT *csrVal,
iT *cscRowIdx,
iT *cscColPtr,
vT *cscVal)
{
int i, j, k, ii, jj, kk, procs;
#pragma omp parallel
procs = omp_get_num_threads();
// bsize can be configured to LLC size per core
int bsize = nnz / procs;
int blocksByL2 = procs;
int dataLeftL2 = nnz % procs;
int nthreadPerBlock = 1;
#if defined(__MIC__) || defined(__AVX2__)
char *env_nthreadPerBlock;
if ((env_nthreadPerBlock = getenv("NTHREADPERBLOCK")) != NULL) {
nthreadPerBlock = atoi(env_nthreadPerBlock);
} else {
nthreadPerBlock = 1;
}
bsize = nnz / (procs / nthreadPerBlock);
blocksByL2 = procs / nthreadPerBlock;
dataLeftL2 = nnz % (procs / nthreadPerBlock);
#endif
long allocBlocks = blocksByL2 != 0 ? blocksByL2 : (dataLeftL2 != 0 ? 1 : 0);
bsize = blocksByL2 != 0 ? bsize : (dataLeftL2 != 0 ? dataLeftL2 : 0);
long size = 2 * allocBlocks * (n + 1) * sizeof(iT);
iT *csrRowUnroll = (iT *)malloc(nnz * sizeof(iT));
iT *cscColPtrAux = (iT *)malloc(size);
iT *cscRowIdxAux = (iT *)malloc(2 * nnz * sizeof(iT));
vT *cscValAux = (vT *)malloc(2 * nnz * sizeof(vT));
#ifndef __MIC__
memset(cscColPtrAux, 0, size);
#endif
iT *ptra, *ptrb, *idxa, *idxb;
vT *vala, *valb;
ptra = cscColPtrAux;
ptrb = cscColPtrAux + allocBlocks * (n + 1);
idxa = cscRowIdxAux;
idxb = cscRowIdxAux + nnz;
vala = cscValAux;
valb = cscValAux + nnz;
#pragma omp parallel for default(shared) private(i, j, ii, jj)
for (i = 0 ; i < m; i++) {
ii = csrRowPtr[i + 1] - csrRowPtr[i];
jj = csrRowPtr[i];
for (j = 0; j < ii; j++)
{
csrRowUnroll[jj + j] = i;
}
}
// Stage 1: csc to csr for each tile
int loop = blocksByL2 / procs;
int left = blocksByL2 % procs;
if (csrVal != NULL) {
#pragma omp parallel default(shared) private(i, j, k)
{
int tid = omp_get_thread_num();
for (i = 0; i < loop; i++) {
long inter_start = i * procs * (n + 1) + tid * (n + 1);
long intra_start = i * procs * bsize + tid * bsize;
#ifdef __MIC__
for (j = 0; j < n + 1; j++) {
ptrb[inter_start + j] = 0;
ptra[inter_start + j] = 0;
}
#endif
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
ptrb[inter_start + col + 1]++;
ptra[inter_start + col + 1]++;
}
for (k = 0; k < n; k++) {
int tmp = ptrb[inter_start + k + 1];
ptra[inter_start + k + 1] = tmp + ptra[inter_start + k];
}
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
long pos = intra_start + ptra[inter_start + col + 1] - ptrb[inter_start + col + 1]--;
idxa[pos] = csrRowUnroll[intra_start + j];
vala[pos] = csrVal[intra_start + j];
}
}
if (left) {
#if defined(__MIC__) || defined(__AVX2__)
if (tid % nthreadPerBlock == 0)
#else
if (tid < left)
#endif
{
long inter_start = loop * procs * (n + 1) + (tid / nthreadPerBlock) * (n + 1);
long intra_start = loop * procs * bsize + (tid / nthreadPerBlock) * bsize;
#ifdef __MIC__
for (j = 0; j < n + 1; j++) {
ptrb[inter_start + j] = 0;
ptra[inter_start + j] = 0;
}
#endif
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
ptrb[inter_start + col + 1]++;
}
for (k = 0; k < n; k++) {
int tmp = ptrb[inter_start + k + 1];
ptra[inter_start + k + 1] = tmp + ptra[inter_start + k];
}
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
long pos = intra_start + ptra[inter_start + col + 1] - ptrb[inter_start + col + 1]--;
idxa[pos] = csrRowUnroll[intra_start + j];
vala[pos] = csrVal[intra_start + j];
}
}
}
}
} else {
#pragma omp parallel default(shared) private(i, j, k)
{
int tid = omp_get_thread_num();
for (i = 0; i < loop; i++) {
long inter_start = i * procs * (n + 1) + tid * (n + 1);
long intra_start = i * procs * bsize + tid * bsize;
#ifdef __MIC__
for (j = 0; j < n + 1; j++) {
ptrb[inter_start + j] = 0;
ptra[inter_start + j] = 0;
}
#endif
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
ptrb[inter_start + col + 1]++;
ptra[inter_start + col + 1]++;
}
for (k = 0; k < n; k++) {
int tmp = ptrb[inter_start + k + 1];
ptra[inter_start + k + 1] = tmp + ptra[inter_start + k];
}
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
long pos = intra_start + ptra[inter_start + col + 1] - ptrb[inter_start + col + 1]--;
idxa[pos] = csrRowUnroll[intra_start + j];
}
}
if (left) {
#if defined(__MIC__) || defined(__AVX2__)
if (tid % nthreadPerBlock == 0)
#else
if (tid < left)
#endif
{
long inter_start = loop * procs * (n + 1) + (tid / nthreadPerBlock) * (n + 1);
long intra_start = loop * procs * bsize + (tid / nthreadPerBlock) * bsize;
#ifdef __MIC__
for (j = 0; j < n + 1; j++) {
ptrb[inter_start + j] = 0;
ptra[inter_start + j] = 0;
}
#endif
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
ptrb[inter_start + col + 1]++;
}
for (k = 0; k < n; k++) {
int tmp = ptrb[inter_start + k + 1];
ptra[inter_start + k + 1] = tmp + ptra[inter_start + k];
}
for (j = 0; j < bsize; j++) {
int col = csrColIdx[intra_start + j];
long pos = intra_start + ptra[inter_start + col + 1] - ptrb[inter_start + col + 1]--;
idxa[pos] = csrRowUnroll[intra_start + j];
}
}
}
}
}
if (dataLeftL2) {
long inter_start = 0, intra_start = 0, len = dataLeftL2;
if (blocksByL2) {
inter_start = (blocksByL2 - 1) * (n + 1);
intra_start = (blocksByL2 - 1) * bsize;
len += bsize;
}
for (k = 0; k < n; k++) {
ptra[inter_start + k + 1] = 0;
}
for (j = 0; j < len; j++) {
int col = csrColIdx[intra_start + j];
ptrb[inter_start + col + 1]++;
}
for (k = 0; k < n; k++) {
long tmp = ptrb[inter_start + k + 1];
ptra[inter_start + k + 1] = tmp + ptra[inter_start + k];
}
if (csrVal != NULL) {
for (j = 0; j < len; j++) {
int col = csrColIdx[intra_start + j];
long pos = intra_start + ptra[inter_start + col + 1] - ptrb[inter_start + col + 1]--;
idxa[pos] = csrRowUnroll[intra_start + j];
vala[pos] = csrVal[intra_start + j];
}
} else {
for (j = 0; j < len; j++) {
int col = csrColIdx[intra_start + j];
long pos = intra_start + ptra[inter_start + col + 1] - ptrb[inter_start + col + 1]--;
idxa[pos] = csrRowUnroll[intra_start + j];
}
}
}
// Stage 2: in-memory merge csc until number of blocks is less than twice of thread number
int blocks = allocBlocks;
while (blocks >= 2 * procs) {
loop = blocks / (2 * procs);
left = blocks % (2 * procs);
#pragma omp parallel default(shared) private(i, j, k)
{
int tid = omp_get_thread_num();
iT *csrRowPtrA, *csrRowPtrB, *csrRowPtrC;
iT *csrColIdxA, *csrColIdxB, *csrColIdxC;
vT *csrValA, *csrValB, *csrValC;
iT *lptra = ptra;
iT *lptrb = ptrb;
iT *lidxa = idxa;
iT *lidxb = idxb;
vT *lvala = vala;
vT *lvalb = valb;
for (i = 0; i < loop; i++) {
int srcBlockStart = i * (2 * procs) + 2 * tid;
int dstBlockStart = i * procs + tid;
csrRowPtrA = lptra + srcBlockStart * (n + 1);
csrColIdxA = lidxa + srcBlockStart * bsize;
csrValA = lvala + srcBlockStart * bsize;
csrRowPtrB = lptra + (srcBlockStart + 1) * (n + 1);
csrColIdxB = lidxa + (srcBlockStart + 1) * bsize;
csrValB = lvala + (srcBlockStart + 1) * bsize;
csrRowPtrC = lptrb + dstBlockStart * (n + 1);
csrColIdxC = lidxb + srcBlockStart * bsize;
csrValC = lvalb + srcBlockStart * bsize;
segment_merge_csr(n + 1, csrRowPtrA, csrColIdxA, csrValA,
csrRowPtrB, csrColIdxB, csrValB,
csrRowPtrC, csrColIdxC, csrValC);
}
#pragma omp single
{
blocks = loop * procs;
}
if (left) {
if (tid < (left / 2)) {
int srcBlockStart = loop * (2 * procs) + 2 * tid;
int dstBlockStart = loop * procs + tid;
csrRowPtrA = lptra + srcBlockStart * (n + 1);
csrColIdxA = lidxa + srcBlockStart * bsize;
csrValA = lvala + srcBlockStart * bsize;
csrRowPtrB = lptra + (srcBlockStart + 1) * (n + 1);
csrColIdxB = lidxa + (srcBlockStart + 1) * bsize;
csrValB = lvala + (srcBlockStart + 1) * bsize;
csrRowPtrC = lptrb + dstBlockStart * (n + 1);
csrColIdxC = lidxb + srcBlockStart * bsize;
csrValC = lvalb + srcBlockStart * bsize;
segment_merge_csr(n + 1, csrRowPtrA, csrColIdxA, csrValA,
csrRowPtrB, csrColIdxB, csrValB,
csrRowPtrC, csrColIdxC, csrValC);
}
#pragma omp single
{
blocks += left / 2;
if (left % 2) {
int srcBlockStart = loop * (2 * procs) + left - 1;
int dstBlockStart = blocks;
memcpy(lptrb + dstBlockStart * (n + 1), lptra + srcBlockStart * (n + 1), (n + 1) * sizeof(iT));
memcpy(lidxb + srcBlockStart * bsize, lidxa + srcBlockStart * bsize, lptra[srcBlockStart * (n + 1) + n] * sizeof(iT));
memcpy(lvalb + srcBlockStart * bsize, lvala + srcBlockStart * bsize, lptra[srcBlockStart * (n + 1) + n] * sizeof(vT));
blocks += 1;
}
}
}
}
iT *ptrt, *idxt;
vT *valt;
ptrt = ptra;
ptra = ptrb;
ptrb = ptrt;
idxt = idxa;
idxa = idxb;
idxb = idxt;
valt = vala;
vala = valb;
valb = valt;
bsize *= 2;
}
// Stage 3: use multiple threads to merge each pair until one csc
while (blocks != 1) {
int pairs = blocks / 2;
int lefts = blocks % 2;
int nthreadPerPair = procs / pairs;
#pragma omp parallel default(shared) private(i, j, k)
{
int tid = omp_get_thread_num();
iT *csrRowPtrA, *csrRowPtrB, *csrRowPtrC;
iT *csrColIdxA, *csrColIdxB, *csrColIdxC;
vT *csrValA, *csrValB, *csrValC;
iT *lptra = ptra;
iT *lptrb = ptrb;
iT *lidxa = idxa;
iT *lidxb = idxb;
vT *lvala = vala;
vT *lvalb = valb;
int srcBlockStart, dstBlockStart;
int rowPerThread, rowLeftPerId, mapTRPerPair, rowBeginThread, rowEndThread;
if (tid < pairs * nthreadPerPair) {
if (nthreadPerPair == 1) {
srcBlockStart = tid * 2;
dstBlockStart = tid;
csrRowPtrA = lptra + srcBlockStart * (n + 1);
csrColIdxA = lidxa + srcBlockStart * bsize;
csrValA = lvala + srcBlockStart * bsize;
csrRowPtrB = lptra + (srcBlockStart + 1) * (n + 1);
csrColIdxB = lidxa + (srcBlockStart + 1) * bsize;
csrValB = lvala + (srcBlockStart + 1) * bsize;
csrRowPtrC = lptrb + dstBlockStart * (n + 1);
csrColIdxC = lidxb + srcBlockStart * bsize;
csrValC = lvalb + srcBlockStart * bsize;
segment_merge_csr(n + 1, csrRowPtrA, csrColIdxA, csrValA,
csrRowPtrB, csrColIdxB, csrValB,
csrRowPtrC, csrColIdxC, csrValC);
} else {
srcBlockStart = (tid / nthreadPerPair) * 2;
dstBlockStart = tid / nthreadPerPair;
rowPerThread = (n + 1) / nthreadPerPair;
rowLeftPerId = (n + 1) % nthreadPerPair;
mapTRPerPair = tid % nthreadPerPair;
rowBeginThread = mapTRPerPair * rowPerThread;
rowEndThread = rowBeginThread + rowPerThread - 1;
if (rowLeftPerId) {
if (mapTRPerPair < rowLeftPerId) {
rowBeginThread = rowBeginThread + mapTRPerPair;
rowEndThread += (mapTRPerPair + 1);
} else {
rowBeginThread = rowBeginThread + rowLeftPerId;
rowEndThread += rowLeftPerId;
}
}
csrRowPtrA = lptra + srcBlockStart * (n + 1);
csrColIdxA = lidxa + srcBlockStart * bsize;
csrValA = lvala + srcBlockStart * bsize;
csrRowPtrB = lptra + (srcBlockStart + 1) * (n + 1);
csrColIdxB = lidxa + (srcBlockStart + 1) * bsize;
csrValB = lvala + (srcBlockStart + 1) * bsize;
csrRowPtrC = lptrb + dstBlockStart * (n + 1);
csrColIdxC = lidxb + srcBlockStart * bsize;
csrValC = lvalb + srcBlockStart * bsize;
segment_merge_csr_multithreads(rowBeginThread, rowEndThread, csrRowPtrA, csrColIdxA, csrValA,
csrRowPtrB, csrColIdxB, csrValB,
csrRowPtrC, csrColIdxC, csrValC);
}
}
if (lefts) {
#ifdef __MIC__
#pragma omp for private(i)
for (i = 0; i < n + 1; i++) {
lptrb[pairs * (n + 1) + i] = lptra[(blocks - 1) * (n + 1) + i];
}
#pragma omp for private(i)
for (i = 0; i < lptra[(blocks - 1) * (n + 1) + n]; i++) {
lidxb[(blocks - 1) * bsize + i] = lidxa[(blocks - 1) * bsize + i];
lvalb[(blocks - 1) * bsize + i] = lvala[(blocks - 1) * bsize + i];
}
#pragma omp single
{
blocks = pairs + 1;
}
#else
#pragma omp single
{
int srcBlockStart = blocks - 1;
int dstBlockStart = pairs;
memcpy(lptrb + dstBlockStart * (n + 1), lptra + srcBlockStart * (n + 1), (n + 1) * sizeof(iT));
memcpy(lidxb + srcBlockStart * bsize, lidxa + srcBlockStart * bsize, lptra[srcBlockStart * (n + 1) + n] * sizeof(iT));
memcpy(lvalb + srcBlockStart * bsize, lvala + srcBlockStart * bsize, lptra[srcBlockStart * (n + 1) + n] * sizeof(vT));
blocks = pairs + 1;
}
#endif
} else {
#pragma omp single
{
blocks = pairs;
}
}
}
iT *ptrt, *idxt;
vT *valt;
ptrt = ptra;
ptra = ptrb;
ptrb = ptrt;
idxt = idxa;
idxa = idxb;
idxb = idxt;
valt = vala;
vala = valb;
valb = valt;
bsize *= 2;
}
#ifdef __MIC__
#pragma omp parallel for default(shared) private(i)
for (i = 0; i < n + 1; i++) {
cscColPtr[i] = ptra[i];
}
if (csrVal != NULL) {
#pragma omp parallel for default(shared) private(i)
for (i = 0; i < ptra[n]; i++) {
cscRowIdx[i] = idxa[i];
cscVal[i] = vala[i];
}
} else {
#pragma omp parallel for default(shared) private(i)
for (i = 0; i < ptra[n]; i++) {
cscRowIdx[i] = idxa[i];
}
}
#else
memcpy(cscColPtr, ptra, (n + 1) * sizeof(iT));
memcpy(cscRowIdx, idxa, (ptra[n] - ptra[0]) * sizeof(iT));
if(csrVal != NULL)
memcpy(cscVal, vala, (ptra[n] - ptra[0]) * sizeof(vT));
#endif
free(csrRowUnroll);
free(cscColPtrAux);
free(cscRowIdxAux);
free(cscValAux);
}
#endif