-
Notifications
You must be signed in to change notification settings - Fork 2
Implement downscaling for the almost-infinite scenarios #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
juntyr
wants to merge
22
commits into
main
Choose a base branch
from
almost-infinite-downsampled
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
62160cd
Early ... no late drafting
juntyr f913a0b
More progress on dispersal sampler + started with scenario
juntyr d14906a
Small further progress on origin sampler
juntyr 81e9bbc
Further progress on unscaled scenario dispatch
juntyr c9abdf3
Minor cleanup
juntyr 89cc93b
Feature-gate cuda(embed) for dispersal sampler
juntyr 60c7fe4
Implement self-dispersal probability using numerical sampling
juntyr 0534eb8
Implement args parsing for downscaled scenarios
juntyr 465ed80
Small fixes
juntyr 7b0c6e7
Small fixes + hacky faster dispersal
juntyr 3796091
Use precomputed dispersal in downscaled non-self-dispersal sampler
juntyr 4b40dbf
Allow configuring sampling and non-self-dispersal strategy
juntyr 3e57737
Fix sample_index_u64 for CUDA
juntyr 8da048f
Small fix
juntyr 60cafd8
Fix docs typo
juntyr 3ff4ca1
Upgrade MSRV to 1.81
juntyr e3b4ba3
Start expecting some lints
juntyr 6f0bc53
Fix clippy lints
juntyr 0ad71b3
Upgrade to latest rust-cuda with syn v2.0
juntyr 9bedd00
Bump MSRV to 1.82-nightly
juntyr 7d80526
Fix clippy lints
juntyr 3a961bd
Update rust-cuda to the latest version
juntyr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
necsim/impls/no-std/src/cogs/dispersal_sampler/almost_infinite_downscaled.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
use core::marker::PhantomData; | ||
|
||
use necsim_core::{ | ||
cogs::{DispersalSampler, MathsCore, RngCore, RngSampler, SeparableDispersalSampler}, | ||
landscape::Location, | ||
}; | ||
use necsim_core_bond::ClosedUnitF64; | ||
|
||
use crate::cogs::habitat::almost_infinite::{ | ||
downscaled::AlmostInfiniteDownscaledHabitat, AlmostInfiniteHabitat, | ||
}; | ||
|
||
#[allow(clippy::module_name_repetitions)] | ||
#[derive(Debug)] | ||
#[cfg_attr(feature = "cuda", derive(rust_cuda::lend::LendRustToCuda))] | ||
#[cfg_attr(feature = "cuda", cuda(free = "M", free = "G"))] | ||
pub struct AlmostInfiniteDownscaledDispersalSampler< | ||
M: MathsCore, | ||
G: RngCore<M>, | ||
D: Clone + DispersalSampler<M, AlmostInfiniteHabitat<M>, G>, | ||
> { | ||
#[cuda(embed)] | ||
dispersal: D, | ||
marker: PhantomData<(M, G)>, | ||
} | ||
|
||
impl<M: MathsCore, G: RngCore<M>, D: Clone + DispersalSampler<M, AlmostInfiniteHabitat<M>, G>> | ||
AlmostInfiniteDownscaledDispersalSampler<M, G, D> | ||
{ | ||
#[must_use] | ||
pub fn new(dispersal: D) -> Self { | ||
Self { | ||
dispersal, | ||
marker: PhantomData::<(M, G)>, | ||
} | ||
} | ||
} | ||
|
||
impl<M: MathsCore, G: RngCore<M>, D: Clone + DispersalSampler<M, AlmostInfiniteHabitat<M>, G>> Clone | ||
for AlmostInfiniteDownscaledDispersalSampler<M, G, D> | ||
{ | ||
fn clone(&self) -> Self { | ||
Self { | ||
dispersal: self.dispersal.clone(), | ||
marker: PhantomData::<(M, G)>, | ||
} | ||
} | ||
} | ||
|
||
#[contract_trait] | ||
impl<M: MathsCore, G: RngCore<M>, D: Clone + DispersalSampler<M, AlmostInfiniteHabitat<M>, G>> | ||
DispersalSampler<M, AlmostInfiniteDownscaledHabitat<M>, G> | ||
for AlmostInfiniteDownscaledDispersalSampler<M, G, D> | ||
{ | ||
#[must_use] | ||
fn sample_dispersal_from_location( | ||
&self, | ||
location: &Location, | ||
habitat: &AlmostInfiniteDownscaledHabitat<M>, | ||
rng: &mut G, | ||
) -> Location { | ||
// TODO: optimise | ||
let sub_index = rng.sample_index_u32(habitat.downscale_area()); | ||
|
||
let index_x = sub_index % (habitat.downscale_x() as u32); | ||
let index_y = sub_index % (habitat.downscale_y() as u32); | ||
|
||
// generate an upscaled location by sampling a random sub-location | ||
let location = Location::new(location.x() + index_x, location.y() + index_y); | ||
|
||
// sample dispersal from the inner dispersal sampler as normal | ||
let target_location = | ||
self.dispersal | ||
.sample_dispersal_from_location(&location, habitat.unscaled(), rng); | ||
|
||
let index_x = target_location.x() % (habitat.downscale_x() as u32); | ||
let index_y = target_location.y() % (habitat.downscale_y() as u32); | ||
|
||
// downscale the target location | ||
Location::new(target_location.x() - index_x, target_location.y() - index_y) | ||
} | ||
} | ||
|
||
#[contract_trait] | ||
impl<M: MathsCore, G: RngCore<M>, D: Clone + DispersalSampler<M, AlmostInfiniteHabitat<M>, G>> | ||
SeparableDispersalSampler<M, AlmostInfiniteDownscaledHabitat<M>, G> | ||
for AlmostInfiniteDownscaledDispersalSampler<M, G, D> | ||
{ | ||
#[must_use] | ||
fn sample_non_self_dispersal_from_location( | ||
&self, | ||
location: &Location, | ||
habitat: &AlmostInfiniteDownscaledHabitat<M>, | ||
rng: &mut G, | ||
) -> Location { | ||
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); | ||
} | ||
|
||
target_location | ||
} | ||
|
||
#[must_use] | ||
fn get_self_dispersal_probability_at_location( | ||
&self, | ||
_location: &Location, | ||
_habitat: &AlmostInfiniteDownscaledHabitat<M>, | ||
) -> ClosedUnitF64 { | ||
unimplemented!() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.