| 1 | // Copyright 2019 Google LLC |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #ifndef __STDC_FORMAT_MACROS |
| 17 | #define __STDC_FORMAT_MACROS // before inttypes.h |
| 18 | #endif |
| 19 | #include <inttypes.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #include <cmath> // std::abs |
| 25 | #include <memory> |
| 26 | #include <numeric> // std::iota, std::inner_product |
| 27 | |
| 28 | #undef HWY_TARGET_INCLUDE |
| 29 | #define HWY_TARGET_INCLUDE "hwy/examples/benchmark.cc" |
| 30 | #include "hwy/foreach_target.h" // IWYU pragma: keep |
| 31 | |
| 32 | // Must come after foreach_target.h to avoid redefinition errors. |
| 33 | #include "hwy/aligned_allocator.h" |
| 34 | #include "hwy/highway.h" |
| 35 | #include "hwy/nanobenchmark.h" |
| 36 | |
| 37 | HWY_BEFORE_NAMESPACE(); |
| 38 | namespace hwy { |
| 39 | namespace HWY_NAMESPACE { |
| 40 | |
| 41 | // These templates are not found via ADL. |
| 42 | #if HWY_TARGET != HWY_SCALAR |
| 43 | using hwy::HWY_NAMESPACE::CombineShiftRightLanes; |
| 44 | #endif |
| 45 | |
| 46 | class TwoArray { |
| 47 | public: |
| 48 | // Must be a multiple of the vector lane count * 8. |
| 49 | static size_t NumItems() { return 3456; } |
| 50 | |
| 51 | TwoArray() |
| 52 | : a_(AllocateAligned<float>(NumItems() * 2)), b_(a_.get() + NumItems()) { |
| 53 | // = 1, but compiler doesn't know |
| 54 | const float init = static_cast<float>(Unpredictable1()); |
| 55 | std::iota(a_.get(), a_.get() + NumItems(), init); |
| 56 | std::iota(b_, b_ + NumItems(), init); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | AlignedFreeUniquePtr<float[]> a_; |
| 61 | float* b_; |
| 62 | }; |
| 63 | |
| 64 | // Measures durations, verifies results, prints timings. |
| 65 | template <class Benchmark> |
| 66 | void RunBenchmark(const char* caption) { |
| 67 | printf("%10s: ", caption); |
| 68 | const size_t kNumInputs = 1; |
| 69 | const size_t num_items = Benchmark::NumItems() * size_t(Unpredictable1()); |
| 70 | const FuncInput inputs[kNumInputs] = {num_items}; |
| 71 | Result results[kNumInputs]; |
| 72 | |
| 73 | Benchmark benchmark; |
| 74 | |
| 75 | Params p; |
| 76 | p.verbose = false; |
| 77 | p.max_evals = 7; |
| 78 | p.target_rel_mad = 0.002; |
| 79 | const size_t num_results = MeasureClosure( |
| 80 | [&benchmark](const FuncInput input) { return benchmark(input); }, inputs, |
| 81 | kNumInputs, results, p); |
| 82 | if (num_results != kNumInputs) { |
| 83 | fprintf(stderr, "MeasureClosure failed.\n"); |
| 84 | } |
| 85 | |
| 86 | benchmark.Verify(num_items); |
| 87 | |
| 88 | for (size_t i = 0; i < num_results; ++i) { |
| 89 | const double cycles_per_item = |
| 90 | results[i].ticks / static_cast<double>(results[i].input); |
| 91 | const double mad = results[i].variability * cycles_per_item; |
| 92 | printf("%6" PRIu64 ": %6.3f (+/- %5.3f)\n", |
| 93 | static_cast<uint64_t>(results[i].input), cycles_per_item, mad); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void Intro() { |
| 98 | const float in[16] = {1, 2, 3, 4, 5, 6}; |
| 99 | float out[16]; |
| 100 | const ScalableTag<float> d; // largest possible vector |
| 101 | for (size_t i = 0; i < 16; i += Lanes(d)) { |
| 102 | const auto vec = LoadU(d, in + i); // no alignment requirement |
| 103 | auto result = Mul(vec, vec); |
| 104 | result = Add(result, result); // can update if not const |
| 105 | StoreU(result, d, out + i); |
| 106 | } |
| 107 | printf("\nF(x)->2*x^2, F(%.0f) = %.1f\n", in[2], out[2]); |
| 108 | } |
| 109 | |
| 110 | // BEGINNER: dot product |
| 111 | // 0.4 cyc/float = bronze, 0.25 = silver, 0.15 = gold! |
| 112 | class BenchmarkDot : public TwoArray { |
| 113 | public: |
| 114 | BenchmarkDot() : dot_{-1.0f} {} |
| 115 | |
| 116 | FuncOutput operator()(const size_t num_items) { |
| 117 | const ScalableTag<float> d; |
| 118 | const size_t N = Lanes(d); |
| 119 | using V = decltype(Zero(d)); |
| 120 | // Compiler doesn't make independent sum* accumulators, so unroll manually. |
| 121 | // We cannot use an array because V might be a sizeless type. For reasonable |
| 122 | // code, we unroll 4x, but 8x might help (2 FMA ports * 4 cycle latency). |
| 123 | V sum0 = Zero(d); |
| 124 | V sum1 = Zero(d); |
| 125 | V sum2 = Zero(d); |
| 126 | V sum3 = Zero(d); |
| 127 | const float* const HWY_RESTRICT pa = &a_[0]; |
| 128 | const float* const HWY_RESTRICT pb = b_; |
| 129 | for (size_t i = 0; i < num_items; i += 4 * N) { |
| 130 | const auto a0 = Load(d, pa + i + 0 * N); |
| 131 | const auto b0 = Load(d, pb + i + 0 * N); |
| 132 | sum0 = MulAdd(a0, b0, sum0); |
| 133 | const auto a1 = Load(d, pa + i + 1 * N); |
| 134 | const auto b1 = Load(d, pb + i + 1 * N); |
| 135 | sum1 = MulAdd(a1, b1, sum1); |
| 136 | const auto a2 = Load(d, pa + i + 2 * N); |
| 137 | const auto b2 = Load(d, pb + i + 2 * N); |
| 138 | sum2 = MulAdd(a2, b2, sum2); |
| 139 | const auto a3 = Load(d, pa + i + 3 * N); |
| 140 | const auto b3 = Load(d, pb + i + 3 * N); |
| 141 | sum3 = MulAdd(a3, b3, sum3); |
| 142 | } |
| 143 | // Reduction tree: sum of all accumulators by pairs into sum0. |
| 144 | sum0 = Add(sum0, sum1); |
| 145 | sum2 = Add(sum2, sum3); |
| 146 | sum0 = Add(sum0, sum2); |
| 147 | dot_ = GetLane(SumOfLanes(d, sum0)); |
| 148 | return static_cast<FuncOutput>(dot_); |
| 149 | } |
| 150 | void Verify(size_t num_items) { |
| 151 | if (dot_ == -1.0f) { |
| 152 | fprintf(stderr, "Dot: must call Verify after benchmark"); |
| 153 | abort(); |
| 154 | } |
| 155 | |
| 156 | const float expected = |
| 157 | std::inner_product(a_.get(), a_.get() + num_items, b_, 0.0f); |
| 158 | const float rel_err = std::abs(expected - dot_) / expected; |
| 159 | if (rel_err > 1.1E-6f) { |
| 160 | fprintf(stderr, "Dot: expected %e actual %e (%e)\n", expected, dot_, |
| 161 | rel_err); |
| 162 | abort(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | float dot_; // for Verify |
| 168 | }; |
| 169 | |
| 170 | // INTERMEDIATE: delta coding |
| 171 | // 1.0 cycles/float = bronze, 0.7 = silver, 0.4 = gold! |
| 172 | struct BenchmarkDelta : public TwoArray { |
| 173 | FuncOutput operator()(const size_t num_items) const { |
| 174 | #if HWY_TARGET == HWY_SCALAR |
| 175 | b_[0] = a_[0]; |
| 176 | for (size_t i = 1; i < num_items; ++i) { |
| 177 | b_[i] = a_[i] - a_[i - 1]; |
| 178 | } |
| 179 | #elif HWY_CAP_GE256 |
| 180 | // Larger vectors are split into 128-bit blocks, easiest to use the |
| 181 | // unaligned load support to shift between them. |
| 182 | const ScalableTag<float> df; |
| 183 | const size_t N = Lanes(df); |
| 184 | size_t i; |
| 185 | b_[0] = a_[0]; |
| 186 | for (i = 1; i < N; ++i) { |
| 187 | b_[i] = a_[i] - a_[i - 1]; |
| 188 | } |
| 189 | for (; i < num_items; i += N) { |
| 190 | const auto a = Load(df, &a_[i]); |
| 191 | const auto shifted = LoadU(df, &a_[i - 1]); |
| 192 | Store(a - shifted, df, &b_[i]); |
| 193 | } |
| 194 | #else // 128-bit |
| 195 | // Slightly better than unaligned loads |
| 196 | const HWY_CAPPED(float, 4) df; |
| 197 | const size_t N = Lanes(df); |
| 198 | size_t i; |
| 199 | b_[0] = a_[0]; |
| 200 | for (i = 1; i < N; ++i) { |
| 201 | b_[i] = a_[i] - a_[i - 1]; |
| 202 | } |
| 203 | auto prev = Load(df, &a_[0]); |
| 204 | for (; i < num_items; i += Lanes(df)) { |
| 205 | const auto a = Load(df, &a_[i]); |
| 206 | const auto shifted = CombineShiftRightLanes<3>(df, a, prev); |
| 207 | prev = a; |
| 208 | Store(Sub(a, shifted), df, &b_[i]); |
| 209 | } |
| 210 | #endif |
| 211 | return static_cast<FuncOutput>(b_[num_items - 1]); |
| 212 | } |
| 213 | |
| 214 | void Verify(size_t num_items) { |
| 215 | for (size_t i = 0; i < num_items; ++i) { |
| 216 | const float expected = (i == 0) ? a_[0] : a_[i] - a_[i - 1]; |
| 217 | const float err = std::abs(expected - b_[i]); |
| 218 | if (err > 1E-6f) { |
| 219 | fprintf(stderr, "Delta: expected %e, actual %e\n", expected, b_[i]); |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | void RunBenchmarks() { |
| 226 | Intro(); |
| 227 | printf("------------------------ %s\n", TargetName(HWY_TARGET)); |
| 228 | RunBenchmark<BenchmarkDot>("dot"); |
| 229 | RunBenchmark<BenchmarkDelta>("delta"); |
| 230 | } |
| 231 | |
| 232 | // NOLINTNEXTLINE(google-readability-namespace-comments) |
| 233 | } // namespace HWY_NAMESPACE |
| 234 | } // namespace hwy |
| 235 | HWY_AFTER_NAMESPACE(); |
| 236 | |
| 237 | #if HWY_ONCE |
| 238 | namespace hwy { |
| 239 | HWY_EXPORT(RunBenchmarks); |
| 240 | |
| 241 | void Run() { |
| 242 | for (int64_t target : SupportedAndGeneratedTargets()) { |
| 243 | SetSupportedTargetsForTest(target); |
| 244 | HWY_DYNAMIC_DISPATCH(RunBenchmarks)(); |
| 245 | } |
| 246 | SetSupportedTargetsForTest(0); // Reset the mask afterwards. |
| 247 | } |
| 248 | |
| 249 | } // namespace hwy |
| 250 | |
| 251 | int main(int /*argc*/, char** /*argv*/) { |
| 252 | hwy::Run(); |
| 253 | return 0; |
| 254 | } |
| 255 | #endif // HWY_ONCE |