1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-06-13 16:56:52 +00:00
gnss-sdr/src/tests/benchmarks/benchmark_detector.cc

188 lines
6.4 KiB
C++
Raw Normal View History

2020-06-21 19:10:22 +00:00
/*!
* \file benchmark_detector.cc
* \brief Benchmark for preamble detection implementations
* \author Carles Fernandez-Prades, 2020. cfernandez(at)cttc.es
*
*
* -------------------------------------------------------------------------
*
* Copyright (C) 2020 (see AUTHORS file for a list of contributors)
*
* GNSS-SDR is a software defined Global Navigation
* Satellite Systems receiver
*
* This file is part of GNSS-SDR.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* -------------------------------------------------------------------------
*/
#include "GPS_L1_CA.h"
#include <benchmark/benchmark.h>
#include <algorithm> // for std::generate
2020-06-21 19:10:22 +00:00
#include <array>
#include <cmath> // std::for signbit
2020-06-21 19:10:22 +00:00
#include <cstdint>
#include <functional> // for std::plus
#include <numeric> // for std::accumulate, std::inner_product
2020-06-21 19:10:22 +00:00
#include <random>
#include <vector>
2020-07-01 21:05:33 +00:00
#if COMPILER_HAS_STD_TRANSFORM_REDUCE_WITH_POLICY
#include <execution>
#endif
2020-06-21 19:10:22 +00:00
void bm_forloop(benchmark::State& state)
{
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
2020-06-21 19:10:22 +00:00
// fill the inputs
std::random_device rd;
std::default_random_engine e2(rd());
std::uniform_real_distribution<> dist(-1.0, 1.0);
std::generate(d_symbol_history.begin(), d_symbol_history.end(), [&dist, &e2]() { return dist(e2); });
std::generate(d_preamble_samples.begin(), d_preamble_samples.end(), [n = 0]() mutable { return (GPS_CA_PREAMBLE_SYMBOLS_STR[n++] == '1' ? 1 : -1); });
2020-06-21 19:10:22 +00:00
while (state.KeepRunning())
{
2020-06-24 11:59:50 +00:00
int32_t corr_value = 0;
for (size_t i = 0; i < d_preamble_samples.size(); i++)
2020-06-21 19:10:22 +00:00
{
if (d_symbol_history[i] < 0.0)
{
corr_value -= d_preamble_samples[i];
}
else
{
corr_value += d_preamble_samples[i];
}
}
}
}
2020-06-24 11:59:50 +00:00
void bm_accumulate(benchmark::State& state)
2020-06-21 19:10:22 +00:00
{
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
2020-06-21 19:10:22 +00:00
// fill the inputs
std::random_device rd;
std::default_random_engine e2(rd());
std::uniform_real_distribution<> dist(-1.0, 1.0);
std::generate(d_symbol_history.begin(), d_symbol_history.end(), [&dist, &e2]() { return dist(e2); });
std::generate(d_preamble_samples.begin(), d_preamble_samples.end(), [n = 0]() mutable { return (GPS_CA_PREAMBLE_SYMBOLS_STR[n++] == '1' ? 1 : -1); });
2020-06-21 19:10:22 +00:00
while (state.KeepRunning())
{
2020-06-24 11:59:50 +00:00
int32_t corr_value = 0;
2020-06-21 19:10:22 +00:00
corr_value += std::accumulate(d_symbol_history.begin(),
d_symbol_history.end(),
2020-06-21 19:10:22 +00:00
0,
[&d_preamble_samples, n = 0](float a, float b) mutable { return (b > 0.0 ? a + d_preamble_samples[n++] : a - d_preamble_samples[n++]); });
}
}
2020-06-30 11:41:20 +00:00
void bm_inner_product(benchmark::State& state)
{
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
// fill the inputs
std::random_device rd;
std::default_random_engine e2(rd());
std::uniform_real_distribution<> dist(-1.0, 1.0);
std::generate(d_symbol_history.begin(), d_symbol_history.end(), [&dist, &e2]() { return dist(e2); });
std::generate(d_preamble_samples.begin(), d_preamble_samples.end(), [n = 0]() mutable { return (GPS_CA_PREAMBLE_SYMBOLS_STR[n++] == '1' ? 1 : -1); });
while (state.KeepRunning())
{
int32_t corr_value = 0;
corr_value += std::inner_product(d_symbol_history.begin(),
d_symbol_history.end(),
d_preamble_samples.begin(),
0,
2020-06-30 11:41:20 +00:00
#if COMPILER_HAS_STD_PLUS_VOID
std::plus<>(),
2020-06-30 11:41:20 +00:00
#else
std::plus<int32_t>(),
2020-06-30 11:41:20 +00:00
#endif
[](float a, int32_t b) { return (std::signbit(a) ? -b : b); });
}
}
2020-07-01 21:05:33 +00:00
#if COMPILER_HAS_STD_TRANSFORM_REDUCE
void bm_transform_reduce(benchmark::State& state)
{
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
// fill the inputs
std::random_device rd;
std::default_random_engine e2(rd());
std::uniform_real_distribution<> dist(-1.0, 1.0);
std::generate(d_symbol_history.begin(), d_symbol_history.end(), [&dist, &e2]() { return dist(e2); });
std::generate(d_preamble_samples.begin(), d_preamble_samples.end(), [n = 0]() mutable { return (GPS_CA_PREAMBLE_SYMBOLS_STR[n++] == '1' ? 1 : -1); });
while (state.KeepRunning())
{
int32_t corr_value = 0;
corr_value += std::transform_reduce(d_symbol_history.begin(),
d_symbol_history.end(),
d_preamble_samples.begin(),
0,
std::plus<>(),
[](auto a, auto b) { return (std::signbit(a) ? -b : b); });
}
}
#endif
#if COMPILER_HAS_STD_TRANSFORM_REDUCE_WITH_POLICY
void bm_transform_reduce_policy(benchmark::State& state)
{
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
// fill the inputs
std::random_device rd;
std::default_random_engine e2(rd());
std::uniform_real_distribution<> dist(-1.0, 1.0);
std::generate(d_symbol_history.begin(), d_symbol_history.end(), [&dist, &e2]() { return dist(e2); });
std::generate(d_preamble_samples.begin(), d_preamble_samples.end(), [n = 0]() mutable { return (GPS_CA_PREAMBLE_SYMBOLS_STR[n++] == '1' ? 1 : -1); });
while (state.KeepRunning())
{
int32_t corr_value = 0;
corr_value += std::transform_reduce(
std::execution::par,
d_symbol_history.begin(),
d_symbol_history.end(),
d_preamble_samples.begin(),
0,
std::plus<>(),
[](auto a, auto b) { return (std::signbit(a) ? -b : b); });
}
}
#endif
2020-06-21 19:10:22 +00:00
BENCHMARK(bm_forloop);
2020-06-24 11:59:50 +00:00
BENCHMARK(bm_accumulate);
BENCHMARK(bm_inner_product);
2020-07-01 21:05:33 +00:00
#if COMPILER_HAS_STD_TRANSFORM_REDUCE
BENCHMARK(bm_transform_reduce);
#endif
#if COMPILER_HAS_STD_TRANSFORM_REDUCE_WITH_POLICY
BENCHMARK(bm_transform_reduce_policy);
#endif
2020-06-21 19:10:22 +00:00
BENCHMARK_MAIN();