-
Notifications
You must be signed in to change notification settings - Fork 0
/
whilte_list_path_impl.cc
executable file
·5413 lines (4269 loc) · 267 KB
/
whilte_list_path_impl.cc
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 "default_auto_tuner.hpp"
using namespace std;
void reset_exe_node_param_and_param_strategy_of_sub_graph(exe_compressed_sub_graph_t* sub_graph, param_strategy_of_sub_graph_t* sub_graph_param_strategy)
{
// cout << "reset_exe_node_param_and_param_strategy_of_sub_graph" << endl;
assert(sub_graph != NULL && sub_graph_param_strategy != NULL);
assert(sub_graph->exe_node_vec.size() == sub_graph_param_strategy->param_strategy_vec.size());
assert(sub_graph->exe_node_vec.size() > 0 && sub_graph_param_strategy->param_strategy_vec.size() > 0);
// 检查输入
for (unsigned long i = 0; i < sub_graph->exe_node_vec.size(); i++)
{
assert(sub_graph->exe_node_vec[i].param != NULL);
assert(sub_graph_param_strategy->param_strategy_vec[i].param == sub_graph->exe_node_vec[i].param);
assert(sub_graph_param_strategy->param_strategy_vec[i].param_strategy != NULL);
}
// 执行exe node的reset
reset_param_of_all_sub_compressed_graph(sub_graph);
// 重新连接参数策略和参数
bind_exe_node_param_param_strategy_of_sub_graph(sub_graph, sub_graph_param_strategy);
}
void reset_exe_node_param_and_param_strategy_of_sub_graph(exe_dense_sub_graph_t* sub_graph, param_strategy_of_sub_graph_t* sub_graph_param_strategy)
{
// cout << "reset_exe_node_param_and_param_strategy_of_sub_graph" << endl;
assert(sub_graph != NULL && sub_graph_param_strategy != NULL);
assert(sub_graph->exe_node_vec.size() == sub_graph_param_strategy->param_strategy_vec.size());
assert(sub_graph->exe_node_vec.size() > 0 && sub_graph_param_strategy->param_strategy_vec.size() > 0);
// 检查输入
for (unsigned long i = 0; i < sub_graph->exe_node_vec.size(); i++)
{
assert(sub_graph->exe_node_vec[i].param != NULL);
assert(sub_graph_param_strategy->param_strategy_vec[i].param == sub_graph->exe_node_vec[i].param);
assert(sub_graph_param_strategy->param_strategy_vec[i].param_strategy != NULL);
}
// 执行exe node的reset
reset_param_of_all_sub_dense_graph(sub_graph);
// 重新连接参数策略和参数
bind_exe_node_param_param_strategy_of_sub_graph(sub_graph, sub_graph_param_strategy);
}
void malloc_exe_node_param_and_param_strategy_of_sub_graph(exe_compressed_sub_graph_t* sub_graph, param_strategy_of_sub_graph_t* sub_graph_param_strategy)
{
assert(sub_graph != NULL && sub_graph_param_strategy != NULL);
assert(sub_graph->exe_node_vec.size() == sub_graph->preorder_node_set.size() && sub_graph->preorder_node_set.size() == sub_graph_param_strategy->param_strategy_vec.size());
// 检查输入
for (unsigned long i = 0; i < sub_graph->exe_node_vec.size(); i++)
{
assert(sub_graph->exe_node_vec[i].param != NULL);
assert(sub_graph_param_strategy->param_strategy_vec[i].param == sub_graph->exe_node_vec[i].param);
assert(sub_graph_param_strategy->param_strategy_vec[i].param_strategy != NULL);
}
// 为所有的节点重新申请新的参数,但是不析构已有的参数
malloc_param_of_all_sub_compressed_graph(sub_graph);
bind_exe_node_param_param_strategy_of_sub_graph(sub_graph, sub_graph_param_strategy);
}
void bind_exe_node_param_param_strategy_of_sub_graph(exe_compressed_sub_graph_t* sub_graph, param_strategy_of_sub_graph_t* sub_graph_param_strategy)
{
assert(sub_graph != NULL && sub_graph_param_strategy != NULL);
assert(sub_graph->exe_node_vec.size() == sub_graph_param_strategy->param_strategy_vec.size());
assert(sub_graph->exe_node_vec.size() > 0 && sub_graph->preorder_node_set.size() > 0);
// 重新绑定参数策略和参数
for (unsigned long i = 0; i < sub_graph->exe_node_vec.size(); i++)
{
// 检查类型是不是正确
assert(sub_graph_param_strategy->param_strategy_vec[i].node_type == sub_graph->exe_node_vec[i].type);
assert(sub_graph_param_strategy->param_strategy_vec[i].param_strategy != NULL);
sub_graph_param_strategy->param_strategy_vec[i].param = sub_graph->exe_node_vec[i].param;
}
}
void bind_exe_node_param_param_strategy_of_sub_graph(exe_dense_sub_graph_t* sub_graph, param_strategy_of_sub_graph_t* sub_graph_param_strategy)
{
assert(sub_graph != NULL && sub_graph_param_strategy != NULL);
assert(sub_graph->exe_node_vec.size() == sub_graph_param_strategy->param_strategy_vec.size());
assert(sub_graph->exe_node_vec.size() > 0 && sub_graph->preorder_node_set.size() > 0);
// 重新绑定参数策略和参数
for (unsigned long i = 0; i < sub_graph->exe_node_vec.size(); i++)
{
assert(sub_graph_param_strategy->param_strategy_vec[i].node_type == sub_graph->exe_node_vec[i].type);
assert(sub_graph_param_strategy->param_strategy_vec[i].param_strategy != NULL);
sub_graph_param_strategy->param_strategy_vec[i].param = sub_graph->exe_node_vec[i].param;
}
}
void del_param_of_total_exe_graph_and_strategy_graph_safely(dense_view_matrix_and_compressed_sub_block_exe_graph_and_template_t* total_graph)
{
assert(total_graph != NULL);
// 首先析构稠密视图的路径
del_exe_node_param_of_dense_view_matrix(&(total_graph->dense_sub_graph_and_param_strategy.dense_sub_graph));
// 然后析构稠密视图的策略路径
del_strategy_of_param_strategy_node_in_sub_matrix(&(total_graph->dense_sub_graph_and_param_strategy.dense_sub_graph_param_strategy));
// 析构玩之后都变成空的
for (auto sub_graph_item : total_graph->dense_sub_graph_and_param_strategy.dense_sub_graph.exe_node_vec)
{
assert(sub_graph_item.param == NULL);
}
for (auto strategy_item : total_graph->dense_sub_graph_and_param_strategy.dense_sub_graph_param_strategy.param_strategy_vec)
{
assert(strategy_item.param_strategy == NULL);
}
// 遍历所有的子图
for (unsigned long i = 0; i < total_graph->compressed_sub_block_exe_graph_and_template_vec.size(); i++)
{
del_exe_node_param_of_compress_sub_matrix(&(total_graph->compressed_sub_block_exe_graph_and_template_vec[i].sub_graph));
del_strategy_of_param_strategy_node_in_sub_matrix(&(total_graph->compressed_sub_block_exe_graph_and_template_vec[i].sub_graph_param_strategy));
// 析构玩之后参数都变成NULL
for (auto sub_graph_item : total_graph->compressed_sub_block_exe_graph_and_template_vec[i].sub_graph.exe_node_vec)
{
assert(sub_graph_item.param == NULL);
}
for (auto strategy_item : total_graph->compressed_sub_block_exe_graph_and_template_vec[i].sub_graph_param_strategy.param_strategy_vec)
{
assert(strategy_item.param_strategy == NULL);
}
if (total_graph->compressed_sub_block_exe_graph_and_template_vec[i].temp_node.template_param != NULL)
{
del_param_of_template_node(&(total_graph->compressed_sub_block_exe_graph_and_template_vec[i].temp_node));
assert(total_graph->compressed_sub_block_exe_graph_and_template_vec[i].temp_node.template_param == NULL);
}
}
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy1(exe_dense_sub_graph_t dense_graph, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
// 一上来先执行对应的密集子图
sparse_struct_t* matrix = get_matrix_dense_view_graph(&dense_graph);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
// 获得最优的子图优化路径
compressed_sub_block_exe_graph_and_template_t best_sub_graph_path = find_best_path_of_white_list_strategy1(matrix, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
// 析构当前的矩阵
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
return best_sub_graph_path;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy1(dense_view_matrix_exe_graph_and_param_strategy_t dense_graph, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
sparse_struct_t* matrix = execute_dense_matrix_exe_graph_with_param_strategy(&(dense_graph.dense_sub_graph), &(dense_graph.dense_sub_graph_param_strategy));
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
// 获得最优的子图优化路径
compressed_sub_block_exe_graph_and_template_t best_sub_graph_path = find_best_path_of_white_list_strategy1(matrix, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
// 析构当前的矩阵
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
return best_sub_graph_path;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy1(sparse_struct_t* input_matrix, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
// 检查从外部传入的矩阵
assert(input_matrix != NULL && input_matrix->block_coor_table.item_arr.size() > sub_matrix_id && input_matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && input_matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
assert(input_matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 2);
// 执行拷贝
sparse_struct_t* matrix = val_copy_from_old_matrix_struct(input_matrix);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
assert(matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 2);
compressed_block_t* compressed_block_ptr = matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr;
// 找出最优的子图、最优的参数策略、最优模板类型及其参数
template_node_t best_temp_node;
exe_compressed_sub_graph_t best_sub_graph;
param_strategy_of_sub_graph_t best_sub_graph_param_strategy;
best_time = 99999999999999;
best_gflops = 0;
// 定一个优化路径骨架
exe_compressed_sub_graph_t sub_graph_skeleton;
// 定一个参数设定的骨架
param_strategy_of_sub_graph_t param_strategy_skeleton;
// 定义骨架,首先是一个ROW_padding和BLB_row
compressed_row_padding_direct_param_strategy_t row_padding_param_strategy;
compressed_tblock_level_row_div_evenly_param_strategy_t BLB_row_div_evenly_param_strategy;
add_a_exe_node_and_param_strategy_to_exe_compressed_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton, COMPRESSED_ROW_PADDING, COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY, &row_padding_param_strategy);
add_a_exe_node_and_param_strategy_to_exe_compressed_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton, COMPRESSED_TBLOCK_LEVEL_ROW_DIV, COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY, &BLB_row_div_evenly_param_strategy);
// 增加对于参数的调节
param_enumerater_t param_setter;
assert(sub_graph_skeleton.exe_node_vec.size() == 2);
// 对应参数的指针
compressed_row_padding_direct_param_strategy_t* row_padding_direct_param_strategy = (compressed_row_padding_direct_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[0].param_strategy;
compressed_tblock_level_row_div_evenly_param_strategy_t* tblock_level_row_div_evenly_param_strategy = (compressed_tblock_level_row_div_evenly_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[1].param_strategy;
assert(row_padding_direct_param_strategy != NULL && tblock_level_row_div_evenly_param_strategy != NULL);
// 对于BLB row padding的长度
register_integer_independ_param_to_enumerater(¶m_setter, &(row_padding_direct_param_strategy->multiply), 0, 256, 64);
row_padding_direct_param_strategy->padding_row_length = 1;
// cout << "row_padding_direct_param_strategy->padding_row_length:" << ((compressed_row_padding_direct_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[0].param)->padding_row_length << endl;
// BLB行分块的宽度依赖于rowpadding的大小,在实际处理的时候要执行赋值
// 添加一行一个TLB的切分
compressed_thread_level_row_div_none_param_strategy_t thread_row_param_strategy;
add_a_exe_node_and_param_strategy_to_exe_compressed_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton, COMPRESSED_THREAD_LEVEL_ROW_DIV, COMPRESSED_THREAD_LEVEL_ROW_DIV_NONE_PARAM_STRATEGY, &thread_row_param_strategy);
assert(sub_graph_skeleton.exe_node_vec.size() == 3 && param_strategy_skeleton.param_strategy_vec.size() == 3);
// 是不是要停止搜索
bool search_finished_by_strategy = false;
// 枚举参数,分别得出不同的矩阵
while (set_param_combination_to_next(¶m_setter) == false)
{
if (row_padding_direct_param_strategy->multiply == 0)
{
continue;
}
// 再定义一些参数
tblock_level_row_div_evenly_param_strategy->block_row_num = row_padding_direct_param_strategy->multiply;
// 如果没有稠密视图产生的矩阵
if (matrix == NULL)
{
// 通过稠密视图的块得出需要的子块
matrix = val_copy_from_old_matrix_struct(input_matrix);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
compressed_block_ptr = matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr;
}
// 根据BLB窗口的大小获得padding率
// 获取子块行非零元数量
vector<unsigned long> row_nnz_of_compressed_block = get_nnz_of_each_row_in_compressed_sub_matrix(matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr);
// 打印现在一些参数
cout << "row_padding_direct_param_strategy->multiply:" << row_padding_direct_param_strategy->multiply << ",row_padding_direct_param_strategy->padding_row_length:" << row_padding_direct_param_strategy->padding_row_length << endl;
// padding之前的非零元数量
unsigned long old_nnz = compressed_block_ptr->read_index[0]->length;
unsigned long new_nnz = 0;
// 遍历每一个BLB行条带,计算每个条带的累计的新的nnz数量,不考虑WLB的padding
for (unsigned long i = 0; i < row_nnz_of_compressed_block.size() / tblock_level_row_div_evenly_param_strategy->block_row_num; i++)
{
// 找出行条带的最大值
unsigned long largest_row_nnz = 0;
for (unsigned long global_row_id = tblock_level_row_div_evenly_param_strategy->block_row_num * i; global_row_id < tblock_level_row_div_evenly_param_strategy->block_row_num * (i + 1); global_row_id++)
{
assert(global_row_id < row_nnz_of_compressed_block.size());
if (largest_row_nnz < row_nnz_of_compressed_block[global_row_id])
{
largest_row_nnz = row_nnz_of_compressed_block[global_row_id];
}
}
new_nnz = new_nnz + largest_row_nnz * tblock_level_row_div_evenly_param_strategy->block_row_num;
}
// 看看有没有剩下的部分
if (row_nnz_of_compressed_block.size() % tblock_level_row_div_evenly_param_strategy->block_row_num != 0)
{
unsigned long remain_row_num = row_nnz_of_compressed_block.size() % tblock_level_row_div_evenly_param_strategy->block_row_num;
// 行的最大长度
unsigned long largest_row_nnz = 0;
// 找出行条带的最大值
for (unsigned long global_row_id = row_nnz_of_compressed_block.size() - remain_row_num; global_row_id < row_nnz_of_compressed_block.size(); global_row_id++)
{
assert(global_row_id < row_nnz_of_compressed_block.size());
if (largest_row_nnz < row_nnz_of_compressed_block[global_row_id])
{
largest_row_nnz = row_nnz_of_compressed_block[global_row_id];
}
}
// 计算新条带的nnz数量,但是因为经过了padding,所以最后一块的实际行号是BLB行条带的宽度
new_nnz = new_nnz + largest_row_nnz * tblock_level_row_div_evenly_param_strategy->block_row_num;
}
assert(new_nnz >= old_nnz);
cout << "new_nnz:" << new_nnz << ",old_nnz:" << old_nnz << ",padding rate:" << (float)new_nnz/(float)old_nnz << endl;
if ((float)new_nnz/(float)old_nnz > get_config()["PADDING_RATE_UP_BOUND"].as_integer())
{
cout << "padding rate is larger than " << get_config()["PADDING_RATE_UP_BOUND"].as_integer() << endl;
// 析构matrix
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
// 析构完之后置0
continue;
}
if (data_set_collector != NULL)
{
// 按照当前的逻辑,稠密视图已经加入了对应的数据
assert(data_set_collector->accu_dense_graph_node_type_vec.size() == data_set_collector->accu_dense_param_strategy_type_vec.size());
assert(data_set_collector->accu_dense_graph_node_type_vec.size() > 0);
vector<exe_node_type> compressed_node_type_vec;
vector<exe_node_param_set_strategy> compressed_param_strategy_vec;
vector<float> compressed_param_vec;
// 检查一下插入的数据的数量是不是满足要求,类型是不是满足要求
assert(sub_graph_skeleton.exe_node_vec.size() == 3);
// 分别处理三个节点,第一个节点直接row padding,第二个节点行分块,第三个节点一行一个线程
// 第一个节点的类型,参数策略的类型,以及策略的参数
// COMPRESSED_ROW_PADDING, COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY, compressed_row_padding_direct_param_strategy_t
assert(sub_graph_skeleton.exe_node_vec[0].type == COMPRESSED_ROW_PADDING);
assert(param_strategy_skeleton.param_strategy_vec[0].strategy_type == COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY);
compressed_node_type_vec.push_back(COMPRESSED_ROW_PADDING);
compressed_param_strategy_vec.push_back(COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY);
compressed_row_padding_direct_param_strategy_t* strategy1_ptr = (compressed_row_padding_direct_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[0].param_strategy;
assert(strategy1_ptr != NULL);
compressed_param_vec.push_back(strategy1_ptr->multiply);
compressed_param_vec.push_back(strategy1_ptr->padding_row_length);
// 第二个节点的类型,参数策略的类型,以及策略的参数
// COMPRESSED_TBLOCK_LEVEL_ROW_DIV, COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY, compressed_tblock_level_row_div_evenly_param_strategy_t
assert(sub_graph_skeleton.exe_node_vec[1].type == COMPRESSED_TBLOCK_LEVEL_ROW_DIV);
assert(param_strategy_skeleton.param_strategy_vec[1].strategy_type == COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY);
compressed_node_type_vec.push_back(COMPRESSED_TBLOCK_LEVEL_ROW_DIV);
compressed_param_strategy_vec.push_back(COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY);
compressed_tblock_level_row_div_evenly_param_strategy_t* strategy2_ptr = (compressed_tblock_level_row_div_evenly_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[1].param_strategy;
assert(strategy2_ptr != NULL);
compressed_param_vec.push_back(strategy2_ptr->block_row_num);
// 第三个节点类型,参数策略已经策略参数
// COMPRESSED_THREAD_LEVEL_ROW_DIV, COMPRESSED_THREAD_LEVEL_ROW_DIV_NONE_PARAM_STRATEGY
assert(sub_graph_skeleton.exe_node_vec[2].type == COMPRESSED_THREAD_LEVEL_ROW_DIV);
assert(param_strategy_skeleton.param_strategy_vec[2].strategy_type == COMPRESSED_THREAD_LEVEL_ROW_DIV_NONE_PARAM_STRATEGY);
compressed_node_type_vec.push_back(COMPRESSED_THREAD_LEVEL_ROW_DIV);
compressed_param_strategy_vec.push_back(COMPRESSED_THREAD_LEVEL_ROW_DIV_NONE_PARAM_STRATEGY);
// 清除当前compressed阶段的所有积累值
data_set_collector->clear_compressed_accu_info();
data_set_collector->insert_compressed_stage_node_and_param_to_cur_item(compressed_node_type_vec, compressed_param_strategy_vec, compressed_param_vec);
}
// 执行对应的子块
execute_sub_matrix_exe_graph_with_param_strategy(matrix, sub_matrix_id, &sub_graph_skeleton, ¶m_strategy_skeleton);
// 候选的模板类型
set<template_type> candi_template_type_set;
candi_template_type_set.insert(DIRECT_ATOM_TEMPLATE_WARP_COMPRESS);
float time;
float gflops;
// 寻找对应的最优模板,这里先不处理
template_node_t temp_node = find_best_template_node_of_specific_sub_matrix_from_template_set(matrix, sub_matrix_id, candi_template_type_set, time, gflops, search_strategy_ptr, data_set_collector);
// 根据当前性能是不是超过最佳性能来执行不同的处理
if (gflops > best_gflops)
{
if (best_gflops == 0)
{
// 直接赋值,并且修改最佳优化路径
// 这个时候还没有最优的优化路径
best_gflops = gflops;
best_time = time;
// 从best拷贝出来
best_sub_graph = val_copy_from_old_compressed_sub_matrix(sub_graph_skeleton);
best_sub_graph_param_strategy = val_copy_from_old_param_strategy_of_sub_graph(param_strategy_skeleton);
best_temp_node = val_copy_from_old_template_node(temp_node);
// 重新绑定优化骨架和策略骨架
bind_exe_node_param_param_strategy_of_sub_graph(&best_sub_graph, &best_sub_graph_param_strategy);
}
else
{
// 直接赋值,析构已有的最优路径
best_gflops = gflops;
best_time = time;
// 析构已有的最优参数
del_exe_node_param_of_compress_sub_matrix(&best_sub_graph);
// 析构已有的最优策略
del_strategy_of_param_strategy_node_in_sub_matrix(&best_sub_graph_param_strategy);
// 已有的最优模板
del_param_of_template_node(&best_temp_node);
// 执行新的拷贝
// 从best拷贝出来
best_sub_graph = val_copy_from_old_compressed_sub_matrix(sub_graph_skeleton);
best_sub_graph_param_strategy = val_copy_from_old_param_strategy_of_sub_graph(param_strategy_skeleton);
best_temp_node = val_copy_from_old_template_node(temp_node);
// 重新绑定优化骨架和策略骨架
bind_exe_node_param_param_strategy_of_sub_graph(&best_sub_graph, &best_sub_graph_param_strategy);
}
}
// 析构matrix
// 现在matrix肯定存在
assert(matrix != NULL);
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
// 重置所有参数,并且重置所有参数指针,这一步用来处理一些参数是数组的节点,数组中的内容应该重新清空
reset_exe_node_param_and_param_strategy_of_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton);
// 如果所有的模板参数执行后都发生错误,那么这里的temp_node中可能是没有参数的
if (temp_node.template_param != NULL)
{
del_param_of_template_node(&temp_node);
}
else
{
// 当前可能没有出现对应的
assert(gflops == 0);
}
// 加入提前结束的相关内容
if (search_strategy_ptr != NULL)
{
if (continue_search(search_strategy_ptr) == false)
{
search_finished_by_strategy = true;
}
}
if (search_finished_by_strategy == true)
{
break;
}
}
// 如果没有进上面的循环,那就可能需要在这里析构矩阵
if (matrix != NULL)
{
assert(matrix != NULL);
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
}
// 析构用以遍历各种优化路径的两个骨架的参数
del_strategy_of_param_strategy_node_in_sub_matrix(¶m_strategy_skeleton);
del_exe_node_param_of_compress_sub_matrix(&sub_graph_skeleton);
compressed_sub_block_exe_graph_and_template_t return_sub_graph_exe_node_and_template;
return_sub_graph_exe_node_and_template.sub_graph = best_sub_graph;
return_sub_graph_exe_node_and_template.sub_graph_param_strategy = best_sub_graph_param_strategy;
return_sub_graph_exe_node_and_template.temp_node = best_temp_node;
return return_sub_graph_exe_node_and_template;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy2(exe_dense_sub_graph_t dense_graph, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
// 首先执行对应的稠密子图优化
sparse_struct_t* matrix = get_matrix_dense_view_graph(&dense_graph);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
// 查找最优子图
compressed_sub_block_exe_graph_and_template_t best_sub_graph_path = find_best_path_of_white_list_strategy2(matrix, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
// 析构原来的矩阵
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
return best_sub_graph_path;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy2(dense_view_matrix_exe_graph_and_param_strategy_t dense_graph, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
// 首先执行对应的稠密子图优化
sparse_struct_t* matrix = execute_dense_matrix_exe_graph_with_param_strategy(&(dense_graph.dense_sub_graph), &(dense_graph.dense_sub_graph_param_strategy));
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
// 查找最优子图
compressed_sub_block_exe_graph_and_template_t best_sub_graph_path = find_best_path_of_white_list_strategy2(matrix, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
// 析构原来的矩阵
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
return best_sub_graph_path;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy2(sparse_struct_t* input_matrix, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
assert(input_matrix != NULL);
// 矩阵的一系列检查
assert(input_matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(input_matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && input_matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
assert(input_matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 2);
// 执行拷贝
sparse_struct_t* matrix = val_copy_from_old_matrix_struct(input_matrix);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
assert(matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 2);
compressed_block_t* compressed_block_ptr = matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr;
// 获取每一行的行非零元数量,并且获得最大和最小行非零元数量
vector<unsigned long> row_nnz_of_compressed_block = get_nnz_of_each_row_in_compressed_sub_matrix(matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr);
assert(row_nnz_of_compressed_block.size() > 0);
unsigned long max_row_nnz = row_nnz_of_compressed_block[0];
unsigned long min_row_nnz = row_nnz_of_compressed_block[0];
// 遍历获得最大的和最小的行非零元数量
for (unsigned long i = 1; i < row_nnz_of_compressed_block.size(); i++)
{
unsigned long cur_row_nnz = row_nnz_of_compressed_block[i];
if (max_row_nnz < cur_row_nnz)
{
max_row_nnz = cur_row_nnz;
}
if (min_row_nnz > cur_row_nnz)
{
min_row_nnz = cur_row_nnz;
}
}
// 找出最优的子图、最优的参数策略、最优模板类型及其参数
template_node_t best_temp_node;
exe_compressed_sub_graph_t best_sub_graph;
param_strategy_of_sub_graph_t best_sub_graph_param_strategy;
best_time = 99999999999999;
best_gflops = 0;
// 定一个优化路径骨架
exe_compressed_sub_graph_t sub_graph_skeleton;
// 定一个参数设定的骨架
param_strategy_of_sub_graph_t param_strategy_skeleton;
// 定义骨架,一个是row padding
// 定义骨架,首先是一个ROW_padding和BLB_row
compressed_row_padding_direct_param_strategy_t row_padding_param_strategy;
compressed_tblock_level_row_div_evenly_param_strategy_t BLB_row_div_evenly_param_strategy;
add_a_exe_node_and_param_strategy_to_exe_compressed_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton, COMPRESSED_ROW_PADDING, COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY, &row_padding_param_strategy);
add_a_exe_node_and_param_strategy_to_exe_compressed_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton, COMPRESSED_TBLOCK_LEVEL_ROW_DIV, COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY, &BLB_row_div_evenly_param_strategy);
// 增加对于参数的调节
param_enumerater_t param_setter;
assert(sub_graph_skeleton.exe_node_vec.size() == 2);
// 执行一个列分块
// 对应参数的指针
compressed_row_padding_direct_param_strategy_t* row_padding_direct_param_strategy = (compressed_row_padding_direct_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[0].param_strategy;
compressed_tblock_level_row_div_evenly_param_strategy_t* tblock_level_row_div_evenly_param_strategy = (compressed_tblock_level_row_div_evenly_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[1].param_strategy;
assert(row_padding_direct_param_strategy != NULL && tblock_level_row_div_evenly_param_strategy != NULL);
// 对于BLB row padding的长度,找一个非常小的值
register_integer_independ_param_to_enumerater(¶m_setter, &(row_padding_direct_param_strategy->multiply), 32, 64, 32);
// padding的数量是当前子块的最小行号
row_padding_direct_param_strategy->padding_row_length = min_row_nnz;
// 添加一个TLB的纵分块
compressed_thread_level_col_div_fixed_param_strategy_t thread_col_param_strategy;
// 将策略加到优化骨架中
add_a_exe_node_and_param_strategy_to_exe_compressed_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton, COMPRESSED_THREAD_LEVEL_COL_DIV, COMPRESSED_THREAD_LEVEL_COL_DIV_FIXED_PARAM_STRATEGY, &thread_col_param_strategy);
// 将参数的指针提取出来
compressed_thread_level_col_div_fixed_param_strategy_t* thread_level_col_div_fixed_param_strategy = (compressed_thread_level_col_div_fixed_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[2].param_strategy;
// 列分块的参数调试范围,从0到16,高过这个范围的就使用warp的模板
// col最大值为最大行非零元数量的一半,向上取整,在遍历的过程中需要进一步剪枝,现在只能设定一个固定的值范围
unsigned long fixed_max_col_size = 16;
unsigned long max_col_size_acc_to_nnz = max_row_nnz / 2;
if (max_row_nnz % 2 != 0)
{
max_col_size_acc_to_nnz = max_col_size_acc_to_nnz + 1;
}
if (max_col_size_acc_to_nnz < fixed_max_col_size)
{
fixed_max_col_size = max_col_size_acc_to_nnz;
}
assert(thread_level_col_div_fixed_param_strategy != NULL);
register_integer_independ_param_to_enumerater(¶m_setter, &(thread_level_col_div_fixed_param_strategy->col_block_nnz_num), 1, fixed_max_col_size, 3);
assert(sub_graph_skeleton.exe_node_vec.size() == 3 && param_strategy_skeleton.param_strategy_vec.size() == 3);
bool search_finished_by_strategy = false;
// 遍历所有参数
while (set_param_combination_to_next(¶m_setter) == false)
{
// 矩阵已经不存在,那就执行稠密子图的优化路径,产生新的矩阵
if (matrix == NULL)
{
// 通过稠密视图的块得出需要的子块
matrix = val_copy_from_old_matrix_struct(input_matrix);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
compressed_block_ptr = matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr;
}
// 再定义一些参数
tblock_level_row_div_evenly_param_strategy->block_row_num = row_padding_direct_param_strategy->multiply;
// 打印一些参数
cout << "row_padding_direct_param_strategy->multiply:" << row_padding_direct_param_strategy->multiply << ",row_padding_direct_param_strategy->padding_row_length:" << row_padding_direct_param_strategy->padding_row_length << "thread_level_col_div_fixed_param_strategy->col_block_nnz_num:" << thread_level_col_div_fixed_param_strategy->col_block_nnz_num << endl;
// 查看当前的padding率以及每个BLB中的TLB的数量。超过1008就放弃分块的条带,这里也考虑padding产生的新条带
// 并且需要计算padding率
unsigned long old_nnz = compressed_block_ptr->read_index[0]->length;
unsigned long new_nnz = 0;
// 最大的TLB数量
unsigned long max_TLB_of_BLB = 0;
// 遍历每个BLB条,计算累计的nnz数量,不考虑WLB的padding,并且计算每个条带的TLB的数量
for (unsigned long i = 0; i < row_nnz_of_compressed_block.size() / tblock_level_row_div_evenly_param_strategy->block_row_num; i++)
{
// 累计非零元数量和TLB数量
unsigned long nnz_of_cur_BLB = 0;
unsigned long TLB_num_of_cur_BLB = 0;
for (unsigned long global_row_id = tblock_level_row_div_evenly_param_strategy->block_row_num * i; global_row_id < tblock_level_row_div_evenly_param_strategy->block_row_num * (i + 1); global_row_id++)
{
assert(global_row_id < row_nnz_of_compressed_block.size());
unsigned long cur_row_nnz = row_nnz_of_compressed_block[global_row_id];
// TLB分块之后的行长度初始化为和当前行长度相等的样子
unsigned long new_cur_row_nnz = cur_row_nnz;
// 如果当前行非零元数量不能整除TLB的列长度,那就要补成列的长度
if (cur_row_nnz % thread_level_col_div_fixed_param_strategy->col_block_nnz_num != 0)
{
new_cur_row_nnz = (cur_row_nnz / thread_level_col_div_fixed_param_strategy->col_block_nnz_num + 1) * thread_level_col_div_fixed_param_strategy->col_block_nnz_num;
assert(new_cur_row_nnz > cur_row_nnz);
assert(new_cur_row_nnz % thread_level_col_div_fixed_param_strategy->col_block_nnz_num == 0);
}
// 计算当前TLB数量
unsigned long new_row_TLB_num = new_cur_row_nnz / thread_level_col_div_fixed_param_strategy->col_block_nnz_num;
nnz_of_cur_BLB = nnz_of_cur_BLB + new_cur_row_nnz;
TLB_num_of_cur_BLB = TLB_num_of_cur_BLB + new_row_TLB_num;
}
// 总非零元数量
new_nnz = new_nnz + nnz_of_cur_BLB;
if (max_TLB_of_BLB < TLB_num_of_cur_BLB)
{
max_TLB_of_BLB = TLB_num_of_cur_BLB;
}
}
// 可能还剩最后一个块
if (row_nnz_of_compressed_block.size() % tblock_level_row_div_evenly_param_strategy->block_row_num != 0)
{
// 累计非零元数量和TLB数量
unsigned long nnz_of_cur_BLB = 0;
unsigned long TLB_num_of_cur_BLB = 0;
unsigned long remain_row_num = row_nnz_of_compressed_block.size() % tblock_level_row_div_evenly_param_strategy->block_row_num;
for (unsigned long global_row_id = row_nnz_of_compressed_block.size() - remain_row_num; global_row_id < row_nnz_of_compressed_block.size(); global_row_id++)
{
assert(global_row_id < row_nnz_of_compressed_block.size());
unsigned long cur_row_nnz = row_nnz_of_compressed_block[global_row_id];
// TLB分块之后的行长度初始化为和当前行长度相等的样子
unsigned long new_cur_row_nnz = cur_row_nnz;
// 如果当前行非零元数量不能整除TLB的列长度,那就要补成列的长度
if (cur_row_nnz % thread_level_col_div_fixed_param_strategy->col_block_nnz_num != 0)
{
new_cur_row_nnz = (cur_row_nnz / thread_level_col_div_fixed_param_strategy->col_block_nnz_num + 1) * thread_level_col_div_fixed_param_strategy->col_block_nnz_num;
assert(new_cur_row_nnz > cur_row_nnz);
assert(new_cur_row_nnz % thread_level_col_div_fixed_param_strategy->col_block_nnz_num == 0);
}
// 计算当前TLB数量
unsigned long new_row_TLB_num = new_cur_row_nnz / thread_level_col_div_fixed_param_strategy->col_block_nnz_num;
nnz_of_cur_BLB = nnz_of_cur_BLB + new_cur_row_nnz;
TLB_num_of_cur_BLB = TLB_num_of_cur_BLB + new_row_TLB_num;
}
// 加上被row padding的数量
assert(tblock_level_row_div_evenly_param_strategy->block_row_num > remain_row_num);
unsigned long padding_row_num = tblock_level_row_div_evenly_param_strategy->block_row_num - remain_row_num;
// 每一行非零元的数量
unsigned long new_padding_row_nnz = min_row_nnz;
if (new_padding_row_nnz % thread_level_col_div_fixed_param_strategy->col_block_nnz_num != 0)
{
new_padding_row_nnz = (new_padding_row_nnz / thread_level_col_div_fixed_param_strategy->col_block_nnz_num + 1) * thread_level_col_div_fixed_param_strategy->col_block_nnz_num;
assert(new_padding_row_nnz > min_row_nnz);
assert(new_padding_row_nnz % thread_level_col_div_fixed_param_strategy->col_block_nnz_num == 0);
}
nnz_of_cur_BLB = nnz_of_cur_BLB + padding_row_num * new_padding_row_nnz;
TLB_num_of_cur_BLB = TLB_num_of_cur_BLB + new_padding_row_nnz / thread_level_col_div_fixed_param_strategy->col_block_nnz_num * padding_row_num;
// 总非零元数量
new_nnz = new_nnz + nnz_of_cur_BLB;
if (max_TLB_of_BLB < TLB_num_of_cur_BLB)
{
max_TLB_of_BLB = TLB_num_of_cur_BLB;
}
}
assert(new_nnz >= old_nnz);
cout << "new_nnz:" << new_nnz << ",old_nnz:" << old_nnz << ",padding rate:" << (float)new_nnz/(float)old_nnz << endl;
if ((float)new_nnz/(float)old_nnz > get_config()["PADDING_RATE_UP_BOUND"].as_integer())
{
cout << "padding rate is larger than " << get_config()["PADDING_RATE_UP_BOUND"].as_integer() << endl;
// 析构matrix
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
// 放弃这一轮调参
continue;
}
// 如果BLB中的TLB数量大于一个阈值,就直接放弃这轮调参
if (max_TLB_of_BLB > 1024)
{
cout << "too many (> 1024) TLB in BLB, cause low performance" << endl;
// 析构matrix
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
continue;
}
if (data_set_collector != NULL)
{
// 如果存在一个数据集收集器,那么就需要收集节点的类型和参数
assert(data_set_collector->accu_dense_graph_node_type_vec.size() == data_set_collector->accu_dense_param_strategy_type_vec.size());
assert(data_set_collector->accu_dense_graph_node_type_vec.size() > 0);
vector<exe_node_type> compressed_node_type_vec;
vector<exe_node_param_set_strategy> compressed_param_strategy_vec;
vector<float> compressed_param_vec;
// 一共三个节点
assert(sub_graph_skeleton.exe_node_vec.size() == 3);
// 第一个节点的类型,参数策略的类型,以及策略的参数
// COMPRESSED_ROW_PADDING, COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY, compressed_row_padding_direct_param_strategy_t
assert(sub_graph_skeleton.exe_node_vec[0].type == COMPRESSED_ROW_PADDING);
assert(param_strategy_skeleton.param_strategy_vec[0].strategy_type == COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY);
compressed_node_type_vec.push_back(COMPRESSED_ROW_PADDING);
compressed_param_strategy_vec.push_back(COMPRESSED_ROW_PADDING_DIRECT_PARAM_STRATEGY);
compressed_row_padding_direct_param_strategy_t* strategy1_ptr = (compressed_row_padding_direct_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[0].param_strategy;
assert(strategy1_ptr != NULL);
compressed_param_vec.push_back(strategy1_ptr->multiply);
compressed_param_vec.push_back(strategy1_ptr->padding_row_length);
// 第二个节点的类型
// COMPRESSED_TBLOCK_LEVEL_ROW_DIV, COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY, compressed_tblock_level_row_div_evenly_param_strategy_t
assert(sub_graph_skeleton.exe_node_vec[1].type == COMPRESSED_TBLOCK_LEVEL_ROW_DIV);
assert(param_strategy_skeleton.param_strategy_vec[1].strategy_type == COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY);
compressed_node_type_vec.push_back(COMPRESSED_TBLOCK_LEVEL_ROW_DIV);
compressed_param_strategy_vec.push_back(COMPRESSED_TBLOCK_LEVEL_ROW_DIV_EVENLY_PARAM_STRATEGY);
compressed_tblock_level_row_div_evenly_param_strategy_t* strategy2_ptr = (compressed_tblock_level_row_div_evenly_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[1].param_strategy;
assert(strategy2_ptr != NULL);
compressed_param_vec.push_back(strategy2_ptr->block_row_num);
// 第三个节点的类型
// COMPRESSED_THREAD_LEVEL_COL_DIV, COMPRESSED_THREAD_LEVEL_COL_DIV_FIXED_PARAM_STRATEGY, compressed_thread_level_col_div_fixed_param_strategy_t
assert(sub_graph_skeleton.exe_node_vec[2].type == COMPRESSED_THREAD_LEVEL_COL_DIV);
assert(param_strategy_skeleton.param_strategy_vec[2].strategy_type == COMPRESSED_THREAD_LEVEL_COL_DIV_FIXED_PARAM_STRATEGY);
compressed_node_type_vec.push_back(COMPRESSED_THREAD_LEVEL_COL_DIV);
compressed_param_strategy_vec.push_back(COMPRESSED_THREAD_LEVEL_COL_DIV_FIXED_PARAM_STRATEGY);
compressed_thread_level_col_div_fixed_param_strategy_t* strategy3_ptr = (compressed_thread_level_col_div_fixed_param_strategy_t*)param_strategy_skeleton.param_strategy_vec[2].param_strategy;
assert(strategy3_ptr != NULL);
compressed_param_vec.push_back(strategy3_ptr->col_block_nnz_num);
// 清除当前compressed阶段的所有积累值
data_set_collector->clear_compressed_accu_info();
data_set_collector->insert_compressed_stage_node_and_param_to_cur_item(compressed_node_type_vec, compressed_param_strategy_vec, compressed_param_vec);
}
// 执行对应的的子块
execute_sub_matrix_exe_graph_with_param_strategy(matrix, sub_matrix_id, &sub_graph_skeleton, ¶m_strategy_skeleton);
// 候选模板两个
set<template_type> candi_template_type_set;
// candi_template_type_set.insert(DIRECT_ATOM_TEMPLATE_WARP_COMPRESS);
candi_template_type_set.insert(SHARED_MEMORY_TEMPLATE_WARP_COMPRESS);
// 性能
float time;
float gflops;
// 寻找对应的最优模板,这里先不处理
template_node_t temp_node = find_best_template_node_of_specific_sub_matrix_from_template_set(matrix, sub_matrix_id, candi_template_type_set, time, gflops, search_strategy_ptr, data_set_collector);
if (gflops > best_gflops)
{
if (best_gflops == 0)
{
// 直接赋值,并且修改最佳优化路径
// 这个时候还没有最优的优化路径
best_gflops = gflops;
best_time = time;
// 从best拷贝出来
best_sub_graph = val_copy_from_old_compressed_sub_matrix(sub_graph_skeleton);
best_sub_graph_param_strategy = val_copy_from_old_param_strategy_of_sub_graph(param_strategy_skeleton);
best_temp_node = val_copy_from_old_template_node(temp_node);
// 重新绑定优化骨架和策略骨架
bind_exe_node_param_param_strategy_of_sub_graph(&best_sub_graph, &best_sub_graph_param_strategy);
}
else
{
// 直接赋值,析构已有的最优路径
best_gflops = gflops;
best_time = time;
// 析构已有的最优参数
del_exe_node_param_of_compress_sub_matrix(&best_sub_graph);
// 析构已有的最优策略
del_strategy_of_param_strategy_node_in_sub_matrix(&best_sub_graph_param_strategy);
// 已有的最优模板
del_param_of_template_node(&best_temp_node);
// 执行新的拷贝
// 从best拷贝出来
best_sub_graph = val_copy_from_old_compressed_sub_matrix(sub_graph_skeleton);
best_sub_graph_param_strategy = val_copy_from_old_param_strategy_of_sub_graph(param_strategy_skeleton);
best_temp_node = val_copy_from_old_template_node(temp_node);
// 重新绑定优化骨架和策略骨架
bind_exe_node_param_param_strategy_of_sub_graph(&best_sub_graph, &best_sub_graph_param_strategy);
}
}
// 析构matrix
// 现在matrix肯定存在
assert(matrix != NULL);
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
// 重置所有参数,并且重置所有参数指针,这一步用来处理一些参数是数组的节点,数组中的内容应该重新清空
reset_exe_node_param_and_param_strategy_of_sub_graph(&sub_graph_skeleton, ¶m_strategy_skeleton);
// 如果所有的模板参数执行后都发生错误,那么这里的temp_node中可能是没有参数的
if (temp_node.template_param != NULL)
{
del_param_of_template_node(&temp_node);
}
else
{
// 当前可能没有出现对应的
assert(gflops == 0);
}
// 加入提前结束的相关内容
if (search_strategy_ptr != NULL)
{
if (continue_search(search_strategy_ptr) == false)
{
search_finished_by_strategy = true;
}
}
if (search_finished_by_strategy == true)
{
break;
}
}
// 如果之前没有析构矩阵,现在就析构矩阵
if (matrix != NULL)
{
assert(matrix != NULL);
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
}
// 析构用以遍历各种优化路径的两个骨架的参数
del_strategy_of_param_strategy_node_in_sub_matrix(¶m_strategy_skeleton);
del_exe_node_param_of_compress_sub_matrix(&sub_graph_skeleton);
compressed_sub_block_exe_graph_and_template_t return_sub_graph_exe_node_and_template;
return_sub_graph_exe_node_and_template.sub_graph = best_sub_graph;
return_sub_graph_exe_node_and_template.sub_graph_param_strategy = best_sub_graph_param_strategy;
return_sub_graph_exe_node_and_template.temp_node = best_temp_node;
return return_sub_graph_exe_node_and_template;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy3(exe_dense_sub_graph_t dense_graph, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
// 首先执行对应的稠密子图优化
sparse_struct_t* matrix = get_matrix_dense_view_graph(&dense_graph);
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
compressed_sub_block_exe_graph_and_template_t best_sub_graph_path = find_best_path_of_white_list_strategy3(matrix, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
memory_garbage_manager_t mem_manager;
delete_sparse_struct_t(&mem_manager, matrix);
matrix = NULL;
return best_sub_graph_path;
}
compressed_sub_block_exe_graph_and_template_t find_best_path_of_white_list_strategy3(dense_view_matrix_exe_graph_and_param_strategy_t dense_graph, unsigned long sub_matrix_id, float &best_time, float &best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
sparse_struct_t* matrix = execute_dense_matrix_exe_graph_with_param_strategy(&(dense_graph.dense_sub_graph), &(dense_graph.dense_sub_graph_param_strategy));
assert(matrix != NULL);
// 矩阵的一系列检查
assert(matrix->block_coor_table.item_arr.size() > sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id] != NULL && matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
compressed_sub_block_exe_graph_and_template_t best_sub_graph_path = find_best_path_of_white_list_strategy3(matrix, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);