bench-gemm.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/bench - BLAS-3 type ops.
  3. // (c) Daniel Llorens - 2016-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-gemv.cc for BLAS-2 type ops.
  10. // FIXME Benchmark w/o allocation.
  11. #include <iostream>
  12. #include <iomanip>
  13. #include "ra/bench.hh"
  14. using std::cout, std::endl, std::setw, std::setprecision, ra::TestRecorder;
  15. using ra::Small, ra::View, ra::Unique, ra::dim_t;
  16. using real = double;
  17. // -------------------
  18. // variants of the defaults, should be slower if the default is well picked.
  19. // -------------------
  20. template <class A, class B, class C> inline void
  21. gemm_block_3(ra::View<A, 2> const & a, ra::View<B, 2> const & b, ra::View<C, 2> c)
  22. {
  23. dim_t const m = a.len(0);
  24. dim_t const p = a.len(1);
  25. dim_t const n = b.len(1);
  26. // terminal, using reduce_k, see below
  27. if (max(m, max(p, n))<=64) {
  28. for_each(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto && c, auto && a, auto && b) { c += a*b; })),
  29. c, a, b);
  30. // split a's rows
  31. } else if (m>=max(p, n)) {
  32. gemm_block_3(a(ra::iota(m/2)), b, c(ra::iota(m/2)));
  33. gemm_block_3(a(ra::iota(m-m/2, m/2)), b, c(ra::iota(m-m/2, m/2)));
  34. // split b's columns
  35. } else if (n>=max(m, p)) {
  36. gemm_block_3(a, b(ra::all, ra::iota(n/2)), c(ra::all, ra::iota(n/2)));
  37. gemm_block_3(a, b(ra::all, ra::iota(n-n/2, n/2)), c(ra::all, ra::iota(n-n/2, n/2)));
  38. // split a's columns and b's rows
  39. } else {
  40. gemm_block_3(a(ra::all, ra::iota(p/2)), b(ra::iota(p/2)), c);
  41. gemm_block_3(a(ra::all, ra::iota(p-p/2, p/2)), b(ra::iota(p-p/2, p/2)), c);
  42. }
  43. }
  44. #if RA_USE_BLAS==1
  45. extern "C" {
  46. #include <cblas.h>
  47. }
  48. constexpr CBLAS_TRANSPOSE
  49. fliptr(CBLAS_TRANSPOSE t)
  50. {
  51. if (t==CblasTrans) {
  52. return CblasNoTrans;
  53. } else if (t==CblasNoTrans) {
  54. return CblasTrans;
  55. } else {
  56. assert(0 && "BLAS doesn't support this transpose");
  57. abort();
  58. }
  59. }
  60. constexpr bool
  61. istr(CBLAS_TRANSPOSE t)
  62. {
  63. return (t==CblasTrans) || (t==CblasConjTrans);
  64. }
  65. template <class A> inline void
  66. lead_and_order(A const & a, int & ld, CBLAS_ORDER & order)
  67. {
  68. if (a.step(1)==1) {
  69. order = CblasRowMajor;
  70. ld = a.step(0);
  71. } else if (a.step(0)==1) {
  72. order = CblasColMajor;
  73. ld = a.step(1);
  74. } else {
  75. order = CblasRowMajor;
  76. ld = 0;
  77. assert(0 && "not a BLAS-supported array");
  78. }
  79. }
  80. inline void
  81. gemm_blas_3(ra::View<double, 2> const & A, ra::View<double, 2> const & B, ra::View<double, 2> C)
  82. {
  83. CBLAS_TRANSPOSE ta = CblasNoTrans;
  84. CBLAS_TRANSPOSE tb = CblasNoTrans;
  85. int ldc, lda, ldb;
  86. CBLAS_ORDER orderc, ordera, orderb;
  87. lead_and_order(C, ldc, orderc);
  88. lead_and_order(A, lda, ordera);
  89. lead_and_order(B, ldb, orderb);
  90. int K = A.len(1-istr(ta));
  91. assert(K==B.len(istr(tb)) && "mismatched A/B");
  92. assert(C.len(0)==A.len(istr(ta)) && "mismatched C/A");
  93. assert(C.len(1)==B.len(1-istr(tb)) && "mismatched C/B");
  94. if (ordera!=orderc) {
  95. ta = fliptr(ta);
  96. }
  97. if (orderb!=orderc) {
  98. tb = fliptr(tb);
  99. }
  100. if (C.size()>0) {
  101. cblas_dgemm(orderc, ta, tb, C.len(0), C.len(1), K, 1., A.data(), lda, B.data(), ldb, 0, C.data(), ldc);
  102. }
  103. }
  104. inline auto
  105. gemm_blas(ra::View<double, 2> const & a, ra::View<double, 2> const & b)
  106. {
  107. ra::Big<decltype(a(0, 0)*b(0, 0)), 2> c({a.len(0), b.len(1)}, 0);
  108. gemm_blas_3(a, b, c);
  109. return c;
  110. }
  111. #endif // RA_USE_BLAS
  112. int main()
  113. {
  114. TestRecorder tr(std::cout);
  115. auto gemm_block = [&](auto const & a, auto const & b)
  116. {
  117. ra::Big<decltype(a(0, 0)*b(0, 0)), 2> c({a.len(0), b.len(1)}, 0);
  118. gemm_block_3(a, b, c);
  119. return c;
  120. };
  121. auto gemm_k = [&](auto const & a, auto const & b)
  122. {
  123. dim_t const M = a.len(0);
  124. dim_t const N = b.len(1);
  125. ra::Big<decltype(a(0, 0)*b(0, 0)), 2> c({M, N}, ra::none);
  126. for (dim_t i=0; i<M; ++i) {
  127. for (dim_t j=0; j<N; ++j) {
  128. c(i, j) = dot(a(i), b(ra::all, j));
  129. }
  130. }
  131. return c;
  132. };
  133. // See test/wrank.cc "outer product variants" for the logic.
  134. // TODO based on this, allow a Blitz++ like notation C(i, j) = sum(A(i, k)*B(k, j), k).
  135. auto gemm_reduce_k = [&](auto const & a, auto const & b)
  136. {
  137. dim_t const M = a.len(0);
  138. dim_t const N = b.len(1);
  139. using T = decltype(a(0, 0)*b(0, 0));
  140. ra::Big<T, 2> c({M, N}, T());
  141. for_each(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto && c, auto && a, auto && b) { c += a*b; })),
  142. c, a, b);
  143. return c;
  144. };
  145. #define DEFINE_GEMM_RESTRICT(NAME_K, NAME_IJ, RESTRICT) \
  146. auto NAME_K = [&](auto const & a, auto const & b) \
  147. { \
  148. dim_t const M = a.len(0); \
  149. dim_t const N = b.len(1); \
  150. dim_t const K = a.len(1); \
  151. using T = decltype(a(0, 0)*b(0, 0)); \
  152. ra::Big<T, 2> c({M, N}, T()); \
  153. T * RESTRICT cc = c.data(); \
  154. T const * RESTRICT aa = a.data(); \
  155. T const * RESTRICT bb = b.data(); \
  156. for (dim_t i=0; i<M; ++i) { \
  157. for (dim_t j=0; j<N; ++j) { \
  158. for (dim_t k=0; k<K; ++k) { \
  159. cc[i*N+j] += aa[i*K+k] * bb[k*N+j]; \
  160. } \
  161. } \
  162. } \
  163. return c; \
  164. }; \
  165. \
  166. auto NAME_IJ = [&](auto const & a, auto const & b) \
  167. { \
  168. dim_t const M = a.len(0); \
  169. dim_t const N = b.len(1); \
  170. dim_t const K = a.len(1); \
  171. using T = decltype(a(0, 0)*b(0, 0)); \
  172. ra::Big<T, 2> c({M, N}, T()); \
  173. T * RESTRICT cc = c.data(); \
  174. T const * RESTRICT aa = a.data(); \
  175. T const * RESTRICT bb = b.data(); \
  176. for (dim_t k=0; k<K; ++k) { \
  177. for (dim_t i=0; i<M; ++i) { \
  178. for (dim_t j=0; j<N; ++j) { \
  179. cc[i*N+j] += aa[i*K+k] * bb[k*N+j]; \
  180. } \
  181. } \
  182. } \
  183. return c; \
  184. };
  185. DEFINE_GEMM_RESTRICT(gemm_k_raw, gemm_ij_raw, /* */)
  186. DEFINE_GEMM_RESTRICT(gemm_k_raw_restrict, gemm_ij_raw_restrict, __restrict__)
  187. #undef DEFINE_GEMM_RESTRICT
  188. auto bench_all = [&](int k, int m, int p, int n, int reps)
  189. {
  190. auto bench = [&](auto && f, char const * tag)
  191. {
  192. ra::Big<real, 2> a({m, p}, ra::_0-ra::_1);
  193. ra::Big<real, 2> b({p, n}, ra::_1-2*ra::_0);
  194. ra::Big<real, 2> ref = gemm(a, b);
  195. ra::Big<real, 2> c;
  196. auto bv = Benchmark().repeats(reps).runs(3).run([&]() { c = f(a, b); });
  197. tr.info(std::setw(5), std::fixed, Benchmark::avg(bv)/(m*n*p)/1e-9, " ns [",
  198. Benchmark::stddev(bv)/(m*n*p)/1e-9 ,"] ", tag).test_eq(ref, c);
  199. };
  200. tr.section(m, " (", p, ") ", n, " times ", reps);
  201. // some variants are way too slow to check with larger arrays.
  202. if (k>2) {
  203. bench(gemm_k, "k");
  204. }
  205. if (k>1) {
  206. bench(gemm_k_raw, "k_raw");
  207. bench(gemm_k_raw_restrict, "k_raw_restrict");
  208. }
  209. if (k>0) {
  210. bench(gemm_reduce_k, "reduce_k");
  211. bench(gemm_ij_raw, "ij_raw");
  212. bench(gemm_ij_raw_restrict, "ij_raw_restrict");
  213. }
  214. bench(gemm_block, "block");
  215. #if RA_USE_BLAS==1
  216. bench(gemm_blas, "blas");
  217. #endif
  218. bench([&](auto const & a, auto const & b) { return gemm(a, b); }, "default");
  219. };
  220. bench_all(3, 10, 10, 10, 1000);
  221. bench_all(2, 100, 100, 100, 10);
  222. bench_all(2, 500, 400, 500, 1);
  223. bench_all(1, 10000, 10, 1000, 1);
  224. bench_all(1, 1000, 10, 10000, 1);
  225. bench_all(1, 100000, 10, 100, 1);
  226. bench_all(1, 100, 10, 100000, 1);
  227. return tr.summary();
  228. }