Skip to content

Commit 762bd04

Browse files
authored
Refactoring and optimisations (#238)
* Strengthened habitat-derived cog pre-conditions * Fix type layout hacks in lineages and events * Fix WrappingNoise noise wrapping around 2^32 * Improved CUDA code generation
1 parent a5f67be commit 762bd04

File tree

38 files changed

+717
-556
lines changed

38 files changed

+717
-556
lines changed

necsim/core/src/cogs/dispersal_sampler.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub trait DispersalSampler<M: MathsCore, H: Habitat<M>, G: RngCore<M>>:
1515
crate::cogs::Backup + core::fmt::Debug
1616
{
1717
#[must_use]
18-
#[debug_requires(habitat.contains(location), "location is inside habitat")]
19-
#[debug_ensures(old(habitat).contains(&ret), "target is inside habitat")]
18+
#[debug_requires(habitat.is_location_habitable(location), "location is habitable")]
19+
#[debug_ensures(old(habitat).is_location_habitable(&ret), "target is habitable")]
2020
fn sample_dispersal_from_location(
2121
&self,
2222
location: &Location,
@@ -33,8 +33,8 @@ pub trait SeparableDispersalSampler<M: MathsCore, H: Habitat<M>, G: RngCore<M>>:
3333
DispersalSampler<M, H, G>
3434
{
3535
#[must_use]
36-
#[debug_requires(habitat.contains(location), "location is inside habitat")]
37-
#[debug_ensures(old(habitat).contains(&ret), "target is inside habitat")]
36+
#[debug_requires(habitat.is_location_habitable(location), "location is habitable")]
37+
#[debug_ensures(old(habitat).is_location_habitable(&ret), "target is habitable")]
3838
#[debug_ensures(&ret != location, "disperses to a different location")]
3939
fn sample_non_self_dispersal_from_location(
4040
&self,
@@ -44,7 +44,7 @@ pub trait SeparableDispersalSampler<M: MathsCore, H: Habitat<M>, G: RngCore<M>>:
4444
) -> Location;
4545

4646
#[must_use]
47-
#[debug_requires(habitat.contains(location), "location is inside habitat")]
47+
#[debug_requires(habitat.is_location_habitable(location), "location is habitable")]
4848
fn get_self_dispersal_probability_at_location(
4949
&self,
5050
location: &Location,

necsim/core/src/cogs/habitat.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ pub trait Habitat<M: MathsCore>: crate::cogs::Backup + core::fmt::Debug + Sized
2222
fn get_extent(&self) -> &LandscapeExtent;
2323

2424
#[must_use]
25-
fn contains(&self, location: &Location) -> bool {
26-
self.get_extent().contains(location)
25+
fn is_location_habitable(&self, location: &Location) -> bool {
26+
self.get_extent().contains(location) && self.get_habitat_at_location(location) > 0_u32
27+
}
28+
29+
#[must_use]
30+
fn is_indexed_location_habitable(&self, indexed_location: &IndexedLocation) -> bool {
31+
self.get_extent().contains(indexed_location.location())
32+
&& (indexed_location.index()
33+
< self.get_habitat_at_location(indexed_location.location()))
2734
}
2835

2936
#[must_use]

necsim/core/src/cogs/lineage_store.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub trait LocallyCoherentLineageStore<M: MathsCore, H: Habitat<M>>:
3232
{
3333
#[must_use]
3434
#[debug_requires(
35-
habitat.contains(indexed_location.location()),
36-
"indexed location is inside habitat"
35+
habitat.is_indexed_location_habitable(indexed_location),
36+
"indexed location is habitable"
3737
)]
3838
fn get_global_lineage_reference_at_indexed_location(
3939
&self,
@@ -42,8 +42,8 @@ pub trait LocallyCoherentLineageStore<M: MathsCore, H: Habitat<M>>:
4242
) -> Option<&GlobalLineageReference>;
4343

4444
#[debug_requires(
45-
habitat.contains(lineage.indexed_location.location()),
46-
"indexed location is inside habitat"
45+
habitat.is_indexed_location_habitable(&lineage.indexed_location),
46+
"indexed location is habitable"
4747
)]
4848
#[debug_ensures(self.get_lineage_for_local_reference(
4949
&ret
@@ -68,9 +68,10 @@ pub trait LocallyCoherentLineageStore<M: MathsCore, H: Habitat<M>>:
6868
#[debug_requires(self.get_lineage_for_local_reference(
6969
&reference
7070
).is_some(), "lineage is active")]
71-
#[debug_ensures(old(habitat).contains(
72-
ret.indexed_location.location()
73-
), "prior location is inside habitat")]
71+
#[debug_ensures(
72+
old(habitat).is_indexed_location_habitable(&ret.indexed_location),
73+
"prior indexed location is habitable"
74+
)]
7475
#[debug_ensures(self.get_lineage_for_local_reference(
7576
&old(unsafe { crate::cogs::Backup::backup_unchecked(&reference) })
7677
).is_none(), "lineage was deactivated")]
@@ -104,7 +105,10 @@ pub trait GloballyCoherentLineageStore<M: MathsCore, H: Habitat<M>>:
104105
fn iter_active_locations(&self, habitat: &H) -> Self::LocationIterator<'_>;
105106

106107
#[must_use]
107-
#[debug_requires(habitat.contains(location), "location is inside habitat")]
108+
#[debug_requires(
109+
habitat.is_location_habitable(location),
110+
"location is habitable"
111+
)]
108112
fn get_local_lineage_references_at_location_unordered(
109113
&self,
110114
location: &Location,

necsim/core/src/cogs/speciation_probability.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pub trait SpeciationProbability<M: MathsCore, H: Habitat<M>>:
1111
crate::cogs::Backup + core::fmt::Debug
1212
{
1313
#[must_use]
14-
#[debug_requires(habitat.contains(location), "location is inside habitat")]
14+
#[debug_requires(
15+
habitat.is_location_habitable(location),
16+
"location is habitable"
17+
)]
1518
fn get_speciation_probability_at_location(
1619
&self,
1720
location: &Location,

necsim/core/src/cogs/turnover_rate.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ pub trait TurnoverRate<M: MathsCore, H: Habitat<M>>:
1111
crate::cogs::Backup + core::fmt::Debug
1212
{
1313
#[must_use]
14-
#[debug_requires(habitat.contains(location), "location is inside habitat")]
14+
#[debug_requires(
15+
habitat.is_location_habitable(location),
16+
"location is habitable"
17+
)]
1518
#[debug_ensures(
1619
ret != 0.0_f64 || habitat.get_habitat_at_location(location) == 0_u32,
1720
"only returns zero if the location is inhabitable"

0 commit comments

Comments
 (0)