forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggAll.h
More file actions
230 lines (192 loc) · 9.22 KB
/
Copy pathAggAll.h
File metadata and controls
230 lines (192 loc) · 9.22 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
/*
* 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_AGGALL_H
#define SRC_RUNTIME_LOCAL_KERNELS_AGGALL_H
#include <runtime/local/context/DaphneContext.h>
#include <runtime/local/datastructures/CSRMatrix.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 <cmath>
#include <cstddef>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <typename VTRes, class DTArg> struct AggAll {
static VTRes apply(AggOpCode opCode, const DTArg *arg, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <typename VTRes, class DTArg> VTRes aggAll(AggOpCode opCode, const DTArg *arg, DCTX(ctx)) {
return AggAll<VTRes, DTArg>::apply(opCode, arg, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// scalar <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggAll<VTRes, DenseMatrix<VTArg>> {
static VTRes apply(AggOpCode opCode, const DenseMatrix<VTArg> *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
const VTArg *valuesArg = arg->getValues();
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func;
VTRes agg, stddev;
if (AggOpCodeUtils::isPureBinaryReduction(opCode)) {
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
agg = AggOpCodeUtils::template getNeutral<VTRes>(opCode);
} else {
// TODO Setting the function pointer yields the correct result.
// However, since MEAN, VAR, and STDDEV are not sparse-safe, the
// program does not take the same path for doing the summation, and
// is less efficient. for MEAN, VAR, and STDDEV, we need to sum
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
agg = VTRes(0);
}
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++)
agg = func(agg, static_cast<VTRes>(valuesArg[c]), ctx);
valuesArg += arg->getRowSkip();
}
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return agg;
agg /= arg->getNumCols() * arg->getNumRows();
// The op-code is either MEAN or STDDEV or VAR.
if (opCode == AggOpCode::MEAN) {
return agg;
}
// else op-code is STDDEV or VAR
stddev = 0;
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]) - agg;
stddev = stddev + val * val;
}
valuesArg += arg->getRowSkip();
}
stddev /= arg->getNumCols() * arg->getNumRows();
// Variance --> stddev before sqrt() is variance
if (opCode == AggOpCode::VAR) {
VTRes var = stddev;
return var;
}
stddev = sqrt(stddev);
return stddev;
}
};
// ----------------------------------------------------------------------------
// scalar <- CSRMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggAll<VTRes, CSRMatrix<VTArg>> {
static VTRes aggArray(const VTArg *values, size_t numNonZeros, size_t numCells,
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func, bool isSparseSafe, VTRes neutral, DCTX(ctx)) {
if (numNonZeros) {
VTRes agg = static_cast<VTRes>(values[0]);
for (size_t i = 1; i < numNonZeros; i++)
agg = func(agg, static_cast<VTRes>(values[i]), ctx);
if (!isSparseSafe && numNonZeros < numCells)
agg = func(agg, 0, ctx);
return agg;
} else
return func(neutral, 0, ctx);
}
static VTRes apply(AggOpCode opCode, const CSRMatrix<VTArg> *arg, DCTX(ctx)) {
if (AggOpCodeUtils::isPureBinaryReduction(opCode)) {
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func =
getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
return aggArray(arg->getValues(0), arg->getNumNonZeros(), arg->getNumRows() * arg->getNumCols(), func,
AggOpCodeUtils::isSparseSafe(opCode), AggOpCodeUtils::template getNeutral<VTRes>(opCode),
ctx);
} else { // The op-code is either MEAN or STDDEV or VAR.
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func =
getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
auto agg = aggArray(arg->getValues(0), arg->getNumNonZeros(), arg->getNumRows() * arg->getNumCols(), func,
true, VTRes(0), ctx);
agg = agg / (arg->getNumRows() * arg->getNumCols());
if (opCode == AggOpCode::MEAN)
return agg;
else {
// STDDEV-VAR
VTRes stddev = 0;
const VTArg *valuesArg = arg->getValues(0);
for (size_t i = 0; i < arg->getNumNonZeros(); i++) {
VTRes val = static_cast<VTRes>((valuesArg[i])) - agg;
stddev = stddev + val * val;
}
stddev += ((arg->getNumRows() * arg->getNumCols()) - arg->getNumNonZeros()) * agg * agg;
stddev /= (arg->getNumRows() * arg->getNumCols());
// Variance --> stddev before sqrt() is variance
if (opCode == AggOpCode::VAR) {
VTRes var = stddev;
return var;
}
stddev = sqrt(stddev);
return stddev;
}
}
}
};
// ----------------------------------------------------------------------------
// scalar <- Matrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> struct AggAll<VTRes, Matrix<VTArg>> {
static VTRes apply(AggOpCode opCode, const Matrix<VTArg> *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
EwBinaryScaFuncPtr<VTRes, VTRes, VTRes> func;
VTRes agg, stddev;
if (AggOpCodeUtils::isPureBinaryReduction(opCode)) {
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(opCode));
agg = AggOpCodeUtils::template getNeutral<VTRes>(opCode);
} else {
// TODO Setting the function pointer yields the correct result.
// However, since MEAN, VAR, and STDDEV are not sparse-safe, the
// program does not take the same path for doing the summation, and
// is less efficient. for MEAN, VAR, and STDDEV, we need to sum
func = getEwBinaryScaFuncPtr<VTRes, VTRes, VTRes>(AggOpCodeUtils::getBinaryOpCode(AggOpCode::SUM));
agg = VTRes(0);
}
for (size_t r = 0; r < numRows; ++r)
for (size_t c = 0; c < numCols; ++c)
agg = func(agg, static_cast<VTRes>(arg->get(r, c)), ctx);
if (AggOpCodeUtils::isPureBinaryReduction(opCode))
return agg;
agg /= numCols * numRows;
// The op-code is either MEAN or STDDEV or VAR.
if (opCode == AggOpCode::MEAN)
return agg;
// else op-code is STDDEV or VAR
stddev = 0;
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)) - agg;
stddev = stddev + val * val;
}
}
stddev /= numCols * numRows;
// VAR --> stddev before sqrt() is variance
if (opCode == AggOpCode::VAR)
return stddev;
// STDDEV
stddev = sqrt(stddev);
return stddev;
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_AGGALL_H