-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.h
154 lines (122 loc) · 3.98 KB
/
Matrix.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
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef MATRIX_H
#define MATRIX_H
#include <memory>
#include "def.h"
// Forward declaration to not include WorkDistribution.h
struct WorkDistribution;
/// Base class for storing a sparse matrix.
struct Matrix {
/// Dimension of this matrix.
int N;
/// Nonzeros in this matrix.
int nz;
};
/// %Matrix stored in coordinate format.
struct MatrixCOO : Matrix {
/// Array of rows.
std::unique_ptr<int[]> I;
/// Array of columns.
std::unique_ptr<int[]> J;
/// Values in the matrix.
std::unique_ptr<floatType[]> V;
/// Nonzeros in a row.
std::unique_ptr<int[]> nzPerRow;
MatrixCOO() = delete;
/// Read matrix in coordinate from \a file.
MatrixCOO(const char *file);
/// Get maximum number of nonzeros in a row.
int getMaxNz() const { return getMaxNz(0, N); }
/// Get maximum number of nonzeros in a row between \a from and \a to.
int getMaxNz(int from, int to) const;
/// @return number of nonzeros for each chunk in \a wd.
void countNz(const WorkDistribution &wd, std::unique_ptr<int[]> &nzDiag,
std::unique_ptr<int[]> &nzMinor) const;
};
/// Data for storing a matrix in CRS format.
struct MatrixDataCRS {
/// Start index in #index and #value for a given row.
int *ptr;
/// Array of column indices.
int *index;
/// Values in the matrix.
floatType *value;
virtual ~MatrixDataCRS() { }
/// Allocate #ptr.
virtual void allocatePtr(int rows);
/// Deallocate #ptr.
virtual void deallocatePtr();
/// Allocate #index and #value.
virtual void allocateIndexAndValue(int values);
/// Deallocate #index and #value.
virtual void deallocateIndexAndValue();
void deallocate() {
deallocatePtr();
deallocateIndexAndValue();
}
};
/// Data for storing a matrix in ELLPACK format.
struct MatrixDataELL {
/// Maximum number of nonzeros in a row.
/// @see MatrixCOO#getMaxNz()
int maxNz;
/// Elements in #index and #data including padding.
int elements;
/// Array of length, holding number of nonzeros per row.
int *length;
/// Array of column indices.
int *index;
/// Data in the matrix.
floatType *data;
virtual ~MatrixDataELL() { }
/// Allocate #length.
virtual void allocateLength(int rows);
/// Deallocate #length.
virtual void deallocateLength();
/// Allocate #index and #data.
virtual void allocateIndexAndData();
/// Deallocate #index and #data.
virtual void deallocateIndexAndData();
void deallocate() {
deallocateLength();
deallocateIndexAndData();
}
};
/// %Matrix with specified data.
template <class Data> struct DataMatrix : Matrix, Data {
/// Convert \a coo.
void convert(const MatrixCOO &coo);
};
using MatrixCRS = DataMatrix<MatrixDataCRS>;
using MatrixELL = DataMatrix<MatrixDataELL>;
/// %Matrix split for a WorkDistribution.
template <class Data> struct SplitMatrix : Matrix {
/// Number of chunks in this matrix.
int numberOfChunks;
/// Data for each chunk of the WorkDistribution.
std::unique_ptr<Data[]> data;
/// Convert \a coo and split based on \a wd.
void convert(const MatrixCOO &coo, const WorkDistribution &wd);
/// Allocate #data.
virtual void allocateData();
virtual ~SplitMatrix();
};
using SplitMatrixCRS = SplitMatrix<MatrixDataCRS>;
using SplitMatrixELL = SplitMatrix<MatrixDataELL>;
/// %Matrix partitioned for a WorkDistribution.
template <class Data> struct PartitionedMatrix : Matrix {
/// Number of chunks in this matrix.
int numberOfChunks;
/// Data on the diagonal for each chunk of the WorkDistribution.
std::unique_ptr<Data[]> diag;
/// Data NOT on the diagonal for each chunk of the WorkDistribution.
std::unique_ptr<Data[]> minor;
/// Convert \a coo and partition based on \a wd.
void convert(const MatrixCOO &coo, const WorkDistribution &wd);
/// Allocate #diag and #minor.
virtual void allocateDiagAndMinor();
virtual ~PartitionedMatrix();
};
using PartitionedMatrixCRS = PartitionedMatrix<MatrixDataCRS>;
using PartitionedMatrixELL = PartitionedMatrix<MatrixDataELL>;
#endif