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
12 #include <benchmark/benchmark.h>
13 #include "bench/utils.h"
14
15 #include <xnnpack.h>
16 #include <xnnpack/aligned-allocator.h>
17 #include <xnnpack/common.h>
18 #include <xnnpack/microfnptr.h>
19 #include <xnnpack/rmax.h>
20
21
f32_rmax(benchmark::State & state,xnn_f32_rmax_ukernel_function f32_rmax,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 static void f32_rmax(
23 benchmark::State& state,
24 xnn_f32_rmax_ukernel_function f32_rmax,
25 benchmark::utils::IsaCheckFunction isa_check = nullptr)
26 {
27 if (isa_check && !isa_check(state)) {
28 return;
29 }
30
31 const size_t elements = state.range(0);
32
33 std::random_device random_device;
34 auto rng = std::mt19937(random_device());
35 auto f32rng = std::bind(std::uniform_real_distribution<float>(-10.0f, 10.0f), std::ref(rng));
36
37 std::vector<float, AlignedAllocator<float, 64>> x(elements);
38 std::generate(x.begin(), x.end(), std::ref(f32rng));
39
40 float y;
41 for (auto _ : state) {
42 f32_rmax(elements * sizeof(float), x.data(), &y);
43 }
44
45 const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
46 if (cpu_frequency != 0) {
47 state.counters["cpufreq"] = cpu_frequency;
48 }
49
50 const size_t elements_per_iteration = elements;
51 state.counters["elements"] =
52 benchmark::Counter(uint64_t(state.iterations()) * elements_per_iteration, benchmark::Counter::kIsRate);
53
54 const size_t bytes_per_iteration = elements * sizeof(float);
55 state.counters["bytes"] =
56 benchmark::Counter(uint64_t(state.iterations()) * bytes_per_iteration, benchmark::Counter::kIsRate);
57 }
58
59 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
60 BENCHMARK_CAPTURE(f32_rmax, sse, xnn_f32_rmax_ukernel__sse)
61 ->RangeMultiplier(10)
62 ->Range(1000, 100000000)
63 ->UseRealTime();
64
65 BENCHMARK_CAPTURE(f32_rmax, avx, xnn_f32_rmax_ukernel__avx, benchmark::utils::CheckAVX)
66 ->RangeMultiplier(10)
67 ->Range(1000, 100000000)
68 ->UseRealTime();
69
70 BENCHMARK_CAPTURE(f32_rmax, avx512f, xnn_f32_rmax_ukernel__avx512f, benchmark::utils::CheckAVX512F)
71 ->RangeMultiplier(10)
72 ->Range(1000, 100000000)
73 ->UseRealTime();
74 #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
75
76 #if XNN_ARCH_ARM || XNN_ARCH_ARM64
77 BENCHMARK_CAPTURE(f32_rmax, neon, xnn_f32_rmax_ukernel__neon, benchmark::utils::CheckNEON)
78 ->RangeMultiplier(10)
79 ->Range(1000, 100000000)
80 ->UseRealTime();
81 #endif // XNN_ARCH_ARM || XNN_ARCH_ARM64
82
83 #if XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
84 BENCHMARK_CAPTURE(f32_rmax, wasmsimd_arm, xnn_f32_rmax_ukernel__wasmsimd_arm)
85 ->RangeMultiplier(10)
86 ->Range(1000, 100000000)
87 ->UseRealTime();
88
89 BENCHMARK_CAPTURE(f32_rmax, wasmsimd_x86, xnn_f32_rmax_ukernel__wasmsimd_x86)
90 ->RangeMultiplier(10)
91 ->Range(1000, 100000000)
92 ->UseRealTime();
93 #endif // XNN_ARCH_WASMSIMD || XNN_ARCH_WASMRELAXEDSIMD
94
95 BENCHMARK_CAPTURE(f32_rmax, scalar, xnn_f32_rmax_ukernel__scalar)
96 ->RangeMultiplier(10)
97 ->Range(1000, 100000000)
98 ->UseRealTime();
99
100 #ifndef XNNPACK_BENCHMARK_NO_MAIN
101 BENCHMARK_MAIN();
102 #endif
103