Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Feb 24, 2022
1 parent b912a09 commit a9d9a88
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
3 changes: 1 addition & 2 deletions benches/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use criterion::{criterion_group, criterion_main};
use criterion::{BenchmarkId, Criterion};
#[cfg(feature = "simd_support")] use packed_simd::*;
use rand::distributions::uniform::{SampleUniform, Uniform, UniformSampler};
use rand::prelude::*;

type BenchRng = SmallRng;
type BenchRng = rand::rngs::SmallRng;

macro_rules! bench_dist_int_group {
($name:literal, $T:ty, $f:ident, $g:expr, $inputs:expr) => {
Expand Down
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);
3 changes: 1 addition & 2 deletions src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ pub use uniform_other::{UniformChar, UniformDuration};
feature = "serde1",
serde(bound(deserialize = "X::Sampler: Deserialize<'de>"))
)]
// HACK: field is pub
pub struct Uniform<X: SampleUniform>(pub X::Sampler);
pub struct Uniform<X: SampleUniform>(X::Sampler);

impl<X: SampleUniform> Uniform<X> {
/// Create a new `Uniform` instance which samples uniformly from the half
Expand Down

0 comments on commit a9d9a88

Please sign in to comment.