Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canon uniform benches #1286

Closed
wants to merge 62 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
1ea5f36
Apply rustfmt to rand::distributions::uniform module
dhardy Oct 19, 2021
cea392a
Move UniformChar, UniformDuration to new submodule
dhardy Feb 24, 2022
03ddfa8
Move UniformFloat to a new submodule
dhardy Feb 24, 2022
9604f2a
Move UniformInt to a new submodule
dhardy Feb 28, 2022
f080e47
Move UniformInt SIMD implementations to new module
dhardy Feb 28, 2022
a87f4e9
UniformInt(single): add ONeill, Canon, Canon-Lemire and Bitmask methods
dhardy Feb 28, 2022
5cecc4c
Bench UniformInt(single) methods (new Criterion benchmark)
dhardy Oct 20, 2021
1fa5b7d
UniformInt(sample): add Lemire, Canon, Canon-Lemire, Bitmask methods
dhardy Oct 20, 2021
ea9949b
UniformInt(sample): add sample_canon_64 (15% faster for i128)
dhardy Oct 22, 2021
a99e9b0
UniformInt(single): add Canon 64 method
dhardy Nov 15, 2021
80643ec
Simplify Lemire's method
dhardy Feb 28, 2022
2c36d18
Bench: add documentation and TODO notes; adjust names
dhardy Feb 28, 2022
2137863
Revise impls: Canon method
dhardy Feb 28, 2022
ba8db32
Bench: add random-range variant for single samples
dhardy Mar 1, 2022
11374ee
Add unbiased Canon method
dhardy Feb 28, 2022
37d16c5
Bench: remove all but random variant for single-samples
dhardy Mar 1, 2022
639d0b2
Bench: restructure
dhardy Mar 1, 2022
91d680e
Bench: add multiple RNGs
dhardy Mar 1, 2022
91e2804
Replace canon_64 with canon_reduced
dhardy Mar 1, 2022
9910822
Uniform int impls: rename macro params to $u32_or_uty, $u64_or_uty
dhardy Mar 1, 2022
fde772a
Add Canon32
dhardy Mar 1, 2022
ac994e3
Add results of uniform-int benches
dhardy Mar 1, 2022
662adb7
Bench: add distr_random
dhardy Mar 1, 2022
1c14f6b
Add results for distr_random benchmarks
dhardy Mar 1, 2022
6bcf5ef
Bench: revise single-random benchmark
dhardy Mar 1, 2022
9f32ab7
Add revised results for single-random
dhardy Mar 1, 2022
1dea2c6
Bench: remove low- and high-reject benchmarks
dhardy Mar 1, 2022
6ba5538
Add written report (partial conclusion based on results)
dhardy Mar 2, 2022
dc7016f
Fix link
dhardy Mar 2, 2022
eadb85d
Add uniform benchmark results for Intel 1145G7
dhardy Jan 14, 2023
9719cc7
Remove Bitmask method
dhardy Feb 1, 2023
aaacba9
Add method Canon32-2
dhardy Feb 1, 2023
72f7d8c
Add new benchmark runs (also on 5800X)
dhardy Feb 2, 2023
5d5ce48
Add gen_range bench
dhardy Feb 3, 2023
6980a11
Remove uniform gen_range bench
dhardy Feb 6, 2023
80033a3
benches/uniform: new algorithm selections by size
dhardy Feb 6, 2023
b3f652f
Rename sample_canon_reduced -> sample_biased_64 for sizes i8-i32
dhardy Feb 6, 2023
76934d7
Remove canon_u32_2 for >= 32-bits
dhardy Feb 6, 2023
0ce5f08
Fix: replace nrmr, z with thresh32_or_uty and 64 variant
dhardy Feb 6, 2023
48e7af3
Remove biased Canon-Lemire method
dhardy Feb 7, 2023
d9fd6c0
Update uniform benches (intention and doc)
dhardy Feb 7, 2023
55c18fa
Add Canon32-Unbiased method
dhardy Feb 7, 2023
f71a011
Add canon_reduced_unbiased
dhardy Feb 7, 2023
6d39052
Add results of last run
dhardy Feb 7, 2023
61915ba
Add note
dhardy Feb 10, 2023
9f4ca8a
benches/uniform.rs: rename selected algorithms to sample and sample-u…
dhardy Feb 11, 2023
ae48e8c
Update criterion to 0.4.0
dhardy Feb 11, 2023
f5ac943
benches/uniform.rs: adjust for more reliable results
dhardy Feb 13, 2023
147add9
Add plots from latest runs
dhardy Feb 13, 2023
7e3ecc8
Rename files in results-uniform-int-5800X-4
dhardy Feb 15, 2023
4d0e264
Add fifth run
dhardy Feb 15, 2023
9232f85
Add Lemire64 (Lemire, 64-bit sampling for 32-bit output)
dhardy Feb 16, 2023
858e9f8
Replace Lemire with Lemire64
dhardy Feb 16, 2023
1030c3b
Remove ONeill, Biased64 samplers
dhardy Feb 10, 2023
9d24bad
Remove old samplers
dhardy Feb 10, 2023
f1486ec
Merge Canon and Canon32 variants
dhardy Feb 10, 2023
3c29fb7
Remove canon-reduced, canon32-2 variants
dhardy Feb 10, 2023
6d3cb0e
benches/uniform.rs: remove $set
dhardy Feb 16, 2023
0ad00d8
Remove sample_canon_unbiased: lemire's method is faster
dhardy Feb 16, 2023
bcfa27b
Add unbiased feature flag; unify sampling methods by name
dhardy Feb 10, 2023
0217d53
Simplify benches/uniform.rs
dhardy Feb 10, 2023
9830956
Remove large, older results from repo
dhardy Feb 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ small_rng = []
# using min-const-generics
min_const_gen = []

