forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertCol.h
More file actions
166 lines (138 loc) · 7.46 KB
/
Copy pathInsertCol.h
File metadata and controls
166 lines (138 loc) · 7.46 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
/*
* 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_INSERTCOL_H
#define SRC_RUNTIME_LOCAL_KERNELS_INSERTCOL_H
#include <runtime/local/context/DaphneContext.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Matrix.h>
#include <sstream>
#include <stdexcept>
#include <cstddef>
#include <cstring>
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DTArg, class DTIns, typename VTSel> struct InsertCol {
static void apply(DTArg *&res, const DTArg *arg, const DTIns *ins, const VTSel colLowerIncl,
const VTSel colUpperExcl, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTArg, class DTIns, typename VTSel>
void insertCol(DTArg *&res, const DTArg *arg, const DTIns *ins, const VTSel colLowerIncl, const VTSel colUpperExcl,
DCTX(ctx)) {
InsertCol<DTArg, DTIns, VTSel>::apply(res, arg, ins, colLowerIncl, colUpperExcl, ctx);
}
// ****************************************************************************
// Boundary validation
// ****************************************************************************
template <typename VTSel>
void validateArgsInsertCol(size_t colLowerIncl_Size, VTSel colLowerIncl, size_t colUpperExcl_Size, VTSel colUpperExcl,
size_t numRowsArg, size_t numColsArg, size_t numRowsIns, size_t numColsIns) {
if (colUpperExcl_Size < colLowerIncl_Size || numColsArg < colUpperExcl_Size ||
(colLowerIncl_Size == numColsArg && colLowerIncl_Size != 0)) {
std::ostringstream errMsg;
errMsg << "invalid arguments '" << colLowerIncl << ", " << colUpperExcl
<< "' passed to InsertCol: it must hold 0 <= colLowerIncl <= "
"colUpperExcl <= #columns "
<< "and colLowerIncl < #columns (unless both are zero) where "
"#columns of arg is '"
<< numColsArg << "'";
throw std::out_of_range(errMsg.str());
}
if (numColsIns != colUpperExcl_Size - colLowerIncl_Size) {
std::ostringstream errMsg;
errMsg << "invalid arguments '" << colLowerIncl << ", " << colUpperExcl
<< "' passed to InsertCol: the number of addressed columns in arg '"
<< colUpperExcl_Size - colLowerIncl_Size << "' and the number of columns in ins '" << numColsIns
<< "' must match";
throw std::out_of_range(errMsg.str());
}
if (numRowsIns != numRowsArg) {
std::ostringstream errMsg;
errMsg << "invalid arguments passed to InsertCol: the number of rows "
"in arg '"
<< numRowsArg << "' and ins '" << numRowsIns << "' must match";
throw std::out_of_range(errMsg.str());
}
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VTArg, typename VTSel> struct InsertCol<DenseMatrix<VTArg>, DenseMatrix<VTArg>, VTSel> {
static void apply(DenseMatrix<VTArg> *&res, const DenseMatrix<VTArg> *arg, const DenseMatrix<VTArg> *ins,
VTSel colLowerIncl, VTSel colUpperExcl, DCTX(ctx)) {
const size_t numRowsArg = arg->getNumRows();
const size_t numColsArg = arg->getNumCols();
const size_t numRowsIns = ins->getNumRows();
const size_t numColsIns = ins->getNumCols();
const size_t colLowerIncl_Size = static_cast<const size_t>(colLowerIncl);
const size_t colUpperExcl_Size = static_cast<const size_t>(colUpperExcl);
validateArgsInsertCol(colLowerIncl_Size, colLowerIncl, colUpperExcl_Size, colUpperExcl, numRowsArg, numColsArg,
numRowsIns, numColsIns);
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTArg>>(numRowsArg, numColsArg, false);
VTArg *valuesRes = res->getValues();
const VTArg *valuesArg = arg->getValues();
const VTArg *valuesIns = ins->getValues();
const size_t rowSkipRes = res->getRowSkip();
const size_t rowSkipArg = arg->getRowSkip();
const size_t rowSkipIns = ins->getRowSkip();
// TODO Can be simplified/more efficient in certain cases.
for (size_t r = 0; r < numRowsArg; r++) {
std::copy(valuesArg, valuesArg + colLowerIncl_Size, valuesRes);
std::copy(valuesIns, valuesIns + numColsIns, valuesRes + colLowerIncl_Size);
std::copy(valuesArg + colUpperExcl_Size, valuesArg + numColsArg, valuesRes + colUpperExcl_Size);
valuesRes += rowSkipRes;
valuesArg += rowSkipArg;
valuesIns += rowSkipIns;
}
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix
// ----------------------------------------------------------------------------
template <typename VTArg, typename VTSel> struct InsertCol<Matrix<VTArg>, Matrix<VTArg>, VTSel> {
static void apply(Matrix<VTArg> *&res, const Matrix<VTArg> *arg, const Matrix<VTArg> *ins, VTSel colLowerIncl,
VTSel colUpperExcl, DCTX(ctx)) {
const size_t numRowsArg = arg->getNumRows();
const size_t numColsArg = arg->getNumCols();
const size_t colLowerIncl_Size = static_cast<const size_t>(colLowerIncl);
const size_t colUpperExcl_Size = static_cast<const size_t>(colUpperExcl);
validateArgsInsertCol(colLowerIncl_Size, colLowerIncl, colUpperExcl_Size, colUpperExcl, numRowsArg, numColsArg,
ins->getNumRows(), ins->getNumCols());
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VTArg>>(numRowsArg, numColsArg, false);
res->prepareAppend();
for (size_t r = 0; r < numRowsArg; ++r) {
// fill values left of insertion, then between and lastly to its
// right
for (size_t c = 0; c < colLowerIncl_Size; ++c)
res->append(r, c, arg->get(r, c));
for (size_t c = colLowerIncl_Size; c < colUpperExcl_Size; ++c)
res->append(r, c, ins->get(r, c - colLowerIncl_Size));
for (size_t c = colUpperExcl_Size; c < numColsArg; ++c)
res->append(r, c, arg->get(r, c));
}
res->finishAppend();
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_INSERTROW_H