Skip to content

Commit

Permalink
Initial draft for partition args broadcast using Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed May 26, 2024
1 parent 2a3dac4 commit 7a9568b
Show file tree
Hide file tree
Showing 44 changed files with 138 additions and 97 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions necsim/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contracts = "0.6.3"
serde = { version = "1.0", default-features = false, features = ["derive"] }

[target.'cfg(target_os = "cuda")'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "697dcf5", features = ["derive"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive"], optional = true }

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "697dcf5", features = ["derive", "host"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "host"], optional = true }
2 changes: 1 addition & 1 deletion necsim/core/maths/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![allow(internal_features)]
#![feature(core_intrinsics)]

pub trait MathsCore: 'static + Clone + core::fmt::Debug {
pub trait MathsCore: 'static + Send + Sync + Clone + core::fmt::Debug {
#[must_use]
fn floor(x: f64) -> f64;
#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion necsim/core/src/cogs/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{

#[allow(clippy::module_name_repetitions)]
pub trait RngCore<M: MathsCore>:
crate::cogs::Backup + Sized + Clone + core::fmt::Debug + Serialize + DeserializeOwned
crate::cogs::Backup + Sized + Send + Clone + core::fmt::Debug + Serialize + DeserializeOwned
{
type Seed: AsMut<[u8]> + Default + Sized;

Expand Down
4 changes: 2 additions & 2 deletions necsim/impls/cuda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contracts = "0.6.3"
serde = { version = "1.0", default-features = false, features = ["derive"] }

[target.'cfg(target_os = "cuda")'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "697dcf5", features = ["derive"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive"] }

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "697dcf5", features = ["derive", "host"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "host"] }
4 changes: 2 additions & 2 deletions necsim/impls/no-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fnv = { version = "1.0", default-features = false, features = [] }
rand_core = "0.6"

[target.'cfg(target_os = "cuda")'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "697dcf5", features = ["derive", "final"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "final"], optional = true }

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "697dcf5", features = ["derive", "final", "host"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "final", "host"], optional = true }
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use necsim_core_bond::{ClosedUnitF64, NonNegativeF64, PositiveF64};
use crate::cogs::habitat::almost_infinite::AlmostInfiniteHabitat;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M", free = "G"))]
pub struct AlmostInfiniteClark2DtDispersalSampler<M: MathsCore, G: RngCore<M>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use necsim_core_bond::{ClosedUnitF64, NonNegativeF64};
use crate::cogs::habitat::almost_infinite::AlmostInfiniteHabitat;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M", free = "G"))]
pub struct AlmostInfiniteNormalDispersalSampler<M: MathsCore, G: RngCore<M>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use necsim_core_bond::ClosedUnitF64;
use crate::cogs::habitat::non_spatial::NonSpatialHabitat;

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M", free = "G"))]
pub struct NonSpatialDispersalSampler<M: MathsCore, G: RngCore<M>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::cogs::{
};

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct SpatiallyImplicitDispersalSampler<M: MathsCore, G: RngCore<M>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::cogs::{
};

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct WrappingNoiseApproximateNormalDispersalSampler<M: MathsCore, G: RngCore<M>> {
Expand Down
1 change: 1 addition & 0 deletions necsim/impls/no-std/src/cogs/habitat/almost_infinite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const ALMOST_INFINITE_EXTENT: LandscapeExtent =
LandscapeExtent::new(Location::new(0, 0), OffByOneU32::max(), OffByOneU32::max());

#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct AlmostInfiniteHabitat<M: MathsCore> {
Expand Down
13 changes: 13 additions & 0 deletions necsim/impls/no-std/src/cogs/habitat/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ use crate::array2d::Array2D;
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct InMemoryHabitat<M: MathsCore> {
// TODO: use an Arc
#[cfg_attr(feature = "cuda", cuda(embed))]
habitat: Final<Box<[u32]>>,
// TODO: use an Arc
#[cfg_attr(feature = "cuda", cuda(embed))]
u64_injection: Final<Box<[u64]>>,
#[cfg_attr(feature = "cuda", cuda(embed))]
extent: LandscapeExtent,
marker: PhantomData<M>,
}

impl<M: MathsCore> Clone for InMemoryHabitat<M> {
fn clone(&self) -> Self {
Self {
habitat: Final::new(self.habitat.clone()),
u64_injection: Final::new(self.u64_injection.clone()),
extent: self.extent.clone(),
marker: PhantomData::<M>,
}
}
}

#[contract_trait]
impl<M: MathsCore> Backup for InMemoryHabitat<M> {
unsafe fn backup_unchecked(&self) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion necsim/impls/no-std/src/cogs/habitat/non_spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use necsim_core::{
use necsim_core_bond::{OffByOneU32, OffByOneU64};

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct NonSpatialHabitat<M: MathsCore> {
Expand Down
2 changes: 1 addition & 1 deletion necsim/impls/no-std/src/cogs/habitat/spatially_implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const SPATIALLY_IMPLICIT_EXTENT: LandscapeExtent =
LandscapeExtent::new(Location::new(0, 0), OffByOneU32::max(), OffByOneU32::max());

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct SpatiallyImplicitHabitat<M: MathsCore> {
Expand Down
12 changes: 6 additions & 6 deletions necsim/impls/no-std/src/cogs/habitat/wrapping_noise/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use alloc::boxed::Box;
use alloc::sync::Arc;
use core::{fmt, num::NonZeroUsize};
use necsim_core_bond::{ClosedUnitF64, OffByOneU64, OpenClosedUnitF64 as PositiveUnitF64};
use r#final::Final;

mod opensimplex_noise;

Expand All @@ -18,6 +17,7 @@ use crate::cogs::{
};

#[allow(clippy::module_name_repetitions)]
#[derive(Clone)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[cfg_attr(feature = "cuda", cuda(free = "M"))]
pub struct WrappingNoiseHabitat<M: MathsCore> {
Expand All @@ -29,7 +29,7 @@ pub struct WrappingNoiseHabitat<M: MathsCore> {
persistence: PositiveUnitF64,
octaves: NonZeroUsize,
#[cfg_attr(feature = "cuda", cuda(embed))]
noise: Final<Box<OpenSimplexNoise>>,
noise: Arc<OpenSimplexNoise>,
}

impl<M: MathsCore> fmt::Debug for WrappingNoiseHabitat<M> {
Expand All @@ -52,7 +52,7 @@ impl<M: MathsCore> WrappingNoiseHabitat<M> {
persistence: PositiveUnitF64,
octaves: NonZeroUsize,
) -> Self {
let noise = Box::new(OpenSimplexNoise::new(Some(seed)));
let noise = Arc::new(OpenSimplexNoise::new(Some(seed)));

// Emperically determine a threshold to uniformly sample habitat
// from the generated Simplex Noise
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<M: MathsCore> WrappingNoiseHabitat<M> {
scale,
persistence,
octaves,
noise: Final::new(noise),
noise,
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ impl<M: MathsCore> Backup for WrappingNoiseHabitat<M> {
scale: self.scale,
persistence: self.persistence,
octaves: self.octaves,
noise: Final::new(self.noise.clone()),
noise: self.noise.clone(),
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions necsim/impls/no-std/src/cogs/rng/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ impl<
}
}

impl<M: MathsCore, G: RandRngCore + RandSeedableRng + Clone + Serialize + DeserializeOwned>
RngCore<M> for RandRng<M, G>
impl<
M: MathsCore,
G: RandRngCore + RandSeedableRng + Send + Clone + Serialize + DeserializeOwned,
> RngCore<M> for RandRng<M, G>
{
type Seed = G::Seed;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use necsim_core_bond::{ClosedUnitF64, OpenClosedUnitF64 as PositiveUnitF64};

use crate::cogs::habitat::spatially_implicit::SpatiallyImplicitHabitat;

#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[allow(clippy::module_name_repetitions)]
pub struct SpatiallyImplicitSpeciationProbability {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use necsim_core::{
};
use necsim_core_bond::ClosedUnitF64;

#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[allow(clippy::module_name_repetitions)]
pub struct UniformSpeciationProbability {
Expand Down
9 changes: 9 additions & 0 deletions necsim/impls/no-std/src/cogs/turnover_rate/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ use crate::{array2d::Array2D, cogs::habitat::in_memory::InMemoryHabitat};
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
pub struct InMemoryTurnoverRate {
#[cfg_attr(feature = "cuda", cuda(embed))]
// TODO: use an Arc
turnover_rate: Final<Box<[NonNegativeF64]>>,
}

impl Clone for InMemoryTurnoverRate {
fn clone(&self) -> Self {
Self {
turnover_rate: Final::new(self.turnover_rate.clone()),
}
}
}

#[contract_trait]
impl Backup for InMemoryTurnoverRate {
unsafe fn backup_unchecked(&self) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion necsim/impls/no-std/src/cogs/turnover_rate/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use necsim_core::{
};
use necsim_core_bond::{NonNegativeF64, PositiveF64};

#[derive(Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))]
#[allow(clippy::module_name_repetitions)]
pub struct UniformTurnoverRate {
Expand Down
2 changes: 1 addition & 1 deletion necsim/impls/std/src/lineage_file/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize, Serializer};

use necsim_core::lineage::Lineage;

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Clone)]
#[serde(try_from = "LineageFileLoaderRaw")]
#[allow(clippy::module_name_repetitions)]
pub struct LineageFileLoader {
Expand Down
10 changes: 3 additions & 7 deletions necsim/partitioning/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ pub trait Partitioning: Sized {

fn get_size(&self) -> PartitionSize;

fn with_local_partition<
R: Reporter,
P: ReporterContext<Reporter = R>,
F: for<'p> FnOnce(Self::LocalPartition<'p, R>) -> Q,
Q,
>(
fn with_local_partition<R: Reporter, P: ReporterContext<Reporter = R>, A: Send + Clone, Q>(
self,
reporter_context: P,
auxiliary: Self::Auxiliary,
inner: F,
args: A,
inner: for<'p> fn(Self::LocalPartition<'p, R>, A) -> Q,
) -> anyhow::Result<Q>;
}

Expand Down
12 changes: 4 additions & 8 deletions necsim/partitioning/monolithic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,12 @@ impl Partitioning for MonolithicPartitioning {
/// # Errors
///
/// Returns an error if the provided event log is not empty.
fn with_local_partition<
R: Reporter,
P: ReporterContext<Reporter = R>,
F: for<'p> FnOnce(Self::LocalPartition<'p, R>) -> Q,
Q,
>(
fn with_local_partition<R: Reporter, P: ReporterContext<Reporter = R>, A: Send + Clone, Q>(
self,
reporter_context: P,
event_log: Self::Auxiliary,
inner: F,
args: A,
inner: for<'p> fn(Self::LocalPartition<'p, R>, A) -> Q,
) -> anyhow::Result<Q> {
let local_partition = if let Some(event_log) = event_log {
MonolithicLocalPartition::Recorded(Box::new(
Expand All @@ -88,7 +84,7 @@ impl Partitioning for MonolithicPartitioning {
))
};

Ok(inner(local_partition))
Ok(inner(local_partition, args))
}
}

Expand Down
12 changes: 4 additions & 8 deletions necsim/partitioning/mpi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,12 @@ impl Partitioning for MpiPartitioning {
/// Returns `MissingEventLog` if the local partition is non-monolithic and
/// the `event_log` is `None`.
/// Returns `InvalidEventSubLog` if creating a sub-`event_log` failed.
fn with_local_partition<
R: Reporter,
P: ReporterContext<Reporter = R>,
F: for<'p> FnOnce(Self::LocalPartition<'p, R>) -> Q,
Q,
>(
fn with_local_partition<R: Reporter, P: ReporterContext<Reporter = R>, A: Send + Clone, Q>(
self,
reporter_context: P,
event_log: Self::Auxiliary,
inner: F,
args: A,
inner: for<'p> fn(Self::LocalPartition<'p, R>, A) -> Q,
) -> anyhow::Result<Q> {
let Some(event_log) = event_log else {
anyhow::bail!(MpiLocalPartitionError::MissingEventLog)
Expand Down Expand Up @@ -234,7 +230,7 @@ impl Partitioning for MpiPartitioning {
)))
};

Ok(inner(local_partition))
Ok(inner(local_partition, args))
})
}
}
Expand Down
Loading

0 comments on commit 7a9568b

Please sign in to comment.