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.

271 lines
9.0 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. #include <iostream>
  4. #include <Eigen/Core>
  5. #include <bench/BenchTimer.h>
  6. using namespace std;
  7. using namespace Eigen;
  8. #ifndef SCALAR
  9. // #define SCALAR std::complex<float>
  10. #define SCALAR float
  11. #endif
  12. typedef SCALAR Scalar;
  13. typedef NumTraits<Scalar>::Real RealScalar;
  14. typedef Matrix<RealScalar,Dynamic,Dynamic> A;
  15. typedef Matrix</*Real*/Scalar,Dynamic,Dynamic> B;
  16. typedef Matrix<Scalar,Dynamic,Dynamic> C;
  17. typedef Matrix<RealScalar,Dynamic,Dynamic> M;
  18. #ifdef HAVE_BLAS
  19. extern "C" {
  20. #include <bench/btl/libs/C_BLAS/blas.h>
  21. }
  22. static float fone = 1;
  23. static float fzero = 0;
  24. static double done = 1;
  25. static double szero = 0;
  26. static std::complex<float> cfone = 1;
  27. static std::complex<float> cfzero = 0;
  28. static std::complex<double> cdone = 1;
  29. static std::complex<double> cdzero = 0;
  30. static char notrans = 'N';
  31. static char trans = 'T';
  32. static char nonunit = 'N';
  33. static char lower = 'L';
  34. static char right = 'R';
  35. static int intone = 1;
  36. void blas_gemm(const MatrixXf& a, const MatrixXf& b, MatrixXf& c)
  37. {
  38. int M = c.rows(); int N = c.cols(); int K = a.cols();
  39. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  40. sgemm_(&notrans,&notrans,&M,&N,&K,&fone,
  41. const_cast<float*>(a.data()),&lda,
  42. const_cast<float*>(b.data()),&ldb,&fone,
  43. c.data(),&ldc);
  44. }
  45. EIGEN_DONT_INLINE void blas_gemm(const MatrixXd& a, const MatrixXd& b, MatrixXd& c)
  46. {
  47. int M = c.rows(); int N = c.cols(); int K = a.cols();
  48. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  49. dgemm_(&notrans,&notrans,&M,&N,&K,&done,
  50. const_cast<double*>(a.data()),&lda,
  51. const_cast<double*>(b.data()),&ldb,&done,
  52. c.data(),&ldc);
  53. }
  54. void blas_gemm(const MatrixXcf& a, const MatrixXcf& b, MatrixXcf& c)
  55. {
  56. int M = c.rows(); int N = c.cols(); int K = a.cols();
  57. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  58. cgemm_(&notrans,&notrans,&M,&N,&K,(float*)&cfone,
  59. const_cast<float*>((const float*)a.data()),&lda,
  60. const_cast<float*>((const float*)b.data()),&ldb,(float*)&cfone,
  61. (float*)c.data(),&ldc);
  62. }
  63. void blas_gemm(const MatrixXcd& a, const MatrixXcd& b, MatrixXcd& c)
  64. {
  65. int M = c.rows(); int N = c.cols(); int K = a.cols();
  66. int lda = a.rows(); int ldb = b.rows(); int ldc = c.rows();
  67. zgemm_(&notrans,&notrans,&M,&N,&K,(double*)&cdone,
  68. const_cast<double*>((const double*)a.data()),&lda,
  69. const_cast<double*>((const double*)b.data()),&ldb,(double*)&cdone,
  70. (double*)c.data(),&ldc);
  71. }
  72. #endif
  73. void matlab_cplx_cplx(const M& ar, const M& ai, const M& br, const M& bi, M& cr, M& ci)
  74. {
  75. cr.noalias() += ar * br;
  76. cr.noalias() -= ai * bi;
  77. ci.noalias() += ar * bi;
  78. ci.noalias() += ai * br;
  79. }
  80. void matlab_real_cplx(const M& a, const M& br, const M& bi, M& cr, M& ci)
  81. {
  82. cr.noalias() += a * br;
  83. ci.noalias() += a * bi;
  84. }
  85. void matlab_cplx_real(const M& ar, const M& ai, const M& b, M& cr, M& ci)
  86. {
  87. cr.noalias() += ar * b;
  88. ci.noalias() += ai * b;
  89. }
  90. template<typename A, typename B, typename C>
  91. EIGEN_DONT_INLINE void gemm(const A& a, const B& b, C& c)
  92. {
  93. c.noalias() += a * b;
  94. }
  95. int main(int argc, char ** argv)
  96. {
  97. std::ptrdiff_t l1 = internal::queryL1CacheSize();
  98. std::ptrdiff_t l2 = internal::queryTopLevelCacheSize();
  99. std::cout << "L1 cache size = " << (l1>0 ? l1/1024 : -1) << " KB\n";
  100. std::cout << "L2/L3 cache size = " << (l2>0 ? l2/1024 : -1) << " KB\n";
  101. typedef internal::gebp_traits<Scalar,Scalar> Traits;
  102. std::cout << "Register blocking = " << Traits::mr << " x " << Traits::nr << "\n";
  103. int rep = 1; // number of repetitions per try
  104. int tries = 2; // number of tries, we keep the best
  105. int s = 2048;
  106. int cache_size = -1;
  107. bool need_help = false;
  108. for (int i=1; i<argc; ++i)
  109. {
  110. if(argv[i][0]=='s')
  111. s = atoi(argv[i]+1);
  112. else if(argv[i][0]=='c')
  113. cache_size = atoi(argv[i]+1);
  114. else if(argv[i][0]=='t')
  115. tries = atoi(argv[i]+1);
  116. else if(argv[i][0]=='p')
  117. rep = atoi(argv[i]+1);
  118. else
  119. need_help = true;
  120. }
  121. if(need_help)
  122. {
  123. std::cout << argv[0] << " s<matrix size> c<cache size> t<nb tries> p<nb repeats>\n";
  124. return 1;
  125. }
  126. if(cache_size>0)
  127. setCpuCacheSizes(cache_size,96*cache_size);
  128. int m = s;
  129. int n = s;
  130. int p = s;
  131. A a(m,p); a.setRandom();
  132. B b(p,n); b.setRandom();
  133. C c(m,n); c.setOnes();
  134. C rc = c;
  135. std::cout << "Matrix sizes = " << m << "x" << p << " * " << p << "x" << n << "\n";
  136. std::ptrdiff_t mc(m), nc(n), kc(p);
  137. internal::computeProductBlockingSizes<Scalar,Scalar>(kc, mc, nc);
  138. std::cout << "blocking size (mc x kc) = " << mc << " x " << kc << "\n";
  139. C r = c;
  140. // check the parallel product is correct
  141. #if defined EIGEN_HAS_OPENMP
  142. int procs = omp_get_max_threads();
  143. if(procs>1)
  144. {
  145. #ifdef HAVE_BLAS
  146. blas_gemm(a,b,r);
  147. #else
  148. omp_set_num_threads(1);
  149. r.noalias() += a * b;
  150. omp_set_num_threads(procs);
  151. #endif
  152. c.noalias() += a * b;
  153. if(!r.isApprox(c)) std::cerr << "Warning, your parallel product is crap!\n\n";
  154. }
  155. #elif defined HAVE_BLAS
  156. blas_gemm(a,b,r);
  157. c.noalias() += a * b;
  158. if(!r.isApprox(c)) std::cerr << "Warning, your product is crap!\n\n";
  159. #else
  160. gemm(a,b,c);
  161. r.noalias() += a.cast<Scalar>() * b.cast<Scalar>();
  162. if(!r.isApprox(c)) std::cerr << "Warning, your product is crap!\n\n";
  163. #endif
  164. #ifdef HAVE_BLAS
  165. BenchTimer tblas;
  166. c = rc;
  167. BENCH(tblas, tries, rep, blas_gemm(a,b,c));
  168. 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";
  169. 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";
  170. #endif
  171. BenchTimer tmt;
  172. c = rc;
  173. BENCH(tmt, tries, rep, gemm(a,b,c));
  174. 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";
  175. 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";
  176. #ifdef EIGEN_HAS_OPENMP
  177. if(procs>1)
  178. {
  179. BenchTimer tmono;
  180. omp_set_num_threads(1);
  181. Eigen::internal::setNbThreads(1);
  182. c = rc;
  183. BENCH(tmono, tries, rep, gemm(a,b,c));
  184. 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";
  185. 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";
  186. 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";
  187. }
  188. #endif
  189. #ifdef DECOUPLED
  190. if((NumTraits<A::Scalar>::IsComplex) && (NumTraits<B::Scalar>::IsComplex))
  191. {
  192. M ar(m,p); ar.setRandom();
  193. M ai(m,p); ai.setRandom();
  194. M br(p,n); br.setRandom();
  195. M bi(p,n); bi.setRandom();
  196. M cr(m,n); cr.setRandom();
  197. M ci(m,n); ci.setRandom();
  198. BenchTimer t;
  199. BENCH(t, tries, rep, matlab_cplx_cplx(ar,ai,br,bi,cr,ci));
  200. 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";
  201. 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";
  202. }
  203. if((!NumTraits<A::Scalar>::IsComplex) && (NumTraits<B::Scalar>::IsComplex))
  204. {
  205. M a(m,p); a.setRandom();
  206. M br(p,n); br.setRandom();
  207. M bi(p,n); bi.setRandom();
  208. M cr(m,n); cr.setRandom();
  209. M ci(m,n); ci.setRandom();
  210. BenchTimer t;
  211. BENCH(t, tries, rep, matlab_real_cplx(a,br,bi,cr,ci));
  212. 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";
  213. 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";
  214. }
  215. if((NumTraits<A::Scalar>::IsComplex) && (!NumTraits<B::Scalar>::IsComplex))
  216. {
  217. M ar(m,p); ar.setRandom();
  218. M ai(m,p); ai.setRandom();
  219. M b(p,n); b.setRandom();
  220. M cr(m,n); cr.setRandom();
  221. M ci(m,n); ci.setRandom();
  222. BenchTimer t;
  223. BENCH(t, tries, rep, matlab_cplx_real(ar,ai,b,cr,ci));
  224. 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";
  225. 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";
  226. }
  227. #endif
  228. return 0;
  229. }