19#include <cuspatial/cuda_utils.hpp>
23#include <rmm/device_uvector.hpp>
25#include <thrust/execution_policy.h>
26#include <thrust/random.h>
27#include <thrust/random/normal_distribution.h>
28#include <thrust/random/uniform_int_distribution.h>
29#include <thrust/tabulate.h>
31#include <cuda/std/type_traits>
43enum class distribution_id : int8_t {
59using integral_to_realType =
60 std::conditional_t<std::is_floating_point_v<T>,
62 std::conditional_t<
sizeof(T) * 8 <= 23,
float,
double>>;
68auto make_normal_dist(T lower_bound, T upper_bound)
70 using realT = integral_to_realType<T>;
71 T
const mean = lower_bound + (upper_bound - lower_bound) / 2;
72 T
const stddev = (upper_bound - lower_bound) / 6;
73 return thrust::random::normal_distribution<realT>(mean, stddev);
76template <
typename T, std::enable_if_t<std::is_
integral_v<T>, T>* =
nullptr>
77auto make_uniform_dist(T range_start, T range_end)
79 return thrust::uniform_int_distribution<T>(range_start, range_end);
82template <
typename T, std::enable_if_t<std::is_
floating_po
int_v<T>>* =
nullptr>
83auto make_uniform_dist(T range_start, T range_end)
85 return thrust::uniform_real_distribution<T>(range_start, range_end);
89double geometric_dist_p(T range_size)
91 constexpr double percentage_in_range = 0.99;
92 double const p = 1 - exp(log(1 - percentage_in_range) / range_size);
93 return p ? p : std::numeric_limits<double>::epsilon();
104 using realType = integral_to_realType<T>;
105 using super_t = thrust::random::normal_distribution<realType>;
110 using result_type = T;
112 : super_t(0, std::labs(upper_bound - lower_bound) / 4.0),
113 _lower_bound(lower_bound),
114 _upper_bound(upper_bound)
118 template <
typename UniformRandomNumberGenerator>
119 __host__ __device__ result_type operator()(UniformRandomNumberGenerator& urng)
121 return _lower_bound < _upper_bound ? std::abs(super_t::operator()(urng)) + _lower_bound
122 : _lower_bound - std::abs(super_t::operator()(urng));
126template <
typename T,
typename Generator>
128 using result_type = T;
130 value_generator(T lower_bound, T upper_bound, thrust::minstd_rand& engine, Generator gen)
131 : lower_bound(std::min(lower_bound, upper_bound)),
132 upper_bound(std::max(lower_bound, upper_bound)),
138 __device__ T operator()(
size_t n)
141 if constexpr (std::is_integral_v<T> && std::is_floating_point_v<
decltype(dist(engine))>) {
142 return std::clamp(
static_cast<T
>(std::round(dist(engine))), lower_bound, upper_bound);
144 return std::clamp(dist(engine), lower_bound, upper_bound);
150 thrust::minstd_rand engine;
154template <
typename T,
typename Generator>
156 using Cart2D = cuspatial::vec_2d<T>;
157 value_generator<T, Generator> vgenx;
158 value_generator<T, Generator> vgeny;
161 vec_2d<T> upper_right,
162 thrust::minstd_rand& engine_x,
163 thrust::minstd_rand& engine_y,
166 : vgenx(lower_left.x, upper_right.x, engine_x, gen_x),
167 vgeny(lower_left.y, upper_right.y, engine_y, gen_y)
171 __device__ Cart2D operator()(
size_t n) {
return {vgenx(n), vgeny(n)}; }
177auto deterministic_engine(
unsigned seed) {
return thrust::minstd_rand{seed}; }
Generates a geometric distribution between lower_bound and upper_bound. This distribution is an appro...