bench-pack.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // (c) Daniel Llorens - 2016-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-pack.cc
  7. /// @brief Benchmark pack() operator vs explode/collapse.
  8. #include <iostream>
  9. #include <iomanip>
  10. #include "ra/test.hh"
  11. #include "ra/complex.hh"
  12. #include "ra/ra.hh"
  13. #include "ra/bench.hh"
  14. using std::cout, std::endl, std::flush, ra::TestRecorder;
  15. using real = double;
  16. using complex = std::complex<double>;
  17. int main()
  18. {
  19. TestRecorder tr(cout);
  20. cout.precision(4);
  21. auto bench = [&tr](auto && f, auto A_, char const * tag, int size, int reps)
  22. {
  23. using A = decltype(A_);
  24. A a({size}, ra::none);
  25. Benchmark bm { reps, 3 };
  26. auto bv = bm.run([&]() { f(a, size); });
  27. tr.info(std::setw(5), std::fixed, bm.avg(bv)/size/1e-9, " ns [", bm.stddev(bv)/size/1e-9 ,"] ", tag)
  28. .test_eq(ra::pack<complex>(ra::iota<real>(size), size-ra::iota<real>(size)), a);
  29. };
  30. auto f_raw = [](auto & a, int size)
  31. {
  32. real * p = reinterpret_cast<real *>(a.data());
  33. for (ra::dim_t i=0; i!=size; ++i, p+=2) {
  34. p[0] = i;
  35. p[1] = size-i;
  36. }
  37. };
  38. auto f_reim = [](auto & a, int size)
  39. {
  40. real_part(a) = ra::iota(size);
  41. imag_part(a) = size-ra::iota(size);
  42. };
  43. auto f_collapse = [](auto & a, int size)
  44. {
  45. auto areim = ra::collapse<real>(a);
  46. areim(ra::all, 0) = ra::iota(size);
  47. areim(ra::all, 1) = size-ra::iota(size);
  48. };
  49. auto f_pack = [](auto & a, int size)
  50. {
  51. a = ra::pack<complex>(ra::iota<real>(size), size-ra::iota<real>(size));
  52. };
  53. auto f_xI = [](auto & a, int size)
  54. {
  55. a = ra::iota<real>(size) + xI(size-ra::iota<real>(size));
  56. };
  57. auto bench_all = [&](auto A_, int size, int n)
  58. {
  59. tr.section("size ", size, ", n ", n);
  60. bench(f_raw, A_, "raw", size, n);
  61. bench(f_reim, A_, "re/im", size, n);
  62. bench(f_collapse, A_, "collapse", size, n);
  63. bench(f_pack, A_, "pack", size, n);
  64. bench(f_xI, A_, "xI", size, n);
  65. };
  66. bench_all(ra::Big<complex, 1>(), 10, 1000000);
  67. bench_all(ra::Big<complex, 1>(), 100, 100000);
  68. bench_all(ra::Big<complex, 1>(), 1000, 10000);
  69. bench_all(ra::Big<complex, 1>(), 10000, 1000);
  70. bench_all(ra::Big<complex, 1>(), 100000, 100);
  71. bench_all(ra::Big<complex, 1>(), 1000000, 10);
  72. return tr.summary();
  73. }