cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

gemv_common.h (1381B)


      1 #include <iostream>
      2 #include <fstream>
      3 #include <vector>
      4 #include <string>
      5 #include <functional>
      6 #include "eigen_src/Eigen/Core"
      7 #include "../BenchTimer.h"
      8 using namespace Eigen;
      9 
     10 #ifndef SCALAR
     11 #error SCALAR must be defined
     12 #endif
     13 
     14 typedef SCALAR Scalar;
     15 
     16 typedef Matrix<Scalar,Dynamic,Dynamic> Mat;
     17 typedef Matrix<Scalar,Dynamic,1>       Vec;
     18 
     19 template<typename Func>
     20 EIGEN_DONT_INLINE
     21 double bench(long m, long n, Func &f)
     22 {
     23   Mat A(m,n);
     24   Vec B(n);
     25   Vec C(m);
     26   A.setRandom();
     27   B.setRandom();
     28   C.setRandom();
     29 
     30   BenchTimer t;
     31 
     32   double up = 1e8/sizeof(Scalar);
     33   double tm0 = 4, tm1 = 10;
     34   if(NumTraits<Scalar>::IsComplex)
     35   {
     36     up /= 4;
     37     tm0 = 2;
     38     tm1 = 4;
     39   }
     40 
     41   double flops = 2. * m * n;
     42   long rep = std::max(1., std::min(100., up/flops) );
     43   long tries = std::max(tm0, std::min(tm1, up/flops) );
     44 
     45   BENCH(t, tries, rep, f(A,B,C));
     46 
     47   return 1e-9 * rep * flops / t.best();
     48 }
     49 
     50 template<typename Func>
     51 int main_gemv(int argc, char **argv, Func& f)
     52 {
     53   std::vector<double> results;
     54 
     55   std::string filename = std::string("gemv_settings.txt");
     56   if(argc>1)
     57     filename = std::string(argv[1]);
     58   std::ifstream settings(filename);
     59   long m, n;
     60   while(settings >> m >> n)
     61   {
     62     //std::cerr << "  Testing " << m << " " << n << std::endl;
     63     results.push_back( bench(m, n, f) );
     64   }
     65 
     66   std::cout << RowVectorXd::Map(results.data(), results.size());
     67 
     68   return 0;
     69 }