-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.cc
executable file
·2321 lines (1925 loc) · 78.7 KB
/
struct.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 "struct.hpp"
#include <string>
#include <string.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cassert>
#include <time.h>
#include <cstdlib>
#include "config.hpp"
using namespace std;
// 打印dense_block_table_item,输入item,是日志输出还是命令行输出
string convert_dense_block_table_item_to_string(dense_block_table_item *item)
{
// 打印范围
string block_range = "[[" + to_string(item->min_dense_row_index) + "," + to_string(item->max_dense_row_index) + "][" +
to_string(item->min_dense_col_index) + "," + to_string(item->max_dense_col_index) + "][" +
to_string(item->begin_coo_index) + "," + to_string(item->end_coo_index) + "]]";
string return_str = block_range + "(";
// 首先先打印所有的坐标
int i;
for (i = 0; i < item->block_coordinate.size(); i++)
{
return_str = return_str + to_string(item->block_coordinate[i]);
if (i != item->block_coordinate.size() - 1)
{
return_str = return_str + ",";
}
}
return_str = return_str + ")" + to_string((long)item->compressed_block_ptr);
return return_str;
}
// 打印dense_block_table,输入table,是日志输出还是命令行输出
void print_dense_block_table(dense_block_table *table, bool if_log, string log_name)
{
if (if_log == false)
{
// 打印到命令行
cout << "====="
<< "block coordinate table:"
<< "=====" << endl;
int i;
dense_block_table_item_t **item_ptr_array = &(table->item_arr[0]);
for (i = 0; i < table->item_arr.size(); i++)
{
cout << convert_dense_block_table_item_to_string(item_ptr_array[i]) << " | " << endl;
}
cout << "=====" << endl;
}
else
{
// 打印到日志文件
ofstream outfile(log_name, ios::app);
outfile << "====="
<< "block coordinate table:"
<< "=====" << endl;
int i;
for (i = 0; i < table->item_arr.size(); i++)
{
outfile << convert_dense_block_table_item_to_string(table->item_arr[i]) << " | " << endl;
}
outfile << "=====" << endl;
outfile.close();
}
}
void split(const std::string &s, std::vector<std::string> &sv, const char delim)
{
sv.clear();
std::istringstream iss(s);
std::string temp;
while (std::getline(iss, temp, delim))
{
sv.emplace_back(std::move(temp));
}
return;
}
// 将矩阵coo数据的文件取出到内存中
void get_matrix_index_and_val_from_file(string coo_file_name, vector<unsigned long> &row_index_vec, vector<unsigned long> &col_index_vec, vector<float> &float_val_vec, vector<double> &double_val_vec, data_type val_data_type, unsigned long &max_row_index, unsigned long &max_col_index)
{
assert(val_data_type == FLOAT || val_data_type == DOUBLE);
assert(row_index_vec.size() == 0 && col_index_vec.size() == 0 && float_val_vec.size() == 0 && double_val_vec.size() == 0);
max_col_index = 0;
max_row_index = 0;
// 读文件
char buf[1024];
ifstream infile;
infile.open(coo_file_name);
bool dataset_first_line = true;
if (infile.is_open())
{
while (infile.good() && !infile.eof())
{
string line_str;
vector<string> sv;
memset(buf, 0, 1024);
infile.getline(buf, 1024);
line_str = buf;
// 碰到奇怪的输入就跳过
if (isspace(line_str[0]) || line_str.empty())
{
continue;
}
split(line_str, sv);
// 矩阵的规模,先是最大行号,然后是最大列号
if (dataset_first_line == true)
{
// 矩阵的规模,因为索引从0开始,所以索引的取值范围应该-1
max_row_index = atol(sv[0].c_str()) - 1;
max_col_index = atol(sv[1].c_str()) - 1;
dataset_first_line = false;
continue;
}
// 佛罗里达矩阵先行索引,然后是列索引
unsigned long row_index = atol(sv[0].c_str()) - 1;
unsigned long col_index = atol(sv[1].c_str()) - 1;
// 增序的比较
if (row_index_vec.size() != 0)
{
assert(col_index_vec.size() != 0);
assert(row_index >= row_index_vec[row_index_vec.size() - 1]);
}
row_index_vec.push_back(row_index);
col_index_vec.push_back(col_index);
if (val_data_type == DOUBLE)
{
double_val_vec.push_back(stod(sv[2].c_str()));
}
else if (val_data_type == FLOAT)
{
float_val_vec.push_back(stof(sv[2].c_str()));
}
else
{
printf("unexpected data type\n");
assert(false);
}
}
}
else
{
cout << "get_matrix_index_and_val_from_file: cannot open file " << coo_file_name << endl;
assert(false);
}
infile.close();
cout << "finish read file" << endl;
}
// 规定读入的文件,然后决定是单精度还是双精度
// 弗洛里达矩阵是先列后行,第一行是矩阵的大小
sparse_struct_t *init_sparse_struct_by_coo_file(string coo_file_name, data_type value_data_type)
{
// 首先准备三个vector,分别是行列索引的值
vector<unsigned long> col_arr;
vector<unsigned long> row_arr;
vector<float> val_arr_float;
vector<double> val_arr_double;
// 在遍历的过程中找到行和列的最大值
unsigned long col_index_max = 0;
unsigned long row_index_max = 0;
// 读文件
char buf[1024];
ifstream infile;
infile.open(coo_file_name);
bool dataset_first_line = true;
if (infile.is_open())
{
// 读弗洛里达矩阵格式,第一行是矩阵规模,先是行数量,然后是列数量
while (infile.good() && !infile.eof())
{
string line_str;
vector<string> sv;
memset(buf, 0, 1024);
infile.getline(buf, 1024);
line_str = buf;
// 碰到奇怪的输入就跳过
if (isspace(line_str[0]) || line_str.empty())
{
continue;
}
split(line_str, sv);
// 矩阵的规模,先是最大行号,然后是最大列号
if (dataset_first_line == true)
{
// 矩阵的规模,因为索引从0开始,所以索引的取值范围应该-1
row_index_max = atol(sv[0].c_str()) - 1;
col_index_max = atol(sv[1].c_str()) - 1;
dataset_first_line = false;
continue;
}
// 佛罗里达矩阵先行索引,然后是列索引
unsigned long row_index = atol(sv[0].c_str()) - 1;
unsigned long col_index = atol(sv[1].c_str()) - 1;
// if (row_index > row_index_max)
// {
// row_index_max = row_index;
// }
// if (col_index > col_index_max)
// {
// col_index_max = col_index;
// }
// 增序的比较
if (row_arr.size() != 0)
{
assert(col_arr.size() != 0);
assert(row_index >= row_arr[row_arr.size() - 1]);
}
// 分别是行、列、值
row_arr.push_back(row_index);
col_arr.push_back(col_index);
if (value_data_type == DOUBLE)
{
val_arr_double.push_back(stod(sv[2].c_str()));
}
else if (value_data_type == FLOAT)
{
val_arr_float.push_back(stof(sv[2].c_str()));
}
else
{
printf("unexpected data type\n");
assert(false);
}
}
}
else
{
cout << "get_matrix_index_and_val_from_file: cannot open file " << coo_file_name << endl;
assert(false);
}
infile.close();
// 打印获得的元素
// int i;
// for(i = 0; i < row_arr.size(); i++){
// cout << row_arr[i] << " " << col_arr[i] << " ";
// if(value_data_type == DOUBLE){
// cout << val_arr_double[i] << endl;
// }else if(value_data_type == FLOAT){
// cout << val_arr_float[i] << endl;
// }
// }
// return NULL;
cout << "finish read file" << endl;
// 初始化这个结构
return init_sparse_struct_by_coo_vector(row_arr, col_arr, val_arr_float, val_arr_double,
value_data_type, col_index_max, row_index_max);
}
sparse_struct_t *init_sparse_struct_by_coo_vector(vector<unsigned long> row_arr, vector<unsigned long> col_arr,
vector<float> val_arr_float, vector<double> val_arr_double, data_type value_data_type,
unsigned long col_index_max, unsigned long row_index_max)
{
assert(value_data_type == DOUBLE || value_data_type == FLOAT);
assert(col_index_max >= 0 && row_index_max >= 0);
// cout << row_arr.size() << " " << col_arr.size() << endl;
assert(row_arr.size() && col_arr.size());
assert(row_arr.size() == val_arr_float.size() || row_arr.size() == val_arr_double.size());
sparse_struct_t *return_struct = new sparse_struct_t();
// 索引和数量差1
return_struct->dense_row_number = row_index_max + 1;
return_struct->dense_col_number = col_index_max + 1;
return_struct->nnz = row_arr.size();
return_struct->origin_nnz = return_struct->nnz;
// 稀疏矩阵现在就一个分块,所以block_coor_table里面必然是空的
return_struct->is_blocked = false;
// 还没有被排序过
return_struct->is_sorted = false;
return_struct->sorted_row_index = NULL;
// 在压缩视图中才产生的东西
return_struct->compressed_block_arr = NULL;
// 将数据拷贝到数组中,这里要进行值拷贝,所以用memcpy更好
// return_struct->coo_row_index_cache = &row_arr[0];
return_struct->coo_col_index_cache = new unsigned long[col_arr.size()];
return_struct->coo_row_index_cache = new unsigned long[row_arr.size()];
memcpy(return_struct->coo_row_index_cache, &row_arr[0], row_arr.size() * sizeof(unsigned long));
memcpy(return_struct->coo_col_index_cache, &col_arr[0], col_arr.size() * sizeof(unsigned long));
return_struct->val_data_type = value_data_type;
if (return_struct->val_data_type == DOUBLE)
{
return_struct->coo_value_cache = new double[val_arr_double.size()];
memcpy(return_struct->coo_value_cache, &val_arr_double[0], val_arr_double.size() * sizeof(double));
}
if (return_struct->val_data_type == FLOAT)
{
return_struct->coo_value_cache = new float[val_arr_float.size()];
memcpy(return_struct->coo_value_cache, &val_arr_float[0], val_arr_float.size() * sizeof(float));
}
cout << "finish init A" << endl;
// 然后是x的数据,这里的x暂时是0.9就好了
return_struct->coo_x_cache.x_data_type = value_data_type;
// 按照列数量申请x
if (value_data_type == FLOAT)
{
return_struct->coo_x_cache.x_arr = new float[return_struct->dense_col_number];
int i;
for (i = 0; i < return_struct->dense_col_number; i++)
{
((float *)return_struct->coo_x_cache.x_arr)[i] = 0.9;
}
}
else
{
return_struct->coo_x_cache.x_arr = new double[return_struct->dense_col_number];
int i;
for (i = 0; i < return_struct->dense_col_number; i++)
{
((double *)return_struct->coo_x_cache.x_arr)[i] = 0.9;
}
}
cout << "finish init x" << endl;
// 打印所有的数据,保证初始化
// int i;
// for(i = 0; i < return_struct->dense_col_number; i++){
// if(value_data_type == FLOAT){
// cout << ((float *)return_struct->coo_x_cache.x_arr)[i] << ",";
// }else{
// cout << ((double *)return_struct->coo_x_cache.x_arr)[i] << ",";
// }
// }
// int i;
// for (i = 0; i < return_struct->dense_col_number; i++)
// {
// cout << return_struct->coo_col_index_cache[i] << ",";
// cout << return_struct->coo_row_index_cache[i] << endl;
// }
return return_struct;
}
sparse_struct_t *val_copy_from_old_matrix_struct(sparse_struct_t *matrix)
{
assert(matrix != NULL);
sparse_struct_t *return_matrix = new sparse_struct_t();
// 拷贝最外层的一些基本数据
return_matrix->dense_row_number = matrix->dense_row_number;
return_matrix->dense_col_number = matrix->dense_col_number;
return_matrix->nnz = matrix->nnz;
return_matrix->origin_nnz = matrix->origin_nnz;
return_matrix->is_compressed = matrix->is_compressed;
return_matrix->is_blocked = matrix->is_blocked;
// 全局的排序索引
if (matrix->sorted_row_index != NULL)
{
assert(matrix->is_sorted == true);
// 拷贝全局排序索引
return_matrix->sorted_row_index = val_copy_from_old_arr_with_data_type(matrix->sorted_row_index, matrix->dense_row_number, matrix->data_type_of_sorted_row_index);
return_matrix->is_sorted = true;
return_matrix->data_type_of_sorted_row_index = matrix->data_type_of_sorted_row_index;
}
else
{
assert(matrix->is_sorted == false);
return_matrix->sorted_row_index = NULL;
return_matrix->is_sorted = false;
}
// 一个被抛弃的指针
assert(matrix->compressed_block_arr == NULL);
// COO行索引、列索引、值数组肯定都是存在的
assert(matrix->coo_value_cache != NULL && matrix->coo_row_index_cache != NULL && matrix->coo_col_index_cache != NULL);
return_matrix->coo_row_index_cache = (unsigned long *)val_copy_from_old_arr_with_data_type(matrix->coo_row_index_cache, matrix->nnz, UNSIGNED_LONG);
return_matrix->coo_col_index_cache = (unsigned long *)val_copy_from_old_arr_with_data_type(matrix->coo_col_index_cache, matrix->nnz, UNSIGNED_LONG);
// 值数组
return_matrix->val_data_type = matrix->val_data_type;
return_matrix->coo_value_cache = val_copy_from_old_arr_with_data_type(matrix->coo_value_cache, matrix->nnz, matrix->val_data_type);
// 如果存在X数组,那就拷贝X数组
if (return_matrix->coo_x_cache.x_arr != NULL)
{
return_matrix->coo_x_cache.x_data_type = matrix->coo_x_cache.x_data_type;
return_matrix->coo_x_cache.x_arr = val_copy_from_old_arr_with_data_type(return_matrix->coo_x_cache.x_arr, return_matrix->dense_col_number, matrix->coo_x_cache.x_data_type);
}
// 子块表
for (int item_id = 0; item_id < matrix->block_coor_table.item_arr.size(); item_id++)
{
// 当前每个子块中都是有确定内容的
assert(matrix->block_coor_table.item_arr[item_id] != NULL);
// 首先拷贝对应的表项
dense_block_table_item_t *new_item = new dense_block_table_item_t();
dense_block_table_item_t *old_item = matrix->block_coor_table.item_arr[item_id];
new_item->block_coordinate = old_item->block_coordinate;
new_item->min_dense_row_index = old_item->min_dense_row_index;
new_item->max_dense_row_index = old_item->max_dense_row_index;
new_item->min_dense_col_index = old_item->min_dense_col_index;
new_item->max_dense_col_index = old_item->max_dense_col_index;
new_item->begin_coo_index = old_item->begin_coo_index;
new_item->end_coo_index = old_item->end_coo_index;
new_item->is_col_blocked = old_item->is_col_blocked;
new_item->is_sorted = old_item->is_sorted;
// 如果存在压缩视图,就要进行压缩视图的值拷贝
if (old_item->compressed_block_ptr != NULL)
{
compressed_block_t *new_compressed_block_ptr = new compressed_block_t();
compressed_block_t *old_compressed_block_ptr = old_item->compressed_block_ptr;
new_compressed_block_ptr->is_sorted = old_compressed_block_ptr->is_sorted;
new_compressed_block_ptr->share_row_with_other_block = old_compressed_block_ptr->share_row_with_other_block;
new_compressed_block_ptr->share_row_with_other_warp = old_compressed_block_ptr->share_row_with_other_warp;
new_compressed_block_ptr->share_row_with_other_thread = old_compressed_block_ptr->share_row_with_other_thread;
// 第一个值数组是肯定存在的
assert(old_compressed_block_ptr->val_arr != NULL);
// 值数组
new_compressed_block_ptr->size = old_compressed_block_ptr->size;
new_compressed_block_ptr->val_data_type = old_compressed_block_ptr->val_data_type;
new_compressed_block_ptr->val_arr = val_copy_from_old_arr_with_data_type(old_compressed_block_ptr->val_arr, old_compressed_block_ptr->size, old_compressed_block_ptr->val_data_type);
// 如果剩下两个数组都存在,也要执行拷贝
if (old_compressed_block_ptr->padding_val_arr != NULL)
{
new_compressed_block_ptr->padding_arr_size = old_compressed_block_ptr->padding_arr_size;
new_compressed_block_ptr->padding_val_arr = val_copy_from_old_arr_with_data_type(old_compressed_block_ptr->padding_val_arr, old_compressed_block_ptr->padding_arr_size, old_compressed_block_ptr->val_data_type);
}
if (old_compressed_block_ptr->staggered_padding_val_arr != NULL)
{
new_compressed_block_ptr->staggered_padding_val_arr_size = old_compressed_block_ptr->staggered_padding_val_arr_size;
new_compressed_block_ptr->staggered_padding_val_arr = val_copy_from_old_arr_with_data_type(old_compressed_block_ptr->staggered_padding_val_arr, old_compressed_block_ptr->staggered_padding_val_arr_size, old_compressed_block_ptr->val_data_type);
}
// 三个索引指针的数组
// 第一个是读索引
for (unsigned long read_index_id = 0; read_index_id < old_compressed_block_ptr->read_index.size(); read_index_id++)
{
assert(old_compressed_block_ptr->read_index[read_index_id] != NULL);
index_of_compress_block_t *old_compress_block_index_ptr = old_compressed_block_ptr->read_index[read_index_id];
index_of_compress_block_t *new_compress_block_index_ptr = new index_of_compress_block_t();
new_compress_block_index_ptr->level_of_this_index = old_compress_block_index_ptr->level_of_this_index;
new_compress_block_index_ptr->index_compressed_type = old_compress_block_index_ptr->index_compressed_type;
new_compress_block_index_ptr->block_num = old_compress_block_index_ptr->block_num;
// 索引可能是存在的
if (old_compress_block_index_ptr->index_arr != NULL)
{
new_compress_block_index_ptr->index_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->index_arr, old_compress_block_index_ptr->length, old_compress_block_index_ptr->index_data_type);
}
new_compress_block_index_ptr->length = old_compress_block_index_ptr->length;
new_compress_block_index_ptr->index_data_type = old_compress_block_index_ptr->index_data_type;
new_compress_block_index_ptr->type_of_index = old_compress_block_index_ptr->type_of_index;
// is_sort_arr,应该不会被用,如果被使用也是在BLB层次被使用
assert(old_compress_block_index_ptr->is_sort_arr == NULL);
if (old_compress_block_index_ptr->is_sort_arr != NULL)
{
assert(read_index_id == 2);
new_compress_block_index_ptr->is_sort_arr = (bool *)val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->is_sort_arr, old_compress_block_index_ptr->block_num, BOOL);
}
assert(new_compress_block_index_ptr->is_sort_arr == NULL);
// 行首索引
if (old_compress_block_index_ptr->index_of_the_first_row_arr != NULL)
{
// 只在TLB、WLB、BLB三个层次
assert(read_index_id == 2 || read_index_id == 3 || read_index_id == 4);
new_compress_block_index_ptr->index_of_the_first_row_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->index_of_the_first_row_arr, old_compress_block_index_ptr->block_num, old_compress_block_index_ptr->data_type_of_index_of_the_first_row_arr);
}
new_compress_block_index_ptr->data_type_of_index_of_the_first_row_arr = old_compress_block_index_ptr->data_type_of_index_of_the_first_row_arr;
// 每个块的行数量
if (old_compress_block_index_ptr->row_number_of_block_arr != NULL)
{
// 只在TLB、WLB、BLB三个层次
assert(read_index_id == 2 || read_index_id == 3 || read_index_id == 4);
new_compress_block_index_ptr->row_number_of_block_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->row_number_of_block_arr, old_compress_block_index_ptr->block_num, old_compress_block_index_ptr->data_type_of_row_number_of_block_arr);
}
new_compress_block_index_ptr->data_type_of_row_number_of_block_arr = old_compress_block_index_ptr->data_type_of_row_number_of_block_arr;
// 一个可能不再被使用的归约信息
if (old_compress_block_index_ptr->tmp_result_write_index_arr != NULL)
{
assert(read_index_id == 3);
new_compress_block_index_ptr->tmp_result_write_index_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->tmp_result_write_index_arr, old_compress_block_index_ptr->block_num, old_compress_block_index_ptr->data_type_of_tmp_result_write_index_arr);
}
new_compress_block_index_ptr->data_type_of_tmp_result_write_index_arr = old_compress_block_index_ptr->data_type_of_tmp_result_write_index_arr;
new_compress_block_index_ptr->max_row_index = old_compress_block_index_ptr->max_row_index;
new_compress_block_index_ptr->min_row_index = old_compress_block_index_ptr->min_row_index;
new_compress_block_index_ptr->max_col_index = old_compress_block_index_ptr->max_col_index;
new_compress_block_index_ptr->min_col_index = old_compress_block_index_ptr->min_col_index;
// 块的第一个非零元的索引
if (old_compress_block_index_ptr->coo_begin_index_arr != NULL)
{
// 只有BLB层次和WLB层次有这两个东西
if (old_compress_block_index_ptr->level_of_this_index == TBLOCK_LEVEL)
{
assert(read_index_id == 2);
new_compress_block_index_ptr->coo_begin_index_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->coo_begin_index_arr, old_compress_block_index_ptr->length, old_compress_block_index_ptr->data_type_of_coo_begin_index_arr);
}
else if (old_compress_block_index_ptr->level_of_this_index == WRAP_LEVEL)
{
assert(read_index_id == 3);
new_compress_block_index_ptr->coo_begin_index_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->coo_begin_index_arr, old_compress_block_index_ptr->block_num, old_compress_block_index_ptr->data_type_of_coo_begin_index_arr);
}
else
{
cout << "should not have coo_begin_index_arr" << endl;
assert(false);
}
}
new_compress_block_index_ptr->data_type_of_coo_begin_index_arr = old_compress_block_index_ptr->data_type_of_coo_begin_index_arr;
// 每个块的非零元数量
if (old_compress_block_index_ptr->coo_block_size_arr != NULL)
{
assert(read_index_id == 3 || read_index_id == 4);
// 这个数组只在WLB和TLB层次有,数组的大小和WLB数量一致
unsigned long warp_block_num = old_compressed_block_ptr->read_index[3]->block_num;
new_compress_block_index_ptr->coo_block_size_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->coo_block_size_arr, warp_block_num, old_compress_block_index_ptr->data_type_of_coo_block_size_arr);
}
new_compress_block_index_ptr->data_type_of_coo_block_size_arr = old_compress_block_index_ptr->data_type_of_coo_block_size_arr;
if (old_compress_block_index_ptr->child_tmp_row_csr_index_arr != NULL)
{
assert(read_index_id == 2 || read_index_id == 3);
new_compress_block_index_ptr->child_tmp_row_csr_index_arr = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->child_tmp_row_csr_index_arr, old_compress_block_index_ptr->size_of_child_tmp_row_csr_index, old_compress_block_index_ptr->data_type_of_child_tmp_row_csr_index);
}
// 数组大小和数据类型
new_compress_block_index_ptr->data_type_of_child_tmp_row_csr_index = old_compress_block_index_ptr->data_type_of_child_tmp_row_csr_index;
new_compress_block_index_ptr->size_of_child_tmp_row_csr_index = old_compress_block_index_ptr->size_of_child_tmp_row_csr_index;
if (old_compress_block_index_ptr->begin_index_in_tmp_row_csr_arr_of_block != NULL)
{
assert(read_index_id == 2 || read_index_id == 3);
new_compress_block_index_ptr->begin_index_in_tmp_row_csr_arr_of_block = val_copy_from_old_arr_with_data_type(old_compress_block_index_ptr->begin_index_in_tmp_row_csr_arr_of_block, old_compress_block_index_ptr->block_num, old_compress_block_index_ptr->data_type_of_begin_index_in_tmp_row_csr_arr_of_block);
}
new_compress_block_index_ptr->data_type_of_begin_index_in_tmp_row_csr_arr_of_block = old_compress_block_index_ptr->data_type_of_begin_index_in_tmp_row_csr_arr_of_block;
// 将读指针写到对应的指针vec中
new_compressed_block_ptr->read_index.push_back(new_compress_block_index_ptr);
}
// 写索引,排序会产生的索引,可能有一个
assert(old_compressed_block_ptr->y_write_index.size() <= 1);
for (unsigned long write_index_id = 0; write_index_id < old_compressed_block_ptr->y_write_index.size(); write_index_id++)
{
assert(old_compressed_block_ptr->is_sorted == true || old_compressed_block_ptr->read_index[2]->is_sort_arr != NULL);
assert(old_compressed_block_ptr->y_write_index[write_index_id] != NULL);
index_of_compress_block_t *old_y_write_index = old_compressed_block_ptr->y_write_index[write_index_id];
index_of_compress_block_t *new_y_write_index = new index_of_compress_block_t();
assert(old_y_write_index->index_arr != NULL);
// 只有index_arr是存在的
new_y_write_index->index_arr = val_copy_from_old_arr_with_data_type(old_y_write_index->index_arr, old_y_write_index->length, old_y_write_index->index_data_type);
}
// 归约索引reduce_help_csr是肯定不存在的
assert(old_compressed_block_ptr->reduce_help_csr.size() == 0);
// 执行对应的赋值
new_item->compressed_block_ptr = new_compressed_block_ptr;
}
// 将对应表项记录下来
return_matrix->block_coor_table.item_arr.push_back(new_item);
}
return return_matrix;
}
void output_struct_coo_to_file(sparse_struct_t *matrix_struct, string file_name)
{
ofstream OsWrite(file_name, ios::out | ios::trunc);
int i;
for (i = 0; i < matrix_struct->nnz; i++)
{
// 打印
OsWrite << matrix_struct->coo_row_index_cache[i] << " " << matrix_struct->coo_col_index_cache[i] << " ";
if (matrix_struct->val_data_type == DOUBLE)
{
OsWrite << ((double *)matrix_struct->coo_value_cache)[i] << endl;
}
else
{
OsWrite << ((float *)matrix_struct->coo_value_cache)[i] << endl;
}
}
OsWrite.close();
}
// 检查分块是不是正确的,主要是查对于coo索引范围的取值范围是不是对的
bool check_dense_block_div(sparse_struct_t *matrix_struct)
{
// 所有的分块索引
vector<dense_block_table_item_t *> table_item_arr = matrix_struct->block_coor_table.item_arr;
// 遍历所有的块,一个个检查
int i;
for (i = 0; i < table_item_arr.size(); i++)
{
// 遍历所有的分块表
dense_block_table_item_t *item = table_item_arr[i];
// 查看是不是增序排列
unsigned long last_row_index = matrix_struct->coo_row_index_cache[item->begin_coo_index];
unsigned long last_col_index = matrix_struct->coo_col_index_cache[item->begin_coo_index];
// 遍历所有coo非零元
int j;
for (j = item->begin_coo_index; j <= item->end_coo_index; j++)
{
// 查看是不是查看是不是换行了,查看列是不是增序排列
if (last_row_index < matrix_struct->coo_row_index_cache[j])
{
// 换行了,重新计算一下最后一个列号
last_col_index = matrix_struct->coo_col_index_cache[j];
}
if (last_col_index > matrix_struct->coo_col_index_cache[j])
{
cout << "col index is not sorted in increased order" << endl;
return false;
}
if (last_row_index > matrix_struct->coo_row_index_cache[j])
{
cout << "row index is not sorted in increasing order" << endl;
return false;
}
if (matrix_struct->coo_row_index_cache[j] < item->min_dense_row_index || matrix_struct->coo_row_index_cache[j] > item->max_dense_row_index)
{
cout << "row index is not in legal limits" << endl;
return false;
}
if (matrix_struct->coo_col_index_cache[j] < item->min_dense_col_index || matrix_struct->coo_col_index_cache[j] > item->max_dense_col_index)
{
cout << "col index is not in legal limits" << endl;
return false;
}
last_row_index = matrix_struct->coo_row_index_cache[j];
}
}
return true;
}
// 将数据类型转化为大写字符串
string convert_data_type_to_string(data_type type)
{
if (type == CHAR)
{
return "CHAR";
}
if (type == UNSIGNED_CHAR)
{
return "UNSIGNED_CHAR";
}
if (type == SHORT)
{
return "SHORT";
}
if (type == UNSIGNED_SHORT)
{
return "UNSIGNED_SHORT";
}
if (type == INT)
{
return "INT";
}
if (type == UNSIGNED_INT)
{
return "UNSIGNED_INT";
}
if (type == LONG)
{
return "LONG";
}
if (type == UNSIGNED_LONG)
{
return "UNSIGNED_LONG";
}
if (type == LONG_LONG)
{
return "LONG_LONG";
}
if (type == UNSIGNED_LONG_LONG)
{
return "UNSIGNED_LONG_LONG";
}
if (type == FLOAT)
{
return "FLOAT";
}
if (type == DOUBLE)
{
return "DOUBLE";
}
assert(false);
return "";
}
// 打印数据类型
void print_data_type(data_type type)
{
if (type == CHAR)
{
cout << "CHAR" << endl;
return;
}
if (type == UNSIGNED_CHAR)
{
cout << "UNSIGNED_CHAR" << endl;
return;
}
if (type == SHORT)
{
cout << "SHORT" << endl;
return;
}
if (type == UNSIGNED_SHORT)
{
cout << "UNSIGNED_SHORT" << endl;
return;
}
if (type == INT)
{
cout << "INT" << endl;
return;
}
if (type == UNSIGNED_INT)
{
cout << "UNSIGNED_INT" << endl;
return;
}
if (type == LONG)
{
cout << "LONG" << endl;
return;
}
if (type == UNSIGNED_LONG)
{
cout << "UNSIGNED_LONG" << endl;
return;
}
if (type == LONG_LONG)
{
cout << "LONG_LONG" << endl;
return;
}
if (type == UNSIGNED_LONG_LONG)
{
cout << "UNSIGNED_LONG_LONG" << endl;
return;
}
if (type == FLOAT)
{
cout << "FLOAT" << endl;
return;
}
if (type == DOUBLE)
{
cout << "DOUBLE" << endl;
return;
}
}
string read_str_from_command_line(int argc,char **argv, int cmd_input_index)
{
assert(cmd_input_index < argc);
assert(argv != NULL && *argv != NULL);
const char* str_ptr = argv[cmd_input_index];
return str_ptr;
}
void memcpy_with_data_type(void *dest, void *source, unsigned long source_size, data_type type)
{
assert(source_size > 0);
if (type == UNSIGNED_CHAR)
{
memcpy(dest, source, sizeof(unsigned char) * source_size);
return;
}
if (type == UNSIGNED_SHORT)
{
memcpy(dest, source, sizeof(unsigned short) * source_size);
return;
}
if (type == UNSIGNED_INT)
{
memcpy(dest, source, sizeof(unsigned int) * source_size);
return;
}
if (type == UNSIGNED_LONG)
{
memcpy(dest, source, sizeof(unsigned long) * source_size);
return;
}
if (type == FLOAT)
{
memcpy(dest, source, sizeof(float) * source_size);
return;
}
if (type == DOUBLE)
{
memcpy(dest, source, sizeof(double) * source_size);
return;
}
if (type == BOOL)
{
memcpy(dest, source, sizeof(bool) * source_size);
return;
}
assert(false);
}
void *val_copy_from_old_arr_with_data_type(void *source, unsigned long source_size, data_type type)
{
assert(source != NULL && source_size > 0);
// 只有四种类型 // 还有两种浮点类型
assert(type == UNSIGNED_CHAR || type == UNSIGNED_INT ||
type == UNSIGNED_SHORT || type == UNSIGNED_LONG ||
type == DOUBLE || type == FLOAT || type == BOOL);
// 申请一个数组
void *return_arr_ptr = malloc_arr(source_size, type);
// 将数组拷贝到新的数组中
memcpy_with_data_type(return_arr_ptr, source, source_size, type);
assert(return_arr_ptr != NULL);
return return_arr_ptr;
}
void *malloc_arr(unsigned long length, data_type type_of_arr)
{
// 只有四种类型 // 还有两种浮点类型
assert(type_of_arr == UNSIGNED_CHAR || type_of_arr == UNSIGNED_INT ||
type_of_arr == UNSIGNED_SHORT || type_of_arr == UNSIGNED_LONG ||
type_of_arr == DOUBLE || type_of_arr == FLOAT || type_of_arr == BOOL ||
type_of_arr == CHAR || type_of_arr == INT || type_of_arr == SHORT ||
type_of_arr == LONG);
assert(length > 0);
// 申请数组
if (type_of_arr == UNSIGNED_CHAR)
{
// cout << "123" << endl;
return new unsigned char[length];
}
else if (type_of_arr == UNSIGNED_SHORT)
{
// cout << "1231" << endl;
return new unsigned short[length];
}
else if (type_of_arr == UNSIGNED_INT)
{
// cout << "1232," << length << endl;
unsigned int *return_ptr = new unsigned int[length];
// cout << "1" << endl;
return (void *)return_ptr;
}
else if (type_of_arr == DOUBLE)
{
// cout << "1233" << endl;
return new double[length];
}
else if (type_of_arr == FLOAT)
{
// cout << "1234" << endl;
return new float[length];
}
else if (type_of_arr == BOOL)