Added google benchmark support + small test

This commit is contained in:
emeric
2024-01-07 15:49:30 +01:00
parent 5882e61886
commit 19b0856dac
6 changed files with 211 additions and 138 deletions
+10
View File
@@ -24,6 +24,7 @@ pkg_check_modules(LIBAV IMPORTED_TARGET libavcodec libavutil libavformat)
pkg_check_modules(Archive REQUIRED IMPORTED_TARGET libarchive) pkg_check_modules(Archive REQUIRED IMPORTED_TARGET libarchive)
find_package(PAM) find_package(PAM)
find_package(STB) find_package(STB)
find_package(benchmark)
# WT # WT
if (NOT Wt_FOUND) if (NOT Wt_FOUND)
@@ -70,6 +71,15 @@ elseif (IMAGE_LIBRARY STREQUAL STB AND NOT STB_FOUND)
endif () endif ()
message(STATUS "IMAGE_LIBRARY set to ${IMAGE_LIBRARY}") message(STATUS "IMAGE_LIBRARY set to ${IMAGE_LIBRARY}")
# Benchmark
if (benchmark_FOUND)
set(BUILD_BENCHMARKS ON)
message(STATUS "Google Benchmark found. Building benchmarks")
else ()
set(BUILD_BENCHMARKS OFF)
message(STATUS "Google Benchmark not found. Not building benchmarks")
endif()
add_subdirectory(src) add_subdirectory(src)
install(DIRECTORY approot DESTINATION share/lms) install(DIRECTORY approot DESTINATION share/lms)
+4
View File
@@ -22,3 +22,7 @@ install(TARGETS lmssom DESTINATION lib)
if(BUILD_TESTING) if(BUILD_TESTING)
add_subdirectory(test) add_subdirectory(test)
endif() endif()
if (BUILD_BENCHMARKS)
add_subdirectory(bench)
endif()
+9
View File
@@ -0,0 +1,9 @@
add_executable(bench-som
SomBench.cpp
)
target_link_libraries(bench-som PRIVATE
lmssom
benchmark
)
+54
View File
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <random>
#include <benchmark/benchmark.h>
#include "som/Network.hpp"
using namespace SOM;
// Benchmark function
static void BM_Matrix(benchmark::State& state)
{
std::minstd_rand randomEngine{ 42 };
std::uniform_int_distribution distrib{ 0, 1000 };
Matrix<int> matrix{ static_cast<Coordinate>(state.range(0)), static_cast<Coordinate>(state.range(0)) };
for (Coordinate x {}; x < matrix.getWidth(); ++x )
{
for (Coordinate y {}; y < matrix.getHeight(); ++y )
matrix.get({ x, y }) = distrib(randomEngine);
}
for (auto _ : state)
{
// Code inside this loop is measured repeatedly
const Position pos{ matrix.getPositionMinElement([](int a, int b) { return a < b; }) };
benchmark::DoNotOptimize(pos);
}
// Perform cleanup here if needed
}
// Register the benchmark with custom range
BENCHMARK(BM_Matrix)->Arg(3)->Arg(6)->Arg(12)->Arg(24);
BENCHMARK_MAIN();
+28 -32
View File
@@ -26,12 +26,10 @@
namespace SOM namespace SOM
{ {
using Coordinate = unsigned;
using Coordinate = unsigned; struct Position
using Norm = InputVector::value_type; {
struct Position
{
Coordinate x; Coordinate x;
Coordinate y; Coordinate y;
@@ -47,33 +45,32 @@ struct Position
{ {
return x == other.x && y == other.y; return x == other.x && y == other.y;
} }
}; };
template <typename T> template <typename T>
class Matrix class Matrix
{ {
public: public:
Matrix() = default; Matrix() = default;
Matrix(Coordinate width, Coordinate height) Matrix(Coordinate width, Coordinate height)
: _width {width} : _width{ width }
, _height {height} , _height{ height }
{ {
_values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height)); _values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height));
} }
template<typename... CtArgs> template<typename... CtrArgs>
Matrix(Coordinate width, Coordinate height, CtArgs... args) Matrix(Coordinate width, Coordinate height, CtrArgs&& ... args)
: _width {width} : _width{ width }
, _height {height} , _height{ height }
{ {
_values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height), T{args...}); _values.resize(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height), T{ std::forward<CtrArgs>(args)... });
} }
void clear() void clear()
{ {
std::vector<T> values(static_cast<std::size_t>(_width) * static_cast<std::size_t>(_height)); _values.clear();
_values.swap(values);
} }
Coordinate getHeight() const { return _height; } Coordinate getHeight() const { return _height; }
@@ -83,14 +80,14 @@ class Matrix
{ {
assert(position.x < _width); assert(position.x < _width);
assert(position.y < _height); assert(position.y < _height);
return _values[position.x + _width*position.y]; return _values[position.x + _width * position.y];
} }
const T& get(const Position& position) const const T& get(const Position& position) const
{ {
assert(position.x < _width); assert(position.x < _width);
assert(position.y < _height); assert(position.y < _height);
return _values[position.x + _width*position.y]; return _values[position.x + _width * position.y];
} }
T& operator[](const Position& position) { return get(position); } T& operator[](const Position& position) { return get(position); }
@@ -101,25 +98,25 @@ class Matrix
{ {
assert(!_values.empty()); assert(!_values.empty());
auto it {std::min_element(_values.begin(), _values.end(), std::move(func))}; const auto it{ std::min_element(_values.begin(), _values.end(), std::move(func)) };
auto index {static_cast<Coordinate>(std::distance(_values.begin(), it))}; const auto index{ static_cast<Coordinate>(std::distance(_values.begin(), it)) };
return {index % _height, index / _height}; return Position{ index % _height, index / _height };
} }
private: private:
Coordinate _width {}; Coordinate _width{};
Coordinate _height {}; Coordinate _height{};
std::vector<T> _values; std::vector<T> _values;
}; };
} // ns SOM } // ns SOM
namespace std { namespace std
template<>
class hash<SOM::Position>
{ {
template<>
class hash<SOM::Position>
{
public: public:
size_t operator()(const SOM::Position& s) const size_t operator()(const SOM::Position& s) const
{ {
@@ -127,7 +124,6 @@ class hash<SOM::Position>
size_t h2 = std::hash<SOM::Coordinate>()(s.y); size_t h2 = std::hash<SOM::Coordinate>()(s.y);
return h1 ^ (h2 << 1); return h1 ^ (h2 << 1);
} }
}; };
} // ns std } // ns std
+9 -9
View File
@@ -30,15 +30,15 @@
namespace SOM namespace SOM
{ {
using LearningFactor = InputVector::value_type; using LearningFactor = InputVector::value_type;
using Norm = InputVector::value_type;
void checkSameDimensions(const InputVector& a, const InputVector& b); void checkSameDimensions(const InputVector& a, const InputVector& b);
void checkSameDimensions(const InputVector& a, std::size_t inputDimCount); void checkSameDimensions(const InputVector& a, std::size_t inputDimCount);
std::ostream& operator<<(std::ostream& os, const InputVector& a); std::ostream& operator<<(std::ostream& os, const InputVector& a);
class Network
class Network {
{
public: public:
// Init a network with random values // Init a network with random values
Network(Coordinate width, Coordinate height, std::size_t inputDimCount); Network(Coordinate width, Coordinate height, std::size_t inputDimCount);
@@ -95,13 +95,13 @@ class Network
void updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration); void updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration);
std::size_t _inputDimCount {}; std::size_t _inputDimCount{};
InputVector _weights; // weight for each dimension InputVector _weights; // weight for each dimension
Matrix<InputVector> _refVectors; Matrix<InputVector> _refVectors;
DistanceFunc _distanceFunc; DistanceFunc _distanceFunc;
LearningFactorFunc _learningFactorFunc; LearningFactorFunc _learningFactorFunc;
NeighbourhoodFunc _neighbourhoodFunc; NeighbourhoodFunc _neighbourhoodFunc;
}; };
} // namespace SOM } // namespace SOM