File: pico_bench.hpp

package info (click to toggle)
xsimd 13.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,716 kB
  • sloc: cpp: 36,557; sh: 541; makefile: 184; python: 117
file content (246 lines) | stat: -rw-r--r-- 8,142 bytes parent folder | download | duplicates (4)
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
/***************************************************************************
 * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
 * Martin Renou                                                             *
 * Copyright (c) QuantStack                                                 *
 * Copyright (c) Serge Guelton                                              *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

// This file is derived from tsimd (MIT License)
// https://github.com/ospray/tsimd/blob/master/benchmarks/pico_bench.h
// Author Jefferson Amstutz / intel

#ifndef PICO_BENCH_H
#define PICO_BENCH_H

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <iterator>
#include <numeric>
#include <ostream>
#include <type_traits>
#include <utility>
#include <vector>

namespace pico_bench
{
    /* Statistics on some time measurement value T, e.g. T =
     * std::chrono::milliseconds T must be some std::chrono::duration type
     */
    template <typename T>
    class Statistics
    {
        using rep = typename T::rep;
        std::vector<T> samples;

    public:
        std::string time_suffix;

        Statistics(std::vector<T> s)
            : samples(s)
        {
            std::sort(std::begin(samples), std::end(samples));
        }

        T percentile(const float p) const
        {
            return percentile(p, samples);
        }

        // Winsorize the data, sets all entries above 100 - limit percentile and
        // below limit percentile to the value of that percentile
        void winsorize(const float limit)
        {
            winsorize(limit, samples);
        }

        T median() const
        {
            return percentile(50.0, samples);
        }

        T median_abs_dev() const
        {
            const auto m = median();
            std::vector<T> deviations;
            deviations.reserve(samples.size());
            std::transform(std::begin(samples),
                           std::end(samples),
                           std::back_inserter(deviations),
                           [&m](const T& t)
                           { return T { std::abs((t - m).count()) }; });
            std::sort(std::begin(deviations), std::end(deviations));
            return percentile(50.0, deviations);
        }

        T mean() const
        {
            const auto m = std::accumulate(std::begin(samples), std::end(samples), T { 0 });
            return m / samples.size();
        }

        T std_dev() const
        {
            const auto m = mean();
            auto val = std::accumulate(
                std::begin(samples), std::end(samples), T { 0 }, [&m](const T& p, const T& t)
                { return T { static_cast<rep>(p.count() + std::pow((t - m).count(), 2)) }; });
            return T { static_cast<rep>(std::sqrt(1.0 / static_cast<double>(samples.size())
                                                  * static_cast<double>(val.count()))) };
        }

        T min() const
        {
            return samples.front();
        }

        T max() const
        {
            return samples.back();
        }

        std::size_t size() const
        {
            return samples.size();
        }

        const T& operator[](size_t i) const
        {
            return samples[i];
        }

    private:
        // Winsorize the data, sets all entries above 100 - limit percentile and
        // below limit percentile to the value of that percentile
        static void winsorize(const float limit, std::vector<T>& samples)
        {
            const auto low = percentile(limit, samples);
            const auto high = percentile(100.0 - limit, samples);
            for (auto& t : samples)
            {
                if (t < low)
                {
                    t = low;
                }
                else if (t > high)
                {
                    t = high;
                }
            }
        }
        static T percentile(const float p, const std::vector<T>& samples)
        {
            assert(!samples.empty());
            assert(p <= 100.0);
            assert(p >= 0.0);
            if (samples.size() == 1)
            {
                return samples.front();
            }
            if (p == 100.0)
            {
                return samples.back();
            }
            const double rank = p / 100.0 * (static_cast<double>(samples.size()) - 1.0);
            const double low_r = std::floor(rank);
            const double dist = rank - low_r;
            const size_t k = static_cast<size_t>(low_r);
            const auto low = samples[k];
            const auto high = samples[k + 1];
            return T { static_cast<rep>(low.count() + (high - low).count() * dist) };
        }
    };

    /* Benchmarking measurment using some desired unit of time measurement,
     * e.g. T = std::chrono::milliseconds. T must be some std::chrono::duration
     */
    template <typename T>
    class Benchmarker
    {
        const size_t MAX_ITER;
        const T MAX_RUNTIME;

        template <typename Fn>
        struct BenchWrapper
        {
            Fn fn;

            BenchWrapper(Fn fn)
                : fn(fn)
            {
            }
            T operator()()
            {
                auto start = std::chrono::high_resolution_clock::now();
                fn();
                auto end = std::chrono::high_resolution_clock::now();
                return std::chrono::duration_cast<T>(end - start);
            }
        };

    public:
        using stats_type = Statistics<T>;

        // Benchmark the functions either max_iter times or until max_runtime
        // seconds have elapsed max_runtime should be > 0
        Benchmarker(const size_t max_iter, const std::chrono::seconds max_runtime)
            : MAX_ITER(max_iter)
            , MAX_RUNTIME(std::chrono::duration_cast<T>(max_runtime))
        {
        }
        // Create a benchmarker that will run the function for the desired number of
        // iterations, regardless of how long it takes
        Benchmarker(const size_t max_iter)
            : MAX_ITER(max_iter)
            , MAX_RUNTIME(0)
        {
        }

        template <typename Fn>
        typename std::enable_if<std::is_void<decltype(std::declval<Fn>()())>::value,
                                stats_type>::type
        operator()(Fn fn) const
        {
            return (*this)(BenchWrapper<Fn> { fn });
        }

        template <typename Fn>
        typename std::enable_if<std::is_same<decltype(std::declval<Fn>()()), T>::value,
                                stats_type>::type
        operator()(Fn fn) const
        {
            // Do a single un-timed warm up run
            fn();
            T elapsed { 0 };
            std::vector<T> samples;
            for (size_t i = 0; i < MAX_ITER && (MAX_RUNTIME.count() == 0 || elapsed < MAX_RUNTIME);
                 ++i, elapsed += samples.back())
            {
                samples.push_back(fn());
            }
            return stats_type { samples };
        }
    };
} // namespace pico_bench

template <typename T>
std::ostream&
operator<<(std::ostream& os, const pico_bench::Statistics<T>& stats)
{
    os << "Statistics:\n"
       << "\tmax: " << stats.max().count() << stats.time_suffix << "\n"
       << "\tmin: " << stats.min().count() << stats.time_suffix << "\n"
       << "\tmedian: " << stats.median().count() << stats.time_suffix << "\n"
       << "\tmedian abs dev: " << stats.median_abs_dev().count() << stats.time_suffix << "\n"
       << "\tmean: " << stats.mean().count() << stats.time_suffix << "\n"
       << "\tstd dev: " << stats.std_dev().count() << stats.time_suffix << "\n"
       << "\t# of samples: " << stats.size();
    return os;
}

#endif