Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2018 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Authors: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include "Eigen/Dense" |
| 32 | #include "benchmark/benchmark.h" |
| 33 | #include "ceres/small_blas.h" |
| 34 | |
| 35 | namespace ceres { |
| 36 | |
| 37 | // Benchmarking matrix-vector multiply routines and optimizing memory |
| 38 | // access requires that we make sure that they are not just sitting in |
| 39 | // the cache. So, as the benchmarking routine iterates, we need to |
| 40 | // multiply new/different matrice and vectors. Allocating/creating |
| 41 | // these objects in the benchmarking loop is too heavy duty, so we |
| 42 | // create them before hand and cycle through them in the |
| 43 | // benchmark. This class, given the size of the matrix creates such |
| 44 | // matrix and vector objects for use in the benchmark. |
| 45 | class MatrixVectorMultiplyData { |
| 46 | public: |
| 47 | MatrixVectorMultiplyData(int rows, int cols) |
| 48 | : num_elements_(1000), |
| 49 | rows_(rows), |
| 50 | cols_(cols), |
| 51 | a_(num_elements_ * rows, 1.001), |
| 52 | b_(num_elements_ * rows * cols, 1.5), |
| 53 | c_(num_elements_ * cols, 1.00003) {} |
| 54 | |
| 55 | int num_elements() const { return num_elements_; } |
| 56 | double* GetA(int i) { return &a_[i * rows_]; } |
| 57 | double* GetB(int i) { return &b_[i * rows_ * cols_]; } |
| 58 | double* GetC(int i) { return &c_[i * cols_]; } |
| 59 | |
| 60 | private: |
| 61 | const int num_elements_; |
| 62 | const int rows_; |
| 63 | const int cols_; |
| 64 | std::vector<double> a_; |
| 65 | std::vector<double> b_; |
| 66 | std::vector<double> c_; |
| 67 | }; |
| 68 | |
| 69 | // Helper function to generate the various matrix sizes for which we |
| 70 | // run the benchmark. |
| 71 | static void MatrixSizeArguments(benchmark::internal::Benchmark* benchmark) { |
| 72 | std::vector<int> rows = {1, 2, 3, 4, 6, 8}; |
| 73 | std::vector<int> cols = {1, 2, 3, 4, 8, 12, 15}; |
| 74 | for (int r : rows) { |
| 75 | for (int c : cols) { |
| 76 | benchmark->Args({r, c}); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void BM_MatrixVectorMultiply(benchmark::State& state) { |
| 82 | const int rows = state.range(0); |
| 83 | const int cols = state.range(1); |
| 84 | MatrixVectorMultiplyData data(rows, cols); |
| 85 | const int num_elements = data.num_elements(); |
| 86 | int iter = 0; |
| 87 | for (auto _ : state) { |
| 88 | // A += B * C; |
| 89 | internal::MatrixVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>( |
| 90 | data.GetB(iter), rows, cols, data.GetC(iter), data.GetA(iter)); |
| 91 | iter = (iter + 1) % num_elements; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | BENCHMARK(BM_MatrixVectorMultiply)->Apply(MatrixSizeArguments); |
| 96 | |
| 97 | void BM_MatrixTransposeVectorMultiply(benchmark::State& state) { |
| 98 | const int rows = state.range(0); |
| 99 | const int cols = state.range(1); |
| 100 | MatrixVectorMultiplyData data(cols, rows); |
| 101 | const int num_elements = data.num_elements(); |
| 102 | int iter = 0; |
| 103 | for (auto _ : state) { |
| 104 | internal::MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>( |
| 105 | data.GetB(iter), rows, cols, data.GetC(iter), data.GetA(iter)); |
| 106 | iter = (iter + 1) % num_elements; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | BENCHMARK(BM_MatrixTransposeVectorMultiply)->Apply(MatrixSizeArguments); |
| 111 | |
| 112 | } // namespace ceres |
| 113 | |
| 114 | BENCHMARK_MAIN(); |