19#include <cuspatial/cuda_utils.hpp>
23#include <rmm/device_uvector.hpp>
25#include <cuda/std/type_traits>
26#include <thrust/execution_policy.h>
27#include <thrust/random.h>
28#include <thrust/random/normal_distribution.h>
29#include <thrust/random/uniform_int_distribution.h>
30#include <thrust/tabulate.h>
42enum class distribution_id : int8_t {
58using integral_to_realType =
59 std::conditional_t<std::is_floating_point_v<T>,
61 std::conditional_t<
sizeof(T) * 8 <= 23,
float,
double>>;
67auto make_normal_dist(T lower_bound, T upper_bound)
69 using realT = integral_to_realType<T>;
70 T
const mean = lower_bound + (upper_bound - lower_bound) / 2;
71 T
const stddev = (upper_bound - lower_bound) / 6;
72 return thrust::random::normal_distribution<realT>(mean, stddev);
75template <
typename T, std::enable_if_t<std::is_
integral_v<T>, T>* =
nullptr>
76auto make_uniform_dist(T range_start, T range_end)
78 return thrust::uniform_int_distribution<T>(range_start, range_end);
81template <
typename T, std::enable_if_t<std::is_
floating_po
int_v<T>>* =
nullptr>
82auto make_uniform_dist(T range_start, T range_end)
84 return thrust::uniform_real_distribution<T>(range_start, range_end);
88double geometric_dist_p(T range_size)
90 constexpr double percentage_in_range = 0.99;
91 double const p = 1 - exp(log(1 - percentage_in_range) / range_size);
92 return p ? p : std::numeric_limits<double>::epsilon();
103 using realType = integral_to_realType<T>;
104 using super_t = thrust::random::normal_distribution<realType>;
109 using result_type = T;
111 : super_t(0, std::labs(upper_bound - lower_bound) / 4.0),
112 _lower_bound(lower_bound),
113 _upper_bound(upper_bound)
117 template <
typename UniformRandomNumberGenerator>
118 __host__ __device__ result_type operator()(UniformRandomNumberGenerator& urng)
120 return _lower_bound < _upper_bound ? std::abs(super_t::operator()(urng)) + _lower_bound
121 : _lower_bound - std::abs(super_t::operator()(urng));
125template <
typename T,
typename Generator>
127 using result_type = T;
129 value_generator(T lower_bound, T upper_bound, thrust::minstd_rand& engine, Generator gen)
130 : lower_bound(std::min(lower_bound, upper_bound)),
131 upper_bound(std::max(lower_bound, upper_bound)),
137 __device__ T operator()(
size_t n)
140 if constexpr (std::is_integral_v<T> && std::is_floating_point_v<
decltype(dist(engine))>) {
141 return std::clamp(
static_cast<T
>(std::round(dist(engine))), lower_bound, upper_bound);
143 return std::clamp(dist(engine), lower_bound, upper_bound);
149 thrust::minstd_rand engine;
153template <
typename T,
typename Generator>
155 using Cart2D = cuspatial::vec_2d<T>;
156 value_generator<T, Generator> vgenx;
157 value_generator<T, Generator> vgeny;
160 vec_2d<T> upper_right,
161 thrust::minstd_rand& engine_x,
162 thrust::minstd_rand& engine_y,
165 : vgenx(lower_left.x, upper_right.x, engine_x, gen_x),
166 vgeny(lower_left.y, upper_right.y, engine_y, gen_y)
170 __device__ Cart2D operator()(
size_t n) {
return {vgenx(n), vgeny(n)}; }
176auto deterministic_engine(
unsigned seed) {
return thrust::minstd_rand{seed}; }
Generates a geometric distribution between lower_bound and upper_bound. This distribution is an appro...