File: benchmark.h

package info (click to toggle)
libdivide 5.2.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,564 kB
  • sloc: ansic: 131,647; cpp: 70,136; python: 47; makefile: 3
file content (364 lines) | stat: -rw-r--r-- 11,480 bytes parent folder | download
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Shared bench marking code
#pragma once

#include <inttypes.h>
#include <stdio.h>
#if defined(__AVR__)
#include "avr_type_helpers.h"
#else
#include <algorithm>
#include <limits>
#include <type_traits>
#endif

#undef UNUSED
#define UNUSED(x) (void)(x)

#if defined(_WIN32) || defined(WIN32)
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
#define LIBDIVIDE_WINDOWS
#endif

#include "libdivide.h"
#include "outputs.h"
#include "random_numerators.hpp"
#include "timer.hpp"
#include "type_mappings.h"

using namespace libdivide;

#if defined(__GNUC__)
#define NOINLINE __attribute__((__noinline__))
#elif defined(_MSC_VER)
#define NOINLINE __declspec(noinline)
#pragma warning(disable : 4146)
#else
#define NOINLINE
#endif

#if defined(LIBDIVIDE_AVX512)
#define x86_VECTOR_TYPE __m512i
#define SETZERO_SI _mm512_setzero_si512
#define LOAD_SI _mm512_load_si512
#define ADD_EPI64 _mm512_add_epi64
#define ADD_EPI32 _mm512_add_epi32
#define ADD_EPI16 _mm512_add_epi16
#elif defined(LIBDIVIDE_AVX2)
#define x86_VECTOR_TYPE __m256i
#define SETZERO_SI _mm256_setzero_si256
#define LOAD_SI _mm256_load_si256
#define ADD_EPI64 _mm256_add_epi64
#define ADD_EPI32 _mm256_add_epi32
#define ADD_EPI16 _mm256_add_epi16
#elif defined(LIBDIVIDE_SSE2)
#define x86_VECTOR_TYPE __m128i
#define SETZERO_SI _mm_setzero_si128
#define LOAD_SI _mm_load_si128
#define ADD_EPI64 _mm_add_epi64
#define ADD_EPI32 _mm_add_epi32
#define ADD_EPI16 _mm_add_epi16
#endif

// Helper - given a vector of some type, convert it to unsigned and sum it.
// This is factored out in this funny way to avoid signed integer overflow.
template <typename IntT>
inline uint64_t unsigned_sum_vals(const IntT *vals, size_t count) {
    typedef typename std::make_unsigned<IntT>::type UIntT;
    UIntT sum = 0;
    for (size_t i = 0; i < count; i++) {
        sum += static_cast<UIntT>(vals[i]);
    }
    return sum;
}

template <typename IntT, typename Divisor>
NOINLINE uint64_t sum_quotients(const random_numerators<IntT> &vals, const Divisor &div) {
    // Need to use unsigned to avoid signed integer overlow.
    typedef typename std::make_unsigned<IntT>::type UIntT;
    UIntT sum = 0;
    for (auto iter = vals.begin(); iter != vals.end(); ++iter) {
        sum += (UIntT)(*iter / div);
    }
    return (uint64_t)sum;
}

#ifdef x86_VECTOR_TYPE

template <size_t IntSize>
inline x86_VECTOR_TYPE add_vector(x86_VECTOR_TYPE sumX4, x86_VECTOR_TYPE numers) {
    UNUSED(numers);
    abort();
    return sumX4;
}
template <>
inline x86_VECTOR_TYPE add_vector<2U>(x86_VECTOR_TYPE sumX4, x86_VECTOR_TYPE numers) {
    return ADD_EPI16(sumX4, numers);
}
template <>
inline x86_VECTOR_TYPE add_vector<4U>(x86_VECTOR_TYPE sumX4, x86_VECTOR_TYPE numers) {
    return ADD_EPI32(sumX4, numers);
}
template <>
inline x86_VECTOR_TYPE add_vector<8U>(x86_VECTOR_TYPE sumX4, x86_VECTOR_TYPE numers) {
    return ADD_EPI64(sumX4, numers);
}

template <typename IntT, typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const random_numerators<IntT> &vals, const Divisor &div) {
    size_t count = sizeof(x86_VECTOR_TYPE) / sizeof(IntT);
    x86_VECTOR_TYPE sumX4 = SETZERO_SI();
    for (auto iter = vals.begin(); iter != vals.end(); iter += count) {
        x86_VECTOR_TYPE numers = LOAD_SI((const x86_VECTOR_TYPE *)iter);
        numers = numers / div;
        sumX4 = add_vector<sizeof(IntT)>(sumX4, numers);
    }
    return unsigned_sum_vals((const IntT *)&sumX4, count);
}

