forked from AnonymousRepo123/AlphaSparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spmv_header_top.code
executable file
·413 lines (327 loc) · 9.88 KB
/
spmv_header_top.code
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
#include <cuda_runtime.h>
#include <cstdlib>
#include <stdio.h>
#include <vector>
#include <assert.h>
#include <fstream>
#include <string.h>
#include <iostream>
#include <assert.h>
#include <sys/time.h>
using namespace std;
enum compressed_block_index_type
{
COO,
CSR,
ELL
};
enum index_type
{
BLOCK_INDEX,
ROW_INDEX,
COL_INDEX
};
enum data_type
{
CHAR,
UNSIGNED_CHAR,
SHORT,
UNSIGNED_SHORT,
INT,
UNSIGNED_INT,
LONG,
UNSIGNED_LONG,
LONG_LONG,
UNSIGNED_LONG_LONG,
FLOAT,
DOUBLE,
BOOL
};
enum index_level
{
TBLOCK_LEVEL,
WRAP_LEVEL,
THREAD_LEVEL,
OTHERS
};
typedef struct index_of_compressed_block
{
index_level level_of_this_index;
compressed_block_index_type index_compressed_type;
unsigned long block_num;
void *index_arr;
unsigned long length;
data_type index_data_type;
index_type type_of_index;
bool *is_sort_arr;
void *index_of_the_first_row_arr;
data_type data_type_of_index_of_the_first_row_arr;
void *row_number_of_block_arr;
data_type data_type_of_row_number_of_block_arr;
void *tmp_result_write_index_arr;
data_type data_type_of_tmp_result_write_index_arr;
unsigned long max_row_index;
unsigned long min_row_index;
unsigned long max_col_index;
unsigned long min_col_index;
void *coo_begin_index_arr;
data_type data_type_of_coo_begin_index_arr;
void *coo_block_size_arr;
data_type data_type_of_coo_block_size_arr;
void *child_tmp_row_csr_index_arr;
data_type data_type_of_child_tmp_row_csr_index;
unsigned long size_of_child_tmp_row_csr_index;
void *begin_index_in_tmp_row_csr_arr_of_block;
data_type data_type_of_begin_index_in_tmp_row_csr_arr_of_block;
} index_of_compress_block_t;
typedef struct compressed_block
{
vector<index_of_compress_block_t *> read_index;
vector<index_of_compress_block_t *> y_write_index;
vector<index_of_compress_block_t *> reduce_help_csr;
bool is_sorted;
int size;
data_type val_data_type;
void *val_arr;
int padding_arr_size;
void *padding_val_arr;
int staggered_padding_val_arr_size;
void *staggered_padding_val_arr;
} compressed_block_t;
typedef struct dense_block_table_item
{
vector<int> block_coordinate;
unsigned long min_dense_row_index;
unsigned long max_dense_row_index;
unsigned long max_dense_col_index;
unsigned long min_dense_col_index;
unsigned long begin_coo_index;
unsigned long end_coo_index;
bool is_sorted;
compressed_block_t *compressed_block_ptr;
} dense_block_table_item_t;
typedef struct dense_block_table
{
vector<dense_block_table_item_t *> item_arr;
} dense_block_table_t;
typedef struct x_arr
{
data_type x_data_type;
void *x_arr;
} x_arr_t;
typedef struct sparse_struct
{
unsigned long dense_row_number;
unsigned long dense_col_number;
unsigned long nnz;
bool is_blocked;
dense_block_table_t block_coor_table;
bool is_sorted;
data_type data_type_of_sorted_row_index;
void *sorted_row_index;
compressed_block *compressed_block_arr;
unsigned long *coo_row_index_cache;
unsigned long *coo_col_index_cache;
data_type val_data_type;
void *coo_value_cache;
x_arr_t coo_x_cache;
} sparse_struct_t;
void write_double_to_array_with_data_type(void *arr, data_type type, unsigned long write_pos, double write_val)
{
assert(type == DOUBLE || type == FLOAT);
if (type == DOUBLE)
{
double *input_arr = (double *)arr;
input_arr[write_pos] = write_val;
}
if (type == FLOAT)
{
float *input_arr = (float *)arr;
input_arr[write_pos] = write_val;
}
}
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);
assert(length > 0);
if (type_of_arr == UNSIGNED_CHAR)
{
return new unsigned char[length];
}
else if (type_of_arr == UNSIGNED_SHORT)
{
return new unsigned short[length];
}
else if (type_of_arr == UNSIGNED_INT)
{
unsigned int *return_ptr = new unsigned int[length];
return (void *)return_ptr;
}
else if (type_of_arr == DOUBLE)
{
return new double[length];
}
else if (type_of_arr == FLOAT)
{
return new float[length];
}
else
{
return new unsigned long[length];
}
}
void write_to_array_with_data_type(void *arr, data_type type, unsigned long write_pos, unsigned long write_val)
{
assert(type == UNSIGNED_LONG || type == UNSIGNED_INT || type == UNSIGNED_SHORT || type == UNSIGNED_CHAR);
if (type == UNSIGNED_LONG)
{
unsigned long *input_arr = (unsigned long *)arr;
input_arr[write_pos] = write_val;
}
if (type == UNSIGNED_INT)
{
unsigned int *input_arr = (unsigned int *)arr;
input_arr[write_pos] = write_val;
}
if (type == UNSIGNED_SHORT)
{
unsigned short *input_arr = (unsigned short *)arr;
input_arr[write_pos] = write_val;
}
if (type == UNSIGNED_CHAR)
{
unsigned char *input_arr = (unsigned char *)arr;
input_arr[write_pos] = write_val;
}
}
void *read_arr_from_file_with_data_type(unsigned long length, data_type arr_data_type, string file_name)
{
assert(length > 0);
assert(arr_data_type == UNSIGNED_LONG || arr_data_type == UNSIGNED_INT || arr_data_type == UNSIGNED_SHORT || arr_data_type == UNSIGNED_CHAR ||
arr_data_type == BOOL || arr_data_type == FLOAT || arr_data_type == DOUBLE);
void *arr_need_to_return = malloc_arr(length, arr_data_type);
unsigned long cur_insert_index = 0;
if (arr_data_type == UNSIGNED_LONG || arr_data_type == UNSIGNED_INT || arr_data_type == UNSIGNED_SHORT || arr_data_type == UNSIGNED_CHAR || arr_data_type == BOOL)
{
char buf[1024];
ifstream infile;
infile.open(file_name);
if (infile.is_open())
{
while (infile.good() && !infile.eof())
{
string line_str;
memset(buf, 0, 1024);
infile.getline(buf, 1024);
line_str = buf;
if (isspace(line_str[0]) || line_str.empty())
{
continue;
}
unsigned long arr_val = atol(line_str.c_str());
assert(cur_insert_index < length);
write_to_array_with_data_type(arr_need_to_return, arr_data_type, cur_insert_index, arr_val);
cur_insert_index++;
}
}
assert(cur_insert_index == length);
infile.close();
return arr_need_to_return;
}
else if (arr_data_type == DOUBLE || arr_data_type == FLOAT)
{
char buf[1024];
ifstream infile;
infile.open(file_name);
while (infile.good() && !infile.eof())
{
string line_str;
memset(buf, 0, 1024);
infile.getline(buf, 1024);
line_str = buf;
if (isspace(line_str[0]) || line_str.empty())
{
continue;
}
double arr_val = stod(line_str.c_str());
assert(cur_insert_index < length);
write_double_to_array_with_data_type(arr_need_to_return, arr_data_type, cur_insert_index, arr_val);
cur_insert_index++;
}
assert(cur_insert_index == length);
infile.close();
return arr_need_to_return;
}
return arr_need_to_return;
}
unsigned long read_from_array_with_data_type(void *arr, data_type type, unsigned long read_pos)
{
assert(type == UNSIGNED_LONG || type == UNSIGNED_INT || type == UNSIGNED_SHORT || type == UNSIGNED_CHAR || type == BOOL);
if (type == UNSIGNED_LONG)
{
unsigned long *output_arr = (unsigned long *)arr;
return (unsigned long)(output_arr[read_pos]);
}
if (type == UNSIGNED_INT)
{
unsigned int *output_arr = (unsigned int *)arr;
return (unsigned long)(output_arr[read_pos]);
}
if (type == UNSIGNED_SHORT)
{
unsigned short *output_arr = (unsigned short *)arr;
return (unsigned short)(output_arr[read_pos]);
}
if (type == UNSIGNED_CHAR)
{
unsigned char *output_arr = (unsigned char *)arr;
return (unsigned char)(output_arr[read_pos]);
}
if (type == BOOL)
{
bool *output_arr = (bool *)arr;
return (bool)(output_arr[read_pos]);
}
cout << "error" << endl;
exit(-1);
return 0;
}
double read_double_from_array_with_data_type(void *arr, data_type type, unsigned long read_pos)
{
assert(type == DOUBLE || type == FLOAT);
if (type == DOUBLE)
{
double *output_arr = (double *)arr;
return (double)(output_arr[read_pos]);
}
if (type == FLOAT)
{
float *output_arr = (float *)arr;
return (double)(output_arr[read_pos]);
}
return 0;
}
void print_arr_to_file_with_data_type(void *arr, data_type type, unsigned long length, string file_name)
{
assert(type == UNSIGNED_CHAR || type == UNSIGNED_INT || type == UNSIGNED_SHORT || type == UNSIGNED_LONG || type == DOUBLE || type == FLOAT || type == BOOL);
ofstream arrWrite(file_name, ios::out | ios::trunc);
if (type == UNSIGNED_CHAR || type == UNSIGNED_INT || type == UNSIGNED_SHORT || type == UNSIGNED_LONG || type == BOOL)
{
unsigned long i;
for (i = 0; i < length; i++)
{
arrWrite << read_from_array_with_data_type(arr, type, i) << endl;
}
}
else if (type == DOUBLE || type == FLOAT)
{
unsigned long i;
for (i = 0; i < length; i++)
{
arrWrite << read_double_from_array_with_data_type(arr, type, i) << endl;
}
}
arrWrite.close();
}