Skip to content

Commit 7cb30ff

Browse files
committed
Removed MathsCore usage from OriginSampler
1 parent e0177bb commit 7cb30ff

File tree

27 files changed

+97
-82
lines changed

27 files changed

+97
-82
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::num::{NonZeroU128, NonZeroU64, NonZeroUsize};
33

44
use necsim_core::cogs::{
55
distribution::{IndexU128, IndexU64, IndexUsize, Length, UniformClosedOpenUnit},
6-
DistributionSampler, Rng, RngCore,
6+
Backup, DistributionSampler, Rng, RngCore,
77
};
88
use necsim_core_bond::{ClosedOpenUnitF64, NonNegativeF64, PositiveF64};
99
use necsim_core_maths::MathsCore;
@@ -189,6 +189,13 @@ impl RngCore for DummyRng {
189189
}
190190
}
191191

192+
#[contract_trait]
193+
impl Backup for DummyRng {
194+
unsafe fn backup_unchecked(&self) -> Self {
195+
Self(self.0.clone())
196+
}
197+
}
198+
192199
impl Rng<IntrinsicsMathsCore> for DummyRng {
193200
type Generator = Self;
194201
type Sampler = DummyDistributionSamplers;

necsim/impls/no-std/src/cogs/origin_sampler/almost_infinite.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ use super::{TrustedOriginSampler, UntrustedOriginSampler};
1515

1616
const HABITAT_CENTRE: u32 = u32::MAX / 2;
1717

18+
// Note: The MathsCore should not be utilised in the origin sampler
19+
// to improve compatibility
1820
#[allow(clippy::module_name_repetitions)]
1921
pub struct AlmostInfiniteOriginSampler<'h, M: MathsCore, I: Iterator<Item = u64>> {
20-
pre_sampler: OriginPreSampler<M, I>,
22+
pre_sampler: OriginPreSampler<I>,
2123
last_index: u64,
2224
location_iterator: LocationIterator,
2325
radius_squared: u64,
@@ -43,7 +45,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> fmt::Debug
4345
impl<'h, M: MathsCore, I: Iterator<Item = u64>> AlmostInfiniteOriginSampler<'h, M, I> {
4446
#[must_use]
4547
pub fn new(
46-
pre_sampler: OriginPreSampler<M, I>,
48+
pre_sampler: OriginPreSampler<I>,
4749
habitat: &'h AlmostInfiniteHabitat<M>,
4850
radius: u16,
4951
) -> Self {
@@ -60,12 +62,11 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> AlmostInfiniteOriginSampler<'h,
6062
);
6163

6264
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
63-
let upper_bound_size_hint = M::ceil(
64-
f64::from(radius)
65-
* f64::from(radius)
66-
* core::f64::consts::PI
67-
* pre_sampler.get_sample_proportion().get(),
68-
) as u64;
65+
let upper_bound_size_hint = (f64::from(radius)
66+
* f64::from(radius)
67+
* core::f64::consts::PI
68+
* pre_sampler.get_sample_proportion().get()
69+
+ 1.0_f64) as u64;
6970

7071
Self {
7172
pre_sampler,
@@ -89,7 +90,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> UntrustedOriginSampler<'h, M>
8990
self.habitat
9091
}
9192

92-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler> {
93+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler> {
9394
self.pre_sampler
9495
}
9596

necsim/impls/no-std/src/cogs/origin_sampler/decomposition.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{
1212
decomposition::Decomposition,
1313
};
1414

15+
// Note: The MathsCore should not be utilised in the origin sampler
16+
// (only in the decomposition) to improve compatibility
1517
#[allow(clippy::module_name_repetitions)]
1618
#[derive(Debug)]
1719
pub struct DecompositionOriginSampler<
@@ -49,7 +51,7 @@ impl<'d, M: MathsCore, O: UntrustedOriginSampler<'d, M>, D: Decomposition<M, O::
4951
self.origin_sampler.habitat()
5052
}
5153

52-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler> {
54+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler> {
5355
self.origin_sampler.into_pre_sampler()
5456
}
5557

necsim/impls/no-std/src/cogs/origin_sampler/in_memory.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ use crate::cogs::{
1616

1717
use super::{TrustedOriginSampler, UntrustedOriginSampler};
1818

19+
// Note: The MathsCore should not be utilised in the origin sampler
20+
// to improve compatibility
1921
#[allow(clippy::module_name_repetitions)]
2022
pub struct InMemoryOriginSampler<'h, M: MathsCore, I: Iterator<Item = u64>> {
21-
pre_sampler: OriginPreSampler<M, I>,
23+
pre_sampler: OriginPreSampler<I>,
2224
last_index: u64,
2325
location_iterator: Peekable<LocationIterator>,
2426
next_location_index: u32,
@@ -39,7 +41,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> fmt::Debug for InMemoryOriginSam
3941

4042
impl<'h, M: MathsCore, I: Iterator<Item = u64>> InMemoryOriginSampler<'h, M, I> {
4143
#[must_use]
42-
pub fn new(pre_sampler: OriginPreSampler<M, I>, habitat: &'h InMemoryHabitat<M>) -> Self {
44+
pub fn new(pre_sampler: OriginPreSampler<I>, habitat: &'h InMemoryHabitat<M>) -> Self {
4345
Self {
4446
pre_sampler,
4547
last_index: 0_u64,
@@ -61,7 +63,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> UntrustedOriginSampler<'h, M>
6163
self.habitat
6264
}
6365

64-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler> {
66+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler> {
6567
self.pre_sampler
6668
}
6769

necsim/impls/no-std/src/cogs/origin_sampler/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ use pre_sampler::OriginPreSampler;
1919
#[allow(clippy::module_name_repetitions)]
2020
/// `Lineage`s produced by the sampler's iterator must have
2121
/// * unique global references
22+
///
23+
/// Note: The MathsCore should not be utilised in the origin sampler
24+
/// to improve compatibility
2225
pub trait UntrustedOriginSampler<'h, M: MathsCore>:
2326
Sized + core::fmt::Debug + core::iter::Iterator<Item = Lineage>
2427
{
@@ -27,7 +30,7 @@ pub trait UntrustedOriginSampler<'h, M: MathsCore>:
2730

2831
fn habitat(&self) -> &'h Self::Habitat;
2932

30-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler>;
33+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler>;
3134

3235
fn full_upper_bound_size_hint(&self) -> u64;
3336
}

necsim/impls/no-std/src/cogs/origin_sampler/non_spatial.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ use crate::cogs::{
1616

1717
use super::{TrustedOriginSampler, UntrustedOriginSampler};
1818

19+
// Note: The MathsCore should not be utilised in the origin sampler
20+
// to improve compatibility
1921
#[allow(clippy::module_name_repetitions)]
2022
pub struct NonSpatialOriginSampler<'h, M: MathsCore, I: Iterator<Item = u64>> {
21-
pre_sampler: OriginPreSampler<M, I>,
23+
pre_sampler: OriginPreSampler<I>,
2224
last_index: u64,
2325
location_iterator: Peekable<LocationIterator>,
2426
next_location_index: u32,
@@ -39,7 +41,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> fmt::Debug for NonSpatialOriginS
3941

4042
impl<'h, M: MathsCore, I: Iterator<Item = u64>> NonSpatialOriginSampler<'h, M, I> {
4143
#[must_use]
42-
pub fn new(pre_sampler: OriginPreSampler<M, I>, habitat: &'h NonSpatialHabitat<M>) -> Self {
44+
pub fn new(pre_sampler: OriginPreSampler<I>, habitat: &'h NonSpatialHabitat<M>) -> Self {
4345
Self {
4446
pre_sampler,
4547
last_index: 0_u64,
@@ -61,7 +63,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> UntrustedOriginSampler<'h, M>
6163
self.habitat
6264
}
6365

64-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler> {
66+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler> {
6567
self.pre_sampler
6668
}
6769

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,75 @@
11
use core::{
22
fmt,
33
iter::Empty,
4-
marker::PhantomData,
54
ops::{Deref, DerefMut, RangeFrom},
65
};
76

8-
use necsim_core::cogs::MathsCore;
97
use necsim_core_bond::ClosedUnitF64;
108
use necsim_partitioning_core::partition::Partition;
119

1210
const INV_PHI: f64 = 6.180_339_887_498_949e-1_f64;
1311

1412
#[allow(clippy::module_name_repetitions)]
15-
pub struct OriginPreSampler<M: MathsCore, I: Iterator<Item = u64>> {
13+
pub struct OriginPreSampler<I: Iterator<Item = u64>> {
1614
inner: I,
1715
proportion: ClosedUnitF64,
18-
_marker: PhantomData<M>,
1916
}
2017

21-
impl<M: MathsCore, I: Iterator<Item = u64>> OriginPreSampler<M, I> {
18+
impl<I: Iterator<Item = u64>> OriginPreSampler<I> {
2219
pub fn get_sample_proportion(&self) -> ClosedUnitF64 {
2320
self.proportion
2421
}
2522
}
2623

27-
impl<M: MathsCore, I: Iterator<Item = u64>> Deref for OriginPreSampler<M, I> {
24+
impl<I: Iterator<Item = u64>> Deref for OriginPreSampler<I> {
2825
type Target = I;
2926

3027
fn deref(&self) -> &Self::Target {
3128
&self.inner
3229
}
3330
}
3431

35-
impl<M: MathsCore, I: Iterator<Item = u64>> DerefMut for OriginPreSampler<M, I> {
32+
impl<I: Iterator<Item = u64>> DerefMut for OriginPreSampler<I> {
3633
fn deref_mut(&mut self) -> &mut Self::Target {
3734
&mut self.inner
3835
}
3936
}
4037

41-
impl<M: MathsCore, I: Iterator<Item = u64>> fmt::Debug for OriginPreSampler<M, I> {
38+
impl<I: Iterator<Item = u64>> fmt::Debug for OriginPreSampler<I> {
4239
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
4340
fmt.debug_struct(stringify!(OriginPreSampler))
4441
.field("proportion", &self.proportion)
4542
.finish()
4643
}
4744
}
4845

49-
impl<M: MathsCore> OriginPreSampler<M, RangeFrom<u64>> {
46+
impl OriginPreSampler<RangeFrom<u64>> {
5047
#[must_use]
5148
pub fn all() -> Self {
5249
Self {
5350
inner: 0..,
5451
proportion: ClosedUnitF64::one(),
55-
_marker: PhantomData::<M>,
5652
}
5753
}
5854
}
5955

60-
impl<M: MathsCore> OriginPreSampler<M, Empty<u64>> {
56+
impl OriginPreSampler<Empty<u64>> {
6157
#[must_use]
6258
pub fn none() -> Self {
6359
Self {
6460
inner: core::iter::empty(),
6561
proportion: ClosedUnitF64::zero(),
66-
_marker: PhantomData::<M>,
6762
}
6863
}
6964
}
7065

71-
impl<M: MathsCore, I: Iterator<Item = u64>> OriginPreSampler<M, I> {
66+
impl<I: Iterator<Item = u64>> OriginPreSampler<I> {
7267
#[must_use]
7368
pub fn percentage(
7469
mut self,
7570
percentage: ClosedUnitF64,
76-
) -> OriginPreSampler<M, impl Iterator<Item = u64>> {
77-
let inv_geometric_sample_rate = M::ln(1.0_f64 - percentage.get()).recip();
71+
) -> OriginPreSampler<impl Iterator<Item = u64>> {
72+
let inv_geometric_sample_rate = libm::log(1.0_f64 - percentage.get()).recip();
7873

7974
OriginPreSampler {
8075
proportion: self.proportion * percentage,
@@ -89,27 +84,29 @@ impl<M: MathsCore, I: Iterator<Item = u64>> OriginPreSampler<M, I> {
8984

9085
// q = (q + INV_PHI) % 1 where q >= 0
9186
*quasi_random += INV_PHI;
92-
*quasi_random -= M::floor(*quasi_random);
87+
*quasi_random -= if *quasi_random >= 1.0_f64 {
88+
1.0_f64
89+
} else {
90+
0.0_f64
91+
};
9392

9493
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
95-
let skip = M::floor(M::ln(*quasi_random) * inv_geometric_sample_rate) as usize;
94+
let skip = (libm::log(*quasi_random) * inv_geometric_sample_rate) as usize;
9695

9796
self.nth(skip)
9897
}),
99-
_marker: PhantomData::<M>,
10098
}
10199
}
102100

103101
pub fn partition(
104102
mut self,
105103
partition: Partition,
106-
) -> OriginPreSampler<M, impl Iterator<Item = u64>> {
104+
) -> OriginPreSampler<impl Iterator<Item = u64>> {
107105
let _ = self.advance_by(partition.rank() as usize);
108106

109107
OriginPreSampler {
110108
proportion: self.proportion / partition.size(),
111109
inner: self.inner.step_by(partition.size().get() as usize),
112-
_marker: PhantomData::<M>,
113110
}
114111
}
115112
}

necsim/impls/no-std/src/cogs/origin_sampler/resuming.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::{fmt, iter::ExactSizeIterator};
1+
use core::{fmt, iter::ExactSizeIterator, marker::PhantomData};
22

33
use necsim_core::{
44
cogs::{Habitat, MathsCore},
@@ -9,6 +9,8 @@ use crate::cogs::origin_sampler::{pre_sampler::OriginPreSampler, TrustedOriginSa
99

1010
use super::UntrustedOriginSampler;
1111

12+
// Note: The MathsCore should not be utilised in the origin sampler
13+
// to improve compatibility
1214
#[allow(clippy::module_name_repetitions)]
1315
pub struct ResumingOriginSampler<
1416
'h,
@@ -18,9 +20,10 @@ pub struct ResumingOriginSampler<
1820
I: Iterator<Item = u64>,
1921
> {
2022
lineage_iterator: L,
21-
pre_sampler: OriginPreSampler<M, I>,
23+
pre_sampler: OriginPreSampler<I>,
2224
last_index: u64,
2325
habitat: &'h H,
26+
marker: PhantomData<M>,
2427
}
2528

2629
impl<
@@ -49,12 +52,13 @@ impl<
4952
> ResumingOriginSampler<'h, M, H, L, I>
5053
{
5154
#[must_use]
52-
pub fn new(habitat: &'h H, pre_sampler: OriginPreSampler<M, I>, lineage_iterator: L) -> Self {
55+
pub fn new(habitat: &'h H, pre_sampler: OriginPreSampler<I>, lineage_iterator: L) -> Self {
5356
Self {
5457
lineage_iterator,
5558
pre_sampler,
5659
last_index: 0_u64,
5760
habitat,
61+
marker: PhantomData::<M>,
5862
}
5963
}
6064
}
@@ -75,7 +79,7 @@ impl<
7579
self.habitat
7680
}
7781

78-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler> {
82+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler> {
7983
self.pre_sampler
8084
}
8185

@@ -85,9 +89,9 @@ impl<
8589
clippy::cast_possible_truncation,
8690
clippy::cast_sign_loss
8791
)]
88-
let upper_bound_size_hint = M::ceil(
89-
(self.lineage_iterator.len() as f64) * self.pre_sampler.get_sample_proportion().get(),
90-
) as u64;
92+
let upper_bound_size_hint = ((self.lineage_iterator.len() as f64)
93+
* self.pre_sampler.get_sample_proportion().get())
94+
as u64;
9195

9296
upper_bound_size_hint
9397
}

necsim/impls/no-std/src/cogs/origin_sampler/spatially_implicit.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::cogs::{
99

1010
use super::{TrustedOriginSampler, UntrustedOriginSampler};
1111

12+
// Note: The MathsCore should not be utilised in the origin sampler
13+
// to improve compatibility
1214
#[allow(clippy::module_name_repetitions)]
1315
pub struct SpatiallyImplicitOriginSampler<'h, M: MathsCore, I: Iterator<Item = u64>> {
1416
local_iterator: NonSpatialOriginSampler<'h, M, I>,
@@ -28,10 +30,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> fmt::Debug
2830

2931
impl<'h, M: MathsCore, I: Iterator<Item = u64>> SpatiallyImplicitOriginSampler<'h, M, I> {
3032
#[must_use]
31-
pub fn new(
32-
pre_sampler: OriginPreSampler<M, I>,
33-
habitat: &'h SpatiallyImplicitHabitat<M>,
34-
) -> Self {
33+
pub fn new(pre_sampler: OriginPreSampler<I>, habitat: &'h SpatiallyImplicitHabitat<M>) -> Self {
3534
Self {
3635
local_iterator: NonSpatialOriginSampler::new(pre_sampler, habitat.local()),
3736
habitat,
@@ -50,7 +49,7 @@ impl<'h, M: MathsCore, I: Iterator<Item = u64>> UntrustedOriginSampler<'h, M>
5049
self.habitat
5150
}
5251

53-
fn into_pre_sampler(self) -> OriginPreSampler<M, Self::PreSampler> {
52+
fn into_pre_sampler(self) -> OriginPreSampler<Self::PreSampler> {
5453
self.local_iterator.into_pre_sampler()
5554
}
5655

0 commit comments

Comments
 (0)