Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
compute_sum.bench.cpp
Go to the documentation of this file.
1// Diagnostic benchmark for polynomial_arithmetic::compute_sum (reduction
2// over a contiguous Fr array). Two variants, same inputs:
3//
4// compute_sum_scalar — raw scalar for-loop accumulator. No vectorized_for,
5// no Accumulator. Single-thread.
6// compute_sum_full — polynomial_arithmetic::compute_sum, which routes
7// through vectorized_for<VECTOR_FIELD_WIDTH> +
8// Accumulator<Fr>.
9//
10// The full/scalar ratio is the speedup the reduction abstraction earns on
11// the target platform. CorrectnessGuard at startup compares the two paths
12// bit-exactly on 65k random elements.
13
16
17#include <benchmark/benchmark.h>
18
19using namespace benchmark;
20using bb::fr;
21
22constexpr size_t N = 1 << 16;
23
24namespace {
25
26struct SumFixture {
27 std::vector<fr> src;
28
29 SumFixture()
30 : src(N)
31 {
32 for (size_t i = 0; i < N; ++i) {
33 src[i] = fr::random_element();
34 }
35 }
36};
37
38struct CorrectnessGuard {
39 CorrectnessGuard()
40 {
41 SumFixture f;
42 // Reference scalar path.
43 fr ref = 0;
44 for (size_t i = 0; i < N; ++i) {
45 ref = ref + f.src[i];
46 }
47 // Production (vectorized) path.
48 const fr got = bb::polynomial_arithmetic::compute_sum<fr>(f.src.data(), N);
49 if (!(ref == got)) {
50 std::fprintf(stderr, "[COMPUTE_SUM CORRECTNESS] scalar != vectorized\n");
51 std::abort();
52 }
53 }
54};
55static const CorrectnessGuard correctness_guard;
56
57} // namespace
58
59static void bench_compute_sum_scalar(State& state)
60{
61 SumFixture f;
62 for (auto _ : state) {
63 fr result = 0;
64 for (size_t i = 0; i < N; ++i) {
65 result = result + f.src[i];
66 }
67 DoNotOptimize(result);
68 }
69}
70BENCHMARK(bench_compute_sum_scalar);
71
72static void bench_compute_sum_full(State& state)
73{
74 SumFixture f;
75 for (auto _ : state) {
77 DoNotOptimize(result);
78 }
79}
80BENCHMARK(bench_compute_sum_full);
81
constexpr size_t N
BENCHMARK_MAIN()
BENCHMARK(bench_compute_sum_scalar)
constexpr size_t N
template fr compute_sum< fr >(const fr *, const size_t)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept
VectorField result