Skip to content
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

Add Poisson distribution support to Host/Agent random API. #1060

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/flamegpu/runtime/random/AgentRandom.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class AgentRandom {
*/
template<typename T>
__forceinline__ __device__ T logNormal(T mean, T stddev) const;
/**
* Returns a poisson distributed unsigned int according to the provided mean (default 1.0).
* @param mean The mean of the distribution
* @note This implementation uses CURAND's "simple Device API" which is considered the least robust but is more efficient when generating Poisson-distributed random numbers for many different lambdas.
*/
__forceinline__ __device__ unsigned int poisson(double mean = 1.0f) const;
/**
* Returns an integer uniformly distributed in the inclusive range [min, max]
* or
Expand Down Expand Up @@ -110,6 +116,12 @@ template<>
__forceinline__ __device__ double AgentRandom::logNormal(const double mean, const double stddev) const {
return curand_log_normal_double(d_random_state, mean, stddev);
}
/**
* Poisson
*/
__forceinline__ __device__ unsigned int AgentRandom::poisson(const double mean) const {
return curand_poisson(d_random_state, mean);
}
/**
* Uniform Range
*/
Expand Down
14 changes: 14 additions & 0 deletions include/flamegpu/runtime/random/HostRandom.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class HostRandom {
*/
template<typename T>
inline T logNormal(T mean, T stddev) const;
/**
* Returns a poisson distributed unsigned int according to the provided mean (default 1.0).
* @param mean The mean of the distribution
* @note Available as signed and unsigned: char, short, int, long long (default unsigned int)
*/
template<typename T = unsigned int>
inline unsigned int poisson(double mean = 1.0f) const;
/**
* Returns an integer uniformly distributed in the inclusive range [min, max]
* or
Expand Down Expand Up @@ -93,6 +100,13 @@ inline T HostRandom::uniform(const T min, const T max) const {
return rng.getDistribution<T>(dist);
}

template<typename T>
inline unsigned int HostRandom::poisson(const double mean) const {
static_assert(detail::StaticAssert::_Is_IntType<T>::value, "Invalid template argument for HostRandom::poisson(double mean)");
std::poisson_distribution<T> dist(mean);
return rng.getDistribution<T>(dist);
}

/**
* Special cases, std::random doesn't support char, emulate behaviour
*/
Expand Down
3 changes: 3 additions & 0 deletions swig/python/flamegpu.i
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,9 @@ TEMPLATE_VARIABLE_INSTANTIATE_FLOATS(uniform, flamegpu::HostRandom::uniformNoRan
TEMPLATE_VARIABLE_INSTANTIATE_INTS(uniform, flamegpu::HostRandom::uniformRange)
TEMPLATE_VARIABLE_INSTANTIATE_FLOATS(normal, flamegpu::HostRandom::normal)
TEMPLATE_VARIABLE_INSTANTIATE_FLOATS(logNormal, flamegpu::HostRandom::logNormal)
TEMPLATE_VARIABLE_INSTANTIATE_INTS(poisson, flamegpu::HostRandom::poisson)
// Default typeless should be unsigned int
%template(poisson) flamegpu::HostRandom::poisson<uint32_t>;

// Extend the python to add the pure python class decorators
%pythoncode %{
Expand Down