GNU Radio C++ API Reference: benchmark_common.h Source File

| GNU Radio C++ API Reference 3.10.12.0 The Free & Open Software Radio Ecosystem |

Loading...
Searching...
No Matches
benchmark_common.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2002,2004,2013,2018 Free Software Foundation, Inc.
4 * Copyright 2023 Marcus Müller
5 *
6 * This file is part of GNU Radio
7 *
8 * SPDX-License-Identifier: GPL-3.0-or-later
9 *
10 */
11#ifndef INCLUDED_BENCHMARK_COMMON
12#define INCLUDED_BENCHMARK_COMMON
13/* ensure that tweakme.h is included before the bundled spdlog/fmt header, see
14 * https://github.com/gabime/spdlog/issues/2922 */
15#include <spdlog/tweakme.h>
16
17#include <gnuradio/random.h>
18#include <spdlog/fmt/fmt.h>
19#include <string_view>
20#include <chrono>
21#include <cstddef>
22#include <cstdlib>
23#include <limits>
24#include <numeric>
25#include <vector>
26
27template <typename functor>
28[[nodiscard]] auto benchmark(functor test, size_t block_size)
29{
30 std::vector<float> outp(2 * block_size);
31 float* output = outp.data();
32 float *x = &output[0], *y = &output[block_size];
33
34 // generate input in the first half, and also in the second half to touch the memory
35 gr::xoroshiro128p_prng rng(42);
36 for (auto& value : outp) {
37 value = rng() / static_cast<double>(1ULL << 32) - (1ULL << 32);
38 }
39
40 auto before = std::chrono::high_resolution_clock::now();
41 // do the actual work
42
43 test(x, y);
44
45 auto after = std::chrono::high_resolution_clock::now();
46 // get ending CPU usage
47 auto dur =
48 std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
49 after - before);
50
51 // prevent the compiler from discarding the output, not doing the calculations.
52 volatile auto sum = std::accumulate(outp.cbegin(), outp.cend(), 0.0f);
53 if (sum == std::numeric_limits<decltype(sum)>::min()) {
54 // should never be hit
55 return decltype(dur){};
56 }
57
58 return dur;
59}
60template <typename dur_t>
61 auto format_duration(std::string_view name,
62 dur_t dur,
63 size_t iterations,
64 size_t block_size)
65{
66 auto dur_s = std::chrono::duration_cast<std::chrono::duration<double>>(dur);
67 return fmt::format(FMT_STRING("{:<18} time: {:<8.4e} s throughput: {:>6.3e} it/s"),
68 name,
69 dur_s.count(),
70 static_cast<double>(iterations) / dur_s.count());
71}
72
73#endif
format_duration
auto format_duration(std::string_view name, dur_t dur, size_t iterations, size_t block_size)
Definition benchmark_common.h:61
benchmark
auto benchmark(functor test, size_t block_size)
Definition benchmark_common.h:28
gr::xoroshiro128p_prng
wrapper for XOROSHIRO128+ PRNG for use in std::distributions Fulfills C++ named requirements for Unif...
Definition random.h:29
random.h