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

WIP: Investigate Canon's uniform-integer sampling method #1196

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ features = ["into_bits"]
libc = { version = "0.2.22", optional = true, default-features = false }

[dev-dependencies]
criterion = { version = "0.3", features = ["html_reports"] }
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
181 changes: 181 additions & 0 deletions benches/uniform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// 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.

use criterion::{criterion_group, criterion_main};
use criterion::{BenchmarkId, Criterion};
#[cfg(feature = "simd_support")] use packed_simd::*;
use rand::distributions::uniform::{SampleUniform, Uniform, UniformSampler};

type BenchRng = rand::rngs::SmallRng;

macro_rules! bench_dist_int_group {
($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => {
for input in $inputs {
$g.bench_with_input(
BenchmarkId::new($name, input.0),
&input.1,
|b, (low, high)| {
let mut rng = BenchRng::from_entropy();
let dist = Uniform::new_inclusive(low, high);
b.iter(|| <$T as SampleUniform>::Sampler::$f(&dist.0, &mut rng))
},
);
}
};
}

macro_rules! bench_int_group {
($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => {
for input in $inputs {
$g.bench_with_input(
BenchmarkId::new($name, input.0),
&input.1,
|b, (low, high)| {
let mut rng = BenchRng::from_entropy();
b.iter(|| <$T as SampleUniform>::Sampler::$f(low, high, &mut rng))
},
);
}
$g.bench_function(BenchmarkId::new($name, "varying"), |b| {
let (low, mut high) = ($inputs[0].1 .0, $inputs[1].1 .1);
let mut rng = BenchRng::from_entropy();
b.iter(|| {
high = high.wrapping_add(1);
<$T as SampleUniform>::Sampler::$f(low, high, &mut rng)
})
});
};
}

macro_rules! bench_int {
($c:expr, $T:ty, $high:expr) => {{
let inputs = &[("high_reject", $high), ("low_reject", (-1, 2))];

let mut g = $c.benchmark_group(concat!("uniform_dist_int_", stringify!($T)));
bench_dist_int_group!("Old", $T, sample, g, inputs);
bench_dist_int_group!("Lemire", $T, sample_lemire, g, inputs);
bench_dist_int_group!("Canon", $T, sample_canon, g, inputs);
bench_dist_int_group!("Canon64", $T, sample_canon_64, g, inputs);
bench_dist_int_group!("Canon-Lemire", $T, sample_canon_lemire, g, inputs);
bench_dist_int_group!("Bitmask", $T, sample_bitmask, g, inputs);
drop(g);

let mut g = $c.benchmark_group(concat!("uniform_int_", stringify!($T)));
bench_int_group!("Old", $T, sample_single_inclusive, g, inputs);
bench_int_group!("ONeill", $T, sample_single_inclusive_oneill, g, inputs);
bench_int_group!("Canon", $T, sample_single_inclusive_canon, g, inputs);
bench_int_group!("Canon64", $T, sample_single_inclusive_canon_64, g, inputs);
bench_int_group!("Canon-Lemire", $T, sample_inclusive_canon_lemire, g, inputs);
bench_int_group!("Bitmask", $T, sample_single_inclusive_bitmask, g, inputs);
}};
}

fn uniform_int(c: &mut Criterion) {
// for i8/i16, we use 32-bit integers internally so rejection is most common near full-size
// the exact values were determined with an exhaustive search
bench_int!(c, i8, (i8::MIN, 116));
bench_int!(c, i16, (i16::MIN, 32407));
bench_int!(c, i32, (i32::MIN, 1));
bench_int!(c, i64, (i64::MIN, 1));
bench_int!(c, i128, (i128::MIN, 1));
}

#[cfg(feature = "simd_support")]
macro_rules! bench_dist_simd_group {
($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => {
for input in $inputs {
$g.bench_with_input(
BenchmarkId::new($name, input.0),
&input.1,
|b, (low, high)| {
let mut rng = BenchRng::from_entropy();
let (low, high) = (<$T>::splat(*low), <$T>::splat(*high));
let dist = Uniform::new_inclusive(low, high);
b.iter(|| <$T as SampleUniform>::Sampler::$f(&dist.0, &mut rng))
},
);
}
};
}

#[cfg(feature = "simd_support")]
macro_rules! bench_simd_group {
($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => {
for input in $inputs {
$g.bench_with_input(
BenchmarkId::new($name, input.0),
&input.1,
|b, (low, high)| {
let mut rng = BenchRng::from_entropy();
let (low, high) = (<$T>::splat(*low), <$T>::splat(*high));
b.iter(|| <$T as SampleUniform>::Sampler::$f(low, high, &mut rng))
},
);
}
$g.bench_function(BenchmarkId::new($name, "varying"), |b| {
let (low, mut high) = (<$T>::splat($inputs[0].1 .0), <$T>::splat($inputs[1].1 .1));
let mut rng = BenchRng::from_entropy();
b.iter(|| {
high += 1;
<$T as SampleUniform>::Sampler::$f(low, high, &mut rng)
})
});
};
}

#[cfg(feature = "simd_support")]
macro_rules! bench_simd {
($c:expr, $T:ty, $high:expr/*, $incr:expr*/) => {{
let inputs = &[("high_reject", $high), ("low_reject", (-1, 2))];

let mut g = $c.benchmark_group(concat!("uniform_dist_simd_", stringify!($T)));
bench_dist_simd_group!("Old", $T, sample, g, inputs);
bench_dist_simd_group!("Canon", $T, sample_canon, g, inputs);
bench_dist_simd_group!("Canon-Lemire", $T, sample_canon_lemire, g, inputs);
bench_dist_simd_group!("Bitmask", $T, sample_bitmask, g, inputs);
drop(g);

let mut g = $c.benchmark_group(concat!("uniform_int_", stringify!($T)));
bench_simd_group!("Old", $T, sample_single_inclusive, g, inputs);
bench_simd_group!("Canon", $T, sample_single_inclusive_canon, g, inputs);
bench_simd_group!("Canon-Lemire", $T, sample_inclusive_canon_lemire, g, inputs);
bench_simd_group!("Bitmask", $T, sample_single_inclusive_bitmask, g, inputs);

}};
}

#[cfg(feature = "simd_support")]
fn uniform_simd(c: &mut Criterion) {
// for i8/i16, we use 32-bit integers internally so rejection is most common near full-size
// the exact values were determined with an exhaustive search
bench_simd!(c, i8x2, (i8::MIN, 116));
bench_simd!(c, i8x4, (i8::MIN, 116));
bench_simd!(c, i8x8, (i8::MIN, 116));
// bench_simd!(c, i8x16, (i8::MIN, 116));
// bench_simd!(c, i8x32, (i8::MIN, 116));
// bench_simd!(c, i8x64, (i8::MIN, 116));
bench_simd!(c, i16x8, (i16::MIN, 32407));
// bench_simd!(c, i16x16, (i16::MIN, 32407));
bench_simd!(c, i32x4, (i32::MIN, 1));
bench_simd!(c, i32x8, (i32::MIN, 1));
bench_simd!(c, i64x2, (i64::MIN, 1));
bench_simd!(c, i64x4, (i64::MIN, 1));
bench_simd!(c, i64x8, (i64::MIN, 1));
bench_simd!(c, i128x2, (i128::MIN, 1));
bench_simd!(c, i128x4, (i128::MIN, 1));
}

#[cfg(not(feature = "simd_support"))]
fn uniform_simd(_c: &mut Criterion) {}

criterion_group! {
name = benches;
config = Criterion::default();
targets = uniform_int, uniform_simd
}
criterion_main!(benches);
69 changes: 69 additions & 0 deletions benches/uniform_simd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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.

use criterion::{criterion_group, criterion_main};
use criterion::{BenchmarkGroup, BenchmarkId, Criterion};
use rand::distributions::uniform::{SampleUniform, UniformSampler};
use rand::prelude::*;
use packed_simd::*;

type BenchRng = SmallRng;

macro_rules! bench_simd_group {
($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => {
for input in $inputs {
$g.bench_with_input(
BenchmarkId::new($name, input.0),
&input.1,
|b, (low, high)| {
let mut rng = BenchRng::from_entropy();
b.iter(|| <$T as SampleUniform>::Sampler::$f(low, high, &mut rng))
},
);
}
$g.bench_function(BenchmarkId::new($name, "varying"), |b| {
let (low, mut high) = ($inputs[0].1 .0, $inputs[1].1 .1);
let mut rng = BenchRng::from_entropy();
b.iter(|| {
high = high.wrapping_add(1);
<$T as SampleUniform>::Sampler::$f(low, high, &mut rng)
})
});
};
}

macro_rules! bench_simd {
($c:expr, $T:ty, $high:expr) => {{
let mut g = $c.benchmark_group(concat!("uniform_int_", stringify!($T)));
let inputs = &[("high_reject", $high), ("low_reject", (-1, 2))];

bench_simd_group!("Old", $T, sample_single_inclusive, g, inputs);
// bench_simd_group!("ONeill", $T, sample_single_inclusive_oneill, g, inputs);
bench_simd_group!("Canon", $T, sample_single_inclusive_canon, g, inputs);
// bench_simd_group!("Canon-Lemire", $T, sample_inclusive_canon_lemire, g, inputs);
// bench_simd_group!("Bitmask", $T, sample_single_inclusive_bitmask, g, inputs);
}};
}

fn uniform_int(c: &mut Criterion) {
// for i8/i16, we use 32-bit integers internally so rejection is most common near full-size
// the exact values were determined with an exhaustive search
bench_simd!(c, i8x16, (i8::MIN, 116));
// bench_simd!(c, i16, (i16::MIN, 32407));
// bench_simd!(c, i32, (i32::MIN, 1));
// bench_simd!(c, i64, (i64::MIN, 1));
// bench_simd!(c, i128, (i128::MIN, 1));
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = uniform_int
// targets = uniform_int_i8x16, uniform_int_i16, uniform_int_i32, uniform_int_i64, uniform_int_i128
}
criterion_main!(benches);
4 changes: 2 additions & 2 deletions src/distributions/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ simd_impl!(64, u8x8, i8x8, u16x4, i16x4, u32x2, i32x2,);
#[cfg(feature = "simd_support")]
simd_impl!(128, u8x16, i8x16, u16x8, i16x8, u32x4, i32x4, u64x2, i64x2,);
#[cfg(feature = "simd_support")]
simd_impl!(256, u8x32, i8x32, u16x16, i16x16, u32x8, i32x8, u64x4, i64x4,);
simd_impl!(256, u8x32, i8x32, u16x16, i16x16, u32x8, i32x8, u64x4, i64x4, u128x2, i128x2,);
#[cfg(feature = "simd_support")]
simd_impl!(512, u8x64, i8x64, u16x32, i16x32, u32x16, i32x16, u64x8, i64x8,);
simd_impl!(512, u8x64, i8x64, u16x32, i16x32, u32x16, i32x16, u64x8, i64x8, u128x4, i128x4,);
#[cfg(all(
feature = "simd_support",
any(target_arch = "x86", target_arch = "x86_64")
Expand Down
Loading