#elif defined(LIBDIVIDE_NEON)

// Helper to deduce NEON vector type for integral type.
template <typename T>
struct NeonVecFuncs {};

template <>
struct NeonVecFuncs<uint16_t> {
    static inline uint16x8_t dup(uint16_t value) { return vdupq_n_u16(value); }
    static inline uint16x8_t add(uint16x8_t a, uint16x8_t b) { return vaddq_u16(a, b); }
};

template <>
struct NeonVecFuncs<int16_t> {
    static inline int16x8_t dup(int16_t value) { return vdupq_n_s16(value); }
    static inline int16x8_t add(int16x8_t a, int16x8_t b) { return vaddq_s16(a, b); }
};

template <>
struct NeonVecFuncs<uint32_t> {
    static inline uint32x4_t dup(uint32_t value) { return vdupq_n_u32(value); }
    static inline uint32x4_t add(uint32x4_t a, uint32x4_t b) { return vaddq_u32(a, b); }
};

template <>
struct NeonVecFuncs<int32_t> {
    static inline int32x4_t dup(int32_t value) { return vdupq_n_s32(value); }
    static inline int32x4_t add(int32x4_t a, int32x4_t b) { return vaddq_s32(a, b); }
};

template <>
struct NeonVecFuncs<uint64_t> {
    static inline uint64x2_t dup(uint64_t value) { return vdupq_n_u64(value); }
    static inline uint64x2_t add(uint64x2_t a, uint64x2_t b) { return vaddq_u64(a, b); }
};

template <>
struct NeonVecFuncs<int64_t> {
    static inline int64x2_t dup(int64_t value) { return vdupq_n_s64(value); }
    static inline int64x2_t add(int64x2_t a, int64x2_t b) { return vaddq_s64(a, b); }
};

template <typename IntT, typename Divisor>
NOINLINE uint64_t sum_quotients_vec(const random_numerators<IntT> &vals, const Divisor &div) {
    typedef typename NeonVecFor<IntT>::type NeonVectorType;
    size_t count = sizeof(NeonVectorType) / sizeof(IntT);
    NeonVectorType sumX4 = NeonVecFuncs<IntT>::dup(0);
    for (auto iter = vals.begin(); iter != vals.end(); iter += count) {
        NeonVectorType numers = *(NeonVectorType *)iter;
        numers = numers / div;
        sumX4 = NeonVecFuncs<IntT>::add(sumX4, numers);
    }
    return unsigned_sum_vals((const IntT *)&sumX4, count);
}

#endif

// noinline to force compiler to emit this
template <typename IntT>
NOINLINE divider<IntT> generate_1_divisor(IntT d) {
    return divider<IntT>(d);
}

template <typename IntT>
NOINLINE void generate_divisor(const random_numerators<IntT> &vals, IntT denom) {
    for (size_t iter = 0; iter < vals.length(); iter++) {
        (void)generate_1_divisor(denom);
    }
}

struct time_double {
    uint64_t time;  // in nanoseconds
    uint64_t result;
};

template <typename IntT, class DenomT>
using pFuncToTime = uint64_t (*)(const random_numerators<IntT> &, const DenomT &);

template <typename IntT, class DenomT>
NOINLINE static time_double time_function(
    const random_numerators<IntT> &vals, DenomT denom, pFuncToTime<IntT, DenomT> timeFunc) {
    time_double tresult;

    timer t;
    t.start();
    tresult.result = timeFunc(vals, denom);
    t.stop();
    tresult.time = t.duration_nano();
    return tresult;
}

struct TestResult {
    double hardware_time;
    double base_time;
    double branchfree_time;
    double vector_time;
    double vector_branchfree_time;
    double gen_time;
    int algo;
};

#define TEST_COUNT 30

inline void check_result(uint64_t expected, uint64_t actual, uint32_t line_no) {
    if ((actual) != (expected)) {
        PRINT_ERROR("Failure on line ");
        PRINT_ERROR(line_no);
        PRINT_ERROR("\n");
    }
}

