summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/histogram/benchmark/histogram_iteration.cpp
blob: bb18a0ba6cbcbe62c5a58b9a4eb2fb5ef4624368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2018 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <benchmark/benchmark.h>
#include <boost/histogram/axis/integer.hpp>
#include <boost/histogram/axis/regular.hpp>
#include <boost/histogram/histogram.hpp>
#include <boost/histogram/indexed.hpp>
#include <boost/histogram/literals.hpp>
#include <boost/histogram/make_histogram.hpp>
#include <boost/mp11/integral.hpp>
#include <vector>
#include "../test/throw_exception.hpp"

#include <boost/assert.hpp>
struct assert_check {
  assert_check() {
    BOOST_ASSERT(false); // don't run with asserts enabled
  }
} _;

using namespace boost::histogram;
using namespace boost::histogram::literals;

struct tuple {};
struct vector {};
struct vector_of_variant {};

template <unsigned I>
using Dim_t = boost::mp11::mp_int<I>;

using d1 = Dim_t<1>;
using d2 = Dim_t<2>;
using d3 = Dim_t<3>;

auto make_histogram(tuple, d1, unsigned n) {
  return make_histogram_with(std::vector<unsigned>(), axis::integer<>(0, n));
}

auto make_histogram(tuple, d2, unsigned n) {
  return make_histogram_with(std::vector<unsigned>(), axis::integer<>(0, n),
                             axis::integer<>(0, n));
}

auto make_histogram(tuple, d3, unsigned n) {
  return make_histogram_with(std::vector<unsigned>(), axis::integer<>(0, n),
                             axis::integer<>(0, n), axis::integer<>(0, n));
}

template <int Dim>
auto make_histogram(vector, boost::mp11::mp_int<Dim>, unsigned n) {
  std::vector<axis::integer<>> axes;
  for (unsigned d = 0; d < Dim; ++d) axes.emplace_back(axis::integer<>(0, n));
  return make_histogram_with(std::vector<unsigned>(), std::move(axes));
}

template <int Dim>
auto make_histogram(vector_of_variant, boost::mp11::mp_int<Dim>, unsigned n) {
  std::vector<axis::variant<axis::integer<>>> axes;
  for (unsigned d = 0; d < Dim; ++d) axes.emplace_back(axis::integer<>(0, n));
  return make_histogram_with(std::vector<unsigned>(), std::move(axes));
}

template <class Tag>
static void Naive(benchmark::State& state, Tag, d1, coverage cov) {
  auto h = make_histogram(Tag(), d1(), state.range(0));
  const int d = cov == coverage::all;
  for (auto _ : state) {
    for (int i = -d; i < h.axis().size() + d; ++i) {
      benchmark::DoNotOptimize(i);
      benchmark::DoNotOptimize(h.at(i));
    }
  }
}

template <class Tag>
static void Naive(benchmark::State& state, Tag, d2, coverage cov) {
  auto h = make_histogram(Tag(), d2(), state.range(0));
  const int d = cov == coverage::all;
  for (auto _ : state) {
    for (int i = -d; i < h.axis(0_c).size() + d; ++i) {
      for (int j = -d; j < h.axis(1_c).size() + d; ++j) {
        benchmark::DoNotOptimize(i);
        benchmark::DoNotOptimize(j);
        benchmark::DoNotOptimize(h.at(i, j));
      }
    }
  }
}

template <class Tag>
static void Naive(benchmark::State& state, Tag, d3, coverage cov) {
  auto h = make_histogram(Tag(), d3(), state.range(0));
  const int d = cov == coverage::all;
  for (auto _ : state) {
    for (int i = -d; i < h.axis(0_c).size() + d; ++i) {
      for (int j = -d; j < h.axis(1_c).size() + d; ++j) {
        for (int k = -d; k < h.axis(2_c).size() + d; ++k) {
          benchmark::DoNotOptimize(i);
          benchmark::DoNotOptimize(j);
          benchmark::DoNotOptimize(k);
          benchmark::DoNotOptimize(h.at(i, j, k));
        }
      }
    }
  }
}

template <class Tag>
static void Indexed(benchmark::State& state, Tag, d1, coverage cov) {
  auto h = make_histogram(Tag(), d1(), state.range(0));
  for (auto _ : state) {
    for (auto&& x : indexed(h, cov)) {
      benchmark::DoNotOptimize(*x);
      benchmark::DoNotOptimize(x.index());
    }
  }
}

template <class Tag>
static void Indexed(benchmark::State& state, Tag, d2, coverage cov) {
  auto h = make_histogram(Tag(), d2(), state.range(0));
  for (auto _ : state) {
    for (auto&& x : indexed(h, cov)) {
      benchmark::DoNotOptimize(*x);
      benchmark::DoNotOptimize(x.index(0));
      benchmark::DoNotOptimize(x.index(1));
    }
  }
}

template <class Tag>
static void Indexed(benchmark::State& state, Tag, d3, coverage cov) {
  auto h = make_histogram(Tag(), d3(), state.range(0));
  for (auto _ : state) {
    for (auto&& x : indexed(h, cov)) {
      benchmark::DoNotOptimize(*x);
      benchmark::DoNotOptimize(x.index(0));
      benchmark::DoNotOptimize(x.index(1));
      benchmark::DoNotOptimize(x.index(2));
    }
  }
}

#define BENCH(Type, Tag, Dim, Cov)                                             \
  BENCHMARK_CAPTURE(Type, (Tag, Dim, Cov), Tag{}, Dim_t<Dim>{}, coverage::Cov) \
      ->RangeMultiplier(4)                                                     \
      ->Range(4, 256)

BENCH(Naive, tuple, 1, inner);
BENCH(Indexed, tuple, 1, inner);

BENCH(Naive, vector, 1, inner);
BENCH(Indexed, vector, 1, inner);

BENCH(Naive, vector_of_variant, 1, inner);
BENCH(Indexed, vector_of_variant, 1, inner);

BENCH(Naive, tuple, 2, inner);
BENCH(Indexed, tuple, 2, inner);

BENCH(Naive, vector, 2, inner);
BENCH(Indexed, vector, 2, inner);

BENCH(Naive, vector_of_variant, 2, inner);
BENCH(Indexed, vector_of_variant, 2, inner);

BENCH(Naive, tuple, 3, inner);
BENCH(Indexed, tuple, 3, inner);

BENCH(Naive, vector, 3, inner);
BENCH(Indexed, vector, 3, inner);

BENCH(Naive, vector_of_variant, 3, inner);
BENCH(Indexed, vector_of_variant, 3, inner);