Skip to content

Commit bb42eae

Browse files
committed
(ml5717) Moved RNG into Simulation cog
1 parent 590dea6 commit bb42eae

File tree

54 files changed

+587
-441
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+587
-441
lines changed

float-next-after-no-std/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ mod tests_f64 {
312312
LARGEST_POS,
313313
LARGEST_NEG,
314314
];
315-
for x in values {
315+
for x in &values {
316316
assert_eq!(x.next_after(*x), *x);
317317
}
318318
}
@@ -455,8 +455,8 @@ mod tests_f32 {
455455
fn step_to_largest_is_possible() {
456456
let smaller = LARGEST_POS.next_after(NEG_INF);
457457
assert_eq!(smaller.next_after(POS_INF), LARGEST_POS);
458-
let smaller = LARGEST_NEG.next_after(POS_INF);
459-
assert_eq!(smaller.next_after(NEG_INF), LARGEST_NEG);
458+
let smaller2 = LARGEST_NEG.next_after(POS_INF);
459+
assert_eq!(smaller2.next_after(NEG_INF), LARGEST_NEG);
460460
}
461461

462462
#[test]
@@ -496,7 +496,7 @@ mod tests_f32 {
496496
LARGEST_POS,
497497
LARGEST_NEG,
498498
];
499-
for x in values {
499+
for x in &values {
500500
assert_eq!(x.next_after(*x), *x);
501501
}
502502
}

necsim-classical/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ extern crate contracts;
66
use anyhow::Result;
77
use array2d::Array2D;
88

9-
use necsim_core::cogs::LineageStore;
9+
use necsim_core::cogs::{LineageStore, RngCore};
1010
use necsim_core::reporter::Reporter;
11-
use necsim_core::rng::Rng;
1211
use necsim_core::simulation::Simulation;
1312

1413
use necsim_impls_no_std::cogs::active_lineage_sampler::classical::ClassicalActiveLineageSampler;
@@ -41,12 +40,12 @@ impl ClassicalSimulation {
4140
sample_percentage <= 1.0_f64,
4241
"0.0 <= sample_percentage <= 1.0"
4342
)]
44-
pub fn simulate(
43+
pub fn simulate<G: RngCore>(
4544
habitat: &Array2D<u32>,
4645
dispersal: &Array2D<f64>,
4746
speciation_probability_per_generation: f64,
4847
sample_percentage: f64,
49-
rng: &mut impl Rng,
48+
rng: G,
5049
reporter: &mut impl Reporter<InMemoryHabitat, InMemoryLineageReference>,
5150
) -> Result<(f64, usize)> {
5251
let habitat = InMemoryHabitat::new(habitat.clone());
@@ -59,6 +58,7 @@ impl ClassicalSimulation {
5958
let simulation = Simulation::builder()
6059
.speciation_probability_per_generation(speciation_probability_per_generation)
6160
.habitat(habitat)
61+
.rng(rng)
6262
.dispersal_sampler(dispersal_sampler)
6363
.lineage_reference(std::marker::PhantomData::<InMemoryLineageReference>)
6464
.lineage_store(lineage_store)
@@ -67,7 +67,7 @@ impl ClassicalSimulation {
6767
.active_lineage_sampler(active_lineage_sampler)
6868
.build();
6969

70-
let (time, steps) = simulation.simulate(rng, reporter);
70+
let (time, steps) = simulation.simulate(reporter);
7171

7272
Ok((time, steps))
7373
}

necsim-core/src/cogs/active_lineage_sampler.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
use super::{
22
CoalescenceSampler, DispersalSampler, EventSampler, Habitat, LineageReference, LineageStore,
3+
RngCore,
34
};
45

56
use crate::landscape::Location;
6-
use crate::rng::Rng;
77
use crate::simulation::partial::active_lineager_sampler::PartialSimulation;
88

99
#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
1010
#[contract_trait]
1111
pub trait ActiveLineageSampler<
1212
H: Habitat,
13-
D: DispersalSampler<H>,
13+
G: RngCore,
14+
D: DispersalSampler<H, G>,
1415
R: LineageReference<H>,
1516
S: LineageStore<H, R>,
16-
C: CoalescenceSampler<H, R, S>,
17-
E: EventSampler<H, D, R, S, C>,
17+
C: CoalescenceSampler<H, G, R, S>,
18+
E: EventSampler<H, G, D, R, S, C>,
1819
>: core::fmt::Debug
1920
{
2021
#[must_use]
@@ -43,8 +44,8 @@ pub trait ActiveLineageSampler<
4344
fn pop_active_lineage_location_event_time(
4445
&mut self,
4546
time: f64,
46-
simulation: &mut PartialSimulation<H, D, R, S, C, E>,
47-
rng: &mut impl Rng,
47+
simulation: &mut PartialSimulation<H, G, D, R, S, C, E>,
48+
rng: &mut G,
4849
) -> Option<(R, Location, f64)>;
4950

5051
#[debug_requires(time >= 0.0_f64, "time is non-negative")]
@@ -57,7 +58,7 @@ pub trait ActiveLineageSampler<
5758
lineage_reference: R,
5859
location: Location,
5960
time: f64,
60-
simulation: &mut PartialSimulation<H, D, R, S, C, E>,
61-
rng: &mut impl Rng,
61+
simulation: &mut PartialSimulation<H, G, D, R, S, C, E>,
62+
rng: &mut G,
6263
);
6364
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use crate::cogs::RngCore;
12
use crate::landscape::Location;
2-
use crate::rng::Rng;
33

44
use super::{Habitat, LineageReference, LineageStore};
55

66
#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
77
#[contract_trait]
8-
pub trait CoalescenceSampler<H: Habitat, R: LineageReference<H>, S: LineageStore<H, R>>:
8+
pub trait CoalescenceSampler<H: Habitat, G: RngCore, R: LineageReference<H>, S: LineageStore<H, R>>:
99
core::fmt::Debug
1010
{
1111
#[must_use]
@@ -15,6 +15,6 @@ pub trait CoalescenceSampler<H: Habitat, R: LineageReference<H>, S: LineageStore
1515
location: &Location,
1616
habitat: &H,
1717
lineage_store: &S,
18-
rng: &mut impl Rng,
18+
rng: &mut G,
1919
) -> Option<R>;
2020
}

necsim-core/src/cogs/dispersal_sampler.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1+
use crate::cogs::RngCore;
12
use crate::landscape::Location;
2-
use crate::rng::Rng;
33

44
use super::Habitat;
55

6-
pub trait DispersalSampler<H: Habitat>: core::fmt::Debug {
6+
pub trait DispersalSampler<H: Habitat, G: RngCore>: core::fmt::Debug {
77
#[must_use]
8-
fn sample_dispersal_from_location(&self, location: &Location, rng: &mut impl Rng) -> Location;
8+
fn sample_dispersal_from_location(&self, location: &Location, rng: &mut G) -> Location;
99
}
1010

1111
#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
1212
#[allow(clippy::module_name_repetitions)]
1313
#[contract_trait]
14-
pub trait SeparableDispersalSampler<H: Habitat>: DispersalSampler<H> {
14+
pub trait SeparableDispersalSampler<H: Habitat, G: RngCore>: DispersalSampler<H, G> {
1515
#[must_use]
1616
#[debug_ensures(&ret != location, "disperses to a different location")]
17-
fn sample_non_self_dispersal_from_location(
18-
&self,
19-
location: &Location,
20-
rng: &mut impl Rng,
21-
) -> Location;
17+
fn sample_non_self_dispersal_from_location(&self, location: &Location, rng: &mut G)
18+
-> Location;
2219

2320
#[must_use]
2421
#[debug_ensures(ret >= 0.0_f64 && ret <= 1.0_f64, "returns probability")]

necsim-core/src/cogs/event_sampler.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
use float_next_after::NextAfter;
22

3-
use super::{CoalescenceSampler, DispersalSampler, Habitat, LineageReference, LineageStore};
3+
use super::{
4+
CoalescenceSampler, DispersalSampler, Habitat, LineageReference, LineageStore, RngCore,
5+
};
46
use crate::event::{Event, EventType};
57
use crate::landscape::Location;
6-
use crate::rng::Rng;
78
use crate::simulation::partial::event_sampler::PartialSimulation;
89

910
#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
1011
#[contract_trait]
1112
pub trait EventSampler<
1213
H: Habitat,
13-
D: DispersalSampler<H>,
14+
G: RngCore,
15+
D: DispersalSampler<H, G>,
1416
R: LineageReference<H>,
1517
S: LineageStore<H, R>,
16-
C: CoalescenceSampler<H, R, S>,
18+
C: CoalescenceSampler<H, G, R, S>,
1719
>: core::fmt::Debug
1820
{
1921
#[must_use]
@@ -34,8 +36,8 @@ pub trait EventSampler<
3436
lineage_reference: R,
3537
location: Location,
3638
event_time: f64,
37-
simulation: &PartialSimulation<H, D, R, S, C>,
38-
rng: &mut impl Rng,
39+
simulation: &PartialSimulation<H, G, D, R, S, C>,
40+
rng: &mut G,
3941
) -> Event<H, R>;
4042

4143
#[must_use]
@@ -55,9 +57,11 @@ pub trait EventSampler<
5557
&self,
5658
lineage_reference: R,
5759
time: f64,
58-
simulation: &PartialSimulation<H, D, R, S, C>,
59-
rng: &mut impl Rng,
60+
simulation: &PartialSimulation<H, G, D, R, S, C>,
61+
rng: &mut G,
6062
) -> Event<H, R> {
63+
use crate::cogs::RngSampler;
64+
6165
let delta_time =
6266
rng.sample_exponential(simulation.speciation_probability_per_generation * 0.5_f64);
6367

necsim-core/src/cogs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod habitat;
22
pub use habitat::Habitat;
33

4+
mod rng;
5+
pub use rng::{PrimeableRng, RngCore, RngSampler};
6+
47
mod dispersal_sampler;
58
pub use dispersal_sampler::{DispersalSampler, SeparableDispersalSampler};
69

necsim-core/src/rng.rs renamed to necsim-core/src/cogs/rng.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::default::Default;
55
use core::ptr::copy_nonoverlapping;
66

77
#[allow(clippy::module_name_repetitions)]
8-
pub trait RngCore: Sized + Clone {
8+
pub trait RngCore: Sized + Clone + core::fmt::Debug {
99
type Seed: AsMut<[u8]> + Default + Sized;
1010

1111
#[must_use]
@@ -46,15 +46,10 @@ pub trait RngCore: Sized + Clone {
4646
fn sample_u64(&mut self) -> u64;
4747
}
4848

49-
pub trait IncoherentRngCore: RngCore {
50-
type Prime: AsMut<[u8]> + Sized;
51-
52-
fn prime_with(&mut self, prime: Self::Prime);
53-
}
54-
5549
#[allow(clippy::inline_always, clippy::inline_fn_without_body)]
50+
#[allow(clippy::module_name_repetitions)]
5651
#[contract_trait]
57-
pub trait Rng: RngCore {
52+
pub trait RngSampler: RngCore {
5853
#[must_use]
5954
#[inline]
6055
#[debug_ensures(ret >= 0.0_f64 && ret <= 1.0_f64, "samples U(0.0, 1.0)")]
@@ -99,4 +94,11 @@ pub trait Rng: RngCore {
9994
}
10095
}
10196

102-
impl<R: RngCore> Rng for R {}
97+
impl<R: RngCore> RngSampler for R {}
98+
99+
#[allow(clippy::module_name_repetitions)]
100+
pub trait PrimeableRng: RngCore {
101+
type Prime: AsMut<[u8]> + Sized;
102+
103+
fn prime_with(&mut self, prime: Self::Prime);
104+
}

necsim-core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ pub mod intrinsics;
2525
pub mod landscape;
2626
pub mod lineage;
2727
pub mod reporter;
28-
pub mod rng;
2928
pub mod simulation;

necsim-core/src/simulation/builder.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use core::marker::PhantomData;
55

66
use crate::cogs::{
77
ActiveLineageSampler, CoalescenceSampler, DispersalSampler, EventSampler, Habitat,
8-
LineageReference, LineageStore,
8+
LineageReference, LineageStore, RngCore,
99
};
1010

1111
#[derive(TypedBuilder, Debug)]
1212
#[cfg_attr(feature = "cuda", derive(RustToCuda, LendToCuda))]
1313
#[cfg_attr(feature = "cuda", r2cBound(H: rust_cuda::common::RustToCuda))]
14+
#[cfg_attr(feature = "cuda", r2cBound(G: rust_cuda::common::RustToCuda))]
1415
#[cfg_attr(feature = "cuda", r2cBound(D: rust_cuda::common::RustToCuda))]
1516
#[cfg_attr(feature = "cuda", r2cBound(R: rustacuda_core::DeviceCopy))]
1617
#[cfg_attr(feature = "cuda", r2cBound(S: rust_cuda::common::RustToCuda))]
@@ -19,19 +20,22 @@ use crate::cogs::{
1920
#[cfg_attr(feature = "cuda", r2cBound(A: rust_cuda::common::RustToCuda))]
2021
pub struct Simulation<
2122
H: Habitat,
22-
D: DispersalSampler<H>,
23+
G: RngCore,
24+
D: DispersalSampler<H, G>,
2325
R: LineageReference<H>,
2426
S: LineageStore<H, R>,
25-
C: CoalescenceSampler<H, R, S>,
26-
E: EventSampler<H, D, R, S, C>,
27-
A: ActiveLineageSampler<H, D, R, S, C, E>,
27+
C: CoalescenceSampler<H, G, R, S>,
28+
E: EventSampler<H, G, D, R, S, C>,
29+
A: ActiveLineageSampler<H, G, D, R, S, C, E>,
2830
> {
2931
#[builder(default = 0.0_f64, setter(skip))]
3032
pub(super) time: f64,
3133
pub(super) speciation_probability_per_generation: f64,
3234
#[cfg_attr(feature = "cuda", r2cEmbed)]
3335
pub(super) habitat: H,
3436
#[cfg_attr(feature = "cuda", r2cEmbed)]
37+
pub(super) rng: G,
38+
#[cfg_attr(feature = "cuda", r2cEmbed)]
3539
pub(super) dispersal_sampler: D,
3640
pub(super) lineage_reference: PhantomData<R>,
3741
#[cfg_attr(feature = "cuda", r2cEmbed)]
@@ -46,20 +50,22 @@ pub struct Simulation<
4650

4751
impl<
4852
H: Habitat,
49-
D: DispersalSampler<H>,
53+
G: RngCore,
54+
D: DispersalSampler<H, G>,
5055
R: LineageReference<H>,
5156
S: LineageStore<H, R>,
52-
C: CoalescenceSampler<H, R, S>,
53-
E: EventSampler<H, D, R, S, C>,
54-
A: ActiveLineageSampler<H, D, R, S, C, E>,
55-
> Simulation<H, D, R, S, C, E, A>
57+
C: CoalescenceSampler<H, G, R, S>,
58+
E: EventSampler<H, G, D, R, S, C>,
59+
A: ActiveLineageSampler<H, G, D, R, S, C, E>,
60+
> Simulation<H, G, D, R, S, C, E, A>
5661
{
57-
pub fn with_mut_split_active_lineage_sampler<
62+
pub fn with_mut_split_active_lineage_sampler_and_rng<
5863
's,
5964
Q,
6065
F: FnOnce(
6166
&'s mut A,
62-
&mut super::partial::active_lineager_sampler::PartialSimulation<'s, H, D, R, S, C, E>,
67+
&mut super::partial::active_lineager_sampler::PartialSimulation<'s, H, G, D, R, S, C, E>,
68+
&'s mut G,
6369
) -> Q,
6470
>(
6571
&'s mut self,
@@ -68,14 +74,23 @@ impl<
6874
let mut simulation = super::partial::active_lineager_sampler::PartialSimulation {
6975
speciation_probability_per_generation: &self.speciation_probability_per_generation,
7076
habitat: &self.habitat,
77+
rng: PhantomData::<G>,
7178
dispersal_sampler: &self.dispersal_sampler,
7279
lineage_reference: &self.lineage_reference,
7380
lineage_store: &mut self.lineage_store,
7481
coalescence_sampler: &self.coalescence_sampler,
7582
event_sampler: &self.event_sampler,
7683
};
7784

78-
func(&mut self.active_lineage_sampler, &mut simulation)
85+
func(
86+
&mut self.active_lineage_sampler,
87+
&mut simulation,
88+
&mut self.rng,
89+
)
90+
}
91+
92+
pub fn rng_mut(&mut self) -> &mut G {
93+
&mut self.rng
7994
}
8095

8196
pub fn active_lineage_sampler(&self) -> &A {

0 commit comments

Comments
 (0)