benchmark_take_while_kld.cpp
Go to the documentation of this file.
1 // Copyright 2022-2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <benchmark/benchmark.h>
16 
17 #include <array>
18 #include <cstddef>
19 #include <tuple>
20 #include <type_traits>
21 
22 #include <range/v3/algorithm/copy.hpp>
23 #include <range/v3/range/access.hpp>
24 #include <range/v3/range/primitives.hpp>
25 #include <range/v3/view/subrange.hpp>
26 #include <range/v3/view/take_exactly.hpp>
27 
30 #include "beluga/primitives.hpp"
31 #include "beluga/views/sample.hpp"
33 
34 namespace {
35 
36 struct State {
37  double x = 0.;
38  double y = 0.;
39  double theta = 0.;
40 };
41 
42 } // namespace
43 
44 template <>
45 struct beluga::spatial_hash<State> {
46  std::size_t operator()(const State& state) const {
47  const auto tuple = std::make_tuple(state.x, state.y);
48  return beluga::spatial_hash<std::decay_t<decltype(tuple)>>{std::array{1., 1.}}(tuple);
49  }
50 };
51 
52 namespace {
53 
55 using Particle = typename Container::value_type;
56 
57 void BM_FixedResample(benchmark::State& state) {
58  const auto particle_count = state.range(0);
59  state.SetComplexityN(particle_count);
60  const auto container_size = static_cast<std::size_t>(particle_count);
61 
62  auto container = Container{container_size};
63  auto new_container = Container{container_size};
64 
65  for (auto&& [state, weight] : container) {
66  weight = 1.;
67  }
68 
69  for (auto _ : state) {
70  auto samples = container | //
72  ranges::views::take_exactly(container_size);
73  auto first = ranges::begin(new_container);
74  auto last = ranges::copy(samples, first).out;
75  auto result = ranges::make_subrange(first, last);
76  state.counters["SampleSize"] = static_cast<double>(ranges::size(result));
77  }
78 }
79 
80 BENCHMARK(BM_FixedResample)->RangeMultiplier(2)->Range(128, 1'000'000)->Complexity();
81 
82 void BM_AdaptiveResample(benchmark::State& state) {
83  const auto particle_count = state.range(0);
84  state.SetComplexityN(particle_count);
85  const auto container_size = static_cast<std::size_t>(particle_count);
86 
87  auto container = Container{container_size};
88  auto new_container = Container{container_size};
89 
90  double step = 0;
91  int i = 0;
92  for (auto&& [state, weight] : container) {
93  weight = 1.;
94  state.x = step;
95  if (i++ % 2 == 0) {
96  step += 0.05;
97  }
98  }
99 
100  const std::size_t min_samples = 0;
101  const std::size_t max_samples = container_size;
102  const double kld_epsilon = 0.05;
103  const double kld_z = 3.;
104 
105  for (auto _ : state) {
106  auto samples =
107  container | //
109  beluga::views::take_while_kld(beluga::spatial_hash<State>{}, min_samples, max_samples, kld_epsilon, kld_z);
110  auto first = ranges::begin(new_container);
111  auto last = ranges::copy(samples, first).out;
112  auto result = ranges::make_subrange(first, last);
113  state.counters["SampleSize"] = static_cast<double>(ranges::size(result));
114  state.counters["Percentage"] = static_cast<double>(ranges::size(result)) / static_cast<double>(max_samples);
115  }
116 }
117 
118 BENCHMARK(BM_AdaptiveResample)->RangeMultiplier(2)->Range(128, 1'000'000)->Complexity();
119 
120 } // namespace
result
result[0]
primitives.hpp
Implementation of library primitives to abstract member access.
beluga::spatial_hash< State >::operator()
std::size_t operator()(const State &state) const
Definition: benchmark_take_while_kld.cpp:46
beluga::state
constexpr state_detail::state_fn state
Customization point object for accessing the state of a particle.
Definition: primitives.hpp:163
tuple_vector.hpp
Implementation of a tuple of containers.
beluga::weight
constexpr weight_detail::weight_fn weight
Customization point object for accessing the weight of a particle.
Definition: primitives.hpp:264
beluga::TupleVector
Shorthand for a tuple of vectors with the default allocator.
Definition: tuple_vector.hpp:223
beluga::spatial_hash
Callable class, allowing to calculate the hash of a particle state.
Definition: spatial_hash.hpp:100
beluga::spatial_hash< State >
Definition: benchmark_take_while_kld.cpp:45
take_while_kld.hpp
Implementation of a take_while_kld range adaptor object.
spatial_hash.hpp
Implementation of a spatial hash for N dimensional states.
sample.hpp
Implementation of a sample (with replacement) range adaptor object.
beluga::views::take_while_kld
constexpr detail::take_while_kld_fn take_while_kld
Definition: take_while_kld.hpp:170
beluga::views::sample
constexpr ranges::views::view_closure< detail::sample_fn > sample
Definition: sample.hpp:240


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53