-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc-main.cpp
1807 lines (1439 loc) · 53.9 KB
/
sc-main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <fcntl.h>
#include <cstdlib>
#include <assert.h>
#include <fstream>
#include <math.h>
#include <sys/time.h>
#include <vector>
#include <iostream>
#include <sys/stat.h>
#include <string>
#include <cstring>
#include <unistd.h>
#include <climits>
#include <stdint.h>
#include <omp.h>
#include "Graph.hpp"
#include "CSRGraph.hpp"
#include "CSCGraph.hpp"
#include "CountMat.hpp"
#include "Helper.hpp"
#include "EdgeList.hpp"
// for testing pb radix
#ifndef NEC
#include "mkl.h"
#include "radix/commons/builder.h"
#include "radix/commons/command_line.h"
#include "radix/pr.h"
// for testing spmd3
#include "SpDM3/include/dmat.h"
#include "SpDM3/include/spmat.h"
#include "SpDM3/include/matmul.h"
// for RCM reordering
#include "SpMP/CSR.hpp"
#endif
#include <sys/time.h>
#include <float.h>
#ifdef __INTEL_COMPILER
// use avx intrinsics
#include "immintrin.h"
#include "zmmintrin.h"
#endif
static long TestArraySize = 500000000;
using namespace std;
double mysecond()
{
struct timeval tp;
struct timezone tzp;
int i;
i = gettimeofday(&tp,&tzp);
return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 );
}
#define M 20
#ifndef MIN
#define MIN(x,y) ((x)<(y)?(x):(y))
#endif
#ifndef MAX
#define MAX(x,y) ((x)>(y)?(x):(y))
#endif
int checktick()
{
int i, minDelta, Delta;
double t1, t2, timesfound[M];
/* Collect a sequence of M unique time values from the system. */
for (i = 0; i < M; i++) {
t1 = mysecond();
while( ((t2=mysecond()) - t1) < 1.0E-6 )
;
timesfound[i] = t1 = t2;
}
/*
* * Determine the minimum difference between these M values.
* * This result will be our estimate (in microseconds) for the
* * clock granularity.
* */
minDelta = 1000000;
for (i = 1; i < M; i++) {
Delta = (int)( 1.0E6 * (timesfound[i]-timesfound[i-1]) );
minDelta = MIN(minDelta, MAX(Delta,0));
}
return(minDelta);
}
// LLC 33 for Skylake
static const size_t LLC_CAPACITY = 33*1024*1024;
void flushLlc(float* bufToFlushLlc)
{
double sum = 0;
#pragma omp parallel for reduction(+:sum)
for (size_t i = 0; i < LLC_CAPACITY/sizeof(bufToFlushLlc[0]); ++i) {
sum += bufToFlushLlc[i];
}
FILE *fp = fopen("/dev/null", "w");
fprintf(fp, "%f\n", sum);
fclose(fp);
}
#ifndef NEC
void benchmarkSpMVPBRadix(int argc, char** argv, EdgeList& elist, int numCols)
{
double startTime = 0.0;
double timeElapsed = 0.0;
int binSize = 15;
if (argc > 9)
binSize = atoi(argv[9]);
// -------------------- start debug the Radix SpMV ------------------------------
printf("start radix spmv\n");
std::fflush(stdout);
// for radix
typedef BuilderBase<int32_t, int32_t, int32_t> Builder;
typedef RCSRGraph<int32_t> RGraph;
// EdgeList elist(graph_name);
pvector<EdgePair<int32_t, int32_t> > radixList(elist.getNumEdges());
elist.convertToRadixList(radixList);
CLBase cli(argc, argv);
Builder b(cli);
RGraph radixG = b.MakeGraphFromEL(radixList);
printf("Finish build radix graph, vert: %d\n", radixG.num_nodes());
std::fflush(stdout);
double flopsTotal = 2*radixG.num_edges();
double bytesTotal = sizeof(float)*(radixG.num_edges() + 2*radixG.num_nodes()) + sizeof(int)*(radixG.num_edges() + radixG.num_nodes());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
pvector<ParGuider<int32_t, float>*> par_guides(omp_get_max_threads());
#pragma omp parallel
// par_guides[omp_get_thread_num()] = new ParGuider<int32_t, float>(19,omp_get_thread_num(), omp_get_max_threads(), radixG);
par_guides[omp_get_thread_num()] = new ParGuider<int32_t, float>(binSize,omp_get_thread_num(), omp_get_max_threads(), radixG);
printf("Finish build par_parts\n");
std::fflush(stdout);
float* xMat = (float*) _mm_malloc(radixG.num_nodes()*sizeof(float), 64);
float* yMat = (float*) _mm_malloc(radixG.num_nodes()*sizeof(float), 64);
#pragma omp parallel for
for (int i = 0; i < radixG.num_nodes(); ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
startTime = utility::timer();
// check pagerank scores
// for (int j = 0; j < 100; ++j) {
// SpMVRadixPar(xMat, yMat, radixG, 1, kGoalEpsilon, par_parts);
SpMVGuidesPar(xMat, yMat, radixG, numCols, kGoalEpsilon, par_guides);
// }
//
timeElapsed = (utility::timer() - startTime);
printf("Radix SpMV using %f secs\n", timeElapsed/numCols);
printf("Radix SpMV Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("Radix SpMV Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("Radix SpMV Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
//
// check yMat
// for (int i = 0; i < 10; ++i) {
// printf("Elem: %d is: %f\n", i, yMat[i]);
// std::fflush(stdout);
// }
// free memory
_mm_free(xMat);
_mm_free(yMat);
// -------------------- end debug the Radix SpMV ------------------------------
}
#endif
// test bandwidth utilization and throughput
void benchmarkSpMVNaive(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
double startTime = 0.0;
double timeElapsed = 0.0;
CSRGraph csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), false, false, true);
double flopsTotal = csrnaiveG.getNNZ();
double bytesTotal = sizeof(float)*2*csrnaiveG.getNumVertices() + sizeof(int)*(csrnaiveG.getNNZ() + csrnaiveG.getNumVertices());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
#ifndef NEC
float* xMat = (float*)_mm_malloc(csrnaiveG.getNumVertices()*sizeof(float), 64);
float* yMat = (float*)_mm_malloc(csrnaiveG.getNumVertices()*sizeof(float), 64);
#else
float* xMat = (float*)malloc(csrnaiveG.getNumVertices()*sizeof(float));
float* yMat = (float*)malloc(csrnaiveG.getNumVertices()*sizeof(float));
#endif
#pragma omp parallel for
for (int i = 0; i < csrnaiveG.getNumVertices(); ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
#ifndef NEC
float* bufToFlushLlc = (float*)_mm_malloc(LLC_CAPACITY, 64);
#else
float* bufToFlushLlc = (float*)malloc(LLC_CAPACITY);
#endif
// test SpMV naive
// startTime = utility::timer();
for (int j = 0; j < numCols; ++j) {
// flush out LLC
for (int k = 0; k < 16; ++k) {
flushLlc(bufToFlushLlc);
}
// startTime = omp_get_wtime();
startTime = utility::timer();
csrnaiveG.SpMVNaive(xMat, yMat, comp_thds);
// timeElapsed += (omp_get_wtime() = startTime);
timeElapsed += (utility::timer() - startTime);
}
printf("Naive SpMV using %f secs\n", timeElapsed/numCols);
printf("Naive SpMV Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("Naive SpMV Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("Naive SpMV Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
// check yMat
for (int i = 0; i < 10; ++i) {
printf("Elem: %d is: %f\n", i, yMat[i]);
std::fflush(stdout);
}
#ifndef NEC
_mm_free(xMat);
_mm_free(yMat);
_mm_free(bufToFlushLlc);
#else
free(xMat);
free(yMat);
free(bufToFlushLlc);
#endif
}
void benchmarkSpMVNaiveFull(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
double startTime = 0.0;
double timeElapsed = 0.0;
CSRGraph csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), false, false, true);
double flopsTotal = 2*csrnaiveG.getNNZ();
// read n x, nnz val, write n y, Index col idx, row idx
double bytesTotal = sizeof(float)*(csrnaiveG.getNNZ() + 2*csrnaiveG.getNumVertices())
+ sizeof(int)*(csrnaiveG.getNNZ() + csrnaiveG.getNumVertices());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
int testLen = csrnaiveG.getNumVertices()*numCols;
#ifndef NEC
float* xMat = (float*)_mm_malloc(testLen*sizeof(float), 64);
float* yMat = (float*)_mm_malloc(testLen*sizeof(float), 64);
#else
float* xMat = (float*)malloc(testLen*sizeof(float));
float* yMat = (float*)malloc(testLen*sizeof(float));
#endif
#pragma omp parallel for
for (int i = 0; i < testLen; ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
#ifndef NEC
float* bufToFlushLlc = (float*)_mm_malloc(LLC_CAPACITY, 64);
#else
float* bufToFlushLlc = (float*)malloc(LLC_CAPACITY);
#endif
// test SpMV naive
// startTime = utility::timer();
for (int j = 0; j < numCols; ++j) {
// // flush out LLC
// for (int k = 0; k < 16; ++k) {
// flushLlc(bufToFlushLlc);
// }
startTime = utility::timer();
csrnaiveG.SpMVNaiveFull(xMat+j*csrnaiveG.getNumVertices(),
yMat+j*csrnaiveG.getNumVertices(), comp_thds);
timeElapsed += (utility::timer() - startTime);
}
printf("Naive SpMVFull using %f secs\n", timeElapsed/numCols);
printf("Naive SpMVFull Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("Naive SpMVFull Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("Naive SpMVFull Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
// check yMat
for (int i = 0; i < 10; ++i) {
printf("Elem: %d is: %f\n", i, yMat[i]);
std::fflush(stdout);
}
#ifndef NEC
_mm_free(xMat);
_mm_free(yMat);
_mm_free(bufToFlushLlc);
#else
free(xMat);
free(yMat);
free(bufToFlushLlc);
#endif
}
void benchmarkSpMVNaiveFullCSC(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
double startTime = 0.0;
double timeElapsed = 0.0;
CSCGraph<int32_t, float> csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList());
double flopsTotal = 2*csrnaiveG.getNNZ();
// read n x, nnz val, write n y, Index col idx, row idx
double bytesTotal = sizeof(float)*(csrnaiveG.getNNZ() + 2*csrnaiveG.getNumVertices())
+ sizeof(int)*(csrnaiveG.getNNZ() + csrnaiveG.getNumVertices());
csrnaiveG.splitCSC(4*comp_thds);
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
#ifndef NEC
float* xMat = (float*)_mm_malloc(csrnaiveG.getNumVertices()*sizeof(float), 64);
float* yMat = (float*)_mm_malloc(csrnaiveG.getNumVertices()*sizeof(float), 64);
#else
float* xMat = (float*)malloc(csrnaiveG.getNumVertices()*sizeof(float));
float* yMat = (float*)malloc(csrnaiveG.getNumVertices()*sizeof(float));
#endif
#pragma omp parallel for
for (int i = 0; i < csrnaiveG.getNumVertices(); ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
#ifndef NEC
float* bufToFlushLlc = (float*)_mm_malloc(LLC_CAPACITY, 64);
#else
float* bufToFlushLlc = (float*)malloc(LLC_CAPACITY);
#endif
// test SpMV naive
// startTime = utility::timer();
for (int j = 0; j < numCols; ++j) {
// flush out LLC
// for (int k = 0; k < 16; ++k) {
// flushLlc(bufToFlushLlc);
// }
// clear the yMat for each iteration
#pragma omp parallel for
for (int i = 0; i < csrnaiveG.getNumVertices(); ++i) {
yMat[i] = 0.0;
}
startTime = utility::timer();
csrnaiveG.spmvNaiveSplit(xMat, yMat, comp_thds);
timeElapsed += (utility::timer() - startTime);
}
printf("Naive SpMVFull CSC using %f secs\n", timeElapsed/numCols);
printf("Naive SpMVFull CSC Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("Naive SpMVFull CSC Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("Naive SpMVFull CSC Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
//check yMat
for (int i = 0; i < 10; ++i) {
printf("Elem: %d is: %f\n", i, yMat[i]);
std::fflush(stdout);
}
#ifndef NEC
_mm_free(xMat);
_mm_free(yMat);
_mm_free(bufToFlushLlc);
#else
free(xMat);
free(yMat);
free(bufToFlushLlc);
#endif
}
#ifndef NEC
void benchmarkCSCSplitMM(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds, int benchItr)
{
double startTime = 0.0;
double timeElapsed = 0.0;
int iteration = benchItr;
CSCGraph<int32_t, float> csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList());
double nnz = csrnaiveG.getNNZ();
double flopsTotalPerNNZ = numCols;
// read n x, nnz val, write n y, Index col idx, row idx
// double bytesTotal = sizeof(float)*(2*csrnaiveG.getNumVertices())
// + sizeof(int)*(csrnaiveG.getNNZ() + csrnaiveG.getNumVertices());
// here CSC-Split SpMM has different memeory access from CSR-SpMV/SpMM
// nnz row id + nnz col id + nnz y val + numCol*nnz x val
double bytesTotalPerNNZ = sizeof(int)*2 + sizeof(float)*(1 + numCols);
csrnaiveG.splitCSC(4*comp_thds);
// flopsTotal /= (1024*1024*1024);
// bytesTotal /= (1024*1024*1024);
// right-hand multiple vectors
int testLen = csrnaiveG.getNumVertices()*numCols;
float* xMat = (float*)_mm_malloc(testLen*sizeof(float), 64);
float* yMat = (float*)_mm_malloc(testLen*sizeof(float), 64);
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < testLen; ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
float* bufToFlushLlc = (float*)_mm_malloc(LLC_CAPACITY, 64);
#ifdef VTUNE
ofstream vtune_trigger;
vtune_trigger.open("vtune-flag.txt");
vtune_trigger << "Start training process and trigger vtune profiling.\n";
vtune_trigger.close();
#endif
// test CSC-Split SpMM
for (int i = 0; i < iteration; ++i) {
// flush out LLC
// for (int k = 0; k < 16; ++k) {
// flushLlc(bufToFlushLlc);
// }
startTime = utility::timer();
csrnaiveG.spmmSplit(xMat, yMat, numCols, comp_thds);
timeElapsed += (utility::timer() - startTime);
}
printf("CSC-Split SpMM total testing %f secs\n", timeElapsed);
printf("CSC-Split SpMM Compute using %f secs\n", timeElapsed/(numCols*iteration));
printf("CSC-Split Arith Intensity %f\n", (flopsTotalPerNNZ/bytesTotalPerNNZ));
printf("CSC-Split Bd: %f GBytes/sec\n", (bytesTotalPerNNZ*(nnz/(1024*1024*1024)))/(timeElapsed/iteration));
printf("CSC-Split Tht: %f GFLOP/sec\n", (flopsTotalPerNNZ*(nnz/(1024*1024*1024)))/(timeElapsed/iteration));
std::fflush(stdout);
//check yMat
// for (int i = 0; i < 10; ++i) {
// printf("Elem: %d is: %f\n", i, yMat[i]);
// std::fflush(stdout);
// }
_mm_free(xMat);
_mm_free(yMat);
_mm_free(bufToFlushLlc);
}
#endif
#ifndef NEC
// Inspector-Executor interface in MKL 11.3+
// NOTICE: the way to invoke the mkl 11.3 inspector-executor
void benchmarkSpMVMKL(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
int numCalls = numCols;
double startTime = 0.0;
double timeElapsed = 0.0;
CSRGraph csrGMKL;
csrGMKL.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), true, false, true);
double flopsTotal = 2*csrGMKL.getNNZ();
double bytesTotal = sizeof(float)*(csrGMKL.getNNZ() + 2*csrGMKL.getNumVertices()) + sizeof(int)*(csrGMKL.getNNZ() + csrGMKL.getNumVertices());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
sparse_matrix_t mklA;
sparse_status_t stat = mkl_sparse_s_create_csr(
&mklA,
SPARSE_INDEX_BASE_ZERO, csrGMKL.getNumVertices(), csrGMKL.getNumVertices(),
csrGMKL.getIndexRow(), csrGMKL.getIndexRow() + 1,
csrGMKL.getIndexCol(), csrGMKL.getNNZVal());
if (SPARSE_STATUS_SUCCESS != stat) {
fprintf(stderr, "Failed to create mkl csr\n");
return;
}
matrix_descr descA;
descA.type = SPARSE_MATRIX_TYPE_GENERAL;
descA.diag = SPARSE_DIAG_NON_UNIT;
stat = mkl_sparse_set_mv_hint(mklA, SPARSE_OPERATION_NON_TRANSPOSE, descA, numCalls);
if (SPARSE_STATUS_SUCCESS != stat) {
fprintf(stderr, "Failed to set mv hint\n");
return;
}
stat = mkl_sparse_optimize(mklA);
if (SPARSE_STATUS_SUCCESS != stat) {
fprintf(stderr, "Failed to sparse optimize\n");
return;
}
float* xArray = (float*) _mm_malloc(csrGMKL.getNumVertices()*sizeof(float), 64);
float* yArray = (float*) _mm_malloc(csrGMKL.getNumVertices()*sizeof(float), 64);
#pragma omp parallel for
for (int i = 0; i < csrGMKL.getNumVertices(); ++i) {
xArray[i] = 2.0;
yArray[i] = 0.0;
}
// float* yArray = (float*) malloc(csrGMKL.getNumVertices()*sizeof(float));
// std::memset(yArray, 0, csrGMKL.getNumVertices()*sizeof(float));
float* bufToFlushLlc = (float*)_mm_malloc(LLC_CAPACITY, 64);
for (int j = 0; j < numCols; ++j) {
// flush out LLC
for (int k = 0; k < 16; ++k) {
flushLlc(bufToFlushLlc);
}
startTime = utility::timer();
mkl_sparse_s_mv(SPARSE_OPERATION_NON_TRANSPOSE, 1, mklA, descA, xArray, 0, yArray);
timeElapsed += (utility::timer() - startTime);
}
printf("MKL SpMV using %f secs\n", timeElapsed/numCols);
printf("MKL SpMV Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("MKL SpMV Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("MKL SpMV Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
// check yMat
// for (int i = 0; i < 10; ++i) {
// printf("Elem: %d is: %f\n", i, yArray[i]);
// std::fflush(stdout);
// }
_mm_free(xArray);
_mm_free(yArray);
_mm_free(bufToFlushLlc);
}
#endif
#ifndef NEC
void benchmarkMMMKL(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
double startTime;
const int calls = 100;
CSRGraph csrGMKL;
csrGMKL.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), true, false, true);
double flopsTotal = 2*csrGMKL.getNNZ();
double bytesTotal = sizeof(float)*(csrGMKL.getNNZ() + 2*csrGMKL.getNumVertices()) + sizeof(int)*(csrGMKL.getNNZ() + csrGMKL.getNumVertices());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
sparse_matrix_t mklA;
sparse_status_t stat = mkl_sparse_s_create_csr(
&mklA,
SPARSE_INDEX_BASE_ZERO, csrGMKL.getNumVertices(), csrGMKL.getNumVertices(),
csrGMKL.getIndexRow(), csrGMKL.getIndexRow() + 1,
csrGMKL.getIndexCol(), csrGMKL.getNNZVal());
if (SPARSE_STATUS_SUCCESS != stat) {
fprintf(stderr, "Failed to create mkl csr\n");
return;
}
matrix_descr descA;
descA.type = SPARSE_MATRIX_TYPE_GENERAL;
descA.diag = SPARSE_DIAG_NON_UNIT;
stat = mkl_sparse_set_mm_hint(mklA, SPARSE_OPERATION_NON_TRANSPOSE, descA, SPARSE_LAYOUT_COLUMN_MAJOR, numCols, calls);
if (SPARSE_STATUS_SUCCESS != stat) {
fprintf(stderr, "Failed to set mm hint\n");
return;
}
stat = mkl_sparse_optimize(mklA);
if (SPARSE_STATUS_SUCCESS != stat) {
fprintf(stderr, "Failed to sparse optimize\n");
return;
}
int testLen = numCols*csrGMKL.getNumVertices();
float* xMat = (float*) _mm_malloc(testLen*sizeof(float), 64);
float* yMat = (float*) _mm_malloc(testLen*sizeof(float), 64);
#pragma omp parallel for
for (int i = 0; i < testLen; ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
startTime = utility::timer();
mkl_sparse_s_mm(SPARSE_OPERATION_NON_TRANSPOSE, 1, mklA, descA, SPARSE_LAYOUT_COLUMN_MAJOR,
xMat, numCols, csrGMKL.getNumVertices(), 0, yMat, csrGMKL.getNumVertices());
double timeElapsed = (utility::timer() - startTime);
printf("MKL MM using %f secs\n", timeElapsed);
printf("MKL MM using %f secs per SpMV\n", timeElapsed/numCols);
printf("MKL MM Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("MKL MM Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("MKL MM Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
// check yMat
// for (int i = 0; i < 10; ++i) {
// printf("Elem: %d is: %f\n", i, yMat[i]);
// std::fflush(stdout);
// }
_mm_free(xMat);
_mm_free(yMat);
}
#endif
#ifndef NEC
void benchmarkSpMMMKL(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
double startTime = 0.0;
double timeElapsed = 0.0;
printf("Start debug CSR SpMM \n");
std::fflush(stdout);
CSRGraph csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), true, false, true);
double flopsTotal = 2*csrnaiveG.getNNZ();
double bytesTotal = sizeof(float)*(csrnaiveG.getNNZ() + 2*csrnaiveG.getNumVertices()) + sizeof(int)*(csrnaiveG.getNNZ() + csrnaiveG.getNumVertices());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
//
int csrNNZA = csrnaiveG.getNNZ();
int csrRows = csrnaiveG.getNumVertices();
int* csrRowIdx = csrnaiveG.getIndexRow();
int* csrColIdx = csrnaiveG.getIndexCol();
float* csrVals = csrnaiveG.getNNZVal();
int testLen = numCols*csrRows;
float* xMat = (float*) _mm_malloc(testLen*sizeof(float), 64);
float* yMat = (float*) _mm_malloc(testLen*sizeof(float), 64);
// float* yMat = (float*) malloc(testLen*sizeof(float));
// std::memset(yMat, 0, testLen*sizeof(float));
#pragma omp parallel for
for (int i = 0; i < testLen; ++i) {
xMat[i] = 2.0;
yMat[i] = 0.0;
}
// invoke mkl scsrmm
char transa = 'n';
MKL_INT m = csrRows;
MKL_INT n = numCols;
MKL_INT k = csrRows;
float alpha = 1.0;
float beta = 0.0;
char matdescra[5];
matdescra[0] = 'g';
matdescra[3] = 'f'; /*one-based indexing is used*/
startTime = utility::timer();
mkl_scsrmm(&transa, &m, &n, &k, &alpha, matdescra, csrVals, csrColIdx, csrRowIdx, &(csrRowIdx[1]), xMat, &k, &beta, yMat, &k);
timeElapsed += (utility::timer() - startTime);
printf("MKL Old CSR SpMM using %f secs\n", timeElapsed);
printf("MKL Old CSR SpMM using %f secs per SpMV\n", timeElapsed/numCols);
printf("MKL Old CSR SpMM Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("MKL Old CSR SpMM Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("MKL Old CSR SpMM Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
// check yMat
// for (int i = 0; i < 10; ++i) {
// printf("Elem: %d is: %f\n", i, yMat[i]);
// std::fflush(stdout);
// }
// free test mem
_mm_free(xMat);
_mm_free(yMat);
printf("Finish debug CSR SpMM\n");
std::fflush(stdout);
}
#endif
#ifndef NEC
void benchmarkSpDM3(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds)
{
double startTime = 0.0;
double timeElapsed = 0.0;
printf("Start debug Spdm3 SpMM\n");
std::fflush(stdout);
CSRGraph csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), false, false, true);
double flopsTotal = 2*csrnaiveG.getNNZ();
double bytesTotal = sizeof(float)*(csrnaiveG.getNNZ() + 2*csrnaiveG.getNumVertices()) + sizeof(int)*(csrnaiveG.getNNZ() + csrnaiveG.getNumVertices());
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
spdm3::SpMat<int, float> smat(spdm3::SPARSE_CSR, 0);
csrnaiveG.fillSpMat(smat);
// use smat
int rowNum = smat.dim1();
int testLen = rowNum*numCols;
float* xArray = (float*) _mm_malloc (testLen*sizeof(float), 64);
float* yArray = (float*) _mm_malloc (testLen*sizeof(float), 64);
#pragma omp parallel for
for (int i = 0; i < testLen; ++i) {
xArray[i] = 2.0;
yArray[i] = 0.0;
}
// float* yArray = (float*) malloc (testLen*sizeof(float));
// std::memset(yArray, 0, testLen*sizeof(float));
// data copy from xArray to xMat
// TODO replace data copy by pointer assignment
spdm3::DMat<int, float> xMat(rowNum, numCols, rowNum, spdm3::DENSE_COLMAJOR, xArray);
spdm3::DMat<int, float> yMat(rowNum, numCols, rowNum, spdm3::DENSE_COLMAJOR, yArray);
printf("Dmat: row: %d, cols: %d\n", xMat.rows_, xMat.cols_);
std::fflush(stdout);
startTime = utility::timer();
// start the SpMM
spdm3::matmul_blas_colmajor<int>(smat, xMat, yMat);
timeElapsed = (utility::timer() - startTime);
printf("SpDM3 SpMM using %f secs\n", timeElapsed);
printf("SpDM3 SpMM using %f secs per SpMV\n", timeElapsed/numCols);
printf("SpDM3 SpMM Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("SpDM3 SpMM Bd: %f GBytes/sec\n", bytesTotal*numCols/timeElapsed);
printf("SpDM3 SpMM Tht: %f GFLOP/sec\n", flopsTotal*numCols/timeElapsed);
std::fflush(stdout);
// check yMat
// for (int i = 0; i < 10; ++i) {
// printf("Elem: %d is: %f\n", i, yMat.values_[i]);
// std::fflush(stdout);
// }
printf("Finish debug Spdm3 SpMM\n");
std::fflush(stdout);
_mm_free(xArray);
_mm_free(yArray);
}
#endif
void arrayWiseFMAAVX(float** blockPtrDst,float** blockPtrA,float** blockPtrB, int* blockSize,
int blockSizeBasic, float* dst, float* a, float* b, int num_threads)
{
blockPtrDst[0] = dst;
blockPtrA[0] = a;
blockPtrB[0] = b;
//
for (int i = 1; i < num_threads; ++i) {
blockPtrDst[i] = blockPtrDst[i-1] + blockSizeBasic;
blockPtrA[i] = blockPtrA[i-1] + blockSizeBasic;
blockPtrB[i] = blockPtrB[i-1] + blockSizeBasic;
}
//#pragma omp parallel for schedule(static) num_threads(num_threads)
for(int i=0; i<num_threads; i++)
{
float* blockPtrDstLocal = blockPtrDst[i];
float* blockPtrALocal = blockPtrA[i];
float* blockPtrBLocal = blockPtrB[i];
int blockSizeLocal = blockSize[i];
// nec vec opt
//#pragma omp simd aligned(dst, a, b: 64)
for(int j=0; j<blockSizeLocal;j++)
blockPtrDstLocal[j] = blockPtrDstLocal[j] + blockPtrALocal[j]*blockPtrBLocal[j];
}
}
// benchmark the EMA codes
// element-wised vector multiplication and addition
// with LLC flush
void benchmarkEMA(int argc, char** argv, EdgeList& elist, int numCols, int comp_thds, int benchItr)
{
int iteration = benchItr;
printf("Start benchmarking eMA\n");
std::fflush(stdout);
printf("Input EdgeList: vert: %d, Edges: %d\n", elist.getNumVertices(), elist.getNumEdges());
std::fflush(stdout);
double startTime = 0.0;
double timeElapsed = 0.0;
CSRGraph csrnaiveG;
csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), false, false, true);
printf("Input Graph: vert: %d, NNZ: %d\n", csrnaiveG.getNumVertices(), csrnaiveG.getNNZ());
std::fflush(stdout);
long testArraySize = csrnaiveG.getNumVertices();
// a mul plus a add
double flopsTotal = 2*testArraySize;
// z += x*y
// 3n read/write
double bytesTotal = sizeof(float)*(3*testArraySize);
flopsTotal /= (1024*1024*1024);
bytesTotal /= (1024*1024*1024);
#ifndef NEC
float* xMat = (float*)_mm_malloc(testArraySize*sizeof(float), 64);
float* yMat = (float*)_mm_malloc(testArraySize*sizeof(float), 64);
float* zMat = (float*)_mm_malloc(testArraySize*sizeof(float), 64);
#else
//float* xMat = (float*)aligned_alloc(64,testArraySize*sizeof(float));
//float* yMat = (float*)aligned_alloc(64,testArraySize*sizeof(float));
//float* zMat = (float*)aligned_alloc(64,testArraySize*sizeof(float));
float* xMat = (float*)malloc(testArraySize*sizeof(float));
float* yMat = (float*)malloc(testArraySize*sizeof(float));
float* zMat = (float*)malloc(testArraySize*sizeof(float));
#endif
#pragma omp parallel for
for (int i = 0; i < testArraySize; ++i) {
xMat[i] = 2.0;
yMat[i] = 2.0;
zMat[i] = 0.0;
}
#ifndef NEC
float* bufToFlushLlc = (float*)_mm_malloc(LLC_CAPACITY, 64);
#else
float* bufToFlushLlc = (float*)aligned_alloc(64, LLC_CAPACITY);
#endif
int blockSizeBasic = testArraySize/comp_thds;
int* blockSize = (int*) malloc(comp_thds*sizeof(int));
for (int i = 0; i < comp_thds; ++i) {
blockSize[i] = blockSizeBasic;
}
blockSize[comp_thds-1] = ((testArraySize%comp_thds == 0) ) ? blockSizeBasic :
(blockSizeBasic + (testArraySize%comp_thds));
float** blockPtrDst = (float**) malloc(comp_thds*sizeof(float*));
float** blockPtrA = (float**) malloc(comp_thds*sizeof(float*));
float** blockPtrB = (float**) malloc(comp_thds*sizeof(float*));
for (int i = 0; i < comp_thds; ++i) {
blockPtrDst[i] = nullptr;
blockPtrA[i] = nullptr;
blockPtrB[i] = nullptr;
}
#ifdef VTUNE
ofstream vtune_trigger;
vtune_trigger.open("vtune-flag.txt");
vtune_trigger << "Start training process and trigger vtune profiling.\n";
vtune_trigger.close();
#endif
for (int j = 0; j < numCols*iteration; ++j) {
// flush out LLC
// for (int k = 0; k < 16; ++k) {
// flushLlc(bufToFlushLlc);
// }
startTime = utility::timer();
arrayWiseFMAAVX(blockPtrDst, blockPtrA, blockPtrB, blockSize, blockSizeBasic,
zMat, xMat, yMat, comp_thds);
timeElapsed += (utility::timer() - startTime);
}
printf("EMA using %f secs\n", timeElapsed);
printf("EMA using %f secs per col\n", timeElapsed/(numCols*iteration));
printf("EMA Arith Intensity %f\n", (flopsTotal/bytesTotal));
printf("EMA Bd: %f GBytes/sec\n", bytesTotal*numCols*iteration/timeElapsed);
printf("EMA Tht: %f GFLOP/sec\n", flopsTotal*numCols*iteration/timeElapsed);
std::fflush(stdout);
#ifndef NEC
_mm_free(xMat);
_mm_free(yMat);
_mm_free(zMat);
_mm_free(bufToFlushLlc);
#else
free(xMat);
free(yMat);
free(zMat);
free(bufToFlushLlc);
#endif
free(blockPtrDst);
free(blockPtrA);
free(blockPtrB);
free(blockSize);
}
void benchmarkEMANEC(int argc, char** argv, int numCols, int comp_thds, int benchItr)
{
int iteration = benchItr;
printf("Start benchmarking eMA\n");
std::fflush(stdout);
printf("Granularity of clock tick: %d microseconds\n", checktick());
std::fflush(stdout);
//printf("Input EdgeList: vert: %d, Edges: %d\n", elist.getNumVertices(), elist.getNumEdges());
//std::fflush(stdout);
double startTime = 0.0;
double timeElapsed = 0.0;
//CSRGraph csrnaiveG;
//csrnaiveG.createFromEdgeListFile(elist.getNumVertices(), elist.getNumEdges(), elist.getSrcList(), elist.getDstList(), false, false, true);
//printf("Input Graph: vert: %d, NNZ: %d\n", csrnaiveG.getNumVertices(), csrnaiveG.getNNZ());
//std::fflush(stdout);
//long testArraySize = 500000000;
// a mul plus a add