-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparseMatmul.cpp
259 lines (210 loc) · 7.41 KB
/
sparseMatmul.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
#pragma once
#include <vector>
#include <algorithm>
#include <queue>
#include "utils.cpp"
#include "CSR.cpp"
using namespace std;
void preZeroCount(vector<vector<double>> &mat, vector<vector<int>> &preSum, int rows, int cols)
{
for (int row = 0; row < rows; row++)
{
double pre = mat[row][0];
for (int col = 1; col < cols; col++)
{
preSum[row][col] = preSum[row][col - 1];
if (pre == 0)
{
preSum[row][col] += 1;
}
pre = mat[row][col];
}
}
}
void zeroElimination(vector<vector<double>> &mat, vector<vector<int>> &preSum, int rows, int cols)
{
for (int row = 0; row < rows; row++)
{
for (int round = 1; round < cols; round *= 2)
{
for (int col = round; col < cols; col++)
{
if (preSum[row][col] & 1 == 1)
{
mat[row][col - round] = mat[row][col];
mat[row][col] = 0;
}
preSum[row][col] >>= 1;
}
}
}
}
void condenseMatrix(vector<vector<double>> &mat, int rows, int cols)
{
vector<vector<int>> preSum(rows, vector<int>(cols));
preZeroCount(mat, preSum, rows, cols);
zeroElimination(mat, preSum, rows, cols);
}
vector<vector<pair<pair<int, int>, double>>> schedule(vector<vector<double>> &matA, vector<vector<double>> &matB, vector<vector<double>> &matC, int I, int J, int K, int schedulerPortNum)
{
CSRMat<double> matACSR(matA);
auto data = matACSR.getData();
auto colIdx = matACSR.getColIdx();
auto rowIdx = matACSR.getRowIdx();
int condensedCol = -1;
for (int i = 1; i < rowIdx.size(); i++)
{
condensedCol = max(condensedCol, rowIdx[i] - rowIdx[i - 1]);
}
vector<vector<pair<pair<int, int>, double>>> ports(condensedCol);
for (int col = matA[0].size() - 1; col >= 1;)
{
auto leftBorder = col - 1;
for (int row = 1; row <= matA.size(); row++)
{
auto rightBorder = min(col, rowIdx[row] - rowIdx[row - 1]);
for (int idx = leftBorder + rowIdx[row - 1]; idx < rightBorder + rowIdx[row - 1]; idx++)
{
auto val = data[idx];
auto realCol = colIdx[idx];
ports[idx - rowIdx[row - 1]].push_back({{row - 1, realCol}, val});
}
}
col = leftBorder;
}
return ports;
}
vector<CSRMat<double>> partialMatrix(vector<vector<pair<pair<int, int>, double>>> &ports, vector<vector<double>> &matB, int I, int J, int K)
{
CSRMat<double> matBCSR(matB);
auto data = matBCSR.getData();
auto colIdx = matBCSR.getColIdx();
auto rowIdx = matBCSR.getRowIdx();
vector<vector<vector<double>>> partialMatrixs;
vector<CSRMat<double>> partialMatrixsCSR;
for (int denseCols = 0; denseCols < ports.size(); denseCols++)
{
vector<vector<double>> partial(I, vector<double>(K));
CSRMat<double> partialCSR(I);
for (auto ele : ports[denseCols])
{
auto row = ele.first.first;
auto realCol = ele.first.second;
auto leftdata = ele.second;
auto left = rowIdx[realCol];
auto right = rowIdx[realCol + 1];
for (auto idx = left; idx < right; idx++)
{
auto rightdata = data[idx];
auto col = colIdx[idx];
partial[row][col] = leftdata * rightdata;
partialCSR.insert(row, col, leftdata * rightdata);
}
}
partialMatrixs.push_back(partial);
partialMatrixsCSR.push_back(partialCSR);
}
return partialMatrixsCSR;
}
CSRMat<double> mergePartialMat(vector<pair<pair<int, int>, CSRMat<double>>> &partialMatrixs)
{
auto cmp = [](auto &A, auto &B) -> bool
{ return A.first.first > B.first.first || A.first.first == B.first.first && A.first.second > B.first.second; };
priority_queue<pair<pair<int, int>, double>, vector<pair<pair<int, int>, double>>, decltype(cmp)> pq(cmp);
for (auto i = 0; i < partialMatrixs.size(); i++)
{
auto data = partialMatrixs[i].second.getData();
auto colIdx = partialMatrixs[i].second.getColIdx();
auto rowIdx = partialMatrixs[i].second.getRowIdx();
auto border = rowIdx[1];
int row = 0;
for (int j = 0; j < data.size(); j++)
{
if (j >= border)
{
row++;
border = rowIdx[row + 1];
}
pq.push({{row, colIdx[j]}, data[j]});
}
}
auto rows = partialMatrixs[0].second.getRowIdx().size();
CSRMat<double> mergetCSR(rows);
pair<int, int> prev = pq.top().first;
double sum = pq.top().second;
pq.pop();
while (!pq.empty())
{
auto ele = pq.top();
if (ele.first.first == prev.first && ele.first.second == prev.second)
{
sum += ele.second;
}
else
{
mergetCSR.insert(prev.first, prev.second, sum);
prev = ele.first;
sum = ele.second;
}
pq.pop();
}
mergetCSR.insert(prev.first, prev.second, sum);
return mergetCSR;
}
CSRMat<double> huffmanScheduler(vector<vector<pair<pair<int, int>, double>>> &ports, vector<CSRMat<double>> &partialMatrixs, int initialfetchColNum, int schedulerPortNum)
{
auto cmp = [](auto &A, auto &B) -> bool
{ return A.first.first > B.first.first || A.first.first == B.first.first && A.first.second > B.first.second; };
priority_queue<pair<pair<int, int>, CSRMat<double>>, vector<pair<pair<int, int>, CSRMat<double>>>, decltype(cmp)> pq(cmp);
for (auto i = 0; i < ports.size(); i++)
{
pq.push({{ports[i].size(), 1}, partialMatrixs[i]});
}
bool firstRound = true;
while (pq.size() > 1)
{
int cnt = 0;
vector<pair<pair<int, int>, CSRMat<double>>> prepare;
while (cnt < schedulerPortNum)
{
auto ele = pq.top();
cnt++;
prepare.push_back({{ele.first.first, ele.first.second}, ele.second});
pq.pop();
if (firstRound && cnt == initialfetchColNum)
{
firstRound = false;
break;
}
}
int workLoad = 0;
int layer = -1;
for (int i = 0; i < prepare.size(); i++)
{
workLoad += prepare[i].first.first;
layer = max(layer, prepare[i].first.second);
}
auto newPartialMat = mergePartialMat(prepare);
pq.push({{workLoad, layer}, newPartialMat});
}
auto remain = pq.top();
auto csrMat = remain.second;
return csrMat;
}
void sparseMatmul(vector<vector<double>> &matA, vector<vector<double>> &matB, vector<vector<double>> &matC, int I, int J, int K, int schedulerPortNum)
{
auto ports = schedule(matA, matB, matC, I, J, K, schedulerPortNum);
auto partialMatrixs = partialMatrix(ports, matB, I, J, K);
CSRMat<double> matACSR(matA);
auto data = matACSR.getData();
auto colIdx = matACSR.getColIdx();
auto rowIdx = matACSR.getRowIdx();
int condensedCol = -1;
for (int i = 1; i < rowIdx.size(); i++)
{
condensedCol = max(condensedCol, rowIdx[i] - rowIdx[i - 1]);
}
int initialfetchColNum = (condensedCol - 2) % (schedulerPortNum - 1) + 2;
auto finalCSRMAT = huffmanScheduler(ports, partialMatrixs, initialfetchColNum, schedulerPortNum);
matC = finalCSRMAT.restore(I, K);
}