Skip to content

Commit

Permalink
Small fixes + hacky faster dispersal
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Jun 7, 2024
1 parent 465ed80 commit 7b0c6e7
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 83 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,38 @@ impl<M: MathsCore, G: RngCore<M>, D: Clone + DispersalSampler<M, AlmostInfiniteH
habitat: &AlmostInfiniteDownscaledHabitat<M>,
rng: &mut G,
) -> Location {
let mut target_location = self.sample_dispersal_from_location(location, habitat, rng);
// // TODO: must be optimised
// let mut target_location = self.sample_dispersal_from_location(location,
// habitat, rng);

// For now, we just use rejection sampling here
while &target_location == location {
target_location = self.sample_dispersal_from_location(location, habitat, rng);
}
// // For now, we just use rejection sampling here
// while &target_location == location {
// target_location = self.sample_dispersal_from_location(location, habitat,
// rng); }

// target_location

// very dirty nearest neighbour dispersal
let direction = rng.sample_index(core::num::NonZeroUsize::MIN.saturating_add(7));

// 0 1 2
// 7 3
// 6 5 4

let x = match direction {
0 | 6 | 7 => location.x().wrapping_sub(habitat.downscale_x() as u32),
1 | 5 => location.x(),
2..=4 => location.x().wrapping_add(habitat.downscale_x() as u32),
_ => unreachable!(),
};
let y = match direction {
0..=2 => location.y().wrapping_add(habitat.downscale_y() as u32),
3 | 7 => location.y(),
4..=6 => location.y().wrapping_sub(habitat.downscale_y() as u32),
_ => unreachable!(),
};

target_location
Location::new(x, y)
}

