bench-tensorindex.cc 2.4 KB

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