bench-sum-cols.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // -*- mode: c++; coding: utf-8 -*-
  2. /// @file bench-sum-cols.cc
  3. /// @brief Benchmark various ways to sum columns.
  4. // (c) Daniel Llorens - 2016-2017
  5. // This library is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU Lesser General Public License as published by the Free
  7. // Software Foundation; either version 3 of the License, or (at your option) any
  8. // later version.
  9. #include <iostream>
  10. #include <iomanip>
  11. #include "ra/ra.hh"
  12. #include "ra/test.hh"
  13. #include "ra/bench.hh"
  14. using std::cout, std::endl, std::flush, ra::TestRecorder;
  15. using real = double;
  16. int main()
  17. {
  18. TestRecorder tr(cout);
  19. cout.precision(4);
  20. auto bench =
  21. [&tr](char const * tag, int m, int n, int reps, auto && f)
  22. {
  23. ra::Big<real, 2> a({m, n}, ra::_0 - ra::_1);
  24. ra::Big<real, 1> ref({m}, 0);
  25. ref += a*reps;
  26. ra::Big<real, 1> c({m}, ra::none);
  27. auto bv = Benchmark().repeats(reps).runs(3)
  28. .once_f([&](auto && repeat) { c = 0.; repeat([&]() { f(c, a); }); });
  29. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m*n)/1e-9, " ns [",
  30. Benchmark::stddev(bv)/(m*n)/1e-9 ,"] ", tag).test_eq(ref, c);
  31. };
  32. auto bench_all =
  33. [&](int m, int n, int reps)
  34. {
  35. tr.section(m, " x ", n, " times ", reps);
  36. bench("raw", m, n, reps,
  37. [](auto & c, auto const & a)
  38. {
  39. real * __restrict__ ap = a.data();
  40. real * __restrict__ cp = c.data();
  41. ra::dim_t const m = a.size(0);
  42. ra::dim_t const n = a.size(1);
  43. for (ra::dim_t i=0; i!=m; ++i) {
  44. for (ra::dim_t j=0; j!=n; ++j) {
  45. cp[i] += ap[i*n+j];
  46. }
  47. }
  48. });
  49. bench("sideways", m, n, reps,
  50. [](auto & c, auto const & a)
  51. {
  52. for (int j=0, jend=a.size(1); j<jend; ++j) {
  53. c += a(ra::all, j);
  54. }
  55. });
  56. bench("accumcols", m, n, reps,
  57. [](auto & c, auto const & a)
  58. {
  59. for_each([](auto & c, auto && a) { c += sum(a); }, c, iter<1>(a));
  60. });
  61. bench("wrank1", m, n, reps,
  62. [](auto & c, auto const & a)
  63. {
  64. for_each(ra::wrank<0, 0>([](auto & c, auto && a) { c += a; }), c, a);
  65. });
  66. bench("framematch", m, n, reps,
  67. [](auto & c, auto const & a)
  68. {
  69. c += a; // bump c after each row, so it cannot be raveled
  70. });
  71. };
  72. bench_all(1, 1000000, 20);
  73. bench_all(10, 100000, 20);
  74. bench_all(100, 10000, 20);
  75. bench_all(1000, 1000, 20);
  76. bench_all(10000, 100, 20);
  77. bench_all(100000, 10, 20);
  78. bench_all(1000000, 1, 20);
  79. bench_all(1, 10000, 2000);
  80. bench_all(10, 1000, 2000);
  81. bench_all(100, 100, 2000);
  82. bench_all(1000, 10, 2000);
  83. bench_all(10000, 1, 2000);
  84. return tr.summary();
  85. }