forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckEqApprox.h
More file actions
252 lines (213 loc) · 9.31 KB
/
Copy pathCheckEqApprox.h
File metadata and controls
252 lines (213 loc) · 9.31 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
/*
* 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/DenseMatrix.h>
#include <runtime/local/datastructures/Frame.h>
#include <runtime/local/datastructures/Matrix.h>
#include <cstddef>
#include <cstdlib>
#include <cstring>
// TODO This kernel should handle integral value types gracefully, e.g. by
// forwarding to the non-approximate checkEq-kernel. This would allow us to
// easily use the checkEqApprox-kernel in test cases without differentiating
// integral and floating-point value types.
// ****************************************************************************
// Struct for partial template specialization
// ****************************************************************************
template <class DT> struct CheckEqApprox {
static bool apply(const DT *lhs, const DT *rhs, double eps, DCTX(ctx)) = delete;
};
// ****************************************************************************
// Convenience function
// ****************************************************************************
/**
* @brief Checks if the two given matrices are approximately equal.
*
* More precisely, this requires that they have the same dimensions and are
* approximately elementwise equal, i.e., if the difference between two elements
* is not greater than the threshold `eps`, they are considered as equal.
*
* @param lhs The first matrix.
* @param rhs The second matrix.
* @param eps The similarity threshold.
* @return `true` if they are equal, `false` otherwise.
*/
template <class DT> bool checkEqApprox(const DT *lhs, const DT *rhs, double eps, DCTX(ctx)) {
return CheckEqApprox<DT>::apply(lhs, rhs, eps, ctx);
}
/*
// ****************************************************************************
// Operator == for matrices of the same type
// ****************************************************************************
template<class DT>
bool operator==(const DT & lhs, const DT & rhs) {
double eps= 0.000001; //required for the equal operator
// nullptr might become problematic some day.
return checkEqApprox(&lhs, &rhs, eps, nullptr);
}
*/
// ****************************************************************************
// (Partial) template specializations for different data/value types
// ****************************************************************************
// Note that we do not use the generic `get` interface to matrices here since
// this operator is meant to be used for writing tests for, besides others,
// those generic interfaces.
// ----------------------------------------------------------------------------
// DenseMatrix
// ----------------------------------------------------------------------------
template <typename VT> struct CheckEqApprox<DenseMatrix<VT>> {
static bool apply(const DenseMatrix<VT> *lhs, const DenseMatrix<VT> *rhs, double eps, DCTX(ctx)) {
if (lhs == rhs)
return true;
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (numRows != rhs->getNumRows() || numCols != rhs->getNumCols())
return false;
const VT *valuesLhs = lhs->getValues();
const VT *valuesRhs = rhs->getValues();
const size_t rowSkipLhs = lhs->getRowSkip();
const size_t rowSkipRhs = rhs->getRowSkip();
if (valuesLhs == valuesRhs && rowSkipLhs == rowSkipRhs) // same pointer
return true;
for (size_t r = 0; r < numRows; r++) {
for (size_t c = 0; c < numCols; c++) {
VT diff = valuesLhs[c] - valuesRhs[c];
if (diff == 0)
continue;
diff = diff > 0 ? diff : -diff;
if (diff > eps)
return false;
}
valuesLhs += lhs->getRowSkip();
valuesRhs += rhs->getRowSkip();
}
return true;
}
};
// ----------------------------------------------------------------------------
// CSRMatrix
// ----------------------------------------------------------------------------
template <typename VT> struct CheckEqApprox<CSRMatrix<VT>> {
static bool apply(const CSRMatrix<VT> *lhs, const CSRMatrix<VT> *rhs, double eps, DCTX(ctx)) {
if (lhs == rhs)
return true;
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (numRows != rhs->getNumRows() || numCols != rhs->getNumCols())
return false;
for (size_t r = 0; r < numRows; r++) {
const VT *valuesLhs = lhs->getValues(r);
const VT *valuesRhs = rhs->getValues(r);
const size_t nnzElementsLhs = lhs->getNumNonZeros(r);
const size_t nnzElementsRhs = rhs->getNumNonZeros(r);
if (nnzElementsLhs != nnzElementsRhs)
return false;
for (size_t c = 0; c < nnzElementsLhs; c++) {
VT diff = valuesLhs[c] - valuesRhs[c];
if (diff == 0)
continue;
diff = diff > 0 ? diff : -diff;
if (diff > eps)
return false;
}
}
return true;
}
};
// ----------------------------------------------------------------------------
// Frame
// ----------------------------------------------------------------------------
template <> struct CheckEqApprox<Frame> {
static bool apply(const Frame *lhs, const Frame *rhs, double eps, DCTX(ctx)) {
if (lhs == rhs)
return true;
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (numRows != rhs->getNumRows() || numCols != rhs->getNumCols())
return false;
if (memcmp(lhs->getSchema(), rhs->getSchema(), numCols * sizeof(ValueTypeCode)) != 0)
return false;
const std::string *labelsLhs = lhs->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 (lhs->getColumnType(c)) {
// For all value types:
case ValueTypeCode::F64:
if (!checkEqApprox(lhs->getColumn<double>(c), rhs->getColumn<double>(c), eps, ctx))
return false;
break;
case ValueTypeCode::F32:
if (!checkEqApprox(lhs->getColumn<float>(c), rhs->getColumn<float>(c), eps, ctx))
return false;
break;
case ValueTypeCode::SI64:
if (!checkEqApprox(lhs->getColumn<int64_t>(c), rhs->getColumn<int64_t>(c), eps, ctx))
return false;
break;
case ValueTypeCode::SI32:
if (!checkEqApprox(lhs->getColumn<int32_t>(c), rhs->getColumn<int32_t>(c), eps, ctx))
return false;
break;
case ValueTypeCode::SI8:
if (!checkEqApprox(lhs->getColumn<int8_t>(c), rhs->getColumn<int8_t>(c), eps, ctx))
return false;
break;
case ValueTypeCode::UI64:
if (!checkEqApprox(lhs->getColumn<uint64_t>(c), rhs->getColumn<uint64_t>(c), eps, ctx))
return false;
break;
case ValueTypeCode::UI32:
if (!checkEqApprox(lhs->getColumn<uint32_t>(c), rhs->getColumn<uint32_t>(c), eps, ctx))
return false;
break;
case ValueTypeCode::UI8:
if (!checkEqApprox(lhs->getColumn<uint8_t>(c), rhs->getColumn<uint8_t>(c), eps, ctx))
return false;
break;
default:
throw std::runtime_error("CheckEqApprox::apply: unknown value type code");
}
}
return true;
}
};
// ----------------------------------------------------------------------------
// Matrix
// ----------------------------------------------------------------------------
template <typename VT> struct CheckEqApprox<Matrix<VT>> {
static bool apply(const Matrix<VT> *lhs, const Matrix<VT> *rhs, double eps, DCTX(ctx)) {
if (lhs == rhs)
return true;
const size_t numRows = lhs->getNumRows();
const size_t numCols = lhs->getNumCols();
if (numRows != rhs->getNumRows() || numCols != rhs->getNumCols())
return false;
for (size_t r = 0; r < numRows; ++r) {
for (size_t c = 0; c < numCols; ++c) {
double diff = lhs->get(r, c) - rhs->get(r, c);
if (std::abs(diff) > eps)
return false;
}
}
return true;
}
};