-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathCSRMatrix.h
442 lines (373 loc) · 17.4 KB
/
CSRMatrix.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
/*
* Copyright 2021 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/Matrix.h>
#include <runtime/local/datastructures/ValueTypeUtils.h>
#include <algorithm>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <cstddef>
#include <cstring>
/**
* @brief A sparse matrix in Compressed Sparse Row (CSR) format.
*
* This matrix implementation is backed by three contiguous arrays. The
* `values` array contains all non-zero values in the matrix. For each of these
* non-zero values, the `colIdxs` array contains the number of the column.
* Finally, the `rowOffsets` array contains for each row in the matrix the
* offset at which the corresponding entries can be found in the `values` and
* `colIdxs` arrays. Additionally, the `rowOffsets` array ends with the offset
* to the first element after the valid elements in the `values` and `colIdxs`
* arrays.
*
* Each instance of this class might represent a sub-matrix of another
* `CSRMatrix`. Thus, to traverse the matrix by row, you can safely go via the
* `rowOffsets`, but for traversing the matrix by non-zero value, you must
* start at `values[rowOffsets[0]`.
*/
template <typename ValueType> class CSRMatrix : public Matrix<ValueType> {
// `using`, so that we do not need to prefix each occurrence of these
// fields from the super-classes.
using Matrix<ValueType>::numRows;
using Matrix<ValueType>::numCols;
/**
* @brief The number of rows allocated starting from `rowOffsets`. This can
* differ from `numRows` if this `CSRMatrix` is a view on a larger
* `CSRMatrix`.
*/
size_t numRowsAllocated;
bool isRowAllocatedBefore;
/**
* @brief The maximum number of non-zero values this matrix was allocated
* to accommodate.
*/
size_t maxNumNonZeros;
std::shared_ptr<ValueType[]> values;
std::shared_ptr<size_t[]> colIdxs;
std::shared_ptr<size_t[]> rowOffsets;
size_t lastAppendedRowIdx;
// Grant DataObjectFactory access to the private constructors and
// destructors.
template <class DataType, typename... ArgTypes> friend DataType *DataObjectFactory::create(ArgTypes...);
template <class DataType> friend void DataObjectFactory::destroy(const DataType *obj);
/**
* @brief Creates a `CSRMatrix` and allocates enough memory for the
* specified size in the internal `values`, `colIdxs`, and `rowOffsets`
* arrays.
*
* @param maxNumRows The maximum number of rows.
* @param numCols The exact number of columns.
* @param maxNumNonZeros The maximum number of non-zeros in the matrix.
* @param zero Whether the allocated memory of the internal arrays shall be
* initialized to zeros (`true`), or be left uninitialized (`false`).
*/
CSRMatrix(size_t maxNumRows, size_t numCols, size_t maxNumNonZeros, bool zero)
: Matrix<ValueType>(maxNumRows, numCols), numRowsAllocated(maxNumRows), isRowAllocatedBefore(false),
maxNumNonZeros(maxNumNonZeros), values(new ValueType[maxNumNonZeros], std::default_delete<ValueType[]>()),
colIdxs(new size_t[maxNumNonZeros], std::default_delete<size_t[]>()),
rowOffsets(new size_t[numRows + 1], std::default_delete<size_t[]>()), lastAppendedRowIdx(0) {
if (zero) {
memset(values.get(), 0, maxNumNonZeros * sizeof(ValueType));
memset(colIdxs.get(), 0, maxNumNonZeros * sizeof(size_t));
memset(rowOffsets.get(), 0, (numRows + 1) * sizeof(size_t));
}
}
/**
* @brief Creates a `CSRMatrix` around a sub-matrix of another `CSRMatrix`
* without copying the data.
*
* @param src The other `CSRMatrix`.
* @param rowLowerIncl Inclusive lower bound for the range of rows to
* extract.
* @param rowUpperExcl Exclusive upper bound for the range of rows to
* extract.
*/
CSRMatrix(const CSRMatrix<ValueType> *src, size_t rowLowerIncl, size_t rowUpperExcl)
: Matrix<ValueType>(rowUpperExcl - rowLowerIncl, src->numCols),
numRowsAllocated(src->numRowsAllocated - rowLowerIncl), isRowAllocatedBefore(rowLowerIncl > 0),
lastAppendedRowIdx(0) {
if (!src)
throw std::runtime_error("CSRMatrix: src must not be null");
if (rowLowerIncl >= src->numRows)
throw std::runtime_error("CSRMatrix: rowLowerIncl is out of bounds");
if (rowUpperExcl > src->numRows)
throw std::runtime_error("CSRMatrix: rowUpperExcl is out of bounds");
if (rowLowerIncl >= rowUpperExcl)
throw std::runtime_error("CSRMatrix: rowLowerIncl must be lower than rowUpperExcl");
maxNumNonZeros = src->maxNumNonZeros;
values = src->values;
colIdxs = src->colIdxs;
rowOffsets = std::shared_ptr<size_t[]>(src->rowOffsets, src->rowOffsets.get() + rowLowerIncl);
}
virtual ~CSRMatrix() {
// nothing to do
}
void fillNextPosUntil(size_t nextPos, size_t rowIdx) {
if (rowIdx > lastAppendedRowIdx) {
for (size_t r = lastAppendedRowIdx + 2; r <= rowIdx + 1; r++)
rowOffsets.get()[r] = nextPos;
lastAppendedRowIdx = rowIdx;
}
}
public:
template <typename NewValueType> using WithValueType = CSRMatrix<NewValueType>;
static std::string getName() { return "CSRMatrix"; }
void shrinkNumRows(size_t numRows) {
if (numRows > this->numRows)
throw std::runtime_error("CSRMatrix (shrinkNumRows): numRows can only be shrunk");
// TODO Here we could reduce the allocated size of the rowOffsets array.
this->numRows = numRows;
}
size_t getMaxNumNonZeros() const { return maxNumNonZeros; }
size_t getNumNonZeros() const { return rowOffsets.get()[numRows] - rowOffsets.get()[0]; }
size_t getNumNonZeros(size_t rowIdx) const {
if (rowIdx >= numRows)
throw std::runtime_error("CSRMatrix (getNumNonZeros): rowIdx is out of bounds");
return rowOffsets.get()[rowIdx + 1] - rowOffsets.get()[rowIdx];
}
void shrinkNumNonZeros(size_t numNonZeros) {
if (numNonZeros > getNumNonZeros())
throw std::runtime_error("CSRMatrix (shrinkNumNonZeros): "
"numNonZeros can only be shrunk");
// TODO Here we could reduce the allocated size of the values and
// colIdxs arrays.
}
ValueType *getValues() { return values.get(); }
const ValueType *getValues() const { return values.get(); }
std::shared_ptr<ValueType[]> getValuesSharedPtr() const { return values; }
ValueType *getValues(size_t rowIdx) {
// We allow equality here to enable retrieving a pointer to the end.
if (rowIdx > numRows)
throw std::runtime_error("CSRMatrix (getValues): rowIdx is out of bounds");
return values.get() + rowOffsets.get()[rowIdx];
}
const ValueType *getValues(size_t rowIdx) const {
return const_cast<CSRMatrix<ValueType> *>(this)->getValues(rowIdx);
}
size_t *getColIdxs() { return colIdxs.get(); }
const size_t *getColIdxs() const { return colIdxs.get(); }
std::shared_ptr<size_t[]> getColIdxsSharedPtr() const { return colIdxs; }
size_t *getColIdxs(size_t rowIdx) {
// We allow equality here to enable retrieving a pointer to the end.
if (rowIdx > numRows)
throw std::runtime_error("CSRMatrix (getColIdxs): rowIdx is out of bounds");
return colIdxs.get() + rowOffsets.get()[rowIdx];
}
const size_t *getColIdxs(size_t rowIdx) const {
return const_cast<CSRMatrix<ValueType> *>(this)->getColIdxs(rowIdx);
}
size_t *getRowOffsets() { return rowOffsets.get(); }
const size_t *getRowOffsets() const { return rowOffsets.get(); }
std::shared_ptr<size_t[]> getRowOffsetsSharedPtr() const { return rowOffsets; }
ValueType get(size_t rowIdx, size_t colIdx) const override {
if (rowIdx >= numRows)
throw std::runtime_error("CSRMatrix (get): rowIdx is out of bounds");
if (colIdx >= numCols)
throw std::runtime_error("CSRMatrix (get): colIdx is out of bounds");
const size_t *rowColIdxsBeg = getColIdxs(rowIdx);
const size_t *rowColIdxsEnd = getColIdxs(rowIdx + 1);
const size_t *ptrExpected = std::lower_bound(rowColIdxsBeg, rowColIdxsEnd, colIdx);
if (ptrExpected == rowColIdxsEnd || *ptrExpected != colIdx)
// No entry for the given coordinates present.
return ValueType(0);
else
// Entry for the given coordinates present.
return getValues(rowIdx)[ptrExpected - rowColIdxsBeg];
}
void set(size_t rowIdx, size_t colIdx, ValueType value) override {
if (rowIdx >= numRows)
throw std::runtime_error("CSRMatrix (set): rowIdx is out of bounds");
if (colIdx >= numCols)
throw std::runtime_error("CSRMatrix (set): colIdx is out of bounds");
size_t *rowColIdxsBeg = getColIdxs(rowIdx);
size_t *rowColIdxsEnd = getColIdxs(rowIdx + 1);
const size_t *ptrExpected = std::lower_bound(rowColIdxsBeg, rowColIdxsEnd, colIdx);
const size_t posExpected = ptrExpected - rowColIdxsBeg;
const size_t posEnd = colIdxs.get() + rowOffsets.get()[numRowsAllocated] - rowColIdxsBeg;
ValueType *rowValuesBeg = getValues(rowIdx);
if (ptrExpected == rowColIdxsEnd || *ptrExpected != colIdx) {
// No entry for the given coordinates present.
if (value == ValueType(0))
return; // do nothing
else {
// Create gap.
// TODO We might want to reallocate here to ensure that enough
// space is allocated.
if (posEnd)
for (size_t pos = posEnd; pos > posExpected; pos--) {
rowValuesBeg[pos] = rowValuesBeg[pos - 1];
rowColIdxsBeg[pos] = rowColIdxsBeg[pos - 1];
}
// Insert given value and column index into the gap.
rowValuesBeg[posExpected] = value;
rowColIdxsBeg[posExpected] = colIdx;
// Update rowOffsets.
for (size_t r = rowIdx + 1; r <= numRowsAllocated; r++)
rowOffsets.get()[r]++;
}
} else {
// Entry for the given coordinates present.
if (value == ValueType(0)) {
// Close gap.
for (size_t pos = posExpected; pos < posEnd; pos++) {
rowValuesBeg[pos] = rowValuesBeg[pos + 1];
rowColIdxsBeg[pos] = rowColIdxsBeg[pos + 1];
}
// Update rowOffsets.
// TODO We might want to shrink the arrays here.
for (size_t r = rowIdx + 1; r <= numRowsAllocated; r++)
rowOffsets.get()[r]--;
} else
// Simply overwrite the existing value.
rowValuesBeg[posExpected] = value;
}
}
void prepareAppend() override {
if (isRowAllocatedBefore)
// In this case, we assume that the matrix has been populated up to
// just before this view.
rowOffsets.get()[1] = rowOffsets.get()[0];
else
rowOffsets.get()[1] = rowOffsets.get()[0] = 0;
lastAppendedRowIdx = 0;
}
// Note that if this matrix is a view on a larger `CSRMatrix`, then
// `prepareAppend`/`append`/`finishAppend` assume that the larger matrix
// has been populated up to just before the row range of this view.
void append(size_t rowIdx, size_t colIdx, ValueType value) override {
if (rowIdx >= numRows)
throw std::runtime_error("CSRMatrix (append): rowIdx is out of bounds");
if (colIdx >= numCols)
throw std::runtime_error("CSRMatrix (append): colIdx is out of bounds");
if (value == ValueType(0))
return;
const size_t nextPos = rowOffsets.get()[lastAppendedRowIdx + 1];
fillNextPosUntil(nextPos, rowIdx);
values.get()[nextPos] = value;
colIdxs.get()[nextPos] = colIdx;
rowOffsets.get()[rowIdx + 1]++;
}
void finishAppend() override { fillNextPosUntil(rowOffsets.get()[lastAppendedRowIdx + 1], numRows - 1); }
bool isView() const { return (numRowsAllocated > numRows || isRowAllocatedBefore); }
void printValue(std::ostream &os, ValueType val) const {
switch (ValueTypeUtils::codeFor<ValueType>) {
case ValueTypeCode::SI8:
os << static_cast<int32_t>(val);
break;
case ValueTypeCode::UI8:
os << static_cast<uint32_t>(val);
break;
default:
os << val;
break;
}
}
void print(std::ostream &os) const override {
os << "CSRMatrix(" << numRows << 'x' << numCols << ", " << ValueTypeUtils::cppNameFor<ValueType> << ')'
<< std::endl;
// Note that, in general, the values within one row might not be sorted
// by column index. Thus, the following is a little complicated.
ValueType *oneRow = new ValueType[numCols];
for (size_t r = 0; r < numRows; r++) {
memset(oneRow, 0, numCols * sizeof(ValueType));
const size_t rowNumNonZeros = getNumNonZeros(r);
const size_t *rowColIdxs = getColIdxs(r);
const ValueType *rowValues = getValues(r);
for (size_t i = 0; i < rowNumNonZeros; i++)
oneRow[rowColIdxs[i]] = rowValues[i];
for (size_t c = 0; c < numCols; c++) {
printValue(os, oneRow[c]);
if (c < numCols - 1)
os << ' ';
}
os << std::endl;
}
delete[] oneRow;
}
/**
* @brief Prints the internal arrays of this matrix.
*
* Meant to be used for testing and debugging only. Note that this method
* works even if the internal state of the matrix is invalid, e.g., due to
* uninitialized row offsets or column indexes.
*
* @param os The stream to print to.
*/
void printRaw(std::ostream &os) const {
os << "CSRMatrix(" << numRows << 'x' << numCols << ", " << ValueTypeUtils::cppNameFor<ValueType> << ')'
<< std::endl;
os << "maxNumNonZeros: \t" << maxNumNonZeros << std::endl;
os << "values: \t";
for (size_t i = 0; i < maxNumNonZeros; i++)
os << values.get()[i] << ", ";
os << std::endl;
os << "colIdxs: \t";
for (size_t i = 0; i < maxNumNonZeros; i++)
os << colIdxs.get()[i] << ", ";
os << std::endl;
os << "rowOffsets: \t";
for (size_t i = 0; i <= numRows; i++)
os << rowOffsets.get()[i] << ", ";
os << std::endl;
}
CSRMatrix *sliceRow(size_t rl, size_t ru) const override {
return DataObjectFactory::create<CSRMatrix>(this, rl, ru);
}
CSRMatrix *sliceCol(size_t cl, size_t cu) const override {
// TODO add boundary validation when implementing
throw std::runtime_error("CSRMatrix does not support sliceCol yet");
}
CSRMatrix *slice(size_t rl, size_t ru, size_t cl, size_t cu) const override {
// TODO add boundary validation when implementing
throw std::runtime_error("CSRMatrix does not support slice yet");
}
size_t bufferSize() { return this->getNumItems() * sizeof(ValueType); }
bool operator==(const CSRMatrix<ValueType> &rhs) const {
// Note that we do not use the generic `get` interface to matrices here
// since this operator is meant to be used for writing tests for,
// besides others, those generic interfaces.
if (this == &rhs)
return true;
const size_t numRows = this->getNumRows();
const size_t numCols = this->getNumCols();
if (numRows != rhs.getNumRows() || numCols != rhs.getNumCols())
return false;
const ValueType *valuesBegLhs = this->getValues(0);
const ValueType *valuesEndLhs = this->getValues(numRows);
const ValueType *valuesBegRhs = rhs.getValues(0);
const ValueType *valuesEndRhs = rhs.getValues(numRows);
const size_t nnzLhs = valuesEndLhs - valuesBegLhs;
const size_t nnzRhs = valuesEndRhs - valuesBegRhs;
if (nnzLhs != nnzRhs)
return false;
if (valuesBegLhs != valuesBegRhs)
if (memcmp(valuesBegLhs, valuesBegRhs, nnzLhs * sizeof(ValueType)))
return false;
const size_t *colIdxsBegLhs = this->getColIdxs(0);
const size_t *colIdxsBegRhs = rhs.getColIdxs(0);
if (colIdxsBegLhs != colIdxsBegRhs)
if (memcmp(colIdxsBegLhs, colIdxsBegRhs, nnzLhs * sizeof(size_t)))
return false;
return true;
}
size_t serialize(std::vector<char> &buf) const override;
};
template <typename ValueType> std::ostream &operator<<(std::ostream &os, const CSRMatrix<ValueType> &obj) {
obj.print(os);
return os;
}