jet_operator_benchmark.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2021 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. // Author: alex@karatarakis.com (Alexander Karatarakis)
  30. #include <array>
  31. #include "benchmark/benchmark.h"
  32. #include "ceres/jet.h"
  33. namespace ceres {
  34. // Cycle the Jets to avoid caching effects in the benchmark.
  35. template <class JetType>
  36. class JetInputData {
  37. using T = typename JetType::Scalar;
  38. static constexpr std::size_t SIZE = 20;
  39. public:
  40. JetInputData() {
  41. for (int i = 0; i < static_cast<int>(SIZE); i++) {
  42. const T ti = static_cast<T>(i + 1);
  43. a_[i].a = T(1.1) * ti;
  44. a_[i].v.setRandom();
  45. b_[i].a = T(2.2) * ti;
  46. b_[i].v.setRandom();
  47. c_[i].a = T(3.3) * ti;
  48. c_[i].v.setRandom();
  49. d_[i].a = T(4.4) * ti;
  50. d_[i].v.setRandom();
  51. e_[i].a = T(5.5) * ti;
  52. e_[i].v.setRandom();
  53. scalar_a_[i] = T(1.1) * ti;
  54. scalar_b_[i] = T(2.2) * ti;
  55. scalar_c_[i] = T(3.3) * ti;
  56. scalar_d_[i] = T(4.4) * ti;
  57. scalar_e_[i] = T(5.5) * ti;
  58. }
  59. }
  60. void advance() { index_ = (index_ + 1) % SIZE; }
  61. const JetType& a() const { return a_[index_]; }
  62. const JetType& b() const { return b_[index_]; }
  63. const JetType& c() const { return c_[index_]; }
  64. const JetType& d() const { return d_[index_]; }
  65. const JetType& e() const { return e_[index_]; }
  66. T scalar_a() const { return scalar_a_[index_]; }
  67. T scalar_b() const { return scalar_b_[index_]; }
  68. T scalar_c() const { return scalar_c_[index_]; }
  69. T scalar_d() const { return scalar_d_[index_]; }
  70. T scalar_e() const { return scalar_e_[index_]; }
  71. private:
  72. std::size_t index_{0};
  73. std::array<JetType, SIZE> a_{};
  74. std::array<JetType, SIZE> b_{};
  75. std::array<JetType, SIZE> c_{};
  76. std::array<JetType, SIZE> d_{};
  77. std::array<JetType, SIZE> e_{};
  78. std::array<T, SIZE> scalar_a_;
  79. std::array<T, SIZE> scalar_b_;
  80. std::array<T, SIZE> scalar_c_;
  81. std::array<T, SIZE> scalar_d_;
  82. std::array<T, SIZE> scalar_e_;
  83. };
  84. template <std::size_t JET_SIZE, class Function>
  85. static void JetBenchmarkHelper(benchmark::State& state, const Function& func) {
  86. using JetType = Jet<double, JET_SIZE>;
  87. JetInputData<JetType> data{};
  88. JetType out{};
  89. const int iterations = static_cast<int>(state.range(0));
  90. for (auto _ : state) {
  91. for (int i = 0; i < iterations; i++) {
  92. func(data, out);
  93. data.advance();
  94. }
  95. }
  96. benchmark::DoNotOptimize(out);
  97. }
  98. template <std::size_t JET_SIZE>
  99. static void Addition(benchmark::State& state) {
  100. using JetType = Jet<double, JET_SIZE>;
  101. JetBenchmarkHelper<JET_SIZE>(
  102. state, [](const JetInputData<JetType>& d, JetType& out) {
  103. out += +d.a() + d.b() + d.c() + d.d() + d.e();
  104. });
  105. }
  106. BENCHMARK_TEMPLATE(Addition, 3)->Arg(1000);
  107. BENCHMARK_TEMPLATE(Addition, 10)->Arg(1000);
  108. BENCHMARK_TEMPLATE(Addition, 15)->Arg(1000);
  109. BENCHMARK_TEMPLATE(Addition, 25)->Arg(1000);
  110. BENCHMARK_TEMPLATE(Addition, 32)->Arg(1000);
  111. BENCHMARK_TEMPLATE(Addition, 200)->Arg(160);
  112. template <std::size_t JET_SIZE>
  113. static void AdditionScalar(benchmark::State& state) {
  114. using JetType = Jet<double, JET_SIZE>;
  115. JetBenchmarkHelper<JET_SIZE>(
  116. state, [](const JetInputData<JetType>& d, JetType& out) {
  117. out +=
  118. d.scalar_a() + d.scalar_b() + d.c() + d.scalar_d() + d.scalar_e();
  119. });
  120. }
  121. BENCHMARK_TEMPLATE(AdditionScalar, 3)->Arg(1000);
  122. BENCHMARK_TEMPLATE(AdditionScalar, 10)->Arg(1000);
  123. BENCHMARK_TEMPLATE(AdditionScalar, 15)->Arg(1000);
  124. BENCHMARK_TEMPLATE(AdditionScalar, 25)->Arg(1000);
  125. BENCHMARK_TEMPLATE(AdditionScalar, 32)->Arg(1000);
  126. BENCHMARK_TEMPLATE(AdditionScalar, 200)->Arg(160);
  127. template <std::size_t JET_SIZE>
  128. static void Subtraction(benchmark::State& state) {
  129. using JetType = Jet<double, JET_SIZE>;
  130. JetBenchmarkHelper<JET_SIZE>(
  131. state, [](const JetInputData<JetType>& d, JetType& out) {
  132. out -= -d.a() - d.b() - d.c() - d.d() - d.e();
  133. });
  134. }
  135. BENCHMARK_TEMPLATE(Subtraction, 3)->Arg(1000);
  136. BENCHMARK_TEMPLATE(Subtraction, 10)->Arg(1000);
  137. BENCHMARK_TEMPLATE(Subtraction, 15)->Arg(1000);
  138. BENCHMARK_TEMPLATE(Subtraction, 25)->Arg(1000);
  139. BENCHMARK_TEMPLATE(Subtraction, 32)->Arg(1000);
  140. BENCHMARK_TEMPLATE(Subtraction, 200)->Arg(160);
  141. template <std::size_t JET_SIZE>
  142. static void SubtractionScalar(benchmark::State& state) {
  143. using JetType = Jet<double, JET_SIZE>;
  144. JetBenchmarkHelper<JET_SIZE>(
  145. state, [](const JetInputData<JetType>& d, JetType& out) {
  146. out -=
  147. -d.scalar_a() - d.scalar_b() - d.c() - d.scalar_d() - d.scalar_e();
  148. });
  149. }
  150. BENCHMARK_TEMPLATE(SubtractionScalar, 3)->Arg(1000);
  151. BENCHMARK_TEMPLATE(SubtractionScalar, 10)->Arg(1000);
  152. BENCHMARK_TEMPLATE(SubtractionScalar, 15)->Arg(1000);
  153. BENCHMARK_TEMPLATE(SubtractionScalar, 25)->Arg(1000);
  154. BENCHMARK_TEMPLATE(SubtractionScalar, 32)->Arg(1000);
  155. BENCHMARK_TEMPLATE(SubtractionScalar, 200)->Arg(160);
  156. template <std::size_t JET_SIZE>
  157. static void Multiplication(benchmark::State& state) {
  158. using JetType = Jet<double, JET_SIZE>;
  159. JetBenchmarkHelper<JET_SIZE>(
  160. state, [](const JetInputData<JetType>& d, JetType& out) {
  161. out *= d.a() * d.b() * d.c() * d.d() * d.e();
  162. });
  163. }
  164. BENCHMARK_TEMPLATE(Multiplication, 3)->Arg(1000);
  165. BENCHMARK_TEMPLATE(Multiplication, 10)->Arg(1000);
  166. BENCHMARK_TEMPLATE(Multiplication, 15)->Arg(1000);
  167. BENCHMARK_TEMPLATE(Multiplication, 25)->Arg(1000);
  168. BENCHMARK_TEMPLATE(Multiplication, 32)->Arg(1000);
  169. BENCHMARK_TEMPLATE(Multiplication, 200)->Arg(160);
  170. template <std::size_t JET_SIZE>
  171. static void MultiplicationLeftScalar(benchmark::State& state) {
  172. using JetType = Jet<double, JET_SIZE>;
  173. JetBenchmarkHelper<JET_SIZE>(
  174. state, [](const JetInputData<JetType>& d, JetType& out) {
  175. out += d.scalar_a() *
  176. (d.scalar_b() * (d.scalar_c() * (d.scalar_d() * d.e())));
  177. });
  178. }
  179. BENCHMARK_TEMPLATE(MultiplicationLeftScalar, 3)->Arg(1000);
  180. BENCHMARK_TEMPLATE(MultiplicationLeftScalar, 10)->Arg(1000);
  181. BENCHMARK_TEMPLATE(MultiplicationLeftScalar, 15)->Arg(1000);
  182. BENCHMARK_TEMPLATE(MultiplicationLeftScalar, 25)->Arg(1000);
  183. BENCHMARK_TEMPLATE(MultiplicationLeftScalar, 32)->Arg(1000);
  184. BENCHMARK_TEMPLATE(MultiplicationLeftScalar, 200)->Arg(160);
  185. template <std::size_t JET_SIZE>
  186. static void MultiplicationRightScalar(benchmark::State& state) {
  187. using JetType = Jet<double, JET_SIZE>;
  188. JetBenchmarkHelper<JET_SIZE>(
  189. state, [](const JetInputData<JetType>& d, JetType& out) {
  190. out += (((d.a() * d.scalar_b()) * d.scalar_c()) * d.scalar_d()) *
  191. d.scalar_e();
  192. });
  193. }
  194. BENCHMARK_TEMPLATE(MultiplicationRightScalar, 3)->Arg(1000);
  195. BENCHMARK_TEMPLATE(MultiplicationRightScalar, 10)->Arg(1000);
  196. BENCHMARK_TEMPLATE(MultiplicationRightScalar, 15)->Arg(1000);
  197. BENCHMARK_TEMPLATE(MultiplicationRightScalar, 25)->Arg(1000);
  198. BENCHMARK_TEMPLATE(MultiplicationRightScalar, 32)->Arg(1000);
  199. BENCHMARK_TEMPLATE(MultiplicationRightScalar, 200)->Arg(160);
  200. template <std::size_t JET_SIZE>
  201. static void Division(benchmark::State& state) {
  202. using JetType = Jet<double, JET_SIZE>;
  203. JetBenchmarkHelper<JET_SIZE>(
  204. state, [](const JetInputData<JetType>& d, JetType& out) {
  205. out /= d.a() / d.b() / d.c() / d.d() / d.e();
  206. });
  207. }
  208. BENCHMARK_TEMPLATE(Division, 3)->Arg(1000);
  209. BENCHMARK_TEMPLATE(Division, 10)->Arg(1000);
  210. BENCHMARK_TEMPLATE(Division, 15)->Arg(1000);
  211. BENCHMARK_TEMPLATE(Division, 25)->Arg(1000);
  212. BENCHMARK_TEMPLATE(Division, 32)->Arg(1000);
  213. BENCHMARK_TEMPLATE(Division, 200)->Arg(160);
  214. template <std::size_t JET_SIZE>
  215. static void DivisionLeftScalar(benchmark::State& state) {
  216. using JetType = Jet<double, JET_SIZE>;
  217. JetBenchmarkHelper<JET_SIZE>(
  218. state, [](const JetInputData<JetType>& d, JetType& out) {
  219. out += d.scalar_a() /
  220. (d.scalar_b() / (d.scalar_c() / (d.scalar_d() / d.e())));
  221. });
  222. }
  223. BENCHMARK_TEMPLATE(DivisionLeftScalar, 3)->Arg(1000);
  224. BENCHMARK_TEMPLATE(DivisionLeftScalar, 10)->Arg(1000);
  225. BENCHMARK_TEMPLATE(DivisionLeftScalar, 15)->Arg(1000);
  226. BENCHMARK_TEMPLATE(DivisionLeftScalar, 25)->Arg(1000);
  227. BENCHMARK_TEMPLATE(DivisionLeftScalar, 32)->Arg(1000);
  228. BENCHMARK_TEMPLATE(DivisionLeftScalar, 200)->Arg(160);
  229. template <std::size_t JET_SIZE>
  230. static void DivisionRightScalar(benchmark::State& state) {
  231. using JetType = Jet<double, JET_SIZE>;
  232. JetBenchmarkHelper<JET_SIZE>(
  233. state, [](const JetInputData<JetType>& d, JetType& out) {
  234. out += (((d.a() / d.scalar_b()) / d.scalar_c()) / d.scalar_d()) /
  235. d.scalar_e();
  236. });
  237. }
  238. BENCHMARK_TEMPLATE(DivisionRightScalar, 3)->Arg(1000);
  239. BENCHMARK_TEMPLATE(DivisionRightScalar, 10)->Arg(1000);
  240. BENCHMARK_TEMPLATE(DivisionRightScalar, 15)->Arg(1000);
  241. BENCHMARK_TEMPLATE(DivisionRightScalar, 25)->Arg(1000);
  242. BENCHMARK_TEMPLATE(DivisionRightScalar, 32)->Arg(1000);
  243. BENCHMARK_TEMPLATE(DivisionRightScalar, 200)->Arg(160);
  244. template <std::size_t JET_SIZE>
  245. static void MultiplyAndAdd(benchmark::State& state) {
  246. using JetType = Jet<double, JET_SIZE>;
  247. JetBenchmarkHelper<JET_SIZE>(
  248. state, [](const JetInputData<JetType>& d, JetType& out) {
  249. out += d.scalar_a() * d.a() + d.scalar_b() * d.b() +
  250. d.scalar_c() * d.c() + d.scalar_d() * d.d() +
  251. d.scalar_e() * d.e();
  252. });
  253. }
  254. BENCHMARK_TEMPLATE(MultiplyAndAdd, 3)->Arg(1000);
  255. BENCHMARK_TEMPLATE(MultiplyAndAdd, 10)->Arg(1000);
  256. BENCHMARK_TEMPLATE(MultiplyAndAdd, 15)->Arg(1000);
  257. BENCHMARK_TEMPLATE(MultiplyAndAdd, 25)->Arg(1000);
  258. BENCHMARK_TEMPLATE(MultiplyAndAdd, 32)->Arg(1000);
  259. BENCHMARK_TEMPLATE(MultiplyAndAdd, 200)->Arg(160);
  260. } // namespace ceres
  261. BENCHMARK_MAIN();