-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_builder.cc
More file actions
executable file
·2687 lines (2140 loc) · 143 KB
/
code_builder.cc
File metadata and controls
executable file
·2687 lines (2140 loc) · 143 KB
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 "code_builder.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"
#include "empty_op.hpp"
#include <unistd.h>
#include <memory.h>
code_builder_t *init_code_builder(operator_manager_t *op_manager)
{
assert(op_manager != NULL);
assert(op_manager->matrix->block_coor_table.item_arr.size() > 0);
// 检查所有压缩快是不是都有7层索引
for (unsigned int i = 0; i < op_manager->matrix->block_coor_table.item_arr.size(); i++)
{
assert(op_manager->matrix->block_coor_table.item_arr[i]->compressed_block_ptr != NULL);
assert(op_manager->matrix->block_coor_table.item_arr[i]->compressed_block_ptr->read_index.size() == 7);
}
code_builder_t *new_builder = new code_builder_t();
new_builder->op_manager = op_manager;
sparse_struct_t *matrix = op_manager->matrix;
// 表格
dense_block_table_t *block_coor_table = &(matrix->block_coor_table);
// 查看是不是在全局排序过
assert((matrix->is_sorted == true && matrix->sorted_row_index != NULL) || (matrix->is_sorted == false && matrix->sorted_row_index == NULL));
// 遍历所有的密集子矩阵
for (unsigned int i = 0; i < block_coor_table->item_arr.size(); i++)
{
compressed_block_t *compressed_block_view = block_coor_table->item_arr[i]->compressed_block_ptr;
assert(compressed_block_view != NULL);
// 查看当前块是不是和其他块共享行
// 块级别的共享导致了全局层次的归约
new_builder->is_reduce_in_global_level_vec.push_back(compressed_block_view->share_row_with_other_block);
// 默认使用的归约方式是原子加的方式,如果warp和block层次的规约都是原子加,可以直通显存
new_builder->reduce_type_in_global_level_vec.push_back(ATOMIC_ADD);
// 线程级别的行共享导致了warp层次的归约
new_builder->is_reduce_in_warp_level_vec.push_back(compressed_block_view->share_row_with_other_thread);
new_builder->reduce_type_in_warp_level_vec.push_back(ATOMIC_ADD);
// warp级别的行共享导致了block层次的规约
new_builder->is_reduce_in_block_level_vec.push_back(compressed_block_view->share_row_with_other_warp);
new_builder->reduce_type_in_block_level_vec.push_back(ATOMIC_ADD);
// 分配计算资源
new_builder->kernal_block_num_vec.push_back(get_config()["DEFAULT_THREAD_BLOCK_NUM"].as_integer());
new_builder->kernal_thread_num_in_block_vec.push_back(get_config()["DEFAULT_THREAD_NUM_IN_BLOCK"].as_integer());
// 查看是是不是全局排序
if (matrix->is_sorted == true)
{
// 全局排序了,局部肯定不排序
assert(compressed_block_view->y_write_index.size() == 0);
new_builder->sub_block_sort_type_vec.push_back(GLOBAL_SORT);
}
else if (compressed_block_view->y_write_index.size() > 0)
{
// 有局部的排序
new_builder->sub_block_sort_type_vec.push_back(SUB_BLOCK_SORT);
}
else
{
new_builder->sub_block_sort_type_vec.push_back(NO_SORT);
}
}
assert(new_builder->sub_block_sort_type_vec.size() == block_coor_table->item_arr.size());
// 为每一个block在reduce_help_csr中添加一个数组来记录每个thread块结果的全局行索引的CSR压缩
for (unsigned long dense_block_index = 0; dense_block_index < block_coor_table->item_arr.size(); dense_block_index++)
{
compressed_block_t *compressed_block_view = block_coor_table->item_arr[dense_block_index]->compressed_block_ptr;
// 删除归约的相关数组,然后为每个密集矩阵加入一个reduce_help_csr数组
// 删除
assert(compressed_block_view->read_index.size() == 7);
for (unsigned long index_of_read_index = 0; index_of_read_index < compressed_block_view->read_index.size(); index_of_read_index++)
{
index_of_compress_block_t *read_index = compressed_block_view->read_index[index_of_read_index];
if (read_index->child_tmp_row_csr_index_arr != NULL)
{
delete_arr_with_data_type(read_index->child_tmp_row_csr_index_arr, read_index->data_type_of_child_tmp_row_csr_index);
read_index->child_tmp_row_csr_index_arr = NULL;
}
if (read_index->begin_index_in_tmp_row_csr_arr_of_block != NULL)
{
delete_arr_with_data_type(read_index->begin_index_in_tmp_row_csr_arr_of_block, read_index->data_type_of_begin_index_in_tmp_row_csr_arr_of_block);
read_index->begin_index_in_tmp_row_csr_arr_of_block = NULL;
}
}
// 只有全局同步的模板中需要这些
// index_of_compress_block_t* thread_block_index = compressed_block_view->read_index[4];
// assert(thread_block_index->level_of_this_index == THREAD_LEVEL);
// // 因为线程内部没有归约,一个线程最多一行,所以用线程粒度索引的首行号,来确定归约索引
// // vector<unsigned long> get_nnz_of_each_row_in_spec_range(void* row_index_arr, data_type data_type_of_row_index_arr, unsigned long begin_row_bound, unsigned long end_row_bound, unsigned long global_coo_start, unsigned long global_coo_end)
// // 分别遍历三个层次的索引,获取每个thread的全局行号,大小为这个子块行的数量
// vector<unsigned long> tmp_result_number_of_each_row(thread_block_index->max_row_index - thread_block_index->min_row_index + 1);
// memset(&(tmp_result_number_of_each_row[0]), 0, sizeof(unsigned long) * tmp_result_number_of_each_row.size());
// index_of_compress_block_t* warp_block_index = compressed_block_view->read_index[3];
// assert(warp_block_index->level_of_this_index == WRAP_LEVEL);
// index_of_compress_block_t* block_block_index = compressed_block_view->read_index[2];
// assert(block_block_index->level_of_this_index == TBLOCK_LEVEL);
// // 遍历线程块粒度的所有块
// for(unsigned long index_of_block_level_block = 0; index_of_block_level_block < block_block_index->block_num; index_of_block_level_block++){
// // 当前线程块的首行行号
// unsigned long block_first_row_index = read_from_array_with_data_type(block_block_index->index_of_the_first_row_arr, block_block_index->data_type_of_index_of_the_first_row_arr, index_of_block_level_block);
// // 遍历warp粒度的所有块
// unsigned long warp_index_begin = read_from_array_with_data_type(block_block_index->index_arr, block_block_index->index_data_type, index_of_block_level_block);
// unsigned long warp_index_end = read_from_array_with_data_type(block_block_index->index_arr, block_block_index->index_data_type, index_of_block_level_block + 1);
// for(unsigned long index_of_warp_level_block_index = warp_index_begin; index_of_warp_level_block_index < warp_index_end; index_of_warp_level_block_index++){
// // if(index_of_warp_level_block_index >= warp_block_index->block_num){
// // cout << "index_of_warp_level_block_index:" << index_of_warp_level_block_index << ", " << "warp_block_index->block_num:" << warp_block_index->block_num << endl;
// // }
// assert(index_of_warp_level_block_index < warp_block_index->block_num);
// unsigned long warp_first_row_index = read_from_array_with_data_type(warp_block_index->index_of_the_first_row_arr, warp_block_index->data_type_of_index_of_the_first_row_arr, index_of_warp_level_block_index);
// // 遍历thread粒度的所有块
// unsigned long thread_index_begin = read_from_array_with_data_type(warp_block_index->index_arr, warp_block_index->index_data_type, index_of_warp_level_block_index);
// unsigned long thread_index_end = read_from_array_with_data_type(warp_block_index->index_arr, warp_block_index->index_data_type, index_of_warp_level_block_index + 1);
// for(unsigned long index_of_thread_warp = thread_index_begin; index_of_thread_warp < thread_index_end; index_of_thread_warp++){
// // 当前thread块的第一行
// assert(index_of_thread_warp < thread_block_index->block_num);
// unsigned long thread_first_row_index = read_from_array_with_data_type(thread_block_index->index_of_the_first_row_arr, thread_block_index->data_type_of_index_of_the_first_row_arr, index_of_thread_warp);
// // 当前块的全局行号
// unsigned long global_row_index_of_thread_block = block_first_row_index + warp_first_row_index + thread_first_row_index;
// assert(global_row_index_of_thread_block < tmp_result_number_of_each_row.size());
// tmp_result_number_of_each_row[global_row_index_of_thread_block]++;
// }
// }
// }
// 当前块的行数量
// assert(tmp_result_number_of_each_row.size() == thread_block_index->max_row_index - thread_block_index->min_row_index + 1);
// vector<unsigned long> new_reduce_help_csr_item_vec;
// new_reduce_help_csr_item_vec.push_back(0);
// for(unsigned long row_index_in_this_dense_block = 0; row_index_in_this_dense_block < tmp_result_number_of_each_row.size(); row_index_in_this_dense_block++){
// new_reduce_help_csr_item_vec.push_back(new_reduce_help_csr_item_vec[new_reduce_help_csr_item_vec.size()-1] + tmp_result_number_of_each_row[row_index_in_this_dense_block]);
// }
// // 线程粒度结果的行索引的CSR版本的最后一个元素的大小,是thread的块数量
// assert(new_reduce_help_csr_item_vec[new_reduce_help_csr_item_vec.size() - 1] == thread_block_index->block_num);
// // 创建新的索引
// index_of_compress_block_t* new_reduce_index = new index_of_compress_block_t();
// new_reduce_index->level_of_this_index = THREAD_LEVEL;
// new_reduce_index->index_compressed_type = CSR;
// new_reduce_index->index_data_type = find_most_suitable_data_type(thread_block_index->block_num);
// new_reduce_index->length = new_reduce_help_csr_item_vec.size();
// new_reduce_index->index_arr = malloc_arr(new_reduce_index->length, new_reduce_index->index_data_type);
// // 拷贝
// copy_unsigned_long_arr_to_others(&(new_reduce_help_csr_item_vec[0]), new_reduce_index->index_arr, new_reduce_index->index_data_type, new_reduce_index->length);
// compressed_block_view->reduce_help_csr.push_back(new_reduce_index);
// print_arr_to_file_with_data_type(new_reduce_index->index_arr, new_reduce_index->index_data_type, new_reduce_index->length, "/home/duzhen/spmv_builder/data_source/test7.log");
// 给模板初始化为空指针
new_builder->template_vec.push_back(NULL);
new_builder->template_type_vec.push_back(NONE_TEMPLATE);
}
return new_builder;
}
code_builder_t *init_code_builder(operator_manager_t *op_manager, vector<int> sub_matrix_id_vec)
{
// 将一部分的子块初始化到代码生成器中
assert(op_manager != NULL);
assert(op_manager->matrix != NULL);
assert(op_manager->matrix->block_coor_table.item_arr.size() > 0);
// 检查输入是不是正确,是不是每一个
for (int i = 0; i < sub_matrix_id_vec.size(); i++)
{
int sub_matrix_id = sub_matrix_id_vec[i];
assert(sub_matrix_id < op_manager->matrix->block_coor_table.item_arr.size());
// 经过压缩
assert(op_manager->matrix->block_coor_table.item_arr[sub_matrix_id] != NULL);
assert(op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
assert(op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
}
// 针对大矩阵的几个子块创建代码生成器
code_builder_t* new_builder = new code_builder_t();
new_builder->op_manager = op_manager;
sparse_struct_t *matrix = op_manager->matrix;
dense_block_table_t *block_coor_table = &(matrix->block_coor_table);
// 在builder中的有些元数据是空的,先全部初始化,把位置占住
for (unsigned int i = 0; i < block_coor_table->item_arr.size(); i++)
{
new_builder->is_reduce_in_global_level_vec.push_back(false);
new_builder->reduce_type_in_global_level_vec.push_back(ATOMIC_ADD);
// 线程级别的行共享导致了warp层次的归约
new_builder->is_reduce_in_warp_level_vec.push_back(false);
new_builder->reduce_type_in_warp_level_vec.push_back(ATOMIC_ADD);
new_builder->is_reduce_in_block_level_vec.push_back(false);
new_builder->reduce_type_in_block_level_vec.push_back(ATOMIC_ADD);
new_builder->kernal_block_num_vec.push_back(get_config()["DEFAULT_THREAD_BLOCK_NUM"].as_integer());
new_builder->kernal_thread_num_in_block_vec.push_back(get_config()["DEFAULT_THREAD_NUM_IN_BLOCK"].as_integer());
new_builder->sub_block_sort_type_vec.push_back(NO_SORT);
}
assert(new_builder->sub_block_sort_type_vec.size() == block_coor_table->item_arr.size());
// 遍历需要生成代码的密集子矩阵
for (unsigned int i = 0; i < sub_matrix_id_vec.size(); i++)
{
int sub_matrix_id = sub_matrix_id_vec[i];
// 密集子矩阵
compressed_block_t* compressed_block_ptr = block_coor_table->item_arr[sub_matrix_id]->compressed_block_ptr;
assert(compressed_block_ptr != NULL);
new_builder->is_reduce_in_global_level_vec[sub_matrix_id] = compressed_block_ptr->share_row_with_other_block;
new_builder->reduce_type_in_global_level_vec[sub_matrix_id] = ATOMIC_ADD;
new_builder->is_reduce_in_warp_level_vec[sub_matrix_id] = compressed_block_ptr->share_row_with_other_thread;
new_builder->reduce_type_in_warp_level_vec[sub_matrix_id] = ATOMIC_ADD;
new_builder->is_reduce_in_block_level_vec[sub_matrix_id] = compressed_block_ptr->share_row_with_other_warp;
new_builder->reduce_type_in_block_level_vec[sub_matrix_id] = ATOMIC_ADD;
new_builder->kernal_block_num_vec[sub_matrix_id] = get_config()["DEFAULT_THREAD_BLOCK_NUM"].as_integer();
new_builder->kernal_thread_num_in_block_vec[sub_matrix_id] = get_config()["DEFAULT_THREAD_NUM_IN_BLOCK"].as_integer();
// 初始化排序相关的数组
if (matrix->is_sorted == true)
{
// 全局排序了,局部肯定不排序
assert(compressed_block_ptr->y_write_index.size() == 0);
new_builder->sub_block_sort_type_vec[sub_matrix_id] = GLOBAL_SORT;
}
else if (compressed_block_ptr->y_write_index.size() > 0)
{
// 有局部的排序
new_builder->sub_block_sort_type_vec[sub_matrix_id] = SUB_BLOCK_SORT;
}
else
{
new_builder->sub_block_sort_type_vec[sub_matrix_id] = NO_SORT;
}
// 析构一些不需要的数据
assert(compressed_block_ptr->read_index.size() == 7);
for (unsigned long index_of_read_index = 0; index_of_read_index < compressed_block_ptr->read_index.size(); index_of_read_index++)
{
index_of_compress_block_t *read_index = compressed_block_ptr->read_index[index_of_read_index];
if (read_index->child_tmp_row_csr_index_arr != NULL)
{
delete_arr_with_data_type(read_index->child_tmp_row_csr_index_arr, read_index->data_type_of_child_tmp_row_csr_index);
read_index->child_tmp_row_csr_index_arr = NULL;
}
if (read_index->begin_index_in_tmp_row_csr_arr_of_block != NULL)
{
delete_arr_with_data_type(read_index->begin_index_in_tmp_row_csr_arr_of_block, read_index->data_type_of_begin_index_in_tmp_row_csr_arr_of_block);
read_index->begin_index_in_tmp_row_csr_arr_of_block = NULL;
}
}
}
assert(new_builder->sub_block_sort_type_vec.size() == block_coor_table->item_arr.size());
// 并且初始化模板指针
for (unsigned long dense_block_index = 0; dense_block_index < block_coor_table->item_arr.size(); dense_block_index++)
{
// 给模板初始化为空指针
new_builder->template_vec.push_back(NULL);
new_builder->template_type_vec.push_back(NONE_TEMPLATE);
}
return new_builder;
}
void add_template_to_builder(code_builder_t *builder, void *template_ptr, template_type type, unsigned long dense_block_id)
{
assert(builder != NULL && template_ptr != NULL);
assert(builder->op_manager->matrix->block_coor_table.item_arr.size() > dense_block_id);
assert(builder->template_vec.size() > dense_block_id && builder->template_type_vec.size() > dense_block_id);
builder->template_vec[dense_block_id] = template_ptr;
builder->template_type_vec[dense_block_id] = type;
}
// 将归约类型和排序类型转化为字符串
string convert_sort_type_to_str(sort_type type)
{
if (type == GLOBAL_SORT)
{
return "GLOBAL_SORT";
}
if (type == SUB_BLOCK_SORT)
{
return "SUB_BLOCK_SORT";
}
if (type == NO_SORT)
{
return "NO_SORT";
}
assert(false);
}
string convert_reduce_type_to_str(reduce_type type)
{
if (type == ATOMIC_ADD)
{
return "ATOMIC_ADD";
}
if (type == SYNC_BY_SHARED_MEM)
{
return "SYNC_BY_SHARED_MEM";
}
assert(false);
}
// 将元数据转化为字符串
string convert_code_builder_to_str(code_builder_t *builder)
{
assert(builder != NULL);
string return_str = "builder{\n";
// 打印7个数组
// is_reduce_in_warp_level_vec
return_str = return_str + "is_reduce_in_warp_level_vec[";
for (int i = 0; i < builder->is_reduce_in_warp_level_vec.size(); i++)
{
return_str = return_str + to_string(builder->is_reduce_in_warp_level_vec[i]) + ",";
}
return_str = return_str + "],\n";
// is_reduce_in_block_level_vec
return_str = return_str + "is_reduce_in_block_level_vec[";
for (int i = 0; i < builder->is_reduce_in_block_level_vec.size(); i++)
{
return_str = return_str + to_string(builder->is_reduce_in_block_level_vec[i]) + ",";
}
return_str = return_str + "],\n";
// is_reduce_in_global_level_vec
return_str = return_str + "is_reduce_in_global_level_vec[";
for (int i = 0; i < builder->is_reduce_in_global_level_vec.size(); i++)
{
return_str = return_str + to_string(builder->is_reduce_in_global_level_vec[i]) + ",";
}
return_str = return_str + "],\n";
// reduce_type_in_warp_level_vec
return_str = return_str + "reduce_type_in_warp_level_vec[";
for (int i = 0; i < builder->reduce_type_in_warp_level_vec.size(); i++)
{
return_str = return_str + convert_reduce_type_to_str(builder->reduce_type_in_warp_level_vec[i]) + ",";
}
return_str = return_str + "],\n";
// reduce_type_block_level_vec
return_str = return_str + "reduce_type_block_level_vec[";
for (int i = 0; i < builder->reduce_type_in_block_level_vec.size(); i++)
{
return_str = return_str + convert_reduce_type_to_str(builder->reduce_type_in_block_level_vec[i]) + ",";
}
return_str = return_str + "],\n";
// reduce_type_in_global_level_vec
return_str = return_str + "reduce_type_in_global_level_vec[";
for (int i = 0; i < builder->reduce_type_in_global_level_vec.size(); i++)
{
return_str = return_str + convert_reduce_type_to_str(builder->reduce_type_in_global_level_vec[i]) + ",";
}
return_str = return_str + "],\n";
// sub_block_sort_type_vec
return_str = return_str + "sub_block_sort_type_vec[";
for (int i = 0; i < builder->sub_block_sort_type_vec.size(); i++)
{
return_str = return_str + convert_sort_type_to_str(builder->sub_block_sort_type_vec[i]) + ",";
}
return_str = return_str + "],\n";
return_str = return_str + "}\n";
return return_str;
}
string readFileIntoString(string filename)
{
ifstream ifile(filename.c_str());
//将文件读入到ostringstream对象buf中
ostringstream buf;
char ch;
while (buf && ifile.get(ch))
{
buf.put(ch);
}
//返回与流对象buf关联的字符串
return buf.str();
}
string code_of_template_data_struct(void *output_template, template_type type, unsigned long dense_block_id)
{
assert(output_template != NULL);
assert(type != NONE_TEMPLATE);
if (type == DIRECT_ATOM_TEMPLATE)
{
direct_atom_template_t *template_ptr = (direct_atom_template_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == DIRECT_ATOM_TEMPLATE_WARP_COMPRESS)
{
direct_atom_template_warp_compress_t *template_ptr = (direct_atom_template_warp_compress_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS)
{
direct_atom_template_warp_block_compress_t *template_ptr = (direct_atom_template_warp_block_compress_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == SHARED_MEMORY_TEMPLATE)
{
shared_memory_template_t *template_ptr = (shared_memory_template_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == SHARED_MEMORY_TEMPLATE_WARP_COMPRESS)
{
shared_memory_template_warp_compress_t *template_ptr = (shared_memory_template_warp_compress_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == SHARED_MEMORY_LONG_ROW_TEMPLATE)
{
shared_memory_long_row_template_t *template_ptr = (shared_memory_long_row_template_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == SHARED_MEMORY_TOTAL_WARP_REDUCE_TEMPLATE)
{
shared_memory_total_warp_reduce_template_t *template_ptr = (shared_memory_total_warp_reduce_template_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE)
{
direct_atom_total_warp_reduce_template_t *template_ptr = (direct_atom_total_warp_reduce_template_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE)
{
unaligned_warp_reduce_same_TLB_size_template_t* template_ptr = (unaligned_warp_reduce_same_TLB_size_template_t*)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE_WITH_WARP_REDUCE)
{
unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce_t* template_ptr = (unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce_t *) output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
if (type == EMPTY_TEMPLATE)
{
empty_template_t *template_ptr = (empty_template_t *)output_template;
return code_of_template_data_struct(template_ptr, dense_block_id);
}
cout << "code_of_template_data_struct: the template has not support now" << endl;
assert(false);
}
string code_of_read_template_data_from_file_func_define(void *output_template, template_type type, unsigned long dense_block_id, bool force_not_share_global_sort_index)
{
assert(output_template != NULL);
assert(type != NONE_TEMPLATE);
if (type == DIRECT_ATOM_TEMPLATE)
{
direct_atom_template_t *template_ptr = (direct_atom_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == DIRECT_ATOM_TEMPLATE_WARP_COMPRESS)
{
direct_atom_template_warp_compress_t *template_ptr = (direct_atom_template_warp_compress_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == DIRECT_ATOM_TEMPLATE_WARP_BLOCK_COMPRESS)
{
direct_atom_template_warp_block_compress_t *template_ptr = (direct_atom_template_warp_block_compress_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == SHARED_MEMORY_TEMPLATE)
{
shared_memory_template_t *template_ptr = (shared_memory_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == SHARED_MEMORY_LONG_ROW_TEMPLATE)
{
shared_memory_long_row_template_t *template_ptr = (shared_memory_long_row_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == SHARED_MEMORY_TOTAL_WARP_REDUCE_TEMPLATE)
{
shared_memory_total_warp_reduce_template_t *template_ptr = (shared_memory_total_warp_reduce_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id);
}
if (type == DIRECT_ATOM_TOTAL_WARP_REDUCE_TEMPLATE)
{
direct_atom_total_warp_reduce_template_t *template_ptr = (direct_atom_total_warp_reduce_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE)
{
unaligned_warp_reduce_same_TLB_size_template_t* template_ptr = (unaligned_warp_reduce_same_TLB_size_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == UNALIGNED_WARP_REDUCE_SAME_TLB_SIZE_TEMPLATE_WITH_WARP_REDUCE)
{
unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce_t* template_ptr = (unaligned_warp_reduce_same_TLB_size_template_with_warp_reduce_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == SHARED_MEMORY_TEMPLATE_WARP_COMPRESS)
{
shared_memory_template_warp_compress_t *template_ptr = (shared_memory_template_warp_compress_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id, force_not_share_global_sort_index);
}
if (type == EMPTY_TEMPLATE)
{
empty_template_t *template_ptr = (empty_template_t *)output_template;
return code_of_read_template_data_from_file_func_define(template_ptr, dense_block_id);
}
cout << "code_of_read_template_data_from_file_func_define: the template has not support now" << endl;
assert(false);
}
// 生成新的头文件
string build_header_file(code_builder_t *builder)
{
assert(builder != NULL);
string return_str = readFileIntoString(get_config()["spmv_header_file"].as_string());
// 遍历所有的模板,生成对应的代码,分别是数据结构定义和磁盘读
return_str = return_str + "\n\n";
assert(builder->op_manager->matrix->block_coor_table.item_arr.size() == builder->template_vec.size());
for (unsigned long template_index = 0; template_index < builder->template_vec.size(); template_index++)
{
return_str = return_str + code_of_template_data_struct(builder->template_vec[template_index], builder->template_type_vec[template_index], template_index) + "\n";
}
// 读文件的函数声明
for (unsigned long template_index = 0; template_index < builder->template_vec.size(); template_index++)
{
return_str = return_str + code_of_read_template_data_from_file_func_define(builder->template_vec[template_index], builder->template_type_vec[template_index], template_index) + "\n";
}
return return_str;
}
string build_header_file(code_builder_t *builder, vector<int> sub_matrix_id_vec)
{
assert(builder != NULL);
assert(sub_matrix_id_vec.size() > 0);
// 每一个子块都不超标
for (int i = 0; i < sub_matrix_id_vec.size(); i++)
{
int sub_matrix_id = sub_matrix_id_vec[i];
assert(sub_matrix_id < builder->template_vec.size());
assert(sub_matrix_id < builder->op_manager->matrix->block_coor_table.item_arr.size());
}
string return_str = readFileIntoString(get_config()["spmv_header_file"].as_string());
// 遍历所有的模板,生成对应的代码,分别是数据结构定义和磁盘读
return_str = return_str + "\n\n";
assert(builder->op_manager->matrix->block_coor_table.item_arr.size() == builder->template_vec.size());
assert(builder->template_vec.size() == builder->template_type_vec.size());
// 遍历所有要生成模板的子块
for (int i = 0; i < sub_matrix_id_vec.size(); i++)
{
int sub_matrix_id = sub_matrix_id_vec[i];
return_str = return_str + code_of_template_data_struct(builder->template_vec[sub_matrix_id], builder->template_type_vec[sub_matrix_id], sub_matrix_id) + "\n";
}
for (int i = 0; i < sub_matrix_id_vec.size(); i++)
{
int sub_matrix_id = sub_matrix_id_vec[i];
//
return_str = return_str + code_of_read_template_data_from_file_func_define(builder->template_vec[sub_matrix_id], builder->template_type_vec[sub_matrix_id], sub_matrix_id, true) + "\n";
}
return return_str;
}
// string build_header_file(code_builder_t *builder)
// {
// assert(builder != NULL);
// string return_str = readFileIntoString(get_config()["spmv_header_file"].as_string());
// return_str = return_str + code_of_compressed_matrix_content_define(builder);
// return_str = return_str + "\n" + code_of_all_compressed_block_define(builder);
// return_str = return_str + "\n" + code_of_matrix_file_read(builder);
// return return_str;
// }
string code_of_compressed_matrix_content_define(code_builder_t *code_builder)
{
assert(code_builder != NULL);
string return_str = "\n\ntypedef struct compressed_matrix_content\n{\n";
sparse_struct_t *matrix = code_builder->op_manager->matrix;
// 遍历所有的子块中的数组
unsigned long index_of_dense_block;
for (index_of_dense_block = 0; index_of_dense_block < matrix->block_coor_table.item_arr.size(); index_of_dense_block++)
{
compressed_block_t *compressed_block_view = matrix->block_coor_table.item_arr[index_of_dense_block]->compressed_block_ptr;
// dense block的值数组
assert(compressed_block_view != NULL && compressed_block_view->staggered_padding_val_arr != NULL);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, -1, "staggered_padding_val_arr") + " = " + to_string(compressed_block_view->staggered_padding_val_arr_size) + ";\n";
return_str = return_str + code_of_data_type(compressed_block_view->val_data_type) + "* " + code_of_arr_var_name(index_of_dense_block, -1, "staggered_padding_val_arr") + ";\n\n";
// dense block的所有读索引
unsigned long index_of_read_index;
assert(compressed_block_view->read_index.size() == 7);
for (index_of_read_index = 0; index_of_read_index < compressed_block_view->read_index.size(); index_of_read_index++)
{
index_of_compress_block_t *read_index = compressed_block_view->read_index[index_of_read_index];
// 写对应的数组
if (read_index->index_arr != NULL)
{
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "index_arr") + "=" + to_string(read_index->length) + ";\n";
return_str = return_str + code_of_data_type(read_index->index_data_type) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "index_arr") + ";\n";
}
if (read_index->index_of_the_first_row_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3 || index_of_read_index == 4);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "index_of_the_first_row_arr") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_index_of_the_first_row_arr) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "index_of_the_first_row_arr") + ";\n";
}
if (read_index->row_number_of_block_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "row_number_of_block_arr") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_row_number_of_block_arr) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "row_number_of_block_arr") + ";\n";
}
if (read_index->tmp_result_write_index_arr != NULL)
{
// 只有warp层次的分块需要
assert(index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "tmp_result_write_index_arr") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_tmp_result_write_index_arr) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "tmp_result_write_index_arr") + ";\n";
}
if (read_index->coo_begin_index_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
unsigned long size;
if (index_of_read_index == 2)
{
size = read_index->length;
}
else
{
size = read_index->block_num;
}
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "coo_begin_index_arr") + "=" + to_string(size) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_coo_begin_index_arr) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "coo_begin_index_arr") + ";\n";
}
if (read_index->coo_block_size_arr != NULL)
{
assert(index_of_read_index == 3 || index_of_read_index == 4);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "coo_block_size_arr") + "=" + to_string(compressed_block_view->read_index[3]->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_coo_block_size_arr) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "coo_block_size_arr") + ";\n";
}
// 归约信息
if (read_index->child_tmp_row_csr_index_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "child_tmp_row_csr_index_arr") + "=" + to_string(read_index->size_of_child_tmp_row_csr_index) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_child_tmp_row_csr_index) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "child_tmp_row_csr_index_arr") + ";\n";
}
// 归约信息的索引
if (read_index->begin_index_in_tmp_row_csr_arr_of_block != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "begin_index_in_tmp_row_csr_arr_of_block") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_begin_index_in_tmp_row_csr_arr_of_block) + "* " + code_of_arr_var_name(index_of_dense_block, index_of_read_index, "begin_index_in_tmp_row_csr_arr_of_block") + ";\n";
}
return_str = return_str + "\n";
}
// return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" +
// 遍历所有输入索引
unsigned long index_of_y_write_index;
for (index_of_y_write_index = 0; index_of_y_write_index < compressed_block_view->y_write_index.size(); index_of_y_write_index++)
{
index_of_compress_block_t *y_write_index = compressed_block_view->y_write_index[index_of_y_write_index];
assert(y_write_index != NULL && y_write_index->index_arr != NULL);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_y_write_arr_var_name(index_of_dense_block, index_of_y_write_index, "index_arr") + "=" + to_string(y_write_index->length) + ";\n";
return_str = return_str + code_of_data_type(y_write_index->data_type_of_begin_index_in_tmp_row_csr_arr_of_block) + "* " + code_of_y_write_arr_var_name(index_of_dense_block, index_of_y_write_index, "index_arr") + ";\n";
}
}
return_str = return_str + "}compressed_matrix_content_t;\n";
return return_str;
}
// 对部分子块的结构体的定义,部分子块定义矩阵结构
string code_of_compressed_matrix_content_define(code_builder_t *code_builder, vector<int> sub_matrix_id_vec)
{
assert(code_builder != NULL && sub_matrix_id_vec.size() > 0);
string return_str = "\n\ntypedef struct compressed_matrix_content\n{\n";
sparse_struct_t *matrix = code_builder->op_manager->matrix;
assert(matrix != NULL);
// 遍历所有需要进一步执行子矩阵元数据结构体定义的子矩阵
for (int i = 0; i < sub_matrix_id_vec.size(); i++)
{
int sub_matrix_id = sub_matrix_id_vec[i];
assert(sub_matrix_id < code_builder->template_vec.size());
assert(sub_matrix_id < code_builder->op_manager->matrix->block_coor_table.item_arr.size());
assert(code_builder->template_vec[sub_matrix_id] != NULL);
assert(code_builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr != NULL);
assert(code_builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr->read_index.size() == 7);
compressed_block_t* compressed_block_ptr = code_builder->op_manager->matrix->block_coor_table.item_arr[sub_matrix_id]->compressed_block_ptr;
// dense block的值数组
assert(compressed_block_ptr != NULL && compressed_block_ptr->staggered_padding_val_arr != NULL);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, -1, "staggered_padding_val_arr") + " = " + to_string(compressed_block_ptr->staggered_padding_val_arr_size) + ";\n";
return_str = return_str + code_of_data_type(compressed_block_ptr->val_data_type) + "* " + code_of_arr_var_name(sub_matrix_id, -1, "staggered_padding_val_arr") + ";\n\n";
// 遍历当前子块的所有读索引
for (unsigned long index_of_read_index = 0; index_of_read_index < compressed_block_ptr->read_index.size(); index_of_read_index++)
{
index_of_compress_block_t *read_index = compressed_block_ptr->read_index[index_of_read_index];
// 写对应的数组
if (read_index->index_arr != NULL)
{
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "index_arr") + "=" + to_string(read_index->length) + ";\n";
return_str = return_str + code_of_data_type(read_index->index_data_type) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "index_arr") + ";\n";
}
if (read_index->index_of_the_first_row_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3 || index_of_read_index == 4);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "index_of_the_first_row_arr") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_index_of_the_first_row_arr) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "index_of_the_first_row_arr") + ";\n";
}
if (read_index->row_number_of_block_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "row_number_of_block_arr") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_row_number_of_block_arr) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "row_number_of_block_arr") + ";\n";
}
if (read_index->tmp_result_write_index_arr != NULL)
{
// 只有warp层次的分块需要
assert(index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "tmp_result_write_index_arr") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_tmp_result_write_index_arr) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "tmp_result_write_index_arr") + ";\n";
}
if (read_index->coo_begin_index_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
unsigned long size;
if (index_of_read_index == 2)
{
size = read_index->length;
}
else
{
size = read_index->block_num;
}
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "coo_begin_index_arr") + "=" + to_string(size) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_coo_begin_index_arr) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "coo_begin_index_arr") + ";\n";
}
if (read_index->coo_block_size_arr != NULL)
{
assert(index_of_read_index == 3 || index_of_read_index == 4);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "coo_block_size_arr") + "=" + to_string(compressed_block_ptr->read_index[3]->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_coo_block_size_arr) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "coo_block_size_arr") + ";\n";
}
// 归约信息
if (read_index->child_tmp_row_csr_index_arr != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "child_tmp_row_csr_index_arr") + "=" + to_string(read_index->size_of_child_tmp_row_csr_index) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_child_tmp_row_csr_index) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "child_tmp_row_csr_index_arr") + ";\n";
}
// 归约信息的索引
if (read_index->begin_index_in_tmp_row_csr_arr_of_block != NULL)
{
assert(index_of_read_index == 2 || index_of_read_index == 3);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "begin_index_in_tmp_row_csr_arr_of_block") + "=" + to_string(read_index->block_num) + ";\n";
return_str = return_str + code_of_data_type(read_index->data_type_of_begin_index_in_tmp_row_csr_arr_of_block) + "* " + code_of_arr_var_name(sub_matrix_id, index_of_read_index, "begin_index_in_tmp_row_csr_arr_of_block") + ";\n";
}
return_str = return_str + "\n";
}
// 遍历所有写y的索引
for (unsigned long index_of_y_write_index = 0; index_of_y_write_index < compressed_block_ptr->y_write_index.size(); index_of_y_write_index++)
{
index_of_compress_block_t *y_write_index = compressed_block_ptr->y_write_index[index_of_y_write_index];
assert(y_write_index != NULL && y_write_index->index_arr != NULL);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_y_write_arr_var_name(sub_matrix_id, index_of_y_write_index, "index_arr") + "=" + to_string(y_write_index->length) + ";\n";
return_str = return_str + code_of_data_type(y_write_index->data_type_of_begin_index_in_tmp_row_csr_arr_of_block) + "* " + code_of_y_write_arr_var_name(sub_matrix_id, index_of_y_write_index, "index_arr") + ";\n";
}
}
return_str = return_str + "}compressed_matrix_content_t;\n";
return return_str;
}
string code_of_all_compressed_block_define(code_builder_t *code_builder)
{
assert(code_builder != NULL);
string return_str = "\ntypedef struct all_compressed_block\n{\ncompressed_matrix_content_t * all_compressed_matrix_info;\n";
// 查看是否有全局的排序
if (code_builder->op_manager->matrix->sorted_row_index != NULL)
{
assert(code_builder->sub_block_sort_type_vec[0] == GLOBAL_SORT);
return_str = return_str + code_of_data_type(UNSIGNED_LONG) + " size_of_" + code_of_arr_var_name(-1, -1, "sorted_row_index") + "=" + to_string(code_builder->op_manager->matrix->dense_row_number) + ";\n";
return_str = return_str + code_of_data_type(code_builder->op_manager->matrix->data_type_of_sorted_row_index) + "* " + code_of_arr_var_name(-1, -1, "sorted_row_index") + ";\n";
}
return_str = return_str + "}all_compressed_block_t;\n";
return return_str;
}
string code_of_data_type(data_type type)
{
if (type == CHAR)
{
return "char";
}
else if (type == UNSIGNED_CHAR)
{
return "unsigned char";
}
else if (type == SHORT)
{
return "short";
}
else if (type == UNSIGNED_SHORT)
{
return "unsigned short";
}
else if (type == INT)
{
return "int";
}
else if (type == UNSIGNED_INT)
{
return "unsigned int";
}
else if (type == LONG)
{
return "long";
}
else if (type == UNSIGNED_LONG)
{
return "unsigned long";
}
else if (type == LONG_LONG)
{
return "long long";
}
else if (type == UNSIGNED_LONG_LONG)
{
return "unsigned long long";
}
else if (type == FLOAT)
{
return "float";
}
else if (type == DOUBLE)
{
return "double";
}
else if (type == BOOL)
{
return "bool";
}
assert(false);
}
// 某一个稠密子块、某一个read_index的某一个数组的变量名,传入-1表示没有则个层次的索引
string code_of_arr_var_name(int index_of_dense_block, int index_of_read_index, string arr_name)
{
if (index_of_dense_block == -1 && index_of_read_index != -1)
{
return "read_index_" + to_string(index_of_read_index) + "_" + arr_name;
}
else if (index_of_dense_block != -1 && index_of_read_index == -1)
{
return "dense_" + to_string(index_of_dense_block) + "_" + arr_name;
}
else if (index_of_dense_block == -1 && index_of_dense_block == -1)
{