blob: 79a1e5d2e0b2b0f278355abffaff6847ccfe0005 [file] [log] [blame]
Alan Kellyba68f442022-01-25 12:00:37 -08001// 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 <xnnpack/AlignedAllocator.h>
7#include <xnnpack/common.h>
8#include <xnnpack/params.h>
9#include <xnnpack/transpose.h>
10
11#include <algorithm>
12#include <cmath>
13#include <functional>
14#include <numeric>
15#include <vector>
16
17#include "bench/utils.h"
18#include <benchmark/benchmark.h>
19
20void transpose(
21 benchmark::State& state,
22 xnn_x64_transpose_ukernel_function transpose,
23 benchmark::utils::IsaCheckFunction isa_check = nullptr)
24{
25 if (isa_check && !isa_check(state)) {
26 return;
27 }
28 const size_t height = state.range(0);
29 const size_t width = state.range(1);
30 const size_t tile_hbytes = height * sizeof(uint64_t);
31 const size_t tile_wbytes = width * sizeof(uint64_t);
32
33 std::vector<uint64_t, AlignedAllocator<uint64_t, 64>> x(
34 height * width + XNN_EXTRA_BYTES / sizeof(uint64_t));
35 std::vector<uint64_t, AlignedAllocator<uint64_t, 64>> y(
36 height * width + XNN_EXTRA_BYTES / sizeof(uint64_t));
37 std::iota(x.begin(), x.end(), 0);
38 std::fill(y.begin(), y.end(), 0);
39
40 for (auto _ : state) {
41 transpose(x.data(), y.data(), tile_wbytes, tile_hbytes, width,
42 height);
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
51static void BenchmarkKernelSize(benchmark::internal::Benchmark* b)
52{
53 b->ArgNames({"height", "width"});
54 b->Args({32, 32});
55 b->Args({64, 64});
56 b->Args({117, 117});
57 b->Args({1024, 1024});
58}
59
60BENCHMARK_CAPTURE(transpose, 1x2_scalar_int, xnn_x64_transpose_ukernel__1x2_scalar_int)
61 ->Apply(BenchmarkKernelSize)->UseRealTime();
62BENCHMARK_CAPTURE(transpose, 2x1_scalar_int, xnn_x64_transpose_ukernel__2x1_scalar_int)
63 ->Apply(BenchmarkKernelSize)->UseRealTime();
64BENCHMARK_CAPTURE(transpose, 2x2_scalar_int, xnn_x64_transpose_ukernel__2x2_scalar_int)
65 ->Apply(BenchmarkKernelSize)->UseRealTime();
66BENCHMARK_CAPTURE(transpose, 4x1_scalar_int, xnn_x64_transpose_ukernel__4x1_scalar_int)
67 ->Apply(BenchmarkKernelSize)->UseRealTime();
68BENCHMARK_CAPTURE(transpose, 4x2_scalar_int, xnn_x64_transpose_ukernel__4x2_scalar_int)
69 ->Apply(BenchmarkKernelSize)->UseRealTime();
70BENCHMARK_CAPTURE(transpose, 1x2_scalar_float, xnn_x64_transpose_ukernel__1x2_scalar_float)
71 ->Apply(BenchmarkKernelSize)->UseRealTime();
72BENCHMARK_CAPTURE(transpose, 2x1_scalar_float, xnn_x64_transpose_ukernel__2x1_scalar_float)
73 ->Apply(BenchmarkKernelSize)->UseRealTime();
74BENCHMARK_CAPTURE(transpose, 2x2_scalar_float, xnn_x64_transpose_ukernel__2x2_scalar_float)
75 ->Apply(BenchmarkKernelSize)->UseRealTime();
76BENCHMARK_CAPTURE(transpose, 4x1_scalar_float, xnn_x64_transpose_ukernel__4x1_scalar_float)
77 ->Apply(BenchmarkKernelSize)->UseRealTime();
78BENCHMARK_CAPTURE(transpose, 4x2_scalar_float, xnn_x64_transpose_ukernel__4x2_scalar_float)
79 ->Apply(BenchmarkKernelSize)->UseRealTime();
80
81#if XNN_ARCH_X86 || XNN_ARCH_X86_64
Alan Kellyf2b233b2022-01-31 02:53:57 -080082 BENCHMARK_CAPTURE(transpose, 2x2_multi_mov_sse2, xnn_x64_transpose_ukernel__2x2_multi_mov_sse2)
Alan Kellyba68f442022-01-25 12:00:37 -080083 ->Apply(BenchmarkKernelSize)->UseRealTime();
84 BENCHMARK_CAPTURE(transpose, 2x2_multi_switch_sse2, xnn_x64_transpose_ukernel__2x2_multi_switch_sse2)
85 ->Apply(BenchmarkKernelSize)->UseRealTime();
Alan Kellyf2b233b2022-01-31 02:53:57 -080086 BENCHMARK_CAPTURE(transpose, 2x2_reuse_mov_sse2, xnn_x64_transpose_ukernel__2x2_reuse_mov_sse2)
Alan Kellyba68f442022-01-25 12:00:37 -080087 ->Apply(BenchmarkKernelSize)->UseRealTime();
88 BENCHMARK_CAPTURE(transpose, 2x2_reuse_multi_sse2, xnn_x64_transpose_ukernel__2x2_reuse_multi_sse2)
89 ->Apply(BenchmarkKernelSize)->UseRealTime();
90 BENCHMARK_CAPTURE(transpose, 2x2_reuse_switch_sse2, xnn_x64_transpose_ukernel__2x2_reuse_switch_sse2)
91 ->Apply(BenchmarkKernelSize)->UseRealTime();
92#endif // XNN_ARCH_X86 || XNN_ARCH_X86_64
93
94
95#ifndef XNNPACK_BENCHMARK_NO_MAIN
96BENCHMARK_MAIN();
97#endif