template <typename IntT>
NOINLINE TestResult test_one(const random_numerators<IntT> &vals, IntT denom) {
    const bool testBranchfree = (denom != 1);
    divider<IntT, BRANCHFULL> div_bfull(denom);
    divider<IntT, BRANCHFREE> div_bfree(testBranchfree ? denom : 2);

    uint64_t min_my_time = INT64_MAX, min_my_time_branchfree = INT64_MAX,
             min_my_time_vector = INT64_MAX, min_my_time_vector_branchfree = INT64_MAX,
             min_his_time = INT64_MAX, min_gen_time = INT64_MAX;
    time_double tresult;
    for (size_t iter = 0; iter < TEST_COUNT; iter++) {
        tresult = time_function(vals, denom, sum_quotients);
        min_his_time = (std::min)(min_his_time, tresult.time);
        const uint64_t expected = tresult.result;

        tresult = time_function(vals, div_bfull, sum_quotients);
        min_my_time = (std::min)(min_my_time, tresult.time);
        check_result(tresult.result, expected, __LINE__);

        if (testBranchfree) {
            tresult = time_function(vals, div_bfree, sum_quotients);
            min_my_time_branchfree = (std::min)(min_my_time_branchfree, tresult.time);
            check_result(tresult.result, expected, __LINE__);
        }

#if defined(x86_VECTOR_TYPE) || defined(LIBDIVIDE_NEON)
        tresult = time_function(vals, div_bfull, sum_quotients_vec);
        min_my_time_vector = (std::min)(min_my_time_vector, tresult.time);
        check_result(tresult.result, expected, __LINE__);

        if (testBranchfree) {
            tresult = time_function(vals, div_bfree, sum_quotients_vec);
            min_my_time_vector_branchfree = (std::min)(min_my_time_vector_branchfree, tresult.time);
            check_result(tresult.result, expected, __LINE__);
        }
#else
        min_my_time_vector = 0;
        min_my_time_vector_branchfree = 0;
#endif

        {
            timer t;
            t.start();
            generate_divisor(vals, denom);
            t.stop();
            min_gen_time = (std::min)(min_gen_time, t.duration_nano());
        }
    }

    TestResult result;
    result.gen_time = min_gen_time / (double)vals.length();
    result.base_time = min_my_time / (double)vals.length();
    result.branchfree_time = testBranchfree ? min_my_time_branchfree / (double)vals.length() : -1;
    result.vector_time = min_my_time_vector / (double)vals.length();
    result.vector_branchfree_time =
        testBranchfree ? min_my_time_vector_branchfree / (double)vals.length() : -1;
    result.hardware_time = min_his_time / (double)vals.length();
    return result;
}

template <typename _IntT>
int32_t get_algorithm(_IntT d) {
    const auto denom = libdivide_gen(d);
    uint8_t more = denom.more;
    if (!denom.magic)
        return 0;
    else if (!(more & LIBDIVIDE_ADD_MARKER))
        return 1;
    else
        return 2;
}

template <typename _IntT>
NOINLINE TestResult test_one(_IntT d, const random_numerators<_IntT> &data) {
    struct TestResult result = test_one(data, d);
    result.algo = get_algorithm(d);
    return result;
}

// Result column width
#define PRIcw "10"

inline static void print_report_header(void) {
    char buffer[256];
    snprintf(buffer, sizeof buffer,
        "%6s %" PRIcw "s %" PRIcw "s %" PRIcw "s %" PRIcw "s %" PRIcw "s %" PRIcw "s %6s\n", "#",
        "system", "scalar", "scl_bf", "vector", "vec_bf", "gener", "algo");
    PRINT_INFO(buffer);
}

// Result column format spec
#define PRIrc PRIcw ".3f"

template <typename _IntT>
static void print_report_result(_IntT d, struct TestResult result) {
    char denom_buff[32];
    char *pDenom = to_str(denom_buff, d);

    char report_buff[256];
    snprintf(report_buff, sizeof report_buff,
        "%6s %" PRIrc " %" PRIrc " %" PRIrc " %" PRIrc " %" PRIrc " %" PRIrc " %4d\n", pDenom,
        result.hardware_time, result.base_time, result.branchfree_time, result.vector_time,
        result.vector_branchfree_time, result.gen_time, result.algo);
    PRINT_INFO(report_buff);
}

template <typename _IntT>
static void print_banner() {
    char type_buffer[64];
    snprintf(type_buffer, sizeof type_buffer,
        "=== libdivide %s benchmark ===", type_tag<_IntT>::get_tag());
    char buffer[128];
    snprintf(buffer, sizeof buffer, "\n%50s\n\n", type_buffer);
    PRINT_INFO(buffer);
}

template <typename _IntT>
void test_many() {
    print_banner<_IntT>();
    print_report_header();
    random_numerators<_IntT> data;
    _IntT d = 1;
    while (true) {
        print_report_result(d, test_one(d, data));

        if (std::numeric_limits<_IntT>::is_signed) {
            d = -d;
            if (d > 0) d++;
        } else {
            d++;
        }
    }
}