gemv_common.h 1.3 KB

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