1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-11-15 06:14:58 +00:00
gnss-sdr/tests/benchmarks/benchmark_detector.cc

205 lines
6.7 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
*
*
2020-07-28 14:57:15 +00:00
* -----------------------------------------------------------------------------
2020-06-21 19:10:22 +00:00
*
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
2020-06-21 19:10:22 +00:00
* This file is part of GNSS-SDR.
*
* Copyright (C) 2020 (see AUTHORS file for a list of contributors)
2020-06-21 19:10:22 +00:00
* SPDX-License-Identifier: GPL-3.0-or-later
*
2020-07-28 14:57:15 +00:00
* -----------------------------------------------------------------------------
2020-06-21 19:10:22 +00:00
*/
#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
2024-10-06 06:14:46 +00:00
int32_t corr_value = 0;
2020-06-21 19:10:22 +00:00
while (state.KeepRunning())
{
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];
}
}
}
2024-10-06 06:14:46 +00:00
if (corr_value)
{
// avoidl warning
};
2020-06-21 19:10:22 +00:00
}
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
2024-10-06 06:14:46 +00:00
int32_t corr_value = 0;
2020-06-21 19:10:22 +00:00
while (state.KeepRunning())
{
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++]); });
}
2024-10-06 06:14:46 +00:00
if (corr_value)
{
// avoidl warning
};
2020-06-21 19:10:22 +00:00
}
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); });
2024-10-06 06:14:46 +00:00
int32_t corr_value = 0;
while (state.KeepRunning())
{
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); });
}
2024-10-06 06:14:46 +00:00
if (corr_value)
{
// avoidl warning
};
}
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); });
2024-10-06 06:14:46 +00:00
int32_t corr_value = 0;
2020-07-01 21:05:33 +00:00
while (state.KeepRunning())
{
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); });
}
2024-10-06 06:14:46 +00:00
if (corr_value)
{
// avoidl warning
};
2020-07-01 21:05:33 +00:00
}
#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); });
2024-10-06 06:14:46 +00:00
int32_t corr_value = 0;
2020-07-01 21:05:33 +00:00
while (state.KeepRunning())
{
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); });
}
2024-10-06 06:14:46 +00:00
if (corr_value)
{
// avoidl warning
};
2020-07-01 21:05:33 +00:00
}
#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();