Added google benchmark support + small test
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
add_executable(bench-som
|
||||||
|
SomBench.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(bench-som PRIVATE
|
||||||
|
lmssom
|
||||||
|
benchmark
|
||||||
|
)
|
||||||
@@ -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();
|
||||||
@@ -26,9 +26,7 @@
|
|||||||
|
|
||||||
namespace SOM
|
namespace SOM
|
||||||
{
|
{
|
||||||
|
|
||||||
using Coordinate = unsigned;
|
using Coordinate = unsigned;
|
||||||
using Norm = InputVector::value_type;
|
|
||||||
|
|
||||||
struct Position
|
struct Position
|
||||||
{
|
{
|
||||||
@@ -62,18 +60,17 @@ class Matrix
|
|||||||
_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; }
|
||||||
@@ -101,10 +98,10 @@ 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:
|
||||||
@@ -115,8 +112,8 @@ class Matrix
|
|||||||
|
|
||||||
} // ns SOM
|
} // ns SOM
|
||||||
|
|
||||||
namespace std {
|
namespace std
|
||||||
|
{
|
||||||
template<>
|
template<>
|
||||||
class hash<SOM::Position>
|
class hash<SOM::Position>
|
||||||
{
|
{
|
||||||
@@ -128,6 +125,5 @@ class hash<SOM::Position>
|
|||||||
return h1 ^ (h2 << 1);
|
return h1 ^ (h2 << 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // ns std
|
} // ns std
|
||||||
|
|
||||||
|
|||||||
@@ -31,12 +31,12 @@ 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:
|
||||||
|
|||||||
Reference in New Issue
Block a user