Loading...
Searching...
No Matches
random.cuh
1/*
2 * Copyright (c) 2020-2022, 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 <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>
30
31#include <cuda/std/type_traits>
32
33#include <algorithm>
34#include <memory>
35
36namespace cuspatial {
37
38namespace test {
39
43enum class distribution_id : int8_t {
44 UNIFORM,
46 NORMAL,
48 GEOMETRIC,
50};
51
58template <typename T>
59using integral_to_realType =
60 std::conditional_t<std::is_floating_point_v<T>,
61 T,
62 std::conditional_t<sizeof(T) * 8 <= 23, float, double>>;
63
67template <typename T>
68auto make_normal_dist(T lower_bound, T upper_bound)
69{
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);
74}
75
76template <typename T, std::enable_if_t<std::is_integral_v<T>, T>* = nullptr>
77auto make_uniform_dist(T range_start, T range_end)
78{
79 return thrust::uniform_int_distribution<T>(range_start, range_end);
80}
81
82template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
83auto make_uniform_dist(T range_start, T range_end)
84{
85 return thrust::uniform_real_distribution<T>(range_start, range_end);
86}
87
88template <typename T>
89double geometric_dist_p(T range_size)
90{
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();
94}
95
102template <typename T>
103class geometric_distribution : public thrust::random::normal_distribution<integral_to_realType<T>> {
104 using realType = integral_to_realType<T>;
105 using super_t = thrust::random::normal_distribution<realType>;
106 T _lower_bound;
107 T _upper_bound;
108
109 public:
110 using result_type = T;
111 __host__ __device__ explicit geometric_distribution(T lower_bound, T upper_bound)
112 : super_t(0, std::labs(upper_bound - lower_bound) / 4.0),
113 _lower_bound(lower_bound),
114 _upper_bound(upper_bound)
115 {
116 }
117
118 template <typename UniformRandomNumberGenerator>
119 __host__ __device__ result_type operator()(UniformRandomNumberGenerator& urng)
120 {
121 return _lower_bound < _upper_bound ? std::abs(super_t::operator()(urng)) + _lower_bound
122 : _lower_bound - std::abs(super_t::operator()(urng));
123 }
124};
125
126template <typename T, typename Generator>
128 using result_type = T;
129
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)),
133 engine(engine),
134 dist(gen)
135 {
136 }
137
138 __device__ T operator()(size_t n)
139 {
140 engine.discard(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);
143 } else {
144 return std::clamp(dist(engine), lower_bound, upper_bound);
145 }
146 }
147
148 T lower_bound;
149 T upper_bound;
150 thrust::minstd_rand engine;
151 Generator dist;
152};
153
154template <typename T, typename Generator>
156 using Cart2D = cuspatial::vec_2d<T>;
157 value_generator<T, Generator> vgenx;
158 value_generator<T, Generator> vgeny;
159
160 point_generator(vec_2d<T> lower_left,
161 vec_2d<T> upper_right,
162 thrust::minstd_rand& engine_x,
163 thrust::minstd_rand& engine_y,
164 Generator gen_x,
165 Generator gen_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)
168 {
169 }
170
171 __device__ Cart2D operator()(size_t n) { return {vgenx(n), vgeny(n)}; }
172};
173
177auto deterministic_engine(unsigned seed) { return thrust::minstd_rand{seed}; }
178
179} // namespace test
180
181} // namespace cuspatial
Generates a geometric distribution between lower_bound and upper_bound. This distribution is an appro...
Definition random.cuh:103