gemm_common.h 1.3 KB

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