Home | History | Annotate | Download | only in gemm
      1 #include <iostream>
      2 #include <fstream>
      3 #include <vector>
      4 #include <Eigen/Core>
      5 #include "../../BenchTimer.h"
      6 using namespace Eigen;
      7 
      8 #ifndef SCALAR
      9 #error SCALAR must be defined
     10 #endif
     11 
     12 typedef SCALAR Scalar;
     13 
     14 template<typename MatA, typename MatB, typename MatC>
     15 EIGEN_DONT_INLINE
     16 void lazy_gemm(const MatA &A, const MatB &B, MatC &C)
     17 {
     18 //   escape((void*)A.data());
     19 //   escape((void*)B.data());
     20   C.noalias() += A.lazyProduct(B);
     21 //   escape((void*)C.data());
     22 }
     23 
     24 template<int m, int n, int k, int TA>
     25 EIGEN_DONT_INLINE
     26 double bench()
     27 {
     28   typedef Matrix<Scalar,m,k,TA> MatA;
     29   typedef Matrix<Scalar,k,n> MatB;
     30   typedef Matrix<Scalar,m,n> MatC;
     31 
     32   MatA A(m,k);
     33   MatB B(k,n);
     34   MatC C(m,n);
     35   A.setRandom();
     36   B.setRandom();
     37   C.setZero();
     38 
     39   BenchTimer t;
     40 
     41   double up = 1e7*4/sizeof(Scalar);
     42   double tm0 = 10, tm1 = 20;
     43 
     44   double flops = 2. * m * n * k;
     45   long rep = std::max(10., std::min(10000., up/flops) );
     46   long tries = std::max(tm0, std::min(tm1, up/flops) );
     47 
     48   BENCH(t, tries, rep, lazy_gemm(A,B,C));
     49 
     50   return 1e-9 * rep * flops / t.best();
     51 }
     52 
     53 template<int m, int n, int k>
     54 double bench_t(int t)
     55 {
     56   if(t)
     57     return bench<m,n,k,RowMajor>();
     58   else
     59     return bench<m,n,k,0>();
     60 }
     61 
     62 EIGEN_DONT_INLINE
     63 double bench_mnk(int m, int n, int k, int t)
     64 {
     65   int id = m*10000 + n*100 + k;
     66   switch(id) {
     67     case  10101 : return bench_t< 1, 1, 1>(t); break;
     68     case  20202 : return bench_t< 2, 2, 2>(t); break;
     69     case  30303 : return bench_t< 3, 3, 3>(t); break;
     70     case  40404 : return bench_t< 4, 4, 4>(t); break;
     71     case  50505 : return bench_t< 5, 5, 5>(t); break;
     72     case  60606 : return bench_t< 6, 6, 6>(t); break;
     73     case  70707 : return bench_t< 7, 7, 7>(t); break;
     74     case  80808 : return bench_t< 8, 8, 8>(t); break;
     75     case  90909 : return bench_t< 9, 9, 9>(t); break;
     76     case 101010 : return bench_t<10,10,10>(t); break;
     77     case 111111 : return bench_t<11,11,11>(t); break;
     78     case 121212 : return bench_t<12,12,12>(t); break;
     79   }
     80   return 0;
     81 }
     82 
     83 int main(int argc, char **argv)
     84 {
     85   std::vector<double> results;
     86 
     87   std::ifstream settings("lazy_gemm_settings.txt");
     88   long m, n, k, t;
     89   while(settings >> m >> n >> k >> t)
     90   {
     91     //std::cerr << "  Testing " << m << " " << n << " " << k << std::endl;
     92     results.push_back( bench_mnk(m, n, k, t) );
     93   }
     94 
     95   std::cout << RowVectorXd::Map(results.data(), results.size());
     96 
     97   return 0;
     98 }
     99