-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspgemm_full.c
618 lines (560 loc) · 21.8 KB
/
spgemm_full.c
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
// Generated by the Tensor Algebra Compiler (tensor-compiler.org)
// taco "A(i,j)=B(i,k)*C(k,j)" -f=A:ds:0,1 -f=B:ds:0,1 -f=C:ds:0,1 -s="reorder(i,k,j)" -s="precompute(B(i,k)*C(k,j),j,j)" -s="assemble(A,Insert)" -s="parallelize(i,CPUThread,NoRaces)" -write-source=taco_kernel.c -write-compute=taco_compute.c -write-assembly=taco_assembly.c
#ifndef TACO_C_HEADERS
#define TACO_C_HEADERS
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <complex.h>
#include <string.h>
#if _OPENMP
#include <omp.h>
#endif
#define TACO_MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
#define TACO_MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
#define TACO_DEREF(_a) (((___context___*)(*__ctx__))->_a)
#ifndef TACO_TENSOR_T_DEFINED
#define TACO_TENSOR_T_DEFINED
typedef enum { taco_mode_dense, taco_mode_sparse } taco_mode_t;
typedef struct {
int32_t order; // tensor order (number of modes)
int32_t* dimensions; // tensor dimensions
int32_t csize; // component size
int32_t* mode_ordering; // mode storage ordering
taco_mode_t* mode_types; // mode storage types
uint8_t*** indices; // tensor index data (per mode)
uint8_t* vals; // tensor values
uint8_t* fill_value; // tensor fill value
int32_t vals_size; // values array size
} taco_tensor_t;
#endif
#if !_OPENMP
int omp_get_thread_num() { return 0; }
int omp_get_max_threads() { return 1; }
#endif
int cmp(const void *a, const void *b) {
return *((const int*)a) - *((const int*)b);
}
int taco_gallop(int *array, int arrayStart, int arrayEnd, int target) {
if (array[arrayStart] >= target || arrayStart >= arrayEnd) {
return arrayStart;
}
int step = 1;
int curr = arrayStart;
while (curr + step < arrayEnd && array[curr + step] < target) {
curr += step;
step = step * 2;
}
step = step / 2;
while (step > 0) {
if (curr + step < arrayEnd && array[curr + step] < target) {
curr += step;
}
step = step / 2;
}
return curr+1;
}
int taco_binarySearchAfter(int *array, int arrayStart, int arrayEnd, int target) {
if (array[arrayStart] >= target) {
return arrayStart;
}
int lowerBound = arrayStart; // always < target
int upperBound = arrayEnd; // always >= target
while (upperBound - lowerBound > 1) {
int mid = (upperBound + lowerBound) / 2;
int midValue = array[mid];
if (midValue < target) {
lowerBound = mid;
}
else if (midValue > target) {
upperBound = mid;
}
else {
return mid;
}
}
return upperBound;
}
int taco_binarySearchBefore(int *array, int arrayStart, int arrayEnd, int target) {
if (array[arrayEnd] <= target) {
return arrayEnd;
}
int lowerBound = arrayStart; // always <= target
int upperBound = arrayEnd; // always > target
while (upperBound - lowerBound > 1) {
int mid = (upperBound + lowerBound) / 2;
int midValue = array[mid];
if (midValue < target) {
lowerBound = mid;
}
else if (midValue > target) {
upperBound = mid;
}
else {
return mid;
}
}
return lowerBound;
}
taco_tensor_t* init_taco_tensor_t(int32_t order, int32_t csize,
int32_t* dimensions, int32_t* mode_ordering,
taco_mode_t* mode_types) {
taco_tensor_t* t = (taco_tensor_t *) malloc(sizeof(taco_tensor_t));
t->order = order;
t->dimensions = (int32_t *) malloc(order * sizeof(int32_t));
t->mode_ordering = (int32_t *) malloc(order * sizeof(int32_t));
t->mode_types = (taco_mode_t *) malloc(order * sizeof(taco_mode_t));
t->indices = (uint8_t ***) malloc(order * sizeof(uint8_t***));
t->csize = csize;
for (int32_t i = 0; i < order; i++) {
t->dimensions[i] = dimensions[i];
t->mode_ordering[i] = mode_ordering[i];
t->mode_types[i] = mode_types[i];
switch (t->mode_types[i]) {
case taco_mode_dense:
t->indices[i] = (uint8_t **) malloc(1 * sizeof(uint8_t **));
break;
case taco_mode_sparse:
t->indices[i] = (uint8_t **) malloc(2 * sizeof(uint8_t **));
break;
}
}
return t;
}
void deinit_taco_tensor_t(taco_tensor_t* t) {
for (int i = 0; i < t->order; i++) {
free(t->indices[i]);
}
free(t->indices);
free(t->dimensions);
free(t->mode_ordering);
free(t->mode_types);
free(t);
}
#endif
int compute(taco_tensor_t *A, taco_tensor_t *B, taco_tensor_t *C) {
int A1_dimension = (int)(A->dimensions[0]);
int* restrict A2_pos = (int*)(A->indices[1][0]);
double* restrict A_vals = (double*)(A->vals);
int B1_dimension = (int)(B->dimensions[0]);
int* restrict B2_pos = (int*)(B->indices[1][0]);
int* restrict B2_crd = (int*)(B->indices[1][1]);
double* restrict B_vals = (double*)(B->vals);
int C1_dimension = (int)(C->dimensions[0]);
int C2_dimension = (int)(C->dimensions[1]);
int* restrict C2_pos = (int*)(C->indices[1][0]);
int* restrict C2_crd = (int*)(C->indices[1][1]);
double* restrict C_vals = (double*)(C->vals);
double* restrict workspace_all = 0;
int32_t* restrict workspace_index_list_all = 0;
workspace_index_list_all = (int32_t*)malloc(sizeof(int32_t) * (C2_dimension * omp_get_max_threads()));
bool* restrict workspace_already_set_all = calloc((C2_dimension * omp_get_max_threads()), sizeof(bool));
workspace_all = (double*)malloc(sizeof(double) * (C2_dimension * omp_get_max_threads()));
#pragma omp parallel for schedule(runtime)
for (int32_t i = 0; i < B1_dimension; i++) {
int32_t workspace_index_list_size = 0;
double* restrict workspace = workspace_all + C2_dimension * omp_get_thread_num();
int32_t* restrict workspace_index_list = workspace_index_list_all + C2_dimension * omp_get_thread_num();
bool* restrict workspace_already_set = workspace_already_set_all + C2_dimension * omp_get_thread_num();
for (int32_t kB = B2_pos[i]; kB < B2_pos[(i + 1)]; kB++) {
int32_t k = B2_crd[kB];
for (int32_t jC = C2_pos[k]; jC < C2_pos[(k + 1)]; jC++) {
int32_t j = C2_crd[jC];
if (!workspace_already_set[j]) {
workspace[j] = B_vals[kB] * C_vals[jC];
workspace_index_list[workspace_index_list_size] = j;
workspace_already_set[j] = 1;
workspace_index_list_size++;
}
else {
workspace[j] = workspace[j] + B_vals[kB] * C_vals[jC];
}
}
}
qsort(workspace_index_list, workspace_index_list_size, sizeof(int32_t), cmp);
for (int32_t workspace_index_locator = 0; workspace_index_locator < workspace_index_list_size; workspace_index_locator++) {
int32_t j = workspace_index_list[workspace_index_locator];
int32_t pA2 = A2_pos[i];
A2_pos[i] = A2_pos[i] + 1;
A_vals[pA2] = workspace[j];
workspace_already_set[j] = 0;
}
}
free(workspace_index_list_all);
free(workspace_already_set_all);
free(workspace_all);
for (int32_t p = 0; p < A1_dimension; p++) {
A2_pos[A1_dimension - p] = A2_pos[((A1_dimension - p) - 1)];
}
A2_pos[0] = 0;
A->indices[1][0] = (uint8_t*)(A2_pos);
A->vals = (uint8_t*)A_vals;
return 0;
}
int assemble(taco_tensor_t *A, taco_tensor_t *B, taco_tensor_t *C) {
int A1_dimension = (int)(A->dimensions[0]);
int* restrict A2_pos = (int*)(A->indices[1][0]);
int* restrict A2_crd = (int*)(A->indices[1][1]);
double* restrict A_vals = (double*)(A->vals);
int B1_dimension = (int)(B->dimensions[0]);
int* restrict B2_pos = (int*)(B->indices[1][0]);
int* restrict B2_crd = (int*)(B->indices[1][1]);
int C1_dimension = (int)(C->dimensions[0]);
int C2_dimension = (int)(C->dimensions[1]);
int* restrict C2_pos = (int*)(C->indices[1][0]);
int* restrict C2_crd = (int*)(C->indices[1][1]);
int32_t* restrict A2_nnz = 0;
A2_nnz = (int32_t*)malloc(sizeof(int32_t) * B1_dimension);
int32_t* restrict qworkspace_index_list_all = 0;
qworkspace_index_list_all = (int32_t*)malloc(sizeof(int32_t) * (C2_dimension * omp_get_max_threads()));
bool* restrict qworkspace_already_set_all = calloc((C2_dimension * omp_get_max_threads()), sizeof(bool));
#pragma omp parallel for schedule(runtime)
for (int32_t i = 0; i < B1_dimension; i++) {
int32_t qworkspace_index_list_size = 0;
int32_t* restrict qworkspace_index_list = qworkspace_index_list_all + C2_dimension * omp_get_thread_num();
bool* restrict qworkspace_already_set = qworkspace_already_set_all + C2_dimension * omp_get_thread_num();
for (int32_t kB = B2_pos[i]; kB < B2_pos[(i + 1)]; kB++) {
int32_t k = B2_crd[kB];
for (int32_t jC = C2_pos[k]; jC < C2_pos[(k + 1)]; jC++) {
int32_t j = C2_crd[jC];
if (!qworkspace_already_set[j]) {
qworkspace_index_list[qworkspace_index_list_size] = j;
qworkspace_already_set[j] = 1;
qworkspace_index_list_size++;
}
}
}
int32_t tjA2_nnz_val = 0;
for (int32_t qworkspace_index_locator = 0; qworkspace_index_locator < qworkspace_index_list_size; qworkspace_index_locator++) {
int32_t j = qworkspace_index_list[qworkspace_index_locator];
tjA2_nnz_val++;
qworkspace_already_set[j] = 0;
}
A2_nnz[i] = tjA2_nnz_val;
}
free(qworkspace_index_list_all);
free(qworkspace_already_set_all);
A2_pos = (int32_t*)malloc(sizeof(int32_t) * (A1_dimension + 1));
A2_pos[0] = 0;
for (int32_t i = 0; i < A1_dimension; i++) {
A2_pos[i + 1] = A2_pos[i] + A2_nnz[i];
}
A2_crd = (int32_t*)malloc(sizeof(int32_t) * A2_pos[A1_dimension]);
A_vals = (double*)malloc(sizeof(double) * A2_pos[A1_dimension]);
int32_t* restrict workspace_index_list_all = 0;
workspace_index_list_all = (int32_t*)malloc(sizeof(int32_t) * (C2_dimension * omp_get_max_threads()));
bool* restrict workspace_already_set_all = calloc((C2_dimension * omp_get_max_threads()), sizeof(bool));
#pragma omp parallel for schedule(runtime)
for (int32_t i = 0; i < B1_dimension; i++) {
int32_t workspace_index_list_size = 0;
int32_t* restrict workspace_index_list = workspace_index_list_all + C2_dimension * omp_get_thread_num();
bool* restrict workspace_already_set = workspace_already_set_all + C2_dimension * omp_get_thread_num();
for (int32_t kB = B2_pos[i]; kB < B2_pos[(i + 1)]; kB++) {
int32_t k = B2_crd[kB];
for (int32_t jC = C2_pos[k]; jC < C2_pos[(k + 1)]; jC++) {
int32_t j = C2_crd[jC];
if (!workspace_already_set[j]) {
workspace_index_list[workspace_index_list_size] = j;
workspace_already_set[j] = 1;
workspace_index_list_size++;
}
}
}
qsort(workspace_index_list, workspace_index_list_size, sizeof(int32_t), cmp);
for (int32_t workspace_index_locator = 0; workspace_index_locator < workspace_index_list_size; workspace_index_locator++) {
int32_t j = workspace_index_list[workspace_index_locator];
int32_t pA2 = A2_pos[i];
A2_pos[i] = A2_pos[i] + 1;
A2_crd[pA2] = j;
workspace_already_set[j] = 0;
}
}
free(workspace_index_list_all);
free(workspace_already_set_all);
for (int32_t p = 0; p < A1_dimension; p++) {
A2_pos[A1_dimension - p] = A2_pos[((A1_dimension - p) - 1)];
}
A2_pos[0] = 0;
free(A2_nnz);
A->indices[1][0] = (uint8_t*)(A2_pos);
A->indices[1][1] = (uint8_t*)(A2_crd);
A->vals = (uint8_t*)A_vals;
return 0;
}
int evaluate(taco_tensor_t *A, taco_tensor_t *B, taco_tensor_t *C) {
int A1_dimension = (int)(A->dimensions[0]);
int* restrict A2_pos = (int*)(A->indices[1][0]);
int* restrict A2_crd = (int*)(A->indices[1][1]);
double* restrict A_vals = (double*)(A->vals);
int B1_dimension = (int)(B->dimensions[0]);
int* restrict B2_pos = (int*)(B->indices[1][0]);
int* restrict B2_crd = (int*)(B->indices[1][1]);
double* restrict B_vals = (double*)(B->vals);
int C1_dimension = (int)(C->dimensions[0]);
int C2_dimension = (int)(C->dimensions[1]);
int* restrict C2_pos = (int*)(C->indices[1][0]);
int* restrict C2_crd = (int*)(C->indices[1][1]);
double* restrict C_vals = (double*)(C->vals);
int32_t* restrict A2_nnz = 0;
A2_nnz = (int32_t*)malloc(sizeof(int32_t) * B1_dimension);
int32_t* restrict qworkspace_index_list_all = 0;
qworkspace_index_list_all = (int32_t*)malloc(sizeof(int32_t) * (C2_dimension * omp_get_max_threads()));
bool* restrict qworkspace_already_set_all = calloc((C2_dimension * omp_get_max_threads()), sizeof(bool));
#pragma omp parallel for schedule(runtime)
for (int32_t i = 0; i < B1_dimension; i++) {
int32_t qworkspace_index_list_size = 0;
int32_t* restrict qworkspace_index_list = qworkspace_index_list_all + C2_dimension * omp_get_thread_num();
bool* restrict qworkspace_already_set = qworkspace_already_set_all + C2_dimension * omp_get_thread_num();
for (int32_t kB = B2_pos[i]; kB < B2_pos[(i + 1)]; kB++) {
int32_t k = B2_crd[kB];
for (int32_t jC = C2_pos[k]; jC < C2_pos[(k + 1)]; jC++) {
int32_t j = C2_crd[jC];
if (!qworkspace_already_set[j]) {
qworkspace_index_list[qworkspace_index_list_size] = j;
qworkspace_already_set[j] = 1;
qworkspace_index_list_size++;
}
}
}
int32_t tjA2_nnz_val = 0;
for (int32_t qworkspace_index_locator = 0; qworkspace_index_locator < qworkspace_index_list_size; qworkspace_index_locator++) {
int32_t j = qworkspace_index_list[qworkspace_index_locator];
tjA2_nnz_val++;
qworkspace_already_set[j] = 0;
}
A2_nnz[i] = tjA2_nnz_val;
}
free(qworkspace_index_list_all);
free(qworkspace_already_set_all);
A2_pos = (int32_t*)malloc(sizeof(int32_t) * (A1_dimension + 1));
A2_pos[0] = 0;
for (int32_t i = 0; i < A1_dimension; i++) {
A2_pos[i + 1] = A2_pos[i] + A2_nnz[i];
}
A2_crd = (int32_t*)malloc(sizeof(int32_t) * A2_pos[A1_dimension]);
A_vals = (double*)malloc(sizeof(double) * A2_pos[A1_dimension]);
double* restrict workspace_all = 0;
int32_t* restrict workspace_index_list_all = 0;
workspace_index_list_all = (int32_t*)malloc(sizeof(int32_t) * (C2_dimension * omp_get_max_threads()));
bool* restrict workspace_already_set_all = calloc((C2_dimension * omp_get_max_threads()), sizeof(bool));
workspace_all = (double*)malloc(sizeof(double) * (C2_dimension * omp_get_max_threads()));
#pragma omp parallel for schedule(runtime)
for (int32_t i = 0; i < B1_dimension; i++) {
int32_t workspace_index_list_size = 0;
double* restrict workspace = workspace_all + C2_dimension * omp_get_thread_num();
int32_t* restrict workspace_index_list = workspace_index_list_all + C2_dimension * omp_get_thread_num();
bool* restrict workspace_already_set = workspace_already_set_all + C2_dimension * omp_get_thread_num();
for (int32_t kB = B2_pos[i]; kB < B2_pos[(i + 1)]; kB++) {
int32_t k = B2_crd[kB];
for (int32_t jC = C2_pos[k]; jC < C2_pos[(k + 1)]; jC++) {
int32_t j = C2_crd[jC];
if (!workspace_already_set[j]) {
workspace[j] = B_vals[kB] * C_vals[jC];
workspace_index_list[workspace_index_list_size] = j;
workspace_already_set[j] = 1;
workspace_index_list_size++;
}
else {
workspace[j] = workspace[j] + B_vals[kB] * C_vals[jC];
}
}
}
qsort(workspace_index_list, workspace_index_list_size, sizeof(int32_t), cmp);
for (int32_t workspace_index_locator = 0; workspace_index_locator < workspace_index_list_size; workspace_index_locator++) {
int32_t j = workspace_index_list[workspace_index_locator];
int32_t pA2 = A2_pos[i];
A2_pos[i] = A2_pos[i] + 1;
A2_crd[pA2] = j;
A_vals[pA2] = workspace[j];
workspace_already_set[j] = 0;
}
}
free(workspace_index_list_all);
free(workspace_already_set_all);
free(workspace_all);
for (int32_t p = 0; p < A1_dimension; p++) {
A2_pos[A1_dimension - p] = A2_pos[((A1_dimension - p) - 1)];
}
A2_pos[0] = 0;
free(A2_nnz);
A->indices[1][0] = (uint8_t*)(A2_pos);
A->indices[1][1] = (uint8_t*)(A2_crd);
A->vals = (uint8_t*)A_vals;
return 0;
}
/*
* The `pack` functions convert coordinate and value arrays in COO format,
* with nonzeros sorted lexicographically by their coordinates, to the
* specified input format.
*
* The `unpack` function converts the specified output format to coordinate
* and value arrays in COO format.
*
* For both, the `_COO_pos` arrays contain two elements, where the first is 0
* and the second is the number of nonzeros in the tensor.
*/
int pack_B(taco_tensor_t *B, int* B_COO1_pos, int* B_COO1_crd, int* B_COO2_crd, double* B_COO_vals) {
int B1_dimension = (int)(B->dimensions[0]);
int* restrict B2_pos = (int*)(B->indices[1][0]);
int* restrict B2_crd = (int*)(B->indices[1][1]);
double* restrict B_vals = (double*)(B->vals);
B2_pos = (int32_t*)malloc(sizeof(int32_t) * (B1_dimension + 1));
B2_pos[0] = 0;
for (int32_t pB2 = 1; pB2 < (B1_dimension + 1); pB2++) {
B2_pos[pB2] = 0;
}
int32_t B2_crd_size = 1048576;
B2_crd = (int32_t*)malloc(sizeof(int32_t) * B2_crd_size);
int32_t kB = 0;
int32_t B_capacity = 1048576;
B_vals = (double*)malloc(sizeof(double) * B_capacity);
int32_t iB_COO = B_COO1_pos[0];
int32_t pB_COO1_end = B_COO1_pos[1];
while (iB_COO < pB_COO1_end) {
int32_t i = B_COO1_crd[iB_COO];
int32_t B_COO1_segend = iB_COO + 1;
while (B_COO1_segend < pB_COO1_end && B_COO1_crd[B_COO1_segend] == i) {
B_COO1_segend++;
}
int32_t pB2_begin = kB;
int32_t kB_COO = iB_COO;
while (kB_COO < B_COO1_segend) {
int32_t k = B_COO2_crd[kB_COO];
double B_COO_val = B_COO_vals[kB_COO];
kB_COO++;
while (kB_COO < B_COO1_segend && B_COO2_crd[kB_COO] == k) {
B_COO_val += B_COO_vals[kB_COO];
kB_COO++;
}
if (B_capacity <= kB) {
B_vals = (double*)realloc(B_vals, sizeof(double) * (B_capacity * 2));
B_capacity *= 2;
}
B_vals[kB] = B_COO_val;
if (B2_crd_size <= kB) {
B2_crd = (int32_t*)realloc(B2_crd, sizeof(int32_t) * (B2_crd_size * 2));
B2_crd_size *= 2;
}
B2_crd[kB] = k;
kB++;
}
B2_pos[i + 1] = kB - pB2_begin;
iB_COO = B_COO1_segend;
}
int32_t csB2 = 0;
for (int32_t pB20 = 1; pB20 < (B1_dimension + 1); pB20++) {
csB2 += B2_pos[pB20];
B2_pos[pB20] = csB2;
}
B->indices[1][0] = (uint8_t*)(B2_pos);
B->indices[1][1] = (uint8_t*)(B2_crd);
B->vals = (uint8_t*)B_vals;
return 0;
}
int pack_C(taco_tensor_t *C, int* C_COO1_pos, int* C_COO1_crd, int* C_COO2_crd, double* C_COO_vals) {
int C1_dimension = (int)(C->dimensions[0]);
int* restrict C2_pos = (int*)(C->indices[1][0]);
int* restrict C2_crd = (int*)(C->indices[1][1]);
double* restrict C_vals = (double*)(C->vals);
C2_pos = (int32_t*)malloc(sizeof(int32_t) * (C1_dimension + 1));
C2_pos[0] = 0;
for (int32_t pC2 = 1; pC2 < (C1_dimension + 1); pC2++) {
C2_pos[pC2] = 0;
}
int32_t C2_crd_size = 1048576;
C2_crd = (int32_t*)malloc(sizeof(int32_t) * C2_crd_size);
int32_t jC = 0;
int32_t C_capacity = 1048576;
C_vals = (double*)malloc(sizeof(double) * C_capacity);
int32_t kC_COO = C_COO1_pos[0];
int32_t pC_COO1_end = C_COO1_pos[1];
while (kC_COO < pC_COO1_end) {
int32_t k = C_COO1_crd[kC_COO];
int32_t C_COO1_segend = kC_COO + 1;
while (C_COO1_segend < pC_COO1_end && C_COO1_crd[C_COO1_segend] == k) {
C_COO1_segend++;
}
int32_t pC2_begin = jC;
int32_t jC_COO = kC_COO;
while (jC_COO < C_COO1_segend) {
int32_t j = C_COO2_crd[jC_COO];
double C_COO_val = C_COO_vals[jC_COO];
jC_COO++;
while (jC_COO < C_COO1_segend && C_COO2_crd[jC_COO] == j) {
C_COO_val += C_COO_vals[jC_COO];
jC_COO++;
}
if (C_capacity <= jC) {
C_vals = (double*)realloc(C_vals, sizeof(double) * (C_capacity * 2));
C_capacity *= 2;
}
C_vals[jC] = C_COO_val;
if (C2_crd_size <= jC) {
C2_crd = (int32_t*)realloc(C2_crd, sizeof(int32_t) * (C2_crd_size * 2));
C2_crd_size *= 2;
}
C2_crd[jC] = j;
jC++;
}
C2_pos[k + 1] = jC - pC2_begin;
kC_COO = C_COO1_segend;
}
int32_t csC2 = 0;
for (int32_t pC20 = 1; pC20 < (C1_dimension + 1); pC20++) {
csC2 += C2_pos[pC20];
C2_pos[pC20] = csC2;
}
C->indices[1][0] = (uint8_t*)(C2_pos);
C->indices[1][1] = (uint8_t*)(C2_crd);
C->vals = (uint8_t*)C_vals;
return 0;
}
int unpack(int** A_COO1_pos_ptr, int** A_COO1_crd_ptr, int** A_COO2_crd_ptr, double** A_COO_vals_ptr, taco_tensor_t *A) {
int* A_COO1_pos;
int* A_COO1_crd;
int* A_COO2_crd;
double* A_COO_vals;
int A1_dimension = (int)(A->dimensions[0]);
int* restrict A2_pos = (int*)(A->indices[1][0]);
int* restrict A2_crd = (int*)(A->indices[1][1]);
double* restrict A_vals = (double*)(A->vals);
A_COO1_pos = (int32_t*)malloc(sizeof(int32_t) * 2);
A_COO1_pos[0] = 0;
int32_t A_COO1_crd_size = 1048576;
A_COO1_crd = (int32_t*)malloc(sizeof(int32_t) * A_COO1_crd_size);
int32_t A_COO2_crd_size = 1048576;
A_COO2_crd = (int32_t*)malloc(sizeof(int32_t) * A_COO2_crd_size);
int32_t jA_COO = 0;
int32_t A_COO_capacity = 1048576;
A_COO_vals = (double*)malloc(sizeof(double) * A_COO_capacity);
for (int32_t i = 0; i < A1_dimension; i++) {
for (int32_t jA = A2_pos[i]; jA < A2_pos[(i + 1)]; jA++) {
int32_t j = A2_crd[jA];
if (A_COO_capacity <= jA_COO) {
A_COO_vals = (double*)realloc(A_COO_vals, sizeof(double) * (A_COO_capacity * 2));
A_COO_capacity *= 2;
}
A_COO_vals[jA_COO] = A_vals[jA];
if (A_COO2_crd_size <= jA_COO) {
int32_t A_COO2_crd_new_size = TACO_MAX(A_COO2_crd_size * 2,(jA_COO + 1));
A_COO2_crd = (int32_t*)realloc(A_COO2_crd, sizeof(int32_t) * A_COO2_crd_new_size);
A_COO2_crd_size = A_COO2_crd_new_size;
}
A_COO2_crd[jA_COO] = j;
if (A_COO1_crd_size <= jA_COO) {
A_COO1_crd = (int32_t*)realloc(A_COO1_crd, sizeof(int32_t) * (A_COO1_crd_size * 2));
A_COO1_crd_size *= 2;
}
A_COO1_crd[jA_COO] = i;
jA_COO++;
}
}
A_COO1_pos[1] = jA_COO;
*A_COO1_pos_ptr = A_COO1_pos;
*A_COO1_crd_ptr = A_COO1_crd;
*A_COO2_crd_ptr = A_COO2_crd;
*A_COO_vals_ptr = A_COO_vals;
return 0;
}