bench-optimize.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // (c) Daniel Llorens - 2017
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. /// @file bench-optimize.hh
  7. /// @brief Benchmark RA_DO_OPT_SMALLVECTOR (DNW)
  8. // TODO Everything...
  9. #define RA_DO_OPT 0 // disable automatic use, so we can compare with (forced) and without
  10. #define RA_DO_OPT_IOTA 1
  11. #ifdef RA_DO_OPT_SMALLVECTOR // bench requires 1 to be meaningful.
  12. #undef RA_DO_OPT_SMALLVECTOR
  13. #endif
  14. #define RA_DO_OPT_SMALLVECTOR 1
  15. #include "ra/operators.hh"
  16. #include "ra/test.hh"
  17. #include "ra/bench.hh"
  18. #include "ra/mpdebug.hh"
  19. #include <iostream>
  20. #include <iomanip>
  21. using std::cout, std::endl, std::setw, std::setprecision, ra::TestRecorder;
  22. using ra::Small, ra::View, ra::Unique, ra::ra_traits;
  23. using Vec = ra::Small<float, 4>;
  24. int main()
  25. {
  26. TestRecorder tr(std::cout);
  27. tr.section("small vector ops through vector extensions, other types / sizes");
  28. {
  29. ra::Small<double, 4> a = 1 + ra::_0;
  30. ra::Small<double, 2, 4> b = 33 - ra::_1;
  31. auto c = optimize(a + b(1));
  32. tr.info("optimization of view").test(std::is_same_v<decltype(c), ra::Small<double, 4>>);
  33. tr.test_eq(34, c);
  34. }
  35. auto bench_type =
  36. [&](auto v)
  37. {
  38. using Vec = decltype(v);
  39. auto sum_opt =
  40. [&](auto & a, auto & b, auto & c)
  41. {
  42. for (int i=0; i<a.size(0); ++i) {
  43. c(i) = ra::optimize(a(i)+b(i));
  44. static_assert(std::is_same_v<decltype(optimize(a(i)+b(i))), Vec>); // making sure opt is on
  45. }
  46. };
  47. auto sum_unopt =
  48. [&](auto & a, auto & b, auto & c)
  49. {
  50. for (int i=0; i<a.size(0); ++i) {
  51. c(i) = a(i)+b(i);
  52. }
  53. };
  54. auto bench_all =
  55. [&](int reps, int m)
  56. {
  57. auto bench =
  58. [&tr, &m, &reps](auto && f, char const * tag)
  59. {
  60. // FIXME need alignment knobs for Big
  61. alignas (alignof(Vec)) Vec astore[m];
  62. alignas (alignof(Vec)) Vec bstore[m];
  63. alignas (alignof(Vec)) Vec cstore[m];
  64. ra::View<Vec *, 1> a({m}, astore); // FIXME [ra28]
  65. ra::View<Vec *, 1> b({m}, bstore); // FIXME [ra28]
  66. ra::View<Vec *, 1> c({m}, cstore); // FIXME [ra28]
  67. a = +ra::_0 +1;
  68. b = -ra::_0 -1;
  69. c = 99;
  70. auto bv = Benchmark().repeats(reps).runs(3).run([&]() { f(a, b, c); });
  71. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m)/1e-9, " ns [",
  72. Benchmark::stddev(bv)/(m)/1e-9 ,"] ", tag).test(true);
  73. };
  74. tr.section("[", (std::is_same_v<float, typename Vec::value_type> ? "float" : "double"),
  75. " x ", Vec::size(), "] block of ", m, " times ", reps);
  76. bench(sum_opt, "opt");
  77. bench(sum_unopt, "unopt");
  78. };
  79. bench_all(50000, 10);
  80. bench_all(50000, 100);
  81. bench_all(50000, 1000);
  82. };
  83. bench_type(ra::Small<float, 2> {});
  84. bench_type(ra::Small<double, 2> {});
  85. bench_type(ra::Small<float, 4> {});
  86. bench_type(ra::Small<double, 4> {});
  87. bench_type(ra::Small<float, 8> {});
  88. bench_type(ra::Small<double, 8> {});
  89. return tr.summary();
  90. }