# Option: use unbiased sampling. By default, bias affecting no more than one in
# 2^48 samples (for uniform distributions) is accepted.
# Note: enabling this option is expected to affect reproducibility of results.
unbiased = []

[workspace]
members = [
"rand_core",
Expand All @@ -80,6 +85,12 @@ features = ["into_bits"]
libc = { version = "0.2.22", optional = true, default-features = false }

[dev-dependencies]
criterion = "0.4.0"
num-traits = "0.2.14"
rand_pcg = { path = "rand_pcg", version = "0.3.0" }
# Only to test serde1
bincode = "1.2.1"

[[bench]]
name = "uniform"
harness = false
78 changes: 78 additions & 0 deletions benches/uniform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2021 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Implement benchmarks for uniform distributions over integer types

use core::time::Duration;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use rand::distributions::uniform::{SampleRange, Uniform};
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use rand_pcg::{Pcg32, Pcg64};

const WARM_UP_TIME: Duration = Duration::from_millis(1000);
const MEASUREMENT_TIME: Duration = Duration::from_secs(3);
const SAMPLE_SIZE: usize = 100_000;
const N_RESAMPLES: usize = 10_000;

macro_rules! sample {
($R:ty, $T:ty, $U:ty, $g:expr) => {
$g.bench_function(BenchmarkId::new(stringify!($R), "single"), |b| {
let mut rng = <$R>::from_entropy();
let x = rng.gen::<$U>();
let bits = (<$T>::BITS / 2);
let mask = (1 as $U).wrapping_neg() >> bits;
let range = (x >> bits) * (x & mask);
let low = <$T>::MIN;
let high = low.wrapping_add(range as $T);

b.iter(|| (low..=high).sample_single(&mut rng));
});

$g.bench_function(BenchmarkId::new(stringify!($R), "distr"), |b| {
let mut rng = <$R>::from_entropy();
let x = rng.gen::<$U>();
let bits = (<$T>::BITS / 2);
let mask = (1 as $U).wrapping_neg() >> bits;
let range = (x >> bits) * (x & mask);
let low = <$T>::MIN;
let high = low.wrapping_add(range as $T);
let dist = Uniform::<$T>::new_inclusive(<$T>::MIN, high);

b.iter(|| dist.sample(&mut rng));
});
};

($c:expr, $T:ty, $U:ty) => {{
let mut g = $c.benchmark_group(concat!("sample", stringify!($T)));
g.sample_size(SAMPLE_SIZE);
g.warm_up_time(WARM_UP_TIME);
g.measurement_time(MEASUREMENT_TIME);
g.nresamples(N_RESAMPLES);
sample!(SmallRng, $T, $U, g);
sample!(ChaCha8Rng, $T, $U, g);
sample!(Pcg32, $T, $U, g);
sample!(Pcg64, $T, $U, g);
g.finish();
}};
}

fn sample(c: &mut Criterion) {
sample!(c, i8, u8);
sample!(c, i16, u16);
sample!(c, i32, u32);
sample!(c, i64, u64);
sample!(c, i128, u128);
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = sample
}
criterion_main!(benches);
1,658 changes: 1,658 additions & 0 deletions results-uniform-int-5800X-4/distr-random-i128.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,737 changes: 1,737 additions & 0 deletions results-uniform-int-5800X-4/distr-random-i16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,728 changes: 1,728 additions & 0 deletions results-uniform-int-5800X-4/distr-random-i32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,674 changes: 1,674 additions & 0 deletions results-uniform-int-5800X-4/distr-random-i64.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,796 changes: 1,796 additions & 0 deletions results-uniform-int-5800X-4/distr-random-i8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,670 changes: 1,670 additions & 0 deletions results-uniform-int-5800X-4/single-random-i128.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,744 changes: 1,744 additions & 0 deletions results-uniform-int-5800X-4/single-random-i16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,737 changes: 1,737 additions & 0 deletions results-uniform-int-5800X-4/single-random-i32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,682 changes: 1,682 additions & 0 deletions results-uniform-int-5800X-4/single-random-i64.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,741 changes: 1,741 additions & 0 deletions results-uniform-int-5800X-4/single-random-i8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,703 changes: 1,703 additions & 0 deletions results-uniform-int-5800X-5/distr-random-i128.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,840 changes: 1,840 additions & 0 deletions results-uniform-int-5800X-5/distr-random-i16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,096 changes: 2,096 additions & 0 deletions results-uniform-int-5800X-5/distr-random-i32-2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,788 changes: 1,788 additions & 0 deletions results-uniform-int-5800X-5/distr-random-i32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,669 changes: 1,669 additions & 0 deletions results-uniform-int-5800X-5/distr-random-i64.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,849 changes: 1,849 additions & 0 deletions results-uniform-int-5800X-5/distr-random-i8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,670 changes: 1,670 additions & 0 deletions results-uniform-int-5800X-5/single-random-i128.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,723 changes: 1,723 additions & 0 deletions results-uniform-int-5800X-5/single-random-i16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,691 changes: 1,691 additions & 0 deletions results-uniform-int-5800X-5/single-random-i32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,666 changes: 1,666 additions & 0 deletions results-uniform-int-5800X-5/single-random-i64.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,763 changes: 1,763 additions & 0 deletions results-uniform-int-5800X-5/single-random-i8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,723 changes: 1,723 additions & 0 deletions results-uniform-int-5800X-5/violin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading