From 2b269f967cd71c0346b805d439e4c70fdd0468da Mon Sep 17 00:00:00 2001 From: FritzFlorian Date: Mon, 13 May 2019 13:54:30 +0200 Subject: [PATCH] Add matrix multiplication benchmark. --- app/benchmark_matrix/CMakeLists.txt | 5 +++++ app/benchmark_matrix/main.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 app/benchmark_matrix/CMakeLists.txt create mode 100644 app/benchmark_matrix/main.cpp diff --git a/app/benchmark_matrix/CMakeLists.txt b/app/benchmark_matrix/CMakeLists.txt new file mode 100644 index 0000000..0245a5b --- /dev/null +++ b/app/benchmark_matrix/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable(benchmark_matrix main.cpp) +target_link_libraries(benchmark_matrix pls) +if (EASY_PROFILER) + target_link_libraries(benchmark_matrix easy_profiler) +endif () diff --git a/app/benchmark_matrix/main.cpp b/app/benchmark_matrix/main.cpp new file mode 100644 index 0000000..c633820 --- /dev/null +++ b/app/benchmark_matrix/main.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +#include + +const int MATRIX_SIZE = 128; + +template +class matrix { + public: + T data[SIZE][SIZE]; + + matrix(T i = 1) { + std::fill(&data[0][0], &data[0][0] + SIZE * SIZE, i); + } + + void multiply(const matrix &a, const matrix &b) { + auto range = boost::irange(0, SIZE); + pls::algorithm::parallel_for(range.begin(), range.end(), [&](int i) { + this->multiply_column(i, a, b); + }); + } + + private: + void multiply_column(int i, const matrix &a, const matrix &b) { + for (int j = 0; j < SIZE; ++j) { + data[i][j] = 0; + } + for (int k = 0; k < SIZE; ++k) { + for (int j = 0; j < SIZE; ++j) { + data[i][j] += a.data[i][k] * b.data[k][j]; + } + } + } +}; + +void fill_with_data(matrix &a, matrix &b) { + // Fill in some data... + for (int i = 0; i < MATRIX_SIZE; i++) { + for (int j = 0; j < MATRIX_SIZE; j++) { + a.data[i][j] = i; + b.data[i][j] = j; + } + } +} + +int main() { + PROFILE_ENABLE + matrix a; + matrix b; + matrix result; + fill_with_data(a, b); + + pls::internal::helpers::run_mini_benchmark([&] { + result.multiply(a, b); + }, 8, 1000); + + PROFILE_SAVE("test_profile.prof") +} + +//int main() { +// PROFILE_ENABLE +// pls::malloc_scheduler_memory my_scheduler_memory{8, 2u << 18}; +// pls::scheduler scheduler{&my_scheduler_memory, 8}; +// +// matrix a; +// matrix b; +// matrix result; +// fill_with_data(a, b); +// +// scheduler.perform_work([&] { +// PROFILE_MAIN_THREAD +// for (int i = 0; i < 10; i++) { +// PROFILE_WORK_BLOCK("Top Level") +// result.multiply(a, b); +// } +// }); +// +// PROFILE_SAVE("test_profile.prof") +//} + -- libgit2 0.26.0