bench-optimize.cc 3.7 KB

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