bench-tensorindex.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/bench - TensorIndex.
  3. // (c) Daniel Llorens - 2019-2020
  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. #include <iostream>
  9. #include "ra/test.hh"
  10. #include "ra/bench.hh"
  11. using std::cout, std::endl, std::flush, ra::TestRecorder;
  12. int main()
  13. {
  14. TestRecorder tr;
  15. // rank 1
  16. {
  17. ra::Big<int, 1> a = {0, 0, 0};
  18. ra::ply(map([](auto && i) { std::cout << "i: " << i << std::endl; },
  19. a+ra::TensorIndex<0> {}));
  20. ra::ply_ravel(map([](auto && i) { std::cout << "i: " << i << std::endl; },
  21. a+ra::TensorIndex<0> {}));
  22. }
  23. // rank 2
  24. {
  25. ra::Big<int, 2> a = {{0, 0, 0}, {0, 0, 0}};
  26. ra::ply(map([](auto && i, auto && j) { std::cout << "i: " << i << ", " << j << std::endl; },
  27. a+ra::TensorIndex<0> {}, a+ra::TensorIndex<1> {}));
  28. ra::ply_ravel(map([](auto && i, auto && j) { std::cout << "i: " << i << ", " << j << std::endl; },
  29. a+ra::TensorIndex<0> {}, a+ra::TensorIndex<1> {}));
  30. }
  31. // benchmark
  32. auto taking_view =
  33. [](TestRecorder & tr, auto && a)
  34. {
  35. auto fa = [&a]()
  36. {
  37. int c = 0;
  38. ra::ply(ra::map([&c](auto && i, auto && j) { c += 2*i-j; },
  39. a+ra::TensorIndex<0> {}, a+ra::TensorIndex<1> {}));
  40. return c;
  41. };
  42. auto fb = [&a]()
  43. {
  44. int c = 0;
  45. ra::ply_ravel(ra::map([&c](auto && i, auto && j) { c += 2*i-j; },
  46. a+ra::TensorIndex<0> {}, a+ra::TensorIndex<1> {}));
  47. return c;
  48. };
  49. tr.test_eq(499500000, fa());
  50. tr.test_eq(499500000, fb());
  51. auto bench = Benchmark {/* repeats */ 30, /* runs */ 30};
  52. bench.info("vala").report(std::cout, bench.run(fa), 1e-6);
  53. bench.info("valb").report(std::cout, bench.run(fb), 1e-6);
  54. };
  55. ra::Big<int, 2> const a({1000, 1000}, 0);
  56. taking_view(tr, a);
  57. auto b = transpose<1, 0>(a);
  58. taking_view(tr, b);
  59. return tr.summary();
  60. }