-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSparseUtils.cpp
2626 lines (2355 loc) · 90.1 KB
/
SparseUtils.cpp
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
//===- SparseUtils.cpp - Sparse utils for MLIR execution -----------------===//
//
// Copyright 2022 Battelle Memorial Institute
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions
// and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
// and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
#include "comet/ExecutionEngine/RunnerUtils.h"
#include "llvm/Support/raw_ostream.h"
#include <assert.h>
#include <cstdint>
#include <iostream>
#include <sys/time.h>
#include <math.h>
#include <cstdio>
#include <limits>
#include <iomanip>
#include <random>
#include <map>
enum MatrixReadOption
{
DEFAULT = 1, /// standard matrix read
LOWER_TRI_STRICT = 2,
LOWER_TRI = 3,
UPPER_TRI_STRICT = 4,
UPPER_TRI = 5
};
/// helper func: inquire matrix read type
int getMatrixReadOption(int32_t readMode)
{
int selected_matrix_read = DEFAULT;
if (readMode == LOWER_TRI_STRICT)
selected_matrix_read = LOWER_TRI_STRICT;
else if (readMode == LOWER_TRI)
selected_matrix_read = LOWER_TRI;
else if (readMode == UPPER_TRI_STRICT)
selected_matrix_read = UPPER_TRI_STRICT;
else if (readMode == UPPER_TRI)
selected_matrix_read = UPPER_TRI;
return selected_matrix_read;
}
//===----------------------------------------------------------------------===//
/// Small runtime support library for sparse matrices/tensors.
//===----------------------------------------------------------------------===//
/// COO edge tuple
template <typename T>
struct CooTuple
{
uint64_t row;
uint64_t col;
T val;
CooTuple() {}
CooTuple(uint64_t row, uint64_t col) : row(row), col(col) {}
CooTuple(uint64_t row, uint64_t col, T val) : row(row), col(col), val(val) {}
};
//===----------------------------------------------------------------------===//
/// COO matrix type. A COO matrix is just a vector of edge tuples. Tuples are sorted
/// first by row, then by column.
//===----------------------------------------------------------------------===//
template <typename T>
struct CooMatrix
{
///---------------------------------------------------------------------
/// Data members
///---------------------------------------------------------------------
/// Fields
uint64_t num_rows;
uint64_t num_cols;
uint64_t num_nonzeros;
uint64_t num_nonzeros_lowerTri; /// triangular matrix read stats
uint64_t num_nonzeros_upperTri;
uint64_t num_nonzeros_lowerTri_strict;
uint64_t num_nonzeros_upperTri_strict;
CooTuple<T> *coo_tuples;
///---------------------------------------------------------------------
/// Methods
///---------------------------------------------------------------------
/// Constructor
CooMatrix() : num_rows(0), num_cols(0), num_nonzeros(0),
num_nonzeros_lowerTri(0), num_nonzeros_upperTri(0),
num_nonzeros_lowerTri_strict(0), num_nonzeros_upperTri_strict(0),
coo_tuples(NULL) {}
//===----------------------------------------------------------------------===//
// Clear
//===----------------------------------------------------------------------===//
void Clear()
{
if (coo_tuples)
delete[] coo_tuples;
coo_tuples = NULL;
}
/// Destructor
~CooMatrix()
{
/// do nothing. coo_tuples is now cleared from else-where.
/// Clear();
}
/// Display matrix to stdout
void Display()
{
cout << "COO Matrix (" << num_rows << " rows, " << num_cols << " columns, " << num_nonzeros << " non-zeros):\n";
cout << "Ordinal, Row, Column, Value\n";
for (uint64_t i = 0; i < num_nonzeros; i++)
{
cout << '\t' << i << ',' << coo_tuples[i].row << ',' << coo_tuples[i].col << ',' << coo_tuples[i].val << "\n";
}
}
//===----------------------------------------------------------------------===//
/// Builds a MARKET COO sparse from the given file.
//===----------------------------------------------------------------------===//
void InitMarket(
const string &market_filename,
T default_value = 1.0,
bool verbose = false)
{
if (verbose)
{
printf("Reading... ");
fflush(stdout);
}
if (coo_tuples)
{
fprintf(stderr, "ERROR: Matrix already constructed (abrupt exit)!\n");
// updated code should avoid coming to this path
exit(1);
}
std::ifstream ifs;
ifs.open(market_filename.c_str(), std::ifstream::in);
if (!ifs.good())
{
fprintf(stderr, "Error opening file\n");
exit(1);
}
bool array = false;
bool symmetric = false;
bool skew = false;
uint64_t current_nz = 0;
int nparsed = -1;
char line[1024];
if (verbose)
{
printf("Parsing... ");
fflush(stdout);
}
while (true)
{
ifs.getline(line, 1024);
if (!ifs.good())
{
/// Done
break;
}
if (line[0] == '%')
{
/// Comment
if (line[1] == '%')
{
/// Banner
symmetric = (strstr(line, "symmetric") != NULL);
skew = (strstr(line, "skew") != NULL);
array = (strstr(line, "array") != NULL);
if (verbose)
{
printf("(symmetric: %d, skew: %d, array: %d) ", symmetric, skew, array);
fflush(stdout);
}
}
}
else if (nparsed == -1)
{
/// Problem description
nparsed = sscanf(line, "%" PRIu64 " %" PRIu64 " %" PRIu64, &num_rows, &num_cols, &num_nonzeros);
if ((!array) && (nparsed == 3))
{
if (symmetric)
num_nonzeros *= 2;
/// Allocate coo matrix
coo_tuples = new CooTuple<T>[num_nonzeros];
current_nz = 0;
}
else if (array && (nparsed == 2))
{
/// Allocate coo matrix
num_nonzeros = num_rows * num_cols;
coo_tuples = new CooTuple<T>[num_nonzeros];
current_nz = 0;
}
else
{
fprintf(stderr, "Error parsing MARKET matrix: invalid problem description: %s\n", line);
exit(1);
}
}
else
{
/// Edge
if (current_nz >= num_nonzeros)
{
fprintf(stderr, "Error parsing MARKET matrix: encountered more than %" PRIu64 " num_nonzeros\n", num_nonzeros);
exit(1);
}
uint64_t row, col;
T val;
double tempVal = 0.0;
if (array)
{
if (sscanf(line, "%lf", &tempVal) != 1) /// using tempVal instead of templated T val to avoid warning
{
fprintf(stderr, "Error parsing MARKET matrix: badly formed current_nz: '%s' at edge %" PRIu64 "\n", line, current_nz);
exit(1);
}
val = (T)tempVal;
col = (current_nz / num_rows);
row = (current_nz - (num_rows * col));
coo_tuples[current_nz] = CooTuple<T>(row, col, val); /// Convert indices to zero-based
if (row > col)
{
num_nonzeros_lowerTri_strict++;
num_nonzeros_lowerTri++;
}
else if (row < col)
{
num_nonzeros_upperTri_strict++;
num_nonzeros_upperTri++;
}
else /// equal or diagonals
{
num_nonzeros_lowerTri++;
num_nonzeros_upperTri++;
}
}
else
{
/// Parse nonzero (note: using strtol and strtod is 2x faster than sscanf or istream parsing)
char *l = line;
char *t = NULL;
/// parse row
row = strtol(l, &t, 0);
if (t == l)
{
fprintf(stderr, "Error parsing MARKET matrix: badly formed row at edge %" PRIu64 "\n", current_nz);
exit(1);
}
l = t;
/// parse col
col = strtol(l, &t, 0);
if (t == l)
{
fprintf(stderr, "Error parsing MARKET matrix: badly formed col at edge %" PRIu64 "\n", current_nz);
exit(1);
}
l = t;
/// parse val
val = strtod(l, &t);
if (t == l)
{
val = default_value;
}
coo_tuples[current_nz] = CooTuple<T>(row - 1, col - 1, val); /// Convert indices to zero-based
if (row - 1 > col - 1)
{
num_nonzeros_lowerTri_strict++;
num_nonzeros_lowerTri++;
}
else if (row - 1 < col - 1)
{
num_nonzeros_upperTri_strict++;
num_nonzeros_upperTri++;
}
else /// equal or diagonals
{
num_nonzeros_lowerTri++;
num_nonzeros_upperTri++;
}
}
current_nz++;
if (symmetric && (row != col))
{
coo_tuples[current_nz].row = coo_tuples[current_nz - 1].col;
coo_tuples[current_nz].col = coo_tuples[current_nz - 1].row;
coo_tuples[current_nz].val = coo_tuples[current_nz - 1].val * (skew ? -1 : 1);
current_nz++;
}
}
}
/// Adjust nonzero count (nonzeros along the diagonal aren't reversed)
num_nonzeros = current_nz;
if (symmetric)
{
/// we only do one half if matrix is symmetric, so update num according to lower Triangle read.
num_nonzeros_upperTri = num_nonzeros_lowerTri;
num_nonzeros_upperTri_strict = num_nonzeros_lowerTri_strict;
}
if (verbose)
{
printf("done. ");
fflush(stdout);
}
ifs.close();
}
};
/// Sort by rows, then columns
struct CooComparatorRow
{
template <typename CooTuple>
bool operator()(const CooTuple &a, const CooTuple &b) const
{
return ((a.row < b.row) || ((a.row == b.row) && (a.col < b.col)));
}
};
/// Sort by cols, then rows
struct CooComparatorCol
{
template <typename CooTuple>
bool operator()(const CooTuple &a, const CooTuple &b) const
{
return ((a.col < b.col) || ((a.col == b.col) && (a.row < b.row)));
}
};
//===----------------------------------------------------------------------===//
/// CSR matrix type
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
/// CSR sparse format matrix
//===----------------------------------------------------------------------===//
template <typename T>
struct CsrMatrix
{
uint64_t num_rows;
uint64_t num_cols;
uint64_t num_nonzeros;
uint64_t num_nonzeros_lowerTri; /// triangular matrix read stats from COO
uint64_t num_nonzeros_upperTri;
uint64_t num_nonzeros_lowerTri_strict;
uint64_t num_nonzeros_upperTri_strict;
uint64_t *row_offsets;
uint64_t *column_indices;
T *values;
/**
* Initializer
*/
void Init(
CooMatrix<T> *coo_matrix,
int readMode,
bool verbose = false)
{
num_rows = coo_matrix->num_rows;
num_cols = coo_matrix->num_cols;
num_nonzeros = coo_matrix->num_nonzeros;
num_nonzeros_lowerTri = coo_matrix->num_nonzeros_lowerTri;
num_nonzeros_upperTri = coo_matrix->num_nonzeros_upperTri;
num_nonzeros_lowerTri_strict = coo_matrix->num_nonzeros_lowerTri_strict;
num_nonzeros_upperTri_strict = coo_matrix->num_nonzeros_upperTri_strict;
/// Sort by rows, then columns
if (verbose)
printf("Ordering...");
fflush(stdout);
std::stable_sort(coo_matrix->coo_tuples, coo_matrix->coo_tuples + num_nonzeros, CooComparatorRow());
if (verbose)
printf("done.");
fflush(stdout);
row_offsets = new uint64_t[num_rows + 1];
if (readMode == LOWER_TRI_STRICT)
{
column_indices = new uint64_t[num_nonzeros_lowerTri_strict];
values = new T[num_nonzeros_lowerTri_strict];
}
else if (readMode == LOWER_TRI)
{
column_indices = new uint64_t[num_nonzeros_lowerTri];
values = new T[num_nonzeros_lowerTri];
}
else if (readMode == UPPER_TRI_STRICT)
{
column_indices = new uint64_t[num_nonzeros_upperTri_strict];
values = new T[num_nonzeros_upperTri_strict];
}
else if (readMode == UPPER_TRI)
{
column_indices = new uint64_t[num_nonzeros_upperTri];
values = new T[num_nonzeros_upperTri];
}
else /// DEFAULT
{
column_indices = new uint64_t[num_nonzeros];
values = new T[num_nonzeros];
}
uint64_t prev_row = -1;
uint64_t curr_idx = 0;
for (uint64_t current_nz = 0; current_nz < num_nonzeros; current_nz++)
{
uint64_t current_row = coo_matrix->coo_tuples[current_nz].row;
uint64_t current_col = coo_matrix->coo_tuples[current_nz].col;
if (((readMode == LOWER_TRI_STRICT) || (readMode == LOWER_TRI)) &&
(current_row > current_col))
{
/// Fill in rows up to and including the current row
/// printf("\t\tLower> current_nz: %lld, curr_idx: %lld\n", current_nz, curr_idx);
for (uint64_t row = prev_row + 1; row <= current_row; row++)
{
row_offsets[row] = curr_idx;
}
prev_row = current_row;
column_indices[curr_idx] = coo_matrix->coo_tuples[current_nz].col;
values[curr_idx] = coo_matrix->coo_tuples[current_nz].val;
curr_idx++;
}
else if (((readMode == UPPER_TRI_STRICT) || (readMode == UPPER_TRI)) &&
(current_row < current_col))
{
for (uint64_t row = prev_row + 1; row <= current_row; row++)
{
row_offsets[row] = curr_idx;
}
prev_row = current_row;
column_indices[curr_idx] = coo_matrix->coo_tuples[current_nz].col;
values[curr_idx] = coo_matrix->coo_tuples[current_nz].val;
curr_idx++;
}
else if (((readMode == UPPER_TRI) || (readMode == LOWER_TRI)) &&
(current_row == current_col)) /// diagonals
{
for (uint64_t row = prev_row + 1; row <= current_row; row++)
{
row_offsets[row] = curr_idx;
}
prev_row = current_row;
column_indices[curr_idx] = coo_matrix->coo_tuples[current_nz].col;
values[curr_idx] = coo_matrix->coo_tuples[current_nz].val;
curr_idx++;
}
else if (readMode == DEFAULT) /// standard matrix read
{
/// Fill in rows up to and including the current row
/// printf("\t\tLower> current_nz: %lld, curr_idx: %lld\n", current_nz, curr_idx);
for (uint64_t row = prev_row + 1; row <= current_row; row++)
{
row_offsets[row] = curr_idx;
}
prev_row = current_row;
column_indices[curr_idx] = coo_matrix->coo_tuples[current_nz].col;
values[curr_idx] = coo_matrix->coo_tuples[current_nz].val;
curr_idx++;
}
} /// end-loop
/// Fill out any trailing edgeless vertices (and the end-of-list element)
for (uint64_t row = prev_row + 1; row <= num_rows; row++)
{
if (readMode == LOWER_TRI_STRICT)
row_offsets[row] = num_nonzeros_lowerTri_strict;
else if (readMode == UPPER_TRI_STRICT)
row_offsets[row] = num_nonzeros_upperTri_strict;
else if (readMode == UPPER_TRI)
row_offsets[row] = num_nonzeros_upperTri;
else if (readMode == LOWER_TRI)
row_offsets[row] = num_nonzeros_lowerTri;
else /// DEFAULT
row_offsets[row] = num_nonzeros;
}
}
//===----------------------------------------------------------------------===//
/// Clear
//===----------------------------------------------------------------------===//
void Clear()
{
if (row_offsets)
delete[] row_offsets;
if (column_indices)
delete[] column_indices;
if (values)
delete[] values;
row_offsets = NULL;
column_indices = NULL;
values = NULL;
}
//===----------------------------------------------------------------------===//
/// Constructor
//===----------------------------------------------------------------------===//
CsrMatrix(
CooMatrix<T> *coo_matrix,
int readMode,
bool verbose = false)
{
Init(coo_matrix, readMode, verbose);
}
//===----------------------------------------------------------------------===//
/// Destructor
//===----------------------------------------------------------------------===//
~CsrMatrix()
{
Clear();
}
};
//===----------------------------------------------------------------------===//
/// CSC sparse format matrix
//===----------------------------------------------------------------------===//
template <typename T>
struct CscMatrix
{
uint64_t num_rows;
uint64_t num_cols;
uint64_t num_nonzeros;
uint64_t *col_offsets;
uint64_t *row_indices;
T *values;
//===----------------------------------------------------------------------===//
/// Initializer
//===----------------------------------------------------------------------===//
void Init(
CooMatrix<T> *coo_matrix,
bool verbose = false)
{
num_rows = coo_matrix->num_rows;
num_cols = coo_matrix->num_cols;
num_nonzeros = coo_matrix->num_nonzeros;
/// Sort by rows, then columns
if (verbose)
printf("Ordering...");
fflush(stdout);
std::stable_sort(coo_matrix->coo_tuples, coo_matrix->coo_tuples + num_nonzeros, CooComparatorCol());
if (verbose)
printf("done.");
fflush(stdout);
col_offsets = new uint64_t[num_cols + 1];
row_indices = new uint64_t[num_nonzeros];
values = new T[num_nonzeros];
uint64_t prev_col = -1;
for (uint64_t current_nz = 0; current_nz < num_nonzeros; current_nz++)
{
uint64_t current_col = coo_matrix->coo_tuples[current_nz].col;
/// Fill in cols up to and including the current col
for (uint64_t col = prev_col + 1; col <= current_col; col++)
{
col_offsets[col] = current_nz;
}
prev_col = current_col;
row_indices[current_nz] = coo_matrix->coo_tuples[current_nz].row;
values[current_nz] = coo_matrix->coo_tuples[current_nz].val;
}
/// Fill out any trailing edgeless vertices (and the end-of-list element)
for (uint64_t col = prev_col + 1; col <= num_cols; col++)
{
col_offsets[col] = num_nonzeros;
}
}
/// Clear
void Clear()
{
if (col_offsets)
delete[] col_offsets;
if (row_indices)
delete[] row_indices;
if (values)
delete[] values;
col_offsets = NULL;
row_indices = NULL;
values = NULL;
}
/// Constructor
CscMatrix(
CooMatrix<T> *coo_matrix,
bool verbose = false)
{
Init(coo_matrix, verbose);
}
/// Destructor
~CscMatrix()
{
Clear();
}
};
//===----------------------------------------------------------------------===//
/// DCSR matrix type
//===----------------------------------------------------------------------===//
/// DCSR sparse format matrix
template <typename T>
struct DcsrMatrix
{
uint64_t num_rows;
uint64_t num_cols;
uint64_t num_nonzeros;
uint64_t A1pos_size = 0;
uint64_t A1crd_size = 0;
uint64_t A2pos_size = 0;
uint64_t A2crd_size = 0;
uint64_t Aval_size = 0;
uint64_t *A1pos;
uint64_t *A1crd;
uint64_t *A2pos;
uint64_t *A2crd;
T *Aval;
/// Initializer
void Init(
CooMatrix<T> *coo_matrix,
bool verbose = false)
{
num_rows = coo_matrix->num_rows;
num_cols = coo_matrix->num_cols;
num_nonzeros = coo_matrix->num_nonzeros;
/// Sort by rows, then columns
if (verbose)
printf("Ordering...");
fflush(stdout);
std::stable_sort(coo_matrix->coo_tuples, coo_matrix->coo_tuples + num_nonzeros, CooComparatorRow());
if (verbose)
printf("done.");
fflush(stdout);
A1pos = new uint64_t[2];
A1crd = new uint64_t[num_rows];
A2pos = new uint64_t[num_rows + 1];
A2crd = new uint64_t[num_nonzeros];
Aval = new T[num_nonzeros];
uint64_t prev_row = -1;
for (uint64_t current_nz = 0; current_nz < num_nonzeros; current_nz++)
{
uint64_t current_row = coo_matrix->coo_tuples[current_nz].row;
/// Fill in rows up to and including the current row
if (current_row == prev_row)
{
/// A1crd[A1_crd] = current_row;
A2pos[A2pos_size] = current_nz;
}
else
{
A2pos[A2pos_size++] = current_nz;
A1crd[A1crd_size++] = current_row;
prev_row = current_row;
}
A2crd[A2crd_size++] = coo_matrix->coo_tuples[current_nz].col;
Aval[Aval_size++] = coo_matrix->coo_tuples[current_nz].val;
}
A2pos[A2pos_size++] = A2crd_size;
A1pos[A1pos_size++] = 0;
A1pos[A1pos_size++] = A1crd_size;
}
/// Clear
void Clear()
{
if (A1pos)
delete[] A1pos;
if (A1crd)
delete[] A1crd;
if (A2pos)
delete[] A2pos;
if (A2crd)
delete[] A2crd;
if (Aval)
delete[] Aval;
A1pos = NULL;
A1crd = NULL;
A2pos = NULL;
A2crd = NULL;
Aval = NULL;
}
/// Constructor
DcsrMatrix(
CooMatrix<T> *coo_matrix,
bool verbose = false)
{
Init(coo_matrix, verbose);
}
/// Destructor
~DcsrMatrix()
{
Clear();
}
};
///===----------------------------------------------------------------------===//
/// ELLPACK matrix type
///===----------------------------------------------------------------------===//
template <typename T>
struct EllpackMatrix
{
uint64_t num_rows;
uint64_t num_cols;
uint64_t num_nonzeros;
uint64_t *col_crd;
T *Aval;
/// Initializer
void Init(CooMatrix<T> *coo_matrix, bool verbose = false)
{
num_rows = coo_matrix->num_rows;
num_cols = 0;
num_nonzeros = coo_matrix->num_nonzeros;
/// Sort by rows, then columns
if (verbose)
printf("Ordering...");
fflush(stdout);
std::stable_sort(coo_matrix->coo_tuples, coo_matrix->coo_tuples + num_nonzeros, CooComparatorRow());
if (verbose)
printf("done.");
fflush(stdout);
/// Calculate the column count
uint64_t max = 0;
uint64_t buffer = 0;
// int current = -1;
uint64_t current = num_nonzeros > 0 ? coo_matrix->coo_tuples[0].row : 0;
for (uint64_t i = 0; i < num_nonzeros; i++)
{
if (coo_matrix->coo_tuples[i].row == current)
{
++buffer;
}
else
{
if (buffer > max)
max = buffer;
buffer = 1;
current = coo_matrix->coo_tuples[i].row;
}
}
if (buffer > max)
max = buffer;
num_cols = max;
/// Create the column coordinate list
/// TODO: This terrible, but it works
col_crd = new uint64_t[num_rows * num_cols];
Aval = new T[num_rows * num_cols];
uint64_t index = 0;
/// Build the column coordinates/value array
for (uint64_t i = 0; i < num_rows; i++)
{
/// In this loop, get all non-zero column coordinates and track
/// how many we have found
uint64_t found_cols = 0;
for (uint64_t j = 0; j < num_nonzeros; j++)
{
if (coo_matrix->coo_tuples[j].row == i)
{
++found_cols;
}
}
/// If the number of columns we have found in the row is less than
/// the block, we need to add some zeros to create the block
if (found_cols == num_cols)
{
for (uint64_t j = 0; j < num_nonzeros; j++)
{
if (coo_matrix->coo_tuples[j].row == i)
{
col_crd[index] = coo_matrix->coo_tuples[j].col;
Aval[index] = coo_matrix->coo_tuples[j].val;
++index;
}
}
continue;
}
/// If there were no found columns, we'll just add zeros
if (found_cols == 0)
{
for (uint64_t j = 0; j < num_cols; j++)
{
col_crd[index] = j;
Aval[index] = 0;
++index;
}
continue;
}
/// If we have an odd number, we need to add preceeding elements before
/// the actual non-zero indicies
for (uint64_t j = 0; j < num_nonzeros; j++)
{
if (coo_matrix->coo_tuples[j].row == i)
{
// TODO: This IS NOT portable
for (uint64_t k = 0; k < coo_matrix->coo_tuples[j].col && found_cols < num_cols; k++)
{
col_crd[index] = k;
Aval[index] = 0;
++index;
++found_cols;
}
col_crd[index] = coo_matrix->coo_tuples[j].col;
Aval[index] = coo_matrix->coo_tuples[j].val;
++index;
}
}
}
}
/// Clear matrix
void Clear()
{
delete[] col_crd;
delete[] Aval;
}
/// The constructor- calls the initializer
EllpackMatrix(CooMatrix<T> *coo_matrix, bool verbose = false)
{
Init(coo_matrix, verbose);
}
/// Destructor
~EllpackMatrix()
{
Clear();
}
};
//===----------------------------------------------------------------------===//
/// COO tensor 3D type. A COO tensor is just a vector of edge tuples. Tuples are sorted
/// first by first dim, then by second dim and so on.
//===----------------------------------------------------------------------===//
template <typename T>
struct Coo3DTensor
{
//---------------------------------------------------------------------
// Type definitions and constants
//---------------------------------------------------------------------
/// COO edge tuple
struct Coo3DTuple
{
uint64_t index_i;
uint64_t index_j;
uint64_t index_k;
T val;
Coo3DTuple() {}
Coo3DTuple(uint64_t index_i, uint64_t index_j, uint64_t index_k) : index_i(index_i), index_j(index_j), index_k(index_k) {}
Coo3DTuple(uint64_t index_i, uint64_t index_j, uint64_t index_k, T val) : index_i(index_i), index_j(index_j), index_k(index_k), val(val) {}
};
//---------------------------------------------------------------------
/// Data members
//---------------------------------------------------------------------
/// Fields
uint64_t num_index_i;
uint64_t num_index_j;
uint64_t num_index_k;
uint64_t num_nonzeros;
Coo3DTuple *coo_3dtuples;
//---------------------------------------------------------------------
/// Methods
//---------------------------------------------------------------------
/// Constructor
Coo3DTensor() : num_index_i(0), num_index_j(0), num_index_k(0), num_nonzeros(0), coo_3dtuples(NULL) {}
/// Clear
void Clear()
{
if (coo_3dtuples)
delete[] coo_3dtuples;
coo_3dtuples = NULL;
}
/// Destructor
~Coo3DTensor()
{
/// do nothing. coo_3dtuples is now cleared from else-where.
// Clear();
}
// Display matrix to stdout
void Display()
{
cout << "COO Tensor 3D (" << num_index_i << " index_i, " << num_index_j << " index_j, " << num_index_k << " index_k, " << num_nonzeros << " non-zeros):\n";
cout << "Ordinal, index_i, index_j, index_k, Value\n";
for (uint64_t i = 0; i < num_nonzeros; i++)
{
cout << '\t' << i << ',' << coo_3dtuples[i].index_i << ',' << coo_3dtuples[i].index_j << ',' << coo_3dtuples[i].index_k << ',' << coo_3dtuples[i].val << "\n";
}
}
/// Builds a Frostt COO sparse from the given file.
void InitFrostt(
const string &filename,
T default_value = 1.0,
bool verbose = false)
{
if (verbose)
{
printf("Reading... ");
fflush(stdout);
}
if (coo_3dtuples)
{
fprintf(stderr, "\tERROR: Tensor already constructed (abrupt exit)!\n");
/// updated code should avoid coming to this path
exit(1);
}