parallel_vector_operations_benchmark.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. #include <algorithm>
  29. #include "benchmark/benchmark.h"
  30. #include "ceres/eigen_vector_ops.h"
  31. #include "ceres/parallel_for.h"
  32. namespace ceres::internal {
  33. // Older versions of benchmark library (for example, one shipped with
  34. // ubuntu 20.04) do not support range generation and range products
  35. #define VECTOR_SIZES(num_threads) \
  36. Args({1 << 7, num_threads}) \
  37. ->Args({1 << 8, num_threads}) \
  38. ->Args({1 << 9, num_threads}) \
  39. ->Args({1 << 10, num_threads}) \
  40. ->Args({1 << 11, num_threads}) \
  41. ->Args({1 << 12, num_threads}) \
  42. ->Args({1 << 13, num_threads}) \
  43. ->Args({1 << 14, num_threads}) \
  44. ->Args({1 << 15, num_threads}) \
  45. ->Args({1 << 16, num_threads}) \
  46. ->Args({1 << 17, num_threads}) \
  47. ->Args({1 << 18, num_threads}) \
  48. ->Args({1 << 19, num_threads}) \
  49. ->Args({1 << 20, num_threads}) \
  50. ->Args({1 << 21, num_threads}) \
  51. ->Args({1 << 22, num_threads}) \
  52. ->Args({1 << 23, num_threads})
  53. #define VECTOR_SIZE_THREADS \
  54. VECTOR_SIZES(1) \
  55. ->VECTOR_SIZES(2) \
  56. ->VECTOR_SIZES(4) \
  57. ->VECTOR_SIZES(8) \
  58. ->VECTOR_SIZES(16)
  59. static void SetZero(benchmark::State& state) {
  60. const int kVectorSize = static_cast<int>(state.range(0));
  61. Vector x = Vector::Random(kVectorSize);
  62. for (auto _ : state) {
  63. x.setZero();
  64. }
  65. CHECK_EQ(x.squaredNorm(), 0.);
  66. }
  67. BENCHMARK(SetZero)->VECTOR_SIZES(1);
  68. static void SetZeroParallel(benchmark::State& state) {
  69. const int kVectorSize = static_cast<int>(state.range(0));
  70. const int num_threads = static_cast<int>(state.range(1));
  71. ContextImpl context;
  72. context.EnsureMinimumThreads(num_threads);
  73. Vector x = Vector::Random(kVectorSize);
  74. for (auto _ : state) {
  75. ParallelSetZero(&context, num_threads, x);
  76. }
  77. CHECK_EQ(x.squaredNorm(), 0.);
  78. }
  79. BENCHMARK(SetZeroParallel)->VECTOR_SIZE_THREADS;
  80. static void Negate(benchmark::State& state) {
  81. const int kVectorSize = static_cast<int>(state.range(0));
  82. Vector x = Vector::Random(kVectorSize).normalized();
  83. const Vector x_init = x;
  84. for (auto _ : state) {
  85. x = -x;
  86. }
  87. CHECK((x - x_init).squaredNorm() == 0. || (x + x_init).squaredNorm() == 0);
  88. }
  89. BENCHMARK(Negate)->VECTOR_SIZES(1);
  90. static void NegateParallel(benchmark::State& state) {
  91. const int kVectorSize = static_cast<int>(state.range(0));
  92. const int num_threads = static_cast<int>(state.range(1));
  93. ContextImpl context;
  94. context.EnsureMinimumThreads(num_threads);
  95. Vector x = Vector::Random(kVectorSize).normalized();
  96. const Vector x_init = x;
  97. for (auto _ : state) {
  98. ParallelAssign(&context, num_threads, x, -x);
  99. }
  100. CHECK((x - x_init).squaredNorm() == 0. || (x + x_init).squaredNorm() == 0);
  101. }
  102. BENCHMARK(NegateParallel)->VECTOR_SIZE_THREADS;
  103. static void Assign(benchmark::State& state) {
  104. const int kVectorSize = static_cast<int>(state.range(0));
  105. Vector x = Vector::Random(kVectorSize);
  106. Vector y = Vector(kVectorSize);
  107. for (auto _ : state) {
  108. y.block(0, 0, kVectorSize, 1) = x.block(0, 0, kVectorSize, 1);
  109. }
  110. CHECK_EQ((y - x).squaredNorm(), 0.);
  111. }
  112. BENCHMARK(Assign)->VECTOR_SIZES(1);
  113. static void AssignParallel(benchmark::State& state) {
  114. const int kVectorSize = static_cast<int>(state.range(0));
  115. const int num_threads = static_cast<int>(state.range(1));
  116. ContextImpl context;
  117. context.EnsureMinimumThreads(num_threads);
  118. Vector x = Vector::Random(kVectorSize);
  119. Vector y = Vector(kVectorSize);
  120. for (auto _ : state) {
  121. ParallelAssign(&context, num_threads, y, x);
  122. }
  123. CHECK_EQ((y - x).squaredNorm(), 0.);
  124. }
  125. BENCHMARK(AssignParallel)->VECTOR_SIZE_THREADS;
  126. static void D2X(benchmark::State& state) {
  127. const int kVectorSize = static_cast<int>(state.range(0));
  128. const Vector x = Vector::Random(kVectorSize);
  129. const Vector D = Vector::Random(kVectorSize);
  130. Vector y = Vector::Zero(kVectorSize);
  131. for (auto _ : state) {
  132. y = D.array().square() * x.array();
  133. }
  134. CHECK_GT(y.squaredNorm(), 0.);
  135. }
  136. BENCHMARK(D2X)->VECTOR_SIZES(1);
  137. static void D2XParallel(benchmark::State& state) {
  138. const int kVectorSize = static_cast<int>(state.range(0));
  139. const int num_threads = static_cast<int>(state.range(1));
  140. ContextImpl context;
  141. context.EnsureMinimumThreads(num_threads);
  142. const Vector x = Vector::Random(kVectorSize);
  143. const Vector D = Vector::Random(kVectorSize);
  144. Vector y = Vector(kVectorSize);
  145. for (auto _ : state) {
  146. ParallelAssign(&context, num_threads, y, D.array().square() * x.array());
  147. }
  148. CHECK_GT(y.squaredNorm(), 0.);
  149. }
  150. BENCHMARK(D2XParallel)->VECTOR_SIZE_THREADS;
  151. static void DivideSqrt(benchmark::State& state) {
  152. const int kVectorSize = static_cast<int>(state.range(0));
  153. Vector diagonal = Vector::Random(kVectorSize).array().abs();
  154. const double radius = 0.5;
  155. for (auto _ : state) {
  156. diagonal = (diagonal / radius).array().sqrt();
  157. }
  158. CHECK_GT(diagonal.squaredNorm(), 0.);
  159. }
  160. BENCHMARK(DivideSqrt)->VECTOR_SIZES(1);
  161. static void DivideSqrtParallel(benchmark::State& state) {
  162. const int kVectorSize = static_cast<int>(state.range(0));
  163. const int num_threads = static_cast<int>(state.range(1));
  164. ContextImpl context;
  165. context.EnsureMinimumThreads(num_threads);
  166. Vector diagonal = Vector::Random(kVectorSize).array().abs();
  167. const double radius = 0.5;
  168. for (auto _ : state) {
  169. ParallelAssign(
  170. &context, num_threads, diagonal, (diagonal / radius).cwiseSqrt());
  171. }
  172. CHECK_GT(diagonal.squaredNorm(), 0.);
  173. }
  174. BENCHMARK(DivideSqrtParallel)->VECTOR_SIZE_THREADS;
  175. static void Clamp(benchmark::State& state) {
  176. const int kVectorSize = static_cast<int>(state.range(0));
  177. Vector diagonal = Vector::Random(kVectorSize);
  178. const double min = -0.5;
  179. const double max = 0.5;
  180. for (auto _ : state) {
  181. for (int i = 0; i < kVectorSize; ++i) {
  182. diagonal[i] = std::min(std::max(diagonal[i], min), max);
  183. }
  184. }
  185. CHECK_LE(diagonal.maxCoeff(), 0.5);
  186. CHECK_GE(diagonal.minCoeff(), -0.5);
  187. }
  188. BENCHMARK(Clamp)->VECTOR_SIZES(1);
  189. static void ClampParallel(benchmark::State& state) {
  190. const int kVectorSize = static_cast<int>(state.range(0));
  191. const int num_threads = static_cast<int>(state.range(1));
  192. ContextImpl context;
  193. context.EnsureMinimumThreads(num_threads);
  194. Vector diagonal = Vector::Random(kVectorSize);
  195. const double min = -0.5;
  196. const double max = 0.5;
  197. for (auto _ : state) {
  198. ParallelAssign(
  199. &context, num_threads, diagonal, diagonal.array().max(min).min(max));
  200. }
  201. CHECK_LE(diagonal.maxCoeff(), 0.5);
  202. CHECK_GE(diagonal.minCoeff(), -0.5);
  203. }
  204. BENCHMARK(ClampParallel)->VECTOR_SIZE_THREADS;
  205. static void Norm(benchmark::State& state) {
  206. const int kVectorSize = static_cast<int>(state.range(0));
  207. const Vector x = Vector::Random(kVectorSize);
  208. double total = 0.;
  209. for (auto _ : state) {
  210. total += x.norm();
  211. }
  212. CHECK_GT(total, 0.);
  213. }
  214. BENCHMARK(Norm)->VECTOR_SIZES(1);
  215. static void NormParallel(benchmark::State& state) {
  216. const int kVectorSize = static_cast<int>(state.range(0));
  217. const int num_threads = static_cast<int>(state.range(1));
  218. ContextImpl context;
  219. context.EnsureMinimumThreads(num_threads);
  220. const Vector x = Vector::Random(kVectorSize);
  221. double total = 0.;
  222. for (auto _ : state) {
  223. total += Norm(x, &context, num_threads);
  224. }
  225. CHECK_GT(total, 0.);
  226. }
  227. BENCHMARK(NormParallel)->VECTOR_SIZE_THREADS;
  228. static void Dot(benchmark::State& state) {
  229. const int kVectorSize = static_cast<int>(state.range(0));
  230. const Vector x = Vector::Random(kVectorSize);
  231. const Vector y = Vector::Random(kVectorSize);
  232. double total = 0.;
  233. for (auto _ : state) {
  234. total += x.dot(y);
  235. }
  236. CHECK_NE(total, 0.);
  237. }
  238. BENCHMARK(Dot)->VECTOR_SIZES(1);
  239. static void DotParallel(benchmark::State& state) {
  240. const int kVectorSize = static_cast<int>(state.range(0));
  241. const int num_threads = static_cast<int>(state.range(1));
  242. ContextImpl context;
  243. context.EnsureMinimumThreads(num_threads);
  244. const Vector x = Vector::Random(kVectorSize);
  245. const Vector y = Vector::Random(kVectorSize);
  246. double total = 0.;
  247. for (auto _ : state) {
  248. total += Dot(x, y, &context, num_threads);
  249. }
  250. CHECK_NE(total, 0.);
  251. }
  252. BENCHMARK(DotParallel)->VECTOR_SIZE_THREADS;
  253. static void Axpby(benchmark::State& state) {
  254. const int kVectorSize = static_cast<int>(state.range(0));
  255. const Vector x = Vector::Random(kVectorSize);
  256. const Vector y = Vector::Random(kVectorSize);
  257. Vector z = Vector::Zero(kVectorSize);
  258. const double a = 3.1415;
  259. const double b = 1.2345;
  260. for (auto _ : state) {
  261. z = a * x + b * y;
  262. }
  263. CHECK_GT(z.squaredNorm(), 0.);
  264. }
  265. BENCHMARK(Axpby)->VECTOR_SIZES(1);
  266. static void AxpbyParallel(benchmark::State& state) {
  267. const int kVectorSize = static_cast<int>(state.range(0));
  268. const int num_threads = static_cast<int>(state.range(1));
  269. ContextImpl context;
  270. context.EnsureMinimumThreads(num_threads);
  271. const Vector x = Vector::Random(kVectorSize);
  272. const Vector y = Vector::Random(kVectorSize);
  273. Vector z = Vector::Zero(kVectorSize);
  274. const double a = 3.1415;
  275. const double b = 1.2345;
  276. for (auto _ : state) {
  277. Axpby(a, x, b, y, z, &context, num_threads);
  278. }
  279. CHECK_GT(z.squaredNorm(), 0.);
  280. }
  281. BENCHMARK(AxpbyParallel)->VECTOR_SIZE_THREADS;
  282. } // namespace ceres::internal
  283. BENCHMARK_MAIN();