forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggRow.h
More file actions
326 lines (283 loc) · 13.7 KB
/
Copy pathAggRow.h
File metadata and controls
326 lines (283 loc) · 13.7 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
/*
* 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_AGGROW_H
#define SRC_RUNTIME_LOCAL_KERNELS_AGGROW_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/AggAll.h>
#include <runtime/local/kernels/AggOpCode.h>
#include <runtime/local/kernels/EwBinarySca.h>
#include <vector>
#include <cmath>
#include <cstddef>
#include <cstring>
#include <typeinfo>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTRes, class DTArg> struct AggRow {
static void apply(AggOpCode opCode, DTRes *&res, const DTArg *arg, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTRes, class DTArg> void aggRow(AggOpCode opCode, DTRes *&res, const DTArg *arg, DCTX(ctx)) {
AggRow<DTRes, DTArg>::apply(opCode, res, arg, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggRow<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>>(numRows, 1, false);
const VTArg *valuesArg = arg->getValues();
VTRes *valuesRes = res->getValues();
if (opCode == AggOpCode::IDXMIN) {
for (size_t r = 0; r < numRows; r++) {
VTArg minVal = valuesArg[0];
size_t minValIdx = 0;
for (size_t c = 1; c < numCols; c++)
if (valuesArg[c] < minVal) {
minVal = valuesArg[c];
minValIdx = c;
}
*valuesRes = static_cast<VTRes>(minValIdx);
valuesArg += arg->getRowSkip();
valuesRes += res->getRowSkip();
}
} else if (opCode == AggOpCode::IDXMAX) {
for (size_t r = 0; r < numRows; r++) {
VTArg maxVal = valuesArg[0];
size_t maxValIdx = 0;
for (size_t c = 1; c < numCols; c++)
if (valuesArg[c] > maxVal) {
maxVal = valuesArg[c];
maxValIdx = c;
}
*valuesRes = static_cast<VTRes>(maxValIdx);
valuesArg += arg->getRowSkip();
valuesRes += res->getRowSkip();
}
} 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));
for (size_t r = 0; r < numRows; r++) {
VTRes agg = static_cast<VTRes>(*valuesArg);
for (size_t c = 1; c < numCols; c++) {
agg = func(agg, static_cast<VTRes>(valuesArg[c]), ctx);
}
*valuesRes = static_cast<VTRes>(agg);
valuesArg += arg->getRowSkip();
valuesRes += res->getRowSkip();
}
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return;
// The op-code is either MEAN or STDDEV or VAR
valuesRes = res->getValues();
// valuesArg = arg->getValues();
for (size_t r = 0; r < numRows; r++) {
*valuesRes = (*valuesRes) / numCols;
valuesRes += res->getRowSkip();
}
if (opCode == AggOpCode::MEAN)
return;
// else op-code is STDDEV or VAR
// Create a temporary matrix to store the resulting standard
// deviations for each row
auto tmp = DataObjectFactory::create<DenseMatrix<VTRes>>(numRows, 1, true);
VTRes *valuesT = tmp->getValues();
valuesArg = arg->getValues();
valuesRes = res->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);
valuesT[r] = valuesT[r] + val * val;
}
valuesArg += arg->getRowSkip();
valuesRes += res->getRowSkip();
}
valuesRes = res->getValues();
for (size_t c = 0; c < numRows; c++) {
valuesT[c] /= numCols;
if (opCode == AggOpCode::STDDEV)
*valuesRes = sqrt(valuesT[c]);
else
*valuesRes = valuesT[c];
valuesRes += res->getRowSkip();
}
DataObjectFactory::destroy<DenseMatrix<VTRes>>(tmp);
}
}
};
// ----------------------------------------------------------------------------
// DenseMatrix <- CSRMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggRow<DenseMatrix<VTRes>, CSRMatrix<VTArg>> {
static void apply(AggOpCode opCode, DenseMatrix<VTRes> *&res, const CSRMatrix<VTArg> *arg, DCTX(ctx)) {
const size_t numCols = arg->getNumCols();
const size_t numRows = arg->getNumRows();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTRes>>(numRows, 1, false);
VTRes *valuesRes = res->getValues();
if (AggOpCodeUtils::isPureBinaryReduction(opCode)) {
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func =
getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
const bool isSparseSafe = AggOpCodeUtils::isSparseSafe(opCode);
const VTRes neutral = AggOpCodeUtils::template getNeutral<VTRes>(opCode);
for (size_t r = 0; r < numRows; r++) {
*valuesRes = AggAll<VTRes, CSRMatrix<VTArg>>::aggArray(arg->getValues(r), arg->getNumNonZeros(r),
numCols, func, isSparseSafe, neutral, ctx);
valuesRes += res->getRowSkip();
}
} else { // The op-code is either MEAN or STDDEV or VAR
// get sum for each row
size_t ctr = 0;
const VTRes neutral = VTRes(0);
const bool isSparseSafe = true;
auto tmp = DataObjectFactory::create<DenseMatrix<VTRes>>(numRows, 1, true);
VTRes *valuesT = tmp->getValues();
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func =
getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
for (size_t r = 0; r < numRows; r++) {
*valuesRes = AggAll<VTRes, CSRMatrix<VTArg>>::aggArray(arg->getValues(r), arg->getNumNonZeros(r),
numCols, func, isSparseSafe, neutral, ctx);
const VTArg *valuesArg = arg->getValues(0);
const size_t numNonZeros = arg->getNumNonZeros(r);
*valuesRes = *valuesRes / numCols;
if (opCode != AggOpCode::MEAN) {
for (size_t i = ctr; i < ctr + numNonZeros; i++) {
VTRes val = static_cast<VTRes>((valuesArg[i])) - (*valuesRes);
valuesT[r] = valuesT[r] + val * val;
}
ctr += numNonZeros;
valuesT[r] += (numCols - numNonZeros) * (*valuesRes) * (*valuesRes);
valuesT[r] /= numCols;
if (opCode == AggOpCode::STDDEV)
*valuesRes = sqrt(valuesT[r]);
else
*valuesRes = valuesT[r];
}
valuesRes += res->getRowSkip();
}
valuesRes = res->getValues();
DataObjectFactory::destroy<DenseMatrix<VTRes>>(tmp);
}
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggRow<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>>(numRows, 1, false);
if (opCode == AggOpCode::IDXMIN) {
res->prepareAppend();
for (size_t r = 0; r < numRows; ++r) {
VTArg minVal = arg->get(r, 0);
size_t minValIdx = 0;
for (size_t c = 1; c < numCols; ++c) {
VTArg argVal = arg->get(r, c);
if (argVal < minVal) {
minVal = argVal;
minValIdx = c;
}
}
res->append(r, 0, static_cast<VTRes>(minValIdx));
}
res->finishAppend();
} else if (opCode == AggOpCode::IDXMAX) {
res->prepareAppend();
for (size_t r = 0; r < numRows; ++r) {
VTArg maxVal = arg->get(r, 0);
size_t maxValIdx = 0;
for (size_t c = 1; c < numCols; ++c) {
VTArg argVal = arg->get(r, c);
if (argVal > maxVal) {
maxVal = argVal;
maxValIdx = c;
}
}
res->append(r, 0, static_cast<VTRes>(maxValIdx));
}
res->finishAppend();
} 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 r = 0; r < numRows; ++r) {
VTRes agg = static_cast<VTRes>(arg->get(r, 0));
for (size_t c = 1; c < numCols; ++c)
agg = func(agg, static_cast<VTRes>(arg->get(r, c)), ctx);
res->append(r, 0, static_cast<VTRes>(agg));
}
res->finishAppend();
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return;
// The op-code is either MEAN or STDDEV or VAR
for (size_t r = 0; r < numRows; ++r) {
res->set(r, 0, res->get(r, 0) / numCols);
}
if (opCode == AggOpCode::MEAN)
return;
// else op-code is STDDEV or VAR
// Create a temporary matrix to store the resulting standard
// deviations for each row
std::vector<VTRes> tmp(numRows);
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(r, 0);
tmp[r] += val * val;
}
}
res->prepareAppend();
for (size_t r = 0; r < numRows; ++r) {
tmp[r] /= numCols;
if (opCode == AggOpCode::STDDEV)
res->append(r, 0, sqrt(tmp[r]));
else
res->append(r, 0, tmp[r]);
}
res->finishAppend();
}
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_AGGROW_H