lazy_gemm.cpp
Go to the documentation of this file.
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>
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>
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 
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::string filename = std::string("lazy_gemm_settings.txt");
88  if(argc>1)
89  filename = std::string(argv[1]);
90  std::ifstream settings(filename);
91  long m, n, k, t;
92  while(settings >> m >> n >> k >> t)
93  {
94  //std::cerr << " Testing " << m << " " << n << " " << k << std::endl;
95  results.push_back( bench_mnk(m, n, k, t) );
96  }
97 
98  std::cout << RowVectorXd::Map(results.data(), results.size());
99 
100  return 0;
101 }
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
lazy_gemm
EIGEN_DONT_INLINE void lazy_gemm(const MatA &A, const MatB &B, MatC &C)
Definition: lazy_gemm.cpp:16
bench
EIGEN_DONT_INLINE double bench()
Definition: lazy_gemm.cpp:26
bench_mnk
EIGEN_DONT_INLINE double bench_mnk(int m, int n, int k, int t)
Definition: lazy_gemm.cpp:63
main
int main(int argc, char **argv)
Definition: lazy_gemm.cpp:83
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
n
int n
Definition: BiCGSTAB_simple.cpp:1
bench_t
double bench_t(int t)
Definition: lazy_gemm.cpp:54
relicense.filename
filename
Definition: relicense.py:57
Eigen::BenchTimer
Definition: BenchTimer.h:59
BENCH
#define BENCH(TIMER, TRIES, REP, CODE)
Definition: BenchTimer.h:174
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Scalar
SCALAR Scalar
Definition: lazy_gemm.cpp:12
SCALAR
#define SCALAR
Definition: bench_gemm.cpp:23
C
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
results
std::map< std::string, Array< float, 1, 8, DontAlign|RowMajor > > results
Definition: dense_solvers.cpp:10
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
Eigen::PlainObjectBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:562
align_3::t
Point2 t(10, 10)
max
#define max(a, b)
Definition: datatypes.h:20
EIGEN_DONT_INLINE
#define EIGEN_DONT_INLINE
Definition: Macros.h:940


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:53