forked from AnonymousRepo123/AlphaSparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_auto_tuner.cc
executable file
·2642 lines (2100 loc) · 118 KB
/
default_auto_tuner.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"
#include "direct_atom_op.hpp"
#include "direct_atom_op_warp_compress.hpp"
#include "direct_atom_op_warp_block_compress.hpp"
#include "shared_memory_op.hpp"
#include "shared_memory_op_warp_compress.hpp"
#include "shared_memory_long_row_op.hpp"
#include "shared_memory_total_warp_reduce_op.hpp"
#include "direct_atom_total_warp_reduce_op.hpp"
#include "unaligned_warp_reduce_same_TLB_size_op.hpp"
#include "unaligned_warp_reduce_same_TLB_size_op_with_warp_reduce.hpp"
// template_node_t find_best_param_of_specific_template_node(sparse_struct_t* matrix, int sub_matrix_id, template_type type, float& best_time, float& best_gflops)
// {
// 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);
// // 已经被压缩过
// assert(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;
// // 执行默认的一系列分块,将所有的分块补齐
// execute_default_div_to_complete_each_level_blocking(matrix, sub_matrix_id);
// // 查看分块之后的索引数量
// // cout << "compressed_block_ptr->read_index.size():" << compressed_block_ptr->read_index.size() << endl;
// assert(compressed_block_ptr->read_index.size() == 7);
// template_node_t return_template_node;
// // 获取可能的模板集合
// set<template_type> template_set = supported_template_of_sub_matrix(matrix, sub_matrix_id);
// // 遍历所有的模板类型,为每个模板找出对应的参数
// // 如果搜不到对应的模板,这个时候对应的这里的template param是空的,之后在外面处理
// return return_template_node;
// }
template_node_t find_best_template_node_of_specific_sub_matrix_from_template_set(sparse_struct_t* matrix, int sub_matrix_id, set<template_type> template_set, float& best_time, float& best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
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);
// 已经被压缩过
assert(matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
// 自动补齐没有执行的对应层次的分块
execute_default_div_to_complete_each_level_blocking(matrix, sub_matrix_id);
assert(matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
// 要返回的最优模板节点类型和节点参数
template_node_t return_template_node;
return_template_node.template_param = NULL;
vector<int> sub_matrix_id_vec;
sub_matrix_id_vec.push_back(sub_matrix_id);
best_gflops = 0;
// 遍历候选的所有的模板,查看性能,并且不断更新最终的参数,在过程中注意析构最后模板的参数
for (auto template_type : template_set)
{
float cur_template_gflops = 0;
float cur_template_time = 99999999999;
// if (data_set_collector != NULL)
// {
// cout << "find_best_template_node_of_specific_sub_matrix_from_template_set: need to collect ml data" << endl;
// }
template_node_t cur_template_node = find_best_param_of_specific_template_node_of_sub_matrix(matrix, sub_matrix_id, template_type, cur_template_time, cur_template_gflops, search_strategy_ptr, data_set_collector);
// 如果吞吐量更大,那就更新一下最优的模板和参数
if (cur_template_gflops > best_gflops)
{
// 可能需要析构之前模板参数
if (return_template_node.template_param != NULL)
{
del_param_of_template_node(&return_template_node);
}
else
{
assert(best_gflops == 0);
}
// 赋值为新的参数
return_template_node.template_param = cur_template_node.template_param;
return_template_node.type = cur_template_node.type;
best_gflops = cur_template_gflops;
best_time = cur_template_time;
}
// 这里查看是不是要直接退出,只有在有搜索策略的时候需要考虑提前终止的问题
if (search_strategy_ptr != NULL)
{
if (continue_search(search_strategy_ptr) == false)
{
break;
}
}
}
assert(best_gflops >= 0);
if (best_gflops != 0)
{
assert(return_template_node.template_param != NULL);
}
return return_template_node;
}
template_node_t find_best_param_of_specific_template_node_of_sub_matrix(sparse_struct_t* matrix, int sub_matrix_id, template_type type, float& best_time, float& best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
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);
// 已经被压缩过
assert(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() == 7);
// 要返回的最优模板节点类型和节点参数
template_node_t return_template_node;
vector<int> sub_matrix_id_vec;
sub_matrix_id_vec.push_back(sub_matrix_id);
// if (data_set_collector != NULL)
// {
// cout << "find_best_param_of_specific_template_node_of_sub_matrix: need to collect ml data" << endl;
// }
// 首先创建一个临时的代码生成器
// 然后生成对应的模板,找到对应的参数,最后执行对应析构将对应的代码生成器析构,包括对应的模板也会被析构
if (type == DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS)
{
// 生成一个操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成一个代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
direct_atom_template_warp_block_compress_t* new_template = init_direct_atom_template_warp_block_compress(builder, sub_matrix_id);
// 注册模板
add_template_to_builder(builder, new_template, DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最优的参数节点
return_template_node = find_best_param_of_direct_atom_template_warp_block_compress(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 然后析构操作管理器
delete op_manager;
}
else if (type == DIRECT_ATOM_TEMPLATE_WARP_COMPRESS)
{
// 生成一个操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成一个代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
direct_atom_template_warp_compress_t* new_template = init_direct_atom_template_warp_compress(builder, sub_matrix_id);
// 将模板加到代码生成器中
add_template_to_builder(builder, new_template, DIRECT_ATOM_TEMPLATE_WARP_COMPRESS, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最优的参数节点
return_template_node = find_best_param_of_direct_atom_template_warp_compress(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == DIRECT_ATOM_TEMPLATE_WARP_COMPRESS);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 然后析构操作管理器
delete op_manager;
}
else if (type == DIRECT_ATOM_TEMPLATE)
{
// 生成一个操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成一个代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
direct_atom_template_t* new_template = init_direct_atom_template(builder, sub_matrix_id);
// 将模板加到代码生成器中
add_template_to_builder(builder, new_template, DIRECT_ATOM_TEMPLATE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最后参数节点
return_template_node = find_best_param_of_direct_atom_template(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == DIRECT_ATOM_TEMPLATE);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 然后析构操作管理器
delete op_manager;
}
else if (type == DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成一个代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
direct_atom_total_warp_reduce_template_t* new_template = init_direct_atom_total_warp_reduce_template(builder, sub_matrix_id);
// 将模板放到代码生成器中
add_template_to_builder(builder, new_template, DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最后一个参数节点
return_template_node = find_best_param_of_direct_atom_total_warp_reduce_template(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 代码析构管理器
delete op_manager;
}
else if (type == SHARED_MEMORY_LONG_ROW_TEMPLATE)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成一个代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
shared_memory_long_row_template_t* new_template = init_shared_memory_long_row_template(builder, sub_matrix_id);
// 将模板放到代码生成器中
add_template_to_builder(builder, new_template, SHARED_MEMORY_LONG_ROW_TEMPLATE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最后一个参数节点
return_template_node = find_best_param_of_shared_memory_long_row_template(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == SHARED_MEMORY_LONG_ROW_TEMPLATE);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 析构动作管理器
delete op_manager;
}
else if (type == SHARED_MEMORY_TEMPLATE_WARP_COMPRESS)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
shared_memory_template_warp_compress_t* new_template = init_shared_memory_template_warp_compress(builder, sub_matrix_id);
// 将模板放在代码生成器中
add_template_to_builder(builder, new_template, SHARED_MEMORY_TEMPLATE_WARP_COMPRESS, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最优模板参数节点
return_template_node = find_best_param_of_shared_memory_template_warp_compress(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == SHARED_MEMORY_TEMPLATE_WARP_COMPRESS);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 析构动作管理器
delete op_manager;
}
else if (type == SHARED_MEMORY_TEMPLATE)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
shared_memory_template_t* new_template = init_shared_memory_template(builder, sub_matrix_id);
// 将模板放在生成器中
add_template_to_builder(builder, new_template, SHARED_MEMORY_TEMPLATE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最优模板参数节点
return_template_node = find_best_param_of_shared_memory_template(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == SHARED_MEMORY_TEMPLATE);
// 析构动作管理器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 析构动作管理器
delete op_manager;
}
else if (type == SHARED_MEMORY_TOTAL_WARP_REDUCE_TEMPLATE)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 生成代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 生成一个模板
shared_memory_total_warp_reduce_template_t* new_template = init_shared_memory_total_warp_reduce_template(builder, sub_matrix_id);
// 将模板放到代码生成器中
add_template_to_builder(builder, new_template, SHARED_MEMORY_TOTAL_WARP_REDUCE_TEMPLATE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最优模板参数
return_template_node = find_best_param_of_shared_memory_total_warp_reduce_template(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == SHARED_MEMORY_TOTAL_WARP_REDUCE_TEMPLATE);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 析构动作管理器
delete op_manager;
}
else if (type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE_WITH_WARP_REDUCE)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 模板
unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce_t* new_template = init_unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce(builder, sub_matrix_id);
// 放到生成器中
add_template_to_builder(builder, new_template, UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE_WITH_WARP_REDUCE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成最优模板参数
return_template_node = find_best_param_of_unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE_WITH_WARP_REDUCE);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 析构动作管理器
delete op_manager;
}
else if (type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE)
{
// 操作管理器
operator_manager_t* op_manager = init_op_manager(matrix);
// 代码生成器
code_builder_t* builder = init_code_builder(op_manager, sub_matrix_id_vec);
// 模板
unaligned_warp_reduce_same_TLB_size_template_t* new_template = init_unaligned_warp_reduce_same_TLB_size_template(builder, sub_matrix_id);
// 生成器
add_template_to_builder(builder, new_template, UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE, sub_matrix_id);
// 压缩
try_all_compress(new_template);
// 生成模板参数
return_template_node = find_best_param_of_unaligned_warp_reduce_same_TLB_size_template(builder, sub_matrix_id, best_time, best_gflops, search_strategy_ptr, data_set_collector);
assert(return_template_node.type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE);
// 析构代码生成器
memory_garbage_manager_t mem_manager;
delete_code_builder_without_operator_manager(&mem_manager, builder);
// 析构动作管理器
delete op_manager;
}
else
{
// 当前模板的类型不支持
cout << "find_best_param_of_specific_template_node: template type is not supported" << endl;
assert(false);
}
return return_template_node;
}
void execute_default_div_to_complete_each_level_blocking(sparse_struct_t* matrix, int sub_matrix_id)
{
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);
// 已经被压缩过
assert(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;
if (compressed_block_ptr->read_index.size() == 2)
{
// 如果当前索引数量是两个,就补一个block级别的分块
// 根据行数量补一个默认的行分块
// 行数量
unsigned long row_num = compressed_block_ptr->read_index[0]->max_row_index - compressed_block_ptr->read_index[0]->min_row_index + 1;
vector<unsigned int> BLB_row_num;
BLB_row_num.push_back(row_num);
sep_tblock_level_row_csr(compressed_block_ptr, BLB_row_num);
// 已经完成,检查当前索引数量
assert(compressed_block_ptr->read_index.size() == 3);
}
if (compressed_block_ptr->read_index.size() == 3)
{
// 需要一个WLB级别的分块
// warp不处理
vector<unsigned long> sep_BLB_id;
vector<vector<unsigned int>> WLB_row_size_of_each_BLB;
sep_warp_level_row_csr(compressed_block_ptr, sep_BLB_id, WLB_row_size_of_each_BLB);
// 已经完成,检查当前索引数量
assert(compressed_block_ptr->read_index.size() == 4);
}
if (compressed_block_ptr->read_index.size() == 4)
{
// 默认的行切分
vector<unsigned long> sep_WLB_id;
vector<unsigned long> thread_col_size_of_each_WLB;
// 查看warp的数量
unsigned long warp_num = compressed_block_ptr->read_index[3]->block_num;
for (unsigned long i = 0; i < warp_num; i++)
{
sep_WLB_id.push_back(i);
thread_col_size_of_each_WLB.push_back(1);
}
sep_thread_level_col_ell_with_padding(compressed_block_ptr, sep_WLB_id, thread_col_size_of_each_WLB);
assert(compressed_block_ptr->read_index.size() == 7);
}
// cout << "compressed_block_ptr->read_index.size():" << compressed_block_ptr->read_index.size() << endl;
assert(compressed_block_ptr->read_index.size() == 7);
}
// 这个模板的参数替换是不用,性能会输出出来,用来横向比较
template_node_t find_best_param_of_direct_atom_template_warp_block_compress(code_builder_t* builder, int sub_matrix_id, float& return_best_time, float& return_best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
assert(builder != NULL && sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->op_manager != NULL && builder->op_manager->matrix != NULL && sub_matrix_id < builder->template_vec.size());
assert(sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->template_type_vec[sub_matrix_id] == DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS);
assert(builder->template_vec[sub_matrix_id] != NULL && builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
// 有两个参数,一个是block数量和block内thread数量
long tblock_num;
long thread_num_in_block;
// 创建一个参数枚举器,只有thread_num_in_block的参数是需要调整的
param_enumerater_t param_setter;
// 性能影响差距不是很大
register_integer_independ_param_to_enumerater(¶m_setter, &thread_num_in_block, 32, 256, 32);
// 查看是不是第一个模板的枚举
bool is_first_enumerate = true;
// 记录最佳的时间和最佳的性能,以及对应的最佳参数
float best_time = 0;
float best_gflops = 0;
long best_tblock_num = 0;
long best_thread_num_in_block = 0;
direct_atom_template_warp_block_compress_t* target_template = (direct_atom_template_warp_block_compress_t *)builder->template_vec[sub_matrix_id];
// 用一个参数来判断在搜索策略下是不是需要
bool search_finished_by_strategy = false;
while (set_param_combination_to_next(¶m_setter) == false)
{
// 查看TLB的数量
unsigned long TLB_number = target_template->size_of_global_row_index_of_thread_level_block;
// 计算tblock的数量
tblock_num = TLB_number / thread_num_in_block;
if (TLB_number % thread_num_in_block != 0)
{
// 不能乘除要多加一个线程块
tblock_num = tblock_num + 1;
}
if (tblock_num > get_config()["MAX_TBLOCK_NUM"].as_integer() - 1)
{
tblock_num = get_config()["MAX_TBLOCK_NUM"].as_integer() - 1;
}
// 将参数写到模板中
target_template->tblock_num = tblock_num;
target_template->thread_num_in_block = thread_num_in_block;
cout << "find_best_param_of_direct_atom_template_warp_block_compress: target_template->tblock_num:" << target_template->tblock_num << endl;
cout << "find_best_param_of_direct_atom_template_warp_block_compress: target_template->thread_num_in_block:" << target_template->thread_num_in_block << endl;
vector<int> sub_matrix_id_vec;
sub_matrix_id_vec.push_back(sub_matrix_id);
float exe_time = 0;
float exe_gflops = 0;
// 这里对模板的具体参数执行
bool is_success_exe = part_execute_code_builder(builder, sub_matrix_id_vec, exe_time, exe_gflops, string(get_config()["ROOT_PATH_STR"].as_string()) + "/cuda_code", string(get_config()["ROOT_PATH_STR"].as_string()) + "/data_source", is_first_enumerate, true);
// 如果不成功就跳过
if (is_success_exe == false)
{
continue;
}
// 如果运行成功,将参数和最终性能插入到参数数组中
vector<float> param_vec;
if (data_set_collector != NULL)
{
// 已有的内容存在一些积累
assert(data_set_collector->accu_dense_param_strategy_type_vec.size() > 0);
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_compressed_sub_param_strategy_type_vec.size() > 0);
assert(data_set_collector->accu_compressed_sub_param_strategy_type_vec.size() == data_set_collector->accu_compressed_sub_graph_node_type_vec.size());
// 将参数记录下来
param_vec.push_back(target_template->tblock_num);
param_vec.push_back(target_template->thread_num_in_block);
param_vec.push_back(exe_gflops);
// 将参数写到数据集收集器中
data_set_collector->insert_template_node_and_param_to_cur_item_and_add_to_dataset(DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS, param_vec);
}
is_first_enumerate = false;
// 当glops更大的时候,就替换
if (exe_gflops > best_gflops)
{
// 找到更好的参数了
best_gflops = exe_gflops;
best_time = exe_time;
best_tblock_num = tblock_num;
best_thread_num_in_block = thread_num_in_block;
}
// 如果有搜索策略,可能需要看看提前退出的问题
if (search_strategy_ptr != NULL)
{
if (continue_search(search_strategy_ptr, exe_gflops) == false)
{
search_finished_by_strategy = true;
}
}
if (search_finished_by_strategy == true)
{
break;
}
}
// 产生一个新的节点,来记录当前最优的参数组合
template_node_t return_node;
direct_atom_template_warp_block_compress_node_param_t* param_ptr = new direct_atom_template_warp_block_compress_node_param_t();
param_ptr->tblock_num = best_tblock_num;
param_ptr->thread_num_in_block = best_thread_num_in_block;
return_node.type = DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS;
return_node.template_param = param_ptr;
// 将最优性能传到外面
return_best_time = best_time;
return_best_gflops = best_gflops;
return return_node;
}
template_node_t find_best_param_of_direct_atom_template_warp_compress(code_builder_t* builder, int sub_matrix_id, float& return_best_time, float& return_best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
assert(builder != NULL && sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->op_manager != NULL && builder->op_manager->matrix != NULL && sub_matrix_id < builder->template_vec.size());
assert(sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->template_type_vec[sub_matrix_id] == DIRECT_ATOM_TEMPLATE_WARP_COMPRESS);
assert(builder->template_vec[sub_matrix_id] != NULL && builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
index_of_compress_block_t* BLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[2];
index_of_compress_block_t* WLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[3];
unsigned long max_TLB_num = 0;
// 最少的TLB数量
unsigned long min_TLB_num = 9999999;
// 唯一需要调的参数应该只有thread的数量,枚举的下界找小于最小TLB数量的32的倍数,从32的倍数开始调。上界是BLB中TLB数量的最大值最近的大于TLB数量的32的倍数
// 首先找出最大的块TLB数量
for (unsigned long BLB_id = 0; BLB_id < BLB_index->block_num; BLB_id++)
{
// 这个BLB第一个WLB的索引
unsigned long cur_WLB_first_id = read_from_array_with_data_type(BLB_index->index_arr, BLB_index->index_data_type, BLB_id);
// 下一个BLB的第一个WLB的索引
unsigned long next_WLB_first_id = read_from_array_with_data_type(BLB_index->index_arr, BLB_index->index_data_type, BLB_id + 1);
// 只要两个同时小于length,就算出他们的首个TLB索引
if (cur_WLB_first_id < WLB_index->length && next_WLB_first_id < WLB_index->length)
{
unsigned long cur_BLB_first_TLB_id = read_from_array_with_data_type(WLB_index->index_arr, WLB_index->index_data_type, cur_WLB_first_id);
unsigned long next_BLB_first_TLB_id = read_from_array_with_data_type(WLB_index->index_arr, WLB_index->index_data_type, next_WLB_first_id);
unsigned long TLB_num = next_BLB_first_TLB_id - cur_BLB_first_TLB_id;
if (TLB_num > max_TLB_num)
{
max_TLB_num = TLB_num;
}
if (TLB_num < min_TLB_num)
{
min_TLB_num = TLB_num;
}
}
}
// 找到大于TLB的最大32倍数的值
unsigned long enum_up_bound = max_TLB_num / 32;
enum_up_bound = enum_up_bound * 32;
if (max_TLB_num % 32 != 0)
{
enum_up_bound = enum_up_bound + 32;
}
// 大于1024就按照1024来取
if (enum_up_bound > 1024)
{
enum_up_bound = 1024;
}
// 枚举的下界找小于最小TLB数量的32的倍数
unsigned long enum_low_bound = min_TLB_num / 32;
enum_low_bound = enum_low_bound * 32;
if (enum_low_bound < 32)
{
enum_low_bound = 32;
}
if (enum_low_bound > 1024)
{
enum_low_bound = 1024;
}
assert(enum_low_bound <= enum_up_bound);
// 步长按照上界除8之后小于这个值的32的倍数计算。最小不能高过32
unsigned long step_size = (enum_up_bound - enum_low_bound) / 8;
if (step_size % 32 == 0)
{
// 找到大于step_size最小32的倍数
step_size = (step_size / 32) * 32;
}
else
{
step_size = (step_size / 32 + 1) * 32;
}
// 如果这个值小于32, 那就至少32
if (step_size < 32)
{
step_size = 32;
}
cout << "find_best_param_of_direct_atom_template_warp_compress: thread_num_of_tblock setter param:" << " enum_low_bound:" << enum_low_bound << " , enum_up_bound:" << enum_up_bound << " , step_size:" << step_size << endl;
long tblock_num;
long thread_num_in_tblock;
// tblock数量和BLB数量相同
tblock_num = BLB_index->block_num;
if (tblock_num > get_config()["MAX_TBLOCK_NUM"].as_integer() - 1)
{
tblock_num = get_config()["MAX_TBLOCK_NUM"].as_integer() - 1;
}
// 登记参数调优器
param_enumerater_t param_setter;
// 性能影响差距不是很大
register_integer_independ_param_to_enumerater(¶m_setter, &thread_num_in_tblock, enum_low_bound, enum_up_bound, step_size);
// 模板的指针
direct_atom_template_warp_compress_t* target_template = (direct_atom_template_warp_compress_t*)(builder->template_vec[sub_matrix_id]);
long best_thread_num_in_block;
long best_tblock_num;
float best_time = 0;
float best_gflops = 0;
// 查看是不是第一个模板的枚举
bool is_first_enumerate = true;
// 是不是要提前退出
bool search_finished_by_strategy = false;
// 一个个枚举
while (set_param_combination_to_next(¶m_setter) == false)
{
// 将参数写到模板中
target_template->tblock_num = tblock_num;
target_template->thread_num_in_block = thread_num_in_tblock;
cout << "find_best_param_of_direct_atom_template_warp_compress: target_template->tblock_num:" << target_template->tblock_num << endl;
cout << "find_best_param_of_direct_atom_template_warp_compress: target_template->thread_num_in_block:" << target_template->thread_num_in_block << endl;
vector<int> sub_matrix_id_vec;
sub_matrix_id_vec.push_back(sub_matrix_id);
float exe_time = 0;
float exe_gflops = 0;
// 这里对模板的具体参数执行
bool is_success_exe = part_execute_code_builder(builder, sub_matrix_id_vec, exe_time, exe_gflops, string(get_config()["ROOT_PATH_STR"].as_string()) + "/cuda_code", string(get_config()["ROOT_PATH_STR"].as_string()) + "/data_source", is_first_enumerate, true);
// 如果不成功就跳过
if (is_success_exe == false)
{
continue;
}
vector<float> param_vec;
// 如果有数据集收集
if (data_set_collector != NULL)
{
// 已有的内容存在一些积累
assert(data_set_collector->accu_dense_param_strategy_type_vec.size() > 0);
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_compressed_sub_param_strategy_type_vec.size() > 0);
assert(data_set_collector->accu_compressed_sub_param_strategy_type_vec.size() == data_set_collector->accu_compressed_sub_graph_node_type_vec.size());
param_vec.push_back(target_template->thread_num_in_block);
param_vec.push_back(target_template->tblock_num);
param_vec.push_back(exe_gflops);
data_set_collector->insert_template_node_and_param_to_cur_item_and_add_to_dataset(DIRECT_ATOM_TEMPLATE_WARP_COMPRESS, param_vec);
}
is_first_enumerate = false;
// 当glops更大的时候,就替换
if (exe_gflops > best_gflops)
{
// 找到更好的参数了
best_gflops = exe_gflops;
best_time = exe_time;
best_tblock_num = tblock_num;
best_thread_num_in_block = thread_num_in_tblock;
}
// 如果有搜索策略,可能需要看看提前退出的问题
if (search_strategy_ptr != NULL)
{
if (continue_search(search_strategy_ptr, exe_gflops) == false)
{
search_finished_by_strategy = true;
}
}
if (search_finished_by_strategy == true)
{
break;
}
}
// 建立并返回一个模板节点
template_node_t return_node;
return_node.type = DIRECT_ATOM_TEMPLATE_WARP_COMPRESS;
direct_atom_template_warp_compress_node_param_t* param_ptr = new direct_atom_template_warp_compress_node_param_t();
param_ptr->tblock_num = best_tblock_num;
param_ptr->thread_num_in_block = best_thread_num_in_block;
return_node.template_param = param_ptr;
return_best_gflops = best_gflops;
return_best_time = best_time;
return return_node;
}
// 不带任何压缩的
template_node_t find_best_param_of_direct_atom_template(code_builder_t* builder, int sub_matrix_id, float& return_best_time, float& return_best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
assert(builder != NULL && sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->op_manager != NULL && builder->op_manager->matrix != NULL && sub_matrix_id < builder->template_vec.size());
assert(sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->template_type_vec[sub_matrix_id] == DIRECT_ATOM_TEMPLATE);
assert(builder->template_vec[sub_matrix_id] != NULL && builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
index_of_compress_block_t* BLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[2];
index_of_compress_block_t* WLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[3];
// 这个不执行调参,没用
return_best_time = 9999999999999;
return_best_gflops = 0;
template_node_t return_node;
return_node.type = DIRECT_ATOM_TEMPLATE;
direct_atom_template_node_param_t* param_ptr = new direct_atom_template_node_param_t();
return_node.template_param = param_ptr;
return return_node;
}
template_node_t find_best_param_of_direct_atom_total_warp_reduce_template(code_builder_t* builder, int sub_matrix_id, float& return_best_time, float& return_best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
assert(builder != NULL && sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->op_manager != NULL && builder->op_manager->matrix != NULL && sub_matrix_id < builder->template_vec.size());
assert(sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->template_type_vec[sub_matrix_id] == DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE);
assert(builder->template_vec[sub_matrix_id] != NULL && builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
index_of_compress_block_t* BLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[2];
index_of_compress_block_t* WLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[3];
// 有两个参数,一个是block数量和block内thread数量
long tblock_num;
long thread_num_in_block;
// 创建一个参数枚举器,只有thread_num_in_block的参数是需要调整的
param_enumerater_t param_setter;
// 性能影响差距不是很大
register_integer_independ_param_to_enumerater(¶m_setter, &thread_num_in_block, 32, 512, 64);
// 查看是不是第一个模板的枚举
bool is_first_enumerate = true;
// 记录最佳的时间和最佳的性能,以及对应的最佳参数
float best_time = 0;
float best_gflops = 0;
long best_tblock_num = 0;
long best_thread_num_in_block = 0;
direct_atom_total_warp_reduce_template_t* target_template = (direct_atom_total_warp_reduce_template_t *)builder->template_vec[sub_matrix_id];
bool search_finished_by_strategy = false;
while (set_param_combination_to_next(¶m_setter) == false)
{
// 查看WLB的数量
unsigned long WLB_num = WLB_index->block_num;
// 查看最多需要的线程数量,每个WLB需要32个线程
unsigned long TLB_num = WLB_num * 32;
// 计算线程块的数量,原则上线程数量可以稍微多一点
tblock_num = TLB_num / thread_num_in_block;
if (TLB_num % thread_num_in_block != 0)
{
tblock_num = tblock_num + 1;
}
if (tblock_num > get_config()["MAX_TBLOCK_NUM"].as_integer() - 1)
{
tblock_num = get_config()["MAX_TBLOCK_NUM"].as_integer() - 1;
}
// 将参数写到模板中
target_template->tblock_num = tblock_num;
target_template->thread_num_in_block = thread_num_in_block;
cout << "find_best_param_of_direct_atom_total_warp_reduce_template: target_template->tblock_num:" << target_template->tblock_num << endl;
cout << "find_best_param_of_direct_atom_total_warp_reduce_template: target_template->thread_num_in_block:" << target_template->thread_num_in_block << endl;
vector<int> sub_matrix_id_vec;
sub_matrix_id_vec.push_back(sub_matrix_id);
float exe_time = 0;
float exe_gflops = 0;
// 这里对模板的具体参数执行
bool is_success_exe = part_execute_code_builder(builder, sub_matrix_id_vec, exe_time, exe_gflops, string(get_config()["ROOT_PATH_STR"].as_string()) + "/cuda_code", string(get_config()["ROOT_PATH_STR"].as_string()) + "/data_source", is_first_enumerate, true);
// 如果不成功就跳过
if (is_success_exe == false)
{
continue;
}
vector<float> param_vec;
// 如果有数据集收集
if (data_set_collector != NULL)
{
// 已有的内容存在一些积累
assert(data_set_collector->accu_dense_param_strategy_type_vec.size() > 0);
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_compressed_sub_param_strategy_type_vec.size() > 0);
assert(data_set_collector->accu_compressed_sub_param_strategy_type_vec.size() == data_set_collector->accu_compressed_sub_graph_node_type_vec.size());
param_vec.push_back(target_template->thread_num_in_block);
param_vec.push_back(target_template->tblock_num);
param_vec.push_back(exe_gflops);
data_set_collector->insert_template_node_and_param_to_cur_item_and_add_to_dataset(DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE, param_vec);
}
is_first_enumerate = false;
// 当glops更大的时候,就替换
if (exe_gflops > best_gflops)
{
// 找到更好的参数了
best_gflops = exe_gflops;
best_time = exe_time;
best_tblock_num = tblock_num;
best_thread_num_in_block = thread_num_in_block;
}
// 如果有搜索策略,可能需要看看提前退出的问题
if (search_strategy_ptr != NULL)
{
if (continue_search(search_strategy_ptr, exe_gflops) == false)
{
search_finished_by_strategy = true;
}
}
if (search_finished_by_strategy == true)
{
break;
}
}
// 产生一个新的节点,来记录当前最优的参数组合
template_node_t return_node;
direct_atom_total_warp_reduce_template_node_param_t* param_ptr = new direct_atom_total_warp_reduce_template_node_param_t();
param_ptr->tblock_num = best_tblock_num;
param_ptr->thread_num_in_block = best_thread_num_in_block;
return_node.type = DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE;
return_node.template_param = param_ptr;
// 将最优性能传到外面
return_best_time = best_time;
return_best_gflops = best_gflops;
return return_node;
}
template_node_t find_best_param_of_shared_memory_long_row_template(code_builder_t* builder, int sub_matrix_id, float& return_best_time, float& return_best_gflops, search_strategy_t* search_strategy_ptr, shared_ptr<machine_learning_data_set_collector> data_set_collector)
{
assert(builder != NULL && sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->op_manager != NULL && builder->op_manager->matrix != NULL && sub_matrix_id < builder->template_vec.size());
assert(sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(builder->template_type_vec[sub_matrix_id] == SHARED_MEMORY_LONG_ROW_TEMPLATE);
assert(builder->template_vec[sub_matrix_id] != NULL && builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
index_of_compress_block_t* BLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[2];
index_of_compress_block_t* WLB_index = builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index[3];
// 有两个参数,一个是block数量和block内thread数量
long tblock_num;
long thread_num_in_block;
// tblock的和BLB的数量一致
tblock_num = BLB_index->block_num;
if (tblock_num > get_config()["MAX_TBLOCK_NUM"].as_integer() - 1)
{
tblock_num = get_config()["MAX_TBLOCK_NUM"].as_integer() - 1;
}
// 查看是不是第一个模板的枚举
bool is_first_enumerate = true;
// 记录最佳的时间和最佳的性能,以及对应的最佳参数
float best_time = 0;
float best_gflops = 0;
long best_tblock_num = 0;