XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | #include <functional> |
| 9 | #include <random> |
| 10 | #include <vector> |
| 11 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 12 | #include <benchmark/benchmark.h> |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 13 | #include "bench/utils.h" |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 14 | #include <xnnpack/AlignedAllocator.h> |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 15 | #include <xnnpack/common.h> |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 16 | #include <xnnpack/params.h> |
| 17 | #include <xnnpack/rmax.h> |
| 18 | |
| 19 | |
| 20 | static void f32_rmax( |
| 21 | benchmark::State& state, |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 22 | xnn_f32_rmax_ukernel_function f32_rmax, |
| 23 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 24 | { |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 25 | if (isa_check && !isa_check(state)) { |
| 26 | return; |
| 27 | } |
| 28 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 29 | const size_t elements = state.range(0); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 30 | |
| 31 | std::random_device random_device; |
| 32 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 33 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng)); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 34 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 35 | std::vector<float, AlignedAllocator<float, 64>> x(elements); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 36 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 37 | |
| 38 | float y; |
| 39 | for (auto _ : state) { |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 40 | f32_rmax(elements * sizeof(float), x.data(), &y); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 43 | const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency(); |
| 44 | if (cpu_frequency != 0) { |
| 45 | state.counters["cpufreq"] = cpu_frequency; |
| 46 | } |
Frank Barchard | bb4c18b | 2019-09-30 11:05:52 -0700 | [diff] [blame] | 47 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 48 | const size_t elements_per_iteration = elements; |
| 49 | state.counters["elements"] = |
| 50 | benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 51 | |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 52 | const size_t bytes_per_iteration = elements * sizeof(float); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 53 | state.counters["bytes"] = |
Marat Dukhan | d713e8a | 2020-12-04 14:23:12 -0800 | [diff] [blame] | 54 | benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate); |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 57 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 58 | BENCHMARK_CAPTURE(f32_rmax, sse, xnn_f32_rmax_ukernel__sse) |
| 59 | ->RangeMultiplier(10) |
| 60 | ->Range(1000, 100000000) |
| 61 | ->UseRealTime(); |
| 62 | |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 63 | BENCHMARK_CAPTURE(f32_rmax, avx, xnn_f32_rmax_ukernel__avx, benchmark::utils::CheckAVX) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 64 | ->RangeMultiplier(10) |
| 65 | ->Range(1000, 100000000) |
| 66 | ->UseRealTime(); |
| 67 | |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 68 | BENCHMARK_CAPTURE(f32_rmax, avx512f, xnn_f32_rmax_ukernel__avx512f, benchmark::utils::CheckAVX512F) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 69 | ->RangeMultiplier(10) |
| 70 | ->Range(1000, 100000000) |
| 71 | ->UseRealTime(); |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 72 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 73 | |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 74 | #if XNN_ARCH_ARM || XNN_ARCH_ARM64 |
Marat Dukhan | 4e89587 | 2020-12-04 15:27:45 -0800 | [diff] [blame] | 75 | BENCHMARK_CAPTURE(f32_rmax, neon, xnn_f32_rmax_ukernel__neon, benchmark::utils::CheckNEON) |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 76 | ->RangeMultiplier(10) |
| 77 | ->Range(1000, 100000000) |
| 78 | ->UseRealTime(); |
Marat Dukhan | 1dadbf7 | 2019-10-01 10:46:20 -0700 | [diff] [blame] | 79 | #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64 |
Marat Dukhan | 8c41796 | 2020-07-08 12:27:50 -0700 | [diff] [blame] | 80 | |
| 81 | #if XNN_ARCH_WASMSIMD |
| 82 | BENCHMARK_CAPTURE(f32_rmax, wasmsimd_arm, xnn_f32_rmax_ukernel__wasmsimd_arm) |
| 83 | ->RangeMultiplier(10) |
| 84 | ->Range(1000, 100000000) |
| 85 | ->UseRealTime(); |
| 86 | |
| 87 | BENCHMARK_CAPTURE(f32_rmax, wasmsimd_x86, xnn_f32_rmax_ukernel__wasmsimd_x86) |
| 88 | ->RangeMultiplier(10) |
| 89 | ->Range(1000, 100000000) |
| 90 | ->UseRealTime(); |
| 91 | #endif // XNN_ARCH_WASMSIMD |
| 92 | |
XNNPACK Team | b455b12 | 2019-09-27 18:10:33 -0700 | [diff] [blame] | 93 | BENCHMARK_CAPTURE(f32_rmax, scalar, xnn_f32_rmax_ukernel__scalar) |
| 94 | ->RangeMultiplier(10) |
| 95 | ->Range(1000, 100000000) |
| 96 | ->UseRealTime(); |
| 97 | |
| 98 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 99 | BENCHMARK_MAIN(); |
| 100 | #endif |