Debian Codegraph @

cub/benchmarks/nvbench_helper/test/gen_entropy.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 <cub/device/device_run_length_encode.cuh>
29
30 #include <thrust/count.h>
31 #include <thrust/execution_policy.h>
32 #include <thrust/host_vector.h>
33 #include <thrust/sort.h>
34 #include <thrust/transform.h>
35
36 #include <algorithm>
37 #include <array>
38
39 #include <catch2/catch.hpp>
40 #include <nvbench_helper.cuh>
41
42 template <class T>
43 double get_expected_entropy(bit_entropy in_entropy)
44 {
45 if (in_entropy == bit_entropy::_0_000)
46 {
47 return 0.0;
48 }
49
50 if (in_entropy == bit_entropy::_1_000)
51 {
52 return sizeof(T) * 8;
53 }
54
55 const int samples = static_cast<int>(in_entropy) + 1;
56 const double p1 = std::pow(0.5, samples);
57 const double p2 = 1 - p1;
58 const double entropy = (-p1 * std::log2(p1)) + (-p2 * std::log2(p2));
59 return sizeof(T) * 8 * entropy;
60 }
61
62 template <class T>
63 double compute_actual_entropy(thrust::device_vector<T> in)
64 {
65 const int n = static_cast<int>(in.size());
66
67 #if THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA
68 thrust::device_vector<T> unique(n);
69 thrust::device_vector<int> counts(n);
70 thrust::device_vector<int> num_runs(1);
71 thrust::sort(in.begin(), in.end(), less_t{});
72
73 // RLE
74 void* d_temp_storage = nullptr;
75 std::size_t temp_storage_bytes = 0;
76
77 T* d_in = thrust::raw_pointer_cast(in.data());
78 T* d_unique_out = thrust::raw_pointer_cast(unique.data());
79 int* d_counts_out = thrust::raw_pointer_cast(counts.data());
80 int* d_num_runs_out = thrust::raw_pointer_cast(num_runs.data());
81
82 cub::DeviceRunLengthEncode::Encode(
83 d_temp_storage, temp_storage_bytes, d_in, d_unique_out, d_counts_out, d_num_runs_out, n);
84
85 thrust::device_vector<std::uint8_t> temp_storage(temp_storage_bytes);
86 d_temp_storage = thrust::raw_pointer_cast(temp_storage.data());
87
88 cub::DeviceRunLengthEncode::Encode(
89 d_temp_storage, temp_storage_bytes, d_in, d_unique_out, d_counts_out, d_num_runs_out, n);
90
91 thrust::host_vector<int> h_counts = counts;
92 thrust::host_vector<int> h_num_runs = num_runs;
93 #else
94 std::vector<T> h_in(in.begin(), in.end());
95 std::sort(h_in.begin(), h_in.end(), less_t{});
96 thrust::host_vector<int> h_counts;
97 T prev = h_in[0];
98 int length = 1;
99
100 for (std::size_t i = 1; i < h_in.size(); i++)
101 {
102 const T next = h_in[i];
103 if (next == prev)
104 {
105 length++;
106 }
107 else
108 {
109 h_counts.push_back(length);
110 prev = next;
111 length = 1;
112 }
113 }
114 h_counts.push_back(length);
115
116 thrust::host_vector<int> h_num_runs(1, h_counts.size());
117 #endif
118
119 // normalize counts
120 thrust::host_vector<double> ps(h_num_runs[0]);
121 for (std::size_t i = 0; i < ps.size(); i++)
122 {
123 ps[i] = static_cast<double>(h_counts[i]) / n;
124 }
125
126 double entropy = 0.0;
127
128 if (ps.size())
129 {
130 for (double p : ps)
131 {
132 entropy -= p * std::log2(p);
133 }
134 }
135
136 return entropy;
137 }
138
139 TEMPLATE_LIST_TEST_CASE("Generators produce data with given entropy", "[gen]", fundamental_types)
140 {
141 constexpr int num_entropy_levels = 6;
142 std::array<bit_entropy, num_entropy_levels> entropy_levels{
143 bit_entropy::_0_000,
144 bit_entropy::_0_201,
145 bit_entropy::_0_337,
146 bit_entropy::_0_544,
147 bit_entropy::_0_811,
148 bit_entropy::_1_000};
149
150 std::vector<double> entropy(num_entropy_levels);
151 std::transform(entropy_levels.cbegin(), entropy_levels.cend(), entropy.begin(), [](bit_entropy entropy) {
152 const thrust::device_vector<TestType> data = generate(1 << 24, entropy);
153 return compute_actual_entropy(data);
154 });
155
156 REQUIRE(std::is_sorted(entropy.begin(), entropy.end(), less_t{}));
157 REQUIRE(std::unique(entropy.begin(), entropy.end()) == entropy.end());
158 }
159
160 TEST_CASE("Generators support bool", "[gen]")
161 {
162 constexpr int num_entropy_levels = 6;
163 std::array<bit_entropy, num_entropy_levels> entropy_levels{
164 bit_entropy::_0_000,
165 bit_entropy::_0_201,
166 bit_entropy::_0_337,
167 bit_entropy::_0_544,
168 bit_entropy::_0_811,
169 bit_entropy::_1_000};
170
171 std::vector<std::size_t> number_of_set(num_entropy_levels);
172 std::transform(entropy_levels.cbegin(), entropy_levels.cend(), number_of_set.begin(), [](bit_entropy entropy) {
173 const thrust::device_vector<bool> data = generate(1 << 24, entropy);
174 return thrust::count(data.begin(), data.end(), true);
175 });
176
177 REQUIRE(std::is_sorted(number_of_set.begin(), number_of_set.end()));
178 REQUIRE(std::unique(number_of_set.begin(), number_of_set.end()) == number_of_set.end());
179 }