From 58dddbf1dd8ec7c9e4051cf9bede7de6f6b6a0bf Mon Sep 17 00:00:00 2001 From: Juniper Tyree Date: Mon, 27 May 2024 05:57:56 +0000 Subject: [PATCH] Make use of some Arc<[T]>: RustToCuda --- Cargo.lock | 6 ++-- necsim/core/Cargo.toml | 4 +-- necsim/impls/cuda/Cargo.toml | 4 +-- necsim/impls/no-std/Cargo.toml | 4 +-- .../no-std/src/cogs/habitat/in_memory.rs | 31 +++++-------------- .../src/cogs/turnover_rate/in_memory.rs | 30 +++--------------- rustcoalescence/algorithms/cuda/Cargo.toml | 2 +- .../algorithms/cuda/cpu-kernel/Cargo.toml | 2 +- .../algorithms/cuda/gpu-kernel/Cargo.toml | 4 +-- .../src/spatially_explicit/turnover/map.rs | 2 +- .../spatially_explicit/turnover/uniform.rs | 2 +- 11 files changed, 28 insertions(+), 63 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d08ccb3e3..18a108a57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1578,7 +1578,7 @@ dependencies = [ [[package]] name = "rust-cuda" version = "0.1.0" -source = "git+https://github.com/juntyr/rust-cuda?rev=25735d0#25735d08782daf2e8a8e4afe74c1867f02e970f7" +source = "git+https://github.com/juntyr/rust-cuda?rev=f2a377d#f2a377d828f361423c411303118b1753864e7ce3" dependencies = [ "const-type-layout", "final", @@ -1595,7 +1595,7 @@ dependencies = [ [[package]] name = "rust-cuda-derive" version = "0.1.0" -source = "git+https://github.com/juntyr/rust-cuda?rev=25735d0#25735d08782daf2e8a8e4afe74c1867f02e970f7" +source = "git+https://github.com/juntyr/rust-cuda?rev=f2a377d#f2a377d828f361423c411303118b1753864e7ce3" dependencies = [ "proc-macro-error", "proc-macro2", @@ -1606,7 +1606,7 @@ dependencies = [ [[package]] name = "rust-cuda-kernel" version = "0.1.0" -source = "git+https://github.com/juntyr/rust-cuda?rev=25735d0#25735d08782daf2e8a8e4afe74c1867f02e970f7" +source = "git+https://github.com/juntyr/rust-cuda?rev=f2a377d#f2a377d828f361423c411303118b1753864e7ce3" dependencies = [ "cargo_metadata", "colored", diff --git a/necsim/core/Cargo.toml b/necsim/core/Cargo.toml index 7697f7b59..cfc66b28b 100644 --- a/necsim/core/Cargo.toml +++ b/necsim/core/Cargo.toml @@ -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 } diff --git a/necsim/impls/cuda/Cargo.toml b/necsim/impls/cuda/Cargo.toml index 467bd22fc..0bc4114f3 100644 --- a/necsim/impls/cuda/Cargo.toml +++ b/necsim/impls/cuda/Cargo.toml @@ -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"] } diff --git a/necsim/impls/no-std/Cargo.toml b/necsim/impls/no-std/Cargo.toml index 0843e00e8..a952d12fb 100644 --- a/necsim/impls/no-std/Cargo.toml +++ b/necsim/impls/no-std/Cargo.toml @@ -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 } diff --git a/necsim/impls/no-std/src/cogs/habitat/in_memory.rs b/necsim/impls/no-std/src/cogs/habitat/in_memory.rs index 640fe366a..031a4ff08 100644 --- a/necsim/impls/no-std/src/cogs/habitat/in_memory.rs +++ b/necsim/impls/no-std/src/cogs/habitat/in_memory.rs @@ -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}, @@ -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 { - // TODO: use an Arc #[cfg_attr(feature = "cuda", cuda(embed))] - habitat: Final>, - // TODO: use an Arc + habitat: Arc<[u32]>, #[cfg_attr(feature = "cuda", cuda(embed))] - u64_injection: Final>, + u64_injection: Arc<[u64]>, #[cfg_attr(feature = "cuda", cuda(embed))] extent: LandscapeExtent, marker: PhantomData, } -impl Clone for InMemoryHabitat { - 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::, - } - } -} - #[contract_trait] impl Backup for InMemoryHabitat { 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::, } @@ -187,8 +172,8 @@ impl InMemoryHabitat { 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::, }) diff --git a/necsim/impls/no-std/src/cogs/turnover_rate/in_memory.rs b/necsim/impls/no-std/src/cogs/turnover_rate/in_memory.rs index e489bd68c..f12800b11 100644 --- a/necsim/impls/no-std/src/cogs/turnover_rate/in_memory.rs +++ b/necsim/impls/no-std/src/cogs/turnover_rate/in_memory.rs @@ -1,11 +1,9 @@ #![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; @@ -13,29 +11,11 @@ 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>, -} - -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] @@ -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) diff --git a/rustcoalescence/algorithms/cuda/Cargo.toml b/rustcoalescence/algorithms/cuda/Cargo.toml index 30a822fc8..c91617175 100644 --- a/rustcoalescence/algorithms/cuda/Cargo.toml +++ b/rustcoalescence/algorithms/cuda/Cargo.toml @@ -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"] } diff --git a/rustcoalescence/algorithms/cuda/cpu-kernel/Cargo.toml b/rustcoalescence/algorithms/cuda/cpu-kernel/Cargo.toml index 101cb1370..19c722abd 100644 --- a/rustcoalescence/algorithms/cuda/cpu-kernel/Cargo.toml +++ b/rustcoalescence/algorithms/cuda/cpu-kernel/Cargo.toml @@ -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"] } diff --git a/rustcoalescence/algorithms/cuda/gpu-kernel/Cargo.toml b/rustcoalescence/algorithms/cuda/gpu-kernel/Cargo.toml index c16b93f1d..537fcf8de 100644 --- a/rustcoalescence/algorithms/cuda/gpu-kernel/Cargo.toml +++ b/rustcoalescence/algorithms/cuda/gpu-kernel/Cargo.toml @@ -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"] } diff --git a/rustcoalescence/scenarios/src/spatially_explicit/turnover/map.rs b/rustcoalescence/scenarios/src/spatially_explicit/turnover/map.rs index 4d175afae..f53a7f55e 100644 --- a/rustcoalescence/scenarios/src/spatially_explicit/turnover/map.rs +++ b/rustcoalescence/scenarios/src/spatially_explicit/turnover/map.rs @@ -43,7 +43,6 @@ pub enum SpatiallyExplicitTurnoverMapScenarioError { #[derive(Clone)] pub struct SpatiallyExplicitTurnoverMapScenario> { habitat: InMemoryHabitat, - // TODO: use an Arc dispersal_map: Array2D, turnover_rate: InMemoryTurnoverRate, speciation_probability: UniformSpeciationProbability, @@ -116,6 +115,7 @@ impl> Scenario 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); ( diff --git a/rustcoalescence/scenarios/src/spatially_explicit/turnover/uniform.rs b/rustcoalescence/scenarios/src/spatially_explicit/turnover/uniform.rs index 1d1557bdb..5fdb0a382 100644 --- a/rustcoalescence/scenarios/src/spatially_explicit/turnover/uniform.rs +++ b/rustcoalescence/scenarios/src/spatially_explicit/turnover/uniform.rs @@ -41,7 +41,6 @@ pub enum SpatiallyExplicitUniformTurnoverScenarioError { #[derive(Clone)] pub struct SpatiallyExplicitUniformTurnoverScenario> { habitat: InMemoryHabitat, - // TODO: use an Arc dispersal_map: Array2D, turnover_rate: UniformTurnoverRate, speciation_probability: UniformSpeciationProbability, @@ -115,6 +114,7 @@ impl> Scenario 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); (