From 6717619f9f0ad310d37b558142519fc0cd7c3cf9 Mon Sep 17 00:00:00 2001 From: Christos Psarras Date: Sat, 27 Apr 2019 19:56:30 +0200 Subject: [PATCH] Fix code formatting --- examples/benchmarker.cpp | 74 +++++----- include/benchmarker/benchmarker.hpp | 214 +++++++++++++--------------- 2 files changed, 137 insertions(+), 151 deletions(-) diff --git a/examples/benchmarker.cpp b/examples/benchmarker.cpp index e7e27b9..77d7e46 100644 --- a/examples/benchmarker.cpp +++ b/examples/benchmarker.cpp @@ -1,11 +1,10 @@ -// // Created by mcopik on 07.12.16. // #include -#include #include #include +#include #include #include @@ -13,53 +12,48 @@ using linalg_tests::benchmarker; #ifdef HAVE_EIGEN - /// Eigen kernel as a function object - struct eigen_kernel - { - benchmarker & b; - - eigen_kernel(benchmarker & b_) : - b(b_) {} - - void operator()(int rows, int cols) - { - Eigen::MatrixXd A = Eigen::MatrixXd::Random(rows, cols); - Eigen::MatrixXd B = Eigen::MatrixXd::Random(rows, cols); - - auto C = (A * B.transpose()).eval(); - } - - }; +/// Eigen kernel as a function object +struct eigen_kernel { + benchmarker& b; + + eigen_kernel(benchmarker& b_) + : b(b_) + { + } + + void operator()(int rows, int cols) + { + Eigen::MatrixXd A = Eigen::MatrixXd::Random(rows, cols); + Eigen::MatrixXd B = Eigen::MatrixXd::Random(rows, cols); + + auto C = (A * B.transpose()).eval(); + } +}; #endif int main() { - benchmarker benchmark; - benchmark.set_cache_size(8 * 1024 * 1024); - int rows = 200, cols = 100; - int iters = 5; + benchmarker benchmark; + benchmark.set_cache_size(8 * 1024 * 1024); + int rows = 200, cols = 100; + int iters = 5; - // Run a functor without benchmark arg + // Run a functor without benchmark arg #ifdef HAVE_ARMADILLO - std::array labels_arma{{"Armadillo", "1"}}; - benchmark.run(std::cout, labels_arma, iters, - [=, &benchmark]() { - arma::mat A = arma::randu(rows, cols); - arma::mat B = arma::randu(rows, cols); - - auto C = (A * B.t() ).eval(); - }); + std::array labels_arma{ { "Armadillo", "1" } }; + benchmark.run(std::cout, labels_arma, iters, + [=, &benchmark]() { + arma::mat A = arma::randu(rows, cols); + arma::mat B = arma::randu(rows, cols); + + auto C = (A * B.t()).eval(); + }); #endif #ifdef HAVE_EIGEN - std::array labels_eigen{{"Eigen", "1"}}; - eigen_kernel k(benchmark); - benchmark.run(std::cout, labels_eigen, iters, k, rows, cols); + std::array labels_eigen{ { "Eigen", "1" } }; + eigen_kernel k(benchmark); + benchmark.run(std::cout, labels_eigen, iters, k, rows, cols); #endif - } - - - - diff --git a/include/benchmarker/benchmarker.hpp b/include/benchmarker/benchmarker.hpp index bc5d3e0..8dc290e 100644 --- a/include/benchmarker/benchmarker.hpp +++ b/include/benchmarker/benchmarker.hpp @@ -1,136 +1,128 @@ - #ifndef LINALG_TESTS_BENCHMARK_HPP #define LINALG_TESTS_BENCHMARK_HPP -#include -#include -#include #include -#include -#include +#include #include +#include #include +#include +#include +#include namespace linalg_tests { - class basic_benchmarker; - - namespace detail { +class basic_benchmarker; - struct call_helper - { - template - static auto call(basic_benchmarker & b, F && f, Args &&... args) - -> std::result_of_t - { - return f(b, std::forward(args)...); - } +namespace detail { - template - static auto call(basic_benchmarker &, F && f, Args &&... args) - -> std::result_of_t - { - return f(std::forward(args)...); - } - }; + struct call_helper { + template + static auto call(basic_benchmarker& b, F&& f, Args&&... args) + -> std::result_of_t + { + return f(b, std::forward(args)...); + } + template + static auto call(basic_benchmarker&, F&& f, Args&&... args) + -> std::result_of_t + { + return f(std::forward(args)...); } + }; +} - /** +/** * Simple benchmarker capable of running a function and gathering measurements * * Pointers to member functions are not supported. If necessary, they can be implemented * by extending detail::call_helper with C++17 std::invoke * */ - class basic_benchmarker { - - std::vector measurements; - - double dtime_save; - typedef std::chrono::high_resolution_clock clock; - clock::time_point dtime; - - // cache trash - std::unique_ptr cache_trash; - uint32_t trash_size; - - const std::string output_separator = "\t"; - - public: - - basic_benchmarker() : - cache_trash(nullptr) - {} - - void set_cache_size(int l3_size) - { - trash_size = l3_size / sizeof(double); - cache_trash.reset(new double[trash_size]); - - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_real_distribution<> dis(0, 1); - std::for_each(cache_trash.get(), cache_trash.get() + trash_size, [&](double & val) {val = dis(gen);}); - } - - void start_clock() - { - dtime = clock::now(); - } - - void stop_clock() - { - auto x = clock::now(); - dtime_save = std::chrono::duration_cast(x - dtime).count() / 1e9; - } - - template - void run(std::ostream & os, ULabels && user_labels, uint32_t iters, F && f, Args &&... args) - { - measurements.resize(iters); - - for (uint32_t i = 0; i < iters; ++i) { - trash_cache(); - - start_clock(); - detail::call_helper::call(*this, std::forward(f), std::forward(args)...); - stop_clock(); - measurements[i] = dtime_save; - } - - print_results(os, user_labels); - - } - - /// size of runs_labels needs to be greater or equal to number of runs - template - void print_results(std::ostream & os, ULabels && user_labels) - { - for(auto & user_label : user_labels) - os << user_label << output_separator; - for(auto & measurement : measurements) - os << measurement << output_separator; - os << std::endl; - } - - - private: - - void trash_cache() - { - if(cache_trash) { - std::for_each(cache_trash.get(), cache_trash.get() + trash_size, - [](double & val) { - val += 0.01; - }); - } - } +class basic_benchmarker { + + std::vector measurements; + + double dtime_save; + typedef std::chrono::high_resolution_clock clock; + clock::time_point dtime; + + // cache trash + std::unique_ptr cache_trash; + uint32_t trash_size; + + const std::string output_separator = "\t"; + + public: + basic_benchmarker() + : cache_trash(nullptr) + { + } + + void set_cache_size(int l3_size) + { + trash_size = l3_size / sizeof(double); + cache_trash.reset(new double[trash_size]); + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dis(0, 1); + std::for_each(cache_trash.get(), cache_trash.get() + trash_size, [&](double& val) { val = dis(gen); }); + } + + void start_clock() + { + dtime = clock::now(); + } + + void stop_clock() + { + auto x = clock::now(); + dtime_save = std::chrono::duration_cast(x - dtime).count() / 1e9; + } + + template + void run(std::ostream& os, ULabels&& user_labels, uint32_t iters, F&& f, Args&&... args) + { + measurements.resize(iters); + + for (uint32_t i = 0; i < iters; ++i) { + trash_cache(); + + start_clock(); + detail::call_helper::call(*this, std::forward(f), std::forward(args)...); + stop_clock(); + measurements[i] = dtime_save; + } - }; + print_results(os, user_labels); + } + + /// size of runs_labels needs to be greater or equal to number of runs + template + void print_results(std::ostream& os, ULabels&& user_labels) + { + for (auto& user_label : user_labels) + os << user_label << output_separator; + for (auto& measurement : measurements) + os << measurement << output_separator; + os << std::endl; + } + + private: + void trash_cache() + { + if (cache_trash) { + std::for_each(cache_trash.get(), cache_trash.get() + trash_size, + [](double& val) { + val += 0.01; + }); + } + } +}; - typedef basic_benchmarker benchmarker; +typedef basic_benchmarker benchmarker; } - #endif //LINALG_TESTS_BENCHMARK_HPP