-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountMat.cpp
1575 lines (1253 loc) · 42.7 KB
/
CountMat.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
// the implementation of the color-coding algorithms
// Author: Langshi Chen
#include <cassert>
#include <math.h>
#include<cstdlib>
#include<ctime>
#include <cstring>
#include <omp.h>
#include <vector>
#include <map>
#ifndef NEC
#include "mkl.h"
#endif
#include "CountMat.hpp"
#include "Helper.hpp"
using namespace std;
void CountMat::initialization(CSRGraph* graph, CSCGraph<int32_t, float>* graphCSC, int thd_num, int itr_num, int isPruned, int useSPMM, int vtuneStart, bool calculate_automorphisms)
{
// either use _graph or _graphCSC
_graph = graph;
_graphCSC = graphCSC;
if (_graph != nullptr)
_vert_num = _graph->getNumVertices();
else
_vert_num = _graphCSC->getNumVertices();
_thd_num = thd_num;
_itr_num = itr_num;
_isPruned = isPruned;
_useSPMM = useSPMM;
_isScaled = 0;
_vtuneStart = vtuneStart;
_calculate_automorphisms = calculate_automorphisms;
// mkl spmm use one-based csr
if (_graph != nullptr && _graph->useMKL() && _useSPMM == 1)
_graph->makeOneIndex();
_colors_local = (int*)malloc(_vert_num*sizeof(int));
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < _vert_num; ++i) {
_colors_local[i] = 0;
}
#ifdef __INTEL_COMPILER
_bufVec = (float*) _mm_malloc(_vert_num*sizeof(float), 64);
#else
_bufVec = (float*) aligned_alloc(64, _vert_num*sizeof(float));
#endif
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < _vert_num; ++i) {
_bufVec[i] = 0;
}
// doing 16 SIMD float operations
_bufMatCols = 16;
#ifdef __INTEL_COMPILER
_bufMatY = (float*) _mm_malloc(_vert_num*_bufMatCols*sizeof(float), 64);
_bufMatX = (float*) _mm_malloc(_vert_num*_bufMatCols*sizeof(float), 64);
#else
_bufMatY = (float*) aligned_alloc(64, _vert_num*_bufMatCols*sizeof(float));
_bufMatX = (float*) aligned_alloc(64, _vert_num*_bufMatCols*sizeof(float));
#endif
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < _vert_num*_bufMatCols; ++i) {
_bufMatX[i] = 0;
_bufMatY[i] = 0;
}
}
double CountMat::compute(Graph& templates, bool isEstimate)
{/*{{{*/
_templates = &templates;
_color_num = _templates->get_vert_num();
div_tp.DivideTp(*(_templates));
div_tp.sort_tps();
_subtmp_array = div_tp.get_subtps();
_total_sub_num = div_tp.get_subtps_num();
#ifdef VERBOSE
printSubTemps();
#endif
// create the index tables
indexer.initialization(_color_num, _total_sub_num, &_subtmp_array, &div_tp);
// check the effective aux indices
// for (int s = 0; s < _total_sub_num; ++s) {
// printf("Effectiv sub %d\n", s);
// std::fflush(stdout);
// std::vector<int>* effectVector = indexer.getEffectiveAuxIndices();
// for (int i = 0; i < effectVector[s].size(); ++i) {
// printf("index: %d\n", effectVector[s][i]);
// std::fflush(stdout);
// }
// }
// for (int s = 0; s < _total_sub_num; ++s) {
//
// if (_subtmp_array[s].get_vert_num() > 1)
// {
// int idxAux = div_tp.get_aux_node_idx(s);
// int auxSize = indexer.getSubsSize()[idxAux];
// int auxNodesLen = indexer.getCombTable()[_color_num][auxSize];
// printf("Subs: %d, Aux count len: %d, effect len: %d\n", s, auxNodesLen, (indexer.getEffectiveAuxIndices())[s].size());
// std::fflush(stdout);
// }
// }
#ifdef VERBOSE
printf("Start initializaing datatable\n");
std::fflush(stdout);
#endif
_dTable.initDataTable(_subtmp_array, &indexer, _total_sub_num, _color_num, _vert_num, _thd_num, _useSPMM, _bufMatCols);
#ifdef VERBOSE
printf("Finish initializaing datatable\n");
std::fflush(stdout);
#endif
#ifdef VERBOSE
// peak memory usage on a single node
estimatePeakMemUsage();
// for flops of pruned color-coding
double totalFlops = estimateFlopsPGBSC();
double totalMemBand = estimateMemCommPGBSC();
printf("PGBSC Arith Intensity: %f\n", (totalFlops/totalMemBand));
std::fflush(stdout);
// for original color-coding algorithm
totalFlops = estimateFlopsFascia();
totalMemBand = estimateMemCommFascia();
printf("Fascia Arith Intensity: %f\n", (totalFlops/totalMemBand));
std::fflush(stdout);
// for original color-coding algorithm
totalFlops = estimateFlopsPrunedFascia();
totalMemBand = estimateMemCommPrunedFascia();
printf("Pruned Fascia Arith Intensity: %f\n", (totalFlops/totalMemBand));
std::fflush(stdout);
// for sparse input data distribution
degreeDistribution();
estimateTemplate();
#endif
// exit without counting
if (isEstimate)
return 0.0;
#ifdef VERBOSE
printf("Start counting\n");
std::fflush(stdout);
#endif
// allocating the bufVecLeaf buffer
_bufVecLeaf = (float**) malloc (_color_num*sizeof(float*));
if (_useSPMM == 0)
{
for (int i = 0; i < _color_num; ++i) {
#ifdef __INTEL_COMPILER
_bufVecLeaf[i] = (float*) _mm_malloc(_vert_num*sizeof(float), 64);
#else
_bufVecLeaf[i] = (float*) aligned_alloc(64, _vert_num*sizeof(float));
#endif
}
}
else
{
#ifdef __INTEL_COMPILER
_bufVecLeaf[0] = (float*) _mm_malloc((int64_t)(_vert_num)*_color_num*sizeof(float), 64);
#else
_bufVecLeaf[0] = (float*) aligned_alloc(64, (int64_t)(_vert_num)*_color_num*sizeof(float));
#endif
for (int i = 1; i < _color_num; ++i) {
_bufVecLeaf[i] = _bufVecLeaf[0] + i*_vert_num;
}
}
// start counting
double timeStart = utility::timer();
double iterCount = 0.0;
for (int i = 0; i < _itr_num; ++i) {
iterCount += colorCounting();
}
#ifdef VERBOSE
printf("Finish counting\n");
std::fflush(stdout);
#endif
double totalCountTime = (utility::timer() - timeStart);
printf("\nTime for count per iter: %9.6lf seconds\n", totalCountTime/_itr_num);
std::fflush(stdout);
#ifdef VERBOSE
printf("spmv ratio %f\% \n", 100*(_spmvTime/totalCountTime));
std::fflush(stdout);
printf("eMA ratio %f\% \n", 100*(_eMATime/totalCountTime));
std::fflush(stdout);
printf("Peak Mem Usage is : %9.6lf GB\n", _peakMemUsage);
std::fflush(stdout);
printf("SpMM time is : %f second; EMA time is: %f \n", _spmvElapsedTime/_itr_num, _fmaElapsedTime/_itr_num);
std::fflush(stdout);
printf("SpMM Memory bandwidth is : %f GBytes per second\n", (_spmvMemBytes*_itr_num)/_spmvElapsedTime);
std::fflush(stdout);
printf("FMA Memory bandwidth is : %f GBytes per second\n", (_fmaMemBytes*_itr_num)/_fmaElapsedTime);
std::fflush(stdout);
printf("Total Memory bandwidth is : %f GBytes per second\n",
((_spmvMemBytes+_fmaMemBytes)*_itr_num)/(_spmvElapsedTime + _fmaElapsedTime));
std::fflush(stdout);
printf("SpMM Throughput is : %f Gflops per second\n", (_spmvFlops*_itr_num)/_spmvElapsedTime);
std::fflush(stdout);
printf("FMA Throughput is : %f Gflops per second\n", (_fmaFlops*_itr_num)/_fmaElapsedTime);
std::fflush(stdout);
printf("Total Throughput is : %f Gflops per second\n",
((_spmvFlops+_fmaFlops)*_itr_num)/(_spmvElapsedTime+_fmaElapsedTime));
std::fflush(stdout);
#endif
// finish counting
double finalCount = iterCount/(double)_itr_num;
double probColorful = factorial(_color_num) /
( factorial(_color_num - _templates->get_vert_num())*pow(_color_num, _templates->get_vert_num()));
printf("Final raw count is %e\n", finalCount);
std::fflush(stdout);
printf("Prob is %f\n", probColorful);
std::fflush(stdout);
// int automoNum = 1;
int automoNum = _calculate_automorphisms ? automorphismNum() : 1;
finalCount = floor(finalCount/(probColorful*(double)automoNum) + 0.5);
printf("Final count is %e\n", finalCount);
std::fflush(stdout);
return finalCount;
}/*}}}*/
double CountMat::colorCounting()
{/*{{{*/
colorInit();
double countTotal = 0.0;
// reset scaling flag
_isScaled = 0;
for (int s = _total_sub_num - 1; s >= 0; --s) {
int subSize = _subtmp_array[s].get_vert_num();
int mainIdx = div_tp.get_main_node_idx(s);
int auxIdx = div_tp.get_aux_node_idx(s);
_dTable.initSubTempTable(s, mainIdx, auxIdx);
int* idxCombToCount = (indexer.getSubCToCount())[s];
if (subSize == 1) {
_dTable.countCurBottom(idxCombToCount, _colors_local);
}
else
{
//non-bottom case
if (_isPruned == 1)
{
if (_useSPMM == 1)
{
countTotal = countNonBottomePrunedSPMM(s);
}
else
countTotal = countNonBottomePruned(s);
}
else
countTotal = countNonBottomeOriginal(s);
}
// trace the peak mem usage
double compute_mem = 0.0;
process_mem_usage(compute_mem);
compute_mem = compute_mem /(1024*1024);
std::printf("Mem utilization compute step sub %d: %9.6lf GB, count val: %e\n", s, compute_mem, countTotal);
std::fflush(stdout);
_peakMemUsage = (compute_mem > _peakMemUsage) ? compute_mem : _peakMemUsage;
if (mainIdx != DUMMY_VAL)
_dTable.cleanSubTempTable(mainIdx, false);
if (auxIdx != DUMMY_VAL)
_dTable.cleanSubTempTable(auxIdx, false);
}
return countTotal;
}/*}}}*/
//reduce the num of SpMV
double CountMat::countNonBottomePruned(int subsId)
{/*{{{*/
if (subsId == _vtuneStart)
{
// for vtune miami u15-2 subids==3 takes 103 secs
#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
}
int subSize = _subtmp_array[subsId].get_vert_num();
int idxMain = div_tp.get_main_node_idx(subsId);
int idxAux = div_tp.get_aux_node_idx(subsId);
int mainSize = indexer.getSubsSize()[idxMain];
int auxSize = indexer.getSubsSize()[idxAux];
int countCombNum = indexer.getCombTable()[_color_num][subSize];
int splitCombNum = indexer.getCombTable()[subSize][mainSize];
int auxTableLen = indexer.getCombTable()[_color_num][auxSize];
#ifdef VERBOSE
printf("Finish init sub templte %d, vert: %d, comb: %d, splitNum: %d, spmv times: %d, isScaled: %d\n", subsId, subSize,
countCombNum, splitCombNum, auxTableLen, _isScaled);
std::fflush(stdout);
#endif
double countSum = 0.0;
double subSum = 0.0;
int** mainSplitLocal = (indexer.getSplitToCountTable())[0][subsId];
int** auxSplitLocal = (indexer.getSplitToCountTable())[1][subsId];
int* combToCountLocal = (indexer.getCombToCountTable())[subsId];
double* bufLastSub = nullptr;
float* objArray = nullptr;
if (subsId == 0)
{
#ifdef __INTEL_COMPILER
bufLastSub = (double*) _mm_malloc(_vert_num*sizeof(double), 64);
#else
bufLastSub = (double*) aligned_alloc(64, _vert_num*sizeof(double));
#endif
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < _vert_num; ++i) {
bufLastSub[i] = 0;
}
}
#ifdef VERBOSE
double startTime = utility::timer();
double startTimeComp = 0.0;
double eltSpmv = 0.0;
double eltMul = 0.0;
#endif
double spmvStart = 0.0;
double fmaStart = 0.0;
// first the precompute of SpMV results and have a in-place storage
#ifdef VERBOSE
startTimeComp = utility::timer();
#endif
if (_graph != nullptr && _graph->useMKL())
{
// setup hint for mkl spmv
_graph->SpMVMKLHint(auxTableLen);
}
for (int i = 0; i < auxTableLen; ++i) {
float* auxObjArray = _dTable.getAuxArray(i);
#ifdef VERBOSE
spmvStart = utility::timer();
#endif
if (_graph != nullptr)
{
if (_graph->useMKL())
{
//set up hint and optimize
_graph->SpMVMKL(auxObjArray, _bufVec, _thd_num);
}
else
{
_graph->SpMVNaiveFull(auxObjArray, _bufVec, _thd_num);
}
}
else
{
// use CSC-Split SpMV
// clear _bufVec
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int j = 0; j < _vert_num; ++j) {
_bufVec[j] = 0.0;
}
_graphCSC->spmvNaiveSplit(auxObjArray, _bufVec, _thd_num);
}
#ifdef VERBOSE
_spmvElapsedTime += (utility::timer() - spmvStart);
#endif
// check the size of auxArray
if (auxSize > 1)
std::memcpy(auxObjArray, _bufVec, _vert_num*sizeof(float));
else
std::memcpy(_bufVecLeaf[i], _bufVec, _vert_num*sizeof(float));
}
#ifdef VERBOSE
eltSpmv += (utility::timer() - startTimeComp);
#endif
#ifdef VERBOSE
startTimeComp = utility::timer();
#endif
// a second part only involves element-wise multiplication and updating
for(int i=0; i<countCombNum; i++)
{
int combIdx = combToCountLocal[i];
// #ifdef VERBOSE
// printf("Sub: %d, comb: %d, combIdx: %d\n", subsId, i, combIdx);
// std::fflush(stdout);
// #endif
if (subsId > 0)
objArray = _dTable.getCurTableArray(combIdx);
for (int j = 0; j < splitCombNum; ++j) {
int mainIdx = mainSplitLocal[i][j];
int auxIdx = auxSplitLocal[i][j];
// #ifdef VERBOSE
// printf("Sub: %d, mainIdx: %d, auxIdx: %d\n", subsId, mainIdx, auxIdx);
// std::fflush(stdout);
// #endif
// already pre-computed by SpMV
float* auxArraySelect = nullptr;
if (auxSize > 1)
auxArraySelect = _dTable.getAuxArray(auxIdx);
else
auxArraySelect = _bufVecLeaf[auxIdx];
// element-wise mul
float* mainArraySelect = _dTable.getMainArray(mainIdx);
#ifdef VERBOSE
fmaStart = utility::timer();
#endif
if (subsId > 0)
{
if (_isScaled == 0)
_dTable.arrayWiseFMAScale(objArray, auxArraySelect, mainArraySelect, 1.0e-12);
else
{
_dTable.arrayWiseFMAAVX(objArray, auxArraySelect, mainArraySelect);
// _dTable.arrayWiseFMA(objArray, auxArraySelect, mainArraySelect);
}
}
else
{
// the last scale use
_dTable.arrayWiseFMALast(bufLastSub, auxArraySelect, mainArraySelect);
}
#ifdef VERBOSE
_fmaElapsedTime += (utility::timer() - fmaStart);
#endif
}
// if (subsId > 0)
// subSum += sumVec(objArray, _vert_num);
}
#ifdef VERBOSE
eltMul += (utility::timer() - startTimeComp);
#endif
_isScaled = 1;
if (subsId == 0)
{
// sum the vals from bufLastSub
for (int k = 0; k < _vert_num; ++k) {
countSum += bufLastSub[k];
}
// to recover the scale down process
if (_isScaled == 1)
countSum *= 1.0e+12;
#ifdef __INTEL_COMPILER
_mm_free(bufLastSub);
#else
free(bufLastSub);
#endif
}
#ifdef VERBOSE
double subsTime = (utility::timer() - startTime);
printf("Sub %d, counting time %f, Spmv time %f: ratio: %f\%, Mul time %f: ratio: %f\% \n", subsId, subsTime, eltSpmv, 100*(eltSpmv/subsTime), eltMul, 100*(eltMul/subsTime));
_spmvTime += eltSpmv;
_eMATime += eltMul;
// printf("Sub %d, counting val %e\n", subsId, subSum);
// std::fflush(stdout);
#endif
// #ifdef VERBOSE
// printf("Sub %d, NonBottom raw count %e\n", subsId, countSum);
// std::fflush(stdout);
// #endif
return countSum;
}/*}}}*/
double CountMat::countNonBottomePrunedSPMM(int subsId)
{/*{{{*/
if (subsId == _vtuneStart)
{
// for vtune miami u15-2 subids==3 takes 103 secs
#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
}
int subSize = _subtmp_array[subsId].get_vert_num();
int idxMain = div_tp.get_main_node_idx(subsId);
int idxAux = div_tp.get_aux_node_idx(subsId);
int mainSize = indexer.getSubsSize()[idxMain];
int auxSize = indexer.getSubsSize()[idxAux];
int countCombNum = indexer.getCombTable()[_color_num][subSize];
int splitCombNum = indexer.getCombTable()[subSize][mainSize];
int auxTableLen = indexer.getCombTable()[_color_num][auxSize];
#ifdef VERBOSE
printf("Finish init sub templte %d, vert: %d, comb: %d, splitNum: %d, isScaled: %d\n", subsId, subSize,
countCombNum, splitCombNum, _isScaled);
std::fflush(stdout);
#endif
double countSum = 0.0;
double subSum = 0.0;
int** mainSplitLocal = (indexer.getSplitToCountTable())[0][subsId];
int** auxSplitLocal = (indexer.getSplitToCountTable())[1][subsId];
int* combToCountLocal = (indexer.getCombToCountTable())[subsId];
double* bufLastSub = nullptr;
float* objArray = nullptr;
if (subsId == 0)
{
#ifdef __INTEL_COMPILER
bufLastSub = (double*) _mm_malloc(_vert_num*sizeof(double), 64);
#else
bufLastSub = (double*) aligned_alloc(64, _vert_num*sizeof(double));
#endif
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < _vert_num; ++i) {
bufLastSub[i] = 0;
}
}
#ifdef VERBOSE
double startTime = utility::timer();
double startTimeComp = 0.0;
double eltSpmv = 0.0;
double eltMul = 0.0;
#endif
double spmvStart = 0.0;
double fmaStart = 0.0;
// first the precompute of SPMM results and have a in-place storage
#ifdef VERBOSE
startTimeComp = utility::timer();
#endif
// ---- start of SpMM impl -------
if (_graph != nullptr)
{
#ifndef NEC
// CSR MKL SpMM implementation
int batchNum = (auxTableLen + _bufMatCols - 1)/(_bufMatCols);
int colStart = 0;
char transa = 'n';
MKL_INT m = _vert_num;
MKL_INT n = 0;
MKL_INT k = _vert_num;
float alpha = 1.0;
float beta = 0.0;
char matdescra[5];
matdescra[0] = 'g';
matdescra[3] = 'f'; /*one-based indexing is used*/
float* csrVals = _graph->getNNZVal();
int* csrRowIdx = _graph->getIndexRow();
int* csrColIdx = _graph->getIndexCol();
mkl_set_num_threads(_thd_num);
for (int i = 0; i < batchNum; ++i)
{
int batchSize = (i < batchNum -1) ? (_bufMatCols) : (auxTableLen - _bufMatCols*(batchNum-1));
n = batchSize;
#ifdef VERBOSE
spmvStart = utility::timer();
#endif
// invoke the mkl scsrmm kernel
mkl_scsrmm(&transa, &m, &n, &k, &alpha, matdescra, csrVals, csrColIdx, csrRowIdx, &(csrRowIdx[1]), _dTable.getAuxArray(colStart), &k, &beta, _bufMatY, &k);
#ifdef VERBOSE
_spmvElapsedTime += (utility::timer() - spmvStart);
#endif
// copy columns from _bufMatY
if (auxSize > 1)
{
std::memcpy(_dTable.getAuxArray(colStart), _bufMatY, _vert_num*batchSize*sizeof(float));
}
else
{
std::memcpy(_bufVecLeaf[colStart], _bufMatY, _vert_num*batchSize*sizeof(float));
}
// increase colStart;
colStart += batchSize;
}
#endif
}
else
{
// CSC-Split SpMM impl
int batchNum = (auxTableLen + _bufMatCols - 1)/(_bufMatCols);
int colStart = 0;
for (int i = 0; i < batchNum; ++i)
{
int batchSize = (i < batchNum -1) ? (_bufMatCols) : (auxTableLen - _bufMatCols*(batchNum-1));
valType* xInput = _dTable.getAuxArray(colStart);
// convert data structre from column-majored to row-majored
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int j = 0; j <_vert_num; ++j)
{
for (int k = 0; k < batchSize; ++k) {
_bufMatX[j*batchSize+k] = xInput[k*_vert_num+j];
}
}
// clean yOuput
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int j = 0; j < _vert_num*batchSize; ++j) {
_bufMatY[j] = 0.0;
}
#ifdef VERBOSE
spmvStart = utility::timer();
#endif
// invoke the spmm kernel
_graphCSC->spmmSplit(_bufMatX, _bufMatY, batchSize, _thd_num);
#ifdef VERBOSE
_spmvElapsedTime += (utility::timer() - spmvStart);
#endif
// convert data structure back to column-majored
valType* yOutput = (auxSize > 1) ? _dTable.getAuxArray(colStart) : _bufVecLeaf[colStart];
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int j = 0; j < _vert_num; ++j) {
for (int k = 0; k < batchSize; ++k) {
yOutput[k*_vert_num+j] = _bufMatY[j*batchSize+k];
}
}
// increase colStart;
colStart += batchSize;
}
}
#ifdef VERBOSE
eltSpmv += (utility::timer() - startTimeComp);
#endif
#ifdef VERBOSE
startTimeComp = utility::timer();
#endif
// a second part only involves element-wise multiplication and updating
for(int i=0; i<countCombNum; i++)
{
int combIdx = combToCountLocal[i];
// #ifdef VERBOSE
// printf("Sub: %d, comb: %d, combIdx: %d\n", subsId, i, combIdx);
// std::fflush(stdout);
// #endif
if (subsId > 0)
objArray = _dTable.getCurTableArray(combIdx);
for (int j = 0; j < splitCombNum; ++j) {
int mainIdx = mainSplitLocal[i][j];
int auxIdx = auxSplitLocal[i][j];
// #ifdef VERBOSE
// printf("Sub: %d, mainIdx: %d, auxIdx: %d\n", subsId, mainIdx, auxIdx);
// std::fflush(stdout);
// #endif
// already pre-computed by SpMV
float* auxArraySelect = nullptr;
if (auxSize > 1)
auxArraySelect = _dTable.getAuxArray(auxIdx);
else
auxArraySelect = _bufVecLeaf[auxIdx];
// element-wise mul
float* mainArraySelect = _dTable.getMainArray(mainIdx);
#ifdef VERBOSE
fmaStart = utility::timer();
#endif
if (subsId > 0)
{
if (_isScaled == 0)
_dTable.arrayWiseFMAScale(objArray, auxArraySelect, mainArraySelect, 1.0e-12);
else
{
_dTable.arrayWiseFMAAVX(objArray, auxArraySelect, mainArraySelect);
// _dTable.arrayWiseFMA(objArray, auxArraySelect, mainArraySelect);
}
}
else
{
// the last scale use
_dTable.arrayWiseFMALast(bufLastSub, auxArraySelect, mainArraySelect);
}
#ifdef VERBOSE
_fmaElapsedTime += (utility::timer() - fmaStart);
#endif
}
// if (subsId > 0)
// subSum += sumVec(objArray, _vert_num);
}
#ifdef VERBOSE
eltMul += (utility::timer() - startTimeComp);
#endif
_isScaled = 1;
if (subsId == 0)
{
// sum the vals from bufLastSub
for (int k = 0; k < _vert_num; ++k) {
countSum += bufLastSub[k];
}
// to recover the scale down process
if (_isScaled == 1)
countSum *= 1.0e+12;
#ifdef __INTEL_COMPILER
_mm_free(bufLastSub);
#else
free(bufLastSub);
#endif
}
#ifdef VERBOSE
double subsTime = (utility::timer() - startTime);
printf("Sub %d, counting time %f, Spmv time %f: ratio: %f\%, Mul time %f: ratio: %f\% \n", subsId, subsTime, eltSpmv, 100*(eltSpmv/subsTime), eltMul, 100*(eltMul/subsTime));
_spmvTime += eltSpmv;
_eMATime += eltMul;
// printf("Sub %d, counting val %e\n", subsId, subSum);
// std::fflush(stdout);
#endif
// #ifdef VERBOSE
// printf("Sub %d, NonBottom raw count %e\n", subsId, countSum);
// std::fflush(stdout);
// #endif
return countSum;
}/*}}}*/
double CountMat::countNonBottomeOriginal(int subsId)
{/*{{{*/
if (subsId == _vtuneStart)
{
// for vtune
#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
}
int subSize = _subtmp_array[subsId].get_vert_num();
int idxMain = div_tp.get_main_node_idx(subsId);
int mainSize = indexer.getSubsSize()[idxMain];
int countCombNum = indexer.getCombTable()[_color_num][subSize];
int splitCombNum = indexer.getCombTable()[subSize][mainSize];
#ifdef VERBOSE
printf("Finish init sub templte %d, vert: %d, comb: %d, splitNum: %d\n", subsId, subSize,
countCombNum, splitCombNum);
std::fflush(stdout);
#endif
double countSum = 0.0;
int** mainSplitLocal = (indexer.getSplitToCountTable())[0][subsId];
int** auxSplitLocal = (indexer.getSplitToCountTable())[1][subsId];
int* combToCountLocal = (indexer.getCombToCountTable())[subsId];
float* bufLastSub = nullptr;
float* objArray = nullptr;
if (subsId == 0)
{
#ifdef __INTEL_COMPILER
bufLastSub = (float*) _mm_malloc(_vert_num*sizeof(float), 64);
#else
bufLastSub = (float*) aligned_alloc(64, _vert_num*sizeof(float));
#endif
#pragma omp parallel for num_threads(omp_get_max_threads())
for (int i = 0; i < _vert_num; ++i) {
bufLastSub[i] = 0;
}
// std::memset(bufLastSub, 0, _vert_num*sizeof(float));
}
#ifdef VERBOSE
double startTime = utility::timer();
double startTimeComp = 0.0;
double eltSpmv = 0.0;
double eltMul = 0.0;
#endif
for(int i=0; i<countCombNum; i++)
{
int combIdx = combToCountLocal[i];
if (subsId == 0)
objArray = bufLastSub;
else
objArray = _dTable.getCurTableArray(combIdx);
for (int j = 0; j < splitCombNum; ++j) {
int mainIdx = mainSplitLocal[i][j];
int auxIdx = auxSplitLocal[i][j];
float* auxArraySelect = _dTable.getAuxArray(auxIdx);
// spmv
#ifdef VERBOSE
startTimeComp = utility::timer();
#endif
if (_graph != nullptr)
_graph->SpMVNaiveFull(auxArraySelect, _bufVec, _thd_num);
else
_graphCSC->spmvNaiveSplit(auxArraySelect, _bufVec, _thd_num);
#ifdef VERBOSE
eltSpmv += (utility::timer() - startTimeComp);
#endif
// element-wise mul
float* mainArraySelect = _dTable.getMainArray(mainIdx);
#ifdef VERBOSE
startTimeComp = utility::timer();
#endif
_dTable.arrayWiseFMANaive(objArray, _bufVec, mainArraySelect);
#ifdef VERBOSE
eltMul += (utility::timer() - startTimeComp);
#endif
}
}
if (subsId == 0)
{
// sum the vals from bufLastSub
for (int k = 0; k < _vert_num; ++k) {
countSum += bufLastSub[k];
}
#ifdef __INTEL_COMPILER
_mm_free(bufLastSub);
#else
free(bufLastSub);
#endif
}
#ifdef VERBOSE
printf("Sub %d, counting time %f, Spmv time %f, Mul time %f \n", subsId, (utility::timer() - startTime), eltSpmv, eltMul);
// printf("Sub %d, counting time %f\n", subsId, (utility::timer() - startTime));
std::fflush(stdout);
#endif
#ifdef VERBOSE
printf("Sub %d, NonBottom raw count %f\n", subsId, countSum);
std::fflush(stdout);
#endif
return countSum;
}/*}}}*/
void CountMat::colorInit()
{
#pragma omp parallel
{
// each thread has a seed for ranodm number generation
srand(time(0)+ omp_get_thread_num());
#pragma omp for
for (int i = 0; i < _vert_num; ++i) {
_colors_local[i] = (rand()%_color_num);
}
}
}
int CountMat::factorial(int n)
{
if (n <= 1)
return 1;
else
return (n*factorial(n-1));
}