xref: /aosp_15_r20/external/XNNPACK/bench/x64-transpose.cc (revision 4bdc94577ba0e567308109d787f7fec7b531ce36)
1 // Copyright 2021 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 <numeric>
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/transpose.h>
20 
21 
transpose(benchmark::State & state,xnn_x64_transposec_ukernel_function transpose,benchmark::utils::IsaCheckFunction isa_check=nullptr)22 void transpose(
23     benchmark::State& state,
24     xnn_x64_transposec_ukernel_function transpose,
25     benchmark::utils::IsaCheckFunction isa_check = nullptr)
26 {
27   if (isa_check && !isa_check(state)) {
28     return;
29   }
30   const size_t height = state.range(0);
31   const size_t width = state.range(1);
32   const size_t tile_hbytes = height * sizeof(uint64_t);
33   const size_t tile_wbytes = width * sizeof(uint64_t);
34 
35   std::vector<uint64_t, AlignedAllocator<uint64_t, 64>> x(
36       height * width + XNN_EXTRA_BYTES / sizeof(uint64_t));
37   std::vector<uint64_t, AlignedAllocator<uint64_t, 64>> y(
38       height * width + XNN_EXTRA_BYTES / sizeof(uint64_t));
39   std::iota(x.begin(), x.end(), 0);
40   std::fill(y.begin(), y.end(), 0);
41 
42   for (auto _ : state) {
43     transpose(x.data(), y.data(), tile_wbytes, tile_hbytes, width,
44               height);
45   }
46 
47   const uint64_t cpu_frequency = benchmark::utils::GetCurrentCpuFrequency();
48   if (cpu_frequency != 0) {
49     state.counters["cpufreq"] = cpu_frequency;
50   }
51 }
52 
BenchmarkKernelSize(benchmark::internal::Benchmark * b)53 static void BenchmarkKernelSize(benchmark::internal::Benchmark* b)
54 {
55   b->ArgNames({"height", "width"});
56   b->Args({32, 32});
57   b->Args({64, 64});
58   b->Args({117, 117});
59   b->Args({1024, 1024});
60 }
61 
62 BENCHMARK_CAPTURE(transpose, 1x2_scalar_int, xnn_x64_transposec_ukernel__1x2_scalar_int)
63     ->Apply(BenchmarkKernelSize)->UseRealTime();
64 BENCHMARK_CAPTURE(transpose, 2x1_scalar_int, xnn_x64_transposec_ukernel__2x1_scalar_int)
65     ->Apply(BenchmarkKernelSize)->UseRealTime();
66 BENCHMARK_CAPTURE(transpose, 2x2_scalar_int, xnn_x64_transposec_ukernel__2x2_scalar_int)
67     ->Apply(BenchmarkKernelSize)->UseRealTime();
68 BENCHMARK_CAPTURE(transpose, 4x1_scalar_int, xnn_x64_transposec_ukernel__4x1_scalar_int)
69     ->Apply(BenchmarkKernelSize)->UseRealTime();
70 BENCHMARK_CAPTURE(transpose, 4x2_scalar_int, xnn_x64_transposec_ukernel__4x2_scalar_int)
71     ->Apply(BenchmarkKernelSize)->UseRealTime();
72 BENCHMARK_CAPTURE(transpose, 1x2_scalar_float, xnn_x64_transposec_ukernel__1x2_scalar_float)
73     ->Apply(BenchmarkKernelSize)->UseRealTime();
74 BENCHMARK_CAPTURE(transpose, 2x1_scalar_float, xnn_x64_transposec_ukernel__2x1_scalar_float)
75     ->Apply(BenchmarkKernelSize)->UseRealTime();
76 BENCHMARK_CAPTURE(transpose, 2x2_scalar_float, xnn_x64_transposec_ukernel__2x2_scalar_float)
77     ->Apply(BenchmarkKernelSize)->UseRealTime();
78 BENCHMARK_CAPTURE(transpose, 4x1_scalar_float, xnn_x64_transposec_ukernel__4x1_scalar_float)
79     ->Apply(BenchmarkKernelSize)->UseRealTime();
80 BENCHMARK_CAPTURE(transpose, 4x2_scalar_float, xnn_x64_transposec_ukernel__4x2_scalar_float)
81     ->Apply(BenchmarkKernelSize)->UseRealTime();
82 
83 #if XNN_ARCH_X86 || XNN_ARCH_X86_64
84   BENCHMARK_CAPTURE(transpose, 2x2_multi_mov_sse2, xnn_x64_transposec_ukernel__2x2_multi_mov_sse2)
85       ->Apply(BenchmarkKernelSize)->UseRealTime();
86   BENCHMARK_CAPTURE(transpose, 2x2_multi_switch_sse2, xnn_x64_transposec_ukernel__2x2_multi_switch_sse2)
87       ->Apply(BenchmarkKernelSize)->UseRealTime();
88   BENCHMARK_CAPTURE(transpose, 2x2_reuse_mov_sse2, xnn_x64_transposec_ukernel__2x2_reuse_mov_sse2)
89       ->Apply(BenchmarkKernelSize)->UseRealTime();
90   BENCHMARK_CAPTURE(transpose, 2x2_reuse_multi_sse2, xnn_x64_transposec_ukernel__2x2_reuse_multi_sse2)
91       ->Apply(BenchmarkKernelSize)->UseRealTime();
92   BENCHMARK_CAPTURE(transpose, 2x2_reuse_switch_sse2, xnn_x64_transposec_ukernel__2x2_reuse_switch_sse2)
93       ->Apply(BenchmarkKernelSize)->UseRealTime();
94 #endif  // XNN_ARCH_X86 || XNN_ARCH_X86_64
95 
96 
97 #ifndef XNNPACK_BENCHMARK_NO_MAIN
98 BENCHMARK_MAIN();
99 #endif
100