1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-14 12:10:34 +00:00

Add inner_product to detector benchmark

This commit is contained in:
Carles Fernandez 2020-06-29 10:13:07 +02:00
parent 2b3d4b321d
commit 3bc8595dfe
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
2 changed files with 61 additions and 12 deletions

View File

@ -20,18 +20,19 @@
#include "GPS_L1_CA.h"
#include <benchmark/benchmark.h>
#include <algorithm>
#include <algorithm> // for std::generate
#include <array>
#include <cmath> // std::for signbit
#include <cstdint>
#include <numeric>
#include <functional> // for std::plus
#include <numeric> // for std::accumulate, std::inner_product
#include <random>
#include <vector>
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_BITS> d_preamble_samples{};
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
// fill the inputs
std::random_device rd;
@ -39,12 +40,12 @@ void bm_forloop(benchmark::State& state)
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[n++] == '1' ? 1 : -1); });
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;
for (int32_t i = 0; i < GPS_CA_PREAMBLE_LENGTH_BITS; i++)
for (size_t i = 0; i < d_preamble_samples.size(); i++)
{
if (d_symbol_history[i] < 0.0)
{
@ -62,7 +63,7 @@ void bm_forloop(benchmark::State& state)
void bm_accumulate(benchmark::State& state)
{
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_BITS> d_preamble_samples{};
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_SYMBOLS> d_preamble_samples{};
// fill the inputs
std::random_device rd;
@ -70,19 +71,46 @@ void bm_accumulate(benchmark::State& state)
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[n++] == '1' ? 1 : -1); });
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::accumulate(d_symbol_history.begin(),
d_symbol_history.begin() + GPS_CA_PREAMBLE_LENGTH_BITS,
d_symbol_history.end(),
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++]); });
}
}
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,
std::plus<>(),
[&](float a, float b) { return (std::signbit(a) ? -b : b); });
}
}
BENCHMARK(bm_forloop);
BENCHMARK(bm_accumulate);
BENCHMARK(bm_inner_product);
BENCHMARK_MAIN();

View File

@ -23,6 +23,7 @@
#include <array>
#include <chrono>
#include <cstdint>
#include <functional> // for std::plus
#include <numeric>
#include <random>
#include <vector>
@ -32,14 +33,16 @@ TEST(PreambleCorrelationTest, TestMethods)
int64_t n_iter = 100000;
int32_t corr_value = 0;
int32_t corr_value2 = 0;
int32_t corr_value3 = 0;
int32_t sum_corr1 = 0;
int32_t sum_corr2 = 0;
int32_t sum_corr3 = 0;
std::vector<float> d_symbol_history(GPS_CA_PREAMBLE_LENGTH_SYMBOLS, 0.0);
std::array<int32_t, GPS_CA_PREAMBLE_LENGTH_BITS> d_preamble_samples{};
std::chrono::time_point<std::chrono::system_clock> start, end, start2, end2;
std::chrono::time_point<std::chrono::system_clock> start, end, start2, end2, start3, end3;
// fill the inputs
std::random_device rd;
@ -81,11 +84,29 @@ TEST(PreambleCorrelationTest, TestMethods)
}
end2 = std::chrono::system_clock::now();
// Compute correlation, method 3
start3 = std::chrono::system_clock::now();
for (int64_t iter = 0; iter < n_iter; iter++)
{
corr_value3 = std::inner_product(d_symbol_history.begin(),
d_symbol_history.begin() + GPS_CA_PREAMBLE_LENGTH_BITS,
d_preamble_samples.begin(),
0,
std::plus<>(),
[&](float a, float b) { return (std::signbit(a) ? -b : b); });
sum_corr3 += corr_value3;
}
end3 = std::chrono::system_clock::now();
EXPECT_EQ(corr_value, corr_value2);
EXPECT_EQ(corr_value, corr_value3);
EXPECT_EQ(sum_corr1, sum_corr2);
EXPECT_EQ(sum_corr1, sum_corr3);
std::chrono::duration<double> elapsed_seconds = end - start;
std::chrono::duration<double> elapsed_seconds2 = end2 - start2;
std::cout << "Correlation computed with 'C for': done in " << elapsed_seconds.count() * 1.0e9 / n_iter << " nanoseconds" << std::endl;
std::cout << "Correlation computed with lambda: done in " << elapsed_seconds2.count() * 1.0e9 / n_iter << " nanoseconds" << std::endl;
std::chrono::duration<double> elapsed_seconds3 = end3 - start3;
std::cout << "Correlation computed with 'C for' : done in " << elapsed_seconds.count() * 1.0e9 / n_iter << " nanoseconds" << std::endl;
std::cout << "Correlation computed with accumulate : done in " << elapsed_seconds2.count() * 1.0e9 / n_iter << " nanoseconds" << std::endl;
std::cout << "Correlation computed with inner_product : done in " << elapsed_seconds3.count() * 1.0e9 / n_iter << " nanoseconds" << std::endl;
}