Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 1 | #include <algorithm> |
| 2 | #include <cfloat> |
| 3 | #include <chrono> |
| 4 | #include <cmath> |
| 5 | #include <functional> |
| 6 | #include <random> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "bench/utils.h" |
| 10 | #include <xnnpack/AlignedAllocator.h> |
| 11 | #include <xnnpack/common.h> |
| 12 | #include <xnnpack/params.h> |
| 13 | #include <xnnpack/raddextexp.h> |
| 14 | #include <xnnpack/vscaleextexp.h> |
| 15 | |
| 16 | #include <benchmark/benchmark.h> |
| 17 | |
| 18 | |
| 19 | static void f32_vscaleextexp( |
| 20 | benchmark::State& state, |
| 21 | xnn_f32_raddextexp_ukernel_function raddextexp, |
| 22 | xnn_f32_vscaleextexp_ukernel_function vscaleextexp, |
| 23 | benchmark::utils::IsaCheckFunction isa_check = nullptr) |
| 24 | { |
| 25 | if (isa_check && !isa_check(state)) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | const size_t n = state.range(0); |
| 30 | const size_t cache_line_size_max = 128; |
| 31 | const size_t packed_n = benchmark::utils::RoundUp(n, cache_line_size_max / sizeof(float)); |
| 32 | |
| 33 | std::random_device random_device; |
| 34 | auto rng = std::mt19937(random_device()); |
Marat Dukhan | 44f0ca7 | 2020-08-02 21:46:58 -0700 | [diff] [blame] | 35 | auto f32rng = std::bind(std::uniform_real_distribution<float>(-1000.0f, 1000.0f), std::ref(rng)); |
Marat Dukhan | 4c4eb00 | 2019-12-08 21:27:49 -0800 | [diff] [blame] | 36 | |
| 37 | const size_t num_buffers = 1 + |
| 38 | benchmark::utils::DivideRoundUp<size_t>(benchmark::utils::GetMaxCacheSize(), packed_n * sizeof(float)); |
| 39 | std::vector<float, AlignedAllocator<float, 64>> x(n); |
| 40 | std::vector<float, AlignedAllocator<float, 64>> y(packed_n * num_buffers); |
| 41 | |
| 42 | std::generate(x.begin(), x.end(), std::ref(f32rng)); |
| 43 | |
| 44 | benchmark::utils::DisableDenormals(); |
| 45 | |
| 46 | size_t buffer_index = 0; |
| 47 | for (auto _ : state) { |
| 48 | state.PauseTiming(); |
| 49 | float scale[2]; |
| 50 | raddextexp(n * sizeof(float), x.data(), scale); |
| 51 | const float ext_mantissa = 1.0f / scale[0]; |
| 52 | const float ext_exponent = -scale[1]; |
| 53 | if (++buffer_index == num_buffers) { |
| 54 | buffer_index = 0; |
| 55 | } |
| 56 | state.ResumeTiming(); |
| 57 | |
| 58 | vscaleextexp(n * sizeof(float), x.data(), y.data() + packed_n * buffer_index, ext_mantissa, ext_exponent); |
| 59 | } |
| 60 | |
| 61 | state.counters["Freq"] = benchmark::utils::GetCurrentCpuFrequency(); |
| 62 | state.counters["elements"] = |
| 63 | benchmark::Counter(uint64_t(state.iterations()) * n, benchmark::Counter::kIsRate); |
| 64 | state.counters["bytes"] = |
| 65 | benchmark::Counter(uint64_t(state.iterations()) * 2 * sizeof(float) * n, benchmark::Counter::kIsRate); |
| 66 | } |
| 67 | |
| 68 | static void CharacteristicArguments(benchmark::internal::Benchmark* b) { |
| 69 | b->ArgName("N"); |
| 70 | for (int32_t n = 10000; n <= 100000000; n *= 10) { |
| 71 | b->Arg(n); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #if XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 76 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x16, |
| 77 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 78 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x16, |
| 79 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 80 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x32, |
| 81 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 82 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x32, |
| 83 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 84 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x48, |
| 85 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 86 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x48, |
| 87 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 88 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x64, |
| 89 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 90 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x64, |
| 91 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 92 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x80, |
| 93 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 94 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x80, |
| 95 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 96 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x96, |
| 97 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 98 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x96, |
| 99 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 100 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x112, |
| 101 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 102 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x112, |
| 103 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 104 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x128, |
| 105 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 106 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x128, |
| 107 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 108 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x144, |
| 109 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 110 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x144, |
| 111 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 112 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x160, |
| 113 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 114 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x160, |
| 115 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 116 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x176, |
| 117 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 118 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x176, |
| 119 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 120 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx512f_p5_scalef_x192, |
| 121 | xnn_f32_raddextexp_ukernel__avx512f_p5_scalef_x128_acc2, |
| 122 | xnn_f32_vscaleextexp_ukernel__avx512f_p5_scalef_x192, |
| 123 | benchmark::utils::CheckAVX512F)->Apply(CharacteristicArguments)->UseRealTime(); |
| 124 | |
| 125 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x8, |
| 126 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 127 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x8, |
| 128 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 129 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x16, |
| 130 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 131 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x16, |
| 132 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 133 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x24, |
| 134 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 135 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x24, |
| 136 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 137 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x32, |
| 138 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 139 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x32, |
| 140 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 141 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x40, |
| 142 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 143 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x40, |
| 144 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 145 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x48, |
| 146 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 147 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x48, |
| 148 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 149 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x56, |
| 150 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 151 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x56, |
| 152 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 153 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x64, |
| 154 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 155 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x64, |
| 156 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 157 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x72, |
| 158 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 159 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x72, |
| 160 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 161 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x80, |
| 162 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 163 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x80, |
| 164 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 165 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x88, |
| 166 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 167 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x88, |
| 168 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 169 | BENCHMARK_CAPTURE(f32_vscaleextexp, avx2_p5_x96, |
| 170 | xnn_f32_raddextexp_ukernel__avx2_p5_x80_acc2, |
| 171 | xnn_f32_vscaleextexp_ukernel__avx2_p5_x96, |
| 172 | benchmark::utils::CheckAVX2)->Apply(CharacteristicArguments)->UseRealTime(); |
| 173 | #endif // XNN_ARCH_X86 || XNN_ARCH_X86_64 |
| 174 | |
| 175 | #ifndef XNNPACK_BENCHMARK_NO_MAIN |
| 176 | BENCHMARK_MAIN(); |
| 177 | #endif |