-
Notifications
You must be signed in to change notification settings - Fork 89
/
distinct_count_estimator.cuh
287 lines (257 loc) · 10.3 KB
/
distinct_count_estimator.cuh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cuco/detail/hyperloglog/hyperloglog.cuh>
#include <cuco/distinct_count_estimator_ref.cuh>
#include <cuco/hash_functions.cuh>
#include <cuco/types.cuh>
#include <cuco/utility/allocator.hpp>
#include <cuco/utility/cuda_thread_scope.cuh>
#include <cuda/stream_ref>
#include <cstddef>
#include <iterator>
#include <memory>
namespace cuco {
/**
* @brief A GPU-accelerated utility for approximating the number of distinct items in a multiset.
*
* @note This implementation is based on the HyperLogLog++ algorithm:
* https://static.googleusercontent.com/media/research.google.com/de//pubs/archive/40671.pdf.
*
* @tparam T Type of items to count
* @tparam Scope The scope in which operations will be performed by individual threads
* @tparam Hash Hash function used to hash items
* @tparam Allocator Type of allocator used for device storage
*/
template <class T,
cuda::thread_scope Scope = cuda::thread_scope_device,
class Hash = cuco::xxhash_64<T>,
class Allocator = cuco::cuda_allocator<std::byte>>
class distinct_count_estimator {
using impl_type = detail::hyperloglog<T, Scope, Hash, Allocator>;
public:
static constexpr auto thread_scope = impl_type::thread_scope; ///< CUDA thread scope
template <cuda::thread_scope NewScope = thread_scope>
using ref_type = cuco::distinct_count_estimator_ref<T, NewScope, Hash>; ///< Non-owning reference
///< type
using value_type = typename impl_type::value_type; ///< Type of items to count
using hasher = typename impl_type::hasher; ///< Type of hash function
using allocator_type = typename impl_type::allocator_type; ///< Allocator type
// TODO enable CTAD
/**
* @brief Constructs a `distinct_count_estimator` host object.
*
* @note This function synchronizes the given stream.
*
* @param sketch_size_kb Maximum sketch size in KB
* @param hash The hash function used to hash items
* @param alloc Allocator used for allocating device storage
* @param stream CUDA stream used to initialize the object
*/
constexpr distinct_count_estimator(cuco::sketch_size_kb sketch_size_kb = 32_KB,
Hash const& hash = {},
Allocator const& alloc = {},
cuda::stream_ref stream = {});
/**
* @brief Constructs a `distinct_count_estimator` host object.
*
* @note This function synchronizes the given stream.
*
* @param standard_deviation Desired standard deviation for the approximation error
* @param hash The hash function used to hash items
* @param alloc Allocator used for allocating device storage
* @param stream CUDA stream used to initialize the object
*/
constexpr distinct_count_estimator(cuco::standard_deviation standard_deviation,
Hash const& hash = {},
Allocator const& alloc = {},
cuda::stream_ref stream = {});
~distinct_count_estimator() = default;
distinct_count_estimator(distinct_count_estimator const&) = delete;
distinct_count_estimator& operator=(distinct_count_estimator const&) = delete;
distinct_count_estimator(distinct_count_estimator&&) = default; ///< Move constructor
/**
* @brief Copy-assignment operator.
*
* @return Copy of `*this`
*/
distinct_count_estimator& operator=(distinct_count_estimator&&) = default;
/**
* @brief Asynchronously resets the estimator, i.e., clears the current count estimate.
*
* @param stream CUDA stream this operation is executed in
*/
constexpr void clear_async(cuda::stream_ref stream = {}) noexcept;
/**
* @brief Resets the estimator, i.e., clears the current count estimate.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `clear_async`.
*
* @param stream CUDA stream this operation is executed in
*/
constexpr void clear(cuda::stream_ref stream = {});
/**
* @brief Asynchronously adds to be counted items to the estimator.
*
* @tparam InputIt Device accessible random access input iterator where
* <tt>std::is_convertible<std::iterator_traits<InputIt>::value_type,
* T></tt> is `true`
*
* @param first Beginning of the sequence of items
* @param last End of the sequence of items
* @param stream CUDA stream this operation is executed in
*/
template <class InputIt>
constexpr void add_async(InputIt first, InputIt last, cuda::stream_ref stream = {});
/**
* @brief Adds to be counted items to the estimator.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `add_async`.
*
* @tparam InputIt Device accessible random access input iterator where
* <tt>std::is_convertible<std::iterator_traits<InputIt>::value_type,
* T></tt> is `true`
*
* @param first Beginning of the sequence of items
* @param last End of the sequence of items
* @param stream CUDA stream this operation is executed in
*/
template <class InputIt>
constexpr void add(InputIt first, InputIt last, cuda::stream_ref stream = {});
/**
* @brief Asynchronously merges the result of `other` estimator into `*this` estimator.
*
* @throw If this->sketch_bytes() != other.sketch_bytes()
*
* @tparam OtherScope Thread scope of `other` estimator
* @tparam OtherAllocator Allocator type of `other` estimator
*
* @param other Other estimator to be merged into `*this`
* @param stream CUDA stream this operation is executed in
*/
template <cuda::thread_scope OtherScope, class OtherAllocator>
constexpr void merge_async(
distinct_count_estimator<T, OtherScope, Hash, OtherAllocator> const& other,
cuda::stream_ref stream = {});
/**
* @brief Merges the result of `other` estimator into `*this` estimator.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `merge_async`.
*
* @throw If this->sketch_bytes() != other.sketch_bytes()
*
* @tparam OtherScope Thread scope of `other` estimator
* @tparam OtherAllocator Allocator type of `other` estimator
*
* @param other Other estimator to be merged into `*this`
* @param stream CUDA stream this operation is executed in
*/
template <cuda::thread_scope OtherScope, class OtherAllocator>
constexpr void merge(distinct_count_estimator<T, OtherScope, Hash, OtherAllocator> const& other,
cuda::stream_ref stream = {});
/**
* @brief Asynchronously merges the result of `other` estimator reference into `*this` estimator.
*
* @throw If this->sketch_bytes() != other.sketch_bytes()
*
* @tparam OtherScope Thread scope of `other` estimator
*
* @param other_ref Other estimator reference to be merged into `*this`
* @param stream CUDA stream this operation is executed in
*/
template <cuda::thread_scope OtherScope>
constexpr void merge_async(ref_type<OtherScope> const& other_ref, cuda::stream_ref stream = {});
/**
* @brief Merges the result of `other` estimator reference into `*this` estimator.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `merge_async`.
*
* @throw If this->sketch_bytes() != other.sketch_bytes()
*
* @tparam OtherScope Thread scope of `other` estimator
*
* @param other_ref Other estimator reference to be merged into `*this`
* @param stream CUDA stream this operation is executed in
*/
template <cuda::thread_scope OtherScope>
constexpr void merge(ref_type<OtherScope> const& other_ref, cuda::stream_ref stream = {});
/**
* @brief Compute the estimated distinct items count.
*
* @note This function synchronizes the given stream.
*
* @param stream CUDA stream this operation is executed in
*
* @return Approximate distinct items count
*/
[[nodiscard]] constexpr std::size_t estimate(cuda::stream_ref stream = {}) const;
/**
* @brief Get device ref.
*
* @return Device ref object of the current `distinct_count_estimator` host object
*/
[[nodiscard]] constexpr ref_type<> ref() const noexcept;
/**
* @brief Get hash function.
*
* @return The hash function
*/
[[nodiscard]] constexpr auto hash_function() const noexcept;
/**
* @brief Gets the span of the sketch.
*
* @return The cuda::std::span of the sketch
*/
[[nodiscard]] constexpr cuda::std::span<std::byte> sketch() const noexcept;
/**
* @brief Gets the number of bytes required for the sketch storage.
*
* @return The number of bytes required for the sketch
*/
[[nodiscard]] constexpr std::size_t sketch_bytes() const noexcept;
/**
* @brief Gets the number of bytes required for the sketch storage.
*
* @param sketch_size_kb Upper bound sketch size in KB
*
* @return The number of bytes required for the sketch
*/
[[nodiscard]] static constexpr std::size_t sketch_bytes(
cuco::sketch_size_kb sketch_size_kb) noexcept;
/**
* @brief Gets the number of bytes required for the sketch storage.
*
* @param standard_deviation Upper bound standard deviation for approximation error
*
* @return The number of bytes required for the sketch
*/
[[nodiscard]] static constexpr std::size_t sketch_bytes(
cuco::standard_deviation standard_deviation) noexcept;
/**
* @brief Gets the alignment required for the sketch storage.
*
* @return The required alignment
*/
[[nodiscard]] static constexpr std::size_t sketch_alignment() noexcept;
private:
std::unique_ptr<impl_type> impl_; ///< Implementation object
};
} // namespace cuco
#include <cuco/detail/distinct_count_estimator/distinct_count_estimator.inl>