forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggCol.h
More file actions
382 lines (318 loc) · 15.9 KB
/
Copy pathAggCol.h
File metadata and controls
382 lines (318 loc) · 15.9 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
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
/*
* 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.
*/
#ifndef SRC_RUNTIME_LOCAL_KERNELS_AGGCOL_H
#define SRC_RUNTIME_LOCAL_KERNELS_AGGCOL_H
#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/AggOpCode.h>
#include <runtime/local/kernels/EwBinarySca.h>
#include <vector>
#include <cmath>
#include <cstddef>
#include <cstring>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTRes, class DTArg> struct AggCol {
static void apply(AggOpCode opCode, DTRes *&res, const DTArg *arg, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTRes, class DTArg> void aggCol(AggOpCode opCode, DTRes *&res, const DTArg *arg, DCTX(ctx)) {
AggCol<DTRes, DTArg>::apply(opCode, res, arg, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggCol<DenseMatrix<VTRes>, DenseMatrix<VTArg>> {
static void apply(AggOpCode opCode, DenseMatrix<VTRes> *&res, const DenseMatrix<VTArg> *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTRes>>(1, numCols, false);
const VTArg *valuesArg = arg->getValues();
VTRes *valuesRes = res->getValues();
// TODO Merge the cases for IDXMIN and IDXMAX to avoid code duplication.
if (opCode == AggOpCode::IDXMIN) {
// Minimum values seen so far per column (initialize with first row
// of argument).
auto tmp = DataObjectFactory::create<DenseMatrix<VTArg>>(1, numCols, false);
VTArg *valuesTmp = tmp->getValues();
memcpy(valuesTmp, valuesArg, numCols * sizeof(VTArg));
// Positions at which the minimum values were found (initialize with
// zeros), stored directly in the result.
for (size_t c = 0; c < numCols; c++)
valuesRes[c] = 0;
// Scan over the remaining rows and update the minimum values and
// their positions accordingly.
valuesArg += arg->getRowSkip();
for (size_t r = 1; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++)
if (valuesArg[c] < valuesTmp[c]) {
valuesTmp[c] = valuesArg[c];
valuesRes[c] = r;
}
valuesArg += arg->getRowSkip();
}
// Free the temporary minimum values.
DataObjectFactory::destroy(tmp);
} else if (opCode == AggOpCode::IDXMAX) {
// Maximum values seen so far per column (initialize with first row
// of argument).
auto tmp = DataObjectFactory::create<DenseMatrix<VTArg>>(1, numCols, false);
VTArg *valuesTmp = tmp->getValues();
memcpy(valuesTmp, valuesArg, numCols * sizeof(VTArg));
// Positions at which the maximum values were found (initialize with
// zeros), stored directly in the result.
for (size_t c = 0; c < numCols; c++)
valuesRes[c] = 0;
// Scan over the remaining rows and update the maximum values and
// their positions accordingly.
valuesArg += arg->getRowSkip();
for (size_t r = 1; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++)
if (valuesArg[c] > valuesTmp[c]) {
valuesTmp[c] = valuesArg[c];
valuesRes[c] = r;
}
valuesArg += arg->getRowSkip();
}
// Free the temporary maximum values.
DataObjectFactory::destroy(tmp);
} else {
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func;
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
else
// TODO Setting the function pointer yields the correct result.
// However, since MEAN and STDDEV are not sparse-safe, the
// program does not take the same path for doing the summation,
// and is less efficient. for MEAN and STDDDEV, we need to sum
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
// memcpy(valuesRes, valuesArg, numCols * sizeof(VTRes));
// Can't memcpy because we might have different result type
for (size_t c = 0; c < numCols; c++)
valuesRes[c] = static_cast<VTRes>(valuesArg[c]);
for (size_t r = 1; r < numRows; r++) {
valuesArg += arg->getRowSkip();
for (size_t c = 0; c < numCols; c++)
valuesRes[c] = func(valuesRes[c], static_cast<VTRes>(valuesArg[c]), ctx);
}
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return;
// The op-code is either MEAN or STDDEV or VAR.
for (size_t c = 0; c < numCols; c++)
valuesRes[c] /= numRows;
if (opCode == AggOpCode::MEAN)
return;
auto tmp = DataObjectFactory::create<DenseMatrix<VTRes>>(1, numCols, true);
VTRes *valuesT = tmp->getValues();
valuesArg = arg->getValues();
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++) {
VTRes val = static_cast<VTRes>(valuesArg[c]) - valuesRes[c];
valuesT[c] = valuesT[c] + val * val;
}
valuesArg += arg->getRowSkip();
}
for (size_t c = 0; c < numCols; c++) {
valuesT[c] /= numRows;
if (opCode == AggOpCode::STDDEV)
valuesT[c] = sqrt(valuesT[c]);
}
// TODO We could avoid copying by returning tmp and destroying res.
// But that might be wrong if res was not nullptr initially.
memcpy(valuesRes, valuesT, numCols * sizeof(VTRes));
DataObjectFactory::destroy<DenseMatrix<VTRes>>(tmp);
}
}
};
// ----------------------------------------------------------------------------
// DenseMatrix <- CSRMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggCol<DenseMatrix<VTRes>, CSRMatrix<VTArg>> {
static void apply(AggOpCode opCode, DenseMatrix<VTRes> *&res, const CSRMatrix<VTArg> *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTRes>>(1, numCols, true);
VTRes *valuesRes = res->getValues();
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func;
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
else
// TODO Setting the function pointer yields the correct result.
// However, since MEAN and STDDEV are not sparse-safe, the program
// does not take the same path for doing the summation, and is less
// efficient.
// for MEAN and STDDDEV, we need to sum
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
const VTArg *valuesArg = arg->getValues(0);
const size_t *colIdxsArg = arg->getColIdxs(0);
const size_t numNonZeros = arg->getNumNonZeros();
if (AggOpCodeUtils::isSparseSafe(opCode)) {
for (size_t i = 0; i < numNonZeros; i++) {
const size_t colIdx = colIdxsArg[i];
valuesRes[colIdx] = func(valuesRes[colIdx], static_cast<VTRes>(valuesArg[i]), ctx);
}
} else {
size_t *hist = new size_t[numCols](); // initialized to zeros
const size_t numNonZerosFirstRowArg = arg->getNumNonZeros(0);
for (size_t i = 0; i < numNonZerosFirstRowArg; i++) {
size_t colIdx = colIdxsArg[i];
valuesRes[colIdx] = static_cast<VTRes>(valuesArg[i]);
hist[colIdx]++;
}
if (arg->getNumRows() > 1) {
for (size_t i = numNonZerosFirstRowArg; i < numNonZeros; i++) {
const size_t colIdx = colIdxsArg[i];
valuesRes[colIdx] = func(valuesRes[colIdx], static_cast<VTRes>(valuesArg[i]), ctx);
hist[colIdx]++;
}
for (size_t c = 0; c < numCols; c++)
if (hist[c] < numRows)
valuesRes[c] = func(valuesRes[c], VTRes(0), ctx);
}
delete[] hist;
}
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return;
// The op-code is either MEAN or STDDEV or VAR.
for (size_t c = 0; c < numCols; c++)
valuesRes[c] /= arg->getNumRows();
if (opCode == AggOpCode::MEAN)
return;
auto tmp = DataObjectFactory::create<DenseMatrix<VTRes>>(1, numCols, true);
VTRes *valuesT = tmp->getValues();
size_t *nnzCol = new size_t[numCols](); // initialized to zeros
for (size_t i = 0; i < numNonZeros; i++) {
const size_t colIdx = colIdxsArg[i];
VTRes val = static_cast<VTRes>(valuesArg[i]) - valuesRes[colIdx];
valuesT[colIdx] = valuesT[colIdx] + val * val;
nnzCol[colIdx]++;
}
for (size_t c = 0; c < numCols; c++) {
// Take all zeros in the column into account.
valuesT[c] += (valuesRes[c] * valuesRes[c]) * (numRows - nnzCol[c]);
// Finish computation of stddev.
valuesT[c] /= numRows;
if (opCode == AggOpCode::STDDEV)
valuesT[c] = sqrt(valuesT[c]);
}
delete[] nnzCol;
// TODO We could avoid copying by returning tmp and destroying res. But
// that might be wrong if res was not nullptr initially.
memcpy(valuesRes, valuesT, numCols * sizeof(VTRes));
DataObjectFactory::destroy<DenseMatrix<VTRes>>(tmp);
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggCol<Matrix<VTRes>, Matrix<VTArg>> {
static void apply(AggOpCode opCode, Matrix<VTRes> *&res, const Matrix<VTArg> *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTRes>>(1, numCols, false);
if (opCode == AggOpCode::IDXMIN || opCode == AggOpCode::IDXMAX) {
// Minimum/Maximum values seen so far per column (initialize with
// first row of argument).
std::vector<VTArg> tmp;
tmp.reserve(numCols);
for (size_t c = 0; c < numCols; ++c)
tmp.emplace_back(arg->get(0, c));
// Positions at which the minimum/maximum values were found
// (initialize with zeros), stored directly in the result.
res->prepareAppend();
res->finishAppend();
// Scan over the remaining rows and update the minimum values and
// their positions accordingly.
// TODO: reduce code duplication with lambda
// initial test seemed slower than separate loops but should
// be tested again
if (opCode == AggOpCode::IDXMIN) {
for (size_t r = 1; r < numRows; ++r) {
for (size_t c = 0; c < numCols; ++c) {
VTArg argVal = arg->get(r, c);
if (argVal < tmp[c]) {
tmp[c] = argVal;
res->set(0, c, r);
}
}
}
} else {
for (size_t r = 1; r < numRows; ++r) {
for (size_t c = 0; c < numCols; ++c) {
VTArg argVal = arg->get(r, c);
if (argVal > tmp[c]) {
tmp[c] = argVal;
res->set(0, c, r);
}
}
}
}
} else {
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func;
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
else
// TODO Setting the function pointer yields the correct result.
// However, since MEAN and STDDEV are not sparse-safe, the
// program does not take the same path for doing the summation,
// and is less efficient. for MEAN and STDDDEV, we need to sum
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
res->prepareAppend();
for (size_t c = 0; c < numCols; ++c)
res->append(0, c, static_cast<VTRes>(arg->get(0, c)));
res->finishAppend();
for (size_t r = 1; r < numRows; ++r) {
for (size_t c = 0; c < numCols; ++c)
res->set(0, c, func(res->get(0, c), static_cast<VTRes>(arg->get(r, c)), ctx));
}
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return;
// The op-code is either MEAN or STDDEV or VAR.
for (size_t c = 0; c < numCols; ++c)
res->set(0, c, res->get(0, c) / numRows);
if (opCode == AggOpCode::MEAN)
return;
std::vector<VTRes> tmp(numCols);
for (size_t r = 0; r < numRows; ++r) {
for (size_t c = 0; c < numCols; ++c) {
VTRes val = static_cast<VTRes>(arg->get(r, c)) - res->get(0, c);
tmp[c] += val * val;
}
}
res->prepareAppend();
for (size_t c = 0; c < numCols; ++c) {
tmp[c] /= numRows;
if (opCode == AggOpCode::STDDEV)
tmp[c] = sqrt(tmp[c]);
res->append(0, c, tmp[c]);
}
res->finishAppend();
}
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_AGGCOL_H