|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LIBCXX_ALGORITHMS_COMMON_H |
| 10 | +#define LIBCXX_ALGORITHMS_COMMON_H |
1 | 11 |
|
2 | 12 | #include <algorithm>
|
3 |
| -#include <cstdint> |
4 |
| -#include <map> |
5 |
| -#include <random> |
6 |
| -#include <string> |
7 |
| -#include <utility> |
| 13 | +#include <numeric> |
| 14 | +#include <tuple> |
8 | 15 | #include <vector>
|
9 | 16 |
|
10 |
| -#include "CartesianBenchmarks.h" |
11 |
| -#include "GenerateInput.h" |
12 |
| -#include "benchmark/benchmark.h" |
13 |
| -#include "test_macros.h" |
14 |
| - |
15 |
| -namespace { |
| 17 | +#include "../CartesianBenchmarks.h" |
| 18 | +#include "../GenerateInput.h" |
16 | 19 |
|
17 | 20 | enum class ValueType { Uint32, Uint64, Pair, Tuple, String, Float };
|
18 | 21 | struct AllValueTypes : EnumValuesAsTuple<AllValueTypes, ValueType, 6> {
|
@@ -54,9 +57,9 @@ void fillAdversarialQuickSortInput(T& V, size_t N) {
|
54 | 57 | assert(N > 0);
|
55 | 58 | // If an element is equal to gas, it indicates that the value of the element
|
56 | 59 | // is still to be decided and may change over the course of time.
|
57 |
| - const int gas = N - 1; |
| 60 | + const unsigned int gas = N - 1; |
58 | 61 | V.resize(N);
|
59 |
| - for (int i = 0; i < N; ++i) { |
| 62 | + for (unsigned int i = 0; i < N; ++i) { |
60 | 63 | V[i] = gas;
|
61 | 64 | }
|
62 | 65 | // Candidate for the pivot position.
|
@@ -134,7 +137,7 @@ void fillValues(std::vector<std::tuple<T1, T2, T3> >& V, size_t N, Order O) {
|
134 | 137 | }
|
135 | 138 | }
|
136 | 139 |
|
137 |
| -void fillValues(std::vector<std::string>& V, size_t N, Order O) { |
| 140 | +inline void fillValues(std::vector<std::string>& V, size_t N, Order O) { |
138 | 141 | if (O == Order::SingleElement) {
|
139 | 142 | V.resize(N, getRandomString(64));
|
140 | 143 | } else {
|
@@ -228,169 +231,14 @@ void runOpOnCopies(benchmark::State& state, size_t Quantity, Order O,
|
228 | 231 | }
|
229 | 232 | }
|
230 | 233 |
|
231 |
| -template <class ValueType, class Order> |
232 |
| -struct Sort { |
233 |
| - size_t Quantity; |
234 |
| - |
235 |
| - void run(benchmark::State& state) const { |
236 |
| - runOpOnCopies<ValueType>( |
237 |
| - state, Quantity, Order(), BatchSize::CountElements, |
238 |
| - [](auto& Copy) { std::sort(Copy.begin(), Copy.end()); }); |
239 |
| - } |
240 |
| - |
241 |
| - bool skip() const { return Order() == ::Order::Heap; } |
242 |
| - |
243 |
| - std::string name() const { |
244 |
| - return "BM_Sort" + ValueType::name() + Order::name() + "_" + |
245 |
| - std::to_string(Quantity); |
246 |
| - }; |
247 |
| -}; |
248 |
| - |
249 |
| -template <class ValueType, class Order> |
250 |
| -struct StableSort { |
251 |
| - size_t Quantity; |
252 |
| - |
253 |
| - void run(benchmark::State& state) const { |
254 |
| - runOpOnCopies<ValueType>( |
255 |
| - state, Quantity, Order(), BatchSize::CountElements, |
256 |
| - [](auto& Copy) { std::stable_sort(Copy.begin(), Copy.end()); }); |
257 |
| - } |
258 |
| - |
259 |
| - bool skip() const { return Order() == ::Order::Heap; } |
260 |
| - |
261 |
| - std::string name() const { |
262 |
| - return "BM_StableSort" + ValueType::name() + Order::name() + "_" + |
263 |
| - std::to_string(Quantity); |
264 |
| - }; |
265 |
| -}; |
266 |
| - |
267 |
| -template <class ValueType, class Order> |
268 |
| -struct MakeHeap { |
269 |
| - size_t Quantity; |
270 |
| - |
271 |
| - void run(benchmark::State& state) const { |
272 |
| - runOpOnCopies<ValueType>( |
273 |
| - state, Quantity, Order(), BatchSize::CountElements, |
274 |
| - [](auto& Copy) { std::make_heap(Copy.begin(), Copy.end()); }); |
275 |
| - } |
276 |
| - |
277 |
| - std::string name() const { |
278 |
| - return "BM_MakeHeap" + ValueType::name() + Order::name() + "_" + |
279 |
| - std::to_string(Quantity); |
280 |
| - }; |
281 |
| -}; |
282 |
| - |
283 |
| -template <class ValueType> |
284 |
| -struct SortHeap { |
285 |
| - size_t Quantity; |
286 |
| - |
287 |
| - void run(benchmark::State& state) const { |
288 |
| - runOpOnCopies<ValueType>( |
289 |
| - state, Quantity, Order::Heap, BatchSize::CountElements, |
290 |
| - [](auto& Copy) { std::sort_heap(Copy.begin(), Copy.end()); }); |
291 |
| - } |
292 |
| - |
293 |
| - std::string name() const { |
294 |
| - return "BM_SortHeap" + ValueType::name() + "_" + std::to_string(Quantity); |
295 |
| - }; |
296 |
| -}; |
297 |
| - |
298 |
| -template <class ValueType, class Order> |
299 |
| -struct MakeThenSortHeap { |
300 |
| - size_t Quantity; |
301 |
| - |
302 |
| - void run(benchmark::State& state) const { |
303 |
| - runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, |
304 |
| - [](auto& Copy) { |
305 |
| - std::make_heap(Copy.begin(), Copy.end()); |
306 |
| - std::sort_heap(Copy.begin(), Copy.end()); |
307 |
| - }); |
308 |
| - } |
309 |
| - |
310 |
| - std::string name() const { |
311 |
| - return "BM_MakeThenSortHeap" + ValueType::name() + Order::name() + "_" + |
312 |
| - std::to_string(Quantity); |
313 |
| - }; |
314 |
| -}; |
315 |
| - |
316 |
| -template <class ValueType, class Order> |
317 |
| -struct PushHeap { |
318 |
| - size_t Quantity; |
319 |
| - |
320 |
| - void run(benchmark::State& state) const { |
321 |
| - runOpOnCopies<ValueType>( |
322 |
| - state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) { |
323 |
| - for (auto I = Copy.begin(), E = Copy.end(); I != E; ++I) { |
324 |
| - std::push_heap(Copy.begin(), I + 1); |
325 |
| - } |
326 |
| - }); |
327 |
| - } |
328 |
| - |
329 |
| - bool skip() const { return Order() == ::Order::Heap; } |
330 |
| - |
331 |
| - std::string name() const { |
332 |
| - return "BM_PushHeap" + ValueType::name() + Order::name() + "_" + |
333 |
| - std::to_string(Quantity); |
334 |
| - }; |
335 |
| -}; |
336 |
| - |
337 |
| -template <class ValueType> |
338 |
| -struct PopHeap { |
339 |
| - size_t Quantity; |
340 |
| - |
341 |
| - void run(benchmark::State& state) const { |
342 |
| - runOpOnCopies<ValueType>( |
343 |
| - state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) { |
344 |
| - for (auto B = Copy.begin(), I = Copy.end(); I != B; --I) { |
345 |
| - std::pop_heap(B, I); |
346 |
| - } |
347 |
| - }); |
348 |
| - } |
349 | 234 |
|
350 |
| - std::string name() const { |
351 |
| - return "BM_PopHeap" + ValueType::name() + "_" + std::to_string(Quantity); |
352 |
| - }; |
353 |
| -}; |
354 |
| - |
355 |
| -template <class ValueType, class Order> |
356 |
| -struct MinMaxElement { |
357 |
| - size_t Quantity; |
358 |
| - |
359 |
| - void run(benchmark::State& state) const { |
360 |
| - runOpOnCopies<ValueType>(state, Quantity, Order(), BatchSize::CountElements, [](auto& Copy) { |
361 |
| - benchmark::DoNotOptimize(std::minmax_element(Copy.begin(), Copy.end())); |
362 |
| - }); |
363 |
| - } |
364 |
| - |
365 |
| - std::string name() const { |
366 |
| - return "BM_MinMaxElement" + ValueType::name() + Order::name() + "_" + std::to_string(Quantity); |
367 |
| - } |
368 |
| -}; |
369 |
| - |
370 |
| -} // namespace |
371 |
| - |
372 |
| -int main(int argc, char** argv) { |
373 |
| - benchmark::Initialize(&argc, argv); |
374 |
| - if (benchmark::ReportUnrecognizedArguments(argc, argv)) |
375 |
| - return 1; |
376 |
| - |
377 |
| - const std::vector<size_t> Quantities = {1 << 0, 1 << 2, 1 << 4, 1 << 6, |
378 |
| - 1 << 8, 1 << 10, 1 << 14, |
| 235 | +const std::vector<size_t> Quantities = {1 << 0, 1 << 2, 1 << 4, 1 << 6, |
| 236 | + 1 << 8, 1 << 10, 1 << 14, |
379 | 237 | // Running each benchmark in parallel consumes too much memory with MSAN
|
380 | 238 | // and can lead to the test process being killed.
|
381 | 239 | #if !TEST_HAS_FEATURE(memory_sanitizer)
|
382 |
| - 1 << 18 |
| 240 | + 1 << 18 |
383 | 241 | #endif
|
384 |
| - }; |
385 |
| - makeCartesianProductBenchmark<Sort, AllValueTypes, AllOrders>(Quantities); |
386 |
| - makeCartesianProductBenchmark<StableSort, AllValueTypes, AllOrders>( |
387 |
| - Quantities); |
388 |
| - makeCartesianProductBenchmark<MakeHeap, AllValueTypes, AllOrders>(Quantities); |
389 |
| - makeCartesianProductBenchmark<SortHeap, AllValueTypes>(Quantities); |
390 |
| - makeCartesianProductBenchmark<MakeThenSortHeap, AllValueTypes, AllOrders>( |
391 |
| - Quantities); |
392 |
| - makeCartesianProductBenchmark<PushHeap, AllValueTypes, AllOrders>(Quantities); |
393 |
| - makeCartesianProductBenchmark<PopHeap, AllValueTypes>(Quantities); |
394 |
| - makeCartesianProductBenchmark<MinMaxElement, AllValueTypes, AllOrders>(Quantities); |
395 |
| - benchmark::RunSpecifiedBenchmarks(); |
396 |
| -} |
| 242 | +}; |
| 243 | + |
| 244 | +#endif // LIBCXX_ALGORITHMS_COMMON_H |
0 commit comments