-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspmv.cpp
1948 lines (1508 loc) · 60.8 KB
/
spmv.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
//Compiling: icpc indirect_memory.c -o indirect_memory.openmp -openmp -O3 -mmic && scp indirect_memory.openmp mic0:/root/
//
// When N = 256000000
// Elapsed Time is 2.405552
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define MICRO_IN_SEC 1000000.00
#include <omp.h>
#include <unistd.h>
#include <sys/mman.h>
//#include <hbwmalloc.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#define WRITE
#include <immintrin.h>
using namespace std;
#ifndef TYPE_PRECISION
#define TYPE_PRECISION
//#define TYPE_SINGLE
#define TYPE_DOUBLE
#define FLOAT_TYPE double
#ifdef TYPE_SINGLE
#define SIMD_LEN 16
#else
#define SIMD_LEN 8
#endif
#endif
#define FIELD_LENGTH 128
#define floatType double
//#define DONT_USE_MM_MALLOC
#define USE_MM_MALLOC
#define ALIGN 64
#ifdef USE_MM_MALLOC
#define ALLOC(t,s) (t *)_mm_malloc((s)*sizeof(t), ALIGN)
#define FREE(p) _mm_free(p)
#else
#define ALLOC(t,s) new t[s]
#define FREE(p) delete[] p
#endif
struct Coordinate {
int x;
int y;
float val;
};
class i_Range{
public:
int i_start;
int i_end;
i_Range(int a, int b){ i_start = a; i_end = b;}
i_Range(){ i_start = 0; i_end = 0;}
};
struct v_Range{
int v_start;
int v_end;
};
typedef __attribute__((aligned(64))) union zmmd {
__m512d reg;
__m512i regi32;
double elems[8];
int elemsi32[16];
} zmmd_t;
double microtime(){
int tv_sec,tv_usec;
double time;
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
return tv.tv_sec+tv.tv_usec/MICRO_IN_SEC;
}
class Edge{
public:
int el;
int er;
FLOAT_TYPE value;
Edge(int a, int b, FLOAT_TYPE v){ el = a; er = b;value = v;}
Edge(int a, int b){ el = a; er = b;}
};
typedef __attribute__((aligned(64))) struct record{
int segs;
int write;
} record;
void show(float* c)
{
int i;
for (i=0; i<256; i++)
if(i%16==0)
cout<<" ["<<i<<"] = "<< c[i]<<endl;
}
double getAbs(double x) { return (x<0)?(-x):(x); }
inline int coordcmp(const void *v1, const void *v2)
{
struct Coordinate *c1 = (struct Coordinate *) v1;
struct Coordinate *c2 = (struct Coordinate *) v2;
if (c1->x != c2->x)
{
return (c1->x - c2->x);
}
else
{
return (c1->y - c2->y);
}
}
inline double get_simd(__m512d a, int segs_xx)
{
__mmask8 k_pd, k_n_pd;
k_pd = _mm512_int2mask( 0x01 << segs_xx);
// k_n_pd = _mm512_knot(k_pd);
return _mm512_mask_reduce_add_pd(k_pd, a);
}
inline __m512d set_simd_zero(__m512d a, int segs_xx)
{
__mmask8 k_pd, k_n_pd;
k_pd = _mm512_int2mask( 0x01 << segs_xx);
k_n_pd = _mm512_knot(k_pd);
__m512d v_zero_pd = _mm512_set1_pd(0);
return _mm512_mask_add_pd(v_zero_pd, k_n_pd, a, v_zero_pd);
}
void show_int( int *c, int a, int b)
{
int i = 0;
for ( i = a; i<(a+32); i++)
if ( i%1 ==0 )
std::cout<<"i = "<<i<<", c[i] = "<<c[i]<<endl;
}
void show( FLOAT_TYPE *c, int a, int b, int stride)
{
int i = 0;
for ( i = a; i<(a+b); i++)
if ( i%stride ==0 )
std::cout<<"i = "<<i<<", c[i] = "<<c[i]<<endl;
/*
for ( i = b-256; i< b; i++)
if ( i%16 ==0 )
std::cout<<"i = "<<i<<", c[i] = "<<c[i]<<endl;
*/
}
void show( FLOAT_TYPE *c, FLOAT_TYPE *d, int a, int b, int stride)
{
int i = 0;
for ( i = a; i<(a+b); i++)
if ( i%stride ==0 )
std::cout<<" i = "<< i <<": refOut = "<< c[i]<<"; refOut_verify = "<< d[i]<<endl;
/*
for ( i = b-256; i< b; i++)
if ( i%16 ==0 )
std::cout<<"i = "<<i<<", c[i] = "<<c[i]<<endl;
*/
}
void show_list( int* c, int k)
{
int i = 0, j=0;
for ( i = 0; i<k; i++)
{
std::cout<<" "<<c[i];
}
std::cout<<endl;
}
void show_number( int x)
{
std::cout<<"***************************** "<< x<<endl;
}
void show_int( int* c, int k)
{
int i = 0, j=0;
for ( i = 0; i<k; i+=SIMD_LEN)
{
for (j = 0; j<SIMD_LEN; j++)
std::cout<<" "<<c[i+j];
std::cout<<endl;
}
}
void show_sub( int* ll, int* lr, int k)
{
int i = 0, j=0;
for ( i = 0; i<k; i++)
{
std::cout<<ll[i]<<" "<<lr[i]<<endl;;
}
}
void show_simd_epi32( __m512i art_tmp)
{
int* tmp = (int*) _mm_malloc(sizeof(int)*16,64);
_mm512_store_si512(tmp, art_tmp);
int i = 0;
for ( i = 0; i<16; i++)
std::cout<<" ["<<i<<"] = "<<tmp[i];
std::cout<<endl;
_mm_free(tmp);
}
void show_simd_ps( __m512 art_tmp)
{
float* tmp = (float*) _mm_malloc(sizeof(float)*16,64);
_mm512_store_ps(tmp, art_tmp);
int i = 0;
for ( i = 0; i<16; i++)
std::cout<<"i = "<<i<<", tmp[i] = "<<tmp[i]<<endl;
}
void show_simd_pd( __m512d art_tmp)
{
double* tmp = (double*) _mm_malloc(sizeof(double)*8,64);
_mm512_store_pd(tmp, art_tmp);
int i = 0;
for ( i = 0; i<8; i++)
std::cout<<"i = "<<i<<", tmp[i] = "<<tmp[i]<<endl;
}
// ****************************************************************************
// Function: readMatrix
//
// Purpose:
// Reads a sparse matrix from a file of Matrix Market format
// Returns the data structures for the CSR format
//
// Arguments:
// filename: c string with the name of the file to be opened
// val_ptr: input - pointer to uninitialized pointer
// output - pointer to array holding the non-zero values
// for the matrix
// cols_ptr: input - pointer to uninitialized pointer
// output - pointer to array of column indices for each
// element of the sparse matrix
// rowDelimiters: input - pointer to uninitialized pointer
// output - pointer to array holding
// indices to rows of the matrix
// n: input - pointer to uninitialized int
// output - pointer to an int holding the number of non-zero
// elements in the matrix
// size: input - pointer to uninitialized int
// output - pointer to an int holding the number of rows in
// the matrix
//
// Programmer: Lukasz Wesolowski
// Creation: July 2, 2010
// Returns: nothing directly
// allocates and returns *val_ptr, *cols_ptr, and
// *rowDelimiters_ptr indirectly
// returns n and size indirectly through pointers
// ****************************************************************************
//template <typename floatType>
void readMatrix(char *filename, floatType **val_ptr, int **cols_ptr,
int **rowDelimiters_ptr, int *n, int *numRows, int *numCols)
{
std::string line;
char id[FIELD_LENGTH];
char object[FIELD_LENGTH];
char format[FIELD_LENGTH];
char field[FIELD_LENGTH];
char symmetry[FIELD_LENGTH];
std::ifstream mfs( filename );
if( !mfs.good() )
{
std::cerr << "Error: unable to open matrix file " << filename << std::endl;
exit( 1 );
}
int symmetric = 0;
int pattern = 0;
int field_complex = 0;
int nRows, nCols, nElements;
struct Coordinate *coords;
// read matrix header
if( getline( mfs, line ).eof() )
{
std::cerr << "Error: file " << filename << " does not store a matrix" << std::endl;
exit( 1 );
}
sscanf(line.c_str(), "%s %s %s %s %s", id, object, format, field, symmetry);
if (strcmp(object, "matrix") != 0)
{
fprintf(stderr, "Error: file %s does not store a matrix\n", filename);
exit(1);
}
if (strcmp(format, "coordinate") != 0)
{
fprintf(stderr, "Error: matrix representation is dense\n");
exit(1);
}
if (strcmp(field, "pattern") == 0)
{
pattern = 1;
}
if (strcmp(field, "complex") == 0)
{
field_complex = 1;
}
if (strcmp(symmetry, "symmetric") == 0)
{
symmetric = 1;
}
// pattern = 1;
// symmetric = 1;
while (!getline( mfs, line ).eof() )
{
if (line[0] != '%')
{
break;
}
}
// read the matrix size and number of non-zero elements
sscanf(line.c_str(), "%d %d %d", &nRows, &nCols, &nElements);
// sscanf(line.c_str(), "%d %d", &nRows, &nElements);
// nCols = nRows;
int nElements_padding = (nElements%16 == 0) ? nElements : (nElements + 16)/ 16 * 16;
// std::cout<<" numRows is "<<nRows<<"; Number of Elements is "<< nElements<<"; After Padding, Number of Elements is "<< nElements_padding<<endl;
int valSize = nElements_padding * sizeof(struct Coordinate);
if (symmetric)
{
valSize*=2;
}
// coords = new Coordinate[valSize];
coords = (struct Coordinate*)malloc(valSize);
// std::cout<<"11111111111111111111111111111"<<endl;
int index = 0;
float xx99 = 0;
while (!getline( mfs, line ).eof() )
{
if (pattern)
{
sscanf(line.c_str(), "%d %d", &coords[index].x, &coords[index].y);
// coords[index].val = 1;
coords[index].val = index%13;
// coords[index].y = 1;
// assign a random value
// coords[index].val = ((floatType) MAX_RANDOM_VAL *
// (rand() / (RAND_MAX + 1.0)));
}
else if(field_complex)
{
// read the value from file
sscanf(line.c_str(), "%d %d %f %f", &coords[index].x, &coords[index].y,
&coords[index].val, xx99);
}
else
{
// read the value from file
sscanf(line.c_str(), "%d %d %f", &coords[index].x, &coords[index].y,
&coords[index].val);
}
// convert into index-0-as-start representation
// coords[index].x--;
// coords[index].y--;
index++;
// std::cout<<"index = "<<index<<endl;
// add the mirror element if not on main diagonal
if (symmetric && coords[index-1].x != coords[index-1].y)
{
coords[index].x = coords[index-1].y;
coords[index].y = coords[index-1].x;
coords[index].val = coords[index-1].val;
index++;
}
}
// std::cout<<"22222222222222222222222222222222222"<<endl;
nElements = index;
nElements_padding = (nElements%16 == 0) ? nElements : (nElements + 16)/ 16 * 16;
std::cout<<"==========================================================================="<<endl;
std::cout<<"=========********* Informations of the sparse matrix *********=========="<<endl;
std::cout<<endl;
// std::cout<<" numRows is "<<nRows<<"; numCols is "<< nCols<<"; Number of Elements is "<< nElements<<"; After Padding, Number of Elements is "<< nElements_padding<<endl;
std::cout<<" Number of Rows is :"<< nRows<<endl;
std::cout<<" Number of Columns is :"<< nCols<<endl;
std::cout<<" Number of Elements is :"<< nElements<<endl;
std::cout<<" After Alignment :"<< nElements_padding<<endl;
std::cout<<endl;
std::cout<<"==========================================================================="<<endl;
std::cout<<"............ Converting the Raw matrix to CSR ................."<<endl;
// std::cout<<" index = "<< index <<"; nElements_padding = "<< nElements_padding<<endl;
for (int qq = index; qq <nElements_padding; qq ++)
{
coords[qq].x = coords[index - 1].x;
coords[qq].y = coords[index - 1].y;
coords[qq].val = 0;
// std::cout<<"Padding: qq = "<< qq<<"; x = "<< coords[qq].x <<"; y = "<< coords[qq].y <<"; val = "<< coords[qq].val <<endl;
}
//sort the elements
qsort(coords, nElements_padding, sizeof(struct Coordinate), coordcmp);
// std::cout<<"33333333333333333333333333333333333"<<endl;
// create CSR data structures
*n = nElements_padding;
*numRows = nRows;
*numCols = nCols;
// *val_ptr = new floatType[nElements_padding];
// *cols_ptr = new int[nElements_padding];
// *rowDelimiters_ptr = new int[nRows+2];
*val_ptr = (floatType*) _mm_malloc(sizeof(floatType) * nElements_padding, 64);
*cols_ptr = (int*) _mm_malloc(sizeof(int) * nElements_padding, 64);
*rowDelimiters_ptr = (int*) _mm_malloc(sizeof(int) * (nRows+2), 64);
floatType *val = *val_ptr;
int *cols = *cols_ptr;
int *rowDelimiters = *rowDelimiters_ptr;
rowDelimiters[0] = 0;
// rowDelimiters[nRows] = nElements_padding;
int r=0;
// std::cout<<"444444444444444444444444444444444444"<<endl;
int i=0;
for (i=0; i<nElements_padding; i++)
{
while (coords[i].x != r)
{
rowDelimiters[++r] = i;
}
val[i] = coords[i].val;
cols[i] = coords[i].y;
// std::cout<<"i = "<<i<<endl;
}
for(int k = r + 1; k<=(nRows+1); k++)
{
rowDelimiters[k] = i-1;
// std::cout<<" rowDelimiter["<<k<<"] = "<< rowDelimiters[k]<<endl;
}
r = 0;
// delete[] coords;
free(coords);
// std::cout<<"55555555555555555555555555555555"<<endl;
}
// ****************************************************************************
// Function: fill
//
// Purpose:
// Simple routine to initialize input array
//
// Arguments:
// A: pointer to the array to initialize
// n: number of elements in the array
// maxi: specifies range of random values
//
// Programmer: Lukasz Wesolowski
// Creation: June 21, 2010
// Returns: nothing
//
// ****************************************************************************
//template <typename floatType>
void fill(floatType *A, const int n, const float maxi)
{
for (int j = 0; j < n; j++)
{
// A[j] = ((floatType) maxi * (rand() / (RAND_MAX + 1.0f)));
A[j] = 1.0;
}
}
void pre_processing(int Nthrds, int N_start, int N_step, int* vPack_Nblock, int* vPack_vec_record, int* vPack_nnz_rows, floatType* vPack_vec_vals, int* vPack_vec_cols, floatType* h_val, int* h_cols, int* vPack_vec_final, int* vPack_vec_final_2, int* vPack_split, floatType* refOut, int nItems, int numRows, int omega, int* h_rowDelimiters, char* filename)
{
// std::cout<<"=========********* ---------------------------------- *********=========="<<endl;
std::cout<<"==========================================================================="<<endl;
std::cout<<"=========********* Converting (CSR->CVR) *********=========="<<endl;
std::cout<<endl;
//std::cout<<" before pre-processing"<<endl;
double kk0 = microtime();
#pragma omp parallel num_threads(Nthrds)
// for(int thread_idx = 0; thread_idx < Nthrds; thread_idx++)
// for(int thread_idx = N_start; thread_idx < (N_start + N_step); thread_idx++)
{
int thread_idx = omp_get_thread_num();
// int thread_nnz = (nItems / Nthrds /omega/ 16 + 1) * 16 * omega;
int thread_nnz = (nItems / Nthrds /omega/ 16 ) * 16 * omega;
int thread_break = (nItems - thread_nnz * Nthrds) / 16;
int thread_nnz_2 = (nItems / Nthrds / omega / 16 + 1) * 16;
if (thread_idx >=N_start && thread_idx < (N_start + N_step))
{
int seg_idx = 0;
for(int row_block = 0; row_block < vPack_Nblock[thread_idx]; row_block ++)
{
if(seg_idx !=0)
seg_idx = (seg_idx/16 + 1) * 16;
int thread_nnz_start;
int thread_nnz_end;
/*
if(vPack_Nblock[thread_idx] != 1)
{
thread_nnz_start = thread_idx * thread_nnz_2 * omega + thread_nnz_2 * row_block;
thread_nnz_end = (thread_idx) * thread_nnz_2 * omega + thread_nnz_2 * (row_block+1);
}
else
{
thread_nnz_start = thread_idx * thread_nnz;
thread_nnz_end = (thread_idx+1) * thread_nnz;
}
*/
if(thread_idx < thread_break)
{
thread_nnz_start = thread_idx * (thread_nnz + 16);
thread_nnz_end = (thread_idx + 1) * (thread_nnz + 16);
}
else
{
thread_nnz_start = thread_idx * thread_nnz + thread_break * 16;
thread_nnz_end = (thread_idx + 1) * thread_nnz + thread_break * 16;
}
if(thread_idx == (Nthrds - 1) && (row_block == (vPack_Nblock[thread_idx] -1) || vPack_Nblock[thread_idx] == 1))
thread_nnz_end = nItems;
int thread_rows_start, thread_rows_end;
int start = 0;
int stop = numRows;
int median;
int key_median;
int key_median_2;
while (stop >= start)
{
median = (stop + start) / 2;
key_median = h_rowDelimiters[median];
key_median_2 = h_rowDelimiters[median+1];
if (thread_nnz_start >= key_median)
start = median + 1;
else
stop = median - 1;
}
thread_rows_start = start-1;
start = thread_rows_start;
stop = numRows;
while(stop >= start)
{
median = (stop + start) / 2;
key_median = h_rowDelimiters[median];
if((thread_nnz_end -1 ) >= key_median)
start = median + 1;
else
stop = median - 1;
}
thread_rows_end = start - 1;
/*
int ti;
for( ti=thread_rows_start; ti<=(numRows+1); ti++)
{
if(h_rowDelimiters[ti] > (thread_nnz_end -1))
{
thread_rows_end = ti -1;
break;
}
else if(h_rowDelimiters[ti] == (thread_nnz_end-1))
{
thread_rows_end = ti;
break;
}
}
*/
while((h_rowDelimiters[thread_rows_end + 1] - h_rowDelimiters[thread_rows_end] == 0) && (thread_rows_end <=numRows) )
thread_rows_end ++;
vPack_nnz_rows[thread_idx*4] = thread_nnz_start;
vPack_nnz_rows[thread_idx*4+1] = thread_nnz_end;
vPack_nnz_rows[thread_idx*4+2] = thread_rows_start;
vPack_nnz_rows[thread_idx*4+3] = thread_rows_end;
int thread_rows_span = thread_rows_end - thread_rows_start + 1;
floatType* vPack_vals = vPack_vec_vals + thread_nnz_start;
int* vPack_cols = vPack_vec_cols + thread_nnz_start;
floatType* orig_vals = h_val + thread_nnz_start;
int* orig_cols = h_cols + thread_nnz_start;
int* vPack_final = vPack_vec_final + thread_idx * 16;
int* vPack_final_2 = vPack_vec_final_2 + thread_idx * 16 * omega + 16 * row_block;
// int* vPack_record = vPack_vec_record + thread_idx * row_align * 2;
// int* vPack_record = (int*) _mm_malloc(sizeof(int) * (thread_rows_span+32) * 2, 64);
// vPack_list_record[thread_idx] = vPack_record;
int* vPack_record = vPack_vec_record + 2 * (thread_idx * 32 + thread_rows_start) / 16 * 16;
zmmd_t vPack_valID, vPack_rowID, vPack_count;
vPack_count.regi32 = _mm512_set1_epi32(1);
vPack_valID.regi32 = _mm512_set1_epi32(0);
vPack_rowID.regi32 = _mm512_set1_epi32(0);
//xbw
// if(bool_debug)
// std::cout<<" thread "<<thread_idx<<"; start_nnz = "<< thread_nnz_start<<"; end_nnz = "<< thread_nnz_end<<"; rows start: "<<thread_rows_start<<"; rows end: "<<thread_rows_end<<"; span = "<< thread_rows_span<<"; csr["<<thread_rows_start<<"] = "<<h_rowDelimiters[thread_rows_start]<<"; csr["<<thread_rows_end<<"] = "<<h_rowDelimiters[thread_rows_end]<<endl;
zmmd_t vPack_flag;
vPack_flag.regi32 = _mm512_set1_epi32(-1);
int thread_rows_start_init = thread_rows_start;
for(int i = 0; i < 8; i++)
{
if(thread_rows_start < thread_rows_end)
{
vPack_valID.elemsi32[i] = h_rowDelimiters[thread_rows_start] - thread_nnz_start;
vPack_rowID.elemsi32[i] = thread_rows_start;
vPack_count.elemsi32[i] = h_rowDelimiters[thread_rows_start+1] - h_rowDelimiters[thread_rows_start];
}
else if(thread_rows_start == thread_rows_end)
{
vPack_valID.elemsi32[i] = h_rowDelimiters[thread_rows_start] - thread_nnz_start;
vPack_rowID.elemsi32[i] = thread_rows_start;
vPack_count.elemsi32[i] = thread_nnz_end - h_rowDelimiters[thread_rows_start];
}
else if(thread_rows_start > thread_rows_end)
{
vPack_valID.elemsi32[i] = 0;
vPack_rowID.elemsi32[i] = 0;
vPack_count.elemsi32[i] = 0;
}
if(i==0)
{
vPack_valID.elemsi32[i] = 0;
vPack_count.elemsi32[i] = h_rowDelimiters[thread_rows_start+1] - thread_nnz_start;
if(thread_rows_start == thread_rows_end)
{
vPack_count.elemsi32[i] = thread_nnz_end - thread_nnz_start;
}
}
thread_rows_start ++; //should after the if(i==0)
}
// if(bool_debug)
// for(int kki = 0; kki< 8; kki++)
// std::cout<<"kki = "<< kki <<"; valID = "<< vPack_valID.elemsi32[kki]<<"; rowID = "<< vPack_rowID.elemsi32[kki]<<"; count = "<< vPack_count.elemsi32[kki]<<endl;
__m512i v_zero_epi32 = _mm512_set1_epi32(0);
__m512i v_one_epi32 = _mm512_set1_epi32(1);
__m512i v_two_epi32 = _mm512_set1_epi32(2);
__m512i v_four_epi32 = _mm512_set1_epi32(4);
__m512i v_eight_epi32 = _mm512_set1_epi32(8);
__m512i v_sixteen_epi32 = _mm512_set1_epi32(16);
__m512i v_0o8_epi32 = _mm512_set_epi32(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
__m512d v_zero_pd = _mm512_set1_pd(0);
__m512d v_one_pd = _mm512_set1_pd(1);
__m512d v_vals = _mm512_set1_pd(0);
__m512i v_cols, v_trans;
__mmask16 k_lo = _mm512_int2mask(0x00ff);
__mmask16 k_hi = _mm512_int2mask(0xff00);
__mmask16 mask_0xAAAA = _mm512_int2mask(0xaaaa);
__mmask16 mask_0x5555 = _mm512_int2mask(0x5555);
int i=0;
int first_in = 0;
int tt_index = 0;
zmmd_t tt_record;
tt_record.regi32 = _mm512_set1_epi32(0);
__m512i v_idx_cols = _mm512_set_epi32(15,7,13,5,11,3,9,1,14,6,12,4,10,2,8,0);
zmmd_t first_flag;
first_flag.regi32 = _mm512_set1_epi32(0);
int kk=0;
int kkdu = 0;
// #pragma prefetch vPack_vals:0:1024
// #pragma prefetch vPack_cols:0:1024
// #pragma noprefetch vPack_record
#pragma vector nontemporal(vPack_vals, vPack_cols, vPack_record)
for(i = 0; i < (thread_nnz_end - thread_nnz_start)/8; i++)
{
int xx = _mm512_mask_reduce_min_epi32(k_lo, vPack_count.regi32);
if( xx == 0)
{
for(int kk = 0; kk < 8; kk++)
{
if(vPack_count.elemsi32[kk] == 0)
{
// kkdu++;
if(thread_rows_start <= thread_rows_end)
{
// _mm_prefetch((const char *)&vPack_record[seg_idx], _MM_HINT_T1);
// _mm_prefetch((const char *)&vPack_record[seg_idx + 1], _MM_HINT_T1);
if(vPack_rowID.elemsi32[kk] == thread_rows_start_init)
{
vPack_split[thread_idx * 2] = i * 8 + kk;
}
else
{
vPack_record[seg_idx] = i*8+kk;
vPack_record[seg_idx + 1] = vPack_rowID.elemsi32[kk];
seg_idx += 2;
}
while(h_rowDelimiters[thread_rows_start+1] - h_rowDelimiters[thread_rows_start] ==0)
thread_rows_start ++;
vPack_valID.elemsi32[kk] = h_rowDelimiters[thread_rows_start] - thread_nnz_start;
vPack_rowID.elemsi32[kk] = thread_rows_start;
vPack_count.elemsi32[kk] = h_rowDelimiters[thread_rows_start+1] - h_rowDelimiters[thread_rows_start];
if(thread_rows_start == thread_rows_end )
{
if(vPack_split[thread_idx * 2 + 1] == 0)
{
vPack_split[thread_idx * 2 + 1] = i*8 + kk;
}
vPack_count.elemsi32[kk] = thread_nnz_end - h_rowDelimiters[thread_rows_start];
_mm512_mask_store_epi32(vPack_final_2, k_lo, vPack_rowID.regi32);
__mmask16 kzz = _mm512_cmpeq_epi32_mask(vPack_count.regi32, v_zero_epi32);
vPack_flag.regi32 = _mm512_mask_add_epi32(vPack_flag.regi32, kzz, v_zero_epi32, v_zero_epi32);
}
/*
if(bool_debug)
{
std::cout<<" seg_idx = "<< seg_idx/2 <<"; segs = "<< vPack_record[seg_idx]<<"; write = "<< vPack_record[seg_idx + 1] <<"; new row is "<< vPack_rowID.elemsi32[kk]<<"; new length is "<< vPack_count.elemsi32[kk] <<endl;
for(int kk9 = 0; kk9<8; kk9++)
std::cout<<" count["<< kk9<<"] = "<< vPack_count.elemsi32[kk9]<<endl;
}
*/
thread_rows_start ++;
} // end of if( <= )
else if(thread_rows_start > thread_rows_end)
{
int ave = _mm512_mask_reduce_add_epi32(k_lo, vPack_count.regi32)/8;
// _mm_prefetch((const char *)&vPack_record[seg_idx], _MM_HINT_NTA);
// _mm_prefetch((const char *)&vPack_record[seg_idx + 1], _MM_HINT_NTA);
int cali_i = 0;
for(cali_i = 0; cali_i < 8; cali_i ++)
if(vPack_count.elemsi32[cali_i] > ave)
break;
if(first_flag.elemsi32[kk] == 0 )
{
if(first_in == 0)
{
if(vPack_split[thread_idx * 2 + 1] == 0)
{
if(thread_rows_span <=8)
vPack_split[thread_idx * 2 + 1] = -1;
else
vPack_split[thread_idx * 2 + 1] = i*8 + kk;
}
_mm512_mask_store_epi32(vPack_final_2, k_lo, vPack_rowID.regi32);
first_in = 1;
}
vPack_record[seg_idx] = i*8 + kk;
vPack_record[seg_idx + 1] = kk;
vPack_flag.elemsi32[kk] = cali_i;
first_flag.elemsi32[kk] = 1;
}
else
{
vPack_record[seg_idx] = i*8 + kk;
vPack_record[seg_idx + 1] = vPack_flag.elemsi32[kk];
vPack_flag.elemsi32[kk] = cali_i;
}
/*
if(vPack_count.elemsi32[cali_i] >= 2*ave)
{
vPack_valID.elemsi32[kk] = vPack_valID.elemsi32[cali_i] + ave;
vPack_rowID.elemsi32[kk] = cali_i;
vPack_count.elemsi32[kk] = ave;
vPack_count.elemsi32[cali_i] = vPack_count.elemsi32[cali_i] - ave;
}
else
{
vPack_valID.elemsi32[kk] = vPack_valID.elemsi32[cali_i] + ave;
vPack_rowID.elemsi32[kk] = cali_i;
vPack_count.elemsi32[kk] = vPack_count.elemsi32[cali_i] - ave;
vPack_count.elemsi32[cali_i] = ave;
}
*/
{
vPack_valID.elemsi32[kk] = vPack_valID.elemsi32[cali_i];
vPack_rowID.elemsi32[kk] = cali_i;
vPack_count.elemsi32[kk] = ave;
vPack_count.elemsi32[cali_i] = vPack_count.elemsi32[cali_i] - ave;
vPack_valID.elemsi32[cali_i] = vPack_valID.elemsi32[cali_i] + ave;
}
/*
if(bool_debug)
{
std::cout<<" seg_idx = "<< seg_idx/2 <<"; segs = "<< vPack_record[seg_idx]<<"; write = "<< vPack_record[seg_idx + 1] <<"; new row is "<< vPack_rowID.elemsi32[kk]<<"; new length is "<< vPack_count.elemsi32[kk] <<endl;
for(int kk9 = 0; kk9<8; kk9++)
std::cout<<" count["<< kk9<<"] = "<< vPack_count.elemsi32[kk9]<<endl;
}
*/
seg_idx += 2;
}
}
}
}
// i--;
/*
int prefetch_distance = 8;
for(int k = 0; k< prefetch_distance/8; k++)
{
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[0] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[1] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[2] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[3] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[4] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[5] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[6] + 1], _MM_HINT_T0);
_mm_prefetch((const char *)&orig_vals[vPack_valID.elemsi32[7] + 1], _MM_HINT_T0);
}
*/
v_vals = _mm512_i32logather_pd(vPack_valID.regi32, orig_vals, _MM_SCALE_8);
// _mm512_storenrngo_pd(vPack_vals+i*8, v_vals);
_mm512_store_pd(vPack_vals+i*8, v_vals);
if(i%2==0)
{
v_trans = vPack_valID.regi32;
}
else
{
v_trans = _mm512_add_epi32( _mm512_permute4f128_epi32(vPack_valID.regi32,_MM_PERM_BADC), v_trans);
v_cols = _mm512_i32gather_epi32(v_trans, orig_cols, _MM_SCALE_4);
// _mm512_storenrngo_ps(vPack_cols + (i-1)*8 , v_cols);
_mm512_store_epi32(vPack_cols + (i-1)*8 , v_cols);
}
vPack_valID.regi32 = _mm512_mask_add_epi32(v_zero_epi32, k_lo, vPack_valID.regi32, v_one_epi32);
vPack_count.regi32 = _mm512_mask_sub_epi32(v_zero_epi32, k_lo, vPack_count.regi32, v_one_epi32);
if(i == (thread_nnz_end - thread_nnz_start)/8 - 1)
{
for(int kk=0; kk<8; kk++)
{
if(vPack_flag.elemsi32[kk] == -1)
{
vPack_record[seg_idx] = -1;
vPack_record[seg_idx + 1] = kk;
seg_idx += 2;
}
else
{
vPack_record[seg_idx] = -1;
vPack_record[seg_idx + 1] = vPack_flag.elemsi32[kk];
seg_idx += 2;
}
}
}
}