2222/* SOFTWARE. */
2323/* *********************************************************************************/
2424
25- #include < cuda/matrix_csr .hpp>
25+ #include < utils/csr_utils .hpp>
2626#include < utils/exclusive_scan.hpp>
27- #include < vector >
27+ #include < core/error.hpp >
2828#include < algorithm>
29+ #include < cassert>
2930
3031namespace cubool {
3132
32- void MatrixCsr::build (const index *rows, const index *cols, size_t nvals, bool isSorted, bool noDuplicates) {
33- if (nvals == 0 ) {
34- mMatrixImpl .zero_dim (); // no content, empty matrix
35- return ;
36- }
37-
38- std::vector<index> rowOffsets;
39- rowOffsets.resize (getNrows () + 1 , 0 );
33+ void CsrUtils::buildFromData (size_t nrows, size_t ncols,
34+ const index *rows, const index *cols, size_t nvals,
35+ std::vector<index> &rowOffsets, std::vector<index> &colIndices,
36+ bool isSorted, bool noDuplicates) {
4037
41- std::vector<index> colIndices ;
38+ rowOffsets. resize (nrows + 1 , 0 ) ;
4239 colIndices.resize (nvals);
4340
44- // Compute nnz per row
45- for (size_t idx = 0 ; idx < nvals; idx++) {
46- index i = rows[idx];
47- index j = cols[idx];
41+ std::fill (rowOffsets.begin (), rowOffsets.end (), 0 );
4842
49- CHECK_RAISE_ERROR (i < getNrows () && j < getNcols (), InvalidArgument, " Out of matrix bounds value" );
43+ if (nvals == 0 )
44+ return ;
5045
51- rowOffsets[i] += 1 ;
52- }
46+ assert (rows) ;
47+ assert (cols);
5348
54- // Exclusive scan to eval rows offsets
55- ::cubool::exclusive_scan (rowOffsets.begin(), rowOffsets.end(), 0);
49+ for (size_t k = 0 ; k < nvals; k++) {
50+ auto i = rows[k];
51+ auto j = cols[k];
5652
57- // Write offsets for cols
58- std::vector< size_t > writeOffsets ( getNrows (), 0 );
53+ CHECK_RAISE_ERROR (i < nrows, InvalidArgument, " Index out of matrix bounds " );
54+ CHECK_RAISE_ERROR (j < ncols, InvalidArgument, " Index out of matrix bounds " );
5955
60- for (size_t idx = 0 ; idx < nvals; idx++) {
61- index i = rows[idx];
62- index j = cols[idx];
56+ rowOffsets[i]++;
57+ }
6358
64- colIndices[rowOffsets[i] + writeOffsets[i]] = j;
65- writeOffsets[i] += 1 ;
59+ exclusive_scan (rowOffsets.begin (), rowOffsets.end (), 0 );
60+
61+ std::vector<size_t > writeOffset (nrows, 0 );
62+ for (size_t k = 0 ; k < nvals; k++) {
63+ auto i = rows[k];
64+ auto j = cols[k];
65+
66+ colIndices[rowOffsets[i] + writeOffset[i]] = j;
67+ writeOffset[i] += 1 ;
6668 }
6769
6870 if (!isSorted) {
69- for (size_t i = 0 ; i < getNrows () ; i++) {
71+ for (size_t i = 0 ; i < nrows ; i++) {
7072 auto begin = rowOffsets[i];
7173 auto end = rowOffsets[i + 1 ];
7274
@@ -77,10 +79,9 @@ namespace cubool {
7779 }
7880 }
7981
80- // Reduce duplicated values
8182 if (!noDuplicates) {
8283 size_t unique = 0 ;
83- for (size_t i = 0 ; i < getNrows () ; i++) {
84+ for (size_t i = 0 ; i < nrows ; i++) {
8485 index prev = std::numeric_limits<index>::max ();
8586
8687 for (size_t k = rowOffsets[i]; k < rowOffsets[i + 1 ]; k++) {
@@ -93,12 +94,12 @@ namespace cubool {
9394 }
9495
9596 std::vector<index> rowOffsetsReduced;
96- rowOffsetsReduced.resize (getNrows () + 1 , 0 );
97+ rowOffsetsReduced.resize (nrows + 1 , 0 );
9798
9899 std::vector<index> colIndicesReduced;
99100 colIndicesReduced.reserve (unique);
100101
101- for (size_t i = 0 ; i < getNrows () ; i++) {
102+ for (size_t i = 0 ; i < nrows ; i++) {
102103 index prev = std::numeric_limits<index>::max ();
103104
104105 for (size_t k = rowOffsets[i]; k < rowOffsets[i + 1 ]; k++) {
@@ -112,15 +113,28 @@ namespace cubool {
112113 }
113114
114115 // Exclusive scan to eval rows offsets
115- ::cubool:: exclusive_scan (rowOffsetsReduced.begin(), rowOffsetsReduced.end(), 0);
116+ exclusive_scan (rowOffsetsReduced.begin (), rowOffsetsReduced.end (), 0 );
116117
117118 // Now result in respective place
118119 std::swap (rowOffsets, rowOffsetsReduced);
119120 std::swap (colIndices, colIndicesReduced);
120121 }
122+ }
121123
122- // Move actual data to the matrix implementation
123- this ->transferToDevice (rowOffsets, colIndices);
124+ void CsrUtils::extractData (size_t nrows, size_t ncols,
125+ index *rows, index *cols, size_t nvals,
126+ const std::vector<index> &rowOffsets, const std::vector<index> &colIndices) {
127+ assert (rows);
128+ assert (cols);
129+
130+ size_t id = 0 ;
131+ for (index i = 0 ; i < nrows; i++) {
132+ for (index k = rowOffsets[i]; k < rowOffsets[i + 1 ]; k++) {
133+ rows[id] = i;
134+ cols[id] = colIndices[k];
135+ id += 1 ;
136+ }
137+ }
124138 }
125139
126140}
0 commit comments