-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathEwBinaryObjSca.h
157 lines (132 loc) · 6.48 KB
/
EwBinaryObjSca.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
/*
* 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_EWBINARYOBJSCA_H
#define SRC_RUNTIME_LOCAL_KERNELS_EWBINARYOBJSCA_H
#include <runtime/local/context/DaphneContext.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Frame.h>
#include <runtime/local/datastructures/Matrix.h>
#include <runtime/local/kernels/BinaryOpCode.h>
#include <runtime/local/kernels/EwBinarySca.h>
#include <cstddef>
#include <cstring>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTRes, class DTLhs, typename VTRhs> struct EwBinaryObjSca {
static void apply(BinaryOpCode opCode, DTRes *&res, const DTLhs *lhs, VTRhs rhs, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTRes, class DTLhs, typename VTRhs>
void ewBinaryObjSca(BinaryOpCode opCode, DTRes *&res, const DTLhs *lhs, VTRhs rhs, DCTX(ctx)) {
EwBinaryObjSca<DTRes, DTLhs, VTRhs>::apply(opCode, res, lhs, rhs, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix, scalar
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTLhs, typename VTRhs>
struct EwBinaryObjSca<DenseMatrix<VTRes>, DenseMatrix<VTLhs>, VTRhs> {
static void apply(BinaryOpCode opCode, DenseMatrix<VTRes> *&res, const DenseMatrix<VTLhs> *lhs, VTRhs rhs,
DCTX(ctx)) {
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTRes>>(numRows, numCols, false);
const VTLhs *valuesLhs = lhs->getValues();
VTRes *valuesRes = res->getValues();
EwBinaryScaFuncPtr<VTRes, VTLhs, VTRhs> func = getEwBinaryScaFuncPtr<VTRes, VTLhs, VTRhs>(opCode);
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++)
valuesRes[c] = func(valuesLhs[c], rhs, ctx);
valuesLhs += lhs->getRowSkip();
valuesRes += res->getRowSkip();
}
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix, scalar
// ----------------------------------------------------------------------------
template <typename VT> struct EwBinaryObjSca<Matrix<VT>, Matrix<VT>, VT> {
static void apply(BinaryOpCode opCode, Matrix<VT> *&res, const Matrix<VT> *lhs, VT rhs, DCTX(ctx)) {
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
// 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, ctx));
res->finishAppend();
}
};
// ----------------------------------------------------------------------------
// Frame <- Frame, scalar
// ----------------------------------------------------------------------------
template <typename VT>
void ewBinaryFrameColSca(BinaryOpCode opCode, Frame *&res, const Frame *lhs, VT rhs, size_t c, DCTX(ctx)) {
auto *col_res = res->getColumn<VT>(c);
auto *col_lhs = lhs->getColumn<VT>(c);
ewBinaryObjSca<DenseMatrix<VT>, DenseMatrix<VT>, VT>(opCode, col_res, col_lhs, rhs, ctx);
}
template <typename VT> struct EwBinaryObjSca<Frame, Frame, VT> {
static void apply(BinaryOpCode opCode, Frame *&res, const Frame *lhs, VT rhs, DCTX(ctx)) {
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (res == nullptr)
res = DataObjectFactory::create<Frame>(numRows, numCols, lhs->getSchema(), lhs->getLabels(), false);
for (size_t c = 0; c < numCols; c++) {
switch (lhs->getColumnType(c)) {
// For all value types:
case ValueTypeCode::F64:
ewBinaryFrameColSca<double>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::F32:
ewBinaryFrameColSca<float>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::SI64:
ewBinaryFrameColSca<int64_t>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::SI32:
ewBinaryFrameColSca<int32_t>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::SI8:
ewBinaryFrameColSca<int8_t>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::UI64:
ewBinaryFrameColSca<uint64_t>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::UI32:
ewBinaryFrameColSca<uint32_t>(opCode, res, lhs, rhs, c, ctx);
break;
case ValueTypeCode::UI8:
ewBinaryFrameColSca<uint8_t>(opCode, res, lhs, rhs, c, ctx);
break;
default:
throw std::runtime_error("EwBinaryObjSca::apply: unknown value type code");
}
}
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_EWBINARYOBJSCA_H