Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ list(APPEND HEADER_FILES
${SOFABENCHMARK_SRC}/utils/thread_pool.hpp
)
list(APPEND SOURCE_FILES
${SOFABENCHMARK_SRC}/benchmarks/Sofa.LinearAlgebra/BTDMatrix.cpp
${SOFABENCHMARK_SRC}/benchmarks/Sofa.LinearAlgebra/SparseMatrixCompression.cpp
${SOFABENCHMARK_SRC}/benchmarks/Sofa.LinearAlgebra/SparseMatrixMulTranspose.cpp
${SOFABENCHMARK_SRC}/benchmarks/Sofa.LinearAlgebra/SparseMatrixProduct.cpp
Expand Down
83 changes: 83 additions & 0 deletions src/benchmarks/Sofa.LinearAlgebra/BTDMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <benchmark/benchmark.h>

#include <sofa/linearalgebra/BTDMatrix.h>
#include <utils/RandomValuePool.h>

constexpr int64_t maxSubIterations = 8 << 7;


template <std::size_t N, typename T>
static void prepareData(benchmark::State& state, sofa::linearalgebra::BTDMatrix<N, T>& matrix, std::vector<sofa::type::Mat<N, N, T>>& matricesToAdd, std::vector<std::pair<sofa::Index, sofa::Index>>& indices)
{
constexpr auto totalsize = maxSubIterations * N * N;
const std::array<T, totalsize>& values = RandomValuePool<T, totalsize>::get();

matricesToAdd.resize(state.range(0));
for (int64_t i = 0; i < state.range(0); i++)
{
for (sofa::Index j = 0; j < N; ++j)
{
for (sofa::Index k = 0; k < N; ++k)
{
matricesToAdd[i][j][k] = values[i * N * N + j * N + k];
}
}
}

auto randIndices = RandomValuePool<sofa::Index, maxSubIterations * 2>::get();
for (auto& id : randIndices)
{
id *= N;
}
indices.reserve(state.range(0));
for (int64_t i = 0; i < state.range(0) * 2; i += 2)
{
indices.emplace_back(randIndices[i], randIndices[i+1]);
}

const auto max = *std::max_element(randIndices.begin(), randIndices.end());
matrix.resize(N * ((max+N) / N), N * ((max+N) / N));
}

template<std::size_t N, typename T>
static void BM_BTDMatrix_add(benchmark::State& state)
{
sofa::linearalgebra::BTDMatrix<N, T> matrix;
std::vector<sofa::type::Mat<N, N, T>> matricesToAdd;
std::vector<std::pair<sofa::Index, sofa::Index>> indices;
prepareData<N, T>(state, matrix, matricesToAdd, indices);

for (auto _ : state)
{
for (int64_t i = 0; i < state.range(0); ++i)
{
for (sofa::Index j = 0; j < N; ++j)
{
for (sofa::Index k = 0; k < N; ++k)
{
matrix.add(indices[i].first, indices[i].second, matricesToAdd[i](j, k));
}
}
}
}
}

template<std::size_t N, typename T>
static void BM_BTDMatrix_addBlock(benchmark::State& state)
{
sofa::linearalgebra::BTDMatrix<N, T> matrix;
std::vector<sofa::type::Mat<N, N, T>> matricesToAdd;
std::vector<std::pair<sofa::Index, sofa::Index>> indices;
prepareData<N, T>(state, matrix, matricesToAdd, indices);

for (auto _ : state)
{
for (int64_t i = 0; i < state.range(0); ++i)
{
matrix.add(indices[i].first, indices[i].second, matricesToAdd[i]);
}
}
}

BENCHMARK_TEMPLATE(BM_BTDMatrix_add, 6, SReal)->Range(8 << 3, maxSubIterations);
BENCHMARK_TEMPLATE(BM_BTDMatrix_addBlock, 6, SReal)->Range(8 << 3, maxSubIterations);
Loading