Merge branch 'benchmarks' into develop

This commit is contained in:
emeric
2024-02-03 14:58:13 +01:00
8 changed files with 212 additions and 140 deletions
+7
View File
@@ -70,6 +70,13 @@ 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
option(BUILD_BENCHMARKS "Build benchmarks" OFF)
if (BUILD_BENCHMARKS)
find_package(benchmark REQUIRED)
message(STATUS "Building benchmarks")
endif()
add_subdirectory(src) add_subdirectory(src)
install(DIRECTORY approot DESTINATION share/lms) install(DIRECTORY approot DESTINATION share/lms)
+2 -1
View File
@@ -15,6 +15,7 @@ ARG LMS_BUILD_PACKAGES=" \
gcc \ gcc \
g++ \ g++ \
musl-dev \ musl-dev \
benchmark-dev \
boost-dev \ boost-dev \
ffmpeg-dev \ ffmpeg-dev \
libarchive-dev \ libarchive-dev \
@@ -35,7 +36,7 @@ ARG LMS_BUILD_TYPE="Release"
RUN \ RUN \
DIR=/tmp/lms/build && mkdir -p ${DIR} && cd ${DIR} && \ DIR=/tmp/lms/build && mkdir -p ${DIR} && cd ${DIR} && \
xx-info is-cross && export BUILD_TESTS=OFF || export BUILD_TESTS=ON && \ xx-info is-cross && export BUILD_TESTS=OFF || export BUILD_TESTS=ON && \
PKG_CONFIG_PATH=/$(xx-info)/usr/lib/pkgconfig cmake /tmp/lms/ -DCMAKE_INCLUDE_PATH=${PREFIX}/include -DCMAKE_BUILD_TYPE=${LMS_BUILD_TYPE} $(xx-clang --print-cmake-defines) -DCMAKE_PREFIX_PATH=/$(xx-info)/usr/lib/cmake -DBUILD_TESTING=${BUILD_TESTS} && \ PKG_CONFIG_PATH=/$(xx-info)/usr/lib/pkgconfig cmake /tmp/lms/ -DCMAKE_INCLUDE_PATH=${PREFIX}/include -DCMAKE_BUILD_TYPE=${LMS_BUILD_TYPE} $(xx-clang --print-cmake-defines) -DCMAKE_PREFIX_PATH=/$(xx-info)/usr/lib/cmake -DBUILD_TESTING=${BUILD_TESTS} -DBUILD_BENCHMARKS=ON && \
VERBOSE=1 make -j$(nproc) && \ VERBOSE=1 make -j$(nproc) && \
xx-verify src/lms/lms && \ xx-verify src/lms/lms && \
(xx-info is-cross || make test) (xx-info is-cross || make test)
+2 -1
View File
@@ -1,6 +1,7 @@
FROM archlinux:latest FROM archlinux:latest
ARG BUILD_PACKAGES="\ ARG BUILD_PACKAGES="\
benchmark \
clang \ clang \
cmake \ cmake \
boost \ boost \
@@ -22,6 +23,6 @@ COPY . /tmp/lms/
ARG LMS_BUILD_TYPE="Release" ARG LMS_BUILD_TYPE="Release"
RUN \ RUN \
DIR=/tmp/lms/build && mkdir -p ${DIR} && cd ${DIR} && \ DIR=/tmp/lms/build && mkdir -p ${DIR} && cd ${DIR} && \
cmake /tmp/lms/ -DCMAKE_BUILD_TYPE=${LMS_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=/usr && \ cmake /tmp/lms/ -DCMAKE_BUILD_TYPE=${LMS_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_BENCHMARKS=ON && \
VERBOSE=1 make -j$(nproc) && \ VERBOSE=1 make -j$(nproc) && \
make test make test
+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();
+9 -13
View File
@@ -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
+1 -1
View File
@@ -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: