-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcsr_matrix.h
More file actions
327 lines (291 loc) · 10 KB
/
csr_matrix.h
File metadata and controls
327 lines (291 loc) · 10 KB
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
#pragma once
#include <algorithm>
#include <cassert>
#include <functional>
#include <limits>
#include <memory>
#include <omp.h>
#include <optional>
#include <sstream>
#include <tuple>
#include <unistd.h>
#include <vector>
#include <vector>
#include <unordered_map>
#include <cassert>
#include <iostream>
#include <cmath>
#include <fstream>
#include <iostream>
#include "../../LinearAlgebra/Vector/vector.h"
#include "../../LinearAlgebra/Vector/vector_operations.h"
// The CSR matrix format is used if a custom LU decomposition solver is choosen.
// MUMPS relies on the COO format.
template <typename T>
class SparseMatrixCSR
{
public:
using triplet_type = std::tuple<int, int, T>;
SparseMatrixCSR();
SparseMatrixCSR(const SparseMatrixCSR& other);
SparseMatrixCSR(SparseMatrixCSR&& other) noexcept;
explicit SparseMatrixCSR(int rows, int columns, std::function<int(int)> nz_per_row);
explicit SparseMatrixCSR(int rows, int columns, const std::vector<triplet_type>& entries);
explicit SparseMatrixCSR(int rows, int columns, const std::vector<T>& values,
const std::vector<int>& column_indices, const std::vector<int>& row_start_indices);
SparseMatrixCSR& operator=(const SparseMatrixCSR& other);
SparseMatrixCSR& operator=(SparseMatrixCSR&& other) noexcept;
int rows() const;
int columns() const;
int non_zero_size() const;
int row_nz_size(int row) const;
const int& row_nz_index(int row, int nz_index) const;
int& row_nz_index(int row, int nz_index);
const T& row_nz_entry(int row, int nz_index) const;
T& row_nz_entry(int row, int nz_index);
T* values_data() const;
int* column_indices_data() const;
int* row_start_indices_data() const;
template <typename U>
friend std::ostream& operator<<(std::ostream& stream, const SparseMatrixCSR<U>& matrix);
private:
int rows_;
int columns_;
int nnz_;
AllocatableVector<T> values_;
AllocatableVector<int> column_indices_;
AllocatableVector<int> row_start_indices_;
bool is_sorted_entries(const std::vector<triplet_type>& entries)
{
for (size_t i = 1; i < entries.size(); ++i) {
const auto& prev = entries[i - 1];
const auto& curr = entries[i];
if (std::get<0>(prev) > std::get<0>(curr))
return false;
}
return true;
}
};
template <typename U>
std::ostream& operator<<(std::ostream& stream, const SparseMatrixCSR<U>& matrix)
{
stream << "SparseMatrixCSR: " << matrix.rows_ << " x " << matrix.columns_ << "\n";
stream << "Number of non-zeros (nnz): " << matrix.nnz_ << "\n";
stream << "Non-zero elements (row, column, value):\n";
for (int row = 0; row < matrix.rows_; ++row) {
for (int nnz = matrix.row_start_indices_(row); nnz < matrix.row_start_indices_(row + 1); ++nnz) {
stream << "(" << row << ", " << matrix.column_indices_(nnz) << ", " << matrix.values_(nnz) << ")\n";
}
}
return stream;
}
// default construction
template <typename T>
SparseMatrixCSR<T>::SparseMatrixCSR()
: rows_(0)
, columns_(0)
, nnz_(0)
{
}
// copy construction
template <typename T>
SparseMatrixCSR<T>::SparseMatrixCSR(const SparseMatrixCSR& other)
: rows_(other.rows_)
, columns_(other.columns_)
, nnz_(other.nnz_)
, values_("CSR values", nnz_)
, column_indices_("CSR column indices", nnz_)
, row_start_indices_("CSR row start indices", rows_ + 1)
{
copy_vector(values_, ConstVector<T>(other.values_));
copy_vector(column_indices_, ConstVector<int>(other.column_indices_));
copy_vector(row_start_indices_, ConstVector<int>(other.row_start_indices_));
}
// copy assignment
template <typename T>
SparseMatrixCSR<T>& SparseMatrixCSR<T>::operator=(const SparseMatrixCSR& other)
{
if (this == &other) {
// Self-assignment, no work needed
return *this;
}
// Only allocate new memory if the sizes are different
if (nnz_ != other.nnz_ || rows_ != other.rows_) {
values_ = Vector<T>("CSR values", other.nnz_);
column_indices_ = Vector<int>("CSR column indices", other.nnz_);
row_start_indices_ = Vector<int>("CSR row start indices", other.rows_ + 1);
}
// Copy the elements
rows_ = other.rows_;
columns_ = other.columns_;
nnz_ = other.nnz_;
copy_vector(values_, ConstVector<T>(other.values_));
copy_vector(column_indices_, ConstVector<int>(other.column_indices_));
copy_vector(row_start_indices_, ConstVector<int>(other.row_start_indices_));
return *this;
}
// move construction
template <typename T>
SparseMatrixCSR<T>::SparseMatrixCSR(SparseMatrixCSR&& other) noexcept
: rows_(other.rows_)
, columns_(other.columns_)
, nnz_(other.nnz_)
, values_(std::move(other.values_))
, column_indices_(std::move(other.column_indices_))
, row_start_indices_(std::move(other.row_start_indices_))
{
other.nnz_ = 0;
other.rows_ = 0;
other.columns_ = 0;
}
// move assignment
template <typename T>
SparseMatrixCSR<T>& SparseMatrixCSR<T>::operator=(SparseMatrixCSR&& other) noexcept
{
rows_ = other.rows_;
columns_ = other.columns_;
nnz_ = other.nnz_;
values_ = std::move(other.values_);
column_indices_ = std::move(other.column_indices_);
row_start_indices_ = std::move(other.row_start_indices_);
other.nnz_ = 0;
other.rows_ = 0;
other.columns_ = 0;
return *this;
}
template <typename T>
SparseMatrixCSR<T>::SparseMatrixCSR(int rows, int columns, std::function<int(int)> nz_per_row)
: rows_(rows)
, columns_(columns)
, row_start_indices_("CSR row start indices", rows_ + 1)
{
assert(rows >= 0);
assert(columns >= 0);
nnz_ = 0;
for (int i = 0; i < rows; i++) {
row_start_indices_(i) = nnz_;
nnz_ += nz_per_row(i);
}
row_start_indices_(rows) = nnz_;
values_ = Vector<T>("CSR values", nnz_);
column_indices_ = Vector<int>("CSR column indices", nnz_);
assign(values_, T(0));
assign(column_indices_, 0);
}
template <typename T>
SparseMatrixCSR<T>::SparseMatrixCSR(int rows, int columns, const std::vector<triplet_type>& entries)
: // entries: row_idx, col_idx, value
rows_(rows)
, columns_(columns)
, nnz_(entries.size())
, values_("CSR values", nnz_)
, column_indices_("CSR column indices", nnz_)
, row_start_indices_("CSR row start indices", rows_ + 1)
{
assert(rows >= 0);
assert(columns >= 0);
assert(is_sorted_entries(entries) && "Entries must be sorted by row!");
// fill values and column indexes
for (int i = 0; i < nnz_; i++) {
assert(0 <= std::get<0>(entries[i]) && std::get<0>(entries[i]) < rows);
values_(i) = std::get<2>(entries[i]);
column_indices_(i) = std::get<1>(entries[i]);
assert(0 <= column_indices_(i) && column_indices_(i) < columns);
}
//fill row indexes
int count = 0;
row_start_indices_(0) = 0;
for (int r = 0; r < rows; r++) {
while (count < nnz_ && std::get<0>(entries[count]) == r)
count++;
row_start_indices_(r + 1) = count;
}
assert(row_start_indices_(rows) == nnz_);
}
template <typename T>
SparseMatrixCSR<T>::SparseMatrixCSR(int rows, int columns, const std::vector<T>& values,
const std::vector<int>& column_indices, const std::vector<int>& row_start_indices)
: rows_(rows)
, columns_(columns)
, nnz_(values.size())
, values_("CSR values", nnz_)
, column_indices_("CSR column indices", nnz_)
, row_start_indices_("CSR row start indices", rows_ + 1)
{
assert(rows >= 0);
assert(columns >= 0);
assert(row_start_indices.size() == static_cast<size_t>(rows + 1));
assert(values.size() == column_indices.size());
// Copy data to internal storage
std::copy(values.begin(), values.end(), values_.data());
std::copy(column_indices.begin(), column_indices.end(), column_indices_.data());
std::copy(row_start_indices.begin(), row_start_indices.end(), row_start_indices_.data());
}
template <typename T>
int SparseMatrixCSR<T>::rows() const
{
assert(this->rows_ >= 0);
return this->rows_;
}
template <typename T>
int SparseMatrixCSR<T>::columns() const
{
assert(this->columns_ >= 0);
return this->columns_;
}
template <typename T>
int SparseMatrixCSR<T>::non_zero_size() const
{
assert(this->nnz_ >= 0);
assert(static_cast<size_t>(this->nnz_) <= static_cast<size_t>(this->rows_) * static_cast<size_t>(this->columns_));
return this->nnz_;
}
template <typename T>
int SparseMatrixCSR<T>::row_nz_size(int row) const
{
assert(row >= 0 && row < rows_);
return row_start_indices_(row + 1) - row_start_indices_(row);
}
template <typename T>
const int& SparseMatrixCSR<T>::row_nz_index(int row, int nz_index) const
{
assert(row >= 0 && row < rows_);
assert(nz_index >= 0 && nz_index < row_nz_size(row));
return column_indices_(row_start_indices_(row) + nz_index);
}
template <typename T>
int& SparseMatrixCSR<T>::row_nz_index(int row, int nz_index)
{
assert(row >= 0 && row < rows_);
assert(nz_index >= 0 && nz_index < row_nz_size(row));
return column_indices_(row_start_indices_(row) + nz_index);
}
template <typename T>
const T& SparseMatrixCSR<T>::row_nz_entry(int row, int nz_index) const
{
assert(row >= 0 && row < rows_);
assert(nz_index >= 0 && nz_index < row_nz_size(row));
return values_(row_start_indices_(row) + nz_index);
}
template <typename T>
T& SparseMatrixCSR<T>::row_nz_entry(int row, int nz_index)
{
assert(row >= 0 && row < rows_);
assert(nz_index >= 0 && nz_index < row_nz_size(row));
return values_(row_start_indices_(row) + nz_index);
}
template <typename T>
T* SparseMatrixCSR<T>::values_data() const
{
return values_.data();
}
template <typename T>
int* SparseMatrixCSR<T>::column_indices_data() const
{
return column_indices_.data();
}
template <typename T>
int* SparseMatrixCSR<T>::row_start_indices_data() const
{
return row_start_indices_.data();
}