Nearest Neighbor
Generate random values
Write a function producing a
std::vectorof randomfloatvalues.
TIP
Use
std::uniform_real_distribution<float>(0.f, 10.f).
Find nearest neighbor
Given another random value, find the closest neighbor in the random
vectorand return its index in thevector.
Vectorize
Copy the code (or generalize via
template) and work withsimd::vec<float>instead.Two variants:
- Use
simd::unchecked_load(range)to load from an array offloat- Use
std::vector<simd::vec<float>>
Which is better, if at all? When?
Benchmark
Benchmark the three variants, over different sizes of the
vector
TIP
One benchmark function, benchmarking different
vectorsizesvoid name(benchmark::State& state) { const std::size_t n = state.range(0); std::vector<float> values(n); // ... } BENCHMARK(name)->Range(8, 8 << 20);
Extend to 3-D
Go from 1-dim to three dimensional.
- AoS
- SoA
- AoVS
Benchmark the results