Skip to content

Commit 1f535b1

Browse files
committed
(ml5717) Refactored PrimeableRng
1 parent a1ad6b7 commit 1f535b1

File tree

8 files changed

+48
-48
lines changed

8 files changed

+48
-48
lines changed

necsim-core/src/cogs/rng.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use crate::cogs::HabitatToU64Injection;
12
use crate::intrinsics::{floor, ln};
3+
use crate::landscape::IndexedLocation;
24

35
use core::convert::AsMut;
46
use core::default::Default;
@@ -97,8 +99,6 @@ pub trait RngSampler: RngCore {
9799
impl<R: RngCore> RngSampler for R {}
98100

99101
#[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);
102+
pub trait PrimeableRng<H: HabitatToU64Injection>: RngCore {
103+
fn prime_with(&mut self, habitat: &H, indexed_location: &IndexedLocation, time_index: u64);
104104
}

necsim-cuda/kernel/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ impl core::fmt::Debug for F64 {
4040
}
4141

4242
use necsim_core::cogs::{
43-
ActiveLineageSampler, CoalescenceSampler, DispersalSampler, EventSampler, Habitat,
44-
IncoherentLineageStore, LineageReference, PrimeableRng,
43+
ActiveLineageSampler, CoalescenceSampler, DispersalSampler, EventSampler,
44+
HabitatToU64Injection, IncoherentLineageStore, LineageReference, PrimeableRng,
4545
};
4646
use necsim_core::reporter::NullReporter;
4747
use necsim_core::simulation::Simulation;
@@ -84,8 +84,8 @@ pub unsafe extern "ptx-kernel" fn simulate(
8484
}
8585

8686
unsafe fn simulate_generic<
87-
H: Habitat + RustToCuda,
88-
G: PrimeableRng<Prime = [u8; 16]> + RustToCuda,
87+
H: HabitatToU64Injection + RustToCuda,
88+
G: PrimeableRng<H> + RustToCuda,
8989
D: DispersalSampler<H, G> + RustToCuda,
9090
R: LineageReference<H> + DeviceCopy,
9191
S: IncoherentLineageStore<H, R> + RustToCuda,

necsim-cuda/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl CudaSimulation {
149149
sample_percentage <= 1.0_f64,
150150
"0.0 <= sample_percentage <= 1.0"
151151
)]
152-
pub fn simulate<G: PrimeableRng<Prime = [u8; 16]>>(
152+
pub fn simulate<G: PrimeableRng<InMemoryHabitat>>(
153153
habitat: &Array2D<u32>,
154154
dispersal: &Array2D<f64>,
155155
speciation_probability_per_generation: f64,

necsim-impls-no-std/src/cogs/active_lineage_sampler/independent/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod sampler;
1616
#[derive(Debug)]
1717
pub struct IndependentActiveLineageSampler<
1818
H: HabitatToU64Injection,
19-
G: PrimeableRng<Prime = [u8; 16]>,
19+
G: PrimeableRng<H>,
2020
D: DispersalSampler<H, G>,
2121
R: LineageReference<H>,
2222
S: IncoherentLineageStore<H, R>,
@@ -28,7 +28,7 @@ pub struct IndependentActiveLineageSampler<
2828

2929
impl<
3030
H: HabitatToU64Injection,
31-
G: PrimeableRng<Prime = [u8; 16]>,
31+
G: PrimeableRng<H>,
3232
D: DispersalSampler<H, G>,
3333
R: LineageReference<H>,
3434
S: IncoherentLineageStore<H, R>,
@@ -47,7 +47,7 @@ impl<
4747

4848
impl<
4949
H: HabitatToU64Injection,
50-
G: PrimeableRng<Prime = [u8; 16]>,
50+
G: PrimeableRng<H>,
5151
D: DispersalSampler<H, G>,
5252
R: LineageReference<H>,
5353
S: IncoherentLineageStore<H, R>,

necsim-impls-no-std/src/cogs/active_lineage_sampler/independent/sampler.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::IndependentActiveLineageSampler;
1616
#[contract_trait]
1717
impl<
1818
H: HabitatToU64Injection,
19-
G: PrimeableRng<Prime = [u8; 16]>,
19+
G: PrimeableRng<H>,
2020
D: DispersalSampler<H, G>,
2121
R: LineageReference<H>,
2222
S: IncoherentLineageStore<H, R>,
@@ -83,30 +83,7 @@ impl<
8383
let mut time_step = floor(time / delta_t) as u64 + 1;
8484

8585
loop {
86-
let location_bytes = simulation
87-
.habitat
88-
.map_indexed_location_to_u64_injective(&lineage_indexed_location)
89-
.to_le_bytes();
90-
let time_step_bytes = time_step.to_le_bytes();
91-
92-
rng.prime_with([
93-
location_bytes[0],
94-
location_bytes[1],
95-
location_bytes[2],
96-
location_bytes[3],
97-
location_bytes[4],
98-
location_bytes[5],
99-
location_bytes[6],
100-
location_bytes[7],
101-
time_step_bytes[0],
102-
time_step_bytes[1],
103-
time_step_bytes[2],
104-
time_step_bytes[3],
105-
time_step_bytes[4],
106-
time_step_bytes[5],
107-
time_step_bytes[6],
108-
time_step_bytes[7],
109-
]);
86+
rng.prime_with(simulation.habitat, &lineage_indexed_location, time_step);
11087

11188
if rng.sample_event(p) {
11289
break;

necsim-impls-no-std/src/cogs/rng/aes.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use necsim_core::cogs::HabitatToU64Injection;
2+
use necsim_core::landscape::IndexedLocation;
3+
14
use aes_soft::cipher::generic_array::GenericArray;
25
use aes_soft::cipher::{BlockCipher, NewBlockCipher};
36
use aes_soft::Aes128;
@@ -63,11 +66,32 @@ impl necsim_core::cogs::RngCore for AesRng {
6366
}
6467
}
6568

66-
impl necsim_core::cogs::PrimeableRng for AesRng {
67-
type Prime = [u8; 16];
69+
impl<H: HabitatToU64Injection> necsim_core::cogs::PrimeableRng<H> for AesRng {
70+
fn prime_with(&mut self, habitat: &H, indexed_location: &IndexedLocation, time_index: u64) {
71+
let location_bytes = habitat
72+
.map_indexed_location_to_u64_injective(indexed_location)
73+
.to_le_bytes();
74+
75+
self.state[0] = location_bytes[0];
76+
self.state[1] = location_bytes[1];
77+
self.state[2] = location_bytes[2];
78+
self.state[3] = location_bytes[3];
79+
self.state[4] = location_bytes[4];
80+
self.state[5] = location_bytes[5];
81+
self.state[6] = location_bytes[6];
82+
self.state[7] = location_bytes[7];
83+
84+
let time_index_bytes = time_index.to_le_bytes();
85+
86+
self.state[8] = time_index_bytes[0];
87+
self.state[9] = time_index_bytes[1];
88+
self.state[10] = time_index_bytes[2];
89+
self.state[11] = time_index_bytes[3];
90+
self.state[12] = time_index_bytes[4];
91+
self.state[13] = time_index_bytes[5];
92+
self.state[14] = time_index_bytes[6];
93+
self.state[15] = time_index_bytes[7];
6894

69-
fn prime_with(&mut self, prime: Self::Prime) {
70-
self.state = prime;
7195
self.cached = false;
7296
}
7397
}

necsim-impls-no-std/src/cogs/rng/cuda.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use necsim_core::cogs::{PrimeableRng, RngCore};
1+
use necsim_core::cogs::{HabitatToU64Injection, PrimeableRng, RngCore};
2+
use necsim_core::landscape::IndexedLocation;
23

34
#[allow(clippy::module_name_repetitions)]
45
#[derive(Clone, Debug, RustToCuda, LendToCuda)]
@@ -28,10 +29,8 @@ impl<R: RngCore> RngCore for CudaRng<R> {
2829
}
2930
}
3031

31-
impl<R: PrimeableRng> PrimeableRng for CudaRng<R> {
32-
type Prime = <R as PrimeableRng>::Prime;
33-
34-
fn prime_with(&mut self, prime: Self::Prime) {
35-
self.0.prime_with(prime)
32+
impl<H: HabitatToU64Injection, R: PrimeableRng<H>> PrimeableRng<H> for CudaRng<R> {
33+
fn prime_with(&mut self, habitat: &H, indexed_location: &IndexedLocation, time_index: u64) {
34+
self.0.prime_with(habitat, indexed_location, time_index)
3635
}
3736
}

rustcoalescence/src/simulation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use necsim_skipping_gillespie::SkippingGillespieSimulation;
1212

1313
use super::args::{Algorithm, CommandLineArguments};
1414

15-
pub fn simulate<G: PrimeableRng<Prime = [u8; 16]>>(
15+
pub fn simulate<G: PrimeableRng<InMemoryHabitat>>(
1616
args: &CommandLineArguments,
1717
habitat: &Array2D<u32>,
1818
dispersal: &Array2D<f64>,

0 commit comments

Comments
 (0)