forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrame.h
More file actions
506 lines (453 loc) · 19.4 KB
/
Copy pathFrame.h
File metadata and controls
506 lines (453 loc) · 19.4 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
* 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/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Structure.h>
#include <runtime/local/datastructures/ValueTypeCode.h>
#include <runtime/local/datastructures/ValueTypeUtils.h>
#include <iostream>
#include <memory>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include <cinttypes>
#include <cstddef>
#include <cstring>
/**
* @brief A data structure with an individual value type per column.
*
* A `Frame` is organized in column-major fashion and is backed by an
* individual dense array for each column.
*/
class Frame : public Structure {
// Grant DataObjectFactory access to the private constructors and
// destructors.
template<class DataType, typename ... ArgTypes>
friend DataType * DataObjectFactory::create(ArgTypes ...);
template<class DataType>
friend void DataObjectFactory::destroy(const DataType * obj);
/**
* @brief An array of length `numCols` of the value types of the columns of
* this frame.
*
* Note that the schema is not encoded as template parameters since this
* would lead to an explosion of frame types to be compiled.
*/
ValueTypeCode * schema;
/**
* @brief An array of length `numCols` of the names of the columns of this
* frame.
*/
std::string * labels;
/**
* @brief A mapping from a column's label to its position in the frame.
*/
std::unordered_map<std::string, size_t> labels2idxs;
/**
* @brief The common pointer type used for the array of each column,
* irrespective of the actual value type of the column.
*
* Each column can have its own value type, as determined by the `schema`.
* However, we cannot declare the column pointers of individual types,
* since we want to store them in one array. Thus, we use a common pointer
* type for all of them, internally.
*
* Using `uint8_t` is advantageous, since `sizeof(uint8_t) == 1`, which
* simplifies the computation of physical sizes.
*/
using ColByteType = uint8_t;
/**
* @brief An array of length `numCols` of the column arrays of this frame.
*/
std::shared_ptr<ColByteType> * columns;
/**
* @brief Initializes the mapping from column labels to column positions in
* the frame and checks for duplicate column labels.
*
* This method should be called by each constructor, after the column
* labels have been initialized.
*/
void initLabels2Idxs() {
labels2idxs.clear();
for(size_t i = 0; i < numCols; i++) {
if(labels2idxs.count(labels[i]))
throw std::runtime_error(
"a frame's column labels must be unique, but '" +
labels[i] + "' occurs more than once"
);
labels2idxs[labels[i]] = i;
}
}
/**
* @brief Initializes the mapping from column labels to column positions in
* the frame and assigns default labels to duplicate column labels.
*
* This method should only be called by constructors, that may intentionally duplicate
* columns, instead of initLabels2Idxs(), after the column labels have been initialized.
*/
void initDeduplicatedLabels2Idxs() {
labels2idxs.clear();
for(size_t i = 0; i < numCols; i++) {
if(labels2idxs.count(labels[i]))
labels[i] = getDefaultLabel(i);
labels2idxs[labels[i]] = i;
}
}
// TODO Should the given schema array really be copied, or reused?
/**
* @brief Creates a `Frame` and allocates enough memory for the specified
* size.
*
* @param maxNumRows The maximum number of rows.
* @param numCols The exact number of columns.
* @param schema An array of length `numCols` of the value types of the
* individual columns. The given array will be copied.
* @param zero Whether the allocated memory of the internal column arrays
* shall be initialized to zeros (`true`), or be left uninitialized
* (`false`).
*/
Frame(size_t maxNumRows, size_t numCols, const ValueTypeCode * schema, const std::string * labels, bool zero) :
Structure(maxNumRows, numCols),
schema(new ValueTypeCode[numCols]),
labels(new std::string[numCols]),
columns(new std::shared_ptr<ColByteType>[numCols])
{
for(size_t i = 0; i < numCols; i++) {
this->schema[i] = schema[i];
this->labels[i] = labels ? labels[i] : getDefaultLabel(i);
const size_t sizeAlloc = maxNumRows * ValueTypeUtils::sizeOf(schema[i]);
this->columns[i] = std::shared_ptr<ColByteType>(new ColByteType[sizeAlloc],
std::default_delete<ColByteType []>());
if(zero)
memset(this->columns[i].get(), 0, sizeAlloc);
}
initLabels2Idxs();
}
Frame(const Frame * lhs, const Frame * rhs) :
Structure(lhs->getNumRows(), lhs->getNumCols() + rhs->getNumCols())
{
if(lhs->getNumRows() != rhs->getNumRows())
throw std::runtime_error(
"both input frames must have the same number of rows"
);
schema = new ValueTypeCode[numCols];
labels = new std::string[numCols];
columns = new std::shared_ptr<ColByteType>[numCols];
const size_t numColsLhs = lhs->getNumCols();
const size_t numColsRhs = rhs->getNumCols();
for(size_t i = 0; i < numColsLhs; i++) {
schema [i] = lhs->schema[i];
labels [i] = lhs->labels[i];
columns[i] = std::shared_ptr<ColByteType>(lhs->columns[i]);
}
for(size_t i = 0; i < numColsRhs; i++) {
schema [numColsLhs + i] = rhs->schema[i];
labels [numColsLhs + i] = rhs->labels[i];
columns[numColsLhs + i] = std::shared_ptr<ColByteType>(rhs->columns[i]);
}
initLabels2Idxs();
}
template<typename VT>
bool tryValueType(Structure * colMat, ValueTypeCode * schemaSlot, std::shared_ptr<ColByteType> * columnsSlot) {
if(auto colMat2 = dynamic_cast<DenseMatrix<VT> *>(colMat)) {
if (colMat2->getRowSkip() != 1)
throw std::runtime_error("Frame (tryValueType): all given matrices must not be a view of a column of a larger matrix");
*schemaSlot = ValueTypeUtils::codeFor<VT>;
std::shared_ptr<VT[]> orig = colMat2->getValuesSharedPtr();
*columnsSlot = std::shared_ptr<ColByteType>(orig, reinterpret_cast<ColByteType *>(orig.get()));
return true;
}
return false;
}
/**
* @brief Creates a `Frame` with the given single-column matrices as its
* columns.
*
* The schema of the frame is automatically determined based on the value
* types of the given matrices.
*
* The data arrays are shared with the given matrices, i.e., no copying is
* performed.
*
* @param colMats A `std::vector` of single-column matrices. These must be
* `DenseMatrix`s of any value type (the type `Structure` is used here only
* to not depend on a template parameter for the value type). Furthermore,
* these matrices must not be views on a single column of a larger matrix.
*/
Frame(const std::vector<Structure *>& colMats, const std::string * labels) :
Structure(colMats.empty() ? 0 : colMats[0]->getNumRows(), colMats.size())
{
const size_t numCols = colMats.size();
if (numCols == 0)
throw std::runtime_error("Frame: at least one column matrix must be provided");
schema = new ValueTypeCode[numCols];
this->labels = new std::string[numCols];
columns = new std::shared_ptr<ColByteType>[numCols];
for(size_t c = 0; c < numCols; c++) {
Structure * colMat = colMats[c];
if (colMat->getNumCols() != 1)
throw std::runtime_error("Frame: all given matrices must be column matrices");
if (colMat->getNumRows() != numRows)
throw std::runtime_error("Frame: all given matrices must have the same number of rows");
this->labels[c] = labels ? labels[c] : getDefaultLabel(c);
// For all value types.
bool found = tryValueType<int8_t>(colMat, schema + c, columns + c);
found = found || tryValueType<int32_t>(colMat, schema + c, columns + c);
found = found || tryValueType<int64_t>(colMat, schema + c, columns + c);
found = found || tryValueType<uint8_t> (colMat, schema + c, columns + c);
found = found || tryValueType<uint32_t>(colMat, schema + c, columns + c);
found = found || tryValueType<uint64_t>(colMat, schema + c, columns + c);
found = found || tryValueType<float> (colMat, schema + c, columns + c);
found = found || tryValueType<double>(colMat, schema + c, columns + c);
if(!found)
throw std::runtime_error("unsupported value type");
}
initLabels2Idxs();
}
/**
* @brief Creates a `Frame` around a sub-frame of another `Frame` without
* copying the data.
*
* @param src The other frame.
* @param rowLowerIncl Inclusive lower bound for the range of rows to extract.
* @param rowUpperIncl Exclusive upper bound for the range of rows to extract.
* @param numCols The number of columns to extract.
* @param colIdxs An array of length `numCols` of the indexes of the
* columns to extract from `src`.
*/
Frame(const Frame * src, int64_t rowLowerIncl, int64_t rowUpperExcl, size_t numCols, const size_t * colIdxs) :
Structure(rowUpperExcl - rowLowerIncl, numCols)
{
if (src == nullptr)
throw std::runtime_error("invalid argument passed to frame constructor: src must not be null");
if (rowLowerIncl < 0 || rowUpperExcl < rowLowerIncl || static_cast<ssize_t>(src->numRows) < rowUpperExcl
|| (rowLowerIncl == static_cast<ssize_t>(src->numRows) && rowLowerIncl != 0)) {
std::ostringstream errMsg;
errMsg << "invalid arguments '" << rowLowerIncl << ", " << rowUpperExcl
<< "' passed to frame constructor: it must hold 0 <= rowLowerIncl <= rowUpperExcl <= #rows "
<< "and rowLowerIncl < #rows (unless both are zero) where #rows of src is '" << src->numRows << "'";
throw std::out_of_range(errMsg.str());
}
size_t numColsSrc = src->numCols;
for(size_t i = 0; i < numCols; i++) {
if (numColsSrc <= colIdxs[i]) {
std::ostringstream errMsg;
errMsg << "invalid argument '" << colIdxs[i] << "' passed to frame constructor: "
"colIdx is out of bounds for frame with column boundaries '[0, " << numColsSrc << ")'";
throw std::out_of_range(errMsg.str());
}
}
this->schema = new ValueTypeCode[numCols];
this->labels = new std::string[numCols];
this->columns = new std::shared_ptr<ColByteType>[numCols];
for(size_t i = 0; i < numCols; i++) {
this->schema[i] = src->schema[colIdxs[i]];
this->labels[i] = src->labels[colIdxs[i]];
this->columns[i] = std::shared_ptr<ColByteType>(
src->columns[colIdxs[i]],
src->columns[colIdxs[i]].get() + rowLowerIncl * ValueTypeUtils::sizeOf(schema[i])
);
}
initDeduplicatedLabels2Idxs();
}
~Frame() override {
delete[] schema;
delete[] labels;
delete[] columns;
}
public:
/**
* @brief Returns the default label to use for the pos-th column, if no
* column label was specified.
* @param pos The position of the column in the frame (starting at zero).
* @return The default label for the pos-th column.
*/
static std::string getDefaultLabel(size_t pos) {
return "col_" + std::to_string(pos);
}
void shrinkNumRows(size_t numRows) {
// TODO Here we could reduce the allocated size of the column arrays.
this->numRows = numRows;
}
const ValueTypeCode * getSchema() const {
return schema;
}
const std::string * getLabels() const {
return labels;
}
void setLabels(const std::string * newLabels) {
for(size_t i = 0; i < numCols; i++)
labels[i] = newLabels[i];
initLabels2Idxs();
}
size_t getColumnIdx(const std::string & label) const {
auto it = labels2idxs.find(label);
if(it != labels2idxs.end())
return it->second;
throw std::runtime_error("column label not found: '" + label + "'");
}
ValueTypeCode getColumnType(size_t idx) const {
if (idx >= numCols)
throw std::runtime_error("Frame (getColumnType): column index is out of bounds");
return schema[idx];
}
ValueTypeCode getColumnType(const std::string & label) const {
return getColumnType(getColumnIdx(label));
}
template<typename ValueType>
DenseMatrix<ValueType> * getColumn(size_t idx) {
if (ValueTypeUtils::codeFor<ValueType> != schema[idx])
throw std::runtime_error("Frame (getColumn): requested value type must match the type of the column");
return DataObjectFactory::create<DenseMatrix<ValueType>>(
numRows, 1,
std::shared_ptr<ValueType[]>(
columns[idx],
reinterpret_cast<ValueType *>(columns[idx].get())
)
);
}
template<typename ValueType>
const DenseMatrix<ValueType> * getColumn(size_t idx) const {
return const_cast<Frame *>(this)->getColumn<ValueType>(idx);
}
template<typename ValueType>
DenseMatrix<ValueType> * getColumn(const std::string & label) {
return getColumn<ValueType>(getColumnIdx(label));
}
template<typename ValueType>
const DenseMatrix<ValueType> * getColumn(const std::string & label) const {
return const_cast<Frame *>(this)->getColumn<ValueType>(label);
}
void * getColumnRaw(size_t idx) {
return columns[idx].get();
}
const void * getColumnRaw(size_t idx) const {
return const_cast<Frame *>(this)->getColumnRaw(idx);
}
size_t getNumDims() const override {
return 2;
}
size_t getNumItems() const override {
return this->numRows * this->numCols;
}
void print(std::ostream & os) const override {
os << "Frame(" << numRows << 'x' << numCols << ", [";
for(size_t c = 0; c < numCols; c++) {
// TODO Ideally, special characters in the labels should be
// escaped.
os << labels[c] << ':';
os << ValueTypeUtils::cppNameForCode(schema[c]);
if(c < numCols - 1)
os << ", ";
}
os << "])" << std::endl;
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++) {
ValueTypeUtils::printValue(os, schema[c], columns[c].get(), r);
if (c < numCols - 1)
os << ' ';
}
os << std::endl;
}
}
Frame* sliceRow(size_t rl, size_t ru) const override {
return slice(rl, ru, 0, numCols);
}
Frame* sliceCol(size_t cl, size_t cu) const override {
return slice(0, numRows, cl, cu);
}
Frame* slice(size_t rl, size_t ru, size_t cl, size_t cu) const override {
if(cl > cu)
throw std::runtime_error("Frame::slice(): cl must not be greater than cu");
size_t * colIdxs = new size_t[cu-cl];
size_t i = 0;
for(size_t c = cl; c < cu; c++, i++)
colIdxs[i] = c;
auto res = DataObjectFactory::create<Frame>(this, rl, ru, cu-cl, colIdxs);
delete[] colIdxs;
return res;
}
size_t serialize(std::vector<char> &buf) const override;
bool operator==(const Frame & rhs) const {
if(this == &rhs)
return true;
const size_t numRows = this->getNumRows();
const size_t numCols = this->getNumCols();
if(numRows != rhs.getNumRows() || numCols != rhs.getNumCols())
return false;
if(memcmp(this->getSchema(), rhs.getSchema(), numCols * sizeof(ValueTypeCode)))
return false;
const std::string * labelsLhs = this->getLabels();
const std::string * labelsRhs = rhs.getLabels();
for (size_t c = 0; c < numCols; c++) {
if(labelsLhs[c] != labelsRhs[c])
return false;
}
for (size_t c = 0; c < numCols; c++)
{
switch(this->getColumnType(c)) {
// For all value types:
case ValueTypeCode::F64:
if (!(*(this->getColumn<double>(c)) == *(rhs.getColumn<double>(c)))) {
return false;
}
break;
case ValueTypeCode::F32:
if (!(*(this->getColumn<float>(c)) == *(rhs.getColumn<float>(c)))) {
return false;
}
break;
case ValueTypeCode::SI64:
if (!(*(this->getColumn<int64_t>(c)) == *(rhs.getColumn<int64_t>(c)))) {
return false;
}
break;
case ValueTypeCode::SI32:
if (!(*(this->getColumn<int32_t>(c)) == *(rhs.getColumn<int32_t>(c)))) {
return false;
}
break;
case ValueTypeCode::SI8 :
if (!(*(this->getColumn<int8_t>(c)) == *(rhs.getColumn<int8_t>(c)))) {
return false;
}
break;
case ValueTypeCode::UI64:
if (!(*(this->getColumn<uint64_t>(c)) == *(rhs.getColumn<uint64_t>(c)))) {
return false;
}
break;
case ValueTypeCode::UI32:
if (!(*(this->getColumn<uint32_t>(c)) == *(rhs.getColumn<uint32_t>(c)))) {
return false;
}
break;
case ValueTypeCode::UI8 :
if (!(*(this->getColumn<uint8_t>(c)) == *(rhs.getColumn<uint8_t>(c)))) {
return false;
}
break;
default:
throw std::runtime_error("CheckEq::apply: unknown value type code");
}
}
return true;
}
};
std::ostream & operator<<(std::ostream & os, const Frame & obj);