-
-
Notifications
You must be signed in to change notification settings - Fork 471
Uniform float improvements #1289
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95b366f
Add uniform_float benchmark
dhardy 5e5879a
Add direct impl of sample_single_inclusive for floats
dhardy 7aedb36
Uniform for floats: extra tests
dhardy 8d98d45
Bench uniform_float: better grouping of plots
dhardy ee1653c
CI: benches require small_rng
dhardy 026292d
Merge branch 'master' into uniform-float
vks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2023 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 FP types | ||
//! | ||
//! Sampling methods compared: | ||
//! | ||
//! - sample: current method: (x12 - 1.0) * (b - a) + a | ||
|
||
use core::time::Duration; | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use rand::distributions::uniform::{SampleUniform, Uniform, UniformSampler}; | ||
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! single_random { | ||
($R:ty, $T:ty, $g:expr) => { | ||
$g.bench_function(BenchmarkId::new(stringify!($T), stringify!($R)), |b| { | ||
let mut rng = <$R>::from_entropy(); | ||
let (mut low, mut high); | ||
loop { | ||
low = <$T>::from_bits(rng.gen()); | ||
high = <$T>::from_bits(rng.gen()); | ||
if (low < high) && (high - low).is_normal() { | ||
break; | ||
} | ||
} | ||
|
||
b.iter(|| <$T as SampleUniform>::Sampler::sample_single_inclusive(low, high, &mut rng)); | ||
}); | ||
}; | ||
|
||
($c:expr, $T:ty) => {{ | ||
let mut g = $c.benchmark_group("uniform_single"); | ||
g.sample_size(SAMPLE_SIZE); | ||
g.warm_up_time(WARM_UP_TIME); | ||
g.measurement_time(MEASUREMENT_TIME); | ||
g.nresamples(N_RESAMPLES); | ||
single_random!(SmallRng, $T, g); | ||
single_random!(ChaCha8Rng, $T, g); | ||
single_random!(Pcg32, $T, g); | ||
single_random!(Pcg64, $T, g); | ||
g.finish(); | ||
}}; | ||
} | ||
|
||
fn single_random(c: &mut Criterion) { | ||
single_random!(c, f32); | ||
single_random!(c, f64); | ||
} | ||
|
||
macro_rules! distr_random { | ||
($R:ty, $T:ty, $g:expr) => { | ||
$g.bench_function(BenchmarkId::new(stringify!($T), stringify!($R)), |b| { | ||
let mut rng = <$R>::from_entropy(); | ||
let dist = loop { | ||
let low = <$T>::from_bits(rng.gen()); | ||
let high = <$T>::from_bits(rng.gen()); | ||
if let Ok(dist) = Uniform::<$T>::new_inclusive(low, high) { | ||
break dist; | ||
} | ||
}; | ||
|
||
b.iter(|| dist.sample(&mut rng)); | ||
}); | ||
}; | ||
|
||
($c:expr, $T:ty) => {{ | ||
let mut g = $c.benchmark_group("uniform_distribution"); | ||
g.sample_size(SAMPLE_SIZE); | ||
g.warm_up_time(WARM_UP_TIME); | ||
g.measurement_time(MEASUREMENT_TIME); | ||
g.nresamples(N_RESAMPLES); | ||
distr_random!(SmallRng, $T, g); | ||
distr_random!(ChaCha8Rng, $T, g); | ||
distr_random!(Pcg32, $T, g); | ||
distr_random!(Pcg64, $T, g); | ||
vks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
g.finish(); | ||
}}; | ||
} | ||
|
||
fn distr_random(c: &mut Criterion) { | ||
distr_random!(c, f32); | ||
distr_random!(c, f64); | ||
} | ||
|
||
criterion_group! { | ||
name = benches; | ||
config = Criterion::default(); | ||
targets = single_random, distr_random | ||
} | ||
criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.