You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
11 KiB

  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 <StormEigen/Core>
  12. #include <bench/BenchTimer.h>
  13. using namespace std;
  14. using namespace StormEigen;
  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 <StormEigen/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. STORMEIGEN_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. }
  93. void matlab_real_cplx(const M& a, const M& br, const M& bi, M& cr, M& ci)
  94. {
  95. cr.noalias() += a * br;
  96. ci.noalias() += a * bi;
  97. }
  98. void matlab_cplx_real(const M& ar, const M& ai, const M& b, M& cr, M& ci)
  99. {
  100. cr.noalias() += ar * b;
  101. ci.noalias() += ai * b;
  102. }
  103. template<typename A, typename B, typename C>
  104. STORMEIGEN_DONT_INLINE void gemm(const A& a, const B& b, C& c)
  105. {
  106. c.noalias() += a * b;
  107. }
  108. int main(int argc, char ** argv)
  109. {
  110. std::ptrdiff_t l1 = internal::queryL1CacheSize();
  111. std::ptrdiff_t l2 = internal::queryTopLevelCacheSize();
  112. std::cout << "L1 cache size = " << (l1>0 ? l1/1024 : -1) << " KB\n";
  113. std::cout << "L2/L3 cache size = " << (l2>0 ? l2/1024 : -1) << " KB\n";
  114. typedef internal::gebp_traits<Scalar,Scalar> Traits;
  115. std::cout << "Register blocking = " << Traits::mr << " x " << Traits::nr << "\n";
  116. int rep = 1; // number of repetitions per try
  117. int tries = 2; // number of tries, we keep the best
  118. int s = 2048;
  119. int m = s;
  120. int n = s;
  121. int p = s;
  122. int cache_size1=-1, cache_size2=l2, cache_size3 = 0;
  123. bool need_help = false;
  124. for (int i=1; i<argc;)
  125. {
  126. if(argv[i][0]=='-')
  127. {
  128. if(argv[i][1]=='s')
  129. {
  130. ++i;
  131. s = atoi(argv[i++]);
  132. m = n = p = s;
  133. if(argv[i][0]!='-')
  134. {
  135. n = atoi(argv[i++]);
  136. p = atoi(argv[i++]);
  137. }
  138. }
  139. else if(argv[i][1]=='c')
  140. {
  141. ++i;
  142. cache_size1 = atoi(argv[i++]);
  143. if(argv[i][0]!='-')
  144. {
  145. cache_size2 = atoi(argv[i++]);
  146. if(argv[i][0]!='-')
  147. cache_size3 = atoi(argv[i++]);
  148. }
  149. }
  150. else if(argv[i][1]=='t')
  151. {
  152. ++i;
  153. tries = atoi(argv[i++]);
  154. }
  155. else if(argv[i][1]=='p')
  156. {
  157. ++i;
  158. rep = atoi(argv[i++]);
  159. }
  160. }
  161. else
  162. {
  163. need_help = true;
  164. break;
  165. }
  166. }
  167. if(need_help)
  168. {
  169. std::cout << argv[0] << " -s <matrix sizes> -c <cache sizes> -t <nb tries> -p <nb repeats>\n";
  170. std::cout << " <matrix sizes> : size\n";
  171. std::cout << " <matrix sizes> : rows columns depth\n";
  172. return 1;
  173. }
  174. #if STORMEIGEN_VERSION_AT_LEAST(3,2,90)
  175. if(cache_size1>0)
  176. setCpuCacheSizes(cache_size1,cache_size2,cache_size3);
  177. #endif
  178. A a(m,p); a.setRandom();
  179. B b(p,n); b.setRandom();
  180. C c(m,n); c.setOnes();
  181. C rc = c;
  182. std::cout << "Matrix sizes = " << m << "x" << p << " * " << p << "x" << n << "\n";
  183. std::ptrdiff_t mc(m), nc(n), kc(p);
  184. internal::computeProductBlockingSizes<Scalar,Scalar>(kc, mc, nc);
  185. std::cout << "blocking size (mc x kc) = " << mc << " x " << kc << "\n";
  186. C r = c;
  187. // check the parallel product is correct
  188. #if defined STORMEIGEN_HAS_OPENMP
  189. StormEigen::initParallel();
  190. int procs = omp_get_max_threads();
  191. if(procs>1)
  192. {
  193. #ifdef HAVE_BLAS
  194. blas_gemm(a,b,r);
  195. #else
  196. omp_set_num_threads(1);
  197. r.noalias() += a * b;
  198. omp_set_num_threads(procs);
  199. #endif
  200. c.noalias() += a * b;
  201. if(!r.isApprox(c)) std::cerr << "Warning, your parallel product is crap!\n\n";
  202. }
  203. #elif defined HAVE_BLAS
  204. blas_gemm(a,b,r);
  205. c.noalias() += a * b;
  206. if(!r.isApprox(c)) {
  207. std::cout << r - c << "\n";
  208. std::cerr << "Warning, your product is crap!\n\n";
  209. }
  210. #else
  211. if(1.*m*n*p<2000.*2000*2000)
  212. {
  213. gemm(a,b,c);
  214. r.noalias() += a.cast<Scalar>() .lazyProduct( b.cast<Scalar>() );
  215. if(!r.isApprox(c)) {
  216. std::cout << r - c << "\n";
  217. std::cerr << "Warning, your product is crap!\n\n";
  218. }
  219. }
  220. #endif
  221. #ifdef HAVE_BLAS
  222. BenchTimer tblas;
  223. c = rc;
  224. BENCH(tblas, tries, rep, blas_gemm(a,b,c));
  225. 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";
  226. 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";
  227. #endif
  228. BenchTimer tmt;
  229. c = rc;
  230. BENCH(tmt, tries, rep, gemm(a,b,c));
  231. 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";
  232. 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";
  233. #ifdef STORMEIGEN_HAS_OPENMP
  234. if(procs>1)
  235. {
  236. BenchTimer tmono;
  237. omp_set_num_threads(1);
  238. StormEigen::setNbThreads(1);
  239. c = rc;
  240. BENCH(tmono, tries, rep, gemm(a,b,c));
  241. 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";
  242. 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";
  243. 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";
  244. }
  245. #endif
  246. if(1.*m*n*p<30*30*30)
  247. {
  248. BenchTimer tmt;
  249. c = rc;
  250. BENCH(tmt, tries, rep, c.noalias()+=a.lazyProduct(b));
  251. 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";
  252. 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";
  253. }
  254. #ifdef DECOUPLED
  255. if((NumTraits<A::Scalar>::IsComplex) && (NumTraits<B::Scalar>::IsComplex))
  256. {
  257. M ar(m,p); ar.setRandom();
  258. M ai(m,p); ai.setRandom();
  259. M br(p,n); br.setRandom();
  260. M bi(p,n); bi.setRandom();
  261. M cr(m,n); cr.setRandom();
  262. M ci(m,n); ci.setRandom();
  263. BenchTimer t;
  264. BENCH(t, tries, rep, matlab_cplx_cplx(ar,ai,br,bi,cr,ci));
  265. 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";
  266. 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";
  267. }
  268. if((!NumTraits<A::Scalar>::IsComplex) && (NumTraits<B::Scalar>::IsComplex))
  269. {
  270. M a(m,p); a.setRandom();
  271. M br(p,n); br.setRandom();
  272. M bi(p,n); bi.setRandom();
  273. M cr(m,n); cr.setRandom();
  274. M ci(m,n); ci.setRandom();
  275. BenchTimer t;
  276. BENCH(t, tries, rep, matlab_real_cplx(a,br,bi,cr,ci));
  277. 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";
  278. 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";
  279. }
  280. if((NumTraits<A::Scalar>::IsComplex) && (!NumTraits<B::Scalar>::IsComplex))
  281. {
  282. M ar(m,p); ar.setRandom();
  283. M ai(m,p); ai.setRandom();
  284. M b(p,n); b.setRandom();
  285. M cr(m,n); cr.setRandom();
  286. M ci(m,n); ci.setRandom();
  287. BenchTimer t;
  288. BENCH(t, tries, rep, matlab_cplx_real(ar,ai,b,cr,ci));
  289. 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";
  290. 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";
  291. }
  292. #endif
  293. return 0;
  294. }