forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertRow.h
More file actions
176 lines (146 loc) · 7.72 KB
/
Copy pathInsertRow.h
File metadata and controls
176 lines (146 loc) · 7.72 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
/*
* 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_INSERTROW_H
#define SRC_RUNTIME_LOCAL_KERNELS_INSERTROW_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 InsertRow {
static void apply(DTArg *&res, const DTArg *arg, const DTIns *ins, const VTSel rowLowerIncl,
const VTSel rowUpperExcl, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
template <class DTArg, class DTIns, typename VTSel>
void insertRow(DTArg *&res, const DTArg *arg, const DTIns *ins, const VTSel rowLowerIncl, const VTSel rowUpperExcl,
DCTX(ctx)) {
InsertRow<DTArg, DTIns, VTSel>::apply(res, arg, ins, rowLowerIncl, rowUpperExcl, ctx);
}
// ****************************************************************************
// Boundary validation
// ****************************************************************************
template <typename VTSel>
void validateArgsInsertRow(size_t rowLowerIncl_Size, VTSel rowLowerIncl, size_t rowUpperExcl_Size, VTSel rowUpperExcl,
size_t numRowsArg, size_t numColsArg, size_t numRowsIns, size_t numColsIns) {
if (rowUpperExcl_Size < rowLowerIncl_Size || numRowsArg < rowUpperExcl_Size ||
(rowLowerIncl_Size == numRowsArg && rowLowerIncl_Size != 0)) {
std::ostringstream errMsg;
errMsg << "invalid arguments '" << rowLowerIncl << ", " << rowUpperExcl
<< "' passed to InsertRow: it must hold 0 <= rowLowerIncl <= "
"rowUpperExcl <= #rows "
<< "and rowLowerIncl < #rows (unless both are zero) where #rows "
"of arg is '"
<< numRowsArg << "'";
throw std::out_of_range(errMsg.str());
}
if (numRowsIns != rowUpperExcl_Size - rowLowerIncl_Size) {
std::ostringstream errMsg;
errMsg << "invalid arguments '" << rowLowerIncl << ", " << rowUpperExcl
<< "' passed to InsertRow: the number of addressed rows in arg '"
<< rowUpperExcl_Size - rowLowerIncl_Size << "' and the number of rows in ins '" << numRowsIns
<< "' must match";
throw std::out_of_range(errMsg.str());
}
if (numColsIns != numColsArg) {
std::ostringstream errMsg;
errMsg << "invalid arguments passed to InsertRow: the number of "
"columns in arg '"
<< numColsArg << "' and ins '" << numColsIns << "' must match";
throw std::out_of_range(errMsg.str());
}
}
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// ----------------------------------------------------------------------------
// DenseMatrix <- DenseMatrix
// ----------------------------------------------------------------------------
template <typename VT, typename VTSel> struct InsertRow<DenseMatrix<VT>, DenseMatrix<VT>, VTSel> {
static void apply(DenseMatrix<VT> *&res, const DenseMatrix<VT> *arg, const DenseMatrix<VT> *ins, VTSel rowLowerIncl,
VTSel rowUpperExcl, 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 rowLowerIncl_Size = static_cast<const size_t>(rowLowerIncl);
const size_t rowUpperExcl_Size = static_cast<const size_t>(rowUpperExcl);
validateArgsInsertRow(rowLowerIncl_Size, rowLowerIncl, rowUpperExcl_Size, rowUpperExcl, numRowsArg, numColsArg,
numRowsIns, numColsIns);
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRowsArg, numColsArg, false);
VT *valuesRes = res->getValues();
const VT *valuesArg = arg->getValues();
const VT *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 < rowLowerIncl_Size; r++) {
std::copy(valuesArg, valuesArg + numColsArg, valuesRes);
valuesRes += rowSkipRes;
valuesArg += rowSkipArg;
}
for (size_t r = rowLowerIncl_Size; r < rowUpperExcl_Size; r++) {
std::copy(valuesIns, valuesIns + numColsArg, valuesRes);
valuesRes += rowSkipRes;
valuesIns += rowSkipIns;
}
valuesArg += rowSkipArg * numRowsIns; // skip rows in arg
for (size_t r = rowUpperExcl_Size; r < numRowsArg; r++) {
std::copy(valuesArg, valuesArg + numColsArg, valuesRes);
valuesRes += rowSkipRes;
valuesArg += rowSkipArg;
}
}
};
// ----------------------------------------------------------------------------
// Matrix <- Matrix
// ----------------------------------------------------------------------------
template <typename VT, typename VTSel> struct InsertRow<Matrix<VT>, Matrix<VT>, VTSel> {
static void apply(Matrix<VT> *&res, const Matrix<VT> *arg, const Matrix<VT> *ins, VTSel rowLowerIncl,
VTSel rowUpperExcl, DCTX(ctx)) {
const size_t numRowsArg = arg->getNumRows();
const size_t numColsArg = arg->getNumCols();
const size_t rowLowerIncl_Size = static_cast<const size_t>(rowLowerIncl);
const size_t rowUpperExcl_Size = static_cast<const size_t>(rowUpperExcl);
validateArgsInsertRow(rowLowerIncl_Size, rowLowerIncl, rowUpperExcl_Size, rowUpperExcl, numRowsArg, numColsArg,
ins->getNumRows(), ins->getNumCols());
if (res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRowsArg, numColsArg, false);
// fill values above insertion, then between and lastly below
res->prepareAppend();
for (size_t r = 0; r < rowLowerIncl_Size; ++r)
for (size_t c = 0; c < numColsArg; ++c)
res->append(r, c, arg->get(r, c));
for (size_t r = rowLowerIncl_Size; r < rowUpperExcl_Size; ++r)
for (size_t c = 0; c < numColsArg; ++c)
res->append(r, c, ins->get(r - rowLowerIncl_Size, c));
for (size_t r = rowUpperExcl_Size; r < numRowsArg; ++r)
for (size_t c = 0; c < numColsArg; ++c)
res->append(r, c, arg->get(r, c));
res->finishAppend();
}
};
#endif // SRC_RUNTIME_LOCAL_KERNELS_INSERTROW_H