-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.cpp
472 lines (392 loc) · 13.1 KB
/
Matrix.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
// SPDX-License-Identifier: GPL-3.0-or-later
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>
#include "Matrix.h"
#include "WorkDistribution.h"
MatrixCOO::MatrixCOO(const char *file) {
try {
std::ifstream is(file);
if (!is.is_open()) {
std::cerr << "Can't open file with matrix!" << std::endl;
std::exit(1);
}
// Read first line.
std::string line;
std::stringstream ss;
getline(is, line);
ss.str(line);
std::string banner, mtx, crd, type, storage;
ss >> banner >> mtx >> crd >> type >> storage;
// Transform to lower case for comparison.
std::transform(mtx.begin(), mtx.end(), mtx.begin(), ::tolower);
std::transform(crd.begin(), crd.end(), crd.begin(), ::tolower);
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
std::transform(storage.begin(), storage.end(), storage.begin(), ::tolower);
if (banner != "%%MatrixMarket" || mtx != "matrix" || crd != "coordinate" ||
type != "real") {
std::cerr << "Only supporting real matrices in coordinate format!"
<< std::endl;
std::exit(1);
}
bool symmetric = false;
if (storage == "symmetric") {
symmetric = true;
} else if (storage != "general") {
std::cerr << "Only supporting general or symmetric matrices!"
<< std::endl;
std::exit(1);
}
// Skip following lines with comments.
do {
getline(is, line);
} while (line.size() == 0 || line[0] == '%');
// Read dimensions.
int M;
ss.str(line);
ss.clear();
ss >> M >> N >> nz;
if (N != M) {
std::cerr << "Need a square matrix!" << std::endl;
std::exit(1);
}
if (symmetric) {
// Store upper and lower triangular!
nz = 2 * nz - N;
}
// Allocate memory. No implementation will need to "optimize" this because
// MatrixCOO is not really meant to be used in "real" computations.
I.reset(new int[nz]);
J.reset(new int[nz]);
V.reset(new floatType[nz]);
nzPerRow.reset(new int[N]);
std::memset(nzPerRow.get(), 0, sizeof(int) * N);
// Read matrix.
for (int i = 0; i < nz; i++) {
getline(is, line);
ss.str(line);
ss.clear();
ss >> I[i] >> J[i] >> V[i];
// Adjust from 1-based to 0-based.
I[i]--;
J[i]--;
// Count nz for each row.
nzPerRow[I[i]]++;
// If not on the main diagonal, we have to duplicate the entry.
if (symmetric && I[i] != J[i]) {
i++;
I[i] = J[i - 1];
J[i] = I[i - 1];
V[i] = V[i - 1];
// Count nz for each row. I[i] is now J[i - 1]!
nzPerRow[I[i]]++;
}
}
is.close();
} catch (...) {
std::cerr << "An exception occurred while reading the matrix!" << std::endl;
std::exit(1);
}
}
int MatrixCOO::getMaxNz(int from, int to) const {
int maxNz = 0;
for (int i = from; i < to; i++) {
if (nzPerRow[i] > maxNz) {
maxNz = nzPerRow[i];
}
}
return maxNz;
}
void MatrixCOO::countNz(const WorkDistribution &wd,
std::unique_ptr<int[]> &nzDiag,
std::unique_ptr<int[]> &nzMinor) const {
// Allocate temporary memory.
nzDiag.reset(new int[N]);
nzMinor.reset(new int[N]);
for (int i = 0; i < nz; i++) {
int row = I[i];
int chunk = wd.findChunk(row);
if (wd.isOnDiagonal(chunk, J[i])) {
nzDiag[row]++;
} else {
nzMinor[row]++;
}
}
}
// -----------------------------------------------------------------------------
// The functions for allocation and deallocation cannot live in the header file:
// Otherwise, they are included from openacc/ which makes the PGI compiler use
// page-locked memory for the matrix. That would decrease overall performance.
void MatrixDataCRS::allocatePtr(int rows) { ptr = new int[rows + 1]; }
void MatrixDataCRS::deallocatePtr() { delete[] ptr; }
void MatrixDataCRS::allocateIndexAndValue(int values) {
index = new int[values];
value = new floatType[values];
}
void MatrixDataCRS::deallocateIndexAndValue() {
delete[] index;
delete[] value;
}
void MatrixDataELL::allocateLength(int rows) { length = new int[rows]; }
void MatrixDataELL::deallocateLength() { delete[] length; }
void MatrixDataELL::allocateIndexAndData() {
index = new int[elements];
data = new floatType[elements];
}
void MatrixDataELL::deallocateIndexAndData() {
delete[] index;
delete[] data;
}
template <class Data> void SplitMatrix<Data>::allocateData() {
data.reset(new Data[numberOfChunks]);
}
template <class Data> SplitMatrix<Data>::~SplitMatrix() {
if (data) {
for (int i = 0; i < numberOfChunks; i++) {
data[i].deallocate();
}
}
}
template <class Data> void PartitionedMatrix<Data>::allocateDiagAndMinor() {
diag.reset(new Data[numberOfChunks]);
minor.reset(new Data[numberOfChunks]);
}
template <class Data> PartitionedMatrix<Data>::~PartitionedMatrix() {
if (diag) {
for (int i = 0; i < numberOfChunks; i++) {
diag[i].deallocate();
}
}
if (minor) {
for (int i = 0; i < numberOfChunks; i++) {
minor[i].deallocate();
}
}
}
// -----------------------------------------------------------------------------
// Conversion to CRS and ELLPACK format.
template <> void DataMatrix<MatrixDataCRS>::convert(const MatrixCOO &coo) {
N = coo.N;
nz = coo.nz;
// Temporary memory to store current offset in index / value per row.
std::unique_ptr<int[]> offsets(new int[N]);
// Construct ptr and initial values for offsets.
allocatePtr(N);
ptr[0] = 0;
for (int i = 1; i <= N; i++) {
// Copy ptr[i - 1] as initial value for offsets[i - 1].
offsets[i - 1] = ptr[i - 1];
ptr[i] = ptr[i - 1] + coo.nzPerRow[i - 1];
}
// Construct index and value.
allocateIndexAndValue(nz);
for (int i = 0; i < nz; i++) {
int row = coo.I[i];
index[offsets[row]] = coo.J[i];
value[offsets[row]] = coo.V[i];
offsets[row]++;
}
}
template <>
void SplitMatrix<MatrixDataCRS>::convert(const MatrixCOO &coo,
const WorkDistribution &wd) {
N = coo.N;
nz = coo.nz;
numberOfChunks = wd.numberOfChunks;
allocateData();
// Temporary memory to store current offset in index / value per row.
std::unique_ptr<int[]> offsets(new int[N]);
// Construct ptr and initial values for offsets for each chunk.
for (int c = 0; c < numberOfChunks; c++) {
int offset = wd.offsets[c];
int length = wd.lengths[c];
data[c].allocatePtr(length);
data[c].ptr[0] = 0;
for (int i = 1; i <= length; i++) {
offsets[offset + i - 1] = data[c].ptr[i - 1];
data[c].ptr[i] = data[c].ptr[i - 1] + coo.nzPerRow[offset + i - 1];
}
}
// Allocate index and value for each chunk.
for (int c = 0; c < numberOfChunks; c++) {
int length = wd.lengths[c];
int values = data[c].ptr[length];
data[c].allocateIndexAndValue(values);
}
// Construct index and value for all chunks.
for (int i = 0; i < nz; i++) {
int row = coo.I[i];
int chunk = wd.findChunk(row);
data[chunk].index[offsets[row]] = coo.J[i];
data[chunk].value[offsets[row]] = coo.V[i];
offsets[row]++;
}
}
template <>
void PartitionedMatrix<MatrixDataCRS>::convert(const MatrixCOO &coo,
const WorkDistribution &wd) {
N = coo.N;
nz = coo.nz;
numberOfChunks = wd.numberOfChunks;
allocateDiagAndMinor();
// Temporary memory to count nonzeros per row.
std::unique_ptr<int[]> nzDiag, nzMinor;
coo.countNz(wd, nzDiag, nzMinor);
// Temporary memory to store current offset in index / value per row.
std::unique_ptr<int[]> offsetsDiag(new int[N]);
std::unique_ptr<int[]> offsetsMinor(new int[N]);
// Construct ptr and initial values for offsets for each chunk.
for (int c = 0; c < numberOfChunks; c++) {
int offset = wd.offsets[c];
int length = wd.lengths[c];
diag[c].allocatePtr(length);
minor[c].allocatePtr(length);
diag[c].ptr[0] = 0;
minor[c].ptr[0] = 0;
for (int i = 1; i <= length; i++) {
offsetsDiag[offset + i - 1] = diag[c].ptr[i - 1];
offsetsMinor[offset + i - 1] = minor[c].ptr[i - 1];
diag[c].ptr[i] = diag[c].ptr[i - 1] + nzDiag[offset + i - 1];
minor[c].ptr[i] = minor[c].ptr[i - 1] + nzMinor[offset + i - 1];
}
}
// Allocate index and value for each chunk.
for (int c = 0; c < numberOfChunks; c++) {
int length = wd.lengths[c];
int valuesDiag = diag[c].ptr[length];
int valuesMinor = diag[c].ptr[length];
diag[c].allocateIndexAndValue(valuesDiag);
minor[c].allocateIndexAndValue(valuesMinor);
}
// Construct index and value for all chunks.
for (int i = 0; i < nz; i++) {
int row = coo.I[i];
int column = coo.J[i];
int chunk = wd.findChunk(row);
if (wd.isOnDiagonal(chunk, column)) {
diag[chunk].index[offsetsDiag[row]] = column;
diag[chunk].value[offsetsDiag[row]] = coo.V[i];
offsetsDiag[row]++;
} else {
minor[chunk].index[offsetsMinor[row]] = column;
minor[chunk].value[offsetsMinor[row]] = coo.V[i];
offsetsMinor[row]++;
}
}
}
template <> void DataMatrix<MatrixDataELL>::convert(const MatrixCOO &coo) {
N = coo.N;
nz = coo.nz;
elements = N * coo.getMaxNz();
// Copy over already collected nonzeros per row.
allocateLength(N);
std::memcpy(length, coo.nzPerRow.get(), sizeof(int) * N);
// Temporary memory to store current offset in index / value per row.
std::unique_ptr<int[]> offsets(new int[N]);
std::memset(offsets.get(), 0, sizeof(int) * N);
// Construct column and data.
allocateIndexAndData();
for (int i = 0; i < nz; i++) {
int row = coo.I[i];
int k = offsets[row] * N + row;
index[k] = coo.J[i];
data[k] = coo.V[i];
offsets[row]++;
}
}
template <>
void SplitMatrix<MatrixDataELL>::convert(const MatrixCOO &coo,
const WorkDistribution &wd) {
N = coo.N;
nz = coo.nz;
numberOfChunks = wd.numberOfChunks;
allocateData();
// Allocate length for each chunk.
for (int c = 0; c < numberOfChunks; c++) {
int offset = wd.offsets[c];
int length = wd.lengths[c];
int maxNz = coo.getMaxNz(offset, offset + length);
// Copy over already collected nonzeros per row.
data[c].allocateLength(length);
std::memcpy(data[c].length, coo.nzPerRow.get() + offset,
sizeof(int) * length);
data[c].elements = maxNz * length;
data[c].allocateIndexAndData();
}
// Temporary memory to store current offset in index / value per row.
std::unique_ptr<int[]> offsets(new int[N]);
std::memset(offsets.get(), 0, sizeof(int) * N);
// Construct column and data for all chunks.
for (int i = 0; i < nz; i++) {
int row = coo.I[i];
int chunk = wd.findChunk(row);
int k = offsets[row] * wd.lengths[chunk] + row - wd.offsets[chunk];
data[chunk].index[k] = coo.J[i];
data[chunk].data[k] = coo.V[i];
offsets[row]++;
}
}
template <>
void PartitionedMatrix<MatrixDataELL>::convert(const MatrixCOO &coo,
const WorkDistribution &wd) {
N = coo.N;
nz = coo.nz;
numberOfChunks = wd.numberOfChunks;
allocateDiagAndMinor();
// Temporary memory to count nonzeros per row.
std::unique_ptr<int[]> nzDiag, nzMinor;
coo.countNz(wd, nzDiag, nzMinor);
// Allocate length for each chunk.
for (int c = 0; c < numberOfChunks; c++) {
int offset = wd.offsets[c];
int length = wd.lengths[c];
int maxNzDiag = 0, maxNzMinor = 0;
for (int i = offset; i < offset + length; i++) {
if (nzDiag[i] > maxNzDiag) {
maxNzDiag = nzDiag[i];
}
if (nzMinor[i] > maxNzMinor) {
maxNzMinor = nzMinor[i];
}
}
// Copy over already collected nonzeros per row.
diag[c].allocateLength(length);
std::memcpy(diag[c].length, nzDiag.get() + offset, sizeof(int) * length);
minor[c].allocateLength(length);
std::memcpy(minor[c].length, nzMinor.get() + offset, sizeof(int) * length);
diag[c].elements = maxNzDiag * length;
diag[c].allocateIndexAndData();
minor[c].elements = maxNzMinor * length;
minor[c].allocateIndexAndData();
}
// Temporary memory to store current offset in index / value per row.
std::unique_ptr<int[]> offsetsDiag(new int[N]);
std::memset(offsetsDiag.get(), 0, sizeof(int) * N);
std::unique_ptr<int[]> offsetsMinor(new int[N]);
std::memset(offsetsMinor.get(), 0, sizeof(int) * N);
// Construct column and data for all chunks.
for (int i = 0; i < nz; i++) {
int row = coo.I[i];
int column = coo.J[i];
int chunk = wd.findChunk(row);
if (wd.isOnDiagonal(chunk, column)) {
int k = offsetsDiag[row] * wd.lengths[chunk] + row - wd.offsets[chunk];
diag[chunk].index[k] = coo.J[i];
diag[chunk].data[k] = coo.V[i];
offsetsDiag[row]++;
} else {
int k = offsetsMinor[row] * wd.lengths[chunk] + row - wd.offsets[chunk];
minor[chunk].index[k] = coo.J[i];
minor[chunk].data[k] = coo.V[i];
offsetsMinor[row]++;
}
}
}
// Instantiate templates:
template struct SplitMatrix<MatrixDataCRS>;
template struct SplitMatrix<MatrixDataELL>;
template struct PartitionedMatrix<MatrixDataCRS>;
template struct PartitionedMatrix<MatrixDataELL>;