2012-01-23 00:52:05 +00:00
|
|
|
/*!
|
2014-03-28 17:52:51 +00:00
|
|
|
* \file multiply_test.cc
|
|
|
|
* \brief This file implements tests for the multiplication of long arrays.
|
2012-01-23 00:52:05 +00:00
|
|
|
* \author Carlos Aviles, 2010. carlos.avilesr(at)googlemail.com
|
|
|
|
* Carles Fernandez-Prades, 2012. cfernandez(at)cttc.es
|
|
|
|
*
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2012-01-23 00:52:05 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2012-01-23 00:52:05 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2012-01-23 00:52:05 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2012-01-23 00:52:05 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-09 21:00:09 +00:00
|
|
|
#include <armadillo>
|
|
|
|
#include <volk/volk.h>
|
|
|
|
#include <volk_gnsssdr/volk_gnsssdr.h>
|
2018-02-25 12:27:27 +00:00
|
|
|
#include <algorithm>
|
2017-08-11 03:18:38 +00:00
|
|
|
#include <chrono>
|
2014-03-23 09:45:03 +00:00
|
|
|
#include <complex>
|
2014-04-03 21:59:14 +00:00
|
|
|
#include <numeric>
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2014-03-28 17:52:51 +00:00
|
|
|
DEFINE_int32(size_multiply_test, 100000, "Size of the arrays used for multiply testing");
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2017-06-25 20:53:11 +00:00
|
|
|
TEST(MultiplyTest, StandardCDoubleImplementation)
|
2012-01-23 00:52:05 +00:00
|
|
|
{
|
2019-02-11 14:33:24 +00:00
|
|
|
auto* input = new double[FLAGS_size_multiply_test];
|
|
|
|
auto* output = new double[FLAGS_size_multiply_test];
|
2018-02-25 12:27:27 +00:00
|
|
|
std::fill_n(input, FLAGS_size_multiply_test, 0.0);
|
2017-08-11 03:18:38 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
for (int i = 0; i < FLAGS_size_multiply_test; i++)
|
2014-03-23 09:45:03 +00:00
|
|
|
{
|
|
|
|
output[i] = input[i] * input[i];
|
|
|
|
}
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
2014-03-28 17:52:51 +00:00
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
2017-08-11 03:18:38 +00:00
|
|
|
<< " doubles in standard C finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2015-05-13 14:40:46 +00:00
|
|
|
|
2019-10-26 20:24:42 +00:00
|
|
|
double acc = 0.0;
|
|
|
|
double expected = 0.0;
|
2018-03-03 01:03:39 +00:00
|
|
|
for (int i = 0; i < FLAGS_size_multiply_test; i++)
|
2014-03-28 17:52:51 +00:00
|
|
|
{
|
|
|
|
acc += output[i];
|
|
|
|
}
|
2015-05-13 14:40:46 +00:00
|
|
|
delete[] input;
|
|
|
|
delete[] output;
|
2017-08-11 03:18:38 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
2014-03-28 17:52:51 +00:00
|
|
|
ASSERT_EQ(expected, acc);
|
2014-03-23 09:45:03 +00:00
|
|
|
}
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2017-06-25 20:53:11 +00:00
|
|
|
TEST(MultiplyTest, ArmadilloImplementation)
|
2014-03-23 09:45:03 +00:00
|
|
|
{
|
2014-03-28 17:52:51 +00:00
|
|
|
arma::vec input(FLAGS_size_multiply_test, arma::fill::zeros);
|
|
|
|
arma::vec output(FLAGS_size_multiply_test);
|
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-03-28 17:52:51 +00:00
|
|
|
|
|
|
|
output = input % input;
|
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
2014-03-28 17:52:51 +00:00
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
2017-08-11 03:18:38 +00:00
|
|
|
<< "-length double Armadillo vectors finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2017-08-11 03:18:38 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
2018-03-03 01:03:39 +00:00
|
|
|
ASSERT_EQ(0, arma::norm(output, 2));
|
2014-03-28 17:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-25 20:53:11 +00:00
|
|
|
TEST(MultiplyTest, StandardCComplexImplementation)
|
2014-03-28 17:52:51 +00:00
|
|
|
{
|
2019-02-11 14:33:24 +00:00
|
|
|
auto* input = new std::complex<float>[FLAGS_size_multiply_test];
|
|
|
|
auto* output = new std::complex<float>[FLAGS_size_multiply_test];
|
2018-02-25 12:27:27 +00:00
|
|
|
std::fill_n(input, FLAGS_size_multiply_test, std::complex<float>(0.0, 0.0));
|
2017-08-11 03:18:38 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
for (int i = 0; i < FLAGS_size_multiply_test; i++)
|
2012-01-23 00:52:05 +00:00
|
|
|
{
|
|
|
|
output[i] = input[i] * input[i];
|
|
|
|
}
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
2014-03-28 17:52:51 +00:00
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
2017-08-11 03:18:38 +00:00
|
|
|
<< " complex<float> in standard C finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2015-05-13 14:40:46 +00:00
|
|
|
|
2019-10-26 20:24:42 +00:00
|
|
|
std::complex<float> expected(0.0, 0.0);
|
|
|
|
std::complex<float> result(0.0, 0.0);
|
2018-03-03 01:03:39 +00:00
|
|
|
for (int i = 0; i < FLAGS_size_multiply_test; i++)
|
|
|
|
{
|
|
|
|
result += output[i];
|
|
|
|
}
|
2015-05-13 14:40:46 +00:00
|
|
|
delete[] input;
|
|
|
|
delete[] output;
|
2017-08-11 03:18:38 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
2014-03-28 17:52:51 +00:00
|
|
|
ASSERT_EQ(expected, result);
|
2014-03-23 09:45:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2017-06-25 20:53:11 +00:00
|
|
|
TEST(MultiplyTest, C11ComplexImplementation)
|
2014-03-29 19:11:32 +00:00
|
|
|
{
|
|
|
|
const std::vector<std::complex<float>> input(FLAGS_size_multiply_test);
|
|
|
|
std::vector<std::complex<float>> output(FLAGS_size_multiply_test);
|
2014-03-29 21:53:56 +00:00
|
|
|
int pos = 0;
|
2017-08-11 03:18:38 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-03-29 19:11:32 +00:00
|
|
|
|
|
|
|
// Trying a range-based for
|
2018-03-03 01:03:39 +00:00
|
|
|
for (const auto& item : input)
|
2014-03-29 19:11:32 +00:00
|
|
|
{
|
2014-03-29 21:53:56 +00:00
|
|
|
output[pos++] = item * item;
|
2014-03-29 19:11:32 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
2014-03-29 19:11:32 +00:00
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
2017-08-11 03:18:38 +00:00
|
|
|
<< " complex<float> vector (C++11-style) finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2017-08-11 03:18:38 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
2014-03-29 19:11:32 +00:00
|
|
|
|
2019-10-26 20:24:42 +00:00
|
|
|
std::complex<float> expected(0.0, 0.0);
|
2014-04-03 21:59:14 +00:00
|
|
|
auto result = std::inner_product(output.begin(), output.end(), output.begin(), expected);
|
2014-03-29 19:11:32 +00:00
|
|
|
ASSERT_EQ(expected, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-25 20:53:11 +00:00
|
|
|
TEST(MultiplyTest, ArmadilloComplexImplementation)
|
2014-03-23 09:45:03 +00:00
|
|
|
{
|
|
|
|
arma::cx_fvec input(FLAGS_size_multiply_test, arma::fill::zeros);
|
|
|
|
arma::cx_fvec output(FLAGS_size_multiply_test);
|
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-03-23 09:45:03 +00:00
|
|
|
|
|
|
|
output = input % input;
|
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
2014-03-28 17:52:51 +00:00
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
2017-08-11 03:18:38 +00:00
|
|
|
<< "-length complex float Armadillo vectors finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2017-08-11 03:18:38 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
2018-03-03 01:03:39 +00:00
|
|
|
ASSERT_EQ(0, arma::norm(output, 2));
|
2014-03-23 09:45:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 17:52:51 +00:00
|
|
|
|
2017-06-25 20:53:11 +00:00
|
|
|
TEST(MultiplyTest, VolkComplexImplementation)
|
2014-03-23 09:45:03 +00:00
|
|
|
{
|
2019-02-11 14:33:24 +00:00
|
|
|
auto* input = static_cast<std::complex<float>*>(volk_gnsssdr_malloc(FLAGS_size_multiply_test * sizeof(std::complex<float>), volk_gnsssdr_get_alignment()));
|
|
|
|
auto* output = static_cast<std::complex<float>*>(volk_gnsssdr_malloc(FLAGS_size_multiply_test * sizeof(std::complex<float>), volk_gnsssdr_get_alignment()));
|
2018-02-25 12:27:27 +00:00
|
|
|
std::fill_n(input, FLAGS_size_multiply_test, std::complex<float>(0.0, 0.0));
|
2014-03-23 09:45:03 +00:00
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
2014-03-23 09:45:03 +00:00
|
|
|
|
2014-03-28 17:52:51 +00:00
|
|
|
volk_32fc_x2_multiply_32fc(output, input, input, FLAGS_size_multiply_test);
|
2011-10-01 18:45:20 +00:00
|
|
|
|
2017-08-11 03:18:38 +00:00
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
2014-03-28 17:52:51 +00:00
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
2017-08-11 03:18:38 +00:00
|
|
|
<< "-length complex float vector using VOLK finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2017-08-11 03:18:38 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
2014-03-28 22:30:20 +00:00
|
|
|
|
2019-02-11 14:33:24 +00:00
|
|
|
auto* mag = static_cast<float*>(volk_gnsssdr_malloc(FLAGS_size_multiply_test * sizeof(float), volk_gnsssdr_get_alignment()));
|
2014-03-28 17:52:51 +00:00
|
|
|
volk_32fc_magnitude_32f(mag, output, FLAGS_size_multiply_test);
|
2014-09-09 20:51:47 +00:00
|
|
|
|
2019-10-26 20:24:42 +00:00
|
|
|
auto* result = new float(0.0);
|
2014-03-28 17:52:51 +00:00
|
|
|
volk_32f_accumulator_s32f(result, mag, FLAGS_size_multiply_test);
|
|
|
|
// Comparing floating-point numbers is tricky.
|
|
|
|
// Due to round-off errors, it is very unlikely that two floating-points will match exactly.
|
2020-02-05 20:24:46 +00:00
|
|
|
// See https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#floating-point-comparison
|
2019-10-26 20:24:42 +00:00
|
|
|
float expected = 0.0;
|
2014-03-28 22:30:20 +00:00
|
|
|
ASSERT_FLOAT_EQ(expected, result[0]);
|
2016-08-18 12:17:02 +00:00
|
|
|
volk_gnsssdr_free(input);
|
|
|
|
volk_gnsssdr_free(output);
|
|
|
|
volk_gnsssdr_free(mag);
|
2011-10-01 18:45:20 +00:00
|
|
|
}
|
2019-10-26 20:24:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(MultiplyTest, VolkComplexImplementationAlloc)
|
|
|
|
{
|
|
|
|
volk_gnsssdr::vector<std::complex<float>> input(FLAGS_size_multiply_test, std::complex<float>(0.0, 0.0));
|
|
|
|
volk_gnsssdr::vector<std::complex<float>> output(FLAGS_size_multiply_test);
|
|
|
|
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> start, end;
|
|
|
|
start = std::chrono::system_clock::now();
|
|
|
|
|
|
|
|
volk_32fc_x2_multiply_32fc(output.data(), input.data(), input.data(), FLAGS_size_multiply_test);
|
|
|
|
|
|
|
|
end = std::chrono::system_clock::now();
|
|
|
|
std::chrono::duration<double> elapsed_seconds = end - start;
|
|
|
|
std::cout << "Element-wise multiplication of " << FLAGS_size_multiply_test
|
|
|
|
<< "-length complex float vector using VOLK ALLOC finished in " << elapsed_seconds.count() * 1e6
|
2020-07-07 16:53:50 +00:00
|
|
|
<< " microseconds\n";
|
2019-10-26 20:24:42 +00:00
|
|
|
ASSERT_LE(0, elapsed_seconds.count() * 1e6);
|
|
|
|
volk_gnsssdr::vector<float> mag(FLAGS_size_multiply_test);
|
|
|
|
volk_32fc_magnitude_32f(mag.data(), output.data(), FLAGS_size_multiply_test);
|
|
|
|
|
|
|
|
auto* result = new float(0.0);
|
|
|
|
volk_32f_accumulator_s32f(result, mag.data(), FLAGS_size_multiply_test);
|
|
|
|
// Comparing floating-point numbers is tricky.
|
|
|
|
// Due to round-off errors, it is very unlikely that two floating-points will match exactly.
|
2020-02-05 20:24:46 +00:00
|
|
|
// See https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#floating-point-comparison
|
2019-10-26 20:24:42 +00:00
|
|
|
float expected = 0.0;
|
|
|
|
ASSERT_FLOAT_EQ(expected, result[0]);
|
|
|
|
}
|