forked from AnonymousRepo123/AlphaSparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unaligned_warp_reduce_same_TLB_size_op.cc
executable file
·1862 lines (1474 loc) · 93.2 KB
/
unaligned_warp_reduce_same_TLB_size_op.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 "unaligned_warp_reduce_same_TLB_size_op.hpp"
#include <string>
#include <sstream>
unaligned_warp_reduce_same_TLB_size_template_t *init_unaligned_warp_reduce_same_TLB_size_template(code_builder_t *builder, unsigned long dense_block_id)
{
assert(builder != NULL);
assert(builder != NULL);
assert(builder->op_manager != NULL);
assert(builder->op_manager->matrix != NULL);
sparse_struct_t *matrix = builder->op_manager->matrix;
assert(matrix->block_coor_table.item_arr.size() > dense_block_id);
compressed_block_t *compressed_block_view = matrix->block_coor_table.item_arr[dense_block_id]->compressed_block_ptr;
assert(compressed_block_view != NULL);
// 检查,warp和block层次都只有一个块,也就是放弃了分块
index_of_compress_block_t *global_row_index = compressed_block_view->read_index[0];
index_of_compress_block_t *global_col_index = compressed_block_view->read_index[1];
index_of_compress_block_t *block_level_index = compressed_block_view->read_index[2];
index_of_compress_block_t *warp_level_index = compressed_block_view->read_index[3];
index_of_compress_block_t *thread_level_index = compressed_block_view->read_index[4];
assert(global_row_index->type_of_index == ROW_INDEX);
assert(global_col_index->type_of_index == COL_INDEX);
assert(block_level_index->level_of_this_index == TBLOCK_LEVEL);
assert(warp_level_index->level_of_this_index == WRAP_LEVEL);
assert(thread_level_index->level_of_this_index == THREAD_LEVEL);
assert(global_row_index->max_row_index == matrix->block_coor_table.item_arr[dense_block_id]->max_dense_row_index);
assert(global_row_index->max_row_index == block_level_index->max_row_index);
assert(block_level_index->max_row_index == thread_level_index->max_row_index);
// 线程粒度的块有每个TLB所占行号的记录
assert(thread_level_index->row_number_of_block_arr != NULL);
assert(block_level_index->block_num == 1 && warp_level_index->block_num == 1);
// 线程粒度的块的大小,
unsigned long global_TLB_size = read_from_array_with_data_type(thread_level_index->coo_block_size_arr, thread_level_index->data_type_of_coo_block_size_arr, 0);
// 传入矩阵的已有COO格式,得到经过去除空行、padding到32*TLB_size大小矩阵。
// COO矩阵的三个索引
vector<unsigned long> dest_row_index_vec;
vector<unsigned long> dest_col_index_vec;
vector<double> dest_val_vec;
assert(compressed_block_view->read_index[0]->type_of_index == ROW_INDEX);
assert(compressed_block_view->read_index[1]->type_of_index == COL_INDEX);
// 重构COO的三个矩阵
fill_empty_and_padding_to_align_warp(compressed_block_view->read_index[0]->index_arr, compressed_block_view->read_index[1]->index_arr, compressed_block_view->val_arr,
compressed_block_view->read_index[0]->index_data_type, compressed_block_view->read_index[1]->index_data_type, compressed_block_view->val_data_type,
compressed_block_view->size, dest_row_index_vec, dest_col_index_vec, dest_val_vec, global_TLB_size);
// TLB的数量
assert(dest_col_index_vec.size() % global_TLB_size == 0);
unsigned long TLB_num = dest_col_index_vec.size() / global_TLB_size;
assert(TLB_num % 32 == 0);
unsigned long WLB_num = TLB_num / 32;
// 得到加和的起始位置的bool flag
vector<vector<bool>> sum_begin_bool_flag = get_sum_begin_bool_flag_of_each_thread(dest_row_index_vec, global_TLB_size);
assert(sum_begin_bool_flag.size() == TLB_num);
// 得到WLB的首行索引
vector<unsigned long> warp_level_block_first_row_vec = get_first_global_row_index_of_each_warp(dest_row_index_vec, global_TLB_size);
assert(warp_level_block_first_row_vec.size() == WLB_num);
// 归约偏移量的最大值
unsigned long max_tmp_result_reduce_offset = 0;
vector<unsigned long> tmp_result_reduce_offset_vec = get_tmp_result_reduce_offset_vec(sum_begin_bool_flag, &max_tmp_result_reduce_offset);
assert(tmp_result_reduce_offset_vec.size() == TLB_num);
// TLB的归约偏移量最大值
unsigned long max_relative_reduce_row_of_thread_level_block = 0;
vector<unsigned long> first_relative_reduce_row_of_thread_level_block_vec = get_first_relative_reduce_row_of_thread_level_block_vec(dest_row_index_vec, warp_level_block_first_row_vec, sum_begin_bool_flag, global_TLB_size, &max_relative_reduce_row_of_thread_level_block);
assert(first_relative_reduce_row_of_thread_level_block_vec.size() == TLB_num);
// store_bool_flag_of_sum_begin_to_file(sum_begin_bool_flag, "/home/duzhen/spmv_builder/data_source/test_result_4");
// print_arr_to_file_with_data_type(&(first_relative_reduce_row_of_thread_level_block_vec[0]), UNSIGNED_LONG, first_relative_reduce_row_of_thread_level_block_vec.size(), "/home/duzhen/spmv_builder/data_source/test_result_5");
// print_arr_to_file_with_data_type(&(tmp_result_reduce_offset_vec[0]), UNSIGNED_LONG, tmp_result_reduce_offset_vec.size(), "/home/duzhen/spmv_builder/data_source/test_result_3");
// cout << "max_relative_reduce_row_of_thread_level_block:" << max_relative_reduce_row_of_thread_level_block << endl;
// cout << "max_tmp_result_reduce_offset:" << max_tmp_result_reduce_offset << endl;
// 查看三个元数据所占的bit
int bit_num_of_relative_reduce_row_of_thread_level_block = get_max_bit_num_of_meta_data(max_relative_reduce_row_of_thread_level_block);
int bit_num_of_tmp_result_reduce_offset = get_max_bit_num_of_meta_data(max_tmp_result_reduce_offset);
int bit_num_of_sum_begin_flag = global_TLB_size;
// 对于最大是0的元数据,也需要占用一个bit
assert(bit_num_of_sum_begin_flag != 0);
if (bit_num_of_relative_reduce_row_of_thread_level_block == 0)
{
bit_num_of_relative_reduce_row_of_thread_level_block = 1;
}
if (bit_num_of_tmp_result_reduce_offset == 0)
{
bit_num_of_tmp_result_reduce_offset = 1;
}
// 这三个加起来要小于64,要不就没有办法使用这个模板
if (bit_num_of_relative_reduce_row_of_thread_level_block + bit_num_of_tmp_result_reduce_offset + bit_num_of_sum_begin_flag > 64)
{
cout << "too large meta data size:" << bit_num_of_relative_reduce_row_of_thread_level_block + bit_num_of_tmp_result_reduce_offset + bit_num_of_sum_begin_flag << ", is not supported" << endl;
assert(false);
}
// cout << max_tmp_result_reduce_offset << endl;
// cout << bit_num_of_tmp_result_reduce_offset << endl;
// cout << convert_meta_data_to_bit_flag_string(bit_num_of_tmp_result_reduce_offset) << endl;
// cout << convert_meta_data_to_bit_flag_string((unsigned long)combine_meta_data_to_unsigned_int(sum_begin_bool_flag[1], tmp_result_reduce_offset_vec[1], first_relative_reduce_row_of_thread_level_block_vec[1], bit_num_of_sum_begin_flag, bit_num_of_tmp_result_reduce_offset, bit_num_of_relative_reduce_row_of_thread_level_block)) << endl;
// exit(-1);
// 申请模板的实例,创建一个模板
unaligned_warp_reduce_same_TLB_size_template_t* output_template = new unaligned_warp_reduce_same_TLB_size_template_t();
output_template->dense_block_index = dense_block_id;
output_template->matrix = matrix;
output_template->kernal_first_row_index = matrix->block_coor_table.item_arr[dense_block_id]->min_dense_row_index;
output_template->kernal_first_col_index = matrix->block_coor_table.item_arr[dense_block_id]->min_dense_col_index;
// 找出最合适的数据结构
data_type combine_meta_data_type = find_most_suitable_data_type_by_bit_num(bit_num_of_relative_reduce_row_of_thread_level_block + bit_num_of_tmp_result_reduce_offset + bit_num_of_sum_begin_flag);
// 数据结构对应的bit占用
int combine_meta_bit_num = bit_num_of_data_type(combine_meta_data_type);
output_template->bit_num_of_thread_level_combine_meta = combine_meta_bit_num;
output_template->bit_num_of_sum_begin_bit_flag = bit_num_of_sum_begin_flag;
output_template->bit_num_of_first_relative_reduce_row_of_thread_level_block = bit_num_of_relative_reduce_row_of_thread_level_block;
output_template->bit_num_of_tmp_result_reduce_offset_of_thread_level_block = bit_num_of_tmp_result_reduce_offset;
// cout << max_relative_reduce_row_of_thread_level_block << " , " << max_tmp_result_reduce_offset << endl;
// 如果出现过列分块,那就全部强制原子加
if (matrix->block_coor_table.item_arr[dense_block_id]->min_dense_col_index == 0 && matrix->block_coor_table.item_arr[dense_block_id]->max_dense_col_index == matrix->dense_col_number - 1)
{
// 稠密子块之间没有共享的行
}
else
{
output_template->is_all_force_atom_add = true;
}
// 全局的TLB的大小
output_template->global_thread_level_block_size = global_TLB_size;
// 用一个数组来存储我们warp的首行索引
output_template->data_type_of_global_first_row_index_of_warp_level_block = global_row_index->index_data_type;
output_template->size_of_global_first_row_index_of_warp_level_block = warp_level_block_first_row_vec.size();
// 申请一个数组来存储首行索引
output_template->global_first_row_index_of_warp_level_block = malloc_arr(output_template->size_of_global_first_row_index_of_warp_level_block, output_template->data_type_of_global_first_row_index_of_warp_level_block);
copy_unsigned_long_arr_to_others(&(warp_level_block_first_row_vec[0]), output_template->global_first_row_index_of_warp_level_block, output_template->data_type_of_global_first_row_index_of_warp_level_block, output_template->size_of_global_first_row_index_of_warp_level_block);
// 加和起始位置二维bool global
output_template->sum_bool_flag_of_sum_begin = sum_begin_bool_flag;
// TLB首个归约位置的相对行号
output_template->data_type_of_first_relative_reduce_row_of_thread_level_block = find_most_suitable_data_type(max_relative_reduce_row_of_thread_level_block);
output_template->size_of_first_relative_reduce_row_of_thread_level_block = first_relative_reduce_row_of_thread_level_block_vec.size();
output_template->first_relative_reduce_row_of_thread_level_block = malloc_arr(output_template->size_of_first_relative_reduce_row_of_thread_level_block, output_template->data_type_of_first_relative_reduce_row_of_thread_level_block);
// 拷贝
copy_unsigned_long_arr_to_others(&(first_relative_reduce_row_of_thread_level_block_vec[0]), output_template->first_relative_reduce_row_of_thread_level_block, output_template->data_type_of_first_relative_reduce_row_of_thread_level_block, output_template->size_of_first_relative_reduce_row_of_thread_level_block);
// TLB之间的归约偏移量
output_template->data_type_of_tmp_result_reduce_offset_of_thread_level_block = find_most_suitable_data_type(max_tmp_result_reduce_offset);
output_template->size_of_tmp_result_reduce_offset_of_thread_level_block = tmp_result_reduce_offset_vec.size();
output_template->tmp_result_reduce_offset_of_thread_level_block = malloc_arr(output_template->size_of_tmp_result_reduce_offset_of_thread_level_block, output_template->data_type_of_tmp_result_reduce_offset_of_thread_level_block);
// 拷贝
copy_unsigned_long_arr_to_others(&(tmp_result_reduce_offset_vec[0]), output_template->tmp_result_reduce_offset_of_thread_level_block, output_template->data_type_of_tmp_result_reduce_offset_of_thread_level_block, output_template->size_of_tmp_result_reduce_offset_of_thread_level_block);
// 三个数组的非零元数量完全相等
assert(output_template->size_of_first_relative_reduce_row_of_thread_level_block == output_template->size_of_tmp_result_reduce_offset_of_thread_level_block);
assert(output_template->size_of_tmp_result_reduce_offset_of_thread_level_block == output_template->sum_bool_flag_of_sum_begin.size());
assert(output_template->size_of_tmp_result_reduce_offset_of_thread_level_block == TLB_num);
// 申请合并之后的元数据空间
output_template->data_type_of_combine_meta_of_thread_level_block = combine_meta_data_type;
output_template->size_of_combine_meta_of_thread_level_block = TLB_num;
output_template->combine_meta_of_thread_level_block = malloc_arr(output_template->size_of_combine_meta_of_thread_level_block, output_template->data_type_of_combine_meta_of_thread_level_block);
// 执行合并,将TLB粒度的所有元数据合并起来
for (unsigned long TLB_id = 0; TLB_id < TLB_num; TLB_id++)
{
vector<bool> bool_flag = output_template->sum_bool_flag_of_sum_begin[TLB_id];
unsigned long row_offset = read_from_array_with_data_type(output_template->first_relative_reduce_row_of_thread_level_block, output_template->data_type_of_first_relative_reduce_row_of_thread_level_block, TLB_id);
unsigned long reduce_offset = read_from_array_with_data_type(output_template->tmp_result_reduce_offset_of_thread_level_block, output_template->data_type_of_tmp_result_reduce_offset_of_thread_level_block, TLB_id);
if (combine_meta_data_type == UNSIGNED_LONG)
{
unsigned long combine_meta = combine_meta_data_to_unsigned_long(bool_flag, reduce_offset, row_offset, bit_num_of_sum_begin_flag, bit_num_of_tmp_result_reduce_offset, bit_num_of_relative_reduce_row_of_thread_level_block);
write_to_array_with_data_type(output_template->combine_meta_of_thread_level_block, combine_meta_data_type, TLB_id, combine_meta);
continue;
}
if (combine_meta_data_type == UNSIGNED_INT)
{
unsigned int combine_meta = combine_meta_data_to_unsigned_int(bool_flag, reduce_offset, row_offset, bit_num_of_sum_begin_flag, bit_num_of_tmp_result_reduce_offset, bit_num_of_relative_reduce_row_of_thread_level_block);
write_to_array_with_data_type(output_template->combine_meta_of_thread_level_block, combine_meta_data_type, TLB_id, combine_meta);
continue;
}
if (combine_meta_data_type == UNSIGNED_SHORT)
{
unsigned short combine_meta = combine_meta_data_to_unsigned_short(bool_flag, reduce_offset, row_offset, bit_num_of_sum_begin_flag, bit_num_of_tmp_result_reduce_offset, bit_num_of_relative_reduce_row_of_thread_level_block);
write_to_array_with_data_type(output_template->combine_meta_of_thread_level_block, combine_meta_data_type, TLB_id, combine_meta);
continue;
}
if (combine_meta_data_type == UNSIGNED_CHAR)
{
unsigned char combine_meta = combine_meta_data_to_unsigned_char(bool_flag, reduce_offset, row_offset, bit_num_of_sum_begin_flag, bit_num_of_tmp_result_reduce_offset, bit_num_of_relative_reduce_row_of_thread_level_block);
write_to_array_with_data_type(output_template->combine_meta_of_thread_level_block, combine_meta_data_type, TLB_id, combine_meta);
continue;
}
cout << "combine meta data type is not supported" << endl;
assert(false);
}
// 将合并之后处理的结果放到文件中
// write_combine_meta_data_to_file(output_template->combine_meta_of_thread_level_block, output_template->data_type_of_combine_meta_of_thread_level_block, output_template->size_of_combine_meta_of_thread_level_block, "/home/duzhen/spmv_builder/data_source/test_result_3");
// exit(-1);
// cout << convert_meta_data_to_bit_flag_string_with_data_type(read_from_array_with_data_type(output_template->combine_meta_of_thread_level_block, output_template->data_type_of_combine_meta_of_thread_level_block, 2), combine_meta_data_type) << endl;
// 接下里处理排序产生的数组
// 最后给出排序索引类型和具体的数组
if (compressed_block_view->y_write_index.size() > 0)
{
// 在子块内排序了
assert(compressed_block_view->is_sorted == true && builder->sub_block_sort_type_vec[dense_block_id] == SUB_BLOCK_SORT && matrix->is_sorted == false);
output_template->global_sort_index = false;
output_template->local_sort_index = true;
// 拷贝
output_template->data_type_of_row_index_before_sort = compressed_block_view->y_write_index[0]->index_data_type;
output_template->row_index_before_sort = compressed_block_view->y_write_index[0]->index_arr;
output_template->size_of_row_index_before_sort = compressed_block_view->y_write_index[0]->length;
}
else if (matrix->sorted_row_index != NULL)
{
cout << "have global sort" << endl;
// 在全局范围内有排序
assert(compressed_block_view->is_sorted == false && matrix->is_sorted == true && builder->sub_block_sort_type_vec[dense_block_id] == GLOBAL_SORT);
output_template->global_sort_index = true;
output_template->local_sort_index = false;
// 拷贝
output_template->data_type_of_row_index_before_sort = matrix->data_type_of_sorted_row_index;
output_template->row_index_before_sort = matrix->sorted_row_index;
output_template->size_of_row_index_before_sort = matrix->dense_row_number;
}
// 执行交错存储,先申请两个空间
output_template->data_type_of_col_index_arr = global_col_index->index_data_type;
output_template->size_of_col_index_arr = dest_col_index_vec.size();
output_template->col_index_arr = malloc_arr(output_template->size_of_col_index_arr, output_template->data_type_of_col_index_arr);
output_template->data_type_of_val_arr = matrix->val_data_type;
output_template->size_of_val_arr = dest_val_vec.size();
output_template->val_arr = malloc_arr(output_template->size_of_val_arr, output_template->data_type_of_val_arr);
// 遍历所有padding之后的非零元
assert(dest_col_index_vec.size() == dest_val_vec.size());
for (unsigned long nz_id = 0; nz_id < dest_val_vec.size(); nz_id++)
{
// 查看当前非零元在线程中的索引
unsigned long cur_col_index = dest_col_index_vec[nz_id];
double cur_val = dest_val_vec[nz_id];
// 当前非零元所在的TLB
unsigned long TLB_id_of_nz = nz_id / global_TLB_size;
// 当前非零元所在的WLB
unsigned long WLB_id_of_nz = TLB_id_of_nz / 32;
// 当前非零元所在的TLB内的相对索引
unsigned long nz_id_in_TLB = nz_id % global_TLB_size;
// 当前TLB在WLB中的索引
unsigned long TLB_id_in_WLB = TLB_id_of_nz % 32;
// 当前非零元的目标位置
unsigned long dest_nz_id = WLB_id_of_nz * (32 * global_TLB_size) + TLB_id_in_WLB + nz_id_in_TLB * 32;
assert(dest_nz_id < dest_val_vec.size());
// 将非零元写到对应位置
write_to_array_with_data_type(output_template->col_index_arr, output_template->data_type_of_col_index_arr, dest_nz_id, cur_col_index);
write_double_to_array_with_data_type(output_template->val_arr, output_template->data_type_of_val_arr, dest_nz_id, cur_val);
}
return output_template;
}
bool is_supported_by_unaligned_warp_reduce_same_TLB_size_template(sparse_struct_t* matrix, unsigned long dense_block_id)
{
compressed_block_t *compressed_block_view = matrix->block_coor_table.item_arr[dense_block_id]->compressed_block_ptr;
assert(compressed_block_view != NULL);
// 检查,warp和block层次都只有一个块,也就是放弃了分块
index_of_compress_block_t *global_row_index = compressed_block_view->read_index[0];
index_of_compress_block_t *global_col_index = compressed_block_view->read_index[1];
index_of_compress_block_t *block_level_index = compressed_block_view->read_index[2];
index_of_compress_block_t *warp_level_index = compressed_block_view->read_index[3];
index_of_compress_block_t *thread_level_index = compressed_block_view->read_index[4];
assert(global_row_index->type_of_index == ROW_INDEX);
assert(global_col_index->type_of_index == COL_INDEX);
assert(block_level_index->level_of_this_index == TBLOCK_LEVEL);
assert(warp_level_index->level_of_this_index == WRAP_LEVEL);
assert(thread_level_index->level_of_this_index == THREAD_LEVEL);
assert(global_row_index->max_row_index == matrix->block_coor_table.item_arr[dense_block_id]->max_dense_row_index);
assert(global_row_index->max_row_index == block_level_index->max_row_index);
assert(block_level_index->max_row_index == thread_level_index->max_row_index);
if (global_row_index->max_row_index != matrix->block_coor_table.item_arr[dense_block_id]->max_dense_row_index)
{
assert(global_row_index->max_row_index > matrix->block_coor_table.item_arr[dense_block_id]->max_dense_row_index);
return false;
}
// 如果thread的压缩类型不是其他就false
if (thread_level_index->index_compressed_type != NO_INDEX)
{
return false;
}
// 如果没有TLB所占行的数量的记录,就false
if (thread_level_index->row_number_of_block_arr == NULL)
{
return false;
}
if (block_level_index->block_num != 1)
{
return false;
}
if (warp_level_index->block_num != 1)
{
return false;
}
unsigned long global_TLB_size = read_from_array_with_data_type(thread_level_index->coo_block_size_arr, thread_level_index->data_type_of_coo_block_size_arr, 0);
// padding之后算元数据,然后看看元数据超不超
// 传入矩阵的已有COO格式,得到经过去除空行、padding到32*TLB_size大小矩阵。
// COO矩阵的三个索引
vector<unsigned long> dest_row_index_vec;
vector<unsigned long> dest_col_index_vec;
vector<double> dest_val_vec;
assert(compressed_block_view->read_index[0]->type_of_index == ROW_INDEX);
assert(compressed_block_view->read_index[1]->type_of_index == COL_INDEX);
// 重构COO的三个矩阵
fill_empty_and_padding_to_align_warp(compressed_block_view->read_index[0]->index_arr, compressed_block_view->read_index[1]->index_arr, compressed_block_view->val_arr,
compressed_block_view->read_index[0]->index_data_type, compressed_block_view->read_index[1]->index_data_type, compressed_block_view->val_data_type,
compressed_block_view->size, dest_row_index_vec, dest_col_index_vec, dest_val_vec, global_TLB_size);
// TLB的数量
assert(dest_col_index_vec.size() % global_TLB_size == 0);
unsigned long TLB_num = dest_col_index_vec.size() / global_TLB_size;
assert(TLB_num % 32 == 0);
unsigned long WLB_num = TLB_num / 32;
// 得到加和的起始位置的bool flag
vector<vector<bool>> sum_begin_bool_flag = get_sum_begin_bool_flag_of_each_thread(dest_row_index_vec, global_TLB_size);
assert(sum_begin_bool_flag.size() == TLB_num);
// 得到WLB的首行索引
vector<unsigned long> warp_level_block_first_row_vec = get_first_global_row_index_of_each_warp(dest_row_index_vec, global_TLB_size);
assert(warp_level_block_first_row_vec.size() == WLB_num);
// 归约偏移量的最大值
unsigned long max_tmp_result_reduce_offset = 0;
vector<unsigned long> tmp_result_reduce_offset_vec = get_tmp_result_reduce_offset_vec(sum_begin_bool_flag, &max_tmp_result_reduce_offset);
assert(tmp_result_reduce_offset_vec.size() == TLB_num);
// TLB的归约偏移量最大值
unsigned long max_relative_reduce_row_of_thread_level_block = 0;
vector<unsigned long> first_relative_reduce_row_of_thread_level_block_vec = get_first_relative_reduce_row_of_thread_level_block_vec(dest_row_index_vec, warp_level_block_first_row_vec, sum_begin_bool_flag, global_TLB_size, &max_relative_reduce_row_of_thread_level_block);
assert(first_relative_reduce_row_of_thread_level_block_vec.size() == TLB_num);
// 查看三个元数据所占的bit
int bit_num_of_relative_reduce_row_of_thread_level_block = get_max_bit_num_of_meta_data(max_relative_reduce_row_of_thread_level_block);
int bit_num_of_tmp_result_reduce_offset = get_max_bit_num_of_meta_data(max_tmp_result_reduce_offset);
int bit_num_of_sum_begin_flag = global_TLB_size;
// 对于最大是0的元数据,也需要占用一个bit
assert(bit_num_of_sum_begin_flag != 0);
if (bit_num_of_relative_reduce_row_of_thread_level_block == 0)
{
bit_num_of_relative_reduce_row_of_thread_level_block = 1;
}
if (bit_num_of_tmp_result_reduce_offset == 0)
{
bit_num_of_tmp_result_reduce_offset = 1;
}
// 这三个加起来要小于64,要不就没有办法使用这个模板
if (bit_num_of_relative_reduce_row_of_thread_level_block + bit_num_of_tmp_result_reduce_offset + bit_num_of_sum_begin_flag > 64)
{
// cout << "too large meta data size:" << bit_num_of_relative_reduce_row_of_thread_level_block + bit_num_of_tmp_result_reduce_offset + bit_num_of_sum_begin_flag << ", is not supported" << endl;
return false;
}
//
return true;
}
bool is_supported_by_unaligned_warp_reduce_same_TLB_size_template(code_builder_t* builder, unsigned long dense_block_id)
{
assert(builder != NULL);
assert(builder != NULL);
assert(builder->op_manager != NULL);
assert(builder->op_manager->matrix != NULL);
sparse_struct_t *matrix = builder->op_manager->matrix;
assert(matrix->block_coor_table.item_arr.size() > dense_block_id);
return is_supported_by_unaligned_warp_reduce_same_TLB_size_template(matrix, dense_block_id);
}
void fill_empty_and_padding_to_align_warp(void *source_row_index_arr, void *source_col_index_arr, void *source_val_arr, data_type data_type_of_row_index_arr,
data_type data_type_of_col_index_arr, data_type data_type_of_val_arr, unsigned long nnz, vector<unsigned long> &dest_row_index_vec,
vector<unsigned long> &dest_col_index_vec, vector<double> &dest_val_vec, unsigned long global_thread_level_block_size)
{
assert(data_type_of_row_index_arr == UNSIGNED_CHAR || data_type_of_row_index_arr == UNSIGNED_SHORT || data_type_of_row_index_arr == UNSIGNED_INT || data_type_of_row_index_arr == UNSIGNED_LONG);
assert(data_type_of_col_index_arr == UNSIGNED_CHAR || data_type_of_col_index_arr == UNSIGNED_SHORT || data_type_of_col_index_arr == UNSIGNED_INT || data_type_of_col_index_arr == UNSIGNED_LONG);
assert(data_type_of_val_arr == DOUBLE || data_type_of_val_arr == FLOAT);
assert(source_row_index_arr != NULL && source_col_index_arr != NULL && source_val_arr != NULL);
assert(dest_row_index_vec.size() == 0 && dest_col_index_vec.size() == 0 && dest_val_vec.size() == 0);
// 上一行的行号
unsigned long last_row_index = 0;
// 首先先执行空行的补齐,将已经没有空行的结果放在对应的几个vector中
for (unsigned long nz_id = 0; nz_id < nnz; nz_id++)
{
// 读出当前行的行号
unsigned long cur_row_index = read_from_array_with_data_type(source_row_index_arr, data_type_of_row_index_arr, nz_id);
unsigned long cur_col_index = read_from_array_with_data_type(source_col_index_arr, data_type_of_col_index_arr, nz_id);
double cur_val = read_double_from_array_with_data_type(source_val_arr, data_type_of_val_arr, nz_id);
// 如果一上来就是空行,特殊处理,因为需要从0行开始补
if (nz_id == 0 && cur_row_index > 0)
{
for (unsigned long row_id = 0; row_id < cur_row_index; row_id++)
{
dest_row_index_vec.push_back(row_id);
dest_col_index_vec.push_back(0);
dest_val_vec.push_back(0);
}
}
else if (cur_row_index > last_row_index + 1)
{
// 如果是在中途出现的空行
for (unsigned long row_id = last_row_index + 1; row_id < cur_row_index; row_id++)
{
dest_row_index_vec.push_back(row_id);
dest_col_index_vec.push_back(0);
dest_val_vec.push_back(0);
}
}
// 空行补完了写本体
dest_row_index_vec.push_back(cur_row_index);
dest_col_index_vec.push_back(cur_col_index);
dest_val_vec.push_back(cur_val);
last_row_index = cur_row_index;
}
// 空行补完了,执行padding到对齐的大小
nnz = dest_row_index_vec.size();
unsigned warp_level_block_nnz = 32 * global_thread_level_block_size;
unsigned long target_nnz = nnz;
if (nnz % warp_level_block_nnz != 0)
{
target_nnz = (nnz / warp_level_block_nnz + 1) * warp_level_block_nnz;
}
assert(target_nnz >= nnz);
// 需要增加的非零元
unsigned long added_nnz = target_nnz - nnz;
for (unsigned long added_nz_id = 0; added_nz_id < added_nnz; added_nz_id++)
{
// 新加的非零元坐标和矩阵的最后一个非零元坐标是一样的
dest_row_index_vec.push_back(dest_row_index_vec[dest_row_index_vec.size() - 1]);
dest_col_index_vec.push_back(dest_col_index_vec[dest_col_index_vec.size() - 1]);
dest_val_vec.push_back(0);
}
// 最后一下检查
assert(dest_col_index_vec.size() == dest_row_index_vec.size() && dest_row_index_vec.size() == dest_val_vec.size());
assert(dest_row_index_vec.size() % (32 * global_thread_level_block_size) == 0);
}
void store_template_data(unaligned_warp_reduce_same_TLB_size_template_t *output_template, string output_dir, bool force_not_share_global_sort_index)
{
assert(output_template != NULL);
srand(time(0));
unsigned long matrix_id = rand() + time(0) % 1000;
// 写这个模板所需要数据的文件夹名称
output_dir = output_dir + "/" + to_string(matrix_id) + "_" + to_string(get_config()["DEFAULT_DEVICE_ID"].as_integer());
// 创建这个文件夹
system(("mkdir " + output_dir).c_str());
// 不压缩
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
// 压缩完之后写
print_arr_to_file_with_data_type(output_template->global_first_row_index_of_warp_level_block, output_template->data_type_of_global_first_row_index_of_warp_level_block, output_template->size_of_global_first_row_index_of_warp_level_block, output_dir + "/global_first_row_index_of_warp_level_block");
}
// 经过压缩的线程粒度的元数据
assert(output_template->combine_meta_of_thread_level_block != NULL);
print_arr_to_file_with_data_type(output_template->combine_meta_of_thread_level_block, output_template->data_type_of_combine_meta_of_thread_level_block, output_template->size_of_combine_meta_of_thread_level_block, output_dir + "/combine_meta_of_thread_level_block");
// 排序相关的数据
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->row_index_before_sort != NULL)
{
assert(output_template->row_index_before_sort != NULL);
// 如果是全局排序,只有第一个才需要存排序之后的行索引
if (output_template->local_sort_index == true)
{
assert(output_template->global_sort_index == false);
print_arr_to_file_with_data_type(output_template->row_index_before_sort, output_template->data_type_of_row_index_before_sort, output_template->size_of_row_index_before_sort, output_dir + "/row_index_before_sort");
}
else if (output_template->global_sort_index == true && (output_template->dense_block_index == 0 || force_not_share_global_sort_index == true))
{
assert(output_template->local_sort_index == false);
print_arr_to_file_with_data_type(output_template->row_index_before_sort, output_template->data_type_of_row_index_before_sort, output_template->size_of_row_index_before_sort, output_dir + "/row_index_before_sort");
}
}
// 值
assert(output_template->val_arr != NULL);
print_arr_to_file_with_data_type(output_template->val_arr, output_template->data_type_of_val_arr, output_template->size_of_val_arr, output_dir + "/val_arr");
// 列
assert(output_template->col_index_arr != NULL);
print_arr_to_file_with_data_type(output_template->col_index_arr, output_template->data_type_of_col_index_arr, output_template->size_of_col_index_arr, output_dir + "/col_index_arr");
output_template->hash_of_this_template = matrix_id;
}
string code_of_template_data_struct(unaligned_warp_reduce_same_TLB_size_template_t *output_template, unsigned long dense_block_id)
{
assert(output_template != NULL);
// 创建一个数据结构
string return_str = "typedef struct compressed_dense_block_" + to_string(dense_block_id) + "\n{\n";
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_global_first_row_index_of_warp_level_block, code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block"));
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block") + " = " + to_string(output_template->size_of_global_first_row_index_of_warp_level_block) + ";\n";
}
return_str = return_str + "\n";
assert(output_template->combine_meta_of_thread_level_block != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_combine_meta_of_thread_level_block, code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block"));
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block") + " = " + to_string(output_template->size_of_combine_meta_of_thread_level_block) + ";\n";
return_str = return_str + "\n";
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->row_index_before_sort != NULL)
{
assert(output_template->row_index_before_sort != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_row_index_before_sort, code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"));
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + " = " + to_string(output_template->size_of_row_index_before_sort) + ";\n";
}
// 值和列索引
return_str = return_str + "\n";
assert(output_template->val_arr != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_val_arr, code_of_arr_var_name(dense_block_id, -1, "val_arr"));
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(dense_block_id, -1, "val_arr") + " = " + to_string(output_template->size_of_val_arr) + ";\n";
return_str = return_str + "\n";
assert(output_template->col_index_arr != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_col_index_arr, code_of_arr_var_name(dense_block_id, -1, "col_index_arr"));
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr") + " = " + to_string(output_template->size_of_col_index_arr) + ";\n";
return_str = return_str + "}";
return_str = return_str + "compressed_dense_block_" + to_string(dense_block_id) + "_t;\n";
return return_str;
}
string code_of_read_template_data_from_file_func_define(unaligned_warp_reduce_same_TLB_size_template_t *output_template, unsigned long dense_block_id, bool force_not_share_global_sort_index)
{
assert(output_template != NULL);
string return_str = "compressed_dense_block_" + to_string(dense_block_id) + "_t* read_dense_block_" + to_string(dense_block_id) + "_from_file(string file_name_prefix)\n{\n";
return_str = return_str + "compressed_dense_block_" + to_string(dense_block_id) + "_t *template_data = new " + "compressed_dense_block_" + to_string(dense_block_id) + "_t();\n";
// warp首行行号
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block") + " = (" + code_of_data_type(output_template->data_type_of_global_first_row_index_of_warp_level_block) + " *)";
return_str = return_str + "read_arr_from_file_with_data_type(template_data->size_of_" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block") + ", " + convert_data_type_to_string(output_template->data_type_of_global_first_row_index_of_warp_level_block) + ", ";
// 要读的文件名
return_str = return_str + "file_name_prefix + \"/global_first_row_index_of_warp_level_block\");\n";
}
return_str = return_str + "\n";
assert(output_template->combine_meta_of_thread_level_block != NULL);
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block") + " = (" + code_of_data_type(output_template->data_type_of_combine_meta_of_thread_level_block) + " *)";
return_str = return_str + "read_arr_from_file_with_data_type(template_data->size_of_" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block") + ", " + convert_data_type_to_string(output_template->data_type_of_combine_meta_of_thread_level_block) + ", ";
// 要读的文件名
return_str = return_str + "file_name_prefix + \"/combine_meta_of_thread_level_block\");\n";
return_str = return_str + "\n";
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->row_index_before_sort != NULL)
{
// 如果有全局的排序索引,只有0号块需要存储
if (output_template->global_sort_index == true)
{
if (dense_block_id == 0 || force_not_share_global_sort_index == true)
{
// 存一个全局的排序
assert(output_template->row_index_before_sort != NULL);
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + " = (" + code_of_data_type(output_template->data_type_of_row_index_before_sort) + " *)";
return_str = return_str + "read_arr_from_file_with_data_type(template_data->size_of_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + ", " + convert_data_type_to_string(output_template->data_type_of_row_index_before_sort) + ", ";
// 要读的文件名
return_str = return_str + "file_name_prefix + \"/row_index_before_sort\");\n";
}
else
{
// 如果已经有了就直接拷贝全局的排序
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + " = NULL;\n";
}
}
else if (output_template->local_sort_index == true)
{
assert(output_template->row_index_before_sort != NULL);
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + " = (" + code_of_data_type(output_template->data_type_of_row_index_before_sort) + " *)";
return_str = return_str + "read_arr_from_file_with_data_type(template_data->size_of_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + ", " + convert_data_type_to_string(output_template->data_type_of_row_index_before_sort) + ", ";
// 要读的文件名
return_str = return_str + "file_name_prefix + \"/row_index_before_sort\");\n";
}
else
{
cout << "error" << endl;
assert(false);
}
}
return_str = return_str + "\n";
assert(output_template->val_arr != NULL);
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "val_arr") + " = (" + code_of_data_type(output_template->data_type_of_val_arr) + " *)";
return_str = return_str + "read_arr_from_file_with_data_type(template_data->size_of_" + code_of_arr_var_name(dense_block_id, -1, "val_arr") + ", " + convert_data_type_to_string(output_template->data_type_of_val_arr) + ", ";
// 要读的文件名
return_str = return_str + "file_name_prefix + \"/val_arr\");\n";
return_str = return_str + "\n";
assert(output_template->col_index_arr != NULL);
return_str = return_str + "template_data->" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr") + " = (" + code_of_data_type(output_template->data_type_of_col_index_arr) + " *)";
return_str = return_str + "read_arr_from_file_with_data_type(template_data->size_of_" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr") + ", " + convert_data_type_to_string(output_template->data_type_of_col_index_arr) + ", ";
// 要读的文件名
return_str = return_str + "file_name_prefix + \"/col_index_arr\");\n";
return_str = return_str + "return template_data;\n";
return_str = return_str + "}\n";
return return_str;
}
// 将模板的数据从CPU拷贝到GPU
string code_of_write_template_data_to_gpu(unaligned_warp_reduce_same_TLB_size_template_t *output_template, unsigned long dense_block_id, bool force_not_share_global_sort_index)
{
assert(output_template != NULL);
string template_data_name = "dense_block_" + to_string(dense_block_id) + "_template_data";
string return_str = "compressed_dense_block_" + to_string(dense_block_id) + "_t *" + template_data_name + " = read_dense_block_" + to_string(dense_block_id) + "_from_file(" + "\"" + string(get_config()["ROOT_PATH_STR"].as_string()) + "/data_source/" + to_string(output_template->hash_of_this_template) + "_" + to_string(get_config()["DEFAULT_DEVICE_ID"].as_integer()) + "\");\n\n";
// 全局排序的数组取一个特殊的名字,并且只处理一次,剩下的从这里拷贝即可
if (output_template->global_sort_index == true)
{
if (output_template->dense_block_index == 0 && force_not_share_global_sort_index == false)
{
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_row_index_before_sort, "device_global_sort_index");
// 申请、拷贝、一气呵成
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_row_index_before_sort, to_string(output_template->size_of_row_index_before_sort), "device_global_sort_index");
return_str = return_str + code_line_of_cuda_memcpy("device_global_sort_index", template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"), output_template->data_type_of_row_index_before_sort, to_string(output_template->size_of_row_index_before_sort), "cudaMemcpyHostToDevice") + "\n";
}
}
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_global_first_row_index_of_warp_level_block, "device_" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block"));
}
// 行顺序数组的声明
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->row_index_before_sort != NULL)
{
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_row_index_before_sort, "device_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"));
}
assert(output_template->combine_meta_of_thread_level_block != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_combine_meta_of_thread_level_block, "device_" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block"));
assert(output_template->val_arr != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_val_arr, "device_" + code_of_arr_var_name(dense_block_id, -1, "val_arr"));
assert(output_template->col_index_arr != NULL);
return_str = return_str + code_line_of_pointer_define(output_template->data_type_of_col_index_arr, "device_" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr"));
return_str = return_str + "\n";
// 申请数组的代码
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_global_first_row_index_of_warp_level_block, to_string(output_template->size_of_global_first_row_index_of_warp_level_block), "device_" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block"));
// 拷贝
return_str = return_str + code_line_of_cuda_memcpy("device_" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block"), template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "global_first_row_index_of_warp_level_block"), output_template->data_type_of_global_first_row_index_of_warp_level_block, to_string(output_template->size_of_global_first_row_index_of_warp_level_block), "cudaMemcpyHostToDevice") + "\n";
}
assert(output_template->combine_meta_of_thread_level_block != NULL);
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_combine_meta_of_thread_level_block, to_string(output_template->size_of_combine_meta_of_thread_level_block), "device_" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block"));
// 拷贝
return_str = return_str + code_line_of_cuda_memcpy("device_" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block"), template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "combine_meta_of_thread_level_block"), output_template->data_type_of_combine_meta_of_thread_level_block, to_string(output_template->size_of_combine_meta_of_thread_level_block), "cudaMemcpyHostToDevice") + "\n";
// 如果是全局的就直接赋值
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->global_sort_index == true)
{
assert(output_template->local_sort_index == false);
// 如果不分享就从内disk中读
if (force_not_share_global_sort_index == true)
{
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_row_index_before_sort, to_string(output_template->size_of_row_index_before_sort), "device_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"));
// 拷贝
return_str = return_str + code_line_of_cuda_memcpy("device_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"), template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"), output_template->data_type_of_row_index_before_sort, to_string(output_template->size_of_row_index_before_sort), "cudaMemcpyHostToDevice") + "\n";
}
else
{
return_str = return_str + "device_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort") + "=" + "device_global_sort_index;\n";
}
}
// 如果是局部的就拷贝
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->local_sort_index == true)
{
assert(output_template->global_sort_index == false && output_template->row_index_before_sort != NULL);
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_row_index_before_sort, to_string(output_template->size_of_row_index_before_sort), "device_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"));
// 拷贝
return_str = return_str + code_line_of_cuda_memcpy("device_" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"), template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "row_index_before_sort"), output_template->data_type_of_row_index_before_sort, to_string(output_template->size_of_row_index_before_sort), "cudaMemcpyHostToDevice") + "\n";
}
assert(output_template->val_arr != NULL);
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_val_arr, to_string(output_template->size_of_val_arr), "device_" + code_of_arr_var_name(dense_block_id, -1, "val_arr"));
// 拷贝
return_str = return_str + code_line_of_cuda_memcpy("device_" + code_of_arr_var_name(dense_block_id, -1, "val_arr"), template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "val_arr"), output_template->data_type_of_val_arr, to_string(output_template->size_of_val_arr), "cudaMemcpyHostToDevice") + "\n";
assert(output_template->col_index_arr != NULL);
return_str = return_str + code_line_of_cuda_malloc(output_template->data_type_of_col_index_arr, to_string(output_template->size_of_col_index_arr), "device_" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr"));
// 拷贝
return_str = return_str + code_line_of_cuda_memcpy("device_" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr"), template_data_name + "->" + code_of_arr_var_name(dense_block_id, -1, "col_index_arr"), output_template->data_type_of_col_index_arr, to_string(output_template->size_of_col_index_arr), "cudaMemcpyHostToDevice") + "\n";
return return_str;
}
string code_of_template_kernal(unaligned_warp_reduce_same_TLB_size_template_t *output_template, unsigned long dense_block_id)
{
assert(output_template != NULL);
// 内核函数的声明
string return_str = "__global__ void spmv_" + to_string(dense_block_id) + "(";
// 用一个变量表明当前形参是不是第一个,如果是第一个就不用点逗号
bool is_first_param = true;
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_global_first_row_index_of_warp_level_block, "* global_first_row_index_of_warp_level_block");
is_first_param = false;
}
if (is_first_param == false)
{
return_str = return_str + ", ";
}
else
{
is_first_param = false;
}
assert(output_template->combine_meta_of_thread_level_block != NULL);
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_combine_meta_of_thread_level_block, "* combine_meta_of_thread_level_block");
// 排序相关的数组
if (output_template->row_index_before_sort_compress == NONE_COMPRESS && output_template->row_index_before_sort != NULL)
{
// 这里代表有排序过
if (is_first_param == false)
{
return_str = return_str + ", ";
}
else
{
is_first_param = false;
}
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_row_index_before_sort, "* row_index_before_sort");
}
if (is_first_param == false)
{
return_str = return_str + ", ";
}
else
{
is_first_param = false;
}
assert(output_template->val_arr != NULL);
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_val_arr, "* val_arr");
return_str = return_str + ", ";
assert(output_template->col_index_arr != NULL);
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_col_index_arr, "* col_index_arr");
// x的值
return_str = return_str + ", ";
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_val_arr, "* device_x_arr");
// y的值
return_str = return_str + ", ";
return_str = return_str + code_of_a_formal_param_declare(output_template->data_type_of_val_arr, "* device_y_arr");
return_str = return_str + ")\n{\n";
// 获取对应计算资源ID
return_str = return_str + "int global_tid = blockDim.x * blockIdx.x + threadIdx.x;\n";
return_str = return_str + "int warp_id = global_tid / 32;\n";
return_str = return_str + "int tid_in_warp = global_tid % 32;\n";
return_str = return_str + "int warp_num = blockDim.x * gridDim.x / 32;\n\n";
// 首行和首列号,如果不是0就保留
if (output_template->kernal_first_row_index != 0)
{
return_str = return_str + "unsigned long kernal_first_row_index = " + to_string(output_template->kernal_first_row_index) + ";\n";
}
if (output_template->kernal_first_col_index != 0)
{
return_str = return_str + "unsigned long kernal_first_col_index = " + to_string(output_template->kernal_first_col_index) + ";\n";
}
// 查看warp的数量
assert((output_template->tblock_num * output_template->thread_num_in_block) % 32 == 0);
assert((output_template->size_of_col_index_arr % (output_template->size_of_combine_meta_of_thread_level_block * output_template->global_thread_level_block_size)) == 0);
int warp_num = (output_template->tblock_num * output_template->thread_num_in_block) / 32;
int WLB_num = output_template->size_of_col_index_arr / (output_template->global_thread_level_block_size * 32);
assert(WLB_num == output_template->size_of_global_first_row_index_of_warp_level_block);
// cout << "warp_num:" << warp_num << endl;
// cout << "WLB_num:" << WLB_num << endl;
// warp_num的数量更少,那就使用for循环
if (warp_num < WLB_num)
{
return_str = return_str + "for (unsigned int WLB_id = warp_id; WLB_id < " + to_string(WLB_num) + "; WLB_id = WLB_id + warp_num)\n{\n";
}
else
{
// warp的数量更多,用if语句
return_str = return_str + "unsigned int WLB_id = warp_id;\n";
return_str = return_str + "if (WLB_id < "+ to_string(WLB_num) +")\n{\n";
}
// 获取首行行号,根据压缩和不压缩分为两种情况
return_str = return_str + "unsigned int WLB_first_row;\n";
if (output_template->global_first_row_index_of_warp_level_block_compress == NONE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block != NULL);
return_str = return_str + "WLB_first_row = global_first_row_index_of_warp_level_block[WLB_id];\n";
}
else if (output_template->global_first_row_index_of_warp_level_block_compress == LINEAR_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block_compress_meta != NULL);
linear_compress_t* compressor = (linear_compress_t *)output_template->global_first_row_index_of_warp_level_block_compress_meta;
return_str = return_str + code_of_arr_read(compressor, "WLB_first_row", "WLB_id") + ";\n";
}
else if (output_template->global_first_row_index_of_warp_level_block_compress == CYCLE_INCREASE_COMPRESS)
{
assert(output_template->global_first_row_index_of_warp_level_block_compress_meta != NULL);
cycle_increase_compress_t* compressor = (cycle_increase_compress_t *)output_template->global_first_row_index_of_warp_level_block_compress_meta;
return_str = return_str + code_of_arr_read(compressor, "WLB_first_row", "WLB_id") + ";\n";
}
else
{
cout << "compress type is not supported in this template" << endl;
assert(false);
}
// 当前线程负责的TLB号
return_str = return_str + "unsigned int global_TLB_id = WLB_id * 32 + tid_in_warp;\n";
return_str = return_str + "\n";
// 获取当前线程的元数据
return_str = return_str + code_of_data_type(output_template->data_type_of_combine_meta_of_thread_level_block) + " combine_meta = combine_meta_of_thread_level_block[global_TLB_id];\n";
return_str = return_str + "\n";
// cout << output_template->bit_num_of_thread_level_combine_meta << endl;
// cout << output_template->bit_num_of_tmp_result_reduce_offset_of_thread_level_block << endl;
// cout << output_template->bit_num_of_first_relative_reduce_row_of_thread_level_block << endl;
// cout << output_template->bit_num_of_sum_begin_bit_flag << endl;
// 获取当前线程第一个归约的行位置,需要分两行来移位,防止编译器把这一行优化掉,导致高位没有丢掉
return_str = return_str + code_of_data_type(output_template->data_type_of_combine_meta_of_thread_level_block) + " TLB_first_reduce_row = combine_meta << " + to_string(output_template->bit_num_of_thread_level_combine_meta - output_template->bit_num_of_first_relative_reduce_row_of_thread_level_block) + ";\n";
return_str = return_str + "TLB_first_reduce_row = TLB_first_reduce_row >> " + to_string(output_template->bit_num_of_thread_level_combine_meta - output_template->bit_num_of_first_relative_reduce_row_of_thread_level_block) + ";\n";
return_str = return_str + "\n";
// TLB向全局内存写中间结果的行偏移量
return_str = return_str + "unsigned int y_offset = TLB_first_reduce_row;\n";
return_str = return_str + "\n";
// 每个线程行身体提前归约的偏移量,分两行归约,防止编译器搞一些优化
return_str = return_str + code_of_data_type(output_template->data_type_of_combine_meta_of_thread_level_block) + " reduce_offset = combine_meta << " + to_string(output_template->bit_num_of_thread_level_combine_meta - output_template->bit_num_of_first_relative_reduce_row_of_thread_level_block - output_template->bit_num_of_tmp_result_reduce_offset_of_thread_level_block) + ";\n";
return_str = return_str + "reduce_offset = reduce_offset >> " + to_string(output_template->bit_num_of_thread_level_combine_meta - output_template->bit_num_of_tmp_result_reduce_offset_of_thread_level_block) + ";\n";
return_str = return_str + "\n";
// 用一个变量记录之前是否出现过sum起始点
return_str = return_str + "bool through_sum_begin_bit = false;\n";
// 行的头部、行的身体和加和的中间结果
return_str = return_str + code_of_data_type(output_template->data_type_of_val_arr) + " row_head_tmp_result = 0;\n";
return_str = return_str + code_of_data_type(output_template->data_type_of_val_arr) + " row_other_tmp_result = 0;\n";
return_str = return_str + code_of_data_type(output_template->data_type_of_val_arr) + " sum_tmp = 0;\n";
// 当前非零元的全局索引
return_str = return_str + "unsigned int global_nz_index = WLB_id * " + to_string(output_template->global_thread_level_block_size * 32) + " + tid_in_warp;\n";
return_str = return_str + "\n";
// 遍历线程的所有非零元
return_str = return_str + "for (unsigned int TLB_nz_id = 0; TLB_nz_id < " + to_string(output_template->global_thread_level_block_size) + "; TLB_nz_id++)\n{\n";
// 获取当前非零元加和起始标记,偏移量的计算依赖于合并之后的元数据大小
return_str = return_str + "bool cur_sum_begin_bit = (combine_meta >> (" + to_string(output_template->bit_num_of_thread_level_combine_meta - 1) + " - TLB_nz_id)) & 0x1;\n";
// 如果当前非零元是第一个行首非零元,那么之前的结果要存起来,用来跨线程归约的行其他部分中间结果
return_str = return_str + "if (cur_sum_begin_bit == true)\n{\n";
// 如果之前已经出现的加和起始位置,那么就需要向显存中写数据了
return_str = return_str + "if (through_sum_begin_bit == true)\n{\n";
// 需要向内存的对应位置写结果,这里计算真实行号
// 根据是不是排序来选择对应的输入方式,假设最大行号不超过
return_str = return_str + code_of_data_type(find_most_suitable_data_type(output_template->matrix->dense_row_number)) + " global_row_index;\n";
// 首先变成当前行号
return_str = return_str + "global_row_index = WLB_first_row + y_offset;\n";
// 如果有排序,就要得到排序之前行号
if (output_template->local_sort_index == true)
{
assert(output_template->global_sort_index == false);
// 根据压缩的情况来找出排序前的行号
if (output_template->row_index_before_sort_compress == NONE_COMPRESS)
{