-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathEwBinaryMat.h
343 lines (305 loc) · 16.2 KB
/
EwBinaryMat.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
/*
* 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/context/DaphneContext.h>
#include <runtime/local/datastructures/CSRMatrix.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Matrix.h>
#include <runtime/local/kernels/BinaryOpCode.h>
#include <runtime/local/kernels/EwBinarySca.h>
#include <cstddef>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTRes, class DTLhs, class DTRhs> struct EwBinaryMat {
static void apply(BinaryOpCode opCode, DTRes *&res, const DTLhs *lhs, const DTRhs *rhs, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTRes, class DTLhs, class DTRhs>
void ewBinaryMat(BinaryOpCode opCode, DTRes *&res, const DTLhs *lhs, const DTRhs *rhs, DCTX(ctx)) {
EwBinaryMat<DTRes, DTLhs, DTRhs>::apply(opCode, res, lhs, rhs, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix, DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTres, typename VTlhs, typename VTrhs>
struct EwBinaryMat<DenseMatrix<VTres>, DenseMatrix<VTlhs>, DenseMatrix<VTrhs>> {
static void apply(BinaryOpCode opCode, DenseMatrix<VTres> *&res, const DenseMatrix<VTlhs> *lhs,
const DenseMatrix<VTrhs> *rhs, DCTX(ctx)) {
const size_t numRowsLhs = lhs->getNumRows();
const size_t numColsLhs = lhs->getNumCols();
const size_t numRowsRhs = rhs->getNumRows();
const size_t numColsRhs = rhs->getNumCols();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTres>>(numRowsLhs, numColsLhs, false);
const VTlhs *valuesLhs = lhs->getValues();
const VTrhs *valuesRhs = rhs->getValues();
VTres *valuesRes = res->getValues();
EwBinaryScaFuncPtr<VTres, VTlhs, VTrhs> func = getEwBinaryScaFuncPtr<VTres, VTlhs, VTrhs>(opCode);
if (numRowsLhs == numRowsRhs && numColsLhs == numColsRhs) {
// matrix op matrix (same size)
for (size_t r = 0; r < numRowsLhs; r++) {
for (size_t c = 0; c < numColsLhs; c++)
valuesRes[c] = func(valuesLhs[c], valuesRhs[c], ctx);
valuesLhs += lhs->getRowSkip();
valuesRhs += rhs->getRowSkip();
valuesRes += res->getRowSkip();
}
} else if (numColsLhs == numColsRhs && (numRowsRhs == 1 || numRowsLhs == 1)) {
// matrix op row-vector
for (size_t r = 0; r < numRowsLhs; r++) {
for (size_t c = 0; c < numColsLhs; c++)
valuesRes[c] = func(valuesLhs[c], valuesRhs[c], ctx);
valuesLhs += lhs->getRowSkip();
valuesRes += res->getRowSkip();
}
} else if (numRowsLhs == numRowsRhs && (numColsRhs == 1 || numColsLhs == 1)) {
// matrix op col-vector
for (size_t r = 0; r < numRowsLhs; r++) {
for (size_t c = 0; c < numColsLhs; c++)
valuesRes[c] = func(valuesLhs[c], valuesRhs[0], ctx);
valuesLhs += lhs->getRowSkip();
valuesRhs += rhs->getRowSkip();
valuesRes += res->getRowSkip();
}
} else {
throw std::runtime_error("EwBinaryMat(Dense) - lhs and rhs must either "
"have the same dimensions, or one of them must be a row/column "
"vector "
"with the width/height of the other, but lhs has shape (" +
std::to_string(numRowsLhs) + " x " + std::to_string(numColsLhs) +
") and rhs has shape (" + std::to_string(numRowsRhs) + " x " +
std::to_string(numColsRhs) + ")");
}
}
};
// ----------------------------------------------------------------------------
// CSRMatrix <- CSRMatrix, CSRMatrix
// ----------------------------------------------------------------------------
template <typename VT> struct EwBinaryMat<CSRMatrix<VT>, CSRMatrix<VT>, CSRMatrix<VT>> {
static void apply(BinaryOpCode opCode, CSRMatrix<VT> *&res, const CSRMatrix<VT> *lhs, const CSRMatrix<VT> *rhs,
DCTX(ctx)) {
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (numRows != rhs->getNumRows() || numCols != rhs->getNumCols())
throw std::runtime_error("EwBinaryMat(CSR) - lhs and rhs must have "
"the same dimensions.");
size_t maxNnz;
switch (opCode) {
case BinaryOpCode::ADD: // merge
maxNnz = lhs->getNumNonZeros() + rhs->getNumNonZeros();
break;
case BinaryOpCode::MUL: // intersect
maxNnz = std::min(lhs->getNumNonZeros(), rhs->getNumNonZeros());
break;
default:
throw std::runtime_error("EwBinaryMat(CSR) - unknown BinaryOpCode");
}
if (res == nullptr)
res = DataObjectFactory::create<CSRMatrix<VT>>(numRows, numCols, maxNnz, false);
size_t *rowOffsetsRes = res->getRowOffsets();
EwBinaryScaFuncPtr<VT, VT, VT> func = getEwBinaryScaFuncPtr<VT, VT, VT>(opCode);
rowOffsetsRes[0] = 0;
switch (opCode) {
case BinaryOpCode::ADD: { // merge non-zero cells
for (size_t rowIdx = 0; rowIdx < numRows; rowIdx++) {
size_t nnzRowLhs = lhs->getNumNonZeros(rowIdx);
size_t nnzRowRhs = rhs->getNumNonZeros(rowIdx);
if (nnzRowLhs && nnzRowRhs) {
// merge within row
const VT *valuesRowLhs = lhs->getValues(rowIdx);
const VT *valuesRowRhs = rhs->getValues(rowIdx);
VT *valuesRowRes = res->getValues(rowIdx);
const size_t *colIdxsRowLhs = lhs->getColIdxs(rowIdx);
const size_t *colIdxsRowRhs = rhs->getColIdxs(rowIdx);
size_t *colIdxsRowRes = res->getColIdxs(rowIdx);
size_t posLhs = 0;
size_t posRhs = 0;
size_t posRes = 0;
while (posLhs < nnzRowLhs && posRhs < nnzRowRhs) {
if (colIdxsRowLhs[posLhs] == colIdxsRowRhs[posRhs]) {
VT funcRes = func(valuesRowLhs[posLhs], valuesRowRhs[posRhs], ctx);
if (funcRes != VT(0)) {
valuesRowRes[posRes] = funcRes;
colIdxsRowRes[posRes] = colIdxsRowLhs[posLhs];
posRes++;
}
posLhs++;
posRhs++;
} else if (colIdxsRowLhs[posLhs] < colIdxsRowRhs[posRhs]) {
valuesRowRes[posRes] = valuesRowLhs[posLhs];
colIdxsRowRes[posRes] = colIdxsRowLhs[posLhs];
posLhs++;
posRes++;
} else {
valuesRowRes[posRes] = valuesRowRhs[posRhs];
colIdxsRowRes[posRes] = colIdxsRowRhs[posRhs];
posRhs++;
posRes++;
}
}
// copy from left
const size_t restRowLhs = nnzRowLhs - posLhs;
memcpy(valuesRowRes + posRes, valuesRowLhs + posLhs, restRowLhs * sizeof(VT));
memcpy(colIdxsRowRes + posRes, colIdxsRowLhs + posLhs, restRowLhs * sizeof(size_t));
// copy from right
const size_t restRowRhs = nnzRowRhs - posRhs;
memcpy(valuesRowRes + posRes, valuesRowRhs + posRhs, restRowRhs * sizeof(VT));
memcpy(colIdxsRowRes + posRes, colIdxsRowRhs + posRhs, restRowRhs * sizeof(size_t));
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx] + posRes + restRowLhs + restRowRhs;
} else if (nnzRowLhs) {
// copy from left
memcpy(res->getValues(rowIdx), lhs->getValues(rowIdx), nnzRowLhs * sizeof(VT));
memcpy(res->getColIdxs(rowIdx), lhs->getColIdxs(rowIdx), nnzRowLhs * sizeof(size_t));
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx] + nnzRowLhs;
} else if (nnzRowRhs) {
// copy from right
memcpy(res->getValues(rowIdx), rhs->getValues(rowIdx), nnzRowRhs * sizeof(VT));
memcpy(res->getColIdxs(rowIdx), rhs->getColIdxs(rowIdx), nnzRowRhs * sizeof(size_t));
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx] + nnzRowRhs;
} else
// empty row in result
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx];
}
break;
}
case BinaryOpCode::MUL: { // intersect non-zero cells
for (size_t rowIdx = 0; rowIdx < numRows; rowIdx++) {
size_t nnzRowLhs = lhs->getNumNonZeros(rowIdx);
size_t nnzRowRhs = rhs->getNumNonZeros(rowIdx);
if (nnzRowLhs && nnzRowRhs) {
// intersect within row
const VT *valuesRowLhs = lhs->getValues(rowIdx);
const VT *valuesRowRhs = rhs->getValues(rowIdx);
VT *valuesRowRes = res->getValues(rowIdx);
const size_t *colIdxsRowLhs = lhs->getColIdxs(rowIdx);
const size_t *colIdxsRowRhs = rhs->getColIdxs(rowIdx);
size_t *colIdxsRowRes = res->getColIdxs(rowIdx);
size_t posLhs = 0;
size_t posRhs = 0;
size_t posRes = 0;
while (posLhs < nnzRowLhs && posRhs < nnzRowRhs) {
if (colIdxsRowLhs[posLhs] == colIdxsRowRhs[posRhs]) {
valuesRowRes[posRes] = func(valuesRowLhs[posLhs], valuesRowRhs[posRhs], ctx);
colIdxsRowRes[posRes] = colIdxsRowLhs[posLhs];
posLhs++;
posRhs++;
posRes++;
} else if (colIdxsRowLhs[posLhs] < colIdxsRowRhs[posRhs])
posLhs++;
else
posRhs++;
}
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx] + posRes;
} else
// empty row in result
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx];
}
break;
}
default:
throw std::runtime_error("EwBinaryMat(CSR) - unknown BinaryOpCode");
}
// TODO Update number of non-zeros in result in the end.
}
};
// ----------------------------------------------------------------------------
// CSRMatrix <- CSRMatrix, DenseMatrix
// ----------------------------------------------------------------------------
template <typename VT> struct EwBinaryMat<CSRMatrix<VT>, CSRMatrix<VT>, DenseMatrix<VT>> {
static void apply(BinaryOpCode opCode, CSRMatrix<VT> *&res, const CSRMatrix<VT> *lhs, const DenseMatrix<VT> *rhs,
DCTX(ctx)) {
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
// TODO: lhs broadcast
if ((numRows != rhs->getNumRows() && rhs->getNumRows() != 1) ||
(numCols != rhs->getNumCols() && rhs->getNumCols() != 1))
throw std::runtime_error("EwBinaryMat(CSR) - lhs and rhs must have "
"the same dimensions (or broadcast)");
size_t maxNnz;
switch (opCode) {
case BinaryOpCode::MUL: // intersect
maxNnz = lhs->getNumNonZeros();
break;
default:
throw std::runtime_error("EwBinaryMat(CSR) - unknown BinaryOpCode");
}
if (res == nullptr)
res = DataObjectFactory::create<CSRMatrix<VT>>(numRows, numCols, maxNnz, false);
size_t *rowOffsetsRes = res->getRowOffsets();
EwBinaryScaFuncPtr<VT, VT, VT> func = getEwBinaryScaFuncPtr<VT, VT, VT>(opCode);
rowOffsetsRes[0] = 0;
switch (opCode) {
case BinaryOpCode::MUL: { // intersect non-zero cells
for (size_t rowIdx = 0; rowIdx < numRows; rowIdx++) {
size_t nnzRowLhs = lhs->getNumNonZeros(rowIdx);
if (nnzRowLhs) {
// intersect within row
const VT *valuesRowLhs = lhs->getValues(rowIdx);
VT *valuesRowRes = res->getValues(rowIdx);
const size_t *colIdxsRowLhs = lhs->getColIdxs(rowIdx);
size_t *colIdxsRowRes = res->getColIdxs(rowIdx);
auto rhsRow = (rhs->getNumRows() == 1 ? 0 : rowIdx);
size_t posRes = 0;
for (size_t posLhs = 0; posLhs < nnzRowLhs; ++posLhs) {
auto rhsCol = (rhs->getNumCols() == 1 ? 0 : colIdxsRowLhs[posLhs]);
auto rVal = rhs->get(rhsRow, rhsCol);
if (rVal != 0) {
valuesRowRes[posRes] = func(valuesRowLhs[posLhs], rVal, ctx);
colIdxsRowRes[posRes] = colIdxsRowLhs[posLhs];
posRes++;
}
}
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx] + posRes;
} else
// empty row in result
rowOffsetsRes[rowIdx + 1] = rowOffsetsRes[rowIdx];
}
break;
}
default:
throw std::runtime_error("EwBinaryMat(CSR) - unknown BinaryOpCode");
}
// TODO Update number of non-zeros in result in the end.
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix, Matrix
// ----------------------------------------------------------------------------
template <typename VT> struct EwBinaryMat<Matrix<VT>, Matrix<VT>, Matrix<VT>> {
static void apply(BinaryOpCode opCode, Matrix<VT> *&res, const Matrix<VT> *lhs, const Matrix<VT> *rhs, DCTX(ctx)) {
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (numRows != rhs->getNumRows() || numCols != rhs->getNumCols())
throw std::runtime_error("EwBinaryMat - lhs and rhs must have the same dimensions.");
// TODO Choose matrix implementation depending on expected number of
// non-zeros.
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRows, numCols, false);
EwBinaryScaFuncPtr<VT, VT, VT> func = getEwBinaryScaFuncPtr<VT, VT, VT>(opCode);
res->prepareAppend();
for (size_t r = 0; r < numRows; ++r)
for (size_t c = 0; c < numCols; ++c)
res->append(r, c, func(lhs->get(r, c), rhs->get(r, c), ctx));
res->finishAppend();
}
};