123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #include "Eigen/Dense"
- #include "benchmark/benchmark.h"
- #include "ceres/small_blas.h"
- namespace ceres {
- class MatrixVectorMultiplyData {
- public:
- MatrixVectorMultiplyData(int rows, int cols)
- : num_elements_(1000),
- rows_(rows),
- cols_(cols),
- a_(num_elements_ * rows, 1.001),
- b_(num_elements_ * rows * cols, 1.5),
- c_(num_elements_ * cols, 1.00003) {}
- int num_elements() const { return num_elements_; }
- double* GetA(int i) { return &a_[i * rows_]; }
- double* GetB(int i) { return &b_[i * rows_ * cols_]; }
- double* GetC(int i) { return &c_[i * cols_]; }
- private:
- const int num_elements_;
- const int rows_;
- const int cols_;
- std::vector<double> a_;
- std::vector<double> b_;
- std::vector<double> c_;
- };
- static void MatrixSizeArguments(benchmark::internal::Benchmark* benchmark) {
- std::vector<int> rows = {1, 2, 3, 4, 6, 8};
- std::vector<int> cols = {1, 2, 3, 4, 8, 12, 15};
- for (int r : rows) {
- for (int c : cols) {
- benchmark->Args({r, c});
- }
- }
- }
- static void BM_MatrixVectorMultiply(benchmark::State& state) {
- const int rows = state.range(0);
- const int cols = state.range(1);
- MatrixVectorMultiplyData data(rows, cols);
- const int num_elements = data.num_elements();
- int iter = 0;
- for (auto _ : state) {
-
- internal::MatrixVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
- data.GetB(iter), rows, cols, data.GetC(iter), data.GetA(iter));
- iter = (iter + 1) % num_elements;
- }
- }
- BENCHMARK(BM_MatrixVectorMultiply)->Apply(MatrixSizeArguments);
- static void BM_MatrixTransposeVectorMultiply(benchmark::State& state) {
- const int rows = state.range(0);
- const int cols = state.range(1);
- MatrixVectorMultiplyData data(cols, rows);
- const int num_elements = data.num_elements();
- int iter = 0;
- for (auto _ : state) {
- internal::MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
- data.GetB(iter), rows, cols, data.GetC(iter), data.GetA(iter));
- iter = (iter + 1) % num_elements;
- }
- }
- BENCHMARK(BM_MatrixTransposeVectorMultiply)->Apply(MatrixSizeArguments);
- }
- BENCHMARK_MAIN();
|