-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_main.cpp
More file actions
executable file
·551 lines (415 loc) · 16.2 KB
/
bench_main.cpp
File metadata and controls
executable file
·551 lines (415 loc) · 16.2 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <array>
#include <cstdio>
#include <iostream>
#include <random>
#include <valarray>
#if defined __AVX512F__ || defined __AVX2__
#include <immintrin.h>
#endif
#include <benchmark/benchmark.h>
static const auto NUM_PHENOS = 500000;
static const auto NUM_PAIRS = 256;
template <typename T = int8_t>
static std::vector<T> make_random_bools(size_t n = NUM_PHENOS) {
auto seed = std::random_device{}();
std::mt19937 rng(seed);
std::uint32_t data;
std::vector<T> bools;
bools.reserve(n);
int bit_count = 0;
for (size_t i = 0; i < n; ++i) {
if (bit_count == 0) {
data = rng();
bit_count = 32;
}
bool bit = data & 1;
data >>= 1;
bit_count--;
bools.emplace_back(bit);
}
return bools;
}
template<typename T = size_t>
static std::vector<std::pair<T, T>> make_pairs(size_t n_phenos = NUM_PHENOS, size_t n_pairs = NUM_PAIRS) {
auto seed = std::random_device{}();
std::mt19937 rng(seed);
std::uniform_int_distribution<T> dis(0, n_phenos - 1);
std::vector<std::pair<T, T>> pairs;
pairs.reserve(n_pairs);
for (size_t i = 0; i < n_pairs; i++) {
pairs.emplace_back(std::make_pair(dis(rng), dis(rng)));
}
std::sort(pairs.begin(), pairs.end());
return pairs;
}
template<typename T = size_t>
static std::pair<std::vector<T>, std::vector<T>> make_pairs_struct_of_arrays(size_t n_phenos = NUM_PHENOS, size_t n_pairs = NUM_PAIRS) {
auto seed = std::random_device{}();
std::mt19937 rng(seed);
std::uniform_int_distribution<T> dis(0, n_phenos - 1);
std::vector<std::pair<T, T>> pairs;
pairs.reserve(n_pairs);
for (size_t i = 0; i < n_pairs; i++) {
pairs.emplace_back(std::make_pair(dis(rng), dis(rng)));
}
std::sort(pairs.begin(), pairs.end());
std::vector<T> left_members;
std::vector<T> right_members;
left_members.reserve(n_pairs);
right_members.reserve(n_pairs);
for (const auto [a, b] : pairs) {
left_members.emplace_back(a);
right_members.emplace_back(b);
}
return std::make_pair(left_members, right_members);
}
#ifdef __AVX512F__
// Maybe I could use c++20's std::popcount instead??
static inline int32_t popcnt128(__m128i n) {
const __m128i n_hi = _mm_unpackhi_epi64(n, n);
return __builtin_popcountll(_mm_cvtsi128_si64(n)) + __builtin_popcountll(_mm_cvtsi128_si64(n_hi));
}
#endif
static void BM_fp(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
std::vector<int8_t> phenotypes_ = make_random_bools();
double cscs = 0;
double cscn = 0;
double cncn = 0;
for (auto _ : state) {
for (auto &p : pairs) {
auto &[p1, p2] = p;
int x = phenotypes_[p1];
int y = phenotypes_[p2];
if (x < -1 || x > 1 || y < -1 || y > 1) {
throw(std::runtime_error("ERROR: invalid phenotype in calculate."));
}
cscs += ((x == 1) && (y == 1));
cscn += ((x == 1) && (y == 0));
cscn += ((x == 0) && (y == 1));
cncn += ((x == 0) && (y == 0));
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cncn);
}
BENCHMARK(BM_fp);
static void BM_nochecks_fp(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
std::vector<int8_t> phenotypes_ = make_random_bools();
double cscs = 0;
double cscn = 0;
double cncn = 0;
for (auto _ : state) {
for (auto &p : pairs) {
auto &[p1, p2] = p;
int x = phenotypes_[p1];
int y = phenotypes_[p2];
cscs += ((x == 1) && (y == 1));
cscn += ((x == 1) && (y == 0));
cscn += ((x == 0) && (y == 1));
cncn += ((x == 0) && (y == 0));
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cncn);
}
BENCHMARK(BM_nochecks_fp);
static void BM_int(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
int64_t cncn = 0;
for (auto _ : state) {
for (auto &p : pairs) {
auto &[p1, p2] = p;
auto x = phenotypes_[p1];
auto y = phenotypes_[p2];
if (x < -1 || x > 1 || y < -1 || y > 1) {
throw(std::runtime_error("ERROR: invalid phenotype in calculate."));
}
cscs += ((x == 1) && (y == 1));
cscn += ((x == 1) && (y == 0));
cscn += ((x == 0) && (y == 1));
cncn += ((x == 0) && (y == 0));
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cncn);
}
BENCHMARK(BM_int);
static void BM_nochecks_int(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
int64_t cncn = 0;
for (auto _ : state) {
for (auto &p : pairs) {
auto &[p1, p2] = p;
auto x = phenotypes_[p1];
auto y = phenotypes_[p2];
cscs += ((x == 1) && (y == 1));
cscn += ((x == 1) && (y == 0));
cscn += ((x == 0) && (y == 1));
cncn += ((x == 0) && (y == 0));
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cncn);
}
BENCHMARK(BM_nochecks_int);
static void BM_nochecks_bool(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
auto pheno_bytes = make_random_bools<bool>();
std::vector<bool> phenotypes_;
phenotypes_.reserve(NUM_PHENOS);
for (auto byte : pheno_bytes) {
phenotypes_.emplace_back(byte);
}
int64_t cscs = 0;
int64_t cscn = 0;
int64_t cncn = 0;
for (auto _ : state) {
for (const auto p : pairs) {
const auto [p1, p2] = p;
const auto x = phenotypes_[p1];
const auto y = phenotypes_[p2];
cscs += x & y;
cscn += x ^ y;
cncn += !(x || y);
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cncn);
}
BENCHMARK(BM_nochecks_bool);
static void BM_nochecks_valarray_bool(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
auto pheno_bytes = make_random_bools<bool>();
std::valarray<bool> phenotypes_(NUM_PHENOS);
for (size_t i = 0; i < pheno_bytes.size(); i++) {
phenotypes_[i] = pheno_bytes[i];
}
int64_t cscs = 0;
int64_t cscn = 0;
int64_t cncn = 0;
for (auto _ : state) {
for (const auto p : pairs) {
const auto [p1, p2] = p;
const auto x = phenotypes_[p1];
const auto y = phenotypes_[p2];
cscs += x & y;
cscn += x ^ y;
cncn += !(x || y);
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cncn);
}
BENCHMARK(BM_nochecks_valarray_bool);
static void BM_nochecks_int8_t_nocncn(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
for (auto _ : state) {
for (const auto p : pairs) {
const auto [p1, p2] = p;
const auto x = phenotypes_[p1];
const auto y = phenotypes_[p2];
cscs += x & y;
cscn += x ^ y;
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
}
BENCHMARK(BM_nochecks_int8_t_nocncn);
static void BM_nochecks_bool_32_bit_pairs_nocncn(benchmark::State &state) {
auto pairs = make_pairs<int32_t>();
std::vector<bool> phenotypes_ = make_random_bools<bool>();
int64_t cscs = 0;
int64_t cscn = 0;
for (auto _ : state) {
for (const auto p : pairs) {
const auto [p1, p2] = p;
const auto x = phenotypes_[p1];
const auto y = phenotypes_[p2];
cscs += x & y;
cscn += x ^ y;
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
}
BENCHMARK(BM_nochecks_bool_32_bit_pairs_nocncn);
static void BM_nochecks_int8_t_32_bit_pairs_nocncn(benchmark::State &state) {
auto pairs = make_pairs<int32_t>();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
for (auto _ : state) {
for (const auto p : pairs) {
const auto [p1, p2] = p;
const auto x = phenotypes_[p1];
const auto y = phenotypes_[p2];
cscs += x & y;
cscn += x ^ y;
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
}
BENCHMARK(BM_nochecks_int8_t_32_bit_pairs_nocncn);
static void BM_fastest_two_accumulators(benchmark::State &state) {
auto pairs = make_pairs<int32_t>();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
int64_t cscs2 = 0;
int64_t cscn2 = 0;
for (auto _ : state) {
for (size_t i = 0; i < pairs.size() - 1; i += 2) {
const auto [p1_a, p1_b] = pairs[i];
const auto x1 = phenotypes_[p1_a];
const auto y1 = phenotypes_[p1_b];
cscs += x1 & y1;
cscn += x1 ^ y1;
const auto [p2_a, p2_b] = pairs[i + 1];
auto x2 = phenotypes_[p2_a];
auto y2 = phenotypes_[p2_b];
cscs2 += x2 & y2;
cscn2 += x2 ^ y2;
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
benchmark::DoNotOptimize(cscs2);
benchmark::DoNotOptimize(cscn2);
}
BENCHMARK(BM_fastest_two_accumulators);
static void BM_access_only(benchmark::State &state) {
std::vector<std::pair<size_t, size_t>> pairs = make_pairs();
std::vector<int8_t> phenotypes_ = make_random_bools();
for (auto _ : state) {
for (const auto p : pairs) {
const auto [p1, p2] = p;
const auto x = phenotypes_[p1];
const auto y = phenotypes_[p2];
benchmark::DoNotOptimize(x);
benchmark::DoNotOptimize(y);
}
}
}
BENCHMARK(BM_access_only);
#ifdef __AVX512F__
static void BM_vectorized_access_only(benchmark::State &state) {
auto [left_members, right_members] = make_pairs_struct_of_arrays<int32_t>();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
for (auto _ : state) {
for (size_t i = 0; i < left_members.size() - 15; i += 16) {
auto left_addresses = _mm512_loadu_si512(&left_members[i]);
auto right_addresses = _mm512_loadu_si512(&right_members[i]);
auto lefts = _mm512_i32gather_epi32(left_addresses, phenotypes_.data(), 1);
auto rights = _mm512_i32gather_epi32(right_addresses, phenotypes_.data(), 1);
auto left_packed = _mm512_cvtepi32_epi8(lefts);
auto right_packed = _mm512_cvtepi32_epi8(rights);
benchmark::DoNotOptimize(left_packed);
benchmark::DoNotOptimize(right_packed);
}
}
}
BENCHMARK(BM_vectorized_access_only);
static void BM_vectorized(benchmark::State &state) {
auto [left_members, right_members] = make_pairs_struct_of_arrays<int32_t>();
std::vector<int8_t> phenotypes_ = make_random_bools();
int64_t cscs = 0;
int64_t cscn = 0;
for (auto _ : state) {
for (size_t i = 0; i < left_members.size() - 15; i += 16) {
auto left_addresses = _mm512_loadu_si512(&left_members[i]);
auto right_addresses = _mm512_loadu_si512(&right_members[i]);
auto lefts = _mm512_i32gather_epi32(left_addresses, phenotypes_.data(), 1);
auto rights = _mm512_i32gather_epi32(right_addresses, phenotypes_.data(), 1);
auto left_packed = _mm512_cvtepi32_epi8(lefts);
auto right_packed = _mm512_cvtepi32_epi8(rights);
auto cscs_batch = _mm_and_si128(left_packed, right_packed);
auto cscn_batch = _mm_xor_si128(left_packed, right_packed);
cscs += popcnt128(cscs_batch);
cscn += popcnt128(cscn_batch);
}
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
}
BENCHMARK(BM_vectorized);
// This could still be made faster by doing aligned loads rather than unaligned
#endif
static void BM_AVX2(benchmark::State &state) {
auto pairs = make_pairs_struct_of_arrays<int32_t>();
std::vector<int8_t> phenotypes_ = make_random_bools();
for (auto _ : state) {
int64_t cscs = 0;
int64_t cscn = 0;
for (size_t i = 0; i < pairs.first.size(); i += 8) {
auto left_addresses = _mm256_loadu_si256(reinterpret_cast<const __m256i_u *>(&pairs.first[i]));
auto right_addresses = _mm256_loadu_si256(reinterpret_cast<const __m256i_u *>(&pairs.second[i]));
auto lefts = _mm256_i32gather_epi32(reinterpret_cast<const int *>(phenotypes_.data()), left_addresses, 1);
auto rights = _mm256_i32gather_epi32(reinterpret_cast<const int *>(phenotypes_.data()), right_addresses, 1);
const auto addition_vector = _mm256_setr_epi8(0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127,
0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127);
// Set the high bit on each byte only if there was a 1 there before!
// We are using this both to mask out only the bytes we care about, as we retrieved 3 bytes of junk for every byte we want
// And also to set things up for movemask_epi8 to look at only the most significant bit
auto left_masked = _mm256_add_epi8(lefts, addition_vector);
auto right_masked = _mm256_add_epi8(rights, addition_vector);
// Pack into an int32.
auto left_packed = _mm256_movemask_epi8(left_masked);
auto right_packed = _mm256_movemask_epi8(right_masked);
auto cscs_batch = left_packed & right_packed;
auto cscn_batch = left_packed ^ right_packed;
cscs += __builtin_popcount(cscs_batch);
cscn += __builtin_popcount(cscn_batch);
}
benchmark::DoNotOptimize(cscs);
benchmark::DoNotOptimize(cscn);
}
}
BENCHMARK(BM_AVX2);
static void BM_AVX2_access_only(benchmark::State &state) {
auto pairs = make_pairs_struct_of_arrays<int32_t>();
std::vector<int8_t> phenotypes_ = make_random_bools();
for (auto _ : state) {
int64_t cscs = 0;
int64_t cscn = 0;
for (size_t i = 0; i < pairs.first.size(); i += 8) {
auto left_addresses = _mm256_loadu_si256(reinterpret_cast<const __m256i_u *>(&pairs.first[i]));
auto right_addresses = _mm256_loadu_si256(reinterpret_cast<const __m256i_u *>(&pairs.second[i]));
auto lefts = _mm256_i32gather_epi32(reinterpret_cast<const int *>(phenotypes_.data()), left_addresses, 1);
auto rights = _mm256_i32gather_epi32(reinterpret_cast<const int *>(phenotypes_.data()), right_addresses, 1);
const auto addition_vector = _mm256_setr_epi8(0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127,
0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127, 0, 0, 0, 127);
// Set the high bit on each byte only if there was a 1 there before!
// We are using this both to mask out only the bytes we care about, as we retrieved 3 bytes of junk for every byte we want
// And also to set things up for movemask_epi8 to look at only the most significant bit
auto left_masked = _mm256_add_epi8(lefts, addition_vector);
auto right_masked = _mm256_add_epi8(rights, addition_vector);
auto left_packed = _mm256_movemask_epi8(left_masked);
auto right_packed = _mm256_movemask_epi8(right_masked);
benchmark::DoNotOptimize(left_packed);
benchmark::DoNotOptimize(right_packed);
}
}
}
BENCHMARK(BM_AVX2_access_only);
BENCHMARK_MAIN();