#[must_use]
Expand Down
134 changes: 65 additions & 69 deletions necsim/impls/no-std/src/cogs/habitat/almost_infinite/downscaled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,44 @@ pub struct AlmostInfiniteDownscaledHabitat<M: MathsCore> {
#[derive(Copy, Clone, Debug, PartialEq, Eq, TypeLayout)]
#[repr(u16)]
pub enum Log2U16 {
Shl0 = 1 << 0,
Shl1 = 1 << 1,
Shl2 = 1 << 2,
Shl3 = 1 << 3,
Shl4 = 1 << 4,
Shl5 = 1 << 5,
Shl6 = 1 << 6,
Shl7 = 1 << 7,
Shl8 = 1 << 8,
Shl9 = 1 << 9,
Shl10 = 1 << 10,
Shl11 = 1 << 11,
Shl12 = 1 << 12,
Shl13 = 1 << 13,
Shl14 = 1 << 14,
Shl15 = 1 << 15,
_1B0 = 1 << 0,
_1B1 = 1 << 1,
_1B2 = 1 << 2,
_1B3 = 1 << 3,
_1B4 = 1 << 4,
_1B5 = 1 << 5,
_1B6 = 1 << 6,
_1B7 = 1 << 7,
_1B8 = 1 << 8,
_1B9 = 1 << 9,
_1B10 = 1 << 10,
_1B11 = 1 << 11,
_1B12 = 1 << 12,
_1B13 = 1 << 13,
_1B14 = 1 << 14,
_1B15 = 1 << 15,
}

impl Log2U16 {
#[must_use]
pub const fn log2(self) -> u32 {
match self {
Self::Shl0 => 0,
Self::Shl1 => 1,
Self::Shl2 => 2,
Self::Shl3 => 3,
Self::Shl4 => 4,
Self::Shl5 => 5,
Self::Shl6 => 6,
Self::Shl7 => 7,
Self::Shl8 => 8,
Self::Shl9 => 9,
Self::Shl10 => 10,
Self::Shl11 => 11,
Self::Shl12 => 12,
Self::Shl13 => 13,
Self::Shl14 => 14,
Self::Shl15 => 15,
Self::_1B0 => 0,
Self::_1B1 => 1,
Self::_1B2 => 2,
Self::_1B3 => 3,
Self::_1B4 => 4,
Self::_1B5 => 5,
Self::_1B6 => 6,
Self::_1B7 => 7,
Self::_1B8 => 8,
Self::_1B9 => 9,
Self::_1B10 => 10,
Self::_1B11 => 11,
Self::_1B12 => 12,
Self::_1B13 => 13,
Self::_1B14 => 14,
Self::_1B15 => 15,
}
}
}
Expand Down Expand Up @@ -232,22 +232,22 @@ impl<'de> serde::Deserialize<'de> for Log2U16 {

fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<Self::Value, E> {
match v {
const { Log2U16::Shl0 as u64 } => Ok(Log2U16::Shl0),
const { Log2U16::Shl1 as u64 } => Ok(Log2U16::Shl1),
const { Log2U16::Shl2 as u64 } => Ok(Log2U16::Shl2),
const { Log2U16::Shl3 as u64 } => Ok(Log2U16::Shl3),
const { Log2U16::Shl4 as u64 } => Ok(Log2U16::Shl4),
const { Log2U16::Shl5 as u64 } => Ok(Log2U16::Shl5),
const { Log2U16::Shl6 as u64 } => Ok(Log2U16::Shl6),
const { Log2U16::Shl7 as u64 } => Ok(Log2U16::Shl7),
const { Log2U16::Shl8 as u64 } => Ok(Log2U16::Shl8),
const { Log2U16::Shl9 as u64 } => Ok(Log2U16::Shl9),
const { Log2U16::Shl10 as u64 } => Ok(Log2U16::Shl10),
const { Log2U16::Shl11 as u64 } => Ok(Log2U16::Shl11),
const { Log2U16::Shl12 as u64 } => Ok(Log2U16::Shl12),
const { Log2U16::Shl13 as u64 } => Ok(Log2U16::Shl13),
const { Log2U16::Shl14 as u64 } => Ok(Log2U16::Shl14),
const { Log2U16::Shl15 as u64 } => Ok(Log2U16::Shl15),
const { Log2U16::_1B0 as u64 } => Ok(Log2U16::_1B0),
const { Log2U16::_1B1 as u64 } => Ok(Log2U16::_1B1),
const { Log2U16::_1B2 as u64 } => Ok(Log2U16::_1B2),
const { Log2U16::_1B3 as u64 } => Ok(Log2U16::_1B3),
const { Log2U16::_1B4 as u64 } => Ok(Log2U16::_1B4),
const { Log2U16::_1B5 as u64 } => Ok(Log2U16::_1B5),
const { Log2U16::_1B6 as u64 } => Ok(Log2U16::_1B6),
const { Log2U16::_1B7 as u64 } => Ok(Log2U16::_1B7),
const { Log2U16::_1B8 as u64 } => Ok(Log2U16::_1B8),
const { Log2U16::_1B9 as u64 } => Ok(Log2U16::_1B9),
const { Log2U16::_1B10 as u64 } => Ok(Log2U16::_1B10),
const { Log2U16::_1B11 as u64 } => Ok(Log2U16::_1B11),
const { Log2U16::_1B12 as u64 } => Ok(Log2U16::_1B12),
const { Log2U16::_1B13 as u64 } => Ok(Log2U16::_1B13),
const { Log2U16::_1B14 as u64 } => Ok(Log2U16::_1B14),
const { Log2U16::_1B15 as u64 } => Ok(Log2U16::_1B15),
v => Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Unsigned(v),
&self,
Expand All @@ -271,22 +271,22 @@ impl<'de> serde::Deserialize<'de> for Log2U16 {
};

let Some(v) = [
Log2U16::Shl0,
Log2U16::Shl1,
Log2U16::Shl2,
Log2U16::Shl3,
Log2U16::Shl4,
Log2U16::Shl5,
Log2U16::Shl6,
Log2U16::Shl7,
Log2U16::Shl8,
Log2U16::Shl9,
Log2U16::Shl10,
Log2U16::Shl11,
Log2U16::Shl12,
Log2U16::Shl13,
Log2U16::Shl14,
Log2U16::Shl15,
Log2U16::_1B0,
Log2U16::_1B1,
Log2U16::_1B2,
Log2U16::_1B3,
Log2U16::_1B4,
Log2U16::_1B5,
Log2U16::_1B6,
Log2U16::_1B7,
Log2U16::_1B8,
Log2U16::_1B9,
Log2U16::_1B10,
Log2U16::_1B11,
Log2U16::_1B12,
Log2U16::_1B13,
Log2U16::_1B14,
Log2U16::_1B15,
]
.get(exp) else {
return Err(serde::de::Error::invalid_value(
Expand All @@ -299,10 +299,6 @@ impl<'de> serde::Deserialize<'de> for Log2U16 {
}
}

if deserializer.is_human_readable() {
deserializer.deserialize_str(Log2U16Visitor)
} else {
deserializer.deserialize_u32(Log2U16Visitor)
}
deserializer.deserialize_any(Log2U16Visitor)
}
}
1 change: 1 addition & 0 deletions necsim/impls/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ bincode = "1.3"
serde = { version = "1.0", features = ["derive"] }
pcg_rand = { version = "0.13", features = ["u128", "serde1"] }
glob = "0.3"
rmp-serde = "1.3"
4 changes: 2 additions & 2 deletions necsim/impls/std/src/lineage_file/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl LineageFileLoader {
pub fn try_new(path: &Path) -> anyhow::Result<Self> {
let file = OpenOptions::new().read(true).write(false).open(path)?;

let mut deserializer =
bincode::Deserializer::with_reader(BufReader::new(file), bincode::options());
let mut deserializer = rmp_serde::Deserializer::new(BufReader::new(file));
// bincode::Deserializer::with_reader(BufReader::new(file), bincode::options());

let lineages = <Vec<Lineage>>::deserialize(&mut deserializer)?;

Expand Down
4 changes: 2 additions & 2 deletions necsim/impls/std/src/lineage_file/saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ impl LineageFileSaver {
///
/// Fails if a the lineages could not be written to the file at `path`
pub fn write<'a, I: Iterator<Item = &'a Lineage>>(mut self, lineages: I) -> anyhow::Result<()> {
let mut serializer =
bincode::Serializer::new(BufWriter::new(&mut self.file), bincode::options());
let mut serializer = rmp_serde::Serializer::new(BufWriter::new(&mut self.file));
// bincode::Serializer::new(BufWriter::new(&mut self.file), bincode::options());

serializer.collect_seq(lineages)?;

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Pin to final 1.79.0 nightly
channel = "nightly-2024-04-28"
components = [ "cargo", "rustfmt", "clippy", "rust-src", "llvm-bitcode-linker", "llvm-tools" ]
targets = [ "x86_64-unknown-linux-gnu", "nvptx64-nvidia-cuda" ]
targets = [ "nvptx64-nvidia-cuda" ]
6 changes: 3 additions & 3 deletions rustcoalescence/src/cli/simulate/parse/event_log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::Path;

use serde::{Deserialize, Deserializer};
use serde_state::DeserializeState;

Expand Down Expand Up @@ -38,7 +36,9 @@ pub(in super::super) fn parse_and_normalise(

let event_log = match event_log {
Some(event_log)
if event_log.directory() == Path::new("I-solemnly-swear-that-I-am-up-to-no-good") =>
if event_log
.directory()
.ends_with("I-solemnly-swear-that-I-am-up-to-no-good") =>
{
None
},
Expand Down

0 comments on commit 7b0c6e7

Please sign in to comment.