-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
319 lines (276 loc) · 9.2 KB
/
test.cpp
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
#include <gtest/gtest.h>
#include <Tracy.hpp>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <lttb.hpp>
template <typename T>
class FastLTTB : public ::testing::Test {
protected:
FastLTTB() {}
~FastLTTB() override {}
static void SetUpTestCase() {
ZoneScoped;
len = 100000000UL; // 100 mil
test_x = new T[len];
test_y = new T[len];
std::printf("Generating dummy f%d data...\n", int(sizeof(T) * 8));
constexpr int lut_size = 2048;
float sin_lut[lut_size];
for (int i = 0; i < lut_size; ++i) {
sin_lut[i] = std::sin(float(i) / lut_size * 2 * M_PI);
}
for (int i = 0; i < len; ++i) {
test_x[i] = i;
float val = 0.0f;
val += 1.000f * sin_lut[(i >> 8) % lut_size];
val += 0.500f * sin_lut[(i >> 4) % lut_size];
val += 0.250f * sin_lut[(i >> 2) % lut_size];
val += 0.125f * sin_lut[(i >> 0) % lut_size];
test_y[i] = val;
}
std::printf("Generation done\n");
}
static void TearDownTestCase() {
delete[] test_x;
delete[] test_y;
}
void SetUp() override {
out_cap = len / 8 + 2;
out_x = new T[out_cap];
out_y = new T[out_cap];
}
void TearDown() override {
delete[] out_x;
delete[] out_y;
}
void write_output(std::string fname, uint64_t out_len, uint64_t offset = 0) {
ZoneScoped;
std::printf("Writing %s with %lu entries.\n", fname.c_str(), out_len);
std::ofstream file(fname);
for (uint64_t i = 0; i < out_len; ++i) {
file << out_x[i + offset] << "," << out_y[i + offset] << "\n";
}
file.close();
}
public:
static uint64_t len;
static T *test_x, *test_y;
uint64_t out_cap;
T *out_x, *out_y;
};
template <typename T>
uint64_t FastLTTB<T>::len;
template <typename T>
T *FastLTTB<T>::test_x;
template <typename T>
T *FastLTTB<T>::test_y;
typedef ::testing::Types<float, double> FloatTypes;
TYPED_TEST_SUITE(FastLTTB, FloatTypes);
TYPED_TEST(FastLTTB, Correct_I100_B1024_Scalar) {
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len = lttb::downsample(test_x, test_y, 100, out_x, out_y, len, 1024);
ASSERT_EQ(out_len, 2);
ASSERT_EQ(out_x[0], test_x[0]);
ASSERT_EQ(out_x[1], test_x[99]);
ASSERT_EQ(out_y[0], test_y[0]);
ASSERT_EQ(out_y[1], test_y[99]);
}
TYPED_TEST(FastLTTB, Correct_I100_B1024_SIMD) {
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len =
lttb::downsample_simd(test_x, test_y, 100, out_x, out_y, len, 1024);
ASSERT_EQ(out_len, 2);
ASSERT_EQ(out_x[0], test_x[0]);
ASSERT_EQ(out_x[1], test_x[99]);
ASSERT_EQ(out_y[0], test_y[0]);
ASSERT_EQ(out_y[1], test_y[99]);
}
TYPED_TEST(FastLTTB, EndPoints_Scalar) {
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len =
lttb::downsample(test_x, test_y, 10000, out_x, out_y, len, 1024);
ASSERT_NE(out_x[0], out_x[out_len - 1]);
}
TYPED_TEST(FastLTTB, EndPoints_SIMD) {
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len =
lttb::downsample_simd(test_x, test_y, 10000, out_x, out_y, len, 1024);
ASSERT_NE(out_x[0], out_x[out_len - 1]);
}
TYPED_TEST(FastLTTB, Timing100M_Scalar) {
ZoneScoped;
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len =
lttb::downsample(test_x, test_y, 100000000, out_x, out_y, len, 1024);
}
TYPED_TEST(FastLTTB, Timing100M_NoX_Scalar) {
ZoneScoped;
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len =
lttb::downsample(nullptr, test_y, 100000000, out_x, out_y, len, 1024);
}
TYPED_TEST(FastLTTB, Timing100M_SIMD) {
ZoneScoped;
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len =
lttb::downsample_simd(test_x, test_y, 100000000, out_x, out_y, len, 1024);
}
TYPED_TEST(FastLTTB, Speedup10M_10Reps) {
ZoneScoped;
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
const int reps = 10;
constexpr uint64_t num_samples = 10000000;
for (int use_x = 0; use_x < 2; ++use_x) {
ZoneScopedN("UseX");
ZoneValue(use_x);
for (int bucket_size = 16; bucket_size < 1 << 16; bucket_size <<= 1) {
double timing_simd = 0.0;
double timing_scalar = 0.0;
double best_timing_simd = 10000.0;
double best_timing_scalar = 10000.0;
ZoneScopedN("BucketSize");
ZoneValue(bucket_size);
{
auto start = std::chrono::high_resolution_clock::now();
ZoneScopedN("SIMD");
for (int i = 0; i < reps; ++i) {
ZoneScoped;
auto single_start = std::chrono::high_resolution_clock::now();
int out_len;
if (use_x != 0) {
out_len = lttb::downsample_simd(test_x, test_y, num_samples, out_x,
out_y, len, bucket_size);
} else {
out_len = lttb::downsample_simd(nullptr, test_y, num_samples, out_x,
out_y, len, bucket_size);
}
auto single_stop = std::chrono::high_resolution_clock::now();
double timing = std::chrono::duration_cast<
std::chrono::duration<double, std::milli>>(
single_stop - single_start)
.count();
best_timing_simd = std::min(best_timing_simd, timing);
ASSERT_NE(out_len, 0);
}
auto stop = std::chrono::high_resolution_clock::now();
timing_simd =
std::chrono::duration_cast<
std::chrono::duration<double, std::milli>>(stop - start)
.count() /
reps;
}
{
auto start = std::chrono::high_resolution_clock::now();
ZoneScopedN("Scalar");
for (int i = 0; i < reps; ++i) {
ZoneScoped;
auto single_start = std::chrono::high_resolution_clock::now();
int out_len;
if (use_x != 0) {
out_len = lttb::downsample(test_x, test_y, num_samples, out_x,
out_y, len, bucket_size);
} else {
out_len = lttb::downsample(nullptr, test_y, num_samples, out_x,
out_y, len, bucket_size);
}
ASSERT_NE(out_len, 0);
auto single_stop = std::chrono::high_resolution_clock::now();
double timing = std::chrono::duration_cast<
std::chrono::duration<double, std::milli>>(
single_stop - single_start)
.count();
best_timing_scalar = std::min(best_timing_scalar, timing);
}
auto stop = std::chrono::high_resolution_clock::now();
timing_scalar =
std::chrono::duration_cast<
std::chrono::duration<double, std::milli>>(stop - start)
.count() /
reps;
}
// clang-format off
std::printf(
"Speedup [bucket size %5d | %10s]: "
"<mean> x%.2f (simd=%8.2f ms; scalar=%8.2f ms) "
"<best> x%.2f (simd=%8.2f ms; scalar=%8.2f ms) "
"\n",
bucket_size, use_x ? "with x" : "without x",
timing_scalar / timing_simd, timing_simd, timing_scalar,
best_timing_scalar / best_timing_simd, best_timing_simd, best_timing_scalar
);
// clang-format on
}
}
}
TYPED_TEST(FastLTTB, Timing100M_NoX_SIMD) {
ZoneScoped;
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
int out_len = lttb::downsample_simd(nullptr, test_y, 100000000, out_x, out_y,
len, 1024);
}
TYPED_TEST(FastLTTB, SIMDCorrect) {
ZoneScoped;
auto test_x = this->test_x;
auto test_y = this->test_y;
auto out_x = this->out_x;
auto out_y = this->out_y;
auto len = this->len;
uint64_t size = 100000;
int bs = 1024;
auto *out_x_1 = out_x;
auto *out_y_1 = out_y;
auto *out_x_2 = out_x + size;
auto *out_y_2 = out_y + size;
// Generate reference
int out_len_ref =
lttb::downsample(test_x, test_y, size, out_x_1, out_y_1, len, 8);
this->write_output("input.csv", out_len_ref);
int out_len_1 =
lttb::downsample(test_x, test_y, size, out_x_1, out_y_1, len, bs);
int out_len_2 =
lttb::downsample_simd(test_x, test_y, size, out_x_2, out_y_2, len, bs);
this->write_output("output_scalar.csv", out_len_1, 0);
this->write_output("output_simd.csv", out_len_2, size);
ASSERT_EQ(out_len_1, out_len_2);
for (uint64_t i = 0; i < out_len_1; ++i) {
EXPECT_EQ(out_x_1[i], out_x_2[i]) << "i=" << i << ", out_len=" << out_len_1;
ASSERT_EQ(out_y_1[i], out_y_2[i]) << "i=" << i << ", out_len=" << out_len_1;
}
}