forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCastObj.h
More file actions
408 lines (353 loc) · 16.6 KB
/
Copy pathCastObj.h
File metadata and controls
408 lines (353 loc) · 16.6 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* 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/Column.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Frame.h>
#include <runtime/local/datastructures/ValueTypeCode.h>
#include <runtime/local/datastructures/ValueTypeUtils.h>
#include <runtime/local/kernels/CastSca.h>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTRes, class DTArg> struct CastObj {
static void apply(DTRes *&res, const DTArg *arg, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
/**
* @brief Performs a cast of the given data object to another type.
*
* @param arg The data object to cast.
* @return The casted data object.
*/
template <class DTRes, class DTArg> void castObj(DTRes *&res, const DTArg *arg, DCTX(ctx)) {
CastObj<DTRes, DTArg>::apply(res, arg, ctx);
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- Frame
// ----------------------------------------------------------------------------
template <typename VTRes> class CastObj<DenseMatrix<VTRes>, Frame> {
/**
* @brief Casts the values of the input column at index `c` and stores the
* casted values to column `c` in the output matrix.
* @param res The output matrix.
* @param argFrm The input frame.
* @param c The position of the column to cast.
*/
template <typename VTArg> static void castCol(DenseMatrix<VTRes> *res, const Frame *argFrm, size_t c) {
const size_t numRows = argFrm->getNumRows();
const DenseMatrix<VTArg> *argCol = argFrm->getColumn<VTArg>(c);
for (size_t r = 0; r < numRows; r++)
res->set(r, c, castSca<VTRes, VTArg>(argCol->get(r, 0), nullptr));
DataObjectFactory::destroy(argCol);
}
public:
static void apply(DenseMatrix<VTRes> *&res, const Frame *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
if (numCols == 1 && arg->getColumnType(0) == ValueTypeUtils::codeFor<VTRes>) {
// The input frame has a single column of the result's value type.
// Zero-cost cast from frame to matrix.
// TODO This case could even be used for (un)signed integers of the
// same width, involving a reinterpret cast of the pointers.
// TODO Can we avoid this const_cast?
res = const_cast<DenseMatrix<VTRes> *>(arg->getColumn<VTRes>(0));
} else {
// The input frame has multiple columns and/or other value types
// than the result.
// Need to change column-major to row-major layout and/or cast the
// individual values.
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTRes>>(numRows, numCols, false);
// TODO We could run over the rows in blocks for cache efficiency.
for (size_t c = 0; c < numCols; c++) {
// TODO We do not really need all cases.
// - All pairs of the same type can be handled by a single
// copy-the-column helper function.
// - All pairs of (un)signed integer types of the same width
// as well.
// - Truncating integers to a narrower type does not need to
// consider (un)signedness either.
// - ...
switch (arg->getColumnType(c)) {
// For all value types:
case ValueTypeCode::F64:
castCol<double>(res, arg, c);
break;
case ValueTypeCode::F32:
castCol<float>(res, arg, c);
break;
case ValueTypeCode::SI64:
castCol<int64_t>(res, arg, c);
break;
case ValueTypeCode::SI32:
castCol<int32_t>(res, arg, c);
break;
case ValueTypeCode::SI8:
castCol<int8_t>(res, arg, c);
break;
case ValueTypeCode::UI64:
castCol<uint64_t>(res, arg, c);
break;
case ValueTypeCode::UI32:
castCol<uint32_t>(res, arg, c);
break;
case ValueTypeCode::UI8:
castCol<uint8_t>(res, arg, c);
break;
case ValueTypeCode::STR:
castCol<std::string>(res, arg, c);
break;
default:
throw std::runtime_error("CastObj::apply: unknown value type code");
}
}
}
}
};
// ----------------------------------------------------------------------------
// Frame <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTArg> class CastObj<Frame, DenseMatrix<VTArg>> {
public:
static void apply(Frame *&res, const DenseMatrix<VTArg> *arg, DCTX(ctx)) {
const size_t numCols = arg->getNumCols();
const size_t numRows = arg->getNumRows();
std::vector<Structure *> cols;
if (numCols == 1 && arg->getRowSkip() == 1) {
// The input matrix has a single column and is not a view into a
// column range of another matrix, so it can be reused as the
// column matrix of the output frame.
// Cheap/Low-cost cast from dense matrix to frame.
cols.push_back(const_cast<DenseMatrix<VTArg> *>(arg));
} else {
// The input matrix has multiple columns.
// Need to change row-major to column-major layout and
// split matrix into single column matrices.
for (size_t c = 0; c < numCols; c++) {
auto *colMatrix = DataObjectFactory::create<DenseMatrix<VTArg>>(numRows, 1, false);
for (size_t r = 0; r < numRows; r++)
colMatrix->set(r, 0, arg->get(r, c));
cols.push_back(colMatrix);
}
}
res = DataObjectFactory::create<Frame>(cols, nullptr);
}
};
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> class CastObj<DenseMatrix<VTRes>, DenseMatrix<VTArg>> {
public:
static void apply(DenseMatrix<VTRes> *&res, const DenseMatrix<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, numCols, false);
auto resVals = res->getValues();
auto argVals = arg->getValues();
if (arg->getRowSkip() == numCols && res->getRowSkip() == numCols)
// Since DenseMatrix implementation is backed by
// a single dense array of values, we can simply
// perform cast in one loop over that array.
for (size_t idx = 0; idx < numCols * numRows; idx++)
resVals[idx] = castSca<VTRes, VTArg>(argVals[idx], ctx);
else
// res and arg might be views into a larger DenseMatrix.
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++)
resVals[c] = castSca<VTRes, VTArg>(argVals[c], ctx);
resVals += res->getRowSkip();
argVals += arg->getRowSkip();
}
}
};
// ----------------------------------------------------------------------------
// DenseMatrix <- CSRMatrix
// ----------------------------------------------------------------------------
template <typename VT> class CastObj<DenseMatrix<VT>, CSRMatrix<VT>> {
public:
static void apply(DenseMatrix<VT> *&res, const CSRMatrix<VT> *arg, DCTX(ctx)) {
const size_t numCols = arg->getNumCols();
const size_t numRows = arg->getNumRows();
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRows, numCols, false);
// TODO This could be done more efficiently by avoiding the get()/set()
// calls (use append() or direct access to the underlying arrays).
VT temp;
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++) {
temp = arg->get(r, c);
res->set(r, c, temp);
}
}
}
};
// ----------------------------------------------------------------------------
// CSRMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VT> class CastObj<CSRMatrix<VT>, DenseMatrix<VT>> {
public:
static void apply(CSRMatrix<VT> *&res, const DenseMatrix<VT> *arg, DCTX(ctx)) {
const size_t numCols = arg->getNumCols();
const size_t numRows = arg->getNumRows();
size_t numNonZeros = 0;
VT temp;
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++) {
temp = arg->get(r, c);
if (temp != 0)
numNonZeros++;
}
}
if (res == nullptr)
res = DataObjectFactory::create<CSRMatrix<VT>>(numRows, numCols, numNonZeros, true);
// TODO This could be done more efficiently by avoiding the get()/set()
// calls (use append() or direct access to the underlying arrays, then
// we could even avoid initializing the output CSRMatrix).
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++) {
temp = arg->get(r, c);
res->set(r, c, temp);
}
}
}
};
// ----------------------------------------------------------------------------
// CSRMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTres, typename VTarg> class CastObj<CSRMatrix<VTres>, CSRMatrix<VTarg>> {
public:
static void apply(CSRMatrix<VTres> *&res, const CSRMatrix<VTarg> *arg, DCTX(ctx)) {
if (res == nullptr)
res = DataObjectFactory::create<CSRMatrix<VTres>>(arg->getNumCols(), arg->getNumRows(),
arg->getNumNonZeros(), true);
auto res_val = res->getValues();
auto res_cidx = res->getColIdxs();
auto res_roff = res->getRowOffsets();
auto arg_val = arg->getValues();
auto arg_cidx = arg->getColIdxs();
auto arg_roff = arg->getRowOffsets();
for (size_t nz = 0; nz < arg->getNumNonZeros(); nz++) {
res_val[nz] = static_cast<VTres>(arg_val[nz]);
res_cidx[nz] = arg_cidx[nz];
}
for (size_t r = 0; r < arg->getNumRows() + 1; r++) {
res_roff[r] = arg_roff[r];
}
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix
// ----------------------------------------------------------------------------
template <typename VTRes, typename VTArg> class CastObj<Matrix<VTRes>, Matrix<VTArg>> {
public:
static void apply(Matrix<VTRes> *&res, const Matrix<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, numCols, false);
res->prepareAppend();
for (size_t r = 0; r < numRows; ++r)
for (size_t c = 0; c < numCols; ++c)
res->append(r, c, static_cast<VTRes>(arg->get(r, c)));
res->finishAppend();
}
};
// ----------------------------------------------------------------------------
// Column <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VT> class CastObj<Column<VT>, DenseMatrix<VT>> {
public:
static void apply(Column<VT> *&res, const DenseMatrix<VT> *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
if (numCols == 1) {
// The input matrix has a single column.
const size_t rowSkipArg = arg->getRowSkip();
if (rowSkipArg == 1) {
// The input's single column is stored contiguously.
// Reuse the input's memory for the result (zero-copy).
res = DataObjectFactory::create<Column<VT>>(numRows, arg->getValuesSharedPtr());
} else {
// The input's single column is not stored contiguosly.
// Copy the input data to the result.
res = DataObjectFactory::create<Column<VT>>(numRows, false);
const VT *valuesArg = arg->getValues();
VT *valuesRes = res->getValues();
for (size_t r = 0; r < numRows; r++) {
valuesRes[r] = *valuesArg;
valuesArg += rowSkipArg;
}
}
} else {
// The input matrix has zero or multiple columns.
throw std::runtime_error("CastObj::apply: cannot cast a matrix with zero or mutliple columns to Column");
}
}
};
// ----------------------------------------------------------------------------
// DenseMatrix <- Column
// ----------------------------------------------------------------------------
template <typename VT> class CastObj<DenseMatrix<VT>, Column<VT>> {
public:
static void apply(DenseMatrix<VT> *&res, const Column<VT> *arg, DCTX(ctx)) {
res = DataObjectFactory::create<DenseMatrix<VT>>(arg->getNumRows(), 1, arg->getValuesSharedPtr());
}
};
// ----------------------------------------------------------------------------
// Column <- Frame
// ----------------------------------------------------------------------------
template <typename VT> class CastObj<Column<VT>, Frame> {
public:
static void apply(Column<VT> *&res, const Frame *arg, DCTX(ctx)) {
const size_t numRows = arg->getNumRows();
const size_t numCols = arg->getNumCols();
if (numCols == 1 && arg->getColumnType(0) == ValueTypeUtils::codeFor<VT>) {
// The input frame has a single column of the result's value type.
// Zero-cost cast from frame to Column.
// TODO This case could even be used for (un)signed integers of the
// same width, involving a reinterpret cast of the pointers.
// TODO Can we avoid this const_cast?
res = DataObjectFactory::create<Column<VT>>(numRows, arg->getColumn<VT>(0)->getValuesSharedPtr());
} else {
// The input frame has multiple columns and/or other value types
// than the result.
throw std::runtime_error("CastObj::apply: cannot cast Frame with mutliple columns to Column");
}
}
};
// ----------------------------------------------------------------------------
// Frame <- Column
// ----------------------------------------------------------------------------
template <typename VT> class CastObj<Frame, Column<VT>> {
public:
static void apply(Frame *&res, const Column<VT> *arg, DCTX(ctx)) {
std::vector<Structure *> colMats;
DenseMatrix<VT> *argMat = nullptr;
castObj<DenseMatrix<VT>>(argMat, arg, ctx);
colMats.push_back(argMat);
res = DataObjectFactory::create<Frame>(colMats, nullptr);
}
};