|
1 |
| -use criterion::{criterion_group, criterion_main, Criterion, SamplingMode, Throughput}; |
2 |
| -use lib::search::{Depth, Engine, Limits, Options, ThreadCount}; |
| 1 | +#![feature(custom_test_frameworks)] |
| 2 | +#![test_runner(criterion::runner)] |
| 3 | + |
| 4 | +use criterion::{Criterion, SamplingMode, Throughput}; |
| 5 | +use criterion_macro::criterion; |
| 6 | +use lib::search::{Depth, Engine, Limits, Options}; |
3 | 7 | use lib::{nnue::Evaluator, util::Integer};
|
4 |
| -use std::thread::available_parallelism; |
5 | 8 | use std::time::{Duration, Instant};
|
| 9 | +use std::{str::FromStr, thread::available_parallelism}; |
| 10 | + |
| 11 | +#[ctor::ctor] |
| 12 | +static POSITION: Evaluator = |
| 13 | + Evaluator::from_str("6br/1KNp1n1r/2p2p2/P1ppRP2/1kP3pP/3PBB2/PN1P4/8 w - - 0 1").unwrap(); |
6 | 14 |
|
7 |
| -fn bencher(reps: u64, positions: &[Evaluator], options: Options, limits: Limits) -> Duration { |
| 15 | +fn bench(reps: u64, options: Options, limits: Limits) -> Duration { |
8 | 16 | let mut time = Duration::ZERO;
|
9 | 17 |
|
10 |
| - for pos in positions { |
11 |
| - for _ in 0..reps { |
12 |
| - let mut e = Engine::with_options(options); |
13 |
| - let timer = Instant::now(); |
14 |
| - e.search(pos, limits); |
15 |
| - time += timer.elapsed(); |
16 |
| - } |
| 18 | + for _ in 0..reps { |
| 19 | + let mut e = Engine::with_options(options); |
| 20 | + let timer = Instant::now(); |
| 21 | + e.search(&POSITION, limits); |
| 22 | + time += timer.elapsed(); |
17 | 23 | }
|
18 | 24 |
|
19 | 25 | time
|
20 | 26 | }
|
21 | 27 |
|
22 |
| -fn bench(c: &mut Criterion) { |
23 |
| - let positions: Vec<Evaluator> = FENS.iter().map(|p| p.parse().unwrap()).collect(); |
24 |
| - let options = match available_parallelism() { |
25 |
| - Err(_) => Options::default(), |
26 |
| - Ok(cores) => match cores.get() / 2 { |
27 |
| - 0 => Options::default(), |
28 |
| - threads => Options { |
29 |
| - threads: ThreadCount::new(threads), |
30 |
| - ..Options::default() |
31 |
| - }, |
32 |
| - }, |
| 28 | +#[criterion] |
| 29 | +fn crit(c: &mut Criterion) { |
| 30 | + let thread_limit = match available_parallelism() { |
| 31 | + Ok(cores) => cores.get().div_ceil(2), |
| 32 | + Err(_) => 1, |
33 | 33 | };
|
34 | 34 |
|
35 |
| - let depth = Depth::new(7); |
36 |
| - c.benchmark_group("search") |
37 |
| - .sampling_mode(SamplingMode::Flat) |
38 |
| - .bench_function("ttd", |b| { |
39 |
| - b.iter_custom(|i| bencher(i, &positions, options, depth.into())) |
40 |
| - }); |
| 35 | + let options = Vec::from_iter((0..=thread_limit.ilog2()).map(|threads| Options { |
| 36 | + threads: 2usize.pow(threads).saturate(), |
| 37 | + ..Options::default() |
| 38 | + })); |
| 39 | + |
| 40 | + for &o in &options { |
| 41 | + let depth = Depth::new(14); |
| 42 | + c.benchmark_group("ttd") |
| 43 | + .sampling_mode(SamplingMode::Flat) |
| 44 | + .sample_size(10 * o.threads.get()) |
| 45 | + .bench_function(o.threads.to_string(), |b| { |
| 46 | + b.iter_custom(|i| bench(i, o, depth.into())) |
| 47 | + }); |
| 48 | + } |
41 | 49 |
|
42 |
| - let nodes = 10000; |
43 |
| - c.benchmark_group("search") |
44 |
| - .sampling_mode(SamplingMode::Flat) |
45 |
| - .throughput(Throughput::Elements(nodes * positions.len() as u64)) |
46 |
| - .bench_function("nps", |b| { |
47 |
| - b.iter_custom(|i| bencher(i, &positions, options, nodes.into())) |
48 |
| - }); |
| 50 | + for &o in &options { |
| 51 | + let nodes = 500_000; |
| 52 | + c.benchmark_group("nps") |
| 53 | + .sampling_mode(SamplingMode::Flat) |
| 54 | + .sample_size(10 * o.threads.get()) |
| 55 | + .throughput(Throughput::Elements(nodes)) |
| 56 | + .bench_function(o.threads.to_string(), |b| { |
| 57 | + b.iter_custom(|i| bench(i, o, nodes.into())) |
| 58 | + }); |
| 59 | + } |
49 | 60 | }
|
50 |
| - |
51 |
| -criterion_group!(benches, bench); |
52 |
| -criterion_main!(benches); |
53 |
| - |
54 |
| -// https://www.chessprogramming.org/CCR_One_Hour_Test |
55 |
| -const FENS: &[&str] = &[ |
56 |
| - "rn1qkb1r/pp2pppp/5n2/3p1b2/3P4/2N1P3/PP3PPP/R1BQKBNR w KQkq - 0 1", |
57 |
| - "rn1qkb1r/pp2pppp/5n2/3p1b2/3P4/1QN1P3/PP3PPP/R1B1KBNR b KQkq - 1 1", |
58 |
| - "r1bqk2r/ppp2ppp/2n5/4P3/2Bp2n1/5N1P/PP1N1PP1/R2Q1RK1 b kq - 1 10", |
59 |
| - "r1bqrnk1/pp2bp1p/2p2np1/3p2B1/3P4/2NBPN2/PPQ2PPP/1R3RK1 w - - 1 12", |
60 |
| - "rnbqkb1r/ppp1pppp/5n2/8/3PP3/2N5/PP3PPP/R1BQKBNR b KQkq - 3 5", |
61 |
| - "rnbq1rk1/pppp1ppp/4pn2/8/1bPP4/P1N5/1PQ1PPPP/R1B1KBNR b KQ - 1 5", |
62 |
| - "r4rk1/3nppbp/bq1p1np1/2pP4/8/2N2NPP/PP2PPB1/R1BQR1K1 b - - 1 12", |
63 |
| - "rn1qkb1r/pb1p1ppp/1p2pn2/2p5/2PP4/5NP1/PP2PPBP/RNBQK2R w KQkq c6 1 6", |
64 |
| - "r1bq1rk1/1pp2pbp/p1np1np1/3Pp3/2P1P3/2N1BP2/PP4PP/R1NQKB1R b KQ - 1 9", |
65 |
| - "rnbqr1k1/1p3pbp/p2p1np1/2pP4/4P3/2N5/PP1NBPPP/R1BQ1RK1 w - - 1 11", |
66 |
| - "rnbqkb1r/pppp1ppp/5n2/4p3/4PP2/2N5/PPPP2PP/R1BQKBNR b KQkq f3 1 3", |
67 |
| - "r1bqk1nr/pppnbppp/3p4/8/2BNP3/8/PPP2PPP/RNBQK2R w KQkq - 2 6", |
68 |
| - "rnbq1b1r/ppp2kpp/3p1n2/8/3PP3/8/PPP2PPP/RNBQKB1R b KQ d3 1 5", |
69 |
| - "rnbqkb1r/pppp1ppp/3n4/8/2BQ4/5N2/PPP2PPP/RNB2RK1 b kq - 1 6", |
70 |
| - "r2q1rk1/2p1bppp/p2p1n2/1p2P3/4P1b1/1nP1BN2/PP3PPP/RN1QR1K1 w - - 1 12", |
71 |
| - "r1bqkb1r/2pp1ppp/p1n5/1p2p3/3Pn3/1B3N2/PPP2PPP/RNBQ1RK1 b kq - 2 7", |
72 |
| - "r2qkbnr/2p2pp1/p1pp4/4p2p/4P1b1/5N1P/PPPP1PP1/RNBQ1RK1 w kq - 1 8", |
73 |
| - "r1bqkb1r/pp3ppp/2np1n2/4p1B1/3NP3/2N5/PPP2PPP/R2QKB1R w KQkq e6 1 7", |
74 |
| - "rn1qk2r/1b2bppp/p2ppn2/1p6/3NP3/1BN5/PPP2PPP/R1BQR1K1 w kq - 5 10", |
75 |
| - "r1b1kb1r/1pqpnppp/p1n1p3/8/3NP3/2N1B3/PPP1BPPP/R2QK2R w KQkq - 3 8", |
76 |
| - "r1bqnr2/pp1ppkbp/4N1p1/n3P3/8/2N1B3/PPP2PPP/R2QK2R b KQ - 2 11", |
77 |
| - "r3kb1r/pp1n1ppp/1q2p3/n2p4/3P1Bb1/2PB1N2/PPQ2PPP/RN2K2R w KQkq - 3 11", |
78 |
| - "r1bq1rk1/pppnnppp/4p3/3pP3/1b1P4/2NB3N/PPP2PPP/R1BQK2R w KQ - 3 7", |
79 |
| - "r2qkbnr/ppp1pp1p/3p2p1/3Pn3/4P1b1/2N2N2/PPP2PPP/R1BQKB1R w KQkq - 2 6", |
80 |
| - "rn2kb1r/pp2pppp/1qP2n2/8/6b1/1Q6/PP1PPPBP/RNB1K1NR b KQkq - 1 6", |
81 |
| -]; |
0 commit comments