bench_gemm.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // g++-4.4 bench_gemm.cpp -I .. -O2 -DNDEBUG -lrt -fopenmp && OMP_NUM_THREADS=2 ./a.out
  2. // icpc bench_gemm.cpp -I .. -O3 -DNDEBUG -lrt -openmp && OMP_NUM_THREADS=2 ./a.out
  3. // Compilation options:
  4. //
  5. // -DSCALAR=std::complex<double>
  6. // -DSCALARA=double or -DSCALARB=double
  7. // -DHAVE_BLAS
  8. // -DDECOUPLED
  9. //
  10. #include <iostream>
  11. #include <Eigen/Core>
  12. #include <bench/BenchTimer.h>
  13. using namespace std;
  14. using namespace Eigen;
  15. #ifndef SCALAR
  16. // #define SCALAR std::complex<float>
  17. #define SCALAR float
  18. #endif
  19. #ifndef SCALARA
  20. #define SCALARA SCALAR
  21. #endif
  22. #ifndef SCALARB
  23. #define SCALARB SCALAR
  24. #endif
  25. typedef SCALAR Scalar;
  26. typedef NumTraits<Scalar>::Real RealScalar;
  27. typedef Matrix<SCALARA,Dynamic,Dynamic> A;
  28. typedef Matrix<SCALARB,Dynamic,Dynamic> B;
  29. typedef Matrix<Scalar,Dynamic,Dynamic> C;
  30. typedef Matrix<RealScalar,Dynamic,Dynamic> M;
  31. #ifdef HAVE_BLAS
  32. extern "C" {
  33. #include <Eigen/src/misc/blas.h>
  34. }
  35. static float fone = 1;
  36. static float fzero = 0;
  37. static double done = 1;
  38. static double szero = 0;
  39. static std::complex<float> cfone = 1;
  40. static std::complex<float> cfzero = 0;
  41. static std::complex<double> cdone = 1;
  42. static std::complex<double> cdzero = 0;
  43. static char notrans = 'N';
  44. static char trans = 'T';
  45. static char nonunit = 'N';
  46. static char lower = 'L';
  47. static char right = 'R';
  48. static int intone = 1;
  49. void blas_gemm(const MatrixXf& a, const MatrixXf& b, MatrixXf& c)
  50. {
  51. int M = c.rows(); int N = c.cols(); int K = a.cols();
  52. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  53. sgemm_(&notrans,&notrans,&M,&N,&K,&fone,
  54. const_cast<float*>(a.data()),&lda,
  55. const_cast<float*>(b.data()),&ldb,&fone,
  56. c.data(),&ldc);
  57. }
  58. EIGEN_DONT_INLINE void blas_gemm(const MatrixXd& a, const MatrixXd& b, MatrixXd& c)
  59. {
  60. int M = c.rows(); int N = c.cols(); int K = a.cols();
  61. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  62. dgemm_(&notrans,&notrans,&M,&N,&K,&done,
  63. const_cast<double*>(a.data()),&lda,
  64. const_cast<double*>(b.data()),&ldb,&done,
  65. c.data(),&ldc);
  66. }
  67. void blas_gemm(const MatrixXcf& a, const MatrixXcf& b, MatrixXcf& c)
  68. {
  69. int M = c.rows(); int N = c.cols(); int K = a.cols();
  70. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  71. cgemm_(&notrans,&notrans,&M,&N,&K,(float*)&cfone,
  72. const_cast<float*>((const float*)a.data()),&lda,
  73. const_cast<float*>((const float*)b.data()),&ldb,(float*)&cfone,
  74. (float*)c.data(),&ldc);
  75. }
  76. void blas_gemm(const MatrixXcd& a, const MatrixXcd& b, MatrixXcd& c)
  77. {
  78. int M = c.rows(); int N = c.cols(); int K = a.cols();
  79. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  80. zgemm_(&notrans,&notrans,&M,&N,&K,(double*)&cdone,
  81. const_cast<double*>((const double*)a.data()),&lda,
  82. const_cast<double*>((const double*)b.data()),&ldb,(double*)&cdone,
  83. (double*)c.data(),&ldc);
  84. }
  85. #endif
  86. void matlab_cplx_cplx(const M& ar, const M& ai, const M& br, const M& bi, M& cr, M& ci)
  87. {
  88. cr.noalias() += ar * br;
  89. cr.noalias() -= ai * bi;
  90. ci.noalias() += ar * bi;
  91. ci.noalias() += ai * br;
  92. // [cr ci] += [ar ai] * br + [-ai ar] * bi
  93. }
  94. void matlab_real_cplx(const M& a, const M& br, const M& bi, M& cr, M& ci)
  95. {
  96. cr.noalias() += a * br;
  97. ci.noalias() += a * bi;
  98. }
  99. void matlab_cplx_real(const M& ar, const M& ai, const M& b, M& cr, M& ci)
  100. {
  101. cr.noalias() += ar * b;
  102. ci.noalias() += ai * b;
  103. }
  104. template<typename A, typename B, typename C>
  105. EIGEN_DONT_INLINE void gemm(const A& a, const B& b, C& c)
  106. {
  107. c.noalias() += a * b;
  108. }
  109. int main(int argc, char ** argv)
  110. {
  111. std::ptrdiff_t l1 = internal::queryL1CacheSize();
  112. std::ptrdiff_t l2 = internal::queryTopLevelCacheSize();
  113. std::cout << "L1 cache size = " << (l1>0 ? l1/1024 : -1) << " KB\n";
  114. std::cout << "L2/L3 cache size = " << (l2>0 ? l2/1024 : -1) << " KB\n";
  115. typedef internal::gebp_traits<Scalar,Scalar> Traits;
  116. std::cout << "Register blocking = " << Traits::mr << " x " << Traits::nr << "\n";
  117. int rep = 1; // number of repetitions per try
  118. int tries = 2; // number of tries, we keep the best
  119. int s = 2048;
  120. int m = s;
  121. int n = s;
  122. int p = s;
  123. int cache_size1=-1, cache_size2=l2, cache_size3 = 0;
  124. bool need_help = false;
  125. for (int i=1; i<argc;)
  126. {
  127. if(argv[i][0]=='-')
  128. {
  129. if(argv[i][1]=='s')
  130. {
  131. ++i;
  132. s = atoi(argv[i++]);
  133. m = n = p = s;
  134. if(argv[i][0]!='-')
  135. {
  136. n = atoi(argv[i++]);
  137. p = atoi(argv[i++]);
  138. }
  139. }
  140. else if(argv[i][1]=='c')
  141. {
  142. ++i;
  143. cache_size1 = atoi(argv[i++]);
  144. if(argv[i][0]!='-')
  145. {
  146. cache_size2 = atoi(argv[i++]);
  147. if(argv[i][0]!='-')
  148. cache_size3 = atoi(argv[i++]);
  149. }
  150. }
  151. else if(argv[i][1]=='t')
  152. {
  153. ++i;
  154. tries = atoi(argv[i++]);
  155. }
  156. else if(argv[i][1]=='p')
  157. {
  158. ++i;
  159. rep = atoi(argv[i++]);
  160. }
  161. }
  162. else
  163. {
  164. need_help = true;
  165. break;
  166. }
  167. }
  168. if(need_help)
  169. {
  170. std::cout << argv[0] << " -s <matrix sizes> -c <cache sizes> -t <nb tries> -p <nb repeats>\n";
  171. std::cout << " <matrix sizes> : size\n";
  172. std::cout << " <matrix sizes> : rows columns depth\n";
  173. return 1;
  174. }
  175. #if EIGEN_VERSION_AT_LEAST(3,2,90)
  176. if(cache_size1>0)
  177. setCpuCacheSizes(cache_size1,cache_size2,cache_size3);
  178. #endif
  179. A a(m,p); a.setRandom();
  180. B b(p,n); b.setRandom();
  181. C c(m,n); c.setOnes();
  182. C rc = c;
  183. std::cout << "Matrix sizes = " << m << "x" << p << " * " << p << "x" << n << "\n";
  184. std::ptrdiff_t mc(m), nc(n), kc(p);
  185. internal::computeProductBlockingSizes<Scalar,Scalar>(kc, mc, nc);
  186. std::cout << "blocking size (mc x kc) = " << mc << " x " << kc << "\n";
  187. C r = c;
  188. // check the parallel product is correct
  189. #if defined EIGEN_HAS_OPENMP
  190. Eigen::initParallel();
  191. int procs = omp_get_max_threads();
  192. if(procs>1)
  193. {
  194. #ifdef HAVE_BLAS
  195. blas_gemm(a,b,r);
  196. #else
  197. omp_set_num_threads(1);
  198. r.noalias() += a * b;
  199. omp_set_num_threads(procs);
  200. #endif
  201. c.noalias() += a * b;
  202. if(!r.isApprox(c)) std::cerr << "Warning, your parallel product is crap!\n\n";
  203. }
  204. #elif defined HAVE_BLAS
  205. blas_gemm(a,b,r);
  206. c.noalias() += a * b;
  207. if(!r.isApprox(c)) {
  208. std::cout << (r - c).norm() << "\n";
  209. std::cerr << "Warning, your product is crap!\n\n";
  210. }
  211. #else
  212. if(1.*m*n*p<2000.*2000*2000)
  213. {
  214. gemm(a,b,c);
  215. r.noalias() += a.cast<Scalar>() .lazyProduct( b.cast<Scalar>() );
  216. if(!r.isApprox(c)) {
  217. std::cout << (r - c).norm() << "\n";
  218. std::cerr << "Warning, your product is crap!\n\n";
  219. }
  220. }
  221. #endif
  222. #ifdef HAVE_BLAS
  223. BenchTimer tblas;
  224. c = rc;
  225. BENCH(tblas, tries, rep, blas_gemm(a,b,c));
  226. std::cout << "blas cpu " << tblas.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tblas.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << tblas.total(CPU_TIMER) << "s)\n";
  227. std::cout << "blas real " << tblas.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tblas.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << tblas.total(REAL_TIMER) << "s)\n";
  228. #endif
  229. BenchTimer tmt;
  230. c = rc;
  231. BENCH(tmt, tries, rep, gemm(a,b,c));
  232. std::cout << "eigen cpu " << tmt.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tmt.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << tmt.total(CPU_TIMER) << "s)\n";
  233. std::cout << "eigen real " << tmt.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tmt.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << tmt.total(REAL_TIMER) << "s)\n";
  234. #ifdef EIGEN_HAS_OPENMP
  235. if(procs>1)
  236. {
  237. BenchTimer tmono;
  238. omp_set_num_threads(1);
  239. Eigen::setNbThreads(1);
  240. c = rc;
  241. BENCH(tmono, tries, rep, gemm(a,b,c));
  242. std::cout << "eigen mono cpu " << tmono.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tmono.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << tmono.total(CPU_TIMER) << "s)\n";
  243. std::cout << "eigen mono real " << tmono.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tmono.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << tmono.total(REAL_TIMER) << "s)\n";
  244. std::cout << "mt speed up x" << tmono.best(CPU_TIMER) / tmt.best(REAL_TIMER) << " => " << (100.0*tmono.best(CPU_TIMER) / tmt.best(REAL_TIMER))/procs << "%\n";
  245. }
  246. #endif
  247. if(1.*m*n*p<30*30*30)
  248. {
  249. BenchTimer tmt;
  250. c = rc;
  251. BENCH(tmt, tries, rep, c.noalias()+=a.lazyProduct(b));
  252. std::cout << "lazy cpu " << tmt.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tmt.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << tmt.total(CPU_TIMER) << "s)\n";
  253. std::cout << "lazy real " << tmt.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/tmt.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << tmt.total(REAL_TIMER) << "s)\n";
  254. }
  255. #ifdef DECOUPLED
  256. if((NumTraits<A::Scalar>::IsComplex) && (NumTraits<B::Scalar>::IsComplex))
  257. {
  258. M ar(m,p); ar.setRandom();
  259. M ai(m,p); ai.setRandom();
  260. M br(p,n); br.setRandom();
  261. M bi(p,n); bi.setRandom();
  262. M cr(m,n); cr.setRandom();
  263. M ci(m,n); ci.setRandom();
  264. BenchTimer t;
  265. BENCH(t, tries, rep, matlab_cplx_cplx(ar,ai,br,bi,cr,ci));
  266. std::cout << "\"matlab\" cpu " << t.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/t.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << t.total(CPU_TIMER) << "s)\n";
  267. std::cout << "\"matlab\" real " << t.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/t.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << t.total(REAL_TIMER) << "s)\n";
  268. }
  269. if((!NumTraits<A::Scalar>::IsComplex) && (NumTraits<B::Scalar>::IsComplex))
  270. {
  271. M a(m,p); a.setRandom();
  272. M br(p,n); br.setRandom();
  273. M bi(p,n); bi.setRandom();
  274. M cr(m,n); cr.setRandom();
  275. M ci(m,n); ci.setRandom();
  276. BenchTimer t;
  277. BENCH(t, tries, rep, matlab_real_cplx(a,br,bi,cr,ci));
  278. std::cout << "\"matlab\" cpu " << t.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/t.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << t.total(CPU_TIMER) << "s)\n";
  279. std::cout << "\"matlab\" real " << t.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/t.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << t.total(REAL_TIMER) << "s)\n";
  280. }
  281. if((NumTraits<A::Scalar>::IsComplex) && (!NumTraits<B::Scalar>::IsComplex))
  282. {
  283. M ar(m,p); ar.setRandom();
  284. M ai(m,p); ai.setRandom();
  285. M b(p,n); b.setRandom();
  286. M cr(m,n); cr.setRandom();
  287. M ci(m,n); ci.setRandom();
  288. BenchTimer t;
  289. BENCH(t, tries, rep, matlab_cplx_real(ar,ai,b,cr,ci));
  290. std::cout << "\"matlab\" cpu " << t.best(CPU_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/t.best(CPU_TIMER))*1e-9 << " GFLOPS \t(" << t.total(CPU_TIMER) << "s)\n";
  291. std::cout << "\"matlab\" real " << t.best(REAL_TIMER)/rep << "s \t" << (double(m)*n*p*rep*2/t.best(REAL_TIMER))*1e-9 << " GFLOPS \t(" << t.total(REAL_TIMER) << "s)\n";
  292. }
  293. #endif
  294. return 0;
  295. }