Loading...
Searching...
No Matches
random.cuh
1/*
2 * Copyright (c) 2020-2024, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuspatial/cuda_utils.hpp>
20#include <cuspatial/error.hpp>
22
23#include <rmm/device_uvector.hpp>
24
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>
31
32#include <algorithm>
33#include <memory>
34
35namespace cuspatial {
36
37namespace test {
38
42enum class distribution_id : int8_t {
43 UNIFORM,
45 NORMAL,
47 GEOMETRIC,
49};
50
57template <typename T>
58using integral_to_realType =
59 std::conditional_t<std::is_floating_point_v<T>,
60 T,
61 std::conditional_t<sizeof(T) * 8 <= 23, float, double>>;
62
66template <typename T>
67auto make_normal_dist(T lower_bound, T upper_bound)
68{
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);
73}
74
75template <typename T, std::enable_if_t<std::is_integral_v<T>, T>* = nullptr>
76auto make_uniform_dist(T range_start, T range_end)
77{
78 return thrust::uniform_int_distribution<T>(range_start, range_end);
79}
80
81template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
82auto make_uniform_dist(T range_start, T range_end)
83{
84 return thrust::uniform_real_distribution<T>(range_start, range_end);
85}
86
87template <typename T>
88double geometric_dist_p(T range_size)
89{
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();
93}
94
101template <typename T>
102class geometric_distribution : public thrust::random::normal_distribution<integral_to_realType<T>> {
103 using realType = integral_to_realType<T>;
104 using super_t = thrust::random::normal_distribution<realType>;
105 T _lower_bound;
106 T _upper_bound;
107
108 public:
109 using result_type = T;
110 __host__ __device__ explicit geometric_distribution(T lower_bound, T upper_bound)
111 : super_t(0, std::labs(upper_bound - lower_bound) / 4.0),
112 _lower_bound(lower_bound),
113 _upper_bound(upper_bound)
114 {
115 }
116
117 template <typename UniformRandomNumberGenerator>
118 __host__ __device__ result_type operator()(UniformRandomNumberGenerator& urng)
119 {
120 return _lower_bound < _upper_bound ? std::abs(super_t::operator()(urng)) + _lower_bound
121 : _lower_bound - std::abs(super_t::operator()(urng));
122 }
123};
124
125template <typename T, typename Generator>
127 using result_type = T;
128
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)),
132 engine(engine),
133 dist(gen)
134 {
135 }
136
137 __device__ T operator()(size_t n)
138 {
139 engine.discard(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);
142 } else {
143 return std::clamp(dist(engine), lower_bound, upper_bound);
144 }
145 }
146
147 T lower_bound;
148 T upper_bound;
149 thrust::minstd_rand engine;
150 Generator dist;
151};
152
153template <typename T, typename Generator>
155 using Cart2D = cuspatial::vec_2d<T>;
156 value_generator<T, Generator> vgenx;
157 value_generator<T, Generator> vgeny;
158
159 point_generator(vec_2d<T> lower_left,
160 vec_2d<T> upper_right,
161 thrust::minstd_rand& engine_x,
162 thrust::minstd_rand& engine_y,
163 Generator gen_x,
164 Generator gen_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)
167 {
168 }
169
170 __device__ Cart2D operator()(size_t n) { return {vgenx(n), vgeny(n)}; }
171};
172
176auto deterministic_engine(unsigned seed) { return thrust::minstd_rand{seed}; }
177
178} // namespace test
179
180} // namespace cuspatial
Generates a geometric distribution between lower_bound and upper_bound. This distribution is an appro...
Definition random.cuh:102