Skip to content

Commit

Permalink
Fix sample_index_u64 for CUDA
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Jun 9, 2024
1 parent 4b40dbf commit 3e57737
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions necsim/core/src/cogs/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ pub trait RngSampler<M: MathsCore>: RngCore<M> {
clippy::cast_sign_loss
)]
let index =
M::floor(self.sample_uniform_closed_open().get() * (length.get() as f64)) as u32;
M::floor(self.sample_uniform_closed_open().get() * (f64::from(length.sub_one()) + 1.0))
as u32;
// Safety in case of f64 rounding errors
index.min(length.sub_one())
}
Expand All @@ -139,7 +140,8 @@ pub trait RngSampler<M: MathsCore>: RngCore<M> {
clippy::cast_sign_loss
)]
let index =
M::floor(self.sample_uniform_closed_open().get() * (length.get() as f64)) as u64;
M::floor(self.sample_uniform_closed_open().get() * ((length.sub_one() as f64) + 1.0))
as u64;
// Safety in case of f64 rounding errors
index.min(length.sub_one())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,26 @@ impl<M: MathsCore, G: RngCore<M>, D: Clone + DispersalSampler<M, AlmostInfiniteH
>= non_self_dispersal.non_self_dispersal_rejection_sampling_probability_threshold()
{
// if the non-self-dispersal probability is larger (or equal) than the
// threshold, use rejection sampling for non-self-dispersal
// threshold, use rejection sampling for non-self-dispersal
// if the threshold is 1.0, we still favour rejection sampling if the
// non-self-dispersal probability is also 1.0 if the threshold is
// 0.0, we always use rejection sampling
// non-self-dispersal probability is also 1.0
// if the threshold is 0.0, we always use rejection sampling
log::info!(
"Rejection sampling is used for separate non-self-dispersal, which has a \
probability of {}%",
self_dispersal_emperical.one_minus().get() * 100.0
);

None
} else {
// if the non-self-dispersal probability is smaller than the threshold,
// precompute a discrete alias sampler for non-self-dispersal
// precompute a discrete alias sampler for non-self-dispersal
log::info!(
"A precomputed alias sampler is used for separate non-self-dispersal, which has a \
probability of {}%",
self_dispersal_emperical.one_minus().get() * 100.0
);

let mut non_self_dispersal = targets
.into_iter()
.filter_map(|(target, count)| {
Expand Down

0 comments on commit 3e57737

Please sign in to comment.