bench-pack.C 2.7 KB

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