-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCSC.h
837 lines (741 loc) · 24.7 KB
/
CSC.h
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
#ifndef _CSC_H_
#define _CSC_H_
#include "Deleter.h"
#include "HeapEntry.h"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <vector>
#include <random>
#include "BitMap.h"
#include "utility.h"
#include <numeric>
extern "C"
{
#include "GraphBLAS.h"
}
#include "Triple.h"
extern "C" {
#include "GTgraph/R-MAT/graph.h"
}
using namespace std;
template <class IT,
class NT> // IT, NT li dichiaro runtime (polimorfismo parametrico)
class CSC {
public:
CSC() : nnz(0), rows(0), cols(0) {}
CSC(IT mynnz, IT m, IT n, int nt)
: nnz(mynnz), rows(m), cols(n) // costruttore di default
{
// Constructing empty Csc objects (size = 0) are not allowed.
assert(nnz != 0 && cols != 0);
colptr = my_malloc<IT>(cols + 1);
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
}
CSC(Triple<IT, NT> *triples, IT mynnz, IT m,
IT n); // altro costruttore di default
CSC(std::vector<std::pair<int64_t, int64_t>> edges, IT mynnz, IT m, IT n);
CSC(IT scale, IT r_scale, IT r_edgefactor); // for tall-skiny matrix
void make_empty() {
if (nnz > 0) {
my_free<IT>(rowids);
my_free<NT>(values);
nnz = 0;
}
if (cols > 0) {
my_free<IT>(colptr);
cols = 0;
}
rows = 0;
}
template <typename AddOperation>
CSC(vector<tuple<IT, IT, NT>> &tuple, IT m, IT n,
AddOperation addop); // costruttore
template <typename AddOperation>
void MergeDuplicates(AddOperation addop); // 1st method
CSC(graph &G);
CSC(IT *ri, IT *ci, NT *val, IT mynnz, IT m, IT n);
CSC(const CSC<IT, NT> &rhs); // copy constructor
CSC (GrB_Matrix A);
CSC<IT, NT> &operator=(const CSC<IT, NT> &rhs); // assignment operator
bool operator==(const CSC<IT, NT> &rhs); // ridefinizione ==
~CSC() // distruttore
{
make_empty();
}
bool isEmpty() { return (nnz == 0); }
void Sorted();
void shuffleIds();
CSC<IT, NT> SpRef(const vector<IT> &ri, const vector<IT> &ci);
CSC<IT, NT> SpRef1(const vector<IT> &ri, const vector<IT> &ci);
CSC<IT, NT> SpRef2(const IT *ri, const IT rilen, const IT *ci,
const IT cilen);
void intersect(const IT *rowids_in, const NT *values_in, const IT len_in,
const IT *ri, const IT len_ri, IT *rowids_out, NT *values_out,
IT *len_out);
void get_grb_mat(GrB_Matrix A);
IT rows;
IT cols;
IT nnz; // number of nonzeros
IT totalcols; // for the parallel case
IT *colptr;
IT *rowids;
NT *values;
};
// copy constructor
template <class IT, class NT>
CSC<IT, NT>::CSC(const CSC<IT, NT> &rhs)
: nnz(rhs.nnz), rows(rhs.rows), cols(rhs.cols) {
if (nnz > 0) {
values = my_malloc<NT>(nnz);
rowids = my_malloc<IT>(nnz);
copy(rhs.values, rhs.values + nnz, values);
copy(rhs.rowids, rhs.rowids + nnz, rowids);
}
if (cols > 0) {
colptr = my_malloc<IT>(cols + 1);
copy(rhs.colptr, rhs.colptr + cols + 1, colptr);
}
}
template <class IT, class NT>
CSC<IT, NT> &CSC<IT, NT>::
operator=(const CSC<IT, NT> &rhs) // ridefinisce operatore = di assegnazione
{
if (this != &rhs) {
if (nnz > 0) // if the existing object is not empty
{
my_free<IT>(rowids);
my_free<NT>(values);
}
if (cols > 0) {
my_free<IT>(colptr);
}
nnz = rhs.nnz;
rows = rhs.rows;
cols = rhs.cols;
if (rhs.nnz > 0) // if the copied object is not empty
{
values = my_malloc<NT>(nnz);
rowids = my_malloc<IT>(nnz);
copy(rhs.values, rhs.values + nnz, values);
copy(rhs.rowids, rhs.rowids + nnz, rowids);
}
if (rhs.cols > 0) {
colptr = my_malloc<IT>(cols + 1);
copy(rhs.colptr, rhs.colptr + cols + 1, colptr);
}
}
return *this;
}
//! Construct a CSC object from a GTgraph object
//! GTgraph might have parallel edges; this constructor sums them up
//! Assumes a sorted GTgraph (primary key: start)
template <class IT, class NT>
CSC<IT, NT>::CSC(graph &G) : nnz(G.m), rows(G.n), cols(G.n) {
// graph is like a triples object
// typedef struct {
// LONG_T m;
// LONG_T n;
// // Arrays of size 'm' storing the edge information
// // A directed edge 'e' (0 <= e < m) from start[e] to end[e]
// // had an integer weight w[e]
// LONG_T* start;
// LONG_T* end;
// WEIGHT_T* w;
// } graph;
cout << "Graph nnz= " << G.m << " and n=" << G.n << endl;
vector<Triple<IT, NT>> simpleG;
vector<pair<pair<IT, IT>, NT>> currCol;
currCol.push_back(make_pair(make_pair(G.start[0], G.end[0]), G.w[0]));
for (IT k = 0; k < nnz - 1; ++k) {
if (G.start[k] != G.start[k + 1]) {
std::sort(currCol.begin(), currCol.end());
simpleG.push_back(Triple<IT, NT>(
currCol[0].first.first, currCol[0].first.second, currCol[0].second));
for (int i = 0; i < currCol.size() - 1; ++i) {
if (currCol[i].first == currCol[i + 1].first) {
simpleG.back().val += currCol[i + 1].second;
} else {
simpleG.push_back(Triple<IT, NT>(currCol[i + 1].first.first,
currCol[i + 1].first.second,
currCol[i + 1].second));
}
}
vector<pair<pair<IT, IT>, NT>>().swap(currCol);
}
currCol.push_back(
make_pair(make_pair(G.start[k + 1], G.end[k + 1]), G.w[k + 1]));
}
// now do the last row
sort(currCol.begin(), currCol.end());
simpleG.push_back(Triple<IT, NT>(currCol[0].first.first,
currCol[0].first.second, currCol[0].second));
for (int i = 0; i < currCol.size() - 1; ++i) {
if (currCol[i].first == currCol[i + 1].first) {
simpleG.back().val += currCol[i + 1].second;
} else {
simpleG.push_back(Triple<IT, NT>(currCol[i + 1].first.first,
currCol[i + 1].first.second,
currCol[i + 1].second));
}
}
nnz = simpleG.size();
cout << "[After duplicate merging] Graph nnz= " << nnz << " and n=" << G.n
<< endl
<< endl;
colptr = my_malloc<IT>(cols + 1);
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
IT *work = my_malloc<IT>(cols);
std::fill(work, work + cols, (IT)0); // initilized to zero
for (IT k = 0; k < nnz; ++k) {
IT tmp = simpleG[k].col;
work[tmp]++; // col counts (i.e, w holds the "col difference array")
}
if (nnz > 0) {
colptr[cols] = CumulativeSum(work, cols); // cumulative sum of w
copy(work, work + cols, colptr);
IT last;
for (IT k = 0; k < nnz; ++k) {
rowids[last = work[simpleG[k].col]++] = simpleG[k].row;
values[last] = simpleG[k].val;
}
}
my_free<IT>(work);
}
// Construct a Csc object from an array of "triple"s
template <class IT, class NT>
CSC<IT, NT>::CSC(Triple<IT, NT> *triples, IT mynnz, IT m, IT n)
: nnz(mynnz), rows(m), cols(n) {
colptr = my_malloc<IT>(cols + 1);
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
vector<pair<IT, NT>> tosort(nnz);
IT *work = my_malloc<IT>(cols);
std::fill(work, work + cols, (IT)0);
for (IT k = 0; k < nnz; ++k) {
IT tmp = triples[k].col;
work[tmp]++; // column counts (i.e, w holds the "col difference array")
}
if (nnz > 0) {
colptr[cols] = CumulativeSum(work, cols); // cumulative sum of w
copy(work, work + cols, colptr);
IT last;
for (IT k = 0; k < nnz; ++k) {
tosort[work[triples[k].col]++] =
make_pair(triples[k].row, triples[k].val);
}
#pragma omp parallel for
for (IT i = 0; i < cols; ++i) {
sort(tosort.begin() + colptr[i], tosort.begin() + colptr[i + 1]);
typename vector<pair<IT, NT>>::iterator
itr; // iterator is a dependent name
IT ind;
for (itr = tosort.begin() + colptr[i], ind = colptr[i];
itr != tosort.begin() + colptr[i + 1]; ++itr, ++ind) {
rowids[ind] = itr->first;
values[ind] = itr->second;
}
}
}
my_free<IT>(work);
}
// Construct a Csc object from an array of pairs
template <class IT, class NT>
CSC<IT,NT>::CSC(std::vector<std::pair<int64_t, int64_t>> edges, IT mynnz, IT m, IT n):nnz(mynnz),rows(m),cols(n)
{
cout << "CSC constructor" << endl;
colptr = my_malloc<IT>(cols + 1);
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
IT *work = my_malloc<IT>(cols);
std::fill(work, work+cols, (IT) 0);
for (IT k = 0 ; k < nnz ; ++k)
{
IT colId = std::get<1>(edges[k]);
work [colId]++ ;
}
if(nnz > 0)
{
colptr[cols] = CumulativeSum (work, cols) ; // cumulative sum of w
copy(work, work+cols, colptr);
for (IT k = 0 ; k < nnz ; ++k)
{
IT colId = std::get<1>(edges[k]);
IT rowId = std::get<0>(edges[k]);
rowids[work[colId]++] = rowId;
}
#pragma omp parallel for
for(IT i=0; i< cols; ++i)
{
sort(rowids + colptr[i], rowids + colptr[i+1]);
}
#pragma omp parallel for
for (IT k = 0 ; k < nnz ; ++k)
{
values[k] = (NT) 1;
}
}
my_free<IT>(work);
}
template <class IT,
class NT>
CSC<IT, NT>::CSC (GrB_Matrix A)
{
static_assert(std::is_same<IT, GrB_Index>::value,
"CSR matrix index type and GrB_Matrix index type "
"must be the same");
bool is_iso, is_jumbled;
GrB_Index ap_size, aj_size, ax_size;
GrB_Index nr, nc, nnz;
GrB_Descriptor desc = NULL;
GrB_Descriptor_new(&desc);
GrB_Matrix_nrows(&nr, A);
GrB_Matrix_ncols(&nc, A);
GrB_Matrix_nvals(&nnz, A);
this->rows = nr;
this->cols = nc;
this->nnz = nnz;
// does not free the matrix, but the matrix has no entries after this
// GxB_Matrix_unpack_CSC(A,
// &this->colptr,
// &this->rowids,
// (void **)&this->values,
// &ap_size,
// &aj_size,
// &ax_size,
// &is_iso,
// &is_jumbled,
// desc);
// assert(!is_iso && "GraphBLAS matrix is iso-valued.");
// assert(!is_jumbled && "GraphBLAS matrix is not sorted\n");
// force sorted output
GxB_Matrix_unpack_CSC(A,
&this->colptr,
&this->rowids,
(void **)&this->values,
&ap_size,
&aj_size,
&ax_size,
nullptr,
nullptr,
desc);
return;
}
template <class IT, class NT>
template <typename AddOperation>
void CSC<IT, NT>::MergeDuplicates(AddOperation addop) {
vector<IT> diff(cols, 0);
std::adjacent_difference(colptr + 1, colptr + cols + 1, diff.begin());
vector<vector<IT>> v_rowids;
vector<vector<NT>> v_values;
if (nnz > 0) {
#pragma omp parallel for
for (int i = 0; i < cols; ++i) {
for (size_t j = colptr[i]; j < colptr[i + 1]; ++j) {
v_rowids[i].push_back(rowids[j]);
v_values[i].push_back(values[j]);
while (j < colptr[i + 1] - 1 && rowids[j] == rowids[j + 1]) {
v_values[i].back() = addop(v_values[i].back(), values[j + 1]);
j++; // increment j
diff[i]--;
}
}
}
}
colptr[cols] = CumulativeSum(diff.data(), cols); // cumulative sum of diff
copy(diff.begin(), diff.end(), colptr); // update the column pointers
my_free<IT>(rowids);
my_free<NT>(values);
cout << "Old number of nonzeros before merging: " << nnz << endl;
nnz = colptr[cols];
cout << "New number of nonzeros after merging: " << nnz << endl;
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
#pragma omp parallel for
for (int i = 0; i < cols; ++i) {
copy(v_rowids[i].begin(), v_rowids[i].end(), rowids + colptr[i]);
copy(v_values[i].begin(), v_values[i].end(), values + colptr[i]);
}
}
//! this version handles duplicates in the input
template <class IT, class NT>
template <typename AddOperation>
// n = kmerdict.size(), m = read_id, nnz = tuple.size()
// CSC<size_t, size_t> *spmat = new CSC<size_t, size_t>(occurrences, read_id,
// kmerdict.size(), plus<size_t>());
CSC<IT, NT>::CSC(vector<tuple<IT, IT, NT>> &tuple, IT m, IT n,
AddOperation addop)
: rows(m), cols(n) {
nnz = tuple.size(); // there might be duplicates
colptr = my_malloc<IT>(cols + 1);
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
vector<pair<IT, NT>> tosort(nnz);
IT *work = my_malloc<IT>(cols);
std::fill(work, work + cols, (IT)0); // riempi di 0 tutto
for (IT k = 0; k < nnz; ++k) {
IT tmp = get<1>(tuple[k]); // temp = read_id
work[tmp]++; // column counts (i.e, w holds the "col difference array")
}
if (nnz > 0) {
colptr[cols] =
CumulativeSum(work, cols); // cumulative sum of work, puntatore
// all'ultima posizione contiene
copy(work, work + cols, colptr);
IT last;
for (IT k = 0; k < nnz; ++k) {
tosort[work[get<1>(tuple[k])]++] =
make_pair(get<0>(tuple[k]), get<2>(tuple[k]));
}
#pragma omp parallel for
for (int i = 0; i < cols; ++i) {
sort(tosort.begin() + colptr[i], tosort.begin() + colptr[i + 1]);
typename vector<pair<IT, NT>>::iterator
itr; // iterator is a dependent name
IT ind;
for (itr = tosort.begin() + colptr[i], ind = colptr[i];
itr != tosort.begin() + colptr[i + 1]; ++itr, ++ind) {
rowids[ind] = itr->first;
values[ind] = itr->second;
}
}
}
// for (IT j = 0; j < nnz; ++j) {
// std::cout << " read_id : " << rowids[j] << " kmer_id : " << get<1>(tuple[j])
// << " pos_in_read : " << values[j] << endl;
// // TO DO: as value I want a pair<kmer_id, vector<posix_in_read>>
// }
my_free<IT>(work);
}
// Construct a Csc object from parallel arrays
template <class IT, class NT>
CSC<IT, NT>::CSC(IT *ri, IT *ci, NT *val, IT mynnz, IT m, IT n)
: nnz(mynnz), rows(m), cols(n) {
assert(nnz != 0 && rows != 0);
colptr = my_malloc<IT>(cols + 1);
rowids = my_malloc<IT>(nnz);
values = my_malloc<NT>(nnz);
vector<pair<IT, NT>> tosort(nnz);
IT *work = my_malloc<IT>(cols);
std::fill(work, work + cols, (IT)0);
for (IT k = 0; k < nnz; ++k) {
IT tmp = ci[k];
work[tmp]++; // column counts (i.e, w holds the "col difference array")
}
if (nnz > 0) {
colptr[cols] = CumulativeSum(work, cols); // cumulative sum of w
copy(work, work + cols, colptr);
IT last;
for (IT k = 0; k < nnz; ++k) {
tosort[work[ci[k]]++] = make_pair(ri[k], val[k]);
}
#pragma omp parallel for
for (int i = 0; i < cols; ++i) {
sort(tosort.begin() + colptr[i], tosort.begin() + colptr[i + 1]);
typename vector<pair<IT, NT>>::iterator
itr; // iterator is a dependent name
IT ind;
for (itr = tosort.begin() + colptr[i], ind = colptr[i];
itr != tosort.begin() + colptr[i + 1]; ++itr, ++ind) {
rowids[ind] = itr->first;
values[ind] = itr->second;
}
}
}
my_free<IT>(work);
}
// check if sorted within columns
template <class IT, class NT> void CSC<IT, NT>::Sorted() {
bool sorted = true;
for (IT i = 0; i < cols; ++i) {
sorted &= my_is_sorted(rowids + colptr[i], rowids + colptr[i + 1],
std::less<IT>());
}
cout << "CSC graph is sorted by row id: "<< sorted << endl;
}
template <class IT, class NT> void CSC<IT, NT>::shuffleIds() {
mt19937_64 mt(0);
for (IT i = 0; i < cols; ++i) {
IT offset = colptr[i];
IT width = colptr[i + 1] - colptr[i];
uniform_int_distribution<IT> rand_scale(0, width - 1);
for (IT j = colptr[i]; j < colptr[i + 1]; ++j) {
IT target = rand_scale(mt);
IT tmpId = rowids[offset + target];
NT tmpVal = values[offset + target];
rowids[offset + target] = rowids[j];
values[offset + target] = values[j];
rowids[j] = tmpId;
values[j] = tmpVal;
}
}
}
template <class IT, class NT>
bool CSC<IT, NT>::operator==(const CSC<IT, NT> &rhs) {
if (nnz != rhs.nnz || rows != rhs.rows || cols != rhs.cols)
return false;
bool same = std::equal(colptr, colptr + cols + 1, rhs.colptr);
same = same && std::equal(rowids, rowids + nnz, rhs.rowids);
bool samebefore = same;
ErrorTolerantEqual<NT> epsilonequal(EPSILON);
same = same && std::equal(values, values + nnz, rhs.values, epsilonequal);
if (samebefore && (!same)) {
#ifdef DEBUG
vector<NT> error(nnz);
transform(values, values + nnz, rhs.values, error.begin(), absdiff<NT>());
vector<pair<NT, NT>> error_original_pair(nnz);
for (IT i = 0; i < nnz; ++i)
error_original_pair[i] = make_pair(error[i], values[i]);
if (error_original_pair.size() > 10) // otherwise would crush for small data
{
partial_sort(error_original_pair.begin(),
error_original_pair.begin() + 10, error_original_pair.end(),
greater<pair<NT, NT>>());
cout << "Highest 10 different entries are: " << endl;
for (IT i = 0; i < 10; ++i)
cout << "Diff: " << error_original_pair[i].first << " on "
<< error_original_pair[i].second << endl;
} else {
sort(error_original_pair.begin(), error_original_pair.end(),
greater<pair<NT, NT>>());
cout << "Highest different entries are: " << endl;
for (typename vector<pair<NT, NT>>::iterator it =
error_original_pair.begin();
it != error_original_pair.end(); ++it)
cout << "Diff: " << it->first << " on " << it->second << endl;
}
#endif
}
return same;
}
template <class IT, class NT>
void CSC<IT, NT>::intersect(const IT *rowids_in, const NT *values_in,
const IT len_in, const IT *ri, const IT len_ri,
IT *rowids_out, NT *values_out, IT *len_out) {
IT maxlen = len_in > len_ri ? len_in : len_ri;
double r =
len_in > len_ri ? (double)len_in / len_ri : (double)len_ri / len_in;
// if(log2(maxlen) < r) // linear scan is asymptotically better
{
IT idx = 0;
for (int j = 0, k = 0; j < len_in && k < len_ri;) {
if (ri[k] < rowids_in[j])
k++;
else if (ri[k] > rowids_in[j])
j++;
else //(ri[k]==rowids[j])
{
values_out[idx] = values_in[j];
rowids_out[idx++] = rowids_in[j];
k++;
j++; // repeated rows are not allowed
}
}
*len_out = idx;
}
// else // use finger search
{}
}
template <typename IT,
typename NT>
void
CSC<IT, NT>::get_grb_mat
(
GrB_Matrix A
)
{
assert(A != NULL && "GraphBLAS matrix to be packed is NULL!");
GrB_Index nr, nc;
GrB_Matrix_nrows(&nr, A);
GrB_Matrix_ncols(&nc, A);
assert(nr == this->rows && nc == this->cols &&
"Dimension mismatch in converting CSR matrix to GraphBLAS matrix.");
bool is_iso = false, is_jumbled = false;
GrB_Index ap_size = sizeof(IT) * (this->rows+1),
aj_size = sizeof(IT) * this->nnz,
ax_size = sizeof(NT) * this->nnz;
GrB_Descriptor desc = NULL;
GrB_Descriptor_new(&desc);
GxB_Matrix_pack_CSC(A,
&this->colptr,
&this->rowids,
(void **)&this->values,
ap_size,
aj_size,
ax_size,
is_iso,
is_jumbled,
desc);
assert(this->colptr == NULL && this->rowids == NULL &&
this->values == NULL);
GrB_Descriptor_free(&desc);
return;
}
template <class IT, class NT>
CSC<IT, NT> CSC<IT, NT>::SpRef2(const IT *ri, const IT rilen, const IT *ci,
const IT cilen) {
if (cilen > 0 && ci[cilen - 1] > cols) {
cerr << "Col indices out of bounds" << endl;
abort();
}
if (rilen > 0 && ri[rilen - 1] > rows) {
cerr << "Row indices out of bounds" << endl;
abort();
}
// count nnz(A[,:J])
IT nnz_ci = 0;
for (int i = 0; i < cilen; i++) {
nnz_ci = nnz_ci + colptr[ci[i] + 1] - colptr[ci[i]];
}
// IT* rowids_out = new IT[nnz_ci];
// NT* values_out = new NT[nnz_ci];
// IT* len_out = new IT[cilen];
IT *rowids_out = my_malloc<IT>(nnz_ci);
IT *values_out = my_malloc<NT>(nnz_ci);
IT *len_out = my_malloc<IT>(cilen);
IT idx = 0;
for (int i = 0; i < cilen; i++) {
IT cidx1 = colptr[ci[i]];
IT cidx2 = colptr[ci[i] + 1];
intersect(&rowids[cidx1], &values[cidx1], cidx2 - cidx1, ri, rilen,
&rowids_out[cidx1], &values_out[cidx1], &len_out[i]);
}
CSC C;
C.rows = rilen;
C.cols = cilen;
// C.colptr = new IT[C.cols+1];
C.colptr = my_malloc<IT>(C.cols + 1);
C.colptr[0] = 0;
for (int i = 0; i < C.cols; ++i) {
C.colptr[i + 1] = C.colptr[i] + len_out[i];
}
C.nnz = C.colptr[C.cols];
// C.rowids = new IT[C.nnz];
// C.values = new NT[C.nnz];
C.rowids = my_malloc<IT>(C.nnz);
C.values = my_malloc<NT>(C.nnz);
for (int i = 0; i < C.cols; ++i) // combine step
{
IT cidx1 = colptr[ci[i]];
IT cidx2 = cidx1 + len_out[i];
copy(&rowids_out[cidx1], &rowids_out[cidx2], C.rowids + C.colptr[i]);
copy(&values_out[cidx1], &values_out[cidx2], C.values + C.colptr[i]);
}
return C;
}
// write genereal purpose set-intersect
// binary search is faster is one of the vectors is very large
// we assume that ri and ci are sorted in ascending order
// also assume that matrix sorted within column
// output is another CSC
// note that ri and ci might have repeated entries
// behaviour is exactly similar to the matlab implementation
template <class IT, class NT>
CSC<IT, NT> CSC<IT, NT>::SpRef(const vector<IT> &ri, const vector<IT> &ci) {
if ((!ci.empty()) && (ci.back() > cols)) {
cerr << "Col indices out of bounds" << endl;
abort();
}
if ((!ri.empty()) && (ri.back() > rows)) {
cerr << "Row indices out of bounds" << endl;
abort();
}
// first, count nnz in the result matrix
IT refnnz = 0;
for (int i = 0; i < ci.size(); i++) {
IT j = colptr[ci[i]], k = 0;
IT endIdx = colptr[ci[i] + 1];
while (j < endIdx && k < ri.size()) {
// cout << j << "=" << rowids[j] << " :: " << k << "=" << ri[k] << " \n";
if (ri[k] < rowids[j])
k++;
else if (ri[k] > rowids[j])
j++;
else //(ri[k]==rowids[j])
{
refnnz++;
k++;
// j++; // wait for the next iteration of the inner loop to alow
// reapted rows
}
}
}
// Next, allocate memory and save the result matrix
// This two-step implementation is better for multithreading
CSC refmat(refnnz, ri.size(), ci.size(), 0);
refmat.colptr[0] = 0;
IT idx = 0;
for (int i = 0; i < ci.size(); i++) {
IT j = colptr[ci[i]], k = 0;
IT endIdx = colptr[ci[i] + 1];
while (j < endIdx && k < ri.size()) {
if (ri[k] < rowids[j])
k++;
else if (ri[k] > rowids[j])
j++;
else //(ri[k]==rowids[j])
{
refmat.values[idx] = values[j];
refmat.rowids[idx++] = rowids[j];
k++;
// j++; // wait for the next iteration of the inner loop to alow reapted
// rows
}
}
refmat.colptr[i + 1] = idx;
}
return refmat;
}
// write genereal purpose set-intersect
// binary search is faster is one of the vectors is very large
// we assume that ri and ci are sorted in ascending order
// also assume that matrix sorted within column
// output is another CSC
// note that ri and ci might have repeated entries
// behaviour is exactly similar to the matlab implementation
template <class IT, class NT>
CSC<IT, NT> CSC<IT, NT>::SpRef1(const vector<IT> &ri, const vector<IT> &ci) {
if ((!ci.empty()) && (ci.back() > cols)) {
cerr << "Col indices out of bounds" << endl;
abort();
}
if ((!ri.empty()) && (ri.back() > rows)) {
cerr << "Row indices out of bounds" << endl;
abort();
}
BitMap bmap(ri.size()); // space requirement n bits
bmap.reset(); // this is time consuming .....
for (int i = 0; i < ri.size(); i++) {
bmap.set_bit(ri[i]);
}
// first, count nnz in the result matrix
IT refnnz = 0;
for (int i = 0; i < ci.size(); i++) {
IT endIdx = colptr[ci[i] + 1];
for (IT j = colptr[ci[i]]; j < endIdx; j++) {
if (bmap.get_bit(rowids[j]))
refnnz++;
}
}
// Next, allocate memory and save the result matrix
// This two-step implementation is better for multithreading
CSC refmat(refnnz, ri.size(), ci.size(), 0);
refmat.colptr[0] = 0;
IT idx = 0;
for (int i = 0; i < ci.size(); i++) {
IT endIdx = colptr[ci[i] + 1];
for (IT j = colptr[ci[i]]; j < endIdx; j++) {
if (bmap.get_bit(rowids[j])) {
refmat.values[idx] = values[j];
refmat.rowids[idx++] = rowids[j];
}
}
refmat.colptr[i + 1] = idx;
}
return refmat;
}
#endif