|
1 |
| -// Copyright 2018 Developers of the Rand project. |
2 |
| -// Copyright 2013-2018 The Rust Project Developers. |
3 |
| -// |
4 |
| -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
5 |
| -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
6 |
| -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
7 |
| -// option. This file may not be copied, modified, or distributed |
8 |
| -// except according to those terms. |
9 |
| - |
10 |
| -//! # Monte Carlo estimation of π with a chosen seed and rayon for parallelism |
11 |
| -//! |
12 |
| -//! Imagine that we have a square with sides of length 2 and a unit circle |
13 |
| -//! (radius = 1), both centered at the origin. The areas are: |
14 |
| -//! |
15 |
| -//! ```text |
16 |
| -//! area of circle = πr² = π * r * r = π |
17 |
| -//! area of square = 2² = 4 |
18 |
| -//! ``` |
19 |
| -//! |
20 |
| -//! The circle is entirely within the square, so if we sample many points |
21 |
| -//! randomly from the square, roughly π / 4 of them should be inside the circle. |
22 |
| -//! |
23 |
| -//! We can use the above fact to estimate the value of π: pick many points in |
24 |
| -//! the square at random, calculate the fraction that fall within the circle, |
25 |
| -//! and multiply this fraction by 4. |
26 |
| -//! |
27 |
| -//! Note on determinism: |
28 |
| -//! It's slightly tricky to build a parallel simulation using Rayon |
29 |
| -//! which is both efficient *and* reproducible. |
30 |
| -//! |
31 |
| -//! Rayon's ParallelIterator api does not guarantee that the work will be |
32 |
| -//! batched into identical batches on every run, so we can't simply use |
33 |
| -//! map_init to construct one RNG per Rayon batch. |
34 |
| -//! |
35 |
| -//! Instead, we do our own batching, so that a Rayon work item becomes a |
36 |
| -//! batch. Then we can fix our rng stream to the batched work item. |
37 |
| -//! Batching amortizes the cost of constructing the Rng from a fixed seed |
38 |
| -//! over BATCH_SIZE trials. Manually batching also turns out to be faster |
39 |
| -//! for the nondeterministic version of this program as well. |
40 |
| -
|
41 |
| -use rand::distr::{Distribution, Uniform}; |
42 |
| -use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng}; |
43 |
| -use rayon::prelude::*; |
44 |
| - |
45 |
| -static SEED: u64 = 0; |
46 |
| -static BATCH_SIZE: u64 = 10_000; |
47 |
| -static BATCHES: u64 = 1000; |
48 |
| - |
49 |
| -fn main() { |
50 |
| - let range = Uniform::new(-1.0f64, 1.0).unwrap(); |
51 |
| - |
52 |
| - let in_circle = (0..BATCHES) |
53 |
| - .into_par_iter() |
54 |
| - .map(|i| { |
55 |
| - let mut rng = ChaCha8Rng::seed_from_u64(SEED); |
56 |
| - // We chose ChaCha because it's fast, has suitable statistical properties for simulation, |
57 |
| - // and because it supports this set_stream() api, which lets us choose a different stream |
58 |
| - // per work item. ChaCha supports 2^64 independent streams. |
59 |
| - rng.set_stream(i); |
60 |
| - let mut count = 0; |
61 |
| - for _ in 0..BATCH_SIZE { |
62 |
| - let a = range.sample(&mut rng); |
63 |
| - let b = range.sample(&mut rng); |
64 |
| - if a * a + b * b <= 1.0 { |
65 |
| - count += 1; |
66 |
| - } |
67 |
| - } |
68 |
| - count |
69 |
| - }) |
70 |
| - .sum::<usize>(); |
71 |
| - |
72 |
| - // assert this is deterministic |
73 |
| - assert_eq!(in_circle, 7852263); |
74 |
| - |
75 |
| - // prints something close to 3.14159... |
76 |
| - println!( |
77 |
| - "π is approximately {}", |
78 |
| - 4. * (in_circle as f64) / ((BATCH_SIZE * BATCHES) as f64) |
79 |
| - ); |
80 |
| -} |
| 1 | +// Copyright 2018 Developers of the Rand project. |
| 2 | +// Copyright 2013-2018 The Rust Project Developers. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 7 | +// option. This file may not be copied, modified, or distributed |
| 8 | +// except according to those terms. |
| 9 | + |
| 10 | +//! # Monte Carlo estimation of π with a chosen seed and rayon for parallelism |
| 11 | +//! |
| 12 | +//! Imagine that we have a square with sides of length 2 and a unit circle |
| 13 | +//! (radius = 1), both centered at the origin. The areas are: |
| 14 | +//! |
| 15 | +//! ```text |
| 16 | +//! area of circle = πr² = π * r * r = π |
| 17 | +//! area of square = 2² = 4 |
| 18 | +//! ``` |
| 19 | +//! |
| 20 | +//! The circle is entirely within the square, so if we sample many points |
| 21 | +//! randomly from the square, roughly π / 4 of them should be inside the circle. |
| 22 | +//! |
| 23 | +//! We can use the above fact to estimate the value of π: pick many points in |
| 24 | +//! the square at random, calculate the fraction that fall within the circle, |
| 25 | +//! and multiply this fraction by 4. |
| 26 | +//! |
| 27 | +//! Note on determinism: |
| 28 | +//! It's slightly tricky to build a parallel simulation using Rayon |
| 29 | +//! which is both efficient *and* reproducible. |
| 30 | +//! |
| 31 | +//! Rayon's ParallelIterator api does not guarantee that the work will be |
| 32 | +//! batched into identical batches on every run, so we can't simply use |
| 33 | +//! map_init to construct one RNG per Rayon batch. |
| 34 | +//! |
| 35 | +//! Instead, we do our own batching, so that a Rayon work item becomes a |
| 36 | +//! batch. Then we can fix our rng stream to the batched work item. |
| 37 | +//! Batching amortizes the cost of constructing the Rng from a fixed seed |
| 38 | +//! over BATCH_SIZE trials. Manually batching also turns out to be faster |
| 39 | +//! for the nondeterministic version of this program as well. |
| 40 | +
|
| 41 | +use rand::distr::{Distribution, Uniform}; |
| 42 | +use rand_chacha::{ChaCha8Rng, rand_core::SeedableRng}; |
| 43 | +use rayon::prelude::*; |
| 44 | + |
| 45 | +static SEED: u64 = 0; |
| 46 | +static BATCH_SIZE: u64 = 10_000; |
| 47 | +static BATCHES: u64 = 1000; |
| 48 | + |
| 49 | +fn main() { |
| 50 | + let range = Uniform::new(-1.0f64, 1.0).unwrap(); |
| 51 | + |
| 52 | + let in_circle = (0..BATCHES) |
| 53 | + .into_par_iter() |
| 54 | + .map(|i| { |
| 55 | + let mut rng = ChaCha8Rng::seed_from_u64(SEED); |
| 56 | + // We chose ChaCha because it's fast, has suitable statistical properties for simulation, |
| 57 | + // and because it supports this set_stream() api, which lets us choose a different stream |
| 58 | + // per work item. ChaCha supports 2^64 independent streams. |
| 59 | + rng.set_stream(i); |
| 60 | + let mut count = 0; |
| 61 | + for _ in 0..BATCH_SIZE { |
| 62 | + let a = range.sample(&mut rng); |
| 63 | + let b = range.sample(&mut rng); |
| 64 | + if a * a + b * b <= 1.0 { |
| 65 | + count += 1; |
| 66 | + } |
| 67 | + } |
| 68 | + count |
| 69 | + }) |
| 70 | + .sum::<usize>(); |
| 71 | + |
| 72 | + // assert this is deterministic |
| 73 | + assert_eq!(in_circle, 7852263); |
| 74 | + |
| 75 | + // prints something close to 3.14159... |
| 76 | + println!( |
| 77 | + "π is approximately {}", |
| 78 | + 4. * (in_circle as f64) / ((BATCH_SIZE * BATCHES) as f64) |
| 79 | + ); |
| 80 | +} |
0 commit comments