-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_pass_simd_pair.cpp
More file actions
692 lines (594 loc) · 23.3 KB
/
two_pass_simd_pair.cpp
File metadata and controls
692 lines (594 loc) · 23.3 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#include <iostream>
#include <vector>
#include <algorithm>
#include <chrono>
#include <random>
#include <string>
#include <cstdint>
#include <atomic>
#include <limits>
#include <cstring>
#include <type_traits>
#include <cmath>
#include <iomanip>
#include <typeinfo>
#include <sys/mman.h> // mmap
#include <unistd.h> // sysconf
// ---------------------------------------------------------
// 请根据你的实际目录结构调整引用路径
// ---------------------------------------------------------
#include "parlay/parallel.h"
// #include "parlay/primitives.h"
#include "parlay/slice.h"
#include "parlay/sequence.h"
#include "parlay/utilities.h"
#include "src/two_pass/two_pass_simd.hpp"
#include "src/modules/utils/KVPair.hpp"
// =========================================================
// 类型配置(你可改)
// =========================================================
using KeyType = int64_t;
using ValType = int64_t;
using PairType = parlay::KVPair<KeyType, ValType>;
// =========================================================
// 小工具:flush(尽量减少并行日志交错感)
// =========================================================
static inline void flush_all_logs() {
std::cout.flush();
std::cerr.flush();
::fflush(stdout);
::fflush(stderr);
}
// =========================================================
// 内存分配 Helper (优先 Huge Pages)
// =========================================================
template <typename T>
std::pair<T*, size_t> alloc_huge(size_t n) {
size_t total_bytes = n * sizeof(T);
size_t huge_page_size = 2097152; // 2MB
size_t alloc_bytes = (total_bytes + huge_page_size - 1) & ~(huge_page_size - 1);
void* ptr = MAP_FAILED;
bool is_huge = false;
#if defined(MAP_HUGETLB)
ptr = mmap(nullptr, alloc_bytes,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (ptr != MAP_FAILED) {
is_huge = true;
}
#endif
if (ptr == MAP_FAILED) {
ptr = mmap(nullptr, alloc_bytes,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
perror("[FATAL] mmap failed");
std::exit(1);
}
#ifdef MADV_HUGEPAGE
madvise(ptr, alloc_bytes, MADV_HUGEPAGE);
#endif
}
double size_mb = alloc_bytes / 1024.0 / 1024.0;
if (is_huge) {
std::cout << " [Alloc] " << size_mb << " MB via MAP_HUGETLB (Success) ✅\n";
} else {
std::cout << " [Alloc] " << size_mb << " MB via Normal mmap + THP (Fallback) ⚠️\n";
}
return {static_cast<T*>(ptr), alloc_bytes};
}
void free_huge(void* ptr, size_t bytes) {
if (ptr && ptr != MAP_FAILED) {
munmap(ptr, bytes);
}
}
// =========================================================
// 工具:计时器
// =========================================================
class Timer {
std::chrono::high_resolution_clock::time_point start_;
public:
Timer() { reset(); }
void reset() { start_ = std::chrono::high_resolution_clock::now(); }
double elapsed() const {
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration<double>(end - start_).count();
}
};
// =========================================================
// 工具:SplitMix64(确定性)
// =========================================================
static inline uint64_t splitmix64_stateless(uint64_t x) {
uint64_t z = x + 0x9E3779B97F4A7C15ULL;
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
z = z ^ (z >> 31);
return z;
}
static inline uint64_t mix64(uint64_t x) {
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9ULL;
x ^= x >> 27;
x *= 0x94d049bb133111ebULL;
x ^= x >> 31;
return x;
}
template <typename T>
static inline uint64_t bits_u64(const T& x) {
static_assert(std::is_trivially_copyable_v<T>, "bits_u64 requires trivially copyable");
uint64_t out = 0;
std::memcpy(&out, &x, sizeof(T)); // 小于8字节则高位保持0
return out;
}
// =========================================================
// Digest(pair/key/value 三套)
// =========================================================
struct Digest {
uint64_t xr = 0;
uint64_t sum = 0;
};
struct MultiDigest {
Digest pair_d;
Digest key_d;
Digest val_d;
};
static inline void digest_accum(Digest& d, uint64_t h) {
d.xr ^= h;
d.sum += h; // uint64_t wrap-around OK
}
static inline bool digest_equal(const Digest& a, const Digest& b) {
return a.xr == b.xr && a.sum == b.sum;
}
static inline Digest digest_combine(const Digest& a, const Digest& b) {
return Digest{a.xr ^ b.xr, a.sum + b.sum};
}
static inline MultiDigest md_combine(const MultiDigest& a, const MultiDigest& b) {
return MultiDigest{
digest_combine(a.pair_d, b.pair_d),
digest_combine(a.key_d, b.key_d),
digest_combine(a.val_d, b.val_d)};
}
static inline uint64_t hash_pair(const PairType& p) {
uint64_t h1 = mix64(bits_u64(p.first));
uint64_t h2 = mix64(bits_u64(p.second));
return mix64(h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2)));
}
MultiDigest compute_multi_digest_parallel(parlay::slice<PairType*, PairType*> data, bool second_is_key) {
const size_t n = data.size();
const size_t P = std::max<size_t>(1, parlay::num_workers());
std::vector<MultiDigest> partial(P);
parlay::parallel_for(0, P, [&](size_t t) {
size_t s = (n * t) / P;
size_t e = (n * (t + 1)) / P;
MultiDigest local{};
for (size_t i = s; i < e; ++i) {
const auto& p = data[i];
const auto& key = second_is_key ? p.second : p.first;
const auto& val = second_is_key ? p.first : p.second;
digest_accum(local.pair_d, hash_pair(p));
digest_accum(local.key_d, mix64(bits_u64(key)));
digest_accum(local.val_d, mix64(bits_u64(val)));
}
partial[t] = local;
});
MultiDigest out{};
for (size_t t = 0; t < P; ++t) out = md_combine(out, partial[t]);
return out;
}
// =========================================================
// Index codec(用于 stable 时把原始下标存到“非key字段”)
// - 如果 carrier 类型不能精确容纳 0..N-1,会自动压缩 stride
// - 压缩后稳定性检查将被跳过(但仍检查 digest/order)
// =========================================================
struct IndexCodec {
size_t stride = 1; // encoded_index = i / stride
bool lossy = false; // true => 无法精确验证稳定性
std::string reason; // 说明为什么 lossy
};
template <typename T>
IndexCodec build_index_codec_for_carrier(size_t n) {
IndexCodec c{};
if constexpr (std::is_integral_v<T>) {
using Lim = std::numeric_limits<T>;
static_assert(Lim::is_integer, "integral expected");
const uint64_t cap = static_cast<uint64_t>(Lim::max()) + 1ULL; // [0, max]
if (n <= cap) {
c.stride = 1;
c.lossy = false;
return c;
}
c.stride = static_cast<size_t>(((static_cast<uint64_t>(n) - 1ULL) / cap) + 1ULL);
c.lossy = true;
c.reason = "index carrier integral range too small; compressed index";
return c;
} else if constexpr (std::is_floating_point_v<T>) {
constexpr uint64_t exact_cap =
(sizeof(T) == 4) ? (1ULL << 24) :
(sizeof(T) == 8) ? (1ULL << 53) : 0ULL;
if (exact_cap == 0) {
c.lossy = true;
c.reason = "unsupported floating carrier precision";
return c;
}
if (n <= exact_cap) {
c.stride = 1;
c.lossy = false;
return c;
}
c.stride = static_cast<size_t>(((static_cast<uint64_t>(n) - 1ULL) / exact_cap) + 1ULL);
c.lossy = true;
c.reason = "stable index not exactly representable in floating-point carrier; compressed index";
return c;
} else {
c.lossy = true;
c.reason = "non-arithmetic carrier cannot store index";
return c;
}
}
// =========================================================
// 运行配置
// =========================================================
struct RunConfig {
bool ascending = true;
bool stable = false;
bool second_is_key = false;
// 生成数据分布配置(智能默认)
size_t key_range = 0; // 0 => auto
bool unstable_random_payload = true; // unstable时非key字段写随机 payload(true)/index(false)
// 新增:每次运行的随机种子(用于让数据每次不同)
uint64_t run_seed = 0;
};
// =========================================================
// 逻辑字段访问(按 second_is_key 自动切换)
// 注意:这里假设 KeyType / ValType 可相互转换;你当前配置是 int64/int64,没问题
// =========================================================
static inline KeyType logical_key_of(const PairType& p, bool second_is_key) {
return second_is_key ? static_cast<KeyType>(p.second) : static_cast<KeyType>(p.first);
}
static inline ValType logical_val_of(const PairType& p, bool second_is_key) {
return second_is_key ? static_cast<ValType>(p.first) : static_cast<ValType>(p.second);
}
static inline void set_logical_key(PairType& p, bool second_is_key, KeyType x) {
if (second_is_key) p.second = static_cast<ValType>(x);
else p.first = static_cast<KeyType>(x);
}
static inline void set_logical_val(PairType& p, bool second_is_key, ValType x) {
if (second_is_key) p.first = static_cast<KeyType>(x);
else p.second = static_cast<ValType>(x);
}
// =========================================================
// 1) 并行数据生成(按 stable / second_is_key 自动模式)
// - stable=true : 非key字段写原始下标(必要时压缩)
// - stable=false: 非key字段写随机payload(更贴近真实情况)
// - run_seed: 每次运行不同;打印 seed 便于复现
// =========================================================
void fill_data_parallel_smart(parlay::slice<PairType*, PairType*> data,
const RunConfig& cfg,
const IndexCodec& codec) {
const size_t n = data.size();
// 智能 key_range:
// stable 测试时用小范围制造大量重复 key,强化稳定性检测;
// unstable 测试时用稍大范围,兼顾重复和吞吐。
size_t key_range = cfg.key_range;
if (key_range == 0) {
key_range = cfg.stable ? 10 : std::max<size_t>(100, n / 1000);
}
if (key_range == 0) key_range = 1;
parlay::parallel_for(0, n, [&](size_t i) {
// ✅ 把 run_seed 混入,保证每次运行数据可不同
uint64_t r0 = splitmix64_stateless(static_cast<uint64_t>(i) ^
cfg.run_seed ^
0x12345678ULL);
uint64_t r1 = splitmix64_stateless(static_cast<uint64_t>(i) ^
(cfg.run_seed + 0x9e3779b97f4a7c15ULL));
PairType p{};
// key:控制重复度
KeyType k = static_cast<KeyType>(r0 % key_range);
// value/payload:stable 用 index(可压缩),unstable 用随机值(或 index)
ValType v{};
if (cfg.stable) {
const uint64_t enc = static_cast<uint64_t>(i / codec.stride);
v = static_cast<ValType>(enc);
} else {
if (cfg.unstable_random_payload) {
v = static_cast<ValType>(r1 ^ (r1 >> 17) ^ (static_cast<uint64_t>(i) * 1315423911ULL));
} else {
v = static_cast<ValType>(i);
}
}
set_logical_key(p, cfg.second_is_key, k);
set_logical_val(p, cfg.second_is_key, v);
data[i] = p;
});
}
// =========================================================
// 2) 详细验证(自动按 stable / second_is_key 切换)
// 永远检查:digest + key order
// 可选检查:stability(若 codec.lossy 则自动跳过)
// =========================================================
struct VerifyReport {
bool pair_digest_ok = true;
bool key_digest_ok = true;
bool val_digest_ok = true;
bool key_order_ok = true;
size_t key_order_errors = 0;
size_t first_key_order_fail = std::numeric_limits<size_t>::max();
bool stability_checked = false;
bool value_stability_ok = true;
size_t stability_errors = 0;
size_t first_stability_fail = std::numeric_limits<size_t>::max();
MultiDigest actual_md{};
};
static void print_digest_line(const char* prefix, const Digest& d) {
std::cout << prefix
<< "xor=0x" << std::hex << d.xr
<< " sum=0x" << d.sum
<< std::dec << "\n";
}
static void print_neighborhood(parlay::slice<PairType*, PairType*> data,
bool second_is_key,
size_t boundary_i,
size_t radius) {
const size_t n = data.size();
if (n == 0 || boundary_i >= n - 1) return;
size_t L = (boundary_i > radius) ? (boundary_i - radius) : 0;
size_t R = std::min(n - 1, boundary_i + 1 + radius);
std::cout << " ---- Neighborhood [" << L << ", " << R
<< "] around boundary (" << boundary_i << ", " << (boundary_i + 1) << ") ----\n";
std::cout << " " << std::setw(6) << "idx"
<< std::setw(18) << "raw.first"
<< std::setw(18) << "raw.second"
<< std::setw(18) << "logical_key"
<< std::setw(18) << "logical_val"
<< "mark\n";
for (size_t i = L; i <= R; ++i) {
const auto& p = data[i];
auto key = logical_key_of(p, second_is_key);
auto val = logical_val_of(p, second_is_key);
std::cout << " " << std::setw(6) << i
<< std::setw(18) << p.first
<< std::setw(18) << p.second
<< std::setw(18) << key
<< std::setw(18) << val;
if (i == boundary_i) std::cout << "<i>";
if (i == boundary_i + 1) std::cout << "<i+1>";
std::cout << "\n";
}
}
VerifyReport verify_detailed_smart(parlay::slice<PairType*, PairType*> data,
const RunConfig& cfg,
const MultiDigest& expected_md,
bool check_stability,
bool print_error_window = true,
size_t error_radius = 4) {
VerifyReport rep{};
const size_t n = data.size();
rep.stability_checked = check_stability;
// 1) digest(pair / key / value)
rep.actual_md = compute_multi_digest_parallel(data, cfg.second_is_key);
rep.pair_digest_ok = digest_equal(rep.actual_md.pair_d, expected_md.pair_d);
rep.key_digest_ok = digest_equal(rep.actual_md.key_d, expected_md.key_d);
rep.val_digest_ok = digest_equal(rep.actual_md.val_d, expected_md.val_d);
// 2) key order + stability
if (n >= 2) {
const size_t M = n - 1;
const size_t P = std::max<size_t>(1, parlay::num_workers());
struct LocalErr {
size_t key_order_errors = 0;
size_t first_key_order_fail = std::numeric_limits<size_t>::max();
size_t stability_errors = 0;
size_t first_stability_fail = std::numeric_limits<size_t>::max();
};
std::vector<LocalErr> partial(P);
parlay::parallel_for(0, P, [&](size_t t) {
size_t s = (M * t) / P;
size_t e = (M * (t + 1)) / P;
LocalErr le{};
for (size_t i = s; i < e; ++i) {
const auto& a = data[i];
const auto& b = data[i + 1];
auto k1 = logical_key_of(a, cfg.second_is_key);
auto k2 = logical_key_of(b, cfg.second_is_key);
bool order_bad = cfg.ascending ? (k1 > k2) : (k1 < k2);
if (order_bad) {
le.key_order_errors++;
if (le.first_key_order_fail == std::numeric_limits<size_t>::max()) {
le.first_key_order_fail = i;
}
}
if (check_stability && !order_bad && (k1 == k2)) {
auto v1 = logical_val_of(a, cfg.second_is_key);
auto v2 = logical_val_of(b, cfg.second_is_key);
if (v1 > v2) {
le.stability_errors++;
if (le.first_stability_fail == std::numeric_limits<size_t>::max()) {
le.first_stability_fail = i;
}
}
}
}
partial[t] = le;
});
for (const auto& le : partial) {
rep.key_order_errors += le.key_order_errors;
rep.stability_errors += le.stability_errors;
if (le.first_key_order_fail < rep.first_key_order_fail) {
rep.first_key_order_fail = le.first_key_order_fail;
}
if (le.first_stability_fail < rep.first_stability_fail) {
rep.first_stability_fail = le.first_stability_fail;
}
}
}
rep.key_order_ok = (rep.key_order_errors == 0);
rep.value_stability_ok = (!check_stability || rep.stability_errors == 0);
// 3) 打印详细信息(失败时)
bool any_fail = !(rep.pair_digest_ok && rep.key_digest_ok && rep.val_digest_ok &&
rep.key_order_ok && rep.value_stability_ok);
if (any_fail) {
std::cout << " [VERIFY DETAIL]\n";
std::cout << " [PAIR checksum] " << (rep.pair_digest_ok ? "OK" : "MISMATCH") << "\n";
if (!rep.pair_digest_ok) {
print_digest_line(" expected: ", expected_md.pair_d);
print_digest_line(" actual : ", rep.actual_md.pair_d);
}
std::cout << " [KEY checksum] " << (rep.key_digest_ok ? "OK" : "MISMATCH") << "\n";
if (!rep.key_digest_ok) {
print_digest_line(" expected: ", expected_md.key_d);
print_digest_line(" actual : ", rep.actual_md.key_d);
}
std::cout << " [VALUE checksum] " << (rep.val_digest_ok ? "OK" : "MISMATCH") << "\n";
if (!rep.val_digest_ok) {
print_digest_line(" expected: ", expected_md.val_d);
print_digest_line(" actual : ", rep.actual_md.val_d);
}
if (rep.key_order_ok) {
std::cout << " [KEY order] OK\n";
} else {
std::cout << " [KEY order] FAIL (errors=" << rep.key_order_errors << ")\n";
std::cout << " [First KEY order violation @ boundary i=" << rep.first_key_order_fail
<< ", i+1=" << (rep.first_key_order_fail + 1) << "]\n";
if (print_error_window) {
print_neighborhood(data, cfg.second_is_key, rep.first_key_order_fail, error_radius);
}
}
if (!check_stability) {
std::cout << " [VALUE stability] N/A (check skipped)\n";
} else if (rep.value_stability_ok) {
std::cout << " [VALUE stability] OK\n";
} else {
std::cout << " [VALUE stability] FAIL (errors=" << rep.stability_errors << ")\n";
std::cout << " [First VALUE stability violation @ boundary i=" << rep.first_stability_fail
<< ", i+1=" << (rep.first_stability_fail + 1) << "]\n";
if (print_error_window) {
print_neighborhood(data, cfg.second_is_key, rep.first_stability_fail, error_radius);
}
}
}
return rep;
}
// =========================================================
// 单次测试(智能版)
// =========================================================
bool run_test_smart(size_t N, const RunConfig& cfg) {
std::cout << "------------------------------------------------\n";
std::cout << "Running Test: "
<< (cfg.ascending ? "Asc" : "Desc")
<< " | stable=" << cfg.stable
<< " | second_is_key=" << cfg.second_is_key
<< " | N=" << N << "\n";
// 1) stable 时决定 index codec(存到非key字段)
bool verify_stability = cfg.stable;
IndexCodec codec{};
if (cfg.stable) {
// stable 时非key字段存 index
// second_is_key=false => non-key 在 second => carrier=ValType
// second_is_key=true => non-key 在 first => carrier=KeyType
codec = cfg.second_is_key ? build_index_codec_for_carrier<KeyType>(N)
: build_index_codec_for_carrier<ValType>(N);
if (codec.lossy) {
verify_stability = false; // 仅跳过稳定性验证,不跳过排序/损坏/顺序检查
std::cout << " [NOTE] skip stability-check only: " << codec.reason
<< " (stride=" << codec.stride << ")\n";
}
}
// 2) 分配
auto [raw_ptr, alloc_bytes] = alloc_huge<PairType>(N);
auto data_slice = parlay::make_slice(raw_ptr, raw_ptr + N);
// 3) 生成数据(智能模式)
fill_data_parallel_smart(data_slice, cfg, codec);
// 4) 排序前摘要
MultiDigest before_md = compute_multi_digest_parallel(data_slice, cfg.second_is_key);
// 5) 执行排序
std::cout << " Sorting... " << std::flush;
Timer t_sort;
parlay::internal_simd::sample_sort_inplace(
data_slice, cfg.ascending, cfg.stable, cfg.second_is_key);
double sort_sec = t_sort.elapsed();
flush_all_logs();
double mops = (sort_sec > 0.0) ? ((static_cast<double>(N) / sort_sec) / 1e6) : 0.0;
std::cout << "Done.\n";
std::cout << " >>> Sort Time: " << sort_sec << " s (" << mops << " Mops/s) <<<\n";
// 6) 验证
std::cout << " Verifying... " << std::flush;
Timer t_check;
VerifyReport rep = verify_detailed_smart(
data_slice, cfg, before_md, verify_stability,
/*print_error_window=*/true,
/*error_radius=*/4);
bool pass = rep.pair_digest_ok &&
rep.key_digest_ok &&
rep.val_digest_ok &&
rep.key_order_ok &&
(!verify_stability || rep.value_stability_ok);
if (pass) {
if (cfg.stable && !verify_stability) {
std::cout << "\033[1;33m[PASS*]\033[0m"
<< " (stable sort executed; stability-check skipped) "
<< "(" << t_check.elapsed() << "s)\n";
} else {
std::cout << "\033[1;32m[PASS]\033[0m"
<< " (" << t_check.elapsed() << "s)\n";
}
} else {
std::cout << "\033[1;31m[FAIL]\033[0m\n";
}
// 7) 释放内存
free_huge(raw_ptr, alloc_bytes);
return pass;
}
// =========================================================
// Main
// 用法:
// ./a.out [N] [stable:0/1] [second_is_key:0/1] [ascending:0/1] [seed(optional)]
//
// 示例:
// ./a.out 100000000 0 1 1 # 每次随机seed(默认)
// ./a.out 100000000 0 1 1 123456789 # 固定seed复现
// ./a.out 100000000 1 1 1 # stable + second_is_key
//
// 若只给 N,则默认:stable=false, second_is_key=true, ascending=true
// =========================================================
int main(int argc, char* argv[]) {
size_t N = 100000000;
RunConfig cfg;
cfg.ascending = true;
cfg.stable = false;
cfg.second_is_key = true; // 你最近在定位这个路径,默认给它
cfg.key_range = 1000000000; // auto
cfg.unstable_random_payload = true;
if (argc > 1) {
try { N = std::stoull(argv[1]); }
catch (...) {
std::cerr << "Invalid N argument\n";
return 1;
}
}
if (argc > 2) cfg.stable = (std::stoi(argv[2]) != 0);
if (argc > 3) cfg.second_is_key = (std::stoi(argv[3]) != 0);
if (argc > 4) cfg.ascending = (std::stoi(argv[4]) != 0);
// ✅ 默认每次运行随机 seed;用户可通过第5个参数覆盖以复现*.
if (argc > 5) {
try {
cfg.run_seed = static_cast<uint64_t>(std::stoull(argv[5]));
} catch (...) {
std::cerr << "Invalid seed argument\n";
return 1;
}
} else {
std::random_device rd;
uint64_t hi = static_cast<uint64_t>(rd());
uint64_t lo = static_cast<uint64_t>(rd());
cfg.run_seed = (hi << 32) ^ lo ^ static_cast<uint64_t>(
std::chrono::high_resolution_clock::now().time_since_epoch().count());
cfg.run_seed = 888555;
}
std::cout << "=== Smart sample_sort correctness/perf smoke test ===\n";
std::cout << " PairType = KVPair<" << typeid(KeyType).name()
<< "," << typeid(ValType).name() << ">\n";
std::cout << " run_seed = " << cfg.run_seed << "\n";
bool ok = run_test_smart(N, cfg);
std::cout << "------------------------------------------------\n";
std::cout << "Summary: " << (ok ? "PASS" : "FAIL") << "\n";
return ok ? 0 : 1;
}