Debian Codegraph @

cub/benchmarks/nvbench_helper/test/gen_uniform_distribution.cu

raw
1 /******************************************************************************
2 * Copyright (c) 2011-2023, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the NVIDIA CORPORATION nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 ******************************************************************************/
27
28 #include <thrust/count.h>
29 #include <thrust/device_vector.h>
30 #include <thrust/host_vector.h>
31
32 #include <cmath>
33 #include <limits>
34 #include <map>
35
36 #include <boost/math/distributions/chi_squared.hpp>
37 #include <catch2/catch.hpp>
38 #include <nvbench_helper.cuh>
39
40 template <typename T>
41 bool is_uniform(thrust::host_vector<T> data, T min, T max)
42 {
43 const double value_range = static_cast<double>(max) - min;
44 const bool exact_binning = value_range < (1 << 20);
45 const int number_of_bins = exact_binning ? static_cast<int>(max - min + 1) : static_cast<int>(std::sqrt(data.size()));
46 thrust::host_vector<int> bins(number_of_bins, 0);
47
48 const double interval = value_range / static_cast<double>(number_of_bins);
49 const double expected_count = static_cast<double>(data.size()) / number_of_bins;
50
51 for (T val : data)
52 {
53 int bin_index = exact_binning ? val - min : (val - static_cast<double>(min)) / interval;
54
55 if (bin_index >= 0 && bin_index < number_of_bins)
56 {
57 bins[bin_index]++;
58 }
59 }
60
61 double chi_square = 0.0;
62 for (const auto& count : bins)
63 {
64 chi_square += std::pow(count - expected_count, 2) / expected_count;
65 }
66
67 boost::math::chi_squared_distribution<double> chi_squared_dist(number_of_bins - 1);
68
69 const double confidence = 0.95;
70 const double critical_value = boost::math::quantile(chi_squared_dist, confidence);
71
72 return chi_square <= critical_value;
73 }
74
75 using types =
76 nvbench::type_list<int8_t,
77 int16_t,
78 int32_t,
79 int64_t,
80 #if NVBENCH_HELPER_HAS_I128
81 int128_t,
82 #endif
83 float,
84 double>;
85
86 TEMPLATE_LIST_TEST_CASE("Generators produce uniformly distributed data", "[gen][uniform]", types)
87 {
88 const std::size_t elements = 1 << GENERATE_COPY(16, 20, 24, 28);
89 const TestType min = std::numeric_limits<TestType>::min();
90 const TestType max = std::numeric_limits<TestType>::max();
91
92 const thrust::device_vector<TestType> data = generate(elements, bit_entropy::_1_000, min, max);
93
94 REQUIRE(is_uniform<TestType>(data, min, max));
95 }
96
97 struct complex_to_real_t
98 {
99 __host__ __device__ float operator()(const complex& c) const
100 {
101 return c.real();
102 }
103 };
104
105 struct complex_to_imag_t
106 {
107 __host__ __device__ float operator()(const complex& c) const
108 {
109 return c.imag();
110 }
111 };
112
113 TEST_CASE("Generators produce uniformly distributed complex", "[gen]")
114 {
115 const float min = std::numeric_limits<float>::min();
116 const float max = std::numeric_limits<float>::max();
117
118 const thrust::device_vector<complex> data = generate(1 << 16);
119
120 thrust::device_vector<float> component(data.size());
121 thrust::transform(data.begin(), data.end(), component.begin(), complex_to_real_t());
122 REQUIRE(is_uniform<float>(component, min, max));
123
124 thrust::transform(data.begin(), data.end(), component.begin(), complex_to_imag_t());
125 REQUIRE(is_uniform<float>(component, min, max));
126 }
127
128 TEST_CASE("Generators produce uniformly distributed bools", "[gen]")
129 {
130 const thrust::device_vector<bool> data = generate(1 << 24, bit_entropy::_0_544);
131
132 const std::size_t falses = thrust::count(data.begin(), data.end(), false);
133 const std::size_t trues = thrust::count(data.begin(), data.end(), true);
134
135 REQUIRE(falses > 0);
136 REQUIRE(trues > 0);
137 REQUIRE(falses + trues == data.size());
138
139 const double ratio = static_cast<double>(falses) / trues;
140 REQUIRE(ratio > 0.7);
141 }
142
143 using offsets = nvbench::type_list<uint32_t, uint64_t>;
144
145 TEMPLATE_LIST_TEST_CASE("Generators produce uniformly distributed offsets", "[gen]", offsets)
146 {
147 const std::size_t min_segment_size = 1;
148 const std::size_t max_segment_size = 256;
149 const std::size_t elements = 1 << GENERATE_COPY(16, 20, 24, 28);
150 const thrust::device_vector<TestType> d_segments =
151 generate.uniform.segment_offsets(elements, min_segment_size, max_segment_size);
152 const thrust::host_vector<TestType> h_segments = d_segments;
153 const std::size_t num_segments = h_segments.size() - 1;
154
155 std::size_t actual_elements = 0;
156 thrust::host_vector<int> segment_sizes(num_segments);
157 for (std::size_t sid = 0; sid < num_segments; sid++)
158 {
159 const TestType begin = h_segments[sid];
160 const TestType end = h_segments[sid + 1];
161 REQUIRE(begin <= end);
162
163 const TestType size = end - begin;
164 REQUIRE(size >= min_segment_size);
165 REQUIRE(size <= max_segment_size);
166
167 segment_sizes[sid] = size;
168 actual_elements += size;
169 }
170
171 REQUIRE(actual_elements == elements);
172 REQUIRE(is_uniform<int>(std::move(segment_sizes), min_segment_size, max_segment_size));
173 }
174
175 TEMPLATE_LIST_TEST_CASE("Generators produce uniformly distributed key segments", "[gen]", types)
176 {
177 const std::size_t min_segment_size = 1;
178 const std::size_t max_segment_size = 128;
179 const std::size_t elements = 1 << GENERATE_COPY(16, 20, 24, 28);
180 const thrust::device_vector<TestType> d_keys =
181 generate.uniform.key_segments(elements, min_segment_size, max_segment_size);
182 REQUIRE(d_keys.size() == elements);
183
184 const thrust::host_vector<TestType> h_keys = d_keys;
185
186 thrust::host_vector<int> segment_sizes;
187
188 TestType prev = h_keys[0];
189 int length = 1;
190
191 for (std::size_t kid = 1; kid < elements; kid++)
192 {
193 TestType next = h_keys[kid];
194
195 if (next == prev)
196 {
197 length++;
198 }
199 else
200 {
201 REQUIRE(length >= min_segment_size);
202 REQUIRE(length <= max_segment_size);
203
204 segment_sizes.push_back(length);
205
206 prev = next;
207 length = 1;
208 }
209 }
210 REQUIRE(length >= min_segment_size);
211 REQUIRE(length <= max_segment_size);
212 segment_sizes.push_back(length);
213
214 REQUIRE(is_uniform<int>(std::move(segment_sizes), min_segment_size, max_segment_size));
215 }