bench-gemv.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/bench - BLAS-2 type ops.
  3. // (c) Daniel Llorens - 2017
  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. // These operations aren't really part of the ET framework, just standalone functions.
  9. // Cf bench-gemm.cc for BLAS-3 type ops.
  10. #include <iostream>
  11. #include <iomanip>
  12. #include "ra/bench.hh"
  13. using std::cout, std::endl, std::setw, std::setprecision, ra::TestRecorder;
  14. using ra::Small, ra::View, ra::Unique;
  15. using real = double;
  16. // -------------------
  17. // variants of the defaults, should be slower if the default is well picked.
  18. // TODO compare with external GEMV/GEVM
  19. // -------------------
  20. enum trans_t { NOTRANS, TRANS };
  21. int main()
  22. {
  23. TestRecorder tr(std::cout);
  24. auto gemv_i = [&](auto const & a, auto const & b)
  25. {
  26. int const M = a.len(0);
  27. ra::Big<decltype(a(0, 0)*b(0)), 1> c({M}, ra::none);
  28. for (int i=0; i<M; ++i) {
  29. c(i) = dot(a(i), b);
  30. }
  31. return c;
  32. };
  33. auto gemv_j = [&](auto const & a, auto const & b)
  34. {
  35. int const M = a.len(0);
  36. int const N = a.len(1);
  37. ra::Big<decltype(a(0, 0)*b(0)), 1> c({M}, 0.);
  38. for (int j=0; j<N; ++j) {
  39. c += a(ra::all, j)*b(j);
  40. }
  41. return c;
  42. };
  43. auto gevm_j = [&](auto const & b, auto const & a)
  44. {
  45. int const N = a.len(1);
  46. ra::Big<decltype(b(0)*a(0, 0)), 1> c({N}, ra::none);
  47. for (int j=0; j<N; ++j) {
  48. c(j) = dot(b, a(ra::all, j));
  49. }
  50. return c;
  51. };
  52. auto gevm_i = [&](auto const & b, auto const & a)
  53. {
  54. int const M = a.len(0);
  55. int const N = a.len(1);
  56. ra::Big<decltype(b(0)*a(0, 0)), 1> c({N}, 0.);
  57. for (int i=0; i<M; ++i) {
  58. c += b(i)*a(i);
  59. }
  60. return c;
  61. };
  62. auto bench_all = [&](int k, int m, int n, int reps)
  63. {
  64. auto bench_mv = [&tr, &m, &n, &reps](auto && f, char const * tag, trans_t t)
  65. {
  66. ra::Big<real, 2> aa({m, n}, ra::_0-ra::_1);
  67. auto a = t==TRANS ? transpose<1, 0>(aa) : aa();
  68. ra::Big<real, 1> b({a.len(1)}, 1-2*ra::_0);
  69. ra::Big<real, 1> ref = gemv(a, b);
  70. ra::Big<real, 1> c;
  71. auto bv = Benchmark().repeats(reps).runs(3).run([&]() { c = f(a, b); });
  72. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m*n)/1e-9, " ns [",
  73. Benchmark::stddev(bv)/(m*n)/1e-9 ,"] ", tag, t==TRANS ? " [T]" : " [N]").test_eq(ref, c);
  74. };
  75. auto bench_vm = [&tr, &m, &n, &reps](auto && f, char const * tag, trans_t t)
  76. {
  77. ra::Big<real, 2> aa({m, n}, ra::_0-ra::_1);
  78. auto a = t==TRANS ? transpose<1, 0>(aa) : aa();
  79. ra::Big<real, 1> b({a.len(0)}, 1-2*ra::_0);
  80. ra::Big<real, 1> ref = gevm(b, a);
  81. ra::Big<real, 1> c;
  82. auto bv = Benchmark().repeats(reps).runs(4).run([&]() { c = f(b, a); });
  83. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m*n)/1e-9, " ns [",
  84. Benchmark::stddev(bv)/(m*n)/1e-9 ,"] ", tag, t==TRANS ? " [T]" : " [N]").test_eq(ref, c);
  85. };
  86. tr.section(m, " x ", n, " times ", reps);
  87. // some variants are way too slow to check with larger arrays.
  88. if (k>0) {
  89. bench_mv(gemv_i, "mv i", NOTRANS);
  90. bench_mv(gemv_i, "mv i", TRANS);
  91. bench_mv(gemv_j, "mv j", NOTRANS);
  92. bench_mv(gemv_j, "mv j", TRANS);
  93. bench_mv([&](auto const & a, auto const & b) { return gemv(a, b); }, "mv default", NOTRANS);
  94. bench_mv([&](auto const & a, auto const & b) { return gemv(a, b); }, "mv default", TRANS);
  95. bench_vm(gevm_i, "vm i", NOTRANS);
  96. bench_vm(gevm_i, "vm i", TRANS);
  97. bench_vm(gevm_j, "vm j", NOTRANS);
  98. bench_vm(gevm_j, "vm j", TRANS);
  99. bench_vm([&](auto const & a, auto const & b) { return gevm(a, b); }, "vm default", NOTRANS);
  100. bench_vm([&](auto const & a, auto const & b) { return gevm(a, b); }, "vm default", TRANS);
  101. }
  102. };
  103. bench_all(3, 10, 10, 1000);
  104. bench_all(3, 100, 100, 10);
  105. bench_all(3, 500, 500, 1);
  106. bench_all(3, 10000, 1000, 1);
  107. bench_all(3, 1000, 10000, 1);
  108. bench_all(3, 100000, 100, 1);
  109. bench_all(3, 100, 100000, 1);
  110. return tr.summary();
  111. }