bench-gemv.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::ViewBig, 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. cout << "RA_DO_FMA is " << RA_DO_FMA << endl;
  25. auto gemv_i = [&](auto const & a, auto const & b)
  26. {
  27. int const M = a.len(0);
  28. ra::Big<decltype(a(0, 0)*b(0)), 1> c({M}, ra::none);
  29. for (int i=0; i<M; ++i) {
  30. c(i) = dot(a(i), b);
  31. }
  32. return c;
  33. };
  34. auto gemv_j = [&](auto const & a, auto const & b)
  35. {
  36. int const M = a.len(0);
  37. int const N = a.len(1);
  38. ra::Big<decltype(a(0, 0)*b(0)), 1> c({M}, 0.);
  39. for (int j=0; j<N; ++j) {
  40. c += a(ra::all, j)*b(j);
  41. }
  42. return c;
  43. };
  44. auto gevm_j = [&](auto const & b, auto const & a)
  45. {
  46. int const N = a.len(1);
  47. ra::Big<decltype(b(0)*a(0, 0)), 1> c({N}, ra::none);
  48. for (int j=0; j<N; ++j) {
  49. c(j) = dot(b, a(ra::all, j));
  50. }
  51. return c;
  52. };
  53. auto gevm_i = [&](auto const & b, auto const & a)
  54. {
  55. int const M = a.len(0);
  56. int const N = a.len(1);
  57. ra::Big<decltype(b(0)*a(0, 0)), 1> c({N}, 0.);
  58. for (int i=0; i<M; ++i) {
  59. c += b(i)*a(i);
  60. }
  61. return c;
  62. };
  63. auto bench_all = [&](int k, int m, int n, int reps)
  64. {
  65. auto bench_mv = [&tr, &m, &n, &reps](auto && f, char const * tag, trans_t t)
  66. {
  67. ra::Big<real, 2> aa({m, n}, ra::_0-ra::_1);
  68. auto a = t==TRANS ? transpose<1, 0>(aa) : aa();
  69. ra::Big<real, 1> b({a.len(1)}, 1-2*ra::_0);
  70. ra::Big<real, 1> ref = gemv(a, b);
  71. ra::Big<real, 1> c;
  72. auto bv = Benchmark().repeats(reps).runs(3).run([&]() { c = f(a, b); });
  73. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m*n)/1e-9, " ns [",
  74. Benchmark::stddev(bv)/(m*n)/1e-9 ,"] ", tag, t==TRANS ? " [T]" : " [N]").test_eq(ref, c);
  75. };
  76. auto bench_vm = [&tr, &m, &n, &reps](auto && f, char const * tag, trans_t t)
  77. {
  78. ra::Big<real, 2> aa({m, n}, ra::_0-ra::_1);
  79. auto a = t==TRANS ? transpose<1, 0>(aa) : aa();
  80. ra::Big<real, 1> b({a.len(0)}, 1-2*ra::_0);
  81. ra::Big<real, 1> ref = gevm(b, a);
  82. ra::Big<real, 1> c;
  83. auto bv = Benchmark().repeats(reps).runs(4).run([&]() { c = f(b, a); });
  84. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m*n)/1e-9, " ns [",
  85. Benchmark::stddev(bv)/(m*n)/1e-9 ,"] ", tag, t==TRANS ? " [T]" : " [N]").test_eq(ref, c);
  86. };
  87. tr.section(m, " x ", n, " times ", reps);
  88. // some variants are way too slow to check with larger arrays.
  89. if (k>0) {
  90. bench_mv(gemv_i, "mv i", NOTRANS);
  91. bench_mv(gemv_i, "mv i", TRANS);
  92. bench_mv(gemv_j, "mv j", NOTRANS);
  93. bench_mv(gemv_j, "mv j", TRANS);
  94. bench_mv([&](auto const & a, auto const & b) { return gemv(a, b); }, "mv default", NOTRANS);
  95. bench_mv([&](auto const & a, auto const & b) { return gemv(a, b); }, "mv default", TRANS);
  96. bench_vm(gevm_i, "vm i", NOTRANS);
  97. bench_vm(gevm_i, "vm i", TRANS);
  98. bench_vm(gevm_j, "vm j", NOTRANS);
  99. bench_vm(gevm_j, "vm j", TRANS);
  100. bench_vm([&](auto const & a, auto const & b) { return gevm(a, b); }, "vm default", NOTRANS);
  101. bench_vm([&](auto const & a, auto const & b) { return gevm(a, b); }, "vm default", TRANS);
  102. }
  103. };
  104. bench_all(3, 10, 10, 1000);
  105. bench_all(3, 100, 100, 10);
  106. bench_all(3, 500, 500, 1);
  107. bench_all(3, 10000, 1000, 1);
  108. bench_all(3, 1000, 10000, 1);
  109. bench_all(3, 100000, 100, 1);
  110. bench_all(3, 100, 100000, 1);
  111. return tr.summary();
  112. }