Debian Codegraph @

cub/benchmarks/nvbench_helper/test/gen_power_law_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/device_vector.h>
29 #include <thrust/host_vector.h>
30
31 #include <algorithm>
32 #include <cmath>
33
34 #include <boost/math/statistics/anderson_darling.hpp>
35 #include <boost/math/statistics/univariate_statistics.hpp>
36 #include <catch2/catch.hpp>
37 #include <nvbench_helper.cuh>
38
39 bool is_normal(thrust::host_vector<double> data)
40 {
41 std::sort(data.begin(), data.end());
42 const double A2 = boost::math::statistics::anderson_darling_normality_statistic(data);
43 return A2 / data.size() < 0.05;
44 }
45
46 using types = nvbench::type_list<uint32_t, uint64_t>;
47
48 TEMPLATE_LIST_TEST_CASE("Generators produce power law distributed data", "[gen][power-law]", types)
49 {
50 const std::size_t elements = 1 << 28;
51 const std::size_t segments = 4 * 1024;
52 const thrust::device_vector<TestType> d_segment_offsets = generate.power_law.segment_offsets(elements, segments);
53 REQUIRE(d_segment_offsets.size() == segments + 1);
54
55 std::size_t actual_elements = 0;
56 thrust::host_vector<double> log_sizes(segments);
57 const thrust::host_vector<TestType> h_segment_offsets = d_segment_offsets;
58 for (int i = 0; i < segments; ++i)
59 {
60 const TestType begin = h_segment_offsets[i];
61 const TestType end = h_segment_offsets[i + 1];
62 REQUIRE(begin <= end);
63
64 const std::size_t size = end - begin;
65 actual_elements += size;
66 log_sizes[i] = std::log(size);
67 }
68
69 REQUIRE(actual_elements == elements);
70 REQUIRE(is_normal(std::move(log_sizes)));
71 }