Skip to content

Commit

Permalink
Make use of some Arc<[T]>: RustToCuda
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed May 27, 2024
1 parent 72121b0 commit 58dddbf
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 63 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 = "25735d0", features = ["derive"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["derive"], optional = true }

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "host"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["derive", "host"], optional = true }
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 = "25735d0", features = ["derive"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["derive"] }

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "host"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", 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 = "25735d0", features = ["derive", "final"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["derive", "final"], optional = true }

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "final", "host"], optional = true }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["derive", "final", "host"], optional = true }
31 changes: 8 additions & 23 deletions necsim/impls/no-std/src/cogs/habitat/in_memory.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use core::marker::PhantomData;

use alloc::{boxed::Box, vec::Vec};

use r#final::Final;
use alloc::{sync::Arc, vec::Vec};

use necsim_core::{
cogs::{Backup, Habitat, MathsCore, RngCore, UniformlySampleableHabitat},
Expand All @@ -13,38 +11,25 @@ use necsim_core_bond::{OffByOneU32, OffByOneU64};
use crate::array2d::Array2D;

#[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 InMemoryHabitat<M: MathsCore> {
// TODO: use an Arc
#[cfg_attr(feature = "cuda", cuda(embed))]
habitat: Final<Box<[u32]>>,
// TODO: use an Arc
habitat: Arc<[u32]>,
#[cfg_attr(feature = "cuda", cuda(embed))]
u64_injection: Final<Box<[u64]>>,
u64_injection: Arc<[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 {
Self {
habitat: Final::new(self.habitat.clone()),
u64_injection: Final::new(self.u64_injection.clone()),
habitat: self.habitat.clone(),
u64_injection: self.u64_injection.clone(),
extent: self.extent.clone(),
marker: PhantomData::<M>,
}
Expand Down Expand Up @@ -187,8 +172,8 @@ impl<M: MathsCore> InMemoryHabitat<M> {
let extent = LandscapeExtent::new(Location::new(0, 0), width, height);

Some(Self {
habitat: Final::new(habitat),
u64_injection: Final::new(u64_injection),
habitat: Arc::from(habitat),
u64_injection: Arc::from(u64_injection),
extent,
marker: PhantomData::<M>,
})
Expand Down
30 changes: 5 additions & 25 deletions necsim/impls/no-std/src/cogs/turnover_rate/in_memory.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
#![allow(non_local_definitions)] // FIXME: displaydoc

use alloc::boxed::Box;

use r#final::Final;
use alloc::sync::Arc;

use necsim_core::{
cogs::{Backup, Habitat, MathsCore, TurnoverRate},
cogs::{Habitat, MathsCore, TurnoverRate},
landscape::Location,
};
use necsim_core_bond::NonNegativeF64;

use crate::{array2d::Array2D, cogs::habitat::in_memory::InMemoryHabitat};

#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
#[derive(Clone, Debug)]
#[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 {
Self {
turnover_rate: Final::new(self.turnover_rate.clone()),
}
}
turnover_rate: Arc<[NonNegativeF64]>,
}

#[contract_trait]
Expand Down Expand Up @@ -85,7 +65,7 @@ impl InMemoryTurnoverRate {
})
{
Ok(Self {
turnover_rate: Final::new(turnover_rate.into_row_major().into_boxed_slice()),
turnover_rate: Arc::from(turnover_rate.into_row_major().into_boxed_slice()),
})
} else {
Err(InMemoryTurnoverRateError::ZeroTurnoverHabitat)
Expand Down
2 changes: 1 addition & 1 deletion rustcoalescence/algorithms/cuda/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ thiserror = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_state = "0.4"
serde_derive_state = "0.4"
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["host"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["host"] }
2 changes: 1 addition & 1 deletion rustcoalescence/algorithms/cuda/cpu-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ necsim-impls-no-std = { path = "../../../../necsim/impls/no-std", features = ["c
necsim-impls-cuda = { path = "../../../../necsim/impls/cuda" }
rustcoalescence-algorithms-cuda-gpu-kernel = { path = "../gpu-kernel" }

rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["host"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["host"] }
4 changes: 2 additions & 2 deletions rustcoalescence/algorithms/cuda/gpu-kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ necsim-impls-no-std = { path = "../../../../necsim/impls/no-std", features = ["c
necsim-impls-cuda = { path = "../../../../necsim/impls/cuda" }

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

[target.'cfg(not(target_os = "cuda"))'.dependencies]
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "25735d0", features = ["derive", "kernel"] }
rust-cuda = { git = "https://github.com/juntyr/rust-cuda", rev = "f2a377d", features = ["derive", "kernel"] }
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pub enum SpatiallyExplicitTurnoverMapScenarioError {
#[derive(Clone)]
pub struct SpatiallyExplicitTurnoverMapScenario<M: MathsCore, G: RngCore<M>> {
habitat: InMemoryHabitat<M>,
// TODO: use an Arc
dispersal_map: Array2D<NonNegativeF64>,
turnover_rate: InMemoryTurnoverRate,
speciation_probability: UniformSpeciationProbability,
Expand Down Expand Up @@ -116,6 +115,7 @@ impl<M: MathsCore, G: RngCore<M>> Scenario<M, G> for SpatiallyExplicitTurnoverMa
Self::OriginSamplerAuxiliary,
Self::DecompositionAuxiliary,
) {
// TODO: push the Arc further into the dispersal sampler to maximise sharing
let dispersal_sampler = D::unchecked_new(&self.dispersal_map, &self.habitat);

(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ pub enum SpatiallyExplicitUniformTurnoverScenarioError {
#[derive(Clone)]
pub struct SpatiallyExplicitUniformTurnoverScenario<M: MathsCore, G: RngCore<M>> {
habitat: InMemoryHabitat<M>,
// TODO: use an Arc
dispersal_map: Array2D<NonNegativeF64>,
turnover_rate: UniformTurnoverRate,
speciation_probability: UniformSpeciationProbability,
Expand Down Expand Up @@ -115,6 +114,7 @@ impl<M: MathsCore, G: RngCore<M>> Scenario<M, G>
Self::OriginSamplerAuxiliary,
Self::DecompositionAuxiliary,
) {
// TODO: push the Arc further into the dispersal sampler to maximise sharing
let dispersal_sampler = D::unchecked_new(&self.dispersal_map, &self.habitat);

(
Expand Down

0 comments on commit 58dddbf

Please sign in